mac
2023-10-10 32b5f366ceaad4aa9a33eccf0b109eef24175495
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
package com.hdl.photovoltaic.other;
 
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
 
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.hdl.linkpm.sdk.core.exception.HDLException;
import com.hdl.photovoltaic.bean.HttpResponsePack;
import com.hdl.photovoltaic.config.UserConfigManage;
import com.hdl.photovoltaic.internet.HttpClient;
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.nio.charset.StandardCharsets;
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;
 
    /**
     * 获取当前对象
     *
     * @return HdlResidenceLogic
     */
    public static synchronized HdlResidenceLogic getInstance() {
        if (sHdlResidenceLogic == null) {
            synchronized (HdlResidenceLogic.class) {
                if (sHdlResidenceLogic == null) {
                    sHdlResidenceLogic = new HdlResidenceLogic();
                }
            }
 
        }
        return sHdlResidenceLogic;
    }
 
    /**
     * 获取住宅列表
     *
     * @return 住宅列表
     */
    public List<HouseInfoBean> getHouseInfoList() {
        return houseInfoList;
    }
 
    public void setHouseInfoList(List<HouseInfoBean> houseInfoList) {
        if (houseInfoList == null) {
            houseInfoList = new ArrayList<>();
        }
        this.houseInfoList = houseInfoList;
    }
 
    /**
     * 获取住宅ID列表
     *
     * @return 住宅ID列表
     */
    public List<HouseIdBean> getHouseIdList() {
        return houseIdList;
    }
 
    public void setHouseIdList(List<HouseIdBean> houseIdList) {
        if (houseIdList == null) {
            houseIdList = new ArrayList<>();
        }
        this.houseIdList = houseIdList;
    }
 
    //住宅ID列表
    private List<HouseIdBean> houseIdList = new ArrayList<>();
    //住宅详情列表
    private List<HouseInfoBean> houseInfoList = new ArrayList<>();
 
 
    /**
     * 获取住宅列表
     *
     * @param cloudCallBeak -
     */
    public void getHouseList(CloudCallBeak<Boolean> cloudCallBeak) {
        //获取住宅ID列表
        getResidenceIdList("", "", new CloudCallBeak<List<HouseIdBean>>() {
            @Override
            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:正常(运行),2:离线,3:连接中,4:故障)
                                    //添加到本地缓存
                                    addHouseToLocalMemory(houseInfoBean);
                                }
                                if (atomicInteger.get() == houseIdList.size()) {
                                    //最后一条退出
                                    if (cloudCallBeak != null) {
                                        cloudCallBeak.onSuccess(true);
                                    }
                                }
 
                            }
 
                            @Override
                            public void onFailure(HDLException e) {
                                atomicInteger.set(atomicInteger.get() + 1);
                                if (atomicInteger.get() == houseIdList.size()) {
                                    //最后一条退出
                                    if (cloudCallBeak != null) {
                                        cloudCallBeak.onFailure(e);
                                    }
                                }
                            }
                        });
 
                    }
                } else {
                    if (cloudCallBeak != null) {
                        cloudCallBeak.onSuccess(true);
                    }
                }
            }
 
            @Override
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(e);
                }
            }
        });
 
    }
 
    /**
     * 获取住宅(电站)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("zoneType", "password");//区域
        json.addProperty("pageNo", 1);//页码
        json.addProperty("pageSize", pageSize);//页数
        List<HouseIdBean> list = new ArrayList<>();
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak<String>() {
            @Override
            public void onSuccess(String jsonStr) {
                if (TextUtils.isEmpty(jsonStr)) {
                    if (cloudCallBeak != null) {
                        cloudCallBeak.onSuccess(list);
                    }
                }
                Gson gson = new Gson();
                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(), new CloudCallBeak<String>() {
                        @Override
                        public void onSuccess(String jsonStr) {
                            atomicInteger.set(atomicInteger.get() + 1);
                            Gson gson = new Gson();
                            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(HDLException e) {
                            atomicInteger.set(atomicInteger.get() + 1);
                            if (atomicInteger.get() == totalPage - 1) {
                                //最后一条退出
                                if (cloudCallBeak != null) {
                                    cloudCallBeak.onSuccess(list);
                                }
                            }
                        }
                    });
                }
 
            }
 
            @Override
            public void onFailure(HDLException exception) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(exception);
                }
            }
        });
 
 
    }
 
    /**
     * 获取住宅(电站)ID详情
     *
     * @param homeId        住宅id
     * @param cloudCallBeak 回调
     */
    public void getResidenceInfo(String homeId, CloudCallBeak<HouseInfoBean> cloudCallBeak) {
        String requestUrl = HttpApi.POST_PowerStation_Info;
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);//电站id
        //json.addProperty("zoneType", "password");//区域
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak<String>() {
            @Override
            public void onSuccess(String jsonStr) {
                if (TextUtils.isEmpty(jsonStr)) {
                    if (cloudCallBeak != null) {
                        cloudCallBeak.onSuccess(null);
                    }
                }
                Gson gson = new Gson();
                HouseInfoBean houseInfoBean = gson.fromJson(jsonStr, HouseInfoBean.class);
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(houseInfoBean);
                }
            }
 
            @Override
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(e);
                }
            }
        });
 
 
    }
 
 
    /**
     * 编辑住宅(电站),
     *
     * @param houseInfoBean -
     * @param cloudCallBeak -
     */
    public void editResidence(HouseInfoBean houseInfoBean, CloudCallBeak<Boolean> cloudCallBeak) {
        String requestUrl = HttpApi.POST_PowerStation_Edit;
        JsonObject json = new JsonObject();
        JsonObject location = new JsonObject();
        json.addProperty("homeId", houseInfoBean.getHomeId());
        location.addProperty("nationCode", houseInfoBean.getLocation().getNationCode());
        location.addProperty("nationName", houseInfoBean.getLocation().getNationName());
        location.addProperty("provinceCode", houseInfoBean.getLocation().getProvinceCode());
        location.addProperty("provinceName", houseInfoBean.getLocation().getProvinceName());
        location.addProperty("cityCode", houseInfoBean.getLocation().getCityCode());
        location.addProperty("cityName", houseInfoBean.getLocation().getCityName());
        json.add("location", location);
        json.addProperty("address", houseInfoBean.getAddress());
        json.addProperty("latitude", houseInfoBean.getLatitude());
        json.addProperty("longitude", houseInfoBean.getLongitude());
        json.addProperty("homeName", houseInfoBean.getHomeName());
        json.addProperty("timezone", houseInfoBean.getTimezone());
        json.addProperty("powerStationType", houseInfoBean.getPowerStationType());
        json.addProperty("workMode", houseInfoBean.getWorkMode());
        json.addProperty("installedCapacity", houseInfoBean.getInstalledCapacity());
        json.addProperty("productionTime", houseInfoBean.getProductionTime());
        json.addProperty("monetaryUnit", houseInfoBean.getMonetaryUnit());
        json.addProperty("electrovalence", houseInfoBean.getElectrovalence());
        json.addProperty("totalCost", houseInfoBean.getTotalCost());
        json.addProperty("zoneType", houseInfoBean.getZoneType());
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak<String>() {
            @Override
            public void onSuccess(String str) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(true);
                }
 
            }
 
            @Override
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(e);
                }
            }
        });
    }
 
    /**
     * 删除住宅(电站)
     *
     * @param homeId        -
     * @param cloudCallBeak -
     */
    public void delResidence(String homeId, CloudCallBeak<Boolean> cloudCallBeak) {
        String requestUrl = HttpApi.POST_PowerStation_Remove;
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);//电站id
        //json.addProperty("zoneType", "password");//区域
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak<String>() {
            @Override
            public void onSuccess(String str) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(true);
                }
 
            }
 
            @Override
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(e);
                }
            }
        });
 
    }
 
    /**
     * 添加住宅(电站)到云端
     *
     * @param houseInfoBean -
     * @param cloudCallBeak -
     */
    public void addHouseToCloud(HouseInfoBean houseInfoBean, CloudCallBeak<Boolean> cloudCallBeak) {
        String requestUrl = HttpApi.POST_PowerStation_Create;
        JsonObject json = new JsonObject();
        JsonObject location = new JsonObject();
        json.addProperty("powerStationImage", "");
        location.addProperty("nationCode", houseInfoBean.getLocation().getNationCode());
        location.addProperty("nationName", houseInfoBean.getLocation().getNationName());
        location.addProperty("provinceCode", houseInfoBean.getLocation().getProvinceCode());
        location.addProperty("provinceName", houseInfoBean.getLocation().getProvinceName());
        location.addProperty("cityCode", houseInfoBean.getLocation().getCityCode());
        location.addProperty("cityName", houseInfoBean.getLocation().getCityName());
        json.add("location", location);
        json.addProperty("address", houseInfoBean.getAddress());
        json.addProperty("latitude", houseInfoBean.getLatitude());
        json.addProperty("longitude", houseInfoBean.getLongitude());
        json.addProperty("homeName", houseInfoBean.getHomeName());
        json.addProperty("timezone", houseInfoBean.getTimezone());
        json.addProperty("powerStationType", houseInfoBean.getPowerStationType());
        json.addProperty("workMode", houseInfoBean.getWorkMode());
        json.addProperty("installedCapacity", houseInfoBean.getInstalledCapacity());
        json.addProperty("productionTime", houseInfoBean.getProductionTime());
        json.addProperty("monetaryUnit", houseInfoBean.getMonetaryUnit());
        json.addProperty("electrovalence", houseInfoBean.getElectrovalence());
        json.addProperty("totalCost", houseInfoBean.getTotalCost());
        json.addProperty("zoneType", houseInfoBean.getZoneType());
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak<String>() {
            @Override
            public void onSuccess(String str) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(true);
                }
 
            }
 
            @Override
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(e);
                }
            }
        });
 
 
    }
 
    /**
     * 获取住宅图片
     */
    public void getResidenceImage(String imageUrl, CloudCallBeak<Bitmap> cloudCallBeak) {
        String requestUrl = imageUrl;
        JsonObject json = new JsonObject();
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak<String>() {
            @Override
            public void onSuccess(String jsonStr) {
                if (TextUtils.isEmpty(jsonStr)) {
                    if (cloudCallBeak != null) {
                        cloudCallBeak.onSuccess(null);
                    }
                    return;
                }
                Bitmap bitmap = BitmapFactory.decodeByteArray(jsonStr.getBytes(), 0, jsonStr.getBytes().length);
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(bitmap);
                }
            }
 
            @Override
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(e);
                }
            }
        });
 
 
    }
 
    /**
     * 添加【住宅详情】到本地缓存
     *
     * @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 住宅找不到返回null
     */
    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)) {
            HdlLogLogic.print("点击同一个住宅");
            return;
        }
        //删除旧的住宅文件夹
        HdlFileLogic.getInstance().deleteDirectory(HdlFileLogic.getInstance().getCurrentHomeRootPath());
        //重新设置住宅id
        UserConfigManage.getInstance().setHomeId(houseIdBean.getHomeId());
        //重新设置本地通讯秘钥
        UserConfigManage.getInstance().setLocalSecret(houseIdBean.getLocalSecret());
        //配置本地通信的信息
        HDLLinkConfig.getInstance().setHomeId(UserConfigManage.getInstance().getHomeId());
        HDLLinkConfig.getInstance().setLocalSecret(UserConfigManage.getInstance().getLocalSecret());
        //重新创建住宅文件夹
        HdlFileLogic.getInstance().createDirectory();
        //进去住宅详情搜索一下逆变器列表,目的是建立通讯通道;
        HdlDeviceLogic.getInstance().searchGateway(null);
    }
 
    static class HouseBeanClass {
 
        //总条数
        private long totalCount;
        //总页数
        private long totalPage;
        //当前页
        private long pageNo;
        //页数
        private long pageSize;
        //电站列表
        private List<HouseIdBean> list;
 
        public long getTotalCount() {
            return totalCount;
        }
 
        public void setTotalCount(long totalCount) {
            this.totalCount = totalCount;
        }
 
        public long getTotalPage() {
            return totalPage;
        }
 
        public void setTotalPage(long totalPage) {
            this.totalPage = totalPage;
        }
 
        public long getPageNo() {
            return pageNo;
        }
 
        public void setPageNo(long pageNo) {
            this.pageNo = pageNo;
        }
 
        public long getPageSize() {
            return pageSize;
        }
 
        public void setPageSize(long pageSize) {
            this.pageSize = pageSize;
        }
 
        public List<HouseIdBean> getList() {
            return list == null ? new ArrayList<>() : list;
        }
 
        public void setList(List<HouseIdBean> list) {
            this.list = list;
        }
    }
 
}