From 49db2ff387b7cc6385d51cefb80a8604600408f0 Mon Sep 17 00:00:00 2001 From: mac <user@users-MacBook-Pro.local> Date: 星期四, 21 三月 2024 16:55:06 +0800 Subject: [PATCH] 2024年03月21日16:54:46 --- app/src/main/java/com/hdl/photovoltaic/other/HdlDeviceLogic.java | 254 ++++++++++++++++++++++++++++++++++++-------------- 1 files changed, 184 insertions(+), 70 deletions(-) diff --git a/app/src/main/java/com/hdl/photovoltaic/other/HdlDeviceLogic.java b/app/src/main/java/com/hdl/photovoltaic/other/HdlDeviceLogic.java index 3ac4c25..1c00f40 100644 --- a/app/src/main/java/com/hdl/photovoltaic/other/HdlDeviceLogic.java +++ b/app/src/main/java/com/hdl/photovoltaic/other/HdlDeviceLogic.java @@ -2,6 +2,7 @@ import android.text.TextUtils; +import com.facebook.imagepipeline.image.OriginalEncodedImageInfo; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonObject; @@ -21,12 +22,17 @@ import com.hdl.photovoltaic.ui.bean.DeviceTimeBean; import com.hdl.photovoltaic.ui.bean.OidBean; import com.hdl.sdk.link.HDLLinkLocalSdk; +import com.hdl.sdk.link.common.config.TopicConstant; +import com.hdl.sdk.link.common.exception.HDLLinkCode; import com.hdl.sdk.link.common.exception.HDLLinkException; +import com.hdl.sdk.link.common.utils.IdUtils; +import com.hdl.sdk.link.core.bean.LinkRequest; 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.core.connect.HDLConnectHelper; import com.hdl.sdk.link.gateway.HDLLinkLocalGateway; import com.hdl.sdk.link.gateway.type.GatewayMasterType; @@ -60,6 +66,7 @@ return sHdlDeviceLogic; } + /** * 鑾峰彇褰撳墠浣忓畢鐨勯�嗗彉鍣ㄥ垪琛�(鍖呮嫭浠庣殑閫嗗彉鍣�) */ @@ -81,64 +88,55 @@ return newList; } - /** - * 鑾峰彇褰撳墠浣忓畢鐨勩�愪富銆戦�嗗彉鍣� - */ - public GatewayBean getCurrentHomeMainGateway(String homeId) { - return queryCurrentHomeMainGateway(this.getCurrentHomeGatewayList(homeId), homeId); - } - - /** - * 鏌ヨ褰撳墠浣忓畢鐨勩�愪富銆戦�嗗彉鍣� - */ - public GatewayBean queryCurrentHomeMainGateway(List<GatewayBean> list, String homeId) { - if (list == null || list.size() == 0) { - return null; - } - GatewayBean findGatewayBean = null; - for (int i = 0; i < list.size(); i++) { - GatewayBean gatewayBean = list.get(i); - if (gatewayBean.getMaster().equals(GatewayMasterType.MasterTrue) && gatewayBean.getHomeId().equals(homeId)) { - //鎵惧埌杩斿洖 - findGatewayBean = gatewayBean; - break; - } - } - return findGatewayBean; - } - /** * 閫嗗彉鍣ㄤ笂浼犳暟鎹埌浜戠(鍖呮嫭:sid锛宱id) * * @param homeId 浣忓畢id - * @param mac 璁惧mac + * @param list 閫嗗彉鍣ㄥ垪琛� */ - public void uploadDataToCloud(String homeId, String mac, CloudCallBeak<Boolean> callBeak) { - getInverterOidList(mac, new LinkCallBack<List<OidBean>>() { - @Override - public void onSuccess(List<OidBean> oidBeanList) { - if (oidBeanList == null) { - return; + public void uploadDataToCloud(String homeId, List<GatewayBean> list, CloudCallBeak<Boolean> callBeak) { + if (list == null || list.size() == 0) { + return; + } + List<OidBean> oidList = new ArrayList<>(); + AtomicInteger atomicInteger = new AtomicInteger(0); + for (int i = 0; i < list.size(); i++) { + atomicInteger.set(atomicInteger.get() + 1); + GatewayBean gatewayBean = list.get(i); + if (gatewayBean == null || TextUtils.isEmpty(gatewayBean.getDevice_mac())) { + continue; + } + getInverterOidList(gatewayBean.getDevice_mac(), new LinkCallBack<List<OidBean>>() { + @Override + public void onSuccess(List<OidBean> oidBeanList) { + if (oidBeanList == null) { + return; + } + oidList.addAll(oidBeanList); + if (atomicInteger.get() == list.size()) { + //鍏ㄩ儴鑾峰彇鍚庡啀涓婁紶 + fullUpdateOid(homeId, oidList, new CloudCallBeak<Boolean>() { + @Override + public void onSuccess(Boolean obj) { + HdlLogLogic.print("涓婁紶oid鍒楄〃鍒颁簯绔垚鍔�--->浣忓畢id:" + homeId + "--->mac:" + gatewayBean.getDevice_mac(), true); + } + + @Override + public void onFailure(HDLException e) { + HdlLogLogic.print("涓婁紶oid鍒楄〃鍒颁簯绔け璐�--->浣忓畢id:" + homeId + "--->mac:" + gatewayBean.getDevice_mac() + "-->" + e.getMsg() + "(" + e.getCode() + ")", true); + } + }); + } } - fullUpdateOid(homeId, oidBeanList, new CloudCallBeak<Boolean>() { - @Override - public void onSuccess(Boolean obj) { - HdlLogLogic.print("涓婁紶oid鍒楄〃鍒颁簯绔垚鍔�--->浣忓畢id:" + homeId + "--->mac:" + mac, true); - } - @Override - public void onFailure(HDLException e) { - HdlLogLogic.print("涓婁紶oid鍒楄〃鍒颁簯绔け璐�--->浣忓畢id:" + homeId + "--->mac:" + mac + "-->" + e.getMsg() + "(" + e.getCode() + ")", true); - } - }); - } + @Override + public void onError(HDLLinkException e) { + HdlLogLogic.print("鑾峰彇閫嗗彉鍣╫id鍒楄〃澶辫触--->浣忓畢id:" + homeId + "--->mac:" + gatewayBean.getDevice_mac() + "-->" + e.getMsg() + "(" + e.getCode() + ")", true); + } + }); + } - @Override - public void onError(HDLLinkException e) { - HdlLogLogic.print("鑾峰彇閫嗗彉鍣╫id鍒楄〃澶辫触--->浣忓畢id:" + homeId + "--->mac:" + mac + "-->" + e.getMsg() + "(" + e.getCode() + ")", true); - } - }); } @@ -170,7 +168,52 @@ if (cloudCallBeak != null) { cloudCallBeak.onSuccess(true); //涓存椂鐨勯�昏緫锛屼笂浼爋id鍒楄〃鍒颁簯绔� - uploadDataToCloud(UserConfigManage.getInstance().getHomeId(), mac, null); + List<GatewayBean> list = HdlDeviceLogic.getInstance().getCurrentHomeGatewayList(homeId); + uploadDataToCloud(UserConfigManage.getInstance().getHomeId(), list, null); + } + } + + @Override + public void onFailure(HDLException e) { + if (cloudCallBeak != null) { + cloudCallBeak.onFailure(e); + } + } + }); + } + + /** + * 娣诲姞澶氫釜閫嗗彉鍣ㄥ埌浜戠涓� + * + * @param list 閫嗗彉鍣ㄥ垪琛� + * @param cloudCallBeak - + */ + public void addAllInverterDeviceToCloud(String homeId, List<GatewayBean> list, CloudCallBeak<Boolean> 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<String>() { + @Override + public void onSuccess(String str) { + if (cloudCallBeak != null) { + cloudCallBeak.onSuccess(true); + //涓存椂鐨勯�昏緫锛屼笂浼爋id鍒楄〃鍒颁簯绔� + uploadDataToCloud(UserConfigManage.getInstance().getHomeId(), list, null); } } @@ -579,6 +622,68 @@ } /** + * 鍒濆鍖栭�嗗彉鍣� + * + * @param mac 缃戝叧mac + */ + public void initializeGateway(String mac, LinkCallBack<Boolean> linkCallBack) { + String requestUrl = TopicApi.GATEWAY_INITIALIZE_REMOTE; + JsonObject jObject = new JsonObject(); + jObject.addProperty("device_mac", mac); + jObject.addProperty("reset_factory", "true");//涓篺alse鎴栨棤姝ゅ瓧娈碉紝鍒欎负"娓呴櫎鏁版嵁" + 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<Boolean> 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); + } + } + }); + + } + + + /** * 鑾峰彇閫嗗彉鍣╫id鍒楄〃 * * @param mac 缃戝叧mac @@ -658,15 +763,11 @@ if (atomicInteger.get() == list.size()) { //鏀堕泦鍒犻櫎閫嗗彉鍣╯id List<String> removeSidList = new ArrayList<>(); - for (int i = 0; i < list.size(); i++) { - CloudInverterDeviceBean cloudInverterDeviceBean = list.get(i); - for (int j = 0; j < HDLLinkLocalGateway.getInstance().getGatewayList().size(); j++) { - GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getGatewayList().get(j); - if (!cloudInverterDeviceBean.getSid().equals(gatewayBean.getSid())) { - //鏈湴鏈夛紝浜戠娌℃湁,鍒犻櫎鏈湴锛� - removeSidList.add(gatewayBean.getSid()); - } - + for (int j = 0; j < HDLLinkLocalGateway.getInstance().getGatewayList().size(); j++) { + GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getGatewayList().get(j); + if (!querySidInverter(list, gatewayBean.getSid())) { + //鏈湴鏈夛紝浜戠娌℃湁,鍒犻櫎鏈湴锛� + removeSidList.add(gatewayBean.getSid()); } } for (int i = 0; i < removeSidList.size(); i++) { @@ -686,15 +787,11 @@ if (atomicInteger.get() == list.size()) { //鏀堕泦鍒犻櫎閫嗗彉鍣╯id List<String> removeSidList = new ArrayList<>(); - for (int i = 0; i < list.size(); i++) { - CloudInverterDeviceBean cloudInverterDeviceBean = list.get(i); - for (int j = 0; j < HDLLinkLocalGateway.getInstance().getGatewayList().size(); j++) { - GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getGatewayList().get(j); - if (!cloudInverterDeviceBean.getSid().equals(gatewayBean.getSid())) { - //鏈湴鏈夛紝浜戠娌℃湁,鍒犻櫎鏈湴锛� - removeSidList.add(gatewayBean.getSid()); - } - + for (int j = 0; j < HDLLinkLocalGateway.getInstance().getGatewayList().size(); j++) { + GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getGatewayList().get(j); + if (!querySidInverter(list, gatewayBean.getSid())) { + //鏈湴鏈夛紝浜戠娌℃湁,鍒犻櫎鏈湴锛� + removeSidList.add(gatewayBean.getSid()); } } for (int i = 0; i < removeSidList.size(); i++) { @@ -787,6 +884,22 @@ }); } }); + } + + /** + * 閫氳繃sid鏌ヨ閫嗗彉鍣� + * + * @param list 浜戠涓婇�嗗彉鍣ㄥ垪琛� + * @return 瀛樺湪涓簍rue, 鍚﹀垯涓篺alse + */ + private boolean querySidInverter(List<CloudInverterDeviceBean> list, String sid) { + for (int i = 0; i < list.size(); i++) { + CloudInverterDeviceBean cloudInverterDeviceBean = list.get(i); + if (sid.equals(cloudInverterDeviceBean.getSid())) { + return true; + } + } + return false; } /** @@ -893,9 +1006,10 @@ * * @param gatewayCallBack - */ - public void searchAllGateway(GatewayCallBack gatewayCallBack) { + public void searchAllNetworkGateway(GatewayCallBack gatewayCallBack) { + List<String> spks = this.getGatewaySpk(); //缃戝叧鎼滅储 - HDLLinkLocalSdk.getInstance().refreshGateway(gatewayCallBack, this.getGatewaySpk()); + HDLLinkLocalGateway.getInstance().refreshGatewayBySpk(spks, true, gatewayCallBack); } /** -- Gitblit v1.8.0