From 7e06d0fb18152ea0e7d898d25c88ef132af3ec6e Mon Sep 17 00:00:00 2001
From: mac <user@users-MacBook-Pro.local>
Date: 星期五, 29 三月 2024 15:26:19 +0800
Subject: [PATCH] 2024年03月29日15:26:15

---
 app/src/main/java/com/hdl/photovoltaic/ui/powerstation/HouseListFragment.java |  383 ++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 341 insertions(+), 42 deletions(-)

diff --git a/app/src/main/java/com/hdl/photovoltaic/ui/powerstation/HouseListFragment.java b/app/src/main/java/com/hdl/photovoltaic/ui/powerstation/HouseListFragment.java
index fdff614..4907c52 100644
--- a/app/src/main/java/com/hdl/photovoltaic/ui/powerstation/HouseListFragment.java
+++ b/app/src/main/java/com/hdl/photovoltaic/ui/powerstation/HouseListFragment.java
@@ -2,11 +2,10 @@
 
 import android.Manifest;
 import android.content.Context;
+import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.hardware.camera2.CameraManager;
 import android.os.Bundle;
-import android.os.SystemClock;
-import android.util.Log;
 import android.view.View;
 
 import androidx.annotation.NonNull;
@@ -14,37 +13,46 @@
 import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
 
 import com.google.gson.Gson;
-import com.google.gson.JsonObject;
+import com.hdl.linkpm.sdk.core.exception.HDLException;
 import com.hdl.photovoltaic.R;
+import com.hdl.photovoltaic.config.ConstantManage;
 import com.hdl.photovoltaic.config.UserConfigManage;
 import com.hdl.photovoltaic.databinding.FragmentHouseListBinding;
 import com.hdl.photovoltaic.base.CustomBaseFragment;
-import com.hdl.photovoltaic.databinding.LoadingConfirmInputBinding;
+import com.hdl.photovoltaic.enums.HomepageTitleTabSwitch;
+import com.hdl.photovoltaic.enums.ShowErrorMode;
+import com.hdl.photovoltaic.listener.CloudCallBeak;
+import com.hdl.photovoltaic.listener.LinkCallBack;
+import com.hdl.photovoltaic.other.HdlCommonLogic;
+import com.hdl.photovoltaic.other.HdlDeviceLogic;
 import com.hdl.photovoltaic.other.HdlLogLogic;
+import com.hdl.photovoltaic.other.HdlResidenceLogic;
 import com.hdl.photovoltaic.other.HdlThreadLogic;
 import com.hdl.photovoltaic.other.HdlUniLogic;
+import com.hdl.photovoltaic.ui.MyPowerStationActivity;
 import com.hdl.photovoltaic.ui.adapter.HouseInfoAdapter;
-import com.hdl.photovoltaic.ui.bean.HouseListBean;
-import com.hdl.photovoltaic.ui.device.FastScanActivity;
+import com.hdl.photovoltaic.ui.bean.CloudInverterDeviceBean;
+import com.hdl.photovoltaic.ui.bean.HouseIdBean;
 import com.hdl.photovoltaic.uni.HDLUniMP;
-import com.hdl.photovoltaic.uni.HDLUniMPSDKManager;
 import com.hdl.photovoltaic.utils.PermissionUtils;
-import com.hdl.photovoltaic.widget.ConfirmationInputDialog;
-
-import org.json.JSONException;
-import org.json.JSONObject;
+import com.hdl.photovoltaic.widget.DelayedConfirmationCancelDialog;
+import com.hdl.sdk.link.common.exception.HDLLinkException;
+import com.hdl.sdk.link.core.bean.eventbus.BaseEventBus;
+import com.hdl.sdk.link.core.bean.gateway.GatewayBean;
+import com.hdl.sdk.link.core.utils.mqtt.MqttRecvClient;
 
 import java.util.ArrayList;
 import java.util.List;
 
