mac
2024-05-21 ed7ae7fcc3adf66fbcc2b9481692791e9a167d04
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package com.hdl.photovoltaic.ui.powerstation;
 
 
import android.os.Bundle;
import android.view.View;
 
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
 
import com.hdl.linkpm.sdk.core.exception.HDLException;
import com.hdl.photovoltaic.R;
import com.hdl.photovoltaic.base.CustomBaseActivity;
import com.hdl.photovoltaic.config.ConstantManage;
import com.hdl.photovoltaic.config.UserConfigManage;
import com.hdl.photovoltaic.databinding.ActivityHouseListEditBinding;
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.HdlResidenceLogic;
import com.hdl.photovoltaic.other.HdlThreadLogic;
import com.hdl.photovoltaic.ui.bean.CloudInverterDeviceBean;
import com.hdl.photovoltaic.ui.bean.HouseIdBean;
import com.hdl.photovoltaic.ui.newC.adapter.HouseListAdapter;
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 org.greenrobot.eventbus.EventBus;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * 电站编辑(B端)
 */
public class HouseListEditActivity extends CustomBaseActivity {
 
    private ActivityHouseListEditBinding viewBinding;
 
    private HouseListAdapter houseListAdapter;
    private List<HouseIdBean> houseListBeanIDList;
 
 
    @Override
    public Object getContentView() {
        viewBinding = ActivityHouseListEditBinding.inflate(getLayoutInflater());
        return viewBinding.getRoot();
    }
 
    @Override
    public void onBindView(Bundle savedInstanceState) {
        setNotificationBarBackgroundColor(CustomColor.white);
        setStatusBarTextColor();
        //初始化数据
        initData();
        //初始化
        initView();
        //初始化界面监听器
        initEvent();
    }
 
    private void initData() {
        this.houseListBeanIDList = new ArrayList<>();
        this.houseListBeanIDList.addAll(HdlResidenceLogic.getInstance().getHouseIdList());
        for (int i = 0; i < this.houseListBeanIDList.size(); i++) {
            this.houseListBeanIDList.get(i).setDelIcon(true);
            this.houseListBeanIDList.get(i).setMove(true);
 
        }
    }
 
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        sendEventBus();
    }
 
    private void sendEventBus() {
        HdlCommonLogic.getInstance().postEventBus(ConstantManage.homepage_title_tab_switch, ConstantManage.station_edit);
        finish();
    }
 
    private void initEvent() {
        //返回按钮
        viewBinding.toolbarTopFragmentHouseListRl.topBackLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendEventBus();
            }
        });
 
        //设置下拉箭头颜色
        viewBinding.fragmentHouseSrl.setColorSchemeResources(R.color.text_FF245EC3);
        //列表下拉按钮
        viewBinding.fragmentHouseSrl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                downReadData(true);
            }
        });
        if (houseListAdapter != null) {
            //电站移动位置按钮
            houseListAdapter.setMoveOnclickListener(new HouseListAdapter.OnMoveClickListener() {
                @Override
                public void onMoveClick(int position, HouseIdBean houseIdBean) {
                    if (position == 0) {
                        HdlThreadLogic.toast(_mActivity, getString(R.string.already_the_first_one));
                        return;
                    }
                    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();//初始化缓存数据
                            houseListAdapter.setList(houseListBeanIDList);//重新刷新列表
                            nullDataUpdateUi(houseListBeanIDList);//检测数据是否为空
                        }
 
                        @Override
                        public void onFailure(HDLException e) {
                            HdlThreadLogic.toast(_mActivity, e);
                        }
                    });
                }
            });
 
            //电站移动位置按钮
            houseListAdapter.setDelOnclickListener(new HouseListAdapter.OnDelClickListener() {
                @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();//初始化缓存数据
//                                    houseListAdapter.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();
                        }
                    });
 
                }
            });
        }
 
    }
 
    private void initView() {
        viewBinding.toolbarTopFragmentHouseListRl.getRoot().setBackgroundColor(getColor(R.color.text_FFFFFFFF));
        viewBinding.toolbarTopFragmentHouseListRl.topBackLl.setVisibility(View.VISIBLE);
        viewBinding.toolbarTopFragmentHouseListRl.topLeftIv.setImageResource(R.drawable.back_c);
        viewBinding.toolbarTopFragmentHouseListRl.topTitleTv.setText(R.string.power_station_editing);
        LinearLayoutManager linearLayout = new LinearLayoutManager(_mActivity);
        houseListAdapter = new HouseListAdapter(_mActivity);
        viewBinding.fragmentHouseSrlListRc.setLayoutManager(linearLayout);
        viewBinding.fragmentHouseSrlListRc.setAdapter(houseListAdapter);
        houseListAdapter.setList(this.houseListBeanIDList);
        this.nullDataUpdateUi(houseListBeanIDList);
    }
 
    /**
     * 删除电站,逆变器
     * (开始删除电站,同时,向逆变器发送初始化逆变器指令,无须处理结果)
     *
     * @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().initializeInverter(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();//初始化缓存数据
                houseListAdapter.setList(houseListBeanIDList);//重新刷新列表
                nullDataUpdateUi(houseListBeanIDList);//检测数据是否为空
            }
 
            @Override
            public void onFailure(HDLException e) {
                hideLoading();
                HdlThreadLogic.toast(_mActivity, e);
            }
        });
    }
 
    /**
     * 刷新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 (houseListAdapter != null) {
                                initData();
                                setAllItemEdit(true);
                                //更新UI
                                houseListAdapter.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);
            }
        });
    }
 
    /**
     * 设置全部缓存数据编辑状态
     *
     * @param edit true表示在编辑状态
     */
    private void setAllItemEdit(boolean edit) {
        for (int i = 0; i < houseListBeanIDList.size(); i++) {
            houseListBeanIDList.get(i).setMove(edit);
        }
    }
 
    /**
     * 没有电站列表的样式
     */
    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);
    }
}