From 9dc800193b64f71612cbc7190da5b4c07ad4e985 Mon Sep 17 00:00:00 2001
From: wjc <1243177876@qq.com>
Date: 星期四, 29 六月 2023 14:19:44 +0800
Subject: [PATCH] 2023年06月29日14:19:42

---
 app/src/main/java/com/hdl/photovoltaic/other/HdlResidenceLogic.java |  387 +++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 333 insertions(+), 54 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 0a4314e..6f7334a 100644
--- a/app/src/main/java/com/hdl/photovoltaic/other/HdlResidenceLogic.java
+++ b/app/src/main/java/com/hdl/photovoltaic/other/HdlResidenceLogic.java
@@ -1,5 +1,7 @@
 package com.hdl.photovoltaic.other;
 
+import android.text.TextUtils;
+
 import com.google.gson.Gson;
 import com.google.gson.JsonObject;
 import com.hdl.photovoltaic.bean.HttpResponsePack;
@@ -8,16 +10,24 @@
 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.HouseListBean;
+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 java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
 
 /**
  * 浣忓畢閫昏緫鐣岄潰
  */
 public class HdlResidenceLogic {
+
+    public static final int pageSize = 50;
 
     private static volatile HdlResidenceLogic sHdlResidenceLogic;
 
@@ -38,73 +48,103 @@
         return sHdlResidenceLogic;
     }
 
+    /**
+     * 鑾峰彇浣忓畢鍒楄〃
+     *
+     * @return 浣忓畢鍒楄〃
+     */
     public List<HouseInfoBean> getHouseInfoList() {
         return houseInfoList;
     }
 
     public void setHouseInfoList(List<HouseInfoBean> houseInfoList) {
+        if (houseInfoList == null) {
+            houseInfoList = new ArrayList<>();
+        }
         this.houseInfoList = houseInfoList;
     }
 
