wjc
2023-08-21 0a18a8180bc6040c941b07df1be1f7b726b4c155
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
package com.hdl.photovoltaic.other;
 
import android.text.TextUtils;
 
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.hdl.hdlhttp.HxHttp;
import com.hdl.linkpm.sdk.core.exception.HDLException;
import com.hdl.photovoltaic.bean.HttpResponsePack;
import com.hdl.photovoltaic.bean.LocalResponse;
import com.hdl.photovoltaic.config.AppConfigManage;
import com.hdl.photovoltaic.config.UserConfigManage;
import com.hdl.photovoltaic.internet.HttpClient;
import com.hdl.photovoltaic.internet.TcpClient;
import com.hdl.photovoltaic.internet.api.HttpApi;
import com.hdl.photovoltaic.internet.api.TopicApi;
import com.hdl.photovoltaic.listener.BaseSuccessFailureCallBeak;
import com.hdl.photovoltaic.listener.CloudCallBeak;
import com.hdl.photovoltaic.listener.LinkCallBack;
import com.hdl.photovoltaic.ui.bean.CloudInverterChildDeviceBean;
import com.hdl.photovoltaic.ui.bean.CloudInverterDeviceBean;
import com.hdl.photovoltaic.ui.bean.InverterDeviceBean;
import com.hdl.photovoltaic.ui.bean.OidBean;
import com.hdl.photovoltaic.uni.HDLUniMP;
import com.hdl.photovoltaic.uni.HDLUniMPSDKManager;
import com.hdl.sdk.link.common.exception.HDLLinkCode;
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.callback.HDLLinkCallBack;
import com.hdl.sdk.link.gateway.HDLLinkLocalGateway;
 
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
 
import io.reactivex.rxjava3.disposables.Disposable;
 
/**
 * 设备逻辑的界面
 */
public class HdlDeviceLogic {
    private static volatile HdlDeviceLogic sHdlDeviceLogic;
 
