mac
2024-03-13 4a2d7f9ac055d10ba7baf58df690e0ad62826f52
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
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.linkpm.sdk.core.exception.HDLException;
import com.hdl.linkpm.sdk.home.type.GatewayType;
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.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.DeviceRemoteInfo;
import com.hdl.photovoltaic.ui.bean.DeviceTimeBean;
import com.hdl.photovoltaic.ui.bean.OidBean;
import com.hdl.sdk.link.HDLLinkLocalSdk;
import com.hdl.sdk.link.common.exception.HDLLinkException;
import com.hdl.sdk.link.core.bean.gateway.GatewayBean;
import com.hdl.sdk.link.core.bean.response.BaseLocalResponse;
import com.hdl.sdk.link.core.callback.GatewayCallBack;
import com.hdl.sdk.link.core.callback.HDLLinkCallBack;
import com.hdl.sdk.link.core.config.HDLLinkConfig;
import com.hdl.sdk.link.gateway.HDLLinkLocalGateway;
import com.hdl.sdk.link.gateway.type.GatewayMasterType;
 
 
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
 
import io.dcloud.common.adapter.util.DeviceInfo;
 
 
/**
 * 设备逻辑
 */
public class HdlDeviceLogic {
    private static volatile HdlDeviceLogic sHdlDeviceLogic;
 
    /**
     * 获取当前对象
     *
     * @return HdlDeviceLogic
     */
    public static synchronized HdlDeviceLogic getInstance() {
        if (sHdlDeviceLogic == null) {
            synchronized (HdlDeviceLogic.class) {
                if (sHdlDeviceLogic == null) {
                    sHdlDeviceLogic = new HdlDeviceLogic();
                }
            }
 
        }
        return sHdlDeviceLogic;
    }
 