-import io.dcloud.feature.unimp.DCUniMPJSCallback;
-
+/**
+ * (浣忓畢)鐢电珯鍒楄〃-鐣岄潰
+ */
 public class HouseListFragment extends CustomBaseFragment {
     private FragmentHouseListBinding viewBinding;
     private HouseInfoAdapter houseInfoAdapter;
     private CameraManager manager;
 
-    private List<HouseListBean> houseListBeanList = null;
+    private List<HouseIdBean> houseListBeanIDList = null;
 
 
     @Override
@@ -56,23 +64,22 @@
     @Override
     public void onBindView(Bundle savedInstanceState) {
         manager = (CameraManager) _mActivity.getSystemService(Context.CAMERA_SERVICE);
-
         initData();
         //鍒濆鍖�
         initView();
         //鍒濆鍖栫晫闈㈢洃鍚櫒
         initEvent();
-
-
     }
 
     private void initEvent() {
 
+
+        //娣诲姞鐢电珯
         viewBinding.toolbarTopFragmentHouseListRl.topMoreIv.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
-                // 鎵撳紑灏忕▼搴忛〉闈�
-                HdlUniLogic.getInstance().openUniMP(HDLUniMP.UNI_EVENT_HOME_CREATION, null);
+                HdlUniLogic.getInstance().openUniMP(HDLUniMP.UNI_EVENT_OPEN_HOME_CREATION, null);
+
             }
         });
         //璁剧疆涓嬫媺绠ご棰滆壊
@@ -80,58 +87,272 @@
         viewBinding.fragmentHouseSrl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
             @Override
             public void onRefresh() {
-
-                viewBinding.fragmentHouseSrl.setRefreshing(false);
-                Log.d("HouseListFragment", "涓嬫媺鍒锋柊");
+                downReadData(true);
             }
         });