-    public List<HouseListBean> getHouseInfoBeanList() {
-        return houseList;
+    /**
+     * 鑾峰彇浣忓畢ID鍒楄〃
+     *
+     * @return 浣忓畢ID鍒楄〃
+     */
+    public List<HouseIdBean> getHouseIdList() {
+        return houseIdList;
     }
 
-    public void setHouseInfoBeanList(List<HouseListBean> houseListBeanList) {
-        this.houseList = houseListBeanList;
+    public void setHouseIdList(List<HouseIdBean> houseIdList) {
+        if (houseIdList == null) {
+            houseIdList = new ArrayList<>();
+        }
+        this.houseIdList = houseIdList;
     }
 
-    //浣忓畢鍒楄〃
-    private List<HouseListBean> houseList = new ArrayList<>();
+    //浣忓畢ID鍒楄〃
+    private List<HouseIdBean> houseIdList = new ArrayList<>();
     //浣忓畢璇︽儏鍒楄〃
     private List<HouseInfoBean> houseInfoList = new ArrayList<>();
 
-    public HouseListBean getHouseByHouseId(String homeId) {
-        return new HouseListBean();
-    }
-
 
     /**
-     * 鑾峰彇浣忓畢(鐢电珯)鍒楄〃
+     * 鑾峰彇浣忓畢鍒楄〃
      *
-     * @param pageNo   褰撳墠椤垫暟
-     * @param pageSize 涓�椤靛ぇ灏�
+     * @param cloudCallBeak -
      */
-    public void getResidenceList(int pageNo, int pageSize, CloudCallBeak<Boolean> cloudCallBeak) {
-        if (pageNo == 1) {
-            //绗竴娆¤繘鏉ユ竻绌哄垪琛�
-            houseList.clear();
-        }
-        String requestUrl = HttpApi.POST_PowerStation_List;
-        JsonObject json = new JsonObject();
-//        json.addProperty("powerSort", "descending");//鍙戠數鍔熺巼鎺掑簭(descending:闄嶅簭ascending:鍗囧簭)
-//        json.addProperty("todayElectricitySort", "descending");//浠婃棩鍙戠數閲忔帓搴�
-//        json.addProperty("createTimeSort", "descending");//鍒涘缓鏃堕棿鎺掑簭
-//        json.addProperty("zoneType", "password");//鍖哄煙
-        json.addProperty("pageNo", pageNo);//椤电爜
-        json.addProperty("pageSize", pageSize);//椤垫暟
-
-        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), true, true, new BaseSuccessFailureCallBeak() {
+    public void getHouseList(CloudCallBeak<Boolean> cloudCallBeak) {
+        //鑾峰彇浣忓畢ID鍒楄〃
+        getResidenceIdList("", "", new CloudCallBeak<List<HouseIdBean>>() {
             @Override
-            public void onSuccess(HttpResponsePack httpResponsePack) {
-                if (httpResponsePack != null && httpResponsePack.getData() != null) {
-                    Gson gson = new Gson();
-                    String jsonStr = gson.toJson(httpResponsePack.getData().toString());
-                    HouseInfoBeanClass houseInfoBeanClass = gson.fromJson(jsonStr, HouseInfoBeanClass.class);
-                    houseList.addAll(houseInfoBeanClass.getList());
-                    //鎬诲叡鏈夊灏戦〉
-                    long totalPage = houseInfoBeanClass.totalPage;
-                    if (totalPage == houseInfoBeanClass.getPageNo()) {
-                        if (cloudCallBeak != null) {
-                            cloudCallBeak.onSuccess(true);
-                        }
-                        return;
-                    }
-                    getResidenceList(pageNo + 1, 100, cloudCallBeak);
+            public void onSuccess(List<HouseIdBean> houseListBeanIDList) {
+                if (houseListBeanIDList != null && houseListBeanIDList.size() > 0) {
+                    houseIdList.clear();
+                    houseIdList.addAll(houseListBeanIDList);
+                    //璁板綍璇锋眰鏉℃暟
+                    AtomicInteger atomicInteger = new AtomicInteger(0);
+                    for (int i = 0; i < houseIdList.size(); i++) {
+                        HouseIdBean houseIdBean = houseIdList.get(i);
+                        //鑾峰彇浣忓畢璇︽儏鍒楄〃
+                        getResidenceInfo(houseIdBean.getHomeId(), new CloudCallBeak<HouseInfoBean>() {
+                            @Override
+                            public void onSuccess(HouseInfoBean houseInfoBean) {
+                                atomicInteger.set(atomicInteger.get() + 1);
+                                if (houseInfoBean != null) {
+                                    houseInfoBean.setHomeImage(houseIdBean.getHomeImage());//鐢电珯鍥剧墖
+                                    houseInfoBean.setInstalledCapacity(houseIdBean.getInstalledCapacity());//瑁呮満瀹归噺
+                                    houseInfoBean.setPower(houseIdBean.getPower());//鍙戠數鍔熺巼
+                                    houseInfoBean.setTodayElectricity(houseIdBean.getTodayElectricity());//浠婂ぉ鍙戠數閲�
+                                    houseInfoBean.setPowerStationStatus(houseIdBean.getPowerStationStatus());//鐢电珯鐘舵��(鐢电珯鐘舵��	1:姝e父(杩愯),2:绂荤嚎,3:杩炴帴涓�,4:鏁呴殰)
+                                    //娣诲姞鍒版湰鍦扮紦瀛�
+                                    addHouseToLocalMemory(houseInfoBean);
+                                }
+                                if (atomicInteger.get() == houseIdList.size()) {
+                                    //鏈�鍚庝竴鏉¢��鍑�
+                                    if (cloudCallBeak != null) {
+                                        cloudCallBeak.onSuccess(true);
+                                    }
+                                }
 
+                            }
+
+                            @Override
+                            public void onFailure(Exception exception) {
+                                atomicInteger.set(atomicInteger.get() + 1);
+                                if (atomicInteger.get() == houseIdList.size()) {
+                                    //鏈�鍚庝竴鏉¢��鍑�
+                                    if (cloudCallBeak != null) {
+                                        cloudCallBeak.onFailure(exception);
+                                    }
+                                }
+                            }
+                        });
+
+                    }
+                } else {
+                    if (cloudCallBeak != null) {
+                        cloudCallBeak.onSuccess(true);
+                    }
                 }
             }
-
 
             @Override
             public void onFailure(Exception exception) {
@@ -113,11 +153,99 @@
                 }
             }
         });
+
     }
 
+    /**
+     * 鑾峰彇浣忓畢(鐢电珯)ID鍒楄〃
+     *
+     * @param key      鍙戠數鍔熺巼鎺掑簭(powerSort);浠婃棩鍙戠數閲忔帓搴�(todayElectricitySort);鍒涘缓鏃堕棿鎺掑簭(createTimeSort);
+     * @param keyValue (descending:闄嶅簭ascending:鍗囧簭),
+     */
+    public void getResidenceIdList(String key, String keyValue, CloudCallBeak<List<HouseIdBean>> cloudCallBeak) {
+
+        String requestUrl = HttpApi.POST_PowerStation_List;
+        JsonObject json = new JsonObject();
+        if (!TextUtils.isEmpty(key) && !TextUtils.isEmpty(keyValue)) {
+            json.addProperty(key, keyValue);//鍙戠數鍔熺巼鎺掑簭(descending:闄嶅簭ascending:鍗囧簭)
+        }
+//        json.addProperty("powerSort", "descending");//鍙戠數鍔熺巼鎺掑簭(descending:闄嶅簭ascending:鍗囧簭)
+//        json.addProperty("todayElectricitySort", "descending");//浠婃棩鍙戠數閲忔帓搴�
+//        json.addProperty("createTimeSort", "descending");//鍒涘缓鏃堕棿鎺掑簭
+//        json.addProperty("zoneType", "password");//鍖哄煙
+        json.addProperty("pageNo", 1);//椤电爜
+        json.addProperty("pageSize", pageSize);//椤垫暟
+        List<HouseIdBean> list = new ArrayList<>();
+        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());
+                    HouseBeanClass houseInfoBeanClass = gson.fromJson(jsonStr, HouseBeanClass.class);
+                    list.addAll(houseInfoBeanClass.getList());
+                    //鎬诲叡鏈夊灏戦〉
+                    long totalPage = houseInfoBeanClass.totalPage;
+                    if (totalPage < 2) {
+                        if (cloudCallBeak != null) {
+                            cloudCallBeak.onSuccess(list);
+                        }
+                        return;
+                    }
+                    //璁板綍璇锋眰鏉℃暟
+                    AtomicInteger atomicInteger = new AtomicInteger(0);
+                    //浠庣浜岄〉鑾峰彇鏁版嵁
+                    for (int i = 2; i <= totalPage; i++) {
+                        json.addProperty("pageNo", i);//鏇存柊椤电爜
+                        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), true, true, new BaseSuccessFailureCallBeak() {
+                            @Override
+                            public void onSuccess(HttpResponsePack httpResponsePack) {
+                                atomicInteger.set(atomicInteger.get() + 1);
+                                Gson gson = new Gson();
+                                String jsonStr = gson.toJson(httpResponsePack.getData());
+                                HouseBeanClass houseInfoBeanClass = gson.fromJson(jsonStr, HouseBeanClass.class);
+                                list.addAll(houseInfoBeanClass.getList());
+                                if (atomicInteger.get() == totalPage - 1) {
+                                    //鏈�鍚庝竴鏉¢��鍑�
+                                    if (cloudCallBeak != null) {
+                                        cloudCallBeak.onSuccess(list);
+                                    }
+                                }
+                            }
+
+                            @Override
+                            public void onFailure(Exception exception) {
+                                atomicInteger.set(atomicInteger.get() + 1);
+                                if (atomicInteger.get() == totalPage - 1) {
+                                    //鏈�鍚庝竴鏉¢��鍑�
+                                    if (cloudCallBeak != null) {
+                                        cloudCallBeak.onSuccess(list);
+                                    }
+                                }
+                            }
+                        });
+                    }
+
+                } else {
+                    if (cloudCallBeak != null) {
+                        cloudCallBeak.onSuccess(list);
+                    }
+                }
+            }
+
+            @Override
+            public void onFailure(Exception exception) {
+                if (cloudCallBeak != null) {
+                    cloudCallBeak.onFailure(exception);
+                }
+            }
+        });
+
+
+    }
 
     /**
-     * 鑾峰彇浣忓畢(鐢电珯)璇︽儏
+     * 鑾峰彇浣忓畢(鐢电珯)ID璇︽儏
      *
      * @param homeId        浣忓畢id
      * @param cloudCallBeak 鍥炶皟
@@ -132,8 +260,9 @@
             public void onSuccess(HttpResponsePack httpResponsePack) {
                 if (httpResponsePack != null && httpResponsePack.getData() != null) {
                     Gson gson = new Gson();
-                    String jsonStr = gson.toJson(httpResponsePack.getData().toString());
+                    String jsonStr = gson.toJson(httpResponsePack.getData());
                     HouseInfoBean houseInfoBean = gson.fromJson(jsonStr, HouseInfoBean.class);
+
                     if (cloudCallBeak != null) {
                         cloudCallBeak.onSuccess(houseInfoBean);
                     }
@@ -153,7 +282,7 @@
 
 
     /**
-     * 缂栬緫浣忓畢(鐢电珯)
+     * 缂栬緫浣忓畢(鐢电珯),
      *
      * @param houseInfoBean -
      * @param cloudCallBeak -
@@ -232,12 +361,12 @@
     }
 
     /**
-     * 娣诲姞浣忓畢(鐢电珯)
+     * 娣诲姞浣忓畢(鐢电珯)鍒颁簯绔�
      *
      * @param houseInfoBean -
      * @param cloudCallBeak -
      */