    /**
     * 获取当前住宅的逆变器列表(包括从的逆变器)
     */
    public List<GatewayBean> getCurrentHomeGatewayList(String homeId) {
        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.getDevice_mac())) {
                    continue;
                }
                //用homeId筛选当前住宅的逆变器列表
                if (gatewayBean.getHomeId().equals(homeId)) {
                    newList.add(gatewayBean);
                }
            }
        }
        return newList;
    }
 
    /**
     * 获取当前住宅的【主】逆变器
     */
    public GatewayBean getCurrentHomeMainGateway(String homeId) {
        return queryCurrentHomeMainGateway(this.getCurrentHomeGatewayList(homeId), homeId);
    }
 
    /**
     * 查询当前住宅的【主】逆变器
     */
    public GatewayBean queryCurrentHomeMainGateway(List<GatewayBean> list, String homeId) {
        if (list == null || list.size() == 0) {
            return null;
        }
        GatewayBean findGatewayBean = null;
        for (int i = 0; i < list.size(); i++) {
            GatewayBean gatewayBean = list.get(i);
            if (gatewayBean.getMaster().equals(GatewayMasterType.MasterTrue) && gatewayBean.getHomeId().equals(homeId)) {
                //找到返回
                findGatewayBean = gatewayBean;
                break;
            }
        }
        return findGatewayBean;
    }
 
 
    /**
     * 逆变器上传数据到云端(包括:sid,oid)
     *
     * @param homeId 住宅id
     * @param mac    设备mac
     */
    public void uploadDataToCloud(String homeId, String mac, CloudCallBeak<Boolean> callBeak) {
        getInverterOidList(mac, new LinkCallBack<List<OidBean>>() {
            @Override
            public void onSuccess(List<OidBean> oidBeanList) {
                if (oidBeanList == null) {
                    return;
                }
                fullUpdateOid(homeId, oidBeanList, new CloudCallBeak<Boolean>() {
                    @Override
                    public void onSuccess(Boolean obj) {
                        HdlLogLogic.print("上传oid列表到云端成功--->住宅id:" + homeId + "--->mac:" + mac, true);
                    }
 
                    @Override
                    public void onFailure(HDLException e) {
                        HdlLogLogic.print("上传oid列表到云端失败--->住宅id:" + homeId + "--->mac:" + mac + "-->" + e.getMsg() + "(" + e.getCode() + ")", true);
                    }
                });
            }
 
            @Override
            public void onError(HDLLinkException e) {
                HdlLogLogic.print("获取逆变器oid列表失败--->住宅id:" + homeId + "--->mac:" + mac + "-->" + e.getMsg() + "(" + e.getCode() + ")", true);
            }
        });
 
    }
 
    /**
     * 添加逆变器到云端上
     *
     * @param mac           -
     * @param spk           -
     * @param sid           -
     * @param oid           -
     * @param name          -
     * @param cloudCallBeak -
     */
    public void addInverterDeviceToCloud(String homeId, 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", homeId);
        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);
                    //临时的逻辑,上传oid列表到云端
                    uploadDataToCloud(UserConfigManage.getInstance().getHomeId(), mac, null);
                }
            }
 
            @Override
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(e);
                }
            }
        });
    }
 
    /**
     * 添加多个逆变器到云端上
     *
     * @param mac           -
     * @param cloudCallBeak -
     */
    public void addAllInverterDeviceToCloud(String homeId, String mac, List<GatewayBean> list, CloudCallBeak<Boolean> cloudCallBeak) {
 
        String requestUrl = HttpApi.POST_Device_Add_All;
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);
        JsonArray ary = new JsonArray();
        for (int i = 0; i < list.size(); i++) {
            GatewayBean gatewayBean=list.get(i);
            JsonObject inverterInfo = new JsonObject();
            inverterInfo.addProperty("mac",gatewayBean.getDevice_mac());
            inverterInfo.addProperty("spk",gatewayBean.getGatewayType());
            inverterInfo.addProperty("sid",gatewayBean.getSid());
            inverterInfo.addProperty("oid",gatewayBean.getOid());
            inverterInfo.addProperty("name",gatewayBean.getDevice_name());
            ary.add(inverterInfo);
        }
        json.add("devices", ary);
        // 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);
                    //临时的逻辑,上传oid列表到云端
                    uploadDataToCloud(UserConfigManage.getInstance().getHomeId(), mac, null);
                }
            }
 
            @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 homeId, String deviceId, CloudCallBeak<Boolean> cloudCallBeak) {
        String requestUrl = HttpApi.POST_Device_Remove;
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);
        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 homeId, String parentOid, CloudCallBeak<List<CloudInverterChildDeviceBean>> cloudCallBeak) {
        String requestUrl = HttpApi.POST_Device_ChildDevices_List;
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);
        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 homeId        住宅id
     * @param oidList       oid列表
     * @param cloudCallBeak 回调update
     */
    public void fullUpdateOid(String homeId, List<OidBean> oidList, CloudCallBeak<Boolean> cloudCallBeak) {
        if (oidList == null || oidList.size() == 0) {
            HdlLogLogic.print("oid列表为空,无法全量更新oid到云端--->", true);
            return;
        }
        String requestUrl = HttpApi.POST_Device_Oid;
        JsonObject json = new JsonObject();
        json.addProperty("operationSource", "PROGRAM_ENERGY");//
        json.addProperty("homeId", homeId);
        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.getDevice_mac());
            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);
                }
            }
        });
 
    }
 
 
    /**
     * (oid)增量添加
     *
     * @param homeId        住宅id
     * @param oidList       oid列表
     * @param cloudCallBeak 回调update
     */
    public void updateOidAdd(String homeId, List<OidBean> oidList, CloudCallBeak<Boolean> cloudCallBeak) {
        if (oidList == null || oidList.size() == 0) {
            HdlLogLogic.print("oid列表为空,无法增量添加oid到云端--->", true);
            return;
        }
        String requestUrl = HttpApi.POST_Device_IncrAdd;
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);
        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.getDevice_mac());
            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);
                }
                HdlLogLogic.print("增量添加oid到云端成功--->");
            }
 
            @Override
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(e);
                }
                HdlLogLogic.print("增量添加oid到云端失败--->" + e.getMsg() + "(" + e.getCode() + ")", true);
            }
        });
 
    }
 
 
    /**
     * 设置网关远程参数
     *
     * @param mac          设备mac
     * @param linkCallBack 回调update
     */
    public void setGatewayRemoteParam(String homeId, String mac, LinkCallBack<Boolean> linkCallBack) {
        String requestUrl = TopicApi.SET_GATEWAY_REMOTE_EDIT;
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);
        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 回调updatetime
     */
    public void getGatewayTime(String mac, LinkCallBack<DeviceTimeBean> linkCallBack) {
        String requestUrl = TopicApi.GET_GATEWAY_TIME;
        TcpClient.getInstance().sendDataToLinkGateway(mac, requestUrl, null, "", new HDLLinkCallBack() {
            @Override
            public void onSuccess(String json) {
 
                if (TextUtils.isEmpty(json)) {
                    if (linkCallBack != null) {
                        linkCallBack.onSuccess(new DeviceTimeBean());
                    }
                    return;
                }
 
                Gson gson = new Gson();
                Type typeOfT = new TypeToken<BaseLocalResponse<DeviceTimeBean>>() {
                }.getType();
                BaseLocalResponse<DeviceTimeBean> baseLocalResponse = gson.fromJson(json, typeOfT);
                if (linkCallBack != null) {
                    linkCallBack.onSuccess(baseLocalResponse.getObjects());
                }
            }
 
            @Override
            public void onError(HDLLinkException e) {
                if (linkCallBack != null) {
                    linkCallBack.onError(e);
                }
            }
        });
 
    }
 
    /**
     * 网关时间修改
     *
     * @param mac          设备mac
     * @param date         日期
     * @param time         时间
     * @param timezone     时区
     * @param linkCallBack 回调update
     */
    public void editGatewayTime(String mac, String date, String time, String timezone, LinkCallBack<Boolean> linkCallBack) {
        String requestUrl = TopicApi.SET_GATEWAY_TIME_EDIT;
        JsonObject json = new JsonObject();
        if (!TextUtils.isEmpty(date)) {
            json.addProperty("date", date);// "2020-08-15"
        }
        if (!TextUtils.isEmpty(time)) {
            json.addProperty("time", time);//"17:25:20"
        }
        if (!TextUtils.isEmpty(timezone)) {
            json.addProperty("timezone", timezone);//时区(语雀https://hdl-gz.yuque.com/wnkbmh/cdrglh/yckkyp#qWOPS)
        }
 
        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 回调update
     */
    public void editGatewayParam(String mac, LinkCallBack<Boolean> linkCallBack) {
        String requestUrl = TopicApi.SET_GATEWAY_EDIT;
        JsonObject json = new JsonObject();
        json.addProperty("master", GatewayMasterType.MasterTrue);
        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 getInverterOidList(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<BaseLocalResponse<List<OidBean>>>() {
                }.getType();
                BaseLocalResponse<List<OidBean>> baseLocalResponse = gson.fromJson(json, typeOfT);
 
                if (linkCallBack == null) {
                    return;
                }
                if (baseLocalResponse == null || baseLocalResponse.getObjects() == null) {
                    linkCallBack.onSuccess(new ArrayList<>());
                } else {
                    linkCallBack.onSuccess(baseLocalResponse.getObjects());
                }
            }
 
            @Override
            public void onError(HDLLinkException e) {
                if (linkCallBack != null) {
                    linkCallBack.onError(e);
                }
            }
        });
 
    }
 
    /**
     * 获取逆变器列表(整合云端和本地列表)
     * 注意:有外网,以云端设备为准,本地存在,云端没有则删除;内网,以本地为主,搜索多少个设备就显示多少个;
     *
     * @param homeId        住宅id
     * @param cloudCallBeak 返回逆变器列表
     */
    public void getCurrentHomeLocalAndCloudGatewayList(String homeId, CloudCallBeak<List<GatewayBean>> cloudCallBeak) {
        HdlDeviceLogic.getInstance().searchCurrentHomeGateway(new GatewayCallBack() {
            @Override
            public void onSuccess(List<GatewayBean> gatewayBeanList) {
                //局域网有2种情况(1:有局域网,有外网;2:有局域网,没有外网);
                HdlDeviceLogic.getInstance().getCloudInverterDeviceList(homeId, new CloudCallBeak<List<CloudInverterDeviceBean>>() {
                    @Override
                    public void onSuccess(List<CloudInverterDeviceBean> list) {
                        ///情况1:有局域网,有外网;
                        if (list == null || list.size() == 0) {
                            //云端没有绑定逆变器,默认返回本地逆变器列表;
                            if (cloudCallBeak != null) {
                                cloudCallBeak.onSuccess(getCurrentHomeGatewayList(homeId));
                            }
                            return;
                        }
                        //记录条数
                        AtomicInteger atomicInteger = new AtomicInteger(0);
                        for (int i = 0; i < list.size(); i++) {
                            CloudInverterDeviceBean cloudInverterDeviceBean = list.get(i);
                            //获取远程设备通讯信息(拿到网关ID,mqtt通讯秘钥)
                            getDeviceRemoteInfo(homeId, cloudInverterDeviceBean.getSpk(), cloudInverterDeviceBean.getOsn(), new CloudCallBeak<DeviceRemoteInfo>() {
                                @Override
                                public void onSuccess(DeviceRemoteInfo deviceRemoteInfo) {
                                    atomicInteger.set(atomicInteger.get() + 1);
                                    //更新逆变器本地缓存信息
                                    refreshGatewayCacheData(true, cloudInverterDeviceBean, deviceRemoteInfo, homeId);
                                    //最后一条才做删除逆变器逻辑处理
                                    if (atomicInteger.get() == list.size()) {
                                        //收集删除逆变器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++) {
                                            //删除本地的逆变器
                                            removeLocalInverter(removeSidList.get(i));
                                        }
                                        if (cloudCallBeak != null) {
                                            cloudCallBeak.onSuccess(getCurrentHomeGatewayList(homeId));
                                        }
                                    }
                                }
 
                                @Override
                                public void onFailure(HDLException e) {
                                    atomicInteger.set(atomicInteger.get() + 1);
                                    //当最后一条请求失败了,才在这里做删除逆变器逻辑处理
                                    if (atomicInteger.get() == list.size()) {
                                        //收集删除逆变器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++) {
                                            //删除本地的逆变器
                                            removeLocalInverter(removeSidList.get(i));
                                        }
                                        if (cloudCallBeak != null) {
                                            cloudCallBeak.onSuccess(getCurrentHomeGatewayList(homeId));
                                        }
                                    }
                                    HdlLogLogic.print("获取设备远程通讯信息失败->msg->" + e.getMsg() + "(" + e.getCode() + ")");
                                }
                            });
                        }
 
 
                    }
 
                    @Override
                    public void onFailure(HDLException e) {
                        ///情况2:有局域网,没有外网;
                        //本地有逆变器列表,获取云端绑定逆变器失败,默认返回本地逆变器列表;
                        if (cloudCallBeak != null) {
                            cloudCallBeak.onSuccess(getCurrentHomeGatewayList(homeId));
                        }
                    }
                });
            }
 
            @Override
            public void onError(HDLLinkException e) {
                //外网只有1种情况(本地搜索逆变器列表失败了)
                HdlDeviceLogic.getInstance().getCloudInverterDeviceList(homeId, new CloudCallBeak<List<CloudInverterDeviceBean>>() {
                    @Override
                    public void onSuccess(List<CloudInverterDeviceBean> list) {
                        //外网进来先【默认】清空本地逆变器列表
                        HDLLinkLocalGateway.getInstance().getGatewayList().clear();
                        if (list == null || list.size() == 0) {
                            if (cloudCallBeak != null) {
                                //云端也没有逆变器列表,返回空列表回去
                                cloudCallBeak.onSuccess(new ArrayList<>());
                            }
                            HdlLogLogic.print("在只有【外网】情况下获取云端逆变器列表返回空列表", true);
                            return;
                        }
                        //记录条数
                        AtomicInteger atomicInteger = new AtomicInteger(0);
 
                        for (int i = 0; i < list.size(); i++) {
                            CloudInverterDeviceBean cloudInverterDeviceBean = list.get(i);
 
                            //获取远程设备通讯信息(拿到网关ID,mqtt通讯秘钥)
                            getDeviceRemoteInfo(homeId, cloudInverterDeviceBean.getSpk(), cloudInverterDeviceBean.getOsn(), new CloudCallBeak<DeviceRemoteInfo>() {
                                @Override
                                public void onSuccess(DeviceRemoteInfo deviceRemoteInfo) {
                                    atomicInteger.set(atomicInteger.get() + 1);
                                    //更新逆变器本地缓存信息
                                    refreshGatewayCacheData(false, cloudInverterDeviceBean, deviceRemoteInfo, homeId);
                                    //到最后一条,才有回调
                                    if (atomicInteger.get() == list.size()) {
                                        if (cloudCallBeak != null) {
                                            cloudCallBeak.onSuccess(getCurrentHomeGatewayList(homeId));
                                        }
                                    }
                                }
 
                                @Override
                                public void onFailure(HDLException e) {
                                    atomicInteger.set(atomicInteger.get() + 1);
                                    //当最后一条请求失败了,才有回调
                                    if (atomicInteger.get() == list.size()) {
                                        if (cloudCallBeak != null) {
                                            cloudCallBeak.onSuccess(getCurrentHomeGatewayList(homeId));
                                        }
                                    }
                                    HdlLogLogic.print("获取设备远程通讯信息失败--->msg--->" + e.getMsg() + "(" + e.getCode() + ")");
                                }
                            });
                        }
 
 
                    }
 
                    @Override
                    public void onFailure(HDLException e) {
                        if (cloudCallBeak != null) {
                            cloudCallBeak.onFailure(e);
                        }
                    }
                });
            }
        });
    }
 
    /**
     * 获取设备远程通讯信息
     *
     * @param homeId        住宅Id
     * @param spk           spk
     * @param mac           设备mac
     * @param cloudCallBeak 回调
     */
    public void getDeviceRemoteInfo(String homeId, String spk, String mac, CloudCallBeak<DeviceRemoteInfo> cloudCallBeak) {
        String requestUrl = HttpApi.POST_Device_RemoteInfo;
        JsonObject json = new JsonObject();
        json.addProperty("homeId", homeId);
        json.addProperty("spk", spk);
        json.addProperty("mac", mac);
        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<DeviceRemoteInfo>() {
                }.getType();
                DeviceRemoteInfo deviceRemoteInfo = new Gson().fromJson(jsonStr, type);
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(deviceRemoteInfo);
                }
            }
 
            @Override
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(e);
                }
            }
        });
    }
 
    /**
     * 删除本地缓存逆变器
     *
     * @param sid 设备sid
     */
    private void removeLocalInverter(String sid) {
        GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getLocalGateway(sid);
        if (gatewayBean != null) {
            HDLLinkLocalGateway.getInstance().getGatewayList().remove(gatewayBean);
        }
    }
 
    /**
     * 清空逆变器住宅id
     *
     * @param mac mac
     */
    public void clearInverterHomeId(String mac, LinkCallBack<Boolean> callBeak) {
        String requestUrl = TopicApi.SET_GATEWAY_REMOTE_EDIT;
        JsonObject json = new JsonObject();
        json.addProperty("homeId", "");
        json.addProperty("server_addr", "");
        json.addProperty("local_secret", "");
        TcpClient.getInstance().sendDataToLinkGateway(mac, true, 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 (callBeak != null) {
                    callBeak.onSuccess(true);
                }
 
            }
 
            @Override
            public void onError(HDLLinkException e) {
                if (callBeak != null) {
                    callBeak.onError(e);
                }
            }
        });
    }
 
    /**
     * 搜索逆变器列表
     * 注意:只搜索当前住宅逆变器和没有被绑定的逆变器
     *
     * @param gatewayCallBack -
     */
    public void searchCurrentHomeGateway(GatewayCallBack gatewayCallBack) {
        List<String> spks = this.getGatewaySpk();
        //网关搜索
        HDLLinkLocalGateway.getInstance().refreshGatewayByHomeIdAndSpk(UserConfigManage.getInstance().getHomeId(), spks, true, gatewayCallBack);
    }
 
    /**
     * 搜索局域网所有逆变器列表
     * 注意:包括已经被绑定到住宅的逆变器
     *
     * @param gatewayCallBack -
     */
    public void searchAllNetworkGateway(GatewayCallBack gatewayCallBack) {
        //网关搜索
        HDLLinkLocalSdk.getInstance().refreshGateway(gatewayCallBack, this.getGatewaySpk());
    }
 
    /**
     * 检测本地连接状态
     *
     * @param deviceMac 设备mac,
     * @param callBeak  返回 true=本地连接;false=远程;
     */
    public void isLocalConnect(String deviceMac, CloudCallBeak<Boolean> callBeak) {
        searchCurrentHomeGateway(new GatewayCallBack() {
            @Override
            public void onSuccess(List<GatewayBean> gatewayBeanList) {
                if (gatewayBeanList == null || gatewayBeanList.size() == 0) {
                    if (callBeak != null) {
                        callBeak.onSuccess(false);
                    }
                    return;
                }
                boolean is_find = false;
                for (int i = 0; i < gatewayBeanList.size(); i++) {
                    GatewayBean gatewayBean = gatewayBeanList.get(i);
                    if (gatewayBean.getDevice_mac().equals(deviceMac)) {
                        //升级的时候搜索网关列表,把之前mqtt秘钥覆盖掉,要重新获取才行;
                        if (!TextUtils.isEmpty(gatewayBean.getHomeId())) {
                            getDeviceRemoteInfo(gatewayBean.getHomeId(), gatewayBean.getSpk(), gatewayBean.getDevice_mac(), new CloudCallBeak<DeviceRemoteInfo>() {
                                @Override
                                public void onSuccess(DeviceRemoteInfo deviceRemoteInfo) {
                                    if (deviceRemoteInfo != null) {
                                        gatewayBean.setAesKey(deviceRemoteInfo.getSecret());//设置mqtt通讯秘钥
                                        gatewayBean.setGatewayId(deviceRemoteInfo.getGatewayId());//设置gatewayId
                                        //todo 设置到【库】里面,底层mqtt订阅,加解密会用到该参数;
                                        HDLLinkConfig.getInstance().setAesKey(deviceRemoteInfo.getSecret());//设置mqtt通讯秘钥库
                                        HDLLinkConfig.getInstance().setGatewayId(deviceRemoteInfo.getGatewayId());//设置gatewayId
                                    }
                                }
 
                                @Override
                                public void onFailure(HDLException e) {
 
                                }
                            });
                        }
                        is_find = true;
                        break;
                    }
 
                }
                if (callBeak != null) {
                    callBeak.onSuccess(is_find);
                }
            }
 
            @Override
            public void onError(HDLLinkException e) {
                if (callBeak != null) {
                    callBeak.onSuccess(false);
                }
            }
        });
    }
 
    /**
     * 检测逆变器有没有连接上云
     *
     * @param deviceMac 设备mac
     */
    public void checkInverterConnectedCloud(String homeId, String deviceMac, CloudCallBeak<CloudInverterDeviceBean> cloudCallBeak) {
        this.getCloudInverterDeviceList(homeId, new CloudCallBeak<List<CloudInverterDeviceBean>>() {
            @Override
            public void onSuccess(List<CloudInverterDeviceBean> list) {
                CloudInverterDeviceBean cloudInverterDeviceBean = null;
                if (list != null && list.size() > 0) {
                    for (int i = 0; i < list.size(); i++) {
                        if (list.get(i).getOsn().equals(deviceMac)) {
                            cloudInverterDeviceBean = list.get(i);
                            break;
                        }
 
                    }
                }
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(cloudInverterDeviceBean);
                }
            }
 
            @Override
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(e);
                }
            }
        });
    }
 
 
    /**
     * 刷新逆变器缓存信息(包括设置mqtt秘钥)
     * 注:
     * 1:本地存在,更新信息即可;
     * 2:本地不存在,则虚拟一个逆变器对象添加在本地;
     *
     * @param deviceRemoteInfo        设备远程通讯信息
     * @param cloudInverterDeviceBean 逆变器
     */
    private void refreshGatewayCacheData(boolean isLocal, CloudInverterDeviceBean cloudInverterDeviceBean, DeviceRemoteInfo deviceRemoteInfo, String homeId) {
        //本地查找逆变器
        GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getLocalGateway(cloudInverterDeviceBean.getOsn());
        if (gatewayBean == null) {
            //不存在,虚拟一个逆变器对象;
            gatewayBean = new GatewayBean();
            HDLLinkLocalGateway.getInstance().getGatewayList().add(gatewayBean);//添加逆变器设备
        }
        //更新属性
        if (deviceRemoteInfo != null) {
            gatewayBean.setAesKey(deviceRemoteInfo.getSecret());//设置mqtt通讯秘钥
            gatewayBean.setGatewayId(deviceRemoteInfo.getGatewayId());//设置gatewayId
            //todo 设置到【库】里面,底层mqtt订阅,加解密会用到该参数;
            HDLLinkConfig.getInstance().setAesKey(deviceRemoteInfo.getSecret());//设置mqtt通讯秘钥库
            HDLLinkConfig.getInstance().setGatewayId(deviceRemoteInfo.getGatewayId());//设置gatewayId
 
        }
        gatewayBean.setHomeId(homeId);//住宅id
        gatewayBean.setDeviceStatus(cloudInverterDeviceBean.getDeviceStatus());//设置网关状态
        gatewayBean.setDeviceId(cloudInverterDeviceBean.getDeviceId());//设置link设备id
        gatewayBean.setDevice_mac(cloudInverterDeviceBean.getOsn());//设置mac
        gatewayBean.setOid(cloudInverterDeviceBean.getOid());//设备oid
        gatewayBean.setSid(cloudInverterDeviceBean.getSid());//设备sid
        gatewayBean.setDevice_name(cloudInverterDeviceBean.getName());//设备名称
        gatewayBean.setGatewayType(cloudInverterDeviceBean.getSpk());//设置spk
        gatewayBean.setLocalEncrypt(true);
        gatewayBean.setMaster(GatewayMasterType.MasterTrue);//默认都是主逆变器(以后支持从逆变器要更改)
        gatewayBean.setSystemStatusDesc(cloudInverterDeviceBean.getSystemStatusDesc());//设备状态
        gatewayBean.setHwVersion(cloudInverterDeviceBean.getHwVersion());//软件版本号
        gatewayBean.setCategorySecondName(cloudInverterDeviceBean.getCategorySecondName());//设备类型(产品二级分类名称
        gatewayBean.setAddresses(cloudInverterDeviceBean.getAddresses());//子网号/设备号,逆变器地址
        gatewayBean.setPowerPvNow(cloudInverterDeviceBean.getPowerPvNow());//发电功率
        gatewayBean.setTotalElectricityPvToday(cloudInverterDeviceBean.getTotalElectricityPvToday());//今日发电量
        gatewayBean.setDevice_model(cloudInverterDeviceBean.getOmodel());//设备型号
        gatewayBean.setSpk(cloudInverterDeviceBean.getSpk());//设备spk
 
 
    }
 
 
    /**
     * 获取网关支持spk列表
     *
     * @return -
     */
    public List<String> getGatewaySpk() {
        List<String> spks = new ArrayList<>();
        spks.add(DEVICE_SPK);
        return spks;
    }
 
    public boolean isGatewaySpk(String spk) {
        if (TextUtils.isEmpty(spk)) {
            return false;
        }
        for (int i = 0; i < this.getGatewaySpk().size(); i++) {
            if (this.getGatewaySpk().get(i).equals(spk)) {
                return true;
            }
        }
        return false;
    }
 
    public final String DEVICE_SPK = "energy.hdl_inverter";
}