+        //杩涘叆鐢电珯璇︽儏,鍒犻櫎鐢电珯,绉诲姩鐢电珯浣嶇疆
         houseInfoAdapter.setNoOnclickListener(new HouseInfoAdapter.OnclickListener() {
             @Override
-            public void onClick(int position) {
+            public void onClick(int position, HouseIdBean houseIdBean) {
                 //鐐瑰嚮浣忓畢璇︽儏
-                HdlLogLogic.print("====鐐瑰嚮浣忓畢璇︽儏" + position);
-                if (houseListBeanList == null || houseListBeanList.size() <= 0) {
+                HdlLogLogic.print("鐐瑰嚮浣忓畢璇︽儏--->" + new Gson().toJson(houseIdBean), false);
+                HdlResidenceLogic.getInstance().switchHouse(houseIdBean);
+                String path = HDLUniMP.UNI_EVENT_OPEN_HOME_DETAILS + "?homeId=" + houseIdBean.getHomeId() + "&homeName=" + houseIdBean.getHomeName() + "&powerStationStatus=" + houseIdBean.getPowerStationStatus();
+                HdlUniLogic.getInstance().openUniMP(path, null);
+
+            }
+
+            @Override
+            public void onMoveClick(int position, HouseIdBean houseIdBean) {
+                if (position == 0) {
+                    HdlThreadLogic.toast(_mActivity, getString(R.string.already_the_first_one));
                     return;
                 }
-                HouseListBean houseListBean = houseListBeanList.get(position);
-                String path = HDLUniMP.UNI_EVENT_HOME_CREATION + "?homeId=" + houseListBean.getHomeId();
-                HdlUniLogic.getInstance().openUniMP(path, null);
+                String frontHomeId = "";
+                if (position > 1) {
+                    frontHomeId = houseListBeanIDList.get(position - 2).getHomeId();
+                }
+                HdlResidenceLogic.getInstance().moveResidence(houseIdBean.getHomeId(), frontHomeId, new CloudCallBeak<Boolean>() {
+                    @Override
+                    public void onSuccess(Boolean obj) {
+                        //绉诲姩鐢电珯浣嶇疆
+                        HdlResidenceLogic.getInstance().moveHouseId(houseIdBean.getHomeId());
+                        initData();//鍒濆鍖栫紦瀛樻暟鎹�
+                        houseInfoAdapter.setList(houseListBeanIDList);//閲嶆柊鍒锋柊鍒楄〃
+                        nullDataUpdateUi(houseListBeanIDList);//妫�娴嬫暟鎹槸鍚︿负绌�
+                    }
+
+                    @Override
+                    public void onFailure(HDLException e) {
+                        HdlThreadLogic.toast(_mActivity, e);
+                    }
+                });
+
+
+            }
+
+            @Override
+            public void onDelClick(int position, HouseIdBean houseIdBean) {
+
+                DelayedConfirmationCancelDialog delayedConfirmationCancelDialog = new DelayedConfirmationCancelDialog(_mActivity);
+                delayedConfirmationCancelDialog.setTitleContent(getString(R.string.loading_title_tip));
+                String homeName = "\"" + houseIdBean.getHomeName() + "\"";
+                delayedConfirmationCancelDialog.setContent(getString(R.string.delete_power_station).replace("%s", homeName));
+                delayedConfirmationCancelDialog.show();
+                delayedConfirmationCancelDialog.startCountdown(4);
+                delayedConfirmationCancelDialog.setYesOnclickListener(new DelayedConfirmationCancelDialog.onYesOnclickListener() {
+                    @Override
+                    public void Confirm() {
+                        delayedConfirmationCancelDialog.dismiss();
+                        showLoading(getString(R.string.deleting_please_wait));
+                        HdlDeviceLogic.getInstance().getCurrentHomeLocalAndCloudGatewayList(houseIdBean.getHomeId(), new CloudCallBeak<List<GatewayBean>>() {
+                            @Override
+                            public void onSuccess(List<GatewayBean> list) {
+                                deleteResidence(houseIdBean.getHomeId());
+                                initializeInverter(list);
+                            }
+
+                            @Override
+                            public void onFailure(HDLException e) {
+                                deleteResidence(houseIdBean.getHomeId());
+                            }
+                        });
+
+//                        //鍒犻櫎浣忓畢
+//                        HdlResidenceLogic.getInstance().delResidence(houseIdBean.getHomeId(), new CloudCallBeak<Boolean>() {
+//                            @Override
+//                            public void onSuccess(Boolean obj) {
+//                                HdlResidenceLogic.getInstance().delHouseId(houseIdBean.getHomeId());
+//                                initData();//鍒濆鍖栫紦瀛樻暟鎹�
+//                                houseInfoAdapter.setList(houseListBeanIDList);//閲嶆柊鍒锋柊鍒楄〃
+//                                nullDataUpdateUi(houseListBeanIDList);//妫�娴嬫暟鎹槸鍚︿负绌�
+//                            }
+//
+//                            @Override
+//                            public void onFailure(HDLException e) {
+//                                HdlThreadLogic.toast(_mActivity, e);
+//                            }
+//                        });
+                    }
+                });
+                delayedConfirmationCancelDialog.setNoOnclickListener(new DelayedConfirmationCancelDialog.onNoOnclickListener() {
+                    @Override
+                    public void Cancel() {
+                        delayedConfirmationCancelDialog.dismiss();
+                    }
+                });
+
+            }
+        });
+
+        //缂栬緫鐢电珯鎸夐挳
+        viewBinding.toolbarTopFragmentHouseListRl.topEditIv.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                startActivity(HouseListEditActivity.class);
             }
         });
     }
 
     private void initView() {
-        viewBinding.toolbarTopFragmentHouseListRl.topTitleTv.setText(R.string.my_power_station_鎴戠殑鐢电珯);
+        viewBinding.toolbarTopFragmentHouseListRl.topTitleTv.setText(R.string.my_power_station);
+        viewBinding.toolbarTopFragmentHouseListRl.topEditIv.setVisibility(View.VISIBLE);
+        viewBinding.toolbarTopFragmentHouseListRl.topEditIv.setImageResource(R.drawable.editor_house);
         viewBinding.toolbarTopFragmentHouseListRl.topMoreIv.setVisibility(View.VISIBLE);
         viewBinding.toolbarTopFragmentHouseListRl.topMoreIv.setImageResource(R.drawable.add);
         LinearLayoutManager linearLayout = new LinearLayoutManager(_mActivity);
-        houseInfoAdapter = new HouseInfoAdapter(this.houseListBeanList);
+        houseInfoAdapter = new HouseInfoAdapter(_mActivity);
         viewBinding.fragmentHouseSrlListRc.setLayoutManager(linearLayout);
         viewBinding.fragmentHouseSrlListRc.setAdapter(houseInfoAdapter);
+        houseInfoAdapter.setList(this.houseListBeanIDList);
+        this.nullDataUpdateUi(houseListBeanIDList);
 
 
     }
 
     private void initData() {
-        this.houseListBeanList = new ArrayList<>();
-        for (int i = 0; i < 11; i++) {
-            HouseListBean houseListBean = new HouseListBean();
-            houseListBean.setHomeName("鐢电珯" + i);
-            this.houseListBeanList.add(houseListBean);
-        }
-
-
+        this.houseListBeanIDList = new ArrayList<>();
+        this.houseListBeanIDList.addAll(HdlResidenceLogic.getInstance().getHouseIdList());
     }