-    public void addResidence(HouseInfoBean houseInfoBean, CloudCallBeak<Boolean> cloudCallBeak) {
+    public void addHouseToCloud(HouseInfoBean houseInfoBean, CloudCallBeak<Boolean> cloudCallBeak) {
         String requestUrl = HttpApi.POST_PowerStation_Create;
         JsonObject json = new JsonObject();
         JsonObject location = new JsonObject();
@@ -282,8 +411,158 @@
 
     }
 
+    /**
+     * 鑾峰彇浣忓畢鍥剧墖
+     */
+    public void getResidenceImage(String imageUrl, CloudCallBeak<List<HouseIdBean>> cloudCallBeak) {
+        String requestUrl = imageUrl;
+        JsonObject json = new JsonObject();
+        List<HouseIdBean> list = new ArrayList<>();
+        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());
+                    HouseBeanClass houseInfoBeanClass = gson.fromJson(jsonStr, HouseBeanClass.class);
+                } else {
+                    if (cloudCallBeak != null) {
+                        cloudCallBeak.onSuccess(list);
+                    }
+                }
+            }
 
-    static class HouseInfoBeanClass {
+            @Override
+            public void onFailure(Exception exception) {
+                if (cloudCallBeak != null) {
+                    cloudCallBeak.onFailure(exception);
+                }
+            }
+        });
+
+
+    }
+
+    /**
+     * 娣诲姞銆愪綇瀹呰鎯呫�戝埌鏈湴缂撳瓨
+     *
+     * @param houseInfoBean 浣忓畢璇︽儏
+     */
+    public void addHouseToLocalMemory(HouseInfoBean houseInfoBean) {
+        if (houseInfoBean == null) {
+            return;
+        }
+        int index = isExistHouse(houseInfoBean.getHomeId());
+        if (index > -1) {
+            houseInfoList.remove(index);
+            houseInfoList.add(index, houseInfoBean);
+        } else {
+            houseInfoList.add(houseInfoBean);
+        }
+    }
+
+    /**
+     * 鏌ヨ鏈湴缂撳瓨浣忓畢鍒楄〃鏄惁瀛樺湪浣忓畢
+     *
+     * @param homeId 浣忓畢id
+     * @return 鍒楄〃绱㈠紩
+     */
+    public int isExistHouse(String homeId) {
+        int index = -1;
+        for (int i = 0; i < houseInfoList.size(); i++) {
+            if (houseInfoList.get(i).getHomeId().equals(homeId)) {
+                index = i;
+                break;
+            }
+        }
+        return index;
+    }
+
+    /**
+     * 鑾峰彇浣忓畢璇︽儏
+     *
+     * @param homeId 浣忓畢id
+     * @return 浣忓畢鎵句笉鍒拌繑鍥瀗ull
+     */
+    public HouseInfoBean getHouse(String homeId) {
+        int position = isExistHouse(homeId);
+        if (position > -1) {
+            return houseInfoList.get(position);
+        }
+        return null;
+    }
+
+    /**
+     * 鍒犻櫎浣忓畢
+     *
+     * @param homeId 浣忓畢id
+     */
+    public void delHouse(String homeId) {
+        int position = isExistHouse(homeId);
+        if (position > -1) {
+            houseInfoList.remove(position);
+        }
+    }
+
+    /**
+     * 鍒囨崲浣忓畢
+     *
+     * @param houseIdBean 浣忓畢Id瀵硅薄
+     */
+    public void switchHouse(HouseIdBean houseIdBean) {
+        String oidHomeId = UserConfigManage.getInstance().getHomeId();
+        if (houseIdBean.getHomeId().equals(oidHomeId)) {
+            return;
+        }
+        //鍒犻櫎鏃х殑浣忓畢鏂囦欢澶�
+        HdlFileLogic.getInstance().deleteDirectory(HdlFileLogic.getInstance().getCurrentHomeRootPath());
+        //閲嶆柊璁剧疆浣忓畢id
+        UserConfigManage.getInstance().setHomeId(houseIdBean.getHomeId());
+        //閲嶆柊璁剧疆鏈湴閫氳绉橀挜
+        UserConfigManage.getInstance().setLocalSecret(houseIdBean.getLocalSecret());
+        //閲嶆柊鍒涘缓浣忓畢鏂囦欢澶�
+        HdlFileLogic.getInstance().createDirectory();
+        //閰嶇疆鏈湴閫氫俊鐨勪俊鎭�
+        initLocalLinkSdk();
+
+//        HdlDeviceLogic.getInstance().getInverterDeviceList(UserConfigManage.getInstance().getHomeId(), new CloudCallBeak<CloudInverterDeviceBean>() {
+//            @Override
+//            public void onSuccess(CloudInverterDeviceBean obj) {
+//
+//
+//            }
+//
+//            @Override
+//            public void onFailure(Exception exception) {
+//
+//            }
+//        });
+        HdlDeviceLogic.getInstance().searchGateway(new GatewayCallBack() {
+            @Override
+            public void onSuccess(List<GatewayBean> gatewayBeanList) {
+
+            }
+
+            @Override
+            public void onError(HDLLinkException e) {
+
+            }
+        });
+
+    }
+
+    /**
+     * 閰嶇疆鏈湴閫氫俊鐨勪俊鎭�(tcp閫氫俊鐢ㄥ埌)
+     */
+    private void initLocalLinkSdk() {
+        HDLLinkConfig.getInstance().setHomeId(UserConfigManage.getInstance().getHomeId());
+        HDLLinkConfig.getInstance().setLocalSecret(UserConfigManage.getInstance().getLocalSecret());
+
+
+    }
+
+
+    static class HouseBeanClass {
 
         //鎬绘潯鏁�
         private long totalCount;
@@ -294,7 +573,7 @@
         //椤垫暟
         private long pageSize;
         //鐢电珯鍒楄〃
-        private List<HouseListBean> list;
+        private List<HouseIdBean> list;
 
         public long getTotalCount() {
             return totalCount;
@@ -328,11 +607,11 @@
             this.pageSize = pageSize;
         }
 
-        public List<HouseListBean> getList() {
+        public List<HouseIdBean> getList() {
             return list == null ? new ArrayList<>() : list;
         }
 
-        public void setList(List<HouseListBean> list) {
+        public void setList(List<HouseIdBean> list) {
             this.list = list;
         }
     }

--
Gitblit v1.8.0