package com.hdl.photovoltaic.other; import android.os.SystemClock; import android.text.TextUtils; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.reflect.TypeToken; import com.hdl.linkpm.sdk.core.exception.HDLException; import com.hdl.photovoltaic.bean.InverterBean; import com.hdl.photovoltaic.bean.LocalResponse; import com.hdl.photovoltaic.bean.PageNumberObject; import com.hdl.photovoltaic.config.AppConfigManage; import com.hdl.photovoltaic.config.UserConfigManage; import com.hdl.photovoltaic.internet.HttpClient; import com.hdl.photovoltaic.internet.TcpClient; import com.hdl.photovoltaic.internet.api.HttpApi; import com.hdl.photovoltaic.internet.api.TopicApi; import com.hdl.photovoltaic.listener.CloudCallBeak; import com.hdl.photovoltaic.listener.LinkCallBack; import com.hdl.photovoltaic.ui.bean.CloudInverterChildDeviceBean; import com.hdl.photovoltaic.ui.bean.CloudInverterDeviceBean; import com.hdl.photovoltaic.ui.bean.DeviceInfoBean; import com.hdl.photovoltaic.ui.bean.DeviceRemoteInfo; import com.hdl.photovoltaic.ui.bean.DeviceTimeBean; import com.hdl.photovoltaic.ui.bean.NotRunBean; import com.hdl.photovoltaic.ui.bean.OidBean; import com.hdl.sdk.link.common.exception.HDLLinkException; import com.hdl.sdk.link.core.bean.gateway.GatewayBean; import com.hdl.sdk.link.core.bean.response.BaseLocalResponse; import com.hdl.sdk.link.core.callback.GatewayCallBack; import com.hdl.sdk.link.core.callback.HDLLinkCallBack; import com.hdl.sdk.link.core.config.HDLLinkConfig; import com.hdl.sdk.link.gateway.HDLLinkLocalGateway; import com.hdl.sdk.link.gateway.type.GatewayMasterType; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Objects; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; /** * 设备逻辑 */ public class HdlDeviceLogic { private static volatile HdlDeviceLogic sHdlDeviceLogic; /** * 获取当前对象 * * @return HdlDeviceLogic */ public static synchronized HdlDeviceLogic getInstance() { if (sHdlDeviceLogic == null) { synchronized (HdlDeviceLogic.class) { if (sHdlDeviceLogic == null) { sHdlDeviceLogic = new HdlDeviceLogic(); } } } return sHdlDeviceLogic; } private List mListDevice = new ArrayList<>(); public void clearDeviceList() { if (mListDevice == null) { return; } if (mListDevice.size() > 0) { mListDevice.clear(); } } public List getDeviceList() { if (mListDevice == null) { return new ArrayList<>(); } return mListDevice; } /** * 获取当前住宅的逆变器列表(包括从的逆变器) * * @param homeId 电站ID * @param cloudInverterDeviceList 元端上的逆变器列表(并网,离网,负载中心) */ public List getCurrentHomeGatewayList(String homeId, List cloudInverterDeviceList) { try { List newList = new ArrayList<>(); //云端有设备列表,本地没有,即HDLLinkLocalGateway.getInstance().getGatewayList()没有,里面会调用refreshGatewayCacheData()方法虚拟一个逆变器实体添加本地列表里面; List list = HDLLinkLocalGateway.getInstance().getGatewayList(); if (list.size() > 0) { for (int i = 0; i < list.size(); i++) { GatewayBean gatewayBean = list.get(i); if (!getGatewaySpk().contains(gatewayBean.getGatewayType())) { continue; } if (TextUtils.isEmpty(gatewayBean.getDevice_mac()) || TextUtils.isEmpty(gatewayBean.getSid()) || TextUtils.isEmpty(gatewayBean.getOid())) { continue; } //用homeId筛选当前住宅的逆变器列表 if (gatewayBean.getHomeId().equals(homeId)) { newList.add(gatewayBean); } } } //有外网,以云端设备列表为准;云端没有,本地存在,则删除本地;否则,没有外网只有局域网,以本地为主,搜索多少个设备就显示多少个 if (cloudInverterDeviceList != null && cloudInverterDeviceList.size() > 0) { List checkNewList = new ArrayList<>(); for (int i = 0; i < cloudInverterDeviceList.size(); i++) { CloudInverterDeviceBean cloudInverterDevice = cloudInverterDeviceList.get(i); for (GatewayBean gatewayBean : newList) { if (cloudInverterDevice.getOsn().equals(gatewayBean.getDevice_mac())) { checkNewList.add(gatewayBean); break; } } } newList.clear(); newList.addAll(checkNewList); } // // 模拟设备列表数据 // List mockData = getGatewayDeviceListMockData(3); // if (mockData.size() > 0) { // for (GatewayBean mockDatum : mockData) { // newList.add(0, mockDatum); // } // } return newList; } catch (Exception e) { return new ArrayList<>(); } } /** * 添加逆变器到缓存列表里面去 * * @param homeId 电站id * @param newGatewayBean 逆变器对象 */ public void addGatewayToLocalCacheMemory(String homeId, GatewayBean newGatewayBean) { if (newGatewayBean == null || TextUtils.isEmpty(homeId)) { return; } List list = getCurrentHomeGatewayList(homeId, null); if (list == null || list.size() == 0) { return; } int index = -1; for (int i = 0; i < list.size(); i++) { GatewayBean gatewayBean = list.get(i); if (gatewayBean.getDevice_mac().equals(newGatewayBean.getDevice_mac())) { index = i; break; } } if (index > 0) { list.remove(index); list.add(index, newGatewayBean); } } /** * 移除缓存列表里面逆变器 * * @param homeId 电站id * @param mac 逆变器mac */ public void removeLocalCacheMemoryGatewayToMac(String homeId, String mac) { if (TextUtils.isEmpty(mac)) { return; } List list = getCurrentHomeGatewayList(homeId, null); if (list == null || list.size() == 0) { return; } int index = -1; for (int i = 0; i < list.size(); i++) { GatewayBean gatewayBean = list.get(i); if (gatewayBean.getDevice_mac().equals(mac)) { index = i; break; } } if (index > 0) { list.remove(index); } } /** * 移除缓存列表里面逆变器 * * @param homeId 电站id * @param deviceId 逆变器deviceId */ public void removeLocalCacheMemoryGatewayToDeviceId(String homeId, String deviceId) { if (TextUtils.isEmpty(deviceId)) { return; } List list = getCurrentHomeGatewayList(homeId, null); if (list == null || list.size() == 0) { return; } int index = -1; for (int i = 0; i < list.size(); i++) { GatewayBean gatewayBean = list.get(i); if (gatewayBean.getDeviceId().equals(deviceId)) { index = i; break; } } if (index > 0) { list.remove(index); } } /** * 添加【设备列表】到本地缓存 * * @param list -设备列表 */ public void setListDevice(List list) { try { if (list == null || list.size() == 0) { return; } if (this.mListDevice.size() == 0) { this.mListDevice.addAll(list); return; } for (int i = 0; i < list.size(); i++) { this.setSingleDevice(list.get(i)); } } catch (Exception ignored) { } } /** * 获取设备列表(安装商) * * @param searchTxt 搜索内容 * @param pageNo 页码 * @param pageSize 页数 */ public void getPowerStationDeviceList(String searchTxt, long pageNo, long pageSize, CloudCallBeak> cloudCallBeak) { String requestUrl = HttpApi.POST_deviceList; JsonObject json = new JsonObject(); if (!TextUtils.isEmpty(searchTxt)) { json.addProperty("searchTxt", searchTxt); } json.addProperty("pageNo", pageNo);//页码 json.addProperty("pageSize", pageSize);//页数 // json.addProperty("zoneType", zoneType);//区域 HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak() { @Override public void onSuccess(String jsonStr) { if (TextUtils.isEmpty(jsonStr)) { if (cloudCallBeak != null) { cloudCallBeak.onSuccess(new PageNumberObject<>()); } } Gson gson = new Gson(); Type type = new TypeToken>() { }.getType(); PageNumberObject pageNumberObject = gson.fromJson(jsonStr, type); if (cloudCallBeak != null) { cloudCallBeak.onSuccess(pageNumberObject); } } @Override public void onFailure(HDLException e) { if (cloudCallBeak != null) { cloudCallBeak.onFailure(e); } } }); } /** * 添加设备到列表里面 * * @param deviceBean -设备对象 */ public void setSingleDevice(CloudInverterDeviceBean deviceBean) { try { if (deviceBean == null) { return; } boolean if_boolean = false; for (int i = 0; i < mListDevice.size(); i++) { if (mListDevice.get(i).getOsn().equals(deviceBean.getOsn())) { //存在替换 mListDevice.remove(i); mListDevice.add(i, deviceBean); if_boolean = true; break; } } if (!if_boolean) { //没有添加 this.mListDevice.add(deviceBean); } } catch (Exception e) { String mes = e.getMessage(); HdlLogLogic.print("---" + mes); } } /** * 逆变器上传数据到云端(包括:sid,oid) * * @param homeId 住宅id * @param list 逆变器列表 * @param whetherToAdd true表示用增量,false表示用全量 */ public void uploadOidDataToCloud(String homeId, List list, boolean whetherToAdd, CloudCallBeak callBeak) { if (list == null || list.isEmpty()) { return; } if (whetherToAdd) { for (int i = 0; i < list.size(); i++) { GatewayBean gatewayBean = list.get(i); if (gatewayBean == null || TextUtils.isEmpty(gatewayBean.getDevice_mac())) { continue; } getInverterOidList(gatewayBean.getDevice_mac(), new LinkCallBack>() { @Override public void onSuccess(List oidBeanList) { if (oidBeanList == null) { HdlLogLogic.print("上传oid列表到云端成功---住宅id:" + homeId + "---mac:" + gatewayBean.getDevice_mac() + "---数据是null", true); return; } //增量添加oid updateOidAdd(homeId, oidBeanList, new CloudCallBeak() { @Override public void onSuccess(Boolean obj) { HdlLogLogic.print("上传oid列表到云端成功(增量)---住宅id:" + homeId + "---mac:" + gatewayBean.getDevice_mac() + "---\r\n数据---" + new Gson().toJson(oidBeanList), true); } @Override public void onFailure(HDLException e) { HdlLogLogic.print("上传oid列表到云端失败(增量)---住宅id:" + homeId + "---mac:" + gatewayBean.getDevice_mac() + "-->" + e.getMsg() + "(" + e.getCode() + ")", true); } }); } @Override public void onError(HDLLinkException e) { HdlLogLogic.print("获取oid列表失败---住宅id:" + homeId + "---mac:" + gatewayBean.getDevice_mac() + "-->" + e.getMsg() + "(" + e.getCode() + ")", true); } }); SystemClock.sleep(200); } } else { //只有进入详情界面 List newOidList = new ArrayList<>(); //表示中间任何一个获取oid失败。都要改变用增量上传oid列表 AtomicBoolean isIncrementalOidAddition = new AtomicBoolean(false); AtomicInteger atomicInteger = new AtomicInteger(0); for (int i = 0; i < list.size(); i++) { GatewayBean gatewayBean = list.get(i); if (gatewayBean == null || TextUtils.isEmpty(gatewayBean.getDevice_mac())) { // isIncrementalOidAddition.set(true); continue; } getInverterOidList(gatewayBean.getDevice_mac(), new LinkCallBack>() { @Override public void onSuccess(List oidBeanList) { atomicInteger.set(atomicInteger.get() + 1); if (oidBeanList == null) { // isIncrementalOidAddition.set(true); return; } newOidList.addAll(oidBeanList); if (atomicInteger.get() == list.size()) { if (newOidList.isEmpty()) { return; } if (isIncrementalOidAddition.get()) { //增量添加oid updateOidAdd(homeId, newOidList, new CloudCallBeak() { @Override public void onSuccess(Boolean obj) { HdlLogLogic.print("上传oid列表到云端成功(增量)---住宅id:" + homeId + "---mac:" + gatewayBean.getDevice_mac() + "---\r\n数据---" + new Gson().toJson(oidBeanList), true); } @Override public void onFailure(HDLException e) { HdlLogLogic.print("上传oid列表到云端失败---住宅id:" + homeId + "---mac:" + gatewayBean.getDevice_mac() + "-->" + e.getMsg() + "(" + e.getCode() + ")", true); } }); } else { //全部获取后再上传 fullUpdateOid(homeId, newOidList, new CloudCallBeak() { @Override public void onSuccess(Boolean obj) { HdlLogLogic.print("上传oid列表到云端成功(全量)---住宅id:" + homeId + "---\r\n数据---" + new Gson().toJson(newOidList), true); } @Override public void onFailure(HDLException e) { HdlLogLogic.print("上传oid列表到云端失败(全量)---住宅id:" + homeId + "-->" + e.getMsg() + "(" + e.getCode() + ")", true); } }); } } } @Override public void onError(HDLLinkException e) { isIncrementalOidAddition.set(true); atomicInteger.set(atomicInteger.get() + 1); if (atomicInteger.get() == list.size()) { if (newOidList.isEmpty()) { HdlLogLogic.print("获取逆变器oid列表失败---住宅id:" + homeId + "---mac:" + gatewayBean.getDevice_mac() + "-->" + e.getMsg() + "(" + e.getCode() + ")", true); return; } //增量添加oid updateOidAdd(homeId, newOidList, new CloudCallBeak() { @Override public void onSuccess(Boolean obj) { HdlLogLogic.print("上传oid列表到云端成功(增量)---住宅id:" + homeId + "---mac:" + gatewayBean.getDevice_mac() + "---\r\n数据---" + new Gson().toJson(newOidList), true); } @Override public void onFailure(HDLException e) { HdlLogLogic.print("上传oid列表到云端失败(增量)---住宅id:" + homeId + "---mac:" + gatewayBean.getDevice_mac() + "-->" + e.getMsg() + "(" + e.getCode() + ")", true); } }); } } }); SystemClock.sleep(200); } } } /** * 添加逆变器到云端上 * * @param mac - * @param spk - * @param sid - * @param oid - * @param name - * @param cloudCallBeak - */ public void addInverterDeviceToCloud(String homeId, String mac, String spk, String sid, String oid, String name, CloudCallBeak cloudCallBeak) { String requestUrl = HttpApi.POST_Device_Add; JsonObject json = new JsonObject(); json.addProperty("homeId", homeId); json.addProperty("mac", mac); json.addProperty("spk", spk); json.addProperty("sid", sid); json.addProperty("oid", oid); json.addProperty("name", name); // json.addProperty("zoneType", "password");//区域 HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak() { @Override public void onSuccess(String str) { if (cloudCallBeak != null) { cloudCallBeak.onSuccess(true); List list = new ArrayList<>(); GatewayBean gatewayBean = new GatewayBean(); gatewayBean.setHomeId(homeId); gatewayBean.setDevice_mac(mac); gatewayBean.setSpk(spk); gatewayBean.setSid(sid); gatewayBean.setOid(oid); gatewayBean.setDevice_name(name); list.add(gatewayBean); //添加设备成功后,上传oid列表到云端 uploadOidDataToCloud(homeId, list, true, null); } } @Override public void onFailure(HDLException e) { //添加逆变器失败后初始化逆变器 HdlLogLogic.print("添加逆变器失败后开始初始化逆变器-->", true); HdlDeviceLogic.getInstance().initializeInverter(mac, new LinkCallBack() { @Override public void onSuccess(Boolean obj) { HdlLogLogic.print("初始化逆变器成功-->mac:" + mac, true); } @Override public void onError(HDLLinkException e) { HdlLogLogic.print("初始化逆变器失败-->mac:" + mac, true); } }); if (cloudCallBeak != null) { cloudCallBeak.onFailure(e); } } }); } /** * 添加多个逆变器到云端上 * * @param list 逆变器列表 * @param cloudCallBeak - */ public void addAllInverterDeviceToCloud(String homeId, List list, CloudCallBeak cloudCallBeak) { String requestUrl = HttpApi.POST_Device_Add_All; JsonObject json = new JsonObject(); json.addProperty("homeId", homeId); JsonArray ary = new JsonArray(); for (int i = 0; i < list.size(); i++) { GatewayBean gatewayBean = list.get(i); JsonObject inverterInfo = new JsonObject(); inverterInfo.addProperty("mac", gatewayBean.getDevice_mac()); inverterInfo.addProperty("spk", gatewayBean.getGatewayType()); inverterInfo.addProperty("sid", gatewayBean.getSid()); inverterInfo.addProperty("oid", gatewayBean.getOid()); inverterInfo.addProperty("name", gatewayBean.getDevice_name()); ary.add(inverterInfo); } json.add("devices", ary); // json.addProperty("zoneType", "password");//区域 HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak() { @Override public void onSuccess(String str) { if (cloudCallBeak != null) { cloudCallBeak.onSuccess(true); //临时的逻辑,上传oid列表到云端 uploadOidDataToCloud(homeId, list, true, null); } } @Override public void onFailure(HDLException e) { if (cloudCallBeak != null) { cloudCallBeak.onFailure(e); } } }); } /** * 获取云端逆变器列表 * * @param homeId 住宅id * @param cloudCallBeak 回调 */ public void getCloudInverterDeviceList(String homeId, CloudCallBeak> cloudCallBeak) { String requestUrl = HttpApi.POST_Device_List; JsonObject json = new JsonObject(); json.addProperty("homeId", homeId); // json.addProperty("zoneType", "password");//区域 HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak() { @Override public void onSuccess(String jsonStr) { if (TextUtils.isEmpty(jsonStr)) { if (cloudCallBeak != null) { cloudCallBeak.onSuccess(null); } } Type type = new TypeToken>() { }.getType(); List list = new Gson().fromJson(jsonStr, type); if (cloudCallBeak != null) { cloudCallBeak.onSuccess(list); } } @Override public void onFailure(HDLException e) { if (cloudCallBeak != null) { cloudCallBeak.onFailure(e); } } }); } /** * 删除云端上逆变器 * * @param deviceId 设备id * @param cloudCallBeak 回调 */ public void delInverterDevice(String homeId, String deviceId, CloudCallBeak cloudCallBeak) { String requestUrl = HttpApi.POST_Device_Remove; JsonObject json = new JsonObject(); json.addProperty("homeId", homeId); json.addProperty("deviceId", deviceId); // json.addProperty("zoneType", "password");//区域 HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak() { @Override public void onSuccess(String str) { if (cloudCallBeak != null) { cloudCallBeak.onSuccess(true); } } @Override public void onFailure(HDLException e) { if (cloudCallBeak != null) { cloudCallBeak.onFailure(e); } } }); } /** * 获取逆变器下挂设备列表 * * @param parentOid 上级设备的Oid * @param cloudCallBeak 回调 */ public void getInverterDeviceChildDeviceList(String homeId, String parentOid, CloudCallBeak> cloudCallBeak) { String requestUrl = HttpApi.POST_Device_ChildDevices_List; JsonObject json = new JsonObject(); json.addProperty("homeId", homeId); json.addProperty("parentOid", parentOid); // json.addProperty("zoneType", "password");//区域 HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak() { @Override public void onSuccess(String jsonStr) { if (TextUtils.isEmpty(jsonStr)) { if (cloudCallBeak != null) { cloudCallBeak.onSuccess(null); } return; } Gson gson = new Gson(); Type typeOfT = new TypeToken>() { }.getType(); List list = gson.fromJson(jsonStr, typeOfT); if (cloudCallBeak != null) { cloudCallBeak.onSuccess(list); } } @Override public void onFailure(HDLException e) { if (cloudCallBeak != null) { cloudCallBeak.onFailure(e); } } }); } /** * 全量更新OID * * @param homeId 住宅id * @param oidList oid列表 * @param cloudCallBeak 回调update */ public void fullUpdateOid(String homeId, List oidList, CloudCallBeak cloudCallBeak) { if (!UserConfigManage.getInstance().getHomeId().equals(homeId)) { HdlLogLogic.print("上传oid列表到云端失败---当前住宅id:" + UserConfigManage.getInstance().getHomeId() + "---上传住宅id:" + homeId, true); return; } if (oidList == null || oidList.isEmpty()) { HdlLogLogic.print("oid列表为空,无法全量更新oid到云端---", true); return; } String requestUrl = HttpApi.POST_Device_Oid; JsonObject json = new JsonObject(); json.addProperty("operationSource", "PROGRAM_ENERGY");// json.addProperty("homeId", homeId); JsonArray jsonArray = new JsonArray(); for (int i = 0; i < oidList.size(); i++) { OidBean oidBean = oidList.get(i); JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("protocolType", oidBean.getProtocolType()); jsonObject.addProperty("deviceType", oidBean.getDeviceType()); jsonObject.addProperty("mac", oidBean.getDevice_mac()); jsonObject.addProperty("oid", oidBean.getOid()); jsonObject.addProperty("device_name", oidBean.getDevice_name()); jsonObject.addProperty("device_model", oidBean.getDevice_model()); jsonObject.addProperty("addresses", oidBean.getAddresses()); jsonObject.addProperty("parentOid", oidBean.getParentOid()); jsonArray.add(jsonObject); } json.add("devices", jsonArray); // json.addProperty("zoneType", "password");//区域 HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak() { @Override public void onSuccess(String str) { if (cloudCallBeak != null) { cloudCallBeak.onSuccess(true); } } @Override public void onFailure(HDLException e) { if (cloudCallBeak != null) { cloudCallBeak.onFailure(e); } } }); } /** * (oid)增量添加 * * @param homeId 住宅id * @param oidList oid列表 * @param cloudCallBeak 回调update */ public void updateOidAdd(String homeId, List oidList, CloudCallBeak cloudCallBeak) { if (!UserConfigManage.getInstance().getHomeId().equals(homeId)) { HdlLogLogic.print("上传oid列表到云端失败---当前住宅id:" + UserConfigManage.getInstance().getHomeId() + "---上传住宅id:" + homeId, true); return; } if (oidList == null || oidList.isEmpty()) { HdlLogLogic.print("oid列表为空,无法增量添加oid到云端---", true); return; } String requestUrl = HttpApi.POST_Device_IncrAdd; JsonObject json = new JsonObject(); json.addProperty("homeId", homeId); JsonArray jsonArray = new JsonArray(); for (int i = 0; i < oidList.size(); i++) { OidBean oidBean = oidList.get(i); if (TextUtils.isEmpty(oidBean.getDevice_model())) { //为空过滤掉; continue; } JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("protocolType", oidBean.getProtocolType()); jsonObject.addProperty("deviceType", oidBean.getDeviceType()); jsonObject.addProperty("mac", oidBean.getDevice_mac()); jsonObject.addProperty("oid", oidBean.getOid()); jsonObject.addProperty("device_name", oidBean.getDevice_name()); jsonObject.addProperty("device_model", oidBean.getDevice_model()); jsonObject.addProperty("addresses", oidBean.getAddresses()); jsonObject.addProperty("parentOid", oidBean.getParentOid()); jsonArray.add(jsonObject); } json.add("devices", jsonArray); // json.addProperty("zoneType", "password");//区域 HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak() { @Override public void onSuccess(String str) { if (cloudCallBeak != null) { cloudCallBeak.onSuccess(true); } HdlLogLogic.print("增量添加oid到云端成功---", true); } @Override public void onFailure(HDLException e) { if (cloudCallBeak != null) { cloudCallBeak.onFailure(e); } HdlLogLogic.print("增量添加oid到云端失败---" + e.getMsg() + "(" + e.getCode() + ")", true); } }); } /** * 修改设备名称逆变器BMS电池包 * 描述: 修改设备名称,支持逆变器,BMS,电池包设备名称修改 * * @param homeId 住宅id * @param deviceType 设备类型(INV:逆变器,BMS:BMS控制盒,BATTERY:电池单元) * @param deviceId 设备id(deviceType = INV 是逆变器deviceId;deviceType = BMS 是BMS的deviceOidId;deviceType = BATTERY 是电池包的deviceOidId) * @param cloudCallBeak 回调update */ public void setRename(String homeId, String deviceType, String deviceId, String name, CloudCallBeak cloudCallBeak) { String requestUrl = HttpApi.POST_Device_Rename; JsonObject json = new JsonObject(); json.addProperty("homeId", homeId); json.addProperty("name", name); json.addProperty("deviceType", deviceType);//设备类型(INV:逆变器,BMS:BMS控制盒,BATTERY:电池单元) json.addProperty("deviceId", deviceId);//设备id(deviceType = INV 是逆变器deviceId;deviceType = BMS 是BMS的deviceOidId;deviceType = BATTERY 是电池包的deviceOidId) // json.addProperty("zoneType", "password");//区域 HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak() { @Override public void onSuccess(String str) { if (cloudCallBeak != null) { cloudCallBeak.onSuccess(true); } } @Override public void onFailure(HDLException e) { if (cloudCallBeak != null) { cloudCallBeak.onFailure(e); } } }); } /** * 设置网关远程参数 * * @param mac 设备mac * @param linkCallBack 回调update */ public void setGatewayRemoteParam(String homeId, String mac, LinkCallBack linkCallBack) { String requestUrl = TopicApi.SET_GATEWAY_REMOTE_EDIT; JsonObject json = new JsonObject(); json.addProperty("homeId", homeId); json.addProperty("server_addr", AppConfigManage.getUserRegionUrl()); json.addProperty("local_secret", UserConfigManage.getInstance().getLocalSecret()); //解密负载数据(写密钥给网关一定是明文,因为那时网关还没有密钥) TcpClient.getInstance().sendDataToLinkGateway(mac, false, requestUrl, json, "", new HDLLinkCallBack() { @Override public void onSuccess(String msg) { GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getLocalGateway(mac); if (gatewayBean != null) { if (!TextUtils.isEmpty(UserConfigManage.getInstance().getLocalSecret())) { gatewayBean.setLocalEncrypt(true); } } if (linkCallBack != null) { linkCallBack.onSuccess(true); } } @Override public void onError(HDLLinkException e) { if (linkCallBack != null) { linkCallBack.onError(e); } } }); } /** * 网关时间读取 * * @param mac 设备mac * @param linkCallBack 回调updatetime */ public void getGatewayTime(String mac, LinkCallBack linkCallBack) { String requestUrl = TopicApi.GET_GATEWAY_TIME; TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, null, "", new HDLLinkCallBack() { @Override public void onSuccess(String json) { if (TextUtils.isEmpty(json)) { if (linkCallBack != null) { linkCallBack.onSuccess(new DeviceTimeBean()); } return; } Gson gson = new Gson(); Type typeOfT = new TypeToken>() { }.getType(); BaseLocalResponse baseLocalResponse = gson.fromJson(json, typeOfT); if (linkCallBack != null) { linkCallBack.onSuccess(baseLocalResponse.getObjects()); } } @Override public void onError(HDLLinkException e) { if (linkCallBack != null) { linkCallBack.onError(e); } } }); } /** * 网关时间修改 * * @param mac 设备mac * @param date 日期 * @param time 时间 * @param timezone 时区 * @param linkCallBack 回调update */ public void editGatewayTime(String mac, String date, String time, String timezone, LinkCallBack linkCallBack) { String requestUrl = TopicApi.SET_GATEWAY_TIME_EDIT; JsonObject json = new JsonObject(); if (!TextUtils.isEmpty(date)) { json.addProperty("date", date);// "2020-08-15" } if (!TextUtils.isEmpty(time)) { json.addProperty("time", time);//"17:25:20" } if (!TextUtils.isEmpty(timezone)) { json.addProperty("timezone", timezone);//时区(语雀https://hdl-gz.yuque.com/wnkbmh/cdrglh/yckkyp#qWOPS) } TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, json, "", new HDLLinkCallBack() { @Override public void onSuccess(String msg) { if (linkCallBack != null) { linkCallBack.onSuccess(true); } } @Override public void onError(HDLLinkException e) { if (linkCallBack != null) { linkCallBack.onError(e); } } }); } /** * 编辑网关参数 * * @param mac 设备mac * @param linkCallBack 回调update */ public void editGatewayParam(String mac, LinkCallBack linkCallBack) { String requestUrl = TopicApi.SET_GATEWAY_EDIT; JsonObject json = new JsonObject(); json.addProperty("master", GatewayMasterType.MasterTrue); TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, json, "", new HDLLinkCallBack() { @Override public void onSuccess(String msg) { if (linkCallBack != null) { linkCallBack.onSuccess(true); } } @Override public void onError(HDLLinkException e) { if (linkCallBack != null) { linkCallBack.onError(e); } } }); } /** * 获取网关详情信息 * * @param mac 网关mac * @param linkCallBack 回调 */ public void getGatewayInfo(String mac, LinkCallBack linkCallBack) { String requestUrl = TopicApi.GET_GATEWAY_INFO; JsonObject json = new JsonObject(); json.addProperty("device_mac", mac); TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, json, "", new HDLLinkCallBack() { @Override public void onSuccess(String json) { if (linkCallBack == null) { return; } if (TextUtils.isEmpty(json)) { linkCallBack.onSuccess(null); return; } Gson gson = new Gson(); Type typeOfT = new TypeToken>() { }.getType(); BaseLocalResponse baseLocalResponse = gson.fromJson(json, typeOfT); if (baseLocalResponse == null || baseLocalResponse.getObjects() == null) { linkCallBack.onSuccess(new InverterBean()); } else { linkCallBack.onSuccess(baseLocalResponse.getObjects()); } } @Override public void onError(HDLLinkException e) { if (linkCallBack != null) { linkCallBack.onError(e); } } }); } /** * 初始化逆变器 * * @param mac 逆变器mac */ public void initializeInverter(String mac, LinkCallBack linkCallBack) { String requestUrl = TopicApi.GATEWAY_INITIALIZE_REMOTE; JsonObject jObject = new JsonObject(); jObject.addProperty("device_mac", mac); jObject.addProperty("reset_factory", "true");//为false或无此字段,则为"清除数据" TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, jObject, "", new HDLLinkCallBack() { @Override public void onSuccess(String json) { if (linkCallBack != null) { linkCallBack.onSuccess(true); } HdlLogLogic.print("初始化逆变器成功-->mac:" + mac, true); } @Override public void onError(HDLLinkException e) { if (linkCallBack != null) { linkCallBack.onError(e); } HdlLogLogic.print("初始化逆变器失败-->mac:" + mac + "---" + e.getMsg() + "(" + e.getCode() + ")", true); } }); } /** * 删除oid * * @param mac 网关mac * @param oid 设备oid */ public void deleteOid(String mac, String oid, LinkCallBack linkCallBack) { String requestUrl = TopicApi.DELETING_GATEWAY_SLAVE; JsonArray jsonArray = new JsonArray(); JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("oid", oid);//设备oid jsonArray.add(jsonObject); TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, jsonArray, "", new HDLLinkCallBack() { @Override public void onSuccess(String json) { if (linkCallBack != null) { linkCallBack.onSuccess(true); } } @Override public void onError(HDLLinkException e) { if (linkCallBack != null) { linkCallBack.onError(e); } } }); } /** * 获取逆变器oid列表 * * @param mac 网关mac * @param linkCallBack 回调 */ public void getInverterOidList(String mac, LinkCallBack> linkCallBack) { String requestUrl = TopicApi.GET_GATEWAY_OID_LIST; TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, null, "", new HDLLinkCallBack() { @Override public void onSuccess(String json) { if (TextUtils.isEmpty(json)) { if (linkCallBack != null) { linkCallBack.onSuccess(null); } return; } Gson gson = new Gson(); Type typeOfT = new TypeToken>>() { }.getType(); BaseLocalResponse> baseLocalResponse = gson.fromJson(json, typeOfT); if (linkCallBack == null) { return; } if (baseLocalResponse == null || baseLocalResponse.getObjects() == null) { linkCallBack.onSuccess(new ArrayList<>()); } else { linkCallBack.onSuccess(baseLocalResponse.getObjects()); } } @Override public void onError(HDLLinkException e) { if (linkCallBack != null) { linkCallBack.onError(e); } } }); } /** * 获取逆变器列表,整合云端逆变器列表和本地逆变器列表(设备类型有:并网逆变器,离线逆变器,负载中心设备); * 注意:有外网,以云端设备列表为准;云端没有,本地存在,则删除本地;否则,没有外网只有局域网,以本地为主,搜索多少个设备就显示多少个 * * @param homeId 住宅id * @param cloudCallBeak 返回逆变器列表 */ public void getCurrentHomeLocalAndCloudGatewayList(String homeId, CloudCallBeak> cloudCallBeak) { searchCurrentHomeGateway(homeId, new GatewayCallBack() { @Override public void onSuccess(List gatewayBeanList) { //向云端获取逆变器列表 getCloudInverterDeviceList(homeId, new CloudCallBeak>() { @Override public void onSuccess(List inverterDeviceList) { //向云端获取负载中心列表 getLoadCentreDeviceList(homeId, new CloudCallBeak>() { @Override public void onSuccess(List loadCentreDeviceList) { if (loadCentreDeviceList != null) { //合并负载中心设备列表,然后在一起做处理 inverterDeviceList.addAll(loadCentreDeviceList); } //设置设备的远程信息 setDeviceRemoteInfo(inverterDeviceList, homeId, cloudCallBeak); } @Override public void onFailure(HDLException e) { //向云端获取负载中心设备列表失败后,仍然处理逆变器设备列表 //设置设备的远程信息 setDeviceRemoteInfo(inverterDeviceList, homeId, cloudCallBeak); } }); } @Override public void onFailure(HDLException e) { //本地有逆变器列表,获取云端绑定逆变器失败,默认返回本地逆变器列表; //向云端获取负载中心列表 getLoadCentreDeviceList(homeId, new CloudCallBeak>() { @Override public void onSuccess(List loadCentreDeviceList) { if (loadCentreDeviceList == null) { if (cloudCallBeak != null) { //没有数据,默认返回本地逆变器列表; cloudCallBeak.onSuccess(getCurrentHomeGatewayList(homeId, null)); } return; } //设置设备的远程信息 setDeviceRemoteInfo(loadCentreDeviceList, homeId, cloudCallBeak); } @Override public void onFailure(HDLException e) { //向云端获取负载中心设备列表失败后,默认返回本地逆变器列表; if (cloudCallBeak != null) { cloudCallBeak.onSuccess(getCurrentHomeGatewayList(homeId, null)); } } }); } }); } @Override public void onError(HDLLinkException e) { // //外网进来先【默认】清空本地逆变器列表 // HDLLinkLocalGateway.getInstance().getGatewayList().clear(); //向云端获取逆变器列表 getCloudInverterDeviceList(homeId, new CloudCallBeak>() { @Override public void onSuccess(List cloudInverterList) { //再向云端获取负载中心列表 getLoadCentreDeviceList(homeId, new CloudCallBeak>() { @Override public void onSuccess(List loadCentreDeviceList) { List newList = new ArrayList<>(); if (cloudInverterList != null) { //添加逆变器设备列表 newList.addAll(cloudInverterList); } if (loadCentreDeviceList != null) { //添加负载中心设备列表 newList.addAll(loadCentreDeviceList); } //整合设备列表后,再一起设置设备的远程信息 setDeviceRemoteInfo(newList, homeId, cloudCallBeak); } @Override public void onFailure(HDLException e) { //设置设备的远程信息 setDeviceRemoteInfo(cloudInverterList, homeId, cloudCallBeak); } }); } @Override public void onFailure(HDLException e) { //向云端获取逆变器列表失败 //再向云端获取负载中心列表 getLoadCentreDeviceList(homeId, new CloudCallBeak>() { @Override public void onSuccess(List loadCentreDeviceList) { //设置设备的远程信息 setDeviceRemoteInfo(loadCentreDeviceList, homeId, cloudCallBeak); } @Override public void onFailure(HDLException e) { //都是获取失败的话,直接返回空列表; if (cloudCallBeak != null) { cloudCallBeak.onSuccess(new ArrayList<>()); } } }); } }); } }); } /** * 设置设备的远程信息(拿到网关ID,mqtt通讯秘钥等信息) * * @param list 云端上的逆变器列表 * @param homeId 住宅id * @param cloudCallBeak 回调 */ public void setDeviceRemoteInfo(List list, String homeId, CloudCallBeak> cloudCallBeak) { if (list == null || list.size() == 0) { //云端没有绑定逆变器,默认返回本地逆变器列表; if (cloudCallBeak != null) { cloudCallBeak.onSuccess(getCurrentHomeGatewayList(homeId, list)); } return; } AtomicInteger atomicInteger = new AtomicInteger(0); for (int i = 0; i < list.size(); i++) { CloudInverterDeviceBean cloudInverterDeviceBean = list.get(i); //获取远程设备通讯信息(拿到网关ID,mqtt通讯秘钥) getDeviceRemoteInfo(homeId, cloudInverterDeviceBean.getSpk(), cloudInverterDeviceBean.getOsn(), new CloudCallBeak() { @Override public void onSuccess(DeviceRemoteInfo deviceRemoteInfo) { atomicInteger.set(atomicInteger.get() + 1); //更新逆变器本地缓存信息 refreshGatewayCacheData(false, cloudInverterDeviceBean, deviceRemoteInfo, homeId); //到最后一条,才处理回调 if (atomicInteger.get() == list.size()) { if (cloudCallBeak != null) { cloudCallBeak.onSuccess(getCurrentHomeGatewayList(homeId, list)); } } } @Override public void onFailure(HDLException e) { atomicInteger.set(atomicInteger.get() + 1); //注意:这个逆变器只能本地控制(因为拿不到mqtt通讯秘钥以及GatewayID,无法远程控制) //更新逆变器本地缓存信息 refreshGatewayCacheData(false, cloudInverterDeviceBean, null, homeId); //到最后一条,才处理回调 if (atomicInteger.get() == list.size()) { if (cloudCallBeak != null) { cloudCallBeak.onSuccess(getCurrentHomeGatewayList(homeId, list)); } } HdlLogLogic.print("获取设备远程通讯信息失败(mac:"+cloudInverterDeviceBean.getOsn()+")------" + e.getMsg() + "(" + e.getCode() + ")",true); } }); } } /** * 通过mac查询逆变器是否存绑定在云端 * * @param list 云端上逆变器列表 * @return true不存在,否则存在 */ private boolean queryMacInverter(List list, String mac) { int exists_value = -1; for (int i = 0; i < list.size(); i++) { CloudInverterDeviceBean cloudInverterDeviceBean = list.get(i); if (mac.equals(cloudInverterDeviceBean.getOsn())) { exists_value = 1; break; } } return exists_value == -1; } /** * 获取设备远程通讯信息 * * @param homeId 住宅Id * @param spk spk * @param mac 设备mac * @param cloudCallBeak 回调 */ public void getDeviceRemoteInfo(String homeId, String spk, String mac, CloudCallBeak cloudCallBeak) { String requestUrl = HttpApi.POST_Device_RemoteInfo; JsonObject json = new JsonObject(); json.addProperty("homeId", homeId); json.addProperty("spk", spk); json.addProperty("mac", mac); HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak() { @Override public void onSuccess(String jsonStr) { if (TextUtils.isEmpty(jsonStr)) { if (cloudCallBeak != null) { cloudCallBeak.onSuccess(null); } } Type type = new TypeToken() { }.getType(); DeviceRemoteInfo deviceRemoteInfo = new Gson().fromJson(jsonStr, type); if (cloudCallBeak != null) { cloudCallBeak.onSuccess(deviceRemoteInfo); } } @Override public void onFailure(HDLException e) { if (cloudCallBeak != null) { cloudCallBeak.onFailure(e); } } }); } /** * 删除本地缓存逆变器 * * @param mac 设备mac */ private void removeLocalInverter(String mac) { GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getLocalGateway(mac); if (gatewayBean != null) { HDLLinkLocalGateway.getInstance().getGatewayList().remove(gatewayBean); } } /** * 判断spk是否是负载中心 * * @param spk spk * @return 真=true,否则返回false */ private boolean isLoadCenterSpk(String spk) { return Objects.equals(spk, LC_SPK); } /** * 清空逆变器住宅id * * @param mac mac */ public void clearInverterHomeId(String mac, LinkCallBack callBeak) { String requestUrl = TopicApi.SET_GATEWAY_REMOTE_EDIT; JsonObject json = new JsonObject(); json.addProperty("homeId", ""); json.addProperty("server_addr", ""); json.addProperty("local_secret", ""); TcpClient.getInstance().sendDataToLinkGateway(mac, true, requestUrl, json, "", new HDLLinkCallBack() { @Override public void onSuccess(String msg) { GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getLocalGateway(mac); if (gatewayBean != null) { if (!TextUtils.isEmpty(UserConfigManage.getInstance().getLocalSecret())) { gatewayBean.setLocalEncrypt(true); } } if (callBeak != null) { callBeak.onSuccess(true); } } @Override public void onError(HDLLinkException e) { if (callBeak != null) { callBeak.onError(e); } } }); } /** * 搜索逆变器列表 * 注意:只搜索当前住宅逆变器和没有被绑定的逆变器 * * @param gatewayCallBack - */ public void searchCurrentHomeGateway(String homeId, GatewayCallBack gatewayCallBack) { List spks = this.getGatewaySpk(); //网关搜索 HDLLinkLocalGateway.getInstance().refreshGatewayByHomeIdAndSpk(homeId, spks, true, gatewayCallBack); } /** * 搜索局域网所有逆变器列表 * 注意:包括已经被绑定到住宅的逆变器 * * @param gatewayCallBack - */ public void searchAllNetworkGateway(GatewayCallBack gatewayCallBack) { List spks = this.getGatewaySpk(); //网关搜索 HDLLinkLocalGateway.getInstance().refreshGatewayBySpk(spks, true, gatewayCallBack); } /** * 检测本地连接状态 * * @param deviceMac 设备mac, * @param callBeak 返回 true=本地连接;false=远程; */ public void isLocalConnect(String homeId, String deviceMac, CloudCallBeak callBeak) { searchCurrentHomeGateway(homeId, new GatewayCallBack() { @Override public void onSuccess(List gatewayBeanList) { if (gatewayBeanList == null || gatewayBeanList.size() == 0) { if (callBeak != null) { callBeak.onSuccess(false); } return; } boolean is_find = false; for (int i = 0; i < gatewayBeanList.size(); i++) { GatewayBean gatewayBean = gatewayBeanList.get(i); if (gatewayBean.getDevice_mac().equals(deviceMac)) { //升级的时候搜索网关列表,把之前mqtt秘钥覆盖掉,要重新获取才行; if (!TextUtils.isEmpty(gatewayBean.getHomeId())) { getDeviceRemoteInfo(gatewayBean.getHomeId(), gatewayBean.getSpk(), gatewayBean.getDevice_mac(), new CloudCallBeak() { @Override public void onSuccess(DeviceRemoteInfo deviceRemoteInfo) { if (deviceRemoteInfo != null) { gatewayBean.setAesKey(deviceRemoteInfo.getSecret());//设置mqtt通讯秘钥 gatewayBean.setGatewayId(deviceRemoteInfo.getGatewayId());//设置gatewayId //todo 设置到【库】里面,底层mqtt订阅,加解密会用到该参数; HDLLinkConfig.getInstance().setAesKey(deviceRemoteInfo.getSecret());//设置mqtt通讯秘钥库 HDLLinkConfig.getInstance().setGatewayId(deviceRemoteInfo.getGatewayId());//设置gatewayId } } @Override public void onFailure(HDLException e) { } }); } is_find = true; break; } } if (callBeak != null) { callBeak.onSuccess(is_find); } } @Override public void onError(HDLLinkException e) { if (callBeak != null) { callBeak.onSuccess(false); } } }); } /** * 检测逆变器有没有连接上云 * * @param deviceMac 设备mac */ public void checkInverterConnectedCloud(String homeId, String deviceMac, CloudCallBeak cloudCallBeak) { this.getCloudInverterDeviceList(homeId, new CloudCallBeak>() { @Override public void onSuccess(List list) { CloudInverterDeviceBean cloudInverterDeviceBean = null; if (list != null && list.size() > 0) { for (int i = 0; i < list.size(); i++) { if (list.get(i).getOsn().equals(deviceMac)) { cloudInverterDeviceBean = list.get(i); break; } } } if (cloudCallBeak != null) { cloudCallBeak.onSuccess(cloudInverterDeviceBean); } } @Override public void onFailure(HDLException e) { if (cloudCallBeak != null) { cloudCallBeak.onFailure(e); } } }); } /** * 获取未运行逆变器列表 * * @param homeId 电站id */ public void getListNotRun(String homeId, CloudCallBeak> cloudCallBeak) { String requestUrl = HttpApi.POST_Device_listNotRun; JsonObject json = new JsonObject(); json.addProperty("homeId", homeId); // json.addProperty("zoneType", "password");//区域 HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak() { @Override public void onSuccess(String jsonStr) { if (TextUtils.isEmpty(jsonStr)) { if (cloudCallBeak != null) { cloudCallBeak.onSuccess(new ArrayList<>()); } } Type type = new TypeToken>() { }.getType(); List list = new Gson().fromJson(jsonStr, type); if (cloudCallBeak != null) { cloudCallBeak.onSuccess(list); } } @Override public void onFailure(HDLException e) { if (cloudCallBeak != null) { cloudCallBeak.onFailure(e); } } }); } /** * 逆变器开机 * * @param homeId 电站id * @param arrayList 设备id数组 */ public void setDeviceStartup(String homeId, JsonArray arrayList, CloudCallBeak cloudCallBeak) { String requestUrl = HttpApi.POST_Device_startup; JsonObject json = new JsonObject(); json.addProperty("homeId", homeId); json.add("deviceIds", arrayList); // json.addProperty("zoneType", "password");//区域 HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak() { @Override public void onSuccess(String jsonStr) { if (cloudCallBeak != null) { cloudCallBeak.onSuccess(true); } } @Override public void onFailure(HDLException e) { if (cloudCallBeak != null) { cloudCallBeak.onFailure(e); } } }); } /** * 获取设备详情 * * @param homeId 电站id * @param arrayList 设备id数组 */ public void getDeviceInfo(String homeId, JsonArray arrayList, CloudCallBeak> cloudCallBeak) { String requestUrl = HttpApi.POST_Device_info; JsonObject json = new JsonObject(); json.addProperty("homeId", homeId); json.add("deviceIds", arrayList); // json.addProperty("zoneType", "password");//区域 HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak() { @Override public void onSuccess(String jsonStr) { if (TextUtils.isEmpty(jsonStr)) { if (cloudCallBeak != null) { cloudCallBeak.onSuccess(new ArrayList<>()); } return; } Gson gson = new Gson(); List list = gson.fromJson(jsonStr, new TypeToken>() { }.getType()); if (cloudCallBeak != null) { cloudCallBeak.onSuccess(list); } } @Override public void onFailure(HDLException e) { if (cloudCallBeak != null) { cloudCallBeak.onFailure(e); } } }); } /** * 获取电站下负载控制中心列表 * * @param homeId 电站id */ public void getLoadCentreDeviceList(String homeId, CloudCallBeak> cloudCallBeak) { String requestUrl = HttpApi.POST_Device_loadCentreDevices; JsonObject json = new JsonObject(); json.addProperty("homeId", homeId); // json.addProperty("zoneType", "password");//区域 HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak() { @Override public void onSuccess(String jsonStr) { if (TextUtils.isEmpty(jsonStr)) { if (cloudCallBeak != null) { cloudCallBeak.onSuccess(new ArrayList<>()); } } Type type = new TypeToken>() { }.getType(); List list = new Gson().fromJson(jsonStr, type); if (cloudCallBeak != null) { cloudCallBeak.onSuccess(list); } } @Override public void onFailure(HDLException e) { if (cloudCallBeak != null) { cloudCallBeak.onFailure(e); } } }); } /** * 逆变器密码登录 * * @param mac 逆变器mac * @param password 逆变器密码 */ public void getPasswordVerification(String mac, String password, LinkCallBack callBeak) { String requestUrl = TopicApi.DELETING_GATEWAY_password_verifiy; JsonObject json = new JsonObject(); json.addProperty("password", password); json.addProperty("mac", mac); TcpClient.getInstance().sendDataToLinkGateway(mac, false, requestUrl, json, "", new HDLLinkCallBack() { @Override public void onSuccess(String msg) { try { Gson gson = new Gson(); LocalResponse localResponse = gson.fromJson(msg, LocalResponse.class); if (localResponse == null) { localResponse = new LocalResponse(); } if (callBeak != null) { callBeak.onSuccess(localResponse); } } catch (Exception e) { if (callBeak != null) { callBeak.onSuccess(new LocalResponse()); } } } @Override public void onError(HDLLinkException e) { if (callBeak != null) { callBeak.onError(e); } } }); } /** * 刷新逆变器缓存信息(包括设置mqtt秘钥) * 注: //目的是为了获取拿到网关ID,mqtt通讯秘钥等信息,缓存本地逆变器列表里面,发送数据数据时自动去缓存列表里面去查找; * 1:本地存在,更新信息即可; * 2:本地不存在,则虚拟一个逆变器对象添加在本地; * * @param deviceRemoteInfo 设备远程通讯信息 * @param cloudInverterDeviceBean 逆变器 */ private void refreshGatewayCacheData(boolean isLocal, CloudInverterDeviceBean cloudInverterDeviceBean, DeviceRemoteInfo deviceRemoteInfo, String homeId) { //本地查找逆变器 GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getLocalGateway(cloudInverterDeviceBean.getOsn()); if (gatewayBean == null) { //不存在,虚拟一个逆变器对象; gatewayBean = new GatewayBean(); HDLLinkLocalGateway.getInstance().getGatewayList().add(gatewayBean);//添加逆变器设备 } //更新属性 if (deviceRemoteInfo != null) { gatewayBean.setAesKey(deviceRemoteInfo.getSecret());//设置mqtt通讯秘钥 gatewayBean.setGatewayId(deviceRemoteInfo.getGatewayId());//设置gatewayId //todo 设置到【库】里面,底层mqtt订阅,加解密会用到该参数; HDLLinkConfig.getInstance().setAesKey(deviceRemoteInfo.getSecret());//设置mqtt通讯秘钥库 HDLLinkConfig.getInstance().setGatewayId(deviceRemoteInfo.getGatewayId());//设置gatewayId } gatewayBean.setHomeId(homeId);//住宅id gatewayBean.setDeviceStatus(cloudInverterDeviceBean.getDeviceStatus());//设置网关状态(1 : 连接中,2 : 故障, 3 : 运行,4 : 离线) gatewayBean.setDeviceId(cloudInverterDeviceBean.getDeviceId());//设置link设备id gatewayBean.setDevice_mac(cloudInverterDeviceBean.getOsn());//设置mac gatewayBean.setOid(cloudInverterDeviceBean.getOid());//设备oid gatewayBean.setSid(cloudInverterDeviceBean.getSid());//设备sid gatewayBean.setDevice_name(cloudInverterDeviceBean.getName());//设备名称 gatewayBean.setGatewayType(cloudInverterDeviceBean.getSpk());//设置spk gatewayBean.setLocalEncrypt(true); //离网逆变器 gatewayBean.setOgMaster(cloudInverterDeviceBean.isOgMaster()); //逆变器 gatewayBean.setMaster(GatewayMasterType.MasterTrue);//默认都是主逆变器(以后支持从逆变器要更改) gatewayBean.setSystemStatusDesc(cloudInverterDeviceBean.getSystemStatusDesc());//设备状态(逆变器设备) gatewayBean.setWorkStatusDesc(cloudInverterDeviceBean.getWorkStatusDesc());//工作状态(负载中心设备) gatewayBean.setHwVersion(cloudInverterDeviceBean.getHwVersion());//软件版本号 gatewayBean.setCategorySecondName(cloudInverterDeviceBean.getCategorySecondName());//设备类型(产品二级分类名称 gatewayBean.setDeviceType(cloudInverterDeviceBean.getDeviceType());//设备类型(INV:逆变器;BMS:BMS控制盒;BATTERY:电池单元) gatewayBean.setAddresses(cloudInverterDeviceBean.getAddresses());//子网号/设备号,逆变器地址 gatewayBean.setPowerPvNow(cloudInverterDeviceBean.getPowerPvNow());//发电功率 gatewayBean.setOutputActivePower(cloudInverterDeviceBean.getOutputActivePower());//逆变器输出有功功率 gatewayBean.setTotalElectricityPvToday(cloudInverterDeviceBean.getTotalElectricityPvToday());//今日发电量 if (!TextUtils.isEmpty(cloudInverterDeviceBean.getOmodel())) { //可能设备没有上报给云端,建议绑定逆变器时候追加一个参数; gatewayBean.setDevice_model(cloudInverterDeviceBean.getOmodel());//设备型号 } gatewayBean.setSpk(cloudInverterDeviceBean.getSpk());//设备spk } /** * 获取网关支持spk列表 * * @return - */ public List getGatewaySpk() { List spks = new ArrayList<>(); spks.add(INVERTER_DEVICE_SPK); spks.add(LC_SPK); spks.add(OFF_INVERTER_OG_SPK); return spks; } public boolean isGatewaySpk(String spk) { if (TextUtils.isEmpty(spk)) { return false; } for (int i = 0; i < this.getGatewaySpk().size(); i++) { if (this.getGatewaySpk().get(i).equals(spk)) { return true; } } return false; } /** * 模拟逆变器设备列表 * * @param sum 模拟多少个 * @return 返回逆变器列表 */ public List getGatewayDeviceListMockData(int sum) { try { List list = new ArrayList<>(); for (int i = 0; i < sum; i++) { String spk = getGatewaySpk().get(i % getGatewaySpk().size()); GatewayBean gatewayBean = new GatewayBean(); gatewayBean.setHomeId("100000000" + i); gatewayBean.setDevice_mac("200000000" + i); gatewayBean.setSpk(spk); gatewayBean.setGateway_type(spk); gatewayBean.setMaster("true"); gatewayBean.setSid("300000000" + i); gatewayBean.setOid("400000000" + i); gatewayBean.setGatewayId("500000000" + i); gatewayBean.setDevice_name("模拟-" + i + "(" + spk + ")"); gatewayBean.setDeviceStatus(5);//默认都是离线¬ list.add(gatewayBean); } return list; } catch (Exception ignored) { return new ArrayList<>(); } } /** * 逆变器spk */ public final String INVERTER_DEVICE_SPK = "energy.hdl_inverter"; /** * 负载中心spk */ public final String LC_SPK = "energy.hdl_lc"; /** * 离线逆变器spk */ public final String OFF_INVERTER_OG_SPK = "energy.inverter_og"; }