+
+    /**
+     * 鏀跺埌EventBUs閫氱煡
+     *
+     * @param eventBus 鏁版嵁
+     */
+    @Override
+    public void onEventMessage(BaseEventBus eventBus) {
+        super.onEventMessage(eventBus);
+        if (HDLUniMP.UNI_EVENT_REPLY_HOME_MODEL.equals(eventBus.getTopic())) {
+            if (HDLUniMP.UNI_EVENT_REPLY_HOME_CREATION.equals(eventBus.getType())) {
+                //uin鍒涘缓鐢电珯鎴愬姛鍚庨�氱煡
+                downReadData(false);
+                if (eventBus.getData() != null) {
+                    Gson gson = new Gson();
+                    String json = eventBus.getData().toString();
+                    HouseIdBean houseIdBean = gson.fromJson(json, HouseIdBean.class);
+                    HdlResidenceLogic.getInstance().switchHouse(houseIdBean);
+
+                }
+            } else if (HDLUniMP.UNI_EVENT_REPLY_HOME_EDIT.equals(eventBus.getType())) {
+                //todo 鐜板湪榛樿鍒锋柊鍏ㄩ儴
+                //uin缂栬緫浣忓畢閫氱煡
+//                String homeId = HdlUniLogic.getInstance().getKeyValue("homeId", eventBus.getData());
+//                String home_name = HdlUniLogic.getInstance().getKeyValue("powerStationName", eventBus.getData());
+//                if (TextUtils.isEmpty(homeId) || TextUtils.isEmpty(home_name)) {
+//                    return;
+//                }
+//                int index = -1;
+//                for (int i = 0; i < houseListBeanIDList.size(); i++) {
+//                    HouseIdBean houseIdBean = houseListBeanIDList.get(i);
+//                    if (houseIdBean.getHomeId().equals(homeId)) {
+//                        index = i;
+//                        houseIdBean.setHomeName(home_name);
+//                        break;
+//                    }
+//                }
+//                if (index > -1) {
+//                    if (houseInfoAdapter != null) {
+//                        //鏇存柊鍗曚釜鏁版嵁
+//                        houseInfoAdapter.notifyItemChanged(index);
+//                    }
+//                }
+
+
+            }
+        } else if (HDLUniMP.UNI_EVENT_REPLY_HOME_CLOSE_HOME_DETAILS_PAGE.equals(eventBus.getType())) {
+            //uin鍏抽棴浣忓畢璇︽儏鐣岄潰閫氱煡
+            if (MqttRecvClient.getInstance() != null) {
+                MqttRecvClient.getInstance().removeAllTopic();
+            }
+            downReadData(false);
+
+        } else if (HDLUniMP.UNI_EVENT_REPLY_DEVICE_LIST.equals(eventBus.getType())) {
+            //杩涘幓浣忓畢璇︽儏uni璇诲彇閫嗗彉鍣ㄥ垪琛ㄦ垚鍔熷悗閫氱煡
+            for (int i = 0; i < HdlDeviceLogic.getInstance().getCurrentHomeGatewayList(UserConfigManage.getInstance().getHomeId()).size(); i++) {
+                String gatewayId = HdlDeviceLogic.getInstance().getCurrentHomeGatewayList(UserConfigManage.getInstance().getHomeId()).get(i).getGatewayId();
+                //瀛楃涓叉槸鑷繁鎸夎鍒欐嫾鎺ョ殑,閲岄潰娉ㄥ唽涓婚鏃朵細瑙f瀽瀛楃涓�,鍙嬁getGatewayId()鍊�;
+                String topic = "/user/" + gatewayId + "/#";
+                //杩涘幓浣忓畢璇︽儏寮�濮嬭闃呬富棰�
+                MqttRecvClient.getInstance().checkAndsubscribeAllTopics(topic);
+            }
+        } else if (eventBus.getTopic().equals(ConstantManage.homepage_title_tab_switch)) {
+            //鎺ユ敹澶栭儴鐐瑰嚮浜嬩欢
+            if (eventBus.getType().equals(HomepageTitleTabSwitch.powerstation.toString())) {
+                HdlLogLogic.print("姝e湪鐐瑰嚮銆愮數绔欍��");
+                initData();//鍒濆鍖栫紦瀛樻暟鎹�
+                houseInfoAdapter.setList(houseListBeanIDList);//閲嶆柊鍒锋柊鍒楄〃
+            }
+        }
+    }
+
+
+    /**
+     * 鍒锋柊UI
+     *
+     * @param isRefreshing 琛ㄧず鏄笅鎷夊埛鏂扮殑
+     */
+    private void downReadData(boolean isRefreshing) {
+        //鑾峰彇浣忓畢(鐢电珯)ID鍒楄〃
+        HdlResidenceLogic.getInstance().getResidenceIdList("", "", new CloudCallBeak<List<HouseIdBean>>() {
+            @Override
+            public void onSuccess(List<HouseIdBean> list) {
+                HdlThreadLogic.runMainThread(new Runnable() {
+                    @Override
+                    public void run() {
+                        if (isRefreshing) {
+                            //鍏抽棴涓嬫媺鍒锋柊鐨勫湀鍦�
+                            viewBinding.fragmentHouseSrl.setRefreshing(false);
+                        }
+                        if (list != null && list.size() > 0) {
+                            //鏇存柊缂撳瓨
+                            HdlResidenceLogic.getInstance().setHouseIdList(list);
+                            if (houseInfoAdapter != null) {
+                                initData();
+                                //鏇存柊UI
+                                houseInfoAdapter.setList(houseListBeanIDList);
+                            }
+
+                        }
+                        nullDataUpdateUi(list);
+                    }
+                }, _mActivity, ShowErrorMode.YES);
+
+            }
+
+            @Override
+            public void onFailure(HDLException e) {
+                HdlThreadLogic.runMainThread(new Runnable() {
+                    @Override
+                    public void run() {
+                        if (isRefreshing) {
+                            //鍏抽棴涓嬫媺鍒锋柊鐨勫湀鍦�
+                            viewBinding.fragmentHouseSrl.setRefreshing(false);
+                        }
+
+                    }
+                }, _mActivity, ShowErrorMode.YES);
+            }
+        });
+    }
+
 
     /**
      * 鐢宠鏉冮檺
      */
