From 32b5f366ceaad4aa9a33eccf0b109eef24175495 Mon Sep 17 00:00:00 2001 From: mac <user@users-MacBook-Pro.local> Date: 星期二, 10 十月 2023 14:55:01 +0800 Subject: [PATCH] 2023年10月10日14:54:56 --- app/src/main/java/com/hdl/photovoltaic/other/HdlDeviceLogic.java | 286 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 files changed, 210 insertions(+), 76 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 6462ac8..037b4bf 100644 --- a/app/src/main/java/com/hdl/photovoltaic/other/HdlDeviceLogic.java +++ b/app/src/main/java/com/hdl/photovoltaic/other/HdlDeviceLogic.java @@ -7,7 +7,12 @@ import com.google.gson.JsonObject; import com.google.gson.reflect.TypeToken; import com.hdl.hdlhttp.HxHttp; +import com.hdl.linkpm.sdk.core.api.HDLCloudHomeApi; +import com.hdl.linkpm.sdk.core.callback.IResponseCallBack; import com.hdl.linkpm.sdk.core.exception.HDLException; +import com.hdl.linkpm.sdk.core.response.HDLResponse; +import com.hdl.linkpm.sdk.device.bean.FunctionInfoBean; +import com.hdl.linkpm.sdk.utils.HDLExceptionSubmitUtils; import com.hdl.photovoltaic.bean.HttpResponsePack; import com.hdl.photovoltaic.bean.LocalResponse; import com.hdl.photovoltaic.config.AppConfigManage; @@ -21,6 +26,7 @@ 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.DeviceRemoteInfo; import com.hdl.photovoltaic.ui.bean.InverterDeviceBean; import com.hdl.photovoltaic.ui.bean.OidBean; import com.hdl.photovoltaic.uni.HDLUniMP; @@ -32,11 +38,16 @@ 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.List; +import java.util.concurrent.atomic.AtomicInteger; + +import io.reactivex.rxjava3.disposables.Disposable; /** @@ -44,29 +55,6 @@ */ public class HdlDeviceLogic { private static volatile HdlDeviceLogic sHdlDeviceLogic; - - /** - * 鑾峰彇褰撳墠浣忓畢鐨勯�嗗彉鍣ㄥ垪琛� - */ - public List<GatewayBean> getGatewayList() { - List<GatewayBean> newList = new ArrayList<>(); - List<GatewayBean> list = HDLLinkLocalGateway.getInstance().getGatewayList(); - if (list.size() > 0) { - for (int i = 0; i < list.size(); i++) { - GatewayBean gatewayBean = list.get(i); - if (TextUtils.isEmpty(gatewayBean.getDevice_mac())) { - continue; - } - if (gatewayBean.getMaster().equals("true") - && gatewayBean.getHomeId().equals(UserConfigManage.getInstance().getHomeId())) { -// if ("0101050217BBC400".equals(gatewayBean.getOid()))//娴嬭瘯鏃惰繃婊ゆ帀鏃犳晥鐨勶紝杩欒鍚庨潰浠g爜瑕佸垹闄� -// continue; - newList.add(gatewayBean); - } - } - } - return newList; - } /** * 鑾峰彇褰撳墠瀵硅薄 @@ -84,6 +72,28 @@ } return sHdlDeviceLogic; } + + /** + * 鑾峰彇褰撳墠浣忓畢鐨勯�嗗彉鍣ㄥ垪琛� + */ + public List<GatewayBean> getCurrentHomeGatewayList() { + List<GatewayBean> newList = new ArrayList<>(); + List<GatewayBean> list = HDLLinkLocalGateway.getInstance().getGatewayList(); + if (list.size() > 0) { + for (int i = 0; i < list.size(); i++) { + GatewayBean gatewayBean = list.get(i); + if (TextUtils.isEmpty(gatewayBean.getDevice_mac())) { + continue; + } + if (gatewayBean.getMaster().equals(GatewayMasterType.MasterTrue) + && gatewayBean.getHomeId().equals(UserConfigManage.getInstance().getHomeId())) { + newList.add(gatewayBean); + } + } + } + return newList; + } + /** * 娣诲姞閫嗗彉鍣ㄥ埌浜戠涓� @@ -411,7 +421,7 @@ public void editGatewayParam(String mac, LinkCallBack<Boolean> linkCallBack) { String requestUrl = TopicApi.SET_GATEWAY_EDIT; JsonObject json = new JsonObject(); - json.addProperty("master", "true"); + json.addProperty("master", GatewayMasterType.MasterTrue); TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, json, "", new HDLLinkCallBack() { @Override public void onSuccess(String msg) { @@ -507,15 +517,16 @@ } /** - * 鑾峰彇鏈湴璁惧鍒楄〃 + * 鑾峰彇閫嗗彉鍣ㄥ垪琛� * 娉ㄦ剰:鏈夊缃戜互浜戠璁惧涓哄噯,鏈湴瀛樺湪锛屼簯绔病鏈夊垯鍒犻櫎锛涘唴缃戜互鏈湴涓轰富,鎼滅储澶氬皯涓澶囧氨鏄剧ず澶氬皯涓紱 * - * @param cloudCallBeak - + * @param cloudCallBeak 杩斿洖閫嗗彉鍣ㄥ垪琛� */ - public void getLocalGatewayList(CloudCallBeak<Boolean> cloudCallBeak) { + public void getCurrentHomeLocalAndCloudGatewayList(CloudCallBeak<List<GatewayBean>> cloudCallBeak) { HdlDeviceLogic.getInstance().searchGateway(new GatewayCallBack() { @Override public void onSuccess(List<GatewayBean> gatewayBeanList) { + //灞�鍩熺綉鎯呭喌 HdlDeviceLogic.getInstance().getCloudInverterDeviceList(UserConfigManage.getInstance().getHomeId(), new CloudCallBeak<List<CloudInverterDeviceBean>>() { @Override public void onSuccess(List<CloudInverterDeviceBean> list) { @@ -523,37 +534,75 @@ // //浜戠娌℃湁缁戝畾閫嗗彉鍣�,浠ユ湰鍦颁负涓�; // HDLLinkLocalGateway.getInstance().getGatewayList().clear(); if (cloudCallBeak != null) { - cloudCallBeak.onSuccess(true); + cloudCallBeak.onSuccess(getCurrentHomeGatewayList()); } return; } - //鏀堕泦鍒犻櫎閫嗗彉鍣╯id - List<String> removeSidList = new ArrayList<>(); + //璁板綍鏉℃暟 + AtomicInteger atomicInteger = new AtomicInteger(0); 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()); + getDeviceRemoteInfo(UserConfigManage.getInstance().getHomeId(), cloudInverterDeviceBean.getSpk(), cloudInverterDeviceBean.getOsn(), new CloudCallBeak<DeviceRemoteInfo>() { + @Override + public void onSuccess(DeviceRemoteInfo deviceRemoteInfo) { + atomicInteger.set(atomicInteger.get() + 1); + refreshGatewayCacheData(true, cloudInverterDeviceBean, deviceRemoteInfo); + 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 i = 0; i < removeSidList.size(); i++) { + //鍒犻櫎鏈湴鐨勯�嗗彉鍣� + removeInverter(removeSidList.get(i)); + } + if (cloudCallBeak != null) { + cloudCallBeak.onSuccess(getCurrentHomeGatewayList()); + } + } } - } - } - for (int i = 0; i < removeSidList.size(); i++) { - //鍒犻櫎鏈湴鐨勯�嗗彉鍣� - removeInverter(removeSidList.get(i)); + @Override + public void onFailure(HDLException e) { + atomicInteger.set(atomicInteger.get() + 1); + //鏈夊彲鑳芥渶鍚庝竴鏉℃槸澶辫触鐨� + 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 i = 0; i < removeSidList.size(); i++) { + //鍒犻櫎鏈湴鐨勯�嗗彉鍣� + removeInverter(removeSidList.get(i)); + } + if (cloudCallBeak != null) { + cloudCallBeak.onSuccess(getCurrentHomeGatewayList()); + } + } + HdlLogLogic.print("鑾峰彇璁惧杩滅▼閫氳淇℃伅澶辫触->msg->" + e.getMsg() + "(" + e.getCode() + ")"); + } + }); } - for (int i = 0; i < list.size(); i++) { - CloudInverterDeviceBean cloudInverterDeviceBean = list.get(i); - GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getLocalGateway(cloudInverterDeviceBean.getSid()); - objectAssignment(cloudInverterDeviceBean, gatewayBean); - } - if (cloudCallBeak != null) { - cloudCallBeak.onSuccess(true); - } } @Override @@ -567,29 +616,47 @@ @Override public void onError(HDLLinkException e) { + //澶栫綉鎯呭喌 HdlDeviceLogic.getInstance().getCloudInverterDeviceList(UserConfigManage.getInstance().getHomeId(), new CloudCallBeak<List<CloudInverterDeviceBean>>() { @Override public void onSuccess(List<CloudInverterDeviceBean> list) { - //娓呮鏈湴缃戝叧鍒楄〃 + //浜戠娌℃湁,娓呯┖鏈湴閫嗗彉鍣ㄥ垪琛� HDLLinkLocalGateway.getInstance().getGatewayList().clear(); if (list == null || list.size() == 0) { if (cloudCallBeak != null) { - cloudCallBeak.onSuccess(true); + cloudCallBeak.onSuccess(getCurrentHomeGatewayList()); } return; } + //璁板綍鏉℃暟 + AtomicInteger atomicInteger = new AtomicInteger(0); for (int i = 0; i < list.size(); i++) { CloudInverterDeviceBean cloudInverterDeviceBean = list.get(i); - //浜戠瀵硅薄鏁版嵁浜ゆ崲鍒版湰鍦板璞� - GatewayBean gatewayBean = new GatewayBean(); - objectAssignment(cloudInverterDeviceBean, gatewayBean); + getDeviceRemoteInfo(UserConfigManage.getInstance().getHomeId(), cloudInverterDeviceBean.getSpk(), cloudInverterDeviceBean.getOsn(), new CloudCallBeak<DeviceRemoteInfo>() { + @Override + public void onSuccess(DeviceRemoteInfo deviceRemoteInfo) { + atomicInteger.set(atomicInteger.get() + 1); + refreshGatewayCacheData(false, cloudInverterDeviceBean, deviceRemoteInfo); + if (atomicInteger.get() == list.size()) { + if (cloudCallBeak != null) { + cloudCallBeak.onSuccess(getCurrentHomeGatewayList()); + } + } + } - //娣诲姞缃戝叧 - HDLLinkLocalGateway.getInstance().getGatewayList().add(gatewayBean); + @Override + public void onFailure(HDLException e) { + atomicInteger.set(atomicInteger.get() + 1); + if (atomicInteger.get() == list.size()) { + if (cloudCallBeak != null) { + cloudCallBeak.onSuccess(getCurrentHomeGatewayList()); + } + } + HdlLogLogic.print("鑾峰彇璁惧杩滅▼閫氳淇℃伅澶辫触->msg->" + e.getMsg() + "(" + e.getCode() + ")"); + } + }); } - if (cloudCallBeak != null) { - cloudCallBeak.onSuccess(true); - } + } @@ -600,6 +667,46 @@ } } }); + } + }); + } + + /** + * 鑾峰彇璁惧杩滅▼閫氳淇℃伅 + * + * @param homeId 浣忓畢Id + * @param spk spk + * @param mac 璁惧mac + * @param cloudCallBeak 鍥炶皟 + * @return - + */ + public void getDeviceRemoteInfo(String homeId, String spk, String mac, CloudCallBeak<DeviceRemoteInfo> 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<String>() { + @Override + public void onSuccess(String jsonStr) { + if (TextUtils.isEmpty(jsonStr)) { + if (cloudCallBeak != null) { + cloudCallBeak.onSuccess(null); + } + } + Type type = new TypeToken<DeviceRemoteInfo>() { + }.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); + } } }); } @@ -617,7 +724,7 @@ } /** - * 鎼滅储缃戝叧鍒楄〃 + * 鎼滅储閫嗗彉鍣ㄥ垪琛� * * @param gatewayCallBack - */ @@ -627,26 +734,51 @@ HDLLinkLocalGateway.getInstance().refreshGatewayByHomeIdAndSpk(UserConfigManage.getInstance().getHomeId(), spks, true, gatewayCallBack); } - - private void objectAssignment(CloudInverterDeviceBean cloudInverterDeviceBean, GatewayBean gatewayBean) { - if (cloudInverterDeviceBean == null || gatewayBean == null) { - return; + /** + * 鍒锋柊閫嗗彉鍣ㄧ紦瀛樹俊鎭�(鍖呮嫭璁剧疆mqtt绉橀挜锛� + * 娉�: + * 1:鏈湴瀛樺湪鏇存柊淇℃伅鍗冲彲; + * 2:浜戠鏈夋湰鍦颁笉瀛樺湪,鍒欒櫄鎷熶竴涓�嗗彉鍣ㄥ璞℃坊鍔犲湪鏈湴; + * + * @param deviceRemoteInfo 璁惧杩滅▼閫氳淇℃伅 + * @param cloudInverterDeviceBean 閫嗗彉鍣� + */ + private static void refreshGatewayCacheData(boolean isLocal, CloudInverterDeviceBean cloudInverterDeviceBean, DeviceRemoteInfo deviceRemoteInfo) { + //鏈湴鏌ユ壘閫嗗彉鍣� + GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getLocalGateway(cloudInverterDeviceBean.getOsn()); + if (gatewayBean == null) { + //涓嶅瓨鍦紝铏氭嫙涓�涓�嗗彉鍣ㄥ璞�; + gatewayBean = new GatewayBean(); + HDLLinkLocalGateway.getInstance().getGatewayList().add(gatewayBean);//娣诲姞閫嗗彉鍣ㄨ澶� } - gatewayBean.setOid(cloudInverterDeviceBean.getOid()); - gatewayBean.setSid(cloudInverterDeviceBean.getSid()); - gatewayBean.setGatewayId(cloudInverterDeviceBean.getGatewayId()); - gatewayBean.setOnline(cloudInverterDeviceBean.isOnline()); - gatewayBean.setDevice_mac(cloudInverterDeviceBean.getOsn());//璁惧mac - gatewayBean.setDevice_name(cloudInverterDeviceBean.getGatewayName()); - gatewayBean.setHomeId(UserConfigManage.getInstance().getHomeId()); + //鏇存柊灞炴�� + if (deviceRemoteInfo != null) { + gatewayBean.setAesKey(deviceRemoteInfo.getSecret());//璁剧疆mqtt閫氳绉橀挜 + gatewayBean.setGatewayId(deviceRemoteInfo.getGatewayId());//璁剧疆gatewayId + //鐢ㄤ箣鍓嶇殑搴�,搴曞眰mqtt璁㈤槄锛屽姞瑙e瘑浼氱敤鍒拌鍙傛暟; + HDLLinkConfig.getInstance().setAesKey(deviceRemoteInfo.getSecret());//璁剧疆mqtt閫氳绉橀挜搴� + HDLLinkConfig.getInstance().setGatewayId(deviceRemoteInfo.getGatewayId());//璁剧疆gatewayId + + } + gatewayBean.setHomeId(UserConfigManage.getInstance().getHomeId());//浣忓畢id + gatewayBean.setDeviceStatus(cloudInverterDeviceBean.getDeviceStatus());//璁剧疆缃戝叧鐘舵�� + 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.setMaster("true"); - gatewayBean.setSystemStatusDesc(cloudInverterDeviceBean.getSystemStatusDesc()); - gatewayBean.setHwVersion(cloudInverterDeviceBean.getHwVersion()); - gatewayBean.setCategorySecondName(cloudInverterDeviceBean.getCategorySecondName()); - gatewayBean.setDeviceId(cloudInverterDeviceBean.getDeviceId()); + gatewayBean.setMaster(GatewayMasterType.MasterTrue); + gatewayBean.setSystemStatusDesc(cloudInverterDeviceBean.getSystemStatusDesc());//璁惧鐘舵�� + gatewayBean.setHwVersion(cloudInverterDeviceBean.getHwVersion());//杞欢鐗堟湰鍙� + gatewayBean.setCategorySecondName(cloudInverterDeviceBean.getCategorySecondName());//璁惧绫诲瀷(浜у搧浜岀骇鍒嗙被鍚嶇О + gatewayBean.setAddresses(cloudInverterDeviceBean.getAddresses());//瀛愮綉鍙�/璁惧鍙� + gatewayBean.setPowerPvNow(cloudInverterDeviceBean.getPowerPvNow());//鍙戠數鍔熺巼 + gatewayBean.setTotalElectricityPvToday(cloudInverterDeviceBean.getTotalElectricityPvToday());//浠婃棩鍙戠數閲� } + /** * 鑾峰彇缃戝叧spk鍒楄〃 @@ -655,7 +787,9 @@ */ private List<String> getGatewaySpk() { List<String> spks = new ArrayList<>(); - spks.add("energy.hdl_inverter"); + spks.add(DEVICE_SPK); return spks; } + + public final String DEVICE_SPK = "energy.hdl_inverter"; } -- Gitblit v1.8.0