From bd80ca98ffb4f483ca2eba47051281fa69e790ac Mon Sep 17 00:00:00 2001
From: 刘卫锦 <lwj@hdlchina.com.cn>
Date: 星期一, 14 八月 2023 10:24:25 +0800
Subject: [PATCH] 代码同步

---
 app/src/main/java/com/hdl/photovoltaic/other/HdlResidenceLogic.java |  134 ++++++++++++++++++++++++++++++++++++++------
 1 files changed, 114 insertions(+), 20 deletions(-)

diff --git a/app/src/main/java/com/hdl/photovoltaic/other/HdlResidenceLogic.java b/app/src/main/java/com/hdl/photovoltaic/other/HdlResidenceLogic.java
index 6f7334a..370410a 100644
--- a/app/src/main/java/com/hdl/photovoltaic/other/HdlResidenceLogic.java
+++ b/app/src/main/java/com/hdl/photovoltaic/other/HdlResidenceLogic.java
@@ -17,6 +17,7 @@
 import com.hdl.sdk.link.core.bean.gateway.GatewayBean;
 import com.hdl.sdk.link.core.callback.GatewayCallBack;
 import com.hdl.sdk.link.core.config.HDLLinkConfig;
+import com.hdl.sdk.link.gateway.HDLLinkLocalGateway;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -258,13 +259,19 @@
         HttpClient.getInstance().requestHttp(requestUrl, json.toString(), true, true, new BaseSuccessFailureCallBeak() {
             @Override
             public void onSuccess(HttpResponsePack httpResponsePack) {
-                if (httpResponsePack != null && httpResponsePack.getData() != null) {
-                    Gson gson = new Gson();
-                    String jsonStr = gson.toJson(httpResponsePack.getData());
-                    HouseInfoBean houseInfoBean = gson.fromJson(jsonStr, HouseInfoBean.class);
+                try {
+                    if (httpResponsePack != null && httpResponsePack.getData() != null) {
+                        Gson gson = new Gson();
+                        String jsonStr = gson.toJson(httpResponsePack.getData());
+                        HouseInfoBean houseInfoBean = gson.fromJson(jsonStr, HouseInfoBean.class);
 
+                        if (cloudCallBeak != null) {
+                            cloudCallBeak.onSuccess(houseInfoBean);
+                        }
+                    }
+                } catch (Exception exception) {
                     if (cloudCallBeak != null) {
-                        cloudCallBeak.onSuccess(houseInfoBean);
+                        cloudCallBeak.onFailure(exception);
                     }
                 }
             }
@@ -511,9 +518,9 @@
      */
     public void switchHouse(HouseIdBean houseIdBean) {
         String oidHomeId = UserConfigManage.getInstance().getHomeId();
-        if (houseIdBean.getHomeId().equals(oidHomeId)) {
-            return;
-        }
+//        if (houseIdBean.getHomeId().equals(oidHomeId)) {
+//            return;
+//        }
         //鍒犻櫎鏃х殑浣忓畢鏂囦欢澶�
         HdlFileLogic.getInstance().deleteDirectory(HdlFileLogic.getInstance().getCurrentHomeRootPath());
         //閲嶆柊璁剧疆浣忓畢id
@@ -524,31 +531,118 @@
         HdlFileLogic.getInstance().createDirectory();
         //閰嶇疆鏈湴閫氫俊鐨勪俊鎭�
         initLocalLinkSdk();
+        getGatewayList(null);
 
-//        HdlDeviceLogic.getInstance().getInverterDeviceList(UserConfigManage.getInstance().getHomeId(), new CloudCallBeak<CloudInverterDeviceBean>() {
-//            @Override
-//            public void onSuccess(CloudInverterDeviceBean obj) {
-//
-//
-//            }
-//
-//            @Override
-//            public void onFailure(Exception exception) {
-//
-//            }
-//        });
+    }
+
+
+    public void getGatewayList(CloudCallBeak<Boolean> baseSuccessFailureCallBeak) {
         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) {
+                        if (list == null||list.size()==0) {
+                            //浜戠娌℃湁缁戝畾閫嗗彉鍣�,娓呯┖鏈湴鍒楄〃;
+                            HDLLinkLocalGateway.getInstance().getGatewayList().clear();
+                            if (baseSuccessFailureCallBeak != null) {
+                                baseSuccessFailureCallBeak.onSuccess(true);
+                            }
+                            return;
+                        }
+                        //鏀堕泦鍒犻櫎閫嗗彉鍣╯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 (baseSuccessFailureCallBeak != null) {
+                            baseSuccessFailureCallBeak.onSuccess(true);
+                        }
+                    }
+
+                    @Override
+                    public void onFailure(Exception exception) {
+                        if (baseSuccessFailureCallBeak != null) {
+                            baseSuccessFailureCallBeak.onFailure(exception);
+                        }
+                    }
+                });
             }
 
             @Override
             public void onError(HDLLinkException e) {
+                HdlDeviceLogic.getInstance().getCloudInverterDeviceList(UserConfigManage.getInstance().getHomeId(), new CloudCallBeak<List<CloudInverterDeviceBean>>() {
+                    @Override
+                    public void onSuccess(List<CloudInverterDeviceBean> list) {
+                        if (list == null) {
+                            if (baseSuccessFailureCallBeak != null) {
+                                baseSuccessFailureCallBeak.onSuccess(true);
+                            }
+                            return;
+                        }
+                        if (list.size() <= 0) {
+                            if (baseSuccessFailureCallBeak != null) {
+                                baseSuccessFailureCallBeak.onSuccess(true);
+                            }
+                            return;
+                        }
+                        HDLLinkLocalGateway.getInstance().getGatewayList().clear();
+                        for (int i = 0; i < list.size(); i++) {
+                            CloudInverterDeviceBean cloudInverterDeviceBean = list.get(i);
+                            GatewayBean gatewayBean = new GatewayBean();
+                            gatewayBean.setOid(cloudInverterDeviceBean.getOid());
+                            gatewayBean.setSid(cloudInverterDeviceBean.getSid());
+                            gatewayBean.setGatewayId(cloudInverterDeviceBean.getGatewayId());
+                            gatewayBean.setOnline(cloudInverterDeviceBean.isOnline());
+                            gatewayBean.setDevice_name(cloudInverterDeviceBean.getGatewayName());
+                            gatewayBean.setDevice_mac(cloudInverterDeviceBean.getOsn());
+                            gatewayBean.setHomeId(UserConfigManage.getInstance().getHomeId());
+                            gatewayBean.setLocalEncrypt(true);
+                            gatewayBean.setMaster("true");
+                            HDLLinkLocalGateway.getInstance().getGatewayList().add(gatewayBean);
+                        }
+                        if (baseSuccessFailureCallBeak != null) {
+                            baseSuccessFailureCallBeak.onSuccess(true);
+                        }
 
+                    }
+
+                    @Override
+                    public void onFailure(Exception exception) {
+                        if (baseSuccessFailureCallBeak != null) {
+                            baseSuccessFailureCallBeak.onFailure(exception);
+                        }
+                    }
+                });
             }
         });
+    }
 
+    /**
+     * 鍒犻櫎鏈湴閫嗗彉鍣�
+     *
+     * @param sid 璁惧sid
+     */
+    private void removeInverter(String sid) {
+        //鑾峰彇鏈湴
+        GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getLocalGateway(sid);
+        if (gatewayBean != null) {
+            HDLLinkLocalGateway.getInstance().getGatewayList().remove(gatewayBean);
+        }
     }
 
     /**

--
Gitblit v1.8.0