-    private void requestPermissions() {
-        String[] ary = PermissionUtils.checkPermission(_mActivity, new String[]{Manifest.permission.CAMERA});
+    private void requestPermissions(PermissionsResultCallback permissionsResultCallback) {
+
+
+        mPermissionsResultCallback = permissionsResultCallback;
+
+        String[] s = new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CAMERA,};
+        String[] ary = PermissionUtils.checkPermission(_mActivity, s);
         if (ary.length > 0) {
             requestPermissions(ary, PermissionUtils.STATUS_SUCCESS);
         } else {
-            startActivity(FastScanActivity.class);
+            if (mPermissionsResultCallback != null) {
+                mPermissionsResultCallback.succeed();
+            }
         }
     }
 
@@ -141,7 +362,13 @@
         if (requestCode == PermissionUtils.STATUS_SUCCESS) {
             for (int i = 0; i < permissions.length; i++) {
                 if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
-                    startActivity(FastScanActivity.class);
+                    if (mPermissionsResultCallback != null) {
+                        mPermissionsResultCallback.succeed();
+                    }
+                } else {
+                    if (mPermissionsResultCallback != null) {
+                        mPermissionsResultCallback.failing();
+                    }
                 }
 
             }
@@ -150,4 +377,76 @@
     }
 
 
+    private PermissionsResultCallback mPermissionsResultCallback;
+
+    public interface PermissionsResultCallback {
+
+        void succeed();
+
+        void failing();
+
+    }
+
+    /**
+     * 娌℃湁鐢电珯鍒楄〃鐨勬牱寮�
+     */
+    private void nullDataUpdateUi(List<HouseIdBean> list) {
+        HdlCommonLogic.getInstance().nullDataUpdateUi(_mActivity, viewBinding.nullDataIc.getRoot(), viewBinding.nullDataIc.nullDataGifAnimationIv, viewBinding.nullDataIc.nullDataTv, getString(R.string.my_power_station_data_null), list != null && list.size() > 0);
+    }
+
+    /**
+     * 鍒犻櫎鐢电珯,閫嗗彉鍣�
+     * (寮�濮嬪垹闄ょ數绔欙紝鍚屾椂锛屽悜閫嗗彉鍣ㄥ彂閫佸垵濮嬪寲閫嗗彉鍣ㄦ寚浠�,鏃犻』澶勭悊缁撴灉)
+     *
+     * @param list 璁惧鍒楄〃
+     */
+    private void initializeInverter(List<GatewayBean> list) {
+        if (list == null || list.size() == 0) {
+            return;
+        }
+        for (int i = 0; i < list.size(); i++) {
+            GatewayBean gatewayBean = list.get(i);
+            HdlDeviceLogic.getInstance().initializeGateway(gatewayBean.getDevice_mac(), new LinkCallBack<Boolean>() {
+                @Override
+                public void onSuccess(Boolean obj) {
+//                                HdlLogLogic.print("鍒濆鍖栭�嗗彉鍣ㄦ垚鍔�-->mac:" + cloudInverterDeviceBean.getOsn(),true);
+                }
+
+                @Override
+                public void onError(HDLLinkException e) {
+//                                HdlLogLogic.print("鍒濆鍖栭�嗗彉鍣ㄥけ璐�-->mac:" + cloudInverterDeviceBean.getOsn(),true);
+                }
+            });
+        }
+    }
+
+    /**
+     * 鍒犻櫎鐢电珯
+     *
+     * @param homeId 鐢电珯id
+     */
+    private void deleteResidence(String homeId) {
+
+        //鍒犻櫎浣忓畢
+        HdlResidenceLogic.getInstance().delResidence(homeId, new CloudCallBeak<Boolean>() {
+            @Override
+            public void onSuccess(Boolean obj) {
+                hideLoading();
+
+                HdlResidenceLogic.getInstance().delHouseId(homeId);
+                initData();//鍒濆鍖栫紦瀛樻暟鎹�
+                houseInfoAdapter.setList(houseListBeanIDList);//閲嶆柊鍒锋柊鍒楄〃
+                nullDataUpdateUi(houseListBeanIDList);//妫�娴嬫暟鎹槸鍚︿负绌�
+            }
+
+            @Override
+            public void onFailure(HDLException e) {
+                hideLoading();
+
+                HdlThreadLogic.toast(_mActivity, e);
+            }
+        });
+    }
+
+
 }

--
Gitblit v1.8.0