From 63b0ee228bf52e8e9c4d7fc51a1117543c9a67af Mon Sep 17 00:00:00 2001
From: 刘卫锦 <lwj@hdlchina.com.cn>
Date: 星期一, 03 七月 2023 19:02:40 +0800
Subject: [PATCH] Merge branch 'master' of http://172.16.1.23:6688/r/~wjc/HDLPhotovoltaicDebugAPP

---
 app/src/main/java/com/hdl/photovoltaic/other/HdlResidenceLogic.java |  138 +++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 125 insertions(+), 13 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 fcdf6ec..83e6b6a 100644
--- a/app/src/main/java/com/hdl/photovoltaic/other/HdlResidenceLogic.java
+++ b/app/src/main/java/com/hdl/photovoltaic/other/HdlResidenceLogic.java
@@ -10,8 +10,14 @@
 import com.hdl.photovoltaic.internet.api.HttpApi;
 import com.hdl.photovoltaic.listener.BaseSuccessFailureCallBeak;
 import com.hdl.photovoltaic.listener.CloudCallBeak;
+import com.hdl.photovoltaic.ui.bean.CloudInverterDeviceBean;
 import com.hdl.photovoltaic.ui.bean.HouseInfoBean;
 import com.hdl.photovoltaic.ui.bean.HouseIdBean;
+import com.hdl.sdk.link.common.exception.HDLLinkException;
+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;
@@ -253,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);
                     }
                 }
             }
@@ -502,21 +514,121 @@
     /**
      * 鍒囨崲浣忓畢
      *
-     * @param homeId 浣忓畢id
+     * @param houseIdBean 浣忓畢Id瀵硅薄
      */
-    public Boolean switchHouse(String homeId) {
+    public void switchHouse(HouseIdBean houseIdBean) {
         String oidHomeId = UserConfigManage.getInstance().getHomeId();
+//        if (houseIdBean.getHomeId().equals(oidHomeId)) {
+//            return;
+//        }
+        //鍒犻櫎鏃х殑浣忓畢鏂囦欢澶�
         HdlFileLogic.getInstance().deleteDirectory(HdlFileLogic.getInstance().getCurrentHomeRootPath());
-        UserConfigManage.getInstance().setHomeId(homeId);
+        //閲嶆柊璁剧疆浣忓畢id
+        UserConfigManage.getInstance().setHomeId(houseIdBean.getHomeId());
+        //閲嶆柊璁剧疆鏈湴閫氳绉橀挜
+        UserConfigManage.getInstance().setLocalSecret(houseIdBean.getLocalSecret());
+        //閲嶆柊鍒涘缓浣忓畢鏂囦欢澶�
         HdlFileLogic.getInstance().createDirectory();
+        //閰嶇疆鏈湴閫氫俊鐨勪俊鎭�
+        initLocalLinkSdk();
 
-        HdlThreadLogic.runThread(new Runnable() {
+        HdlDeviceLogic.getInstance().searchGateway(new GatewayCallBack() {
             @Override
-            public void run() {
-                //鎼滅储涓�涓嬩綇瀹呰澶�
+            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) {
+                            //浜戠娌℃湁缁戝畾閫嗗彉鍣�,娓呯┖鏈湴鍒楄〃;
+                            HDLLinkLocalGateway.getInstance().getGatewayList().clear();
+                            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));
+                        }
+                    }
+
+                    @Override
+                    public void onFailure(Exception exception) {
+
+                    }
+                });
             }
-        }, null, null);
-        return true;
+
+            @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) {
+                            return;
+                        }
+                        if (list.size() <= 0) {
+                            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.setHomeId(UserConfigManage.getInstance().getHomeId());
+                            gatewayBean.setLocalEncrypt(true);
+                            gatewayBean.setMaster("true");
+                            HDLLinkLocalGateway.getInstance().getGatewayList().add(gatewayBean);
+                        }
+
+                    }
+
+                    @Override
+                    public void onFailure(Exception exception) {
+
+                    }
+                });
+            }
+        });
+
+
+    }
+
+    /**
+     * 鍒犻櫎鏈湴閫嗗彉鍣�
+     *
+     * @param sid 璁惧sid
+     */
+    private void removeInverter(String sid) {
+        //鑾峰彇鏈湴鐨勬绫虫尝
+        GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getLocalGateway(sid);
+        if (gatewayBean != null) {
+            HDLLinkLocalGateway.getInstance().getGatewayList().remove(gatewayBean);
+        }
+    }
+
+    /**
+     * 閰嶇疆鏈湴閫氫俊鐨勪俊鎭�(tcp閫氫俊鐢ㄥ埌)
+     */
+    private void initLocalLinkSdk() {
+        HDLLinkConfig.getInstance().setHomeId(UserConfigManage.getInstance().getHomeId());
+        HDLLinkConfig.getInstance().setLocalSecret(UserConfigManage.getInstance().getLocalSecret());
+
+
     }
 
 

--
Gitblit v1.8.0