    /**
     * 获取当前住宅的逆变器列表
     */
    public List<GatewayBean> getGatewayList() {
        List<GatewayBean> newList = new ArrayList<>();
        List<GatewayBean> list = HDLLinkLocalGateway.getInstance().getGatewayList();
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                GatewayBean gatewayBean = list.get(i);
                if (!TextUtils.isEmpty(gatewayBean.getHomeId())
                        && gatewayBean.getHomeId().equals(UserConfigManage.getInstance().getHomeId())) {
                    newList.add(gatewayBean);
                }
 
            }
        }
        return newList;
 
    }
 
    /**
     * 获取当前对象
     *
     * @return HdlDeviceLogic
     */
    public static synchronized HdlDeviceLogic getInstance() {
        if (sHdlDeviceLogic == null) {
            synchronized (HdlDeviceLogic.class) {
                if (sHdlDeviceLogic == null) {
                    sHdlDeviceLogic = new HdlDeviceLogic();
                }
            }
 
        }
        return sHdlDeviceLogic;
    }
 
    /**
     * 添加逆变器到云端上
     *
     * @param mac           -
     * @param spk           -
     * @param sid           -
     * @param oid           -
     * @param name          -
     * @param cloudCallBeak -
     */
    public void addInverterDeviceToCloud(String mac, String spk, String sid, String oid, String name, CloudCallBeak<Boolean> cloudCallBeak) {
 
        String requestUrl = HttpApi.POST_Device_Add;
        JsonObject json = new JsonObject();
        json.addProperty("homeId", UserConfigManage.getInstance().getHomeId());
        json.addProperty("mac", mac);
        json.addProperty("spk", spk);
        json.addProperty("sid", sid);
        json.addProperty("oid", oid);
        json.addProperty("name", name);
        // 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 homeId        住宅id
     * @param cloudCallBeak 回调
     */
    public void getCloudInverterDeviceList(String homeId, CloudCallBeak<List<CloudInverterDeviceBean>> cloudCallBeak) {
        String requestUrl = HttpApi.POST_Device_List;
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);
        // 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);
                    }
                }
                Type type = new TypeToken<List<CloudInverterDeviceBean>>() {
                }.getType();
                List<CloudInverterDeviceBean> list = new Gson().fromJson(jsonStr, type);
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(list);
                }
            }
 
            @Override
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(e);
                }
            }
        });
    }
 
 
    /**
     * 删除逆变器
     *
     * @param deviceId      设备id
     * @param cloudCallBeak 回调
     */
    public void delInverterDevice(String deviceId, CloudCallBeak<Boolean> cloudCallBeak) {
        String requestUrl = HttpApi.POST_Device_Remove;
        JsonObject json = new JsonObject();
        json.addProperty("homeId", UserConfigManage.getInstance().getHomeId());
        json.addProperty("deviceId", deviceId);
        // 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 parentOid     上级设备的Oid
     * @param cloudCallBeak 回调
     */
    public void getInverterDeviceChildDeviceList(String parentOid, CloudCallBeak<List<CloudInverterChildDeviceBean>> cloudCallBeak) {
        String requestUrl = HttpApi.POST_Device_ChildDevices_List;
        JsonObject json = new JsonObject();
        json.addProperty("homeId", UserConfigManage.getInstance().getHomeId());
        json.addProperty("parentOid", parentOid);
        // 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);
                    }
                    return;
                }
                Gson gson = new Gson();
                Type typeOfT = new TypeToken<List<CloudInverterChildDeviceBean>>() {
                }.getType();
                List<CloudInverterChildDeviceBean> list = gson.fromJson(jsonStr, typeOfT);
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(list);
                }
            }
 
            @Override
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(e);
                }
            }
        });
    }
 
    /**
     * 全量更新OID
     *
     * @param oidList       oid列表
     * @param cloudCallBeak 回调update
     */
    public void fullUpdateOid(List<OidBean> oidList, CloudCallBeak<Boolean> cloudCallBeak) {
        String requestUrl = HttpApi.POST_Device_ChildDevices_List;
        JsonObject json = new JsonObject();
        json.addProperty("operationSource", "PROGRAM_ENERGY");//
        json.addProperty("homeId", UserConfigManage.getInstance().getHomeId());
        JsonArray jsonArray = new JsonArray();
        for (int i = 0; i < oidList.size(); i++) {
            OidBean oidBean = oidList.get(i);
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("protocolType", oidBean.getProtocolType());
            jsonObject.addProperty("deviceType", oidBean.getDeviceType());
            jsonObject.addProperty("mac", oidBean.getMac());
            jsonObject.addProperty("oid", oidBean.getOid());
            jsonObject.addProperty("device_name", oidBean.getDevice_name());
            jsonObject.addProperty("device_model", oidBean.getDevice_model());
            jsonObject.addProperty("addresses", oidBean.getAddresses());
            jsonObject.addProperty("parentOid", oidBean.getParentOid());
            jsonArray.add(jsonObject);
 
        }
        json.add("devices", jsonArray);
        // 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 mac          设备mac
     * @param linkCallBack 回调update
     */
    public void setGatewayRemoteParam(String mac, LinkCallBack<Boolean> linkCallBack) {
        String requestUrl = TopicApi.SET_GATEWAY_REMOTE_EDIT;
        JsonObject json = new JsonObject();
        json.addProperty("homeId", UserConfigManage.getInstance().getHomeId());
        json.addProperty("server_addr", AppConfigManage.getUserRegionUrl());
        json.addProperty("local_secret", UserConfigManage.getInstance().getLocalSecret());
        //解密负载数据(因为写密钥给网关一定明文,因为那时网关还没有密钥)
        TcpClient.getInstance().sendDataToLinkGateway(mac, false, requestUrl, json, "", new HDLLinkCallBack() {
            @Override
            public void onSuccess(String msg) {
                GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getLocalGateway(mac);
                if (gatewayBean != null) {
                    if (!TextUtils.isEmpty(UserConfigManage.getInstance().getLocalSecret())) {
                        gatewayBean.setLocalEncrypt(true);
                    }
                }
                if (linkCallBack != null) {
                    linkCallBack.onSuccess(true);
                }
 
            }
 
            @Override
            public void onError(HDLLinkException e) {
                if (linkCallBack != null) {
                    linkCallBack.onError(e);
                }
            }
        });
    }
 
    /**
     * 编辑网关参数
     *
     * @param mac          设备mac
     * @param linkCallBack 回调update
     */
    public void editGatewayParam(String mac, LinkCallBack<Boolean> linkCallBack) {
        String requestUrl = TopicApi.SET_GATEWAY_EDIT;
        JsonObject json = new JsonObject();
        json.addProperty("master", "true");
        TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, json, "", new HDLLinkCallBack() {
            @Override
            public void onSuccess(String msg) {
                if (linkCallBack != null) {
                    linkCallBack.onSuccess(true);
                }
            }
 
            @Override
            public void onError(HDLLinkException e) {
                if (linkCallBack != null) {
                    linkCallBack.onError(e);
                }
            }
        });
 
    }
 
 
    /**
     * 获取网关详情信息
     *
     * @param mac          网关mac
     * @param linkCallBack 回调
     */
    public void getGatewayInfo(String mac, LinkCallBack<GatewayBean> linkCallBack) {
        String requestUrl = TopicApi.GET_GATEWAY_INFO;
        TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, null, "", new HDLLinkCallBack() {
            @Override
            public void onSuccess(String json) {
                if (TextUtils.isEmpty(json)) {
                    if (linkCallBack != null) {
                        linkCallBack.onSuccess(null);
                    }
                    return;
                }
                Gson gson = new Gson();
                GatewayBean gatewayBean = gson.fromJson(json, GatewayBean.class);
                if (linkCallBack != null) {
                    linkCallBack.onSuccess(gatewayBean);
                }
            }
 
            @Override
            public void onError(HDLLinkException e) {
                if (linkCallBack != null) {
                    linkCallBack.onError(e);
                }
            }
        });
 
    }
 
    /**
     * 获取网关oid列表
     *
     * @param mac          网关mac
     * @param linkCallBack 回调
     */
    public void getGatewayOidList(String mac, LinkCallBack<List<OidBean>> linkCallBack) {
        String requestUrl = TopicApi.GET_GATEWAY_OID_LIST;
        TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, null, "", new HDLLinkCallBack() {
            @Override
            public void onSuccess(String json) {
                if (TextUtils.isEmpty(json)) {
                    if (linkCallBack != null) {
                        linkCallBack.onSuccess(null);
                    }
                    return;
                }
                Gson gson = new Gson();
                Type typeOfT = new TypeToken<List<OidBean>>() {
                }.getType();
                List<OidBean> list = gson.fromJson(json, typeOfT);
                if (linkCallBack != null) {
                    linkCallBack.onSuccess(list);
                }
            }
 
            @Override
            public void onError(HDLLinkException e) {
                if (linkCallBack != null) {
                    linkCallBack.onError(e);
                }
            }
        });
 
    }
 
    /**
     * 获取本地设备列表
     * 注意:有外网以云端设备为准,本地存在,云端没有则删除;内网以本地为主,搜索多少个设备就显示多少个;
     *
     * @param cloudCallBeak -
     */
    public void getLocalGatewayList(CloudCallBeak<Boolean> cloudCallBeak) {
        HdlDeviceLogic.getInstance().searchGateway(new GatewayCallBack() {
            @Override
            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 || list.size() == 0) {
//                            //云端没有绑定逆变器,以本地为主;
//                            HDLLinkLocalGateway.getInstance().getGatewayList().clear();
                            if (cloudCallBeak != null) {
                                cloudCallBeak.onSuccess(true);
                            }
                            return;
                        }
                        //收集删除逆变器sid
                        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));
                        }
 
                        if (cloudCallBeak != null) {
                            cloudCallBeak.onSuccess(true);
                        }
                    }
 
                    @Override
                    public void onFailure(HDLException e) {
                        if (cloudCallBeak != null) {
                            cloudCallBeak.onFailure(e);
                        }
                    }
                });
            }
 
            @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 || list.size() == 0) {
                            if (cloudCallBeak != null) {
                                cloudCallBeak.onSuccess(true);
                            }
                            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);
                        }
                        if (cloudCallBeak != null) {
                            cloudCallBeak.onSuccess(true);
                        }
 
                    }
 
                    @Override
                    public void onFailure(HDLException e) {
                        if (cloudCallBeak != null) {
                            cloudCallBeak.onFailure(e);
                        }
                    }
                });
            }
        });
    }
 
    /**
     * 删除本地逆变器
     *
     * @param sid 设备sid
     */
    private void removeInverter(String sid) {
        //获取本地的毫米波
        GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getLocalGateway(sid);
        if (gatewayBean != null) {
            HDLLinkLocalGateway.getInstance().getGatewayList().remove(gatewayBean);
        }
    }
 
    /**
     * 搜索网关列表
     *
     * @param gatewayCallBack -
     */
    public void searchGateway(GatewayCallBack gatewayCallBack) {
        List<String> spks = this.getGatewaySpk();
        //网关搜索
        HDLLinkLocalGateway.getInstance().refreshGatewayByHomeIdAndSpk(UserConfigManage.getInstance().getHomeId(), spks, true, gatewayCallBack);
    }
 
    /**
     * 获取网关spk列表
     *
     * @return -
     */
    private List<String> getGatewaySpk() {
        List<String> spks = new ArrayList<>();
        spks.add("energy.hdl_inverter");
        return spks;
    }
}