From a4e8dee87338beef6f45f053d8fa9c36dc74ed09 Mon Sep 17 00:00:00 2001 From: mac <user@users-MacBook-Pro.local> Date: 星期一, 06 五月 2024 19:53:17 +0800 Subject: [PATCH] 2024年05月06日19:53:11 --- app/src/main/java/com/hdl/photovoltaic/other/HdlDeviceLogic.java | 366 ++++++++++++++++++++++++++++++++++++++++++++------- 1 files changed, 311 insertions(+), 55 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 d5bf0a7..363da34 100644 --- a/app/src/main/java/com/hdl/photovoltaic/other/HdlDeviceLogic.java +++ b/app/src/main/java/com/hdl/photovoltaic/other/HdlDeviceLogic.java @@ -1,5 +1,6 @@ package com.hdl.photovoltaic.other; +import android.os.SystemClock; import android.text.TextUtils; import com.facebook.imagepipeline.image.OriginalEncodedImageInfo; @@ -8,6 +9,7 @@ import com.google.gson.JsonObject; import com.google.gson.reflect.TypeToken; import com.hdl.linkpm.sdk.core.exception.HDLException; +import com.hdl.photovoltaic.bean.PageNumberObject; import com.hdl.photovoltaic.config.AppConfigManage; import com.hdl.photovoltaic.config.UserConfigManage; import com.hdl.photovoltaic.internet.HttpClient; @@ -18,8 +20,11 @@ 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.DeviceBean; import com.hdl.photovoltaic.ui.bean.DeviceRemoteInfo; import com.hdl.photovoltaic.ui.bean.DeviceTimeBean; +import com.hdl.photovoltaic.ui.bean.HouseInfoBean; +import com.hdl.photovoltaic.ui.bean.MessageBean; import com.hdl.photovoltaic.ui.bean.OidBean; import com.hdl.sdk.link.HDLLinkLocalSdk; import com.hdl.sdk.link.common.config.TopicConstant; @@ -66,6 +71,27 @@ return sHdlDeviceLogic; } + public static final String kWp = "kWp"; + public static final String kWh = "kW.h"; + + public static final String kW = "kW"; + private List<DeviceBean> mListDevice = new ArrayList<>(); + + public void clearDeviceList() { + if (mListDevice == null) { + return; + } + if (mListDevice.size() > 0) { + mListDevice.clear(); + } + } + + public List<DeviceBean> getDeviceList() { + if (mListDevice == null) { + return new ArrayList<>(); + } + return mListDevice; + } /** * 鑾峰彇褰撳墠浣忓畢鐨勯�嗗彉鍣ㄥ垪琛�(鍖呮嫭浠庣殑閫嗗彉鍣�) @@ -88,56 +114,275 @@ return newList; } + /** + * 娣诲姞閫嗗彉鍣ㄥ埌缂撳瓨鍒楄〃閲岄潰鍘� + * + * @param homeId 鐢电珯id + * @param newGatewayBean 閫嗗彉鍣ㄥ璞� + */ + public void addGatewayToLocalCacheMemory(String homeId, GatewayBean newGatewayBean) { + if (newGatewayBean == null || TextUtils.isEmpty(homeId)) { + return; + } + List<GatewayBean> list = getCurrentHomeGatewayList(homeId); + 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 閫嗗彉鍣╩ac + */ + public void removeLocalCacheMemoryGateway(String homeId, String mac) { + if (TextUtils.isEmpty(mac)) { + return; + } + List<GatewayBean> list = getCurrentHomeGatewayList(homeId); + 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 searchTxt 鎼滅储鍐呭 + * @param pageNo 椤电爜 + * @param pageSize 椤垫暟 + */ + public void getPowerStationDeviceList(String searchTxt, long pageNo, long pageSize, CloudCallBeak<PageNumberObject<DeviceBean>> 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<String>() { + @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<PageNumberObject<DeviceBean>>() { + }.getType(); + PageNumberObject<DeviceBean> pageNumberObject = gson.fromJson(jsonStr, type); + if (cloudCallBeak != null) { + cloudCallBeak.onSuccess(pageNumberObject); + } + } + + @Override + public void onFailure(HDLException e) { + if (cloudCallBeak != null) { + cloudCallBeak.onFailure(e); + } + } + }); + } + + /** + * 娣诲姞銆愯澶囧垪琛ㄣ�戝埌鏈湴缂撳瓨 + * + * @param list -璁惧鍒楄〃 + */ + public void setListDevice(List<DeviceBean> 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 deviceBean -璁惧瀵硅薄 + */ + public void setSingleDevice(DeviceBean 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锛宱id) * - * @param homeId 浣忓畢id - * @param list 閫嗗彉鍣ㄥ垪琛� + * @param homeId 浣忓畢id + * @param list 閫嗗彉鍣ㄥ垪琛� + * @param whetherToAdd true琛ㄧず鐢ㄥ閲�,false琛ㄧず鐢ㄥ叏閲� */ - public void uploadDataToCloud(String homeId, List<GatewayBean> list, CloudCallBeak<Boolean> callBeak) { + public void uploadOidDataToCloud(String homeId, List<GatewayBean> list, boolean whetherToAdd, 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>() { + 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<List<OidBean>>() { + @Override + public void onSuccess(List<OidBean> oidBeanList) { + if (oidBeanList == null) { + HdlLogLogic.print("涓婁紶oid鍒楄〃鍒颁簯绔垚鍔�--->浣忓畢id:" + homeId + "--->mac:" + gatewayBean.getDevice_mac() + "--->鏁版嵁鏄痭ull", true); + return; + } + //澧為噺娣诲姞oid + updateOidAdd(homeId, oidBeanList, new CloudCallBeak<Boolean>() { @Override public void onSuccess(Boolean obj) { - HdlLogLogic.print("涓婁紶oid鍒楄〃鍒颁簯绔垚鍔�--->浣忓畢id:" + homeId + "--->mac:" + gatewayBean.getDevice_mac(), true); + 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); + HdlLogLogic.print("涓婁紶oid鍒楄〃鍒颁簯绔け璐�(澧為噺)--->浣忓畢id:" + homeId + "--->mac:" + gatewayBean.getDevice_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) { + } + }); + SystemClock.sleep(200); + } + } else { + //鍙湁杩涘叆璇︽儏鐣岄潰 + List<OidBean> newOidList = new ArrayList<>(); + 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())) { + continue; } - }); + getInverterOidList(gatewayBean.getDevice_mac(), new LinkCallBack<List<OidBean>>() { + @Override + public void onSuccess(List<OidBean> oidBeanList) { + atomicInteger.set(atomicInteger.get() + 1); + if (oidBeanList == null) { + return; + } + newOidList.addAll(oidBeanList); + if (atomicInteger.get() == list.size()) { + if (newOidList.size() == 0) { + return; + } +// //澧為噺娣诲姞oid +// updateOidAdd(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, newOidList, new CloudCallBeak<Boolean>() { + @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); + } + }); + } + } + + @Override + public void onError(HDLLinkException e) { + atomicInteger.set(atomicInteger.get() + 1); + if (atomicInteger.get() == list.size()) { + if (newOidList.size() == 0) { + HdlLogLogic.print("鑾峰彇閫嗗彉鍣╫id鍒楄〃澶辫触--->浣忓畢id:" + homeId + "--->mac:" + gatewayBean.getDevice_mac() + "-->" + e.getMsg() + "(" + e.getCode() + ")", true); + return; + } + //澧為噺娣诲姞oid + updateOidAdd(homeId, newOidList, new CloudCallBeak<Boolean>() { + @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); + } } - - } /** @@ -169,7 +414,7 @@ cloudCallBeak.onSuccess(true); //涓存椂鐨勯�昏緫锛屼笂浼爋id鍒楄〃鍒颁簯绔� List<GatewayBean> list = HdlDeviceLogic.getInstance().getCurrentHomeGatewayList(homeId); - uploadDataToCloud(UserConfigManage.getInstance().getHomeId(), list, null); + uploadOidDataToCloud(UserConfigManage.getInstance().getHomeId(), list, true, null); } } @@ -213,7 +458,7 @@ if (cloudCallBeak != null) { cloudCallBeak.onSuccess(true); //涓存椂鐨勯�昏緫锛屼笂浼爋id鍒楄〃鍒颁簯绔� - uploadDataToCloud(UserConfigManage.getInstance().getHomeId(), list, null); + uploadOidDataToCloud(homeId, list, true, null); } } @@ -568,7 +813,7 @@ public void editGatewayParam(String mac, LinkCallBack<Boolean> linkCallBack) { String requestUrl = TopicApi.SET_GATEWAY_EDIT; JsonObject json = new JsonObject(); - json.addProperty("master", GatewayMasterType.MasterFalse); + json.addProperty("master", GatewayMasterType.MasterTrue); TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, json, "", new HDLLinkCallBack() { @Override public void onSuccess(String msg) { @@ -628,17 +873,16 @@ */ public void initializeGateway(String mac, LinkCallBack<Boolean> linkCallBack) { String requestUrl = TopicApi.GATEWAY_INITIALIZE_REMOTE; - JsonObject sendJsonObj = new JsonObject(); JsonObject jObject = new JsonObject(); jObject.addProperty("device_mac", mac); jObject.addProperty("reset_factory", "true");//涓篺alse鎴栨棤姝ゅ瓧娈碉紝鍒欎负"娓呴櫎鏁版嵁" - sendJsonObj.add("objects", jObject); - TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, sendJsonObj, "", new HDLLinkCallBack() { + 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 @@ -646,6 +890,7 @@ if (linkCallBack != null) { linkCallBack.onError(e); } + HdlLogLogic.print("鍒濆鍖栭�嗗彉鍣ㄥけ璐�-->mac:" + mac + "--->" + e.getMsg() + "(" + e.getCode() + ")", true); } }); @@ -763,15 +1008,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++) { @@ -791,15 +1032,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++) { @@ -892,6 +1129,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; } /** @@ -1139,7 +1392,10 @@ gatewayBean.setAddresses(cloudInverterDeviceBean.getAddresses());//瀛愮綉鍙�/璁惧鍙�,閫嗗彉鍣ㄥ湴鍧� gatewayBean.setPowerPvNow(cloudInverterDeviceBean.getPowerPvNow());//鍙戠數鍔熺巼 gatewayBean.setTotalElectricityPvToday(cloudInverterDeviceBean.getTotalElectricityPvToday());//浠婃棩鍙戠數閲� - gatewayBean.setDevice_model(cloudInverterDeviceBean.getOmodel());//璁惧鍨嬪彿 + if (!TextUtils.isEmpty(cloudInverterDeviceBean.getOmodel())) { + //鍙兘璁惧娌℃湁涓婃姤缁欎簯绔�,寤鸿缁戝畾閫嗗彉鍣ㄦ椂鍊欒拷鍔犱竴涓弬鏁�; + gatewayBean.setDevice_model(cloudInverterDeviceBean.getOmodel());//璁惧鍨嬪彿 + } gatewayBean.setSpk(cloudInverterDeviceBean.getSpk());//璁惧spk -- Gitblit v1.8.0