mac
2023-12-29 27d994f4375f604ba7f49a5ba600882884d73126
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
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
package com.hdl.photovoltaic.other;
 
 
import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;
 
import com.google.gson.Gson;
import com.hdl.linkpm.sdk.core.exception.HDLException;
import com.hdl.linkpm.sdk.device.bean.DeviceOidInfoBean;
import com.hdl.linkpm.sdk.ota.bean.CloudDeviceFirmwaresBean;
import com.hdl.linkpm.sdk.ota.bean.CloudGatewayDriversBean;
import com.hdl.linkpm.sdk.ota.bean.DeviceFirmwareBean;
import com.hdl.linkpm.sdk.ota.bean.GatewayDriverBean;
import com.hdl.linkpm.sdk.utils.HDLMD5Utils;
import com.hdl.photovoltaic.HDLApp;
import com.hdl.photovoltaic.R;
import com.hdl.photovoltaic.bean.ModBusBean;
import com.hdl.photovoltaic.config.AppConfigManage;
import com.hdl.photovoltaic.config.UserConfigManage;
import com.hdl.photovoltaic.internet.HttpServer.MyNanoHttpServer;
import com.hdl.photovoltaic.listener.CloudCallBeak;
import com.hdl.photovoltaic.listener.LinkCallBack;
import com.hdl.photovoltaic.ui.bean.CloudInverterDeviceBean;
import com.hdl.photovoltaic.ui.bean.DeviceTimeBean;
import com.hdl.photovoltaic.ui.bean.OidBean;
import com.hdl.photovoltaic.uni.HDLUniMP;
import com.hdl.photovoltaic.uni.HDLUniMPSDKManager;
import com.hdl.photovoltaic.utils.AppManagerUtils;
import com.hdl.photovoltaic.utils.NetworkUtils;
import com.hdl.photovoltaic.utils.WifiUtils;
import com.hdl.sdk.link.common.exception.HDLLinkCode;
import com.hdl.sdk.link.common.exception.HDLLinkException;
import com.hdl.sdk.link.core.bean.eventbus.BaseEventBus;
import com.hdl.sdk.link.core.bean.gateway.GatewayBean;
import com.hdl.sdk.link.core.callback.GatewayCallBack;
import com.hdl.sdk.link.core.callback.ModbusCallBack;
import com.hdl.sdk.link.core.connect.HDLModBusConnect;
import com.hdl.sdk.link.gateway.HDLLinkLocalGateway;
 
import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;
 
 
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
 
import io.dcloud.feature.unimp.DCUniMPJSCallback;
import okhttp3.ResponseBody;
 
/**
 * uni小程序的逻辑
 */
public class HdlUniLogic implements HDLUniMPSDKManager.IOnOtherUniMPEventCallBack {
    private static volatile HdlUniLogic sHdlUniLogic;
 
    /**
     * 获取当前对象
     *
     * @return HdlAccountLogic
     */
    public static synchronized HdlUniLogic getInstance() {
        if (sHdlUniLogic == null) {
            synchronized (HdlUniLogic.class) {
                if (sHdlUniLogic == null) {
                    sHdlUniLogic = new HdlUniLogic();
                }
            }
 
        }
        return sHdlUniLogic;
    }
 
 
    //region ******【原生】和【小程序】通讯的3个方法名 onOtherUniMPEventReceive();openUniMP();sendUni();******
 
    /**
     * 原生接收到uni发来的数据
     *
     * @param appId    -uni小程序指定的appId
     * @param topic    -uni小程序发来主题(大类)
     * @param data     JSONObject com.alibaba.fastjson解析处理
     * @param callback callback.invoke(JSONObject) 用com.alibaba.fastjson
     */
    @Override
    public void onOtherUniMPEventReceive(String appId, String topic, Object data, DCUniMPJSCallback callback) {
        try {
            if (!HDLUniMP.UNI_APP_ID.equals(appId)) {
                return;
            }
            String type = getKeyValue("type", data);//小类
            if (HDLUniMP.UNI_EVENT_REPLY_HOME_MODEL.equals(topic)) {
                //住宅模块
                switch (type) {
                    //创建电站
                    case HDLUniMP.UNI_EVENT_REPLY_HOME_CREATION: {
                        //EventBus事件分发
                        BaseEventBus baseEventBus = new BaseEventBus();
                        baseEventBus.setTopic(HDLUniMP.UNI_EVENT_REPLY_HOME_MODEL);
                        baseEventBus.setType(HDLUniMP.UNI_EVENT_REPLY_HOME_CREATION);
                        baseEventBus.setData(getKeyValue("data", data));
                        EventBus.getDefault().post(baseEventBus);
                    }
                    break;
                    //读取详情
                    case HDLUniMP.UNI_EVENT_REPLY_HOME_DETAILS: {
 
                    }
                    break;
                    //关闭详情页
                    case HDLUniMP.UNI_EVENT_REPLY_HOME_CLOSE_HOME_DETAILS_PAGE: {
                        //EventBus事件分发
                        BaseEventBus baseEventBus = new BaseEventBus();
                        baseEventBus.setType(HDLUniMP.UNI_EVENT_REPLY_HOME_CLOSE_HOME_DETAILS_PAGE);
                        baseEventBus.setData(getKeyValue("data", data));
                        EventBus.getDefault().post(baseEventBus);
                    }
                    break;
                    //住宅【电站】编辑
                    case HDLUniMP.UNI_EVENT_REPLY_HOME_EDIT: {
                        //EventBus事件分发
                        BaseEventBus baseEventBus = new BaseEventBus();
                        baseEventBus.setTopic(HDLUniMP.UNI_EVENT_REPLY_HOME_MODEL);
                        baseEventBus.setType(HDLUniMP.UNI_EVENT_REPLY_HOME_EDIT);
                        baseEventBus.setData(getKeyValue("data", data));
                        EventBus.getDefault().post(baseEventBus);
                    }
                    break;
 
                }
                if (callback != null) {
                    uniCallbackData(null, callback);
                }
            } else if (HDLUniMP.UNI_EVENT_REPLY_DEVICE_MODEL.equals(topic)) {
                //设备模块
                switch (type) {
                    //添加逆变器到云端
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_ADD: {
                        this.uniAddInverterDeviceToCloud(data, callback);
                    }
                    break;
                    //删除云端逆变器
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_DEL: {
                        this.uniDelInverterDevice(data, callback);
                    }
                    break;
                    //局域网搜索逆变器列表
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_NET_LIST: {
                        this.uniSearchGateway(callback);
                    }
                    break;
                    //获取逆变器列表
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_LIST: {
                        this.uniGetCurrentHomeLocalAndCloudGatewayList(data, callback);
                    }
                    break;
                    //modBus协议专用
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_MODBUS_SEND: {
                        sendModBus(data, callback);
                    }
                    break;
                    //获取oid列表
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_OID: {
                        this.uniGetInverterOidList(data, callback);
                    }
                    break;
                    //逆变器时间读取
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_TIME: {
                        this.uniGetInverterTime(data, callback);
                    }
 
                    break;
                    //逆变器时间编辑
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_TIME_EDIT: {
                        this.uniEditInverterTime(data, callback);
                    }
                    break;
                    //逆变器上传数据到云端
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_UPLOADING_DATA: {
                        this.uniUploadDataToCloud(data, callback);
                    }
                    break;
                    //逆变器清空住宅id
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_CLEAR_DATA: {
                        this.uniClearInverterHomeId(data, callback);
                    }
                    break;
 
                }
 
            } else if (HDLUniMP.UNI_EVENT_REPLY_WIFI_MODEL.equals(topic)) {
                WifiUtils wifiUtils = WifiUtils.getInstance();
                //wifi模块
                switch (type) {
                    //获取wifi列表
                    case HDLUniMP.UNI_EVENT_REPLY_WIFI_LIST: {
                        if (callback != null) {
                            uniCallbackData(wifiUtils.getScanResult(), callback);
                        }
                    }
                    break;
                    //当前wifi详情
                    case HDLUniMP.UNI_EVENT_REPLY_WIFI_INFO: {
                        if (callback != null) {
                            uniCallbackData(wifiUtils.getCurrentConnectWifiInfo(), callback);
                        }
                    }
                    break;
                    //wifi连接
                    case HDLUniMP.UNI_EVENT_REPLY_WIFI_CONNECT: {
 
                    }
                    break;
 
                }
            } else if (HDLUniMP.UNI_EVENT_REPLY_OTA_MODEL.equals(topic)) {
                //OTA升级模块
                switch (type) {
                    //向云端获取oid列表
                    case HDLUniMP.UNI_EVENT_REPLY_OTA_CLOUD_OID_LIST: {
                        this.uniGetCloudOidList(data, callback);
                    }
                    break;
                    //当前设备固件列表
                    case HDLUniMP.UNI_EVENT_REPLY_OTA_FIRMWARES_LIST: {
                        this.uniGetCurrentDeviceFirmwares(data, callback);
                    }
                    break;
                    //设备新固件列表
                    case HDLUniMP.UNI_EVENT_REPLY_OTA_FIRMWARES_NEW_LIST: {
                        this.uniGetNewDeviceFirmwares(data, callback);
                    }
                    break;
                    //设备本地固件列表
                    case HDLUniMP.UNI_EVENT_REPLY_OTA_FIRMWARES_LOCAL: {
                        this.uniGetLocalFirmwares(data, callback);
                    }
                    break;
                    //设备固件下载
                    case HDLUniMP.UNI_EVENT_REPLY_OTA_FIRMWARES_DOWNLOAD: {
                        this.uniDeviceFirmwareDownload(data, callback);
                    }
                    break;
                    //设备固件升级
                    case HDLUniMP.UNI_EVENT_REPLY_OTA_FIRMWARES_UPGRADE: {
                        this.uniUpgradeDeviceFirmware(data, callback);
                    }
                    break;
                    //当前设备驱动列表
                    case HDLUniMP.UNI_EVENT_REPLY_OTA_DRIVER_LIST: {
                        this.uniGetCurrentGatewayDrivers(data, callback);
                    }
                    break;
                    //设备新驱动列表
                    case HDLUniMP.UNI_EVENT_REPLY_OTA_DRIVER_NEW: {
                        this.uniGetNewGatewayDrivers(data, callback);
                    }
                    break;
                    //设备本地驱动列表
                    case HDLUniMP.UNI_EVENT_REPLY_OTA_DRIVER_LOCAL: {
                        this.uniGetLocalDrivers(data, callback);
                    }
                    break;
                    //设备驱动下载
                    case HDLUniMP.UNI_EVENT_REPLY_OTA_DRIVER_DOWNLOAD: {
                        this.uniGatewayDriverDownload(data, callback);
                    }
                    break;
                    //设备驱动升级
                    case HDLUniMP.UNI_EVENT_REPLY_OTA_DRIVER_UPGRADE: {
                        this.uniUpgradeGatewayDriver(data, callback);
                    }
                    break;
                    //设备取消驱动,固件下载升级文件
                    case HDLUniMP.UNI_EVENT_REPLY_OTA_DRIVER_CANCEL_DOWNLOAD: {
                        this.uniCancelDownloadingUpgradeFile(data, callback);
//                        HdlThreadLogic.runSubThread(new Runnable() {
//                            @Override
//                            public void run() {
//                                HdlLogLogic.print("当前线程名称取消下载====" + Thread.currentThread().getName());
//                                uniCancelDownloadingUpgradeFile(data, callback);
//                            }
//                        });
                    }
                    break;
                    //设备驱动,固件取消升级
                    case HDLUniMP.UNI_EVENT_REPLY_OTA_CANCEL_UPGRADE: {
 
                        this.uniCancelUpgrade(data, callback);
                    }
                    break;
                    //打开服务
                    case HDLUniMP.UNI_EVENT_REPLY_OTA_OPEN_SERVICE: {
                        this.uniOpenService(data, callback);
                    }
                    break;
                    //关闭服务
                    case HDLUniMP.UNI_EVENT_REPLY_OTA_CLOSE_SERVICE: {
                        this.uniCloseService(data, callback);
                    }
                    break;
 
                }
            } else if (HDLUniMP.UNI_EVENT_REPLY_OTHER_MODEL.equals(topic)) {
                //其它模块
                switch (type) {
                    //获取温度单位
                    case HDLUniMP.UNI_EVENT_REPLY_OTHER_UNIT: {
                        uniCallbackData(UserConfigManage.getInstance().getTemperature_unit(), callback);
                    }
                    break;
                    //获取当前app语言
                    case HDLUniMP.UNI_EVENT_REPLY_OTHER_APP_LANGUAGE: {
                        uniCallbackData(UserConfigManage.getInstance().getCurrentAppLanguage(), callback);
                    }
                    break;
                }
            }
            HdlLogLogic.print("uni发来的数据--->大类-->" + topic + "\r\n" + data, true);
        } catch (Exception e) {
            HdlLogLogic.print("uni发来的数据--->" + e.getMessage(), true);
        }
 
    }
 
 
    /**
     * 原生【打开】uni指定页面
     *
     * @param path       打开路径
     * @param jsonObject 附件数据(没有数据填null)
     */
    public void openUniMP(String path, JSONObject jsonObject) {
        JSONObject json = this.createdJSONObject(jsonObject, true);
        HDLUniMPSDKManager.getInstance().openUniMP(HDLUniMP.UNI_APP_ID, path, json, HdlUniLogic.this);
        HdlLogLogic.print("uni--->组装uni发送数据格式--->" + json, false);
    }
 
    /**
     * 原生【主动】向小程序发送通知事件
     * 注意:需要提前小程序在运行才可成功
     *
     * @param topic            主题大类(如:UNI_EVENT_REPLY_HOME_MODEL)
     * @param callBackBaseBean 自定义实体类
     */
    public void sendUni(String topic, HDLUniMP.UniCallBackBaseBean callBackBaseBean) {
        try {
            HDLUniMPSDKManager.getInstance().sendUniMPEvent(HDLUniMP.UNI_APP_ID, topic, getJSONObject(callBackBaseBean));
        } catch (Exception e) {
            HdlLogLogic.print("uni--->原生主动向小程序发送通知事件--->" + e.getMessage(), false);
        }
    }
 
    /**
     * 检测是否当前页面注册的callback,是的话则移除
     */
    public void checkRemoveOtherUniMPEventCallBack() {
        HDLUniMPSDKManager.getInstance().checkRemoveOtherUniMPEventCallBack(this);
    }
    //endregion
 
    //region ******uni接口方法******
 
    /**
     * 向云端获取逆变器oid列表
     * 前提条件:要上传逆变器oid列表给云端
     *
     * @param callback -
     */
    private void uniGetCloudOidList(Object data, DCUniMPJSCallback callback) {
        HdlOtaLogic.getInstance().getCloudOidList(new CloudCallBeak<List<DeviceOidInfoBean>>() {
            @Override
            public void onSuccess(List<DeviceOidInfoBean> obj) {
                uniCallbackData(obj, callback);
            }
 
            @Override
            public void onFailure(HDLException e) {
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
 
    /**
     * 向云端获取【当前设备固件】列表
     * 前提条件:设备自动上报oid信息给云端
     */
    private void uniGetCurrentDeviceFirmwares(Object data, DCUniMPJSCallback callback) {
        String deviceOidId = getKeyValue("deviceOidId", getKeyValue("data", data));//云端上设备id
        HdlOtaLogic.getInstance().getCurrentDeviceFirmwares(deviceOidId, new CloudCallBeak<List<DeviceFirmwareBean>>() {
            @Override
            public void onSuccess(List<DeviceFirmwareBean> obj) {
                uniCallbackData(obj, callback);
            }
 
            @Override
            public void onFailure(HDLException e) {
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
 
    /**
     * 向云端获取【设备新固件】列表
     * 前提条件:要通过平台软件上传新固件
     */
    private void uniGetNewDeviceFirmwares(Object data, DCUniMPJSCallback callback) {
        String hardwareModel = getKeyValue("hardwareModel", getKeyValue("data", data));//硬件型号
        String osImageId = getKeyValue("osImageId", getKeyValue("data", data));//系统镜像id
        HdlOtaLogic.getInstance().getNewDeviceFirmwares(hardwareModel, osImageId, new CloudCallBeak<List<CloudDeviceFirmwaresBean>>() {
            @Override
            public void onSuccess(List<CloudDeviceFirmwaresBean> obj) {
                uniCallbackData(obj, callback);
            }
 
            @Override
            public void onFailure(HDLException e) {
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
 
    /**
     * 获取【设备本地固件】列表
     */
    private void uniGetLocalFirmwares(Object data, DCUniMPJSCallback callback) {
        try {
            String hardwareModel = getKeyValue("hardwareModel", getKeyValue("data", data));//硬件型号
            String osImageId = getKeyValue("osImageId", getKeyValue("data", data));//系统镜像id
            String firmwareVersionId = getKeyValue("firmwareVersionId", getKeyValue("data", data));//固件版本id
//            String oid = getKeyValue("oid", getKeyValue("data", data));//固件oid
            List<DeviceFirmwareBean> list = new ArrayList<>();
            String driverFileList = HdlFileLogic.getInstance().getFirmwareRootPath();
            File file = new File(driverFileList);
            if (file.list() != null) {
                for (int i = 0; i < Objects.requireNonNull(file.list()).length; i++) {
                    String path = Objects.requireNonNull(file.list())[i];
                    if (!path.contains(osImageId)) {
                        continue;
                    }
                    String[] ary = path.split("/");
                    String[] driverInfoAry = ary[ary.length - 1].split("_");
                    DeviceFirmwareBean deviceFirmwareBean = new DeviceFirmwareBean();
                    deviceFirmwareBean.setOid(driverInfoAry[0]);
                    deviceFirmwareBean.setImageId(driverInfoAry[1]);
                    deviceFirmwareBean.setVersion(driverInfoAry[2].replace(".zip", ""));
                    deviceFirmwareBean.setLocalUrl(HdlFileLogic.getInstance().getFirmwarePathFileName(deviceFirmwareBean.getOid(), osImageId, deviceFirmwareBean.getVersion()));
                    list.add(deviceFirmwareBean);
                }
            }
            if (callback != null) {
                this.uniCallbackData(list, callback);
            }
        } catch (Exception ignored) {
        }
    }
 
    /**
     * 向云端发起【设备固件下载】指令
     */
    private void uniDeviceFirmwareDownload(Object data, DCUniMPJSCallback callback) {
        String deviceOidId = getKeyValue("deviceOidId", getKeyValue("data", data));//云端上设备id
        String oid = getKeyValue("oid", getKeyValue("data", data));//该固件的oid
        String imageId = getKeyValue("imageId", getKeyValue("data", data));//固件镜像id
        String version = getKeyValue("version", getKeyValue("data", data));//固件版本
        String localUrl = getKeyValue("localUrl", getKeyValue("data", data));//储存在本地固件文件路径
        String url = getKeyValue("url", getKeyValue("data", data));//云端上文件地址
        String md5 = getKeyValue("md5", getKeyValue("data", data));//云端上文件MD5
        HdlOtaLogic.getInstance().getDeviceUpgradeDownloadFile(url, new CloudCallBeak<ResponseBody>() {
            @Override
            public void onSuccess(ResponseBody responseBody) {
                HdlThreadLogic.runSubThread(new Runnable() {
                    @Override
                    public void run() {
                        String firmwarePathFileName = HdlFileLogic.getInstance().getFirmwarePathFileName(oid, imageId, version);
                        //不在子线程读流会卡死主线程
                        boolean isBoolean = HdlOtaLogic.getInstance().disposeDownLoadFile(firmwarePathFileName, responseBody, md5, HdlOtaLogic.firmware_type);
                        if (isBoolean) {
                            HdlFileLogic.getInstance().deleteFile(localUrl);//下载成功,删除旧固件文件;
                        } else {
                            HdlFileLogic.getInstance().deleteFile(firmwarePathFileName);//下载失败,删除不完整固件文件;
                        }
 
                        if (isBoolean) {
                            HdlLogLogic.print("写入新固件文件到内存成功.", true);
                            uniCallbackData(null, 0, "写入新驱动文件到内存成功", callback);
                        } else {
                            HdlLogLogic.print("下载固件升级文件失败.", true);
                            uniCallbackData(null, -2, "下载固件升级文件失败", callback);
                        }
                    }
                });
 
            }
 
            @Override
            public void onFailure(HDLException e) {
                HdlLogLogic.print("下载固件文件到内存失败.", true);
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
 
    /**
     * 向云端发起【设备固件】升级OTA指令
     */
    private void uniUpgradeDeviceFirmware(Object data, DCUniMPJSCallback callback) {
        String homeId = UserConfigManage.getInstance().getHomeId();
        String deviceMac = getKeyValue("mac", getKeyValue("data", data));//逆变器设备mac(查找socket)
        String deviceOidId = getKeyValue("deviceOidId", getKeyValue("data", data));//云端上设备id
        String oid = getKeyValue("oid", getKeyValue("data", data));//升级固件的oid
        String firmwareVersionId = getKeyValue("firmwareVersionId", getKeyValue("data", data));//固件版本id
        String version = getKeyValue("version", getKeyValue("data", data));//固件版本
        String hardwareModel = getKeyValue("hardwareModel", getKeyValue("data", data));//云端上文件MD5
        String imageId = getKeyValue("imageId", getKeyValue("data", data));//固件镜像id
        String module = "FW#" + imageId;
 
        //升级本地优先->云端升级
        HdlDeviceLogic.getInstance().isLocalConnect(deviceMac, new CloudCallBeak<Boolean>() {
            @Override
            public void onSuccess(Boolean b) {
                if (b) {
                    HdlLogLogic.print("本地升级--->", true);
                    //本地
                    //1,建立本地服务;
                    //2,告诉网关手机ip和端口;
                    //实例化 获取ip 地址
                    HdlOtaLogic.getInstance().startLocalService(new ServiceConnection() {
                        @Override
                        public void onServiceConnected(ComponentName name, IBinder service) {
                            //本地升级固件文件路径
                            String firmwarePathFileName = HdlFileLogic.getInstance().getFirmwarePathFileName(oid, imageId, version);
                            byte[] data = HdlFileLogic.getInstance().readFileByte(firmwarePathFileName);
                            if (data == null || data.length == 0) {
                                uniCallbackData(null, -2, "本地找不到升级固件文件,请下载好固件文件,再重新升级.", callback);
                                return;
                            }
                            String md5 = HDLMD5Utils.encodeMD5(data);//网关固件需要
                            //升级固件文件地址
                            String upgradeFileLocalPathUrl = "http://" + NetworkUtils.getInstance().getIPAddress(HDLApp.getInstance()) + ":" + MyNanoHttpServer.HTTP_PORT + firmwarePathFileName;
                            //通知给网关升级固件文件地址等信息
                            HdlOtaLogic.getInstance().pushUpgradePacketInfo(deviceMac, oid, module, version, data.length + "", upgradeFileLocalPathUrl, md5, new LinkCallBack<String>() {
                                @Override
                                public void onSuccess(String obj) {
                                    HdlLogLogic.print("通知给网关升级固件文件地址成功.", true);
                                }
 
                                @Override
                                public void onError(HDLLinkException e) {
                                    HdlLogLogic.print("通知给网关升级固件文件地址失败,无法升级.", true);
                                    uniCallbackData(null, -2, "通知给网关升级固件文件地址失败,无法升级,", callback);
                                }
                            });
                        }
 
                        @Override
                        public void onServiceDisconnected(ComponentName name) {
                            uniCallbackData(null, -2, "本地服务有异常失败,无法升级,", callback);
                        }
                    });
                } else {
                    HdlLogLogic.print("在线升级--->", true);
                    //远程升级需要检测在逆变器有没有连接上云
                    HdlDeviceLogic.getInstance().checkInverterConnectedCloud(homeId, deviceMac, new CloudCallBeak<CloudInverterDeviceBean>() {
                        @Override
                        public void onSuccess(CloudInverterDeviceBean cloudInverterDeviceBean) {
                            //1:连接中,2:故障,3:运行,4:离线,6:逆变器连不上云(自定义)
                            if (cloudInverterDeviceBean == null) {
                                uniCallbackData(null, 6, HDLApp.getInstance().getString(R.string.ota_binding_cloud_upgrade_fails), callback);
                                return;
                            }
                            if (cloudInverterDeviceBean.getDeviceStatus() != 3) {
                                uniCallbackData(null, cloudInverterDeviceBean.getDeviceStatus(), HDLApp.getInstance().getString(R.string.ota_not_cloud_upgrade_fails), callback);
                                return;
                            }
                            //4:运行
                            HdlOtaLogic.getInstance().upgradeDeviceFirmware(deviceOidId, firmwareVersionId, new CloudCallBeak<Boolean>() {
                                @Override
                                public void onSuccess(Boolean obj) {
                                    uniCallbackData(obj, callback);
                                }
 
                                @Override
                                public void onFailure(HDLException e) {
                                    uniCallbackData(null, e.getCode(), e.getMsg(), callback);
                                }
                            });
 
                        }
 
                        @Override
                        public void onFailure(HDLException e) {
                            uniCallbackData(null, e.getCode(), e.getMsg(), callback);
                        }
                    });
                }
            }
 
            @Override
            public void onFailure(HDLException e) {
                HdlLogLogic.print("在线升级--->", true);
                //远程升级需要检测在逆变器有没有连接上云
                HdlDeviceLogic.getInstance().checkInverterConnectedCloud(homeId, deviceMac, new CloudCallBeak<CloudInverterDeviceBean>() {
                    @Override
                    public void onSuccess(CloudInverterDeviceBean cloudInverterDeviceBean) {
                        //1:连接中,2:故障,3:运行,4:离线,6:逆变器连不上云(自定义)
                        if (cloudInverterDeviceBean == null) {
                            uniCallbackData(null, 6, HDLApp.getInstance().getString(R.string.ota_binding_cloud_upgrade_fails), callback);
                            return;
                        }
                        if (cloudInverterDeviceBean.getDeviceStatus() != 3) {
                            uniCallbackData(null, cloudInverterDeviceBean.getDeviceStatus(), HDLApp.getInstance().getString(R.string.ota_not_cloud_upgrade_fails), callback);
                            return;
                        }
                        //4:运行
                        HdlOtaLogic.getInstance().upgradeDeviceFirmware(deviceOidId, firmwareVersionId, new CloudCallBeak<Boolean>() {
                            @Override
                            public void onSuccess(Boolean obj) {
                                uniCallbackData(obj, callback);
                            }
 
                            @Override
                            public void onFailure(HDLException e) {
                                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
                            }
                        });
 
                    }
 
                    @Override
                    public void onFailure(HDLException e) {
                        uniCallbackData(null, e.getCode(), e.getMsg(), callback);
                    }
                });
            }
        });
 
    }
 
    /**
     * 向云端获取【当前设备驱动】列表
     * 前提条件:设备自动上报oid信息给云端
     */
    private void uniGetCurrentGatewayDrivers(Object data, DCUniMPJSCallback callback) {
        String deviceOid = getKeyValue("oid", getKeyValue("data", data));
        HdlOtaLogic.getInstance().getCurrentGatewayDrivers(deviceOid, new CloudCallBeak<List<GatewayDriverBean>>() {
            @Override
            public void onSuccess(List<GatewayDriverBean> obj) {
                uniCallbackData(obj, callback);
            }
 
            @Override
            public void onFailure(HDLException e) {
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
 
    /**
     * 向云端获取【设备新驱动】列表
     * 前提条件:要通过平台软件上传新驱动
     */
    private void uniGetNewGatewayDrivers(Object data, DCUniMPJSCallback callback) {
        String driveCode = getKeyValue("driveCode", getKeyValue("data", data));//驱动编号或驱动名称
        String osImageId = getKeyValue("osImageId", getKeyValue("data", data));//驱动类型id
        HdlOtaLogic.getInstance().getNewGatewayDrivers(driveCode, osImageId, new CloudCallBeak<CloudGatewayDriversBean>() {
            @Override
            public void onSuccess(CloudGatewayDriversBean obj) {
                uniCallbackData(obj, callback);
            }
 
            @Override
            public void onFailure(HDLException e) {
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
 
    /**
     * 获取【设备本地驱动】列表
     */
    private void uniGetLocalDrivers(Object data, DCUniMPJSCallback callback) {
        try {
            String driveCode = getKeyValue("driveCode", getKeyValue("data", data));//驱动编号或驱动名称
            String osImageId = getKeyValue("osImageId", getKeyValue("data", data));//驱动类型id
            String driverVersionId = getKeyValue("driverVersionId", getKeyValue("data", data));//驱动类型id
            List<GatewayDriverBean> list = new ArrayList<>();
            String driverFileList = HdlFileLogic.getInstance().getDriveRootPath();
            File file = new File(driverFileList);
            if (file.list() != null) {
                for (int i = 0; i < Objects.requireNonNull(file.list()).length; i++) {
                    String fileName = Objects.requireNonNull(file.list())[i];
                    if (!fileName.contains(driveCode)) {
                        continue;
                    }
                    String[] driverInfoAry = fileName.split("_");
                    GatewayDriverBean gatewayDriverBean = new GatewayDriverBean();
                    gatewayDriverBean.setDriverCode(driverInfoAry[0]);
                    gatewayDriverBean.setVersion(driverInfoAry[1].replace(".zip", ""));
                    gatewayDriverBean.setLocalUrl(HdlFileLogic.getInstance().getDrivePathFileName(gatewayDriverBean.getDriverCode(), gatewayDriverBean.getVersion()));
                    list.add(gatewayDriverBean);
                }
            }
            if (callback != null) {
                this.uniCallbackData(list, callback);
            }
        } catch (Exception exception) {
            this.uniCallbackData(new ArrayList<>(), callback);
        }
 
    }
 
    /**
     * 向云端发起【设备驱动下载】指令
     */
    private void uniGatewayDriverDownload(Object data, DCUniMPJSCallback callback) {
 
        String deviceOid = getKeyValue("oid", getKeyValue("data", data));//逆变器设备oid
        String driverCode = getKeyValue("driverCode", getKeyValue("data", data));//驱动编码
        String imageId = getKeyValue("imageId", getKeyValue("data", data));//驱动镜像id
        String version = getKeyValue("version", getKeyValue("data", data));//驱动版本
        String driverVersionId = getKeyValue("driverVersionId", getKeyValue("data", data));//驱动版本id
        String localUrl = getKeyValue("localUrl", getKeyValue("data", data));//储存在本地驱动文件路径
        String url = getKeyValue("url", getKeyValue("data", data));//云端上文件地址
        String md5 = getKeyValue("md5", getKeyValue("data", data));//云端上文件MD5
        HdlOtaLogic.getInstance().getDeviceUpgradeDownloadFile(url, new CloudCallBeak<ResponseBody>() {
            @Override
            public void onSuccess(ResponseBody responseBody) {
                HdlThreadLogic.runSubThread(new Runnable() {
                    @Override
                    public void run() {
 
                        String drivePathFileName = HdlFileLogic.getInstance().getDrivePathFileName(driverCode, version);
                        //不在子线程读流会卡死主线程
                        boolean isBoolean = HdlOtaLogic.getInstance().disposeDownLoadFile(drivePathFileName, responseBody, md5, HdlOtaLogic.driver_type);
                        if (isBoolean) {
                            HdlFileLogic.getInstance().deleteFile(localUrl);//下载成功,删除旧驱动文件;
                        } else {
                            HdlFileLogic.getInstance().deleteFile(drivePathFileName);//下载失败,删除不完整驱动文件;
                        }
                        if (isBoolean) {
                            HdlLogLogic.print("写入新驱动文件到内存成功.", true);
                            uniCallbackData(null, 0, "写入新驱动文件到内存成功", callback);
                        } else {
                            HdlLogLogic.print("下载驱动升级文件失败.", true);
                            uniCallbackData(null, -2, "下载驱动升级文件失败", callback);
                        }
                    }
                });
 
            }
 
            @Override
            public void onFailure(HDLException e) {
                HdlLogLogic.print("下载驱动文件到内存失败.", true);
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
 
    /**
     * 向云端发起【设备驱动】升级OTA指令
     */
    private void uniUpgradeGatewayDriver(Object data, DCUniMPJSCallback callback) {
 
        String homeId = UserConfigManage.getInstance().getHomeId();
        String deviceMac = getKeyValue("mac", getKeyValue("data", data));//逆变器设备mac(查找socket)
        String deviceOid = getKeyValue("oid", getKeyValue("data", data));//逆变器设备oid
        String driverVersionId = getKeyValue("driverVersionId", getKeyValue("data", data));//驱动版本id
        String version = getKeyValue("version", getKeyValue("data", data));//驱动版本号
        String driverCode = getKeyValue("driverCode", getKeyValue("data", data));//驱动编码
        String imageId = getKeyValue("imageId", getKeyValue("data", data));//固件镜像id
        String module = driverCode + "#" + imageId;
        //升级本地优先->云端升级
        HdlDeviceLogic.getInstance().isLocalConnect(deviceMac, new CloudCallBeak<Boolean>() {
            @Override
            public void onSuccess(Boolean b) {
                if (b) {
                    HdlLogLogic.print("本地升级--->", true);
                    //本地
                    //1,建立本地服务;
                    // 2,告诉网关手机ip和端口;
                    //实例化 获取ip 地址
                    HdlOtaLogic.getInstance().startLocalService(new ServiceConnection() {
                        @Override
                        public void onServiceConnected(ComponentName name, IBinder service) {
                            //本地升级驱动文件路径
                            String drivePathFileName = HdlFileLogic.getInstance().getDrivePathFileName(driverCode, version);
                            byte[] data = HdlFileLogic.getInstance().readFileByte(drivePathFileName);
                            if (data == null || data.length == 0) {
                                uniCallbackData(null, -2, "本地找不到升级驱动文件,请下载好驱动文件,再重新升级.", callback);
                                return;
                            }
                            String md5 = HDLMD5Utils.encodeMD5(data);//网关驱动需要
                            //升级驱动文件地址
                            String upgradeFileLocalPathUrl = "http://" + NetworkUtils.getInstance().getIPAddress(HDLApp.getInstance()) + ":" + MyNanoHttpServer.HTTP_PORT + drivePathFileName;
                            //通知给网关升级驱动文件地址等信息
                            HdlOtaLogic.getInstance().pushUpgradePacketInfo(deviceMac, deviceOid, module, version, data.length + "", upgradeFileLocalPathUrl, md5, new LinkCallBack<String>() {
                                @Override
                                public void onSuccess(String obj) {
                                    HdlLogLogic.print("通知给网关升级驱动文件地址成功.", true);
                                }
 
                                @Override
                                public void onError(HDLLinkException e) {
                                    HdlLogLogic.print("通知给网关升级驱动文件地址失败,无法升级.", true);
                                    uniCallbackData(null, -2, "通知给网关升级驱动文件地址失败,无法升级,", callback);
                                }
                            });
                        }
 
                        @Override
                        public void onServiceDisconnected(ComponentName name) {
                            uniCallbackData(null, -2, "本地服务有异常失败,无法升级,", callback);
                        }
                    });
                } else {
                    HdlLogLogic.print("在线升级--->", true);
                    //远程升级需要【检测】在逆变器有没有连接上云
                    HdlDeviceLogic.getInstance().checkInverterConnectedCloud(homeId, deviceMac, new CloudCallBeak<CloudInverterDeviceBean>() {
                        @Override
                        public void onSuccess(CloudInverterDeviceBean cloudInverterDeviceBean) {
                            //1:连接中,2:故障,3:运行,4:离线,6:逆变器连不上云(自定义)
                            if (cloudInverterDeviceBean == null) {
                                uniCallbackData(null, 6, HDLApp.getInstance().getString(R.string.ota_binding_cloud_upgrade_fails), callback);
                                return;
                            }
                            if (cloudInverterDeviceBean.getDeviceStatus() != 3) {
                                uniCallbackData(null, cloudInverterDeviceBean.getDeviceStatus(), HDLApp.getInstance().getString(R.string.ota_not_cloud_upgrade_fails), callback);
                                return;
                            }
                            //4:运行
                            HdlOtaLogic.getInstance().upgradeGatewayDriver(deviceOid, driverVersionId, new CloudCallBeak<Boolean>() {
                                @Override
                                public void onSuccess(Boolean obj) {
                                    uniCallbackData(obj, callback);
                                }
 
                                @Override
                                public void onFailure(HDLException e) {
                                    uniCallbackData(null, e.getCode(), e.getMsg(), callback);
                                }
                            });
 
                        }
 
                        @Override
                        public void onFailure(HDLException e) {
                            uniCallbackData(null, e.getCode(), e.getMsg(), callback);
                        }
                    });
                }
            }
 
            @Override
            public void onFailure(HDLException e) {
                HdlLogLogic.print("在线升级--->", true);
                //远程升级需要【检测】在逆变器有没有连接上云
                HdlDeviceLogic.getInstance().checkInverterConnectedCloud(homeId, deviceMac, new CloudCallBeak<CloudInverterDeviceBean>() {
                    @Override
                    public void onSuccess(CloudInverterDeviceBean cloudInverterDeviceBean) {
                        //1:连接中,2:故障,3:运行,4:离线,6:逆变器连不上云(自定义)
                        if (cloudInverterDeviceBean == null) {
                            uniCallbackData(null, 6, HDLApp.getInstance().getString(R.string.ota_binding_cloud_upgrade_fails), callback);
                            return;
                        }
                        if (cloudInverterDeviceBean.getDeviceStatus() != 3) {
                            uniCallbackData(null, cloudInverterDeviceBean.getDeviceStatus(), HDLApp.getInstance().getString(R.string.ota_not_cloud_upgrade_fails), callback);
                            return;
                        }
                        //4:运行
                        HdlOtaLogic.getInstance().upgradeGatewayDriver(deviceOid, driverVersionId, new CloudCallBeak<Boolean>() {
                            @Override
                            public void onSuccess(Boolean obj) {
                                uniCallbackData(obj, callback);
                            }
 
                            @Override
                            public void onFailure(HDLException e) {
                                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
                            }
                        });
 
                    }
 
                    @Override
                    public void onFailure(HDLException e) {
                        uniCallbackData(null, e.getCode(), e.getMsg(), callback);
                    }
                });
            }
        });
 
    }
 
    /**
     * 取消下载【驱动,固件】升级文件
     */
    private void uniCancelDownloadingUpgradeFile(Object data, DCUniMPJSCallback callback) {
        HdlOtaLogic.getInstance().stopDownloadUpgradeFile();
        uniCallbackData(null, 0, "取消下载成功.", callback);
    }
 
    /**
     * 取消【驱动,固件】升级
     */
    private void uniCancelUpgrade(Object data, DCUniMPJSCallback callback) {
        HdlOtaLogic.getInstance().cancelUpgrade("", new LinkCallBack<Boolean>() {
            @Override
            public void onSuccess(Boolean obj) {
 
            }
 
            @Override
            public void onError(HDLLinkException e) {
 
            }
        });
    }
 
    /**
     * 开启服务
     */
    private void uniOpenService(Object data, DCUniMPJSCallback callback) {
        HdlOtaLogic.getInstance().startLocalService(new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
            }
 
            @Override
            public void onServiceDisconnected(ComponentName name) {
                HdlOtaLogic.getInstance().mServiceStart = false;
            }
        });
    }
 
    /**
     * 关闭服务
     */
    private void uniCloseService(Object data, DCUniMPJSCallback callback) {
        HdlOtaLogic.getInstance().unLocalService();
    }
 
 
    /**
     * @param deviceStatus 逆变器连接云端状态值( 1:连接中,2:故障,3:运行,4:离线)
     * @return 返回文本
     */
    public String getDeviceStatusString(int deviceStatus) {
        switch (deviceStatus) {
            case 1: {
                return HDLApp.getInstance().getString(R.string.my_power_station_connecting);
            }
            case 2: {
                return HDLApp.getInstance().getString(R.string.my_power_station_malfunction);
            }
            case 4: {
                return HDLApp.getInstance().getString(R.string.my_power_station_off_line);
            }
            default:
                return "";
        }
 
    }
 
    /**
     * 逆变器清空住宅id
     *
     * @param data     uni数据
     * @param callback uni回调
     */
    private void uniClearInverterHomeId(Object data, DCUniMPJSCallback callback) {
        String mac = getKeyValue("mac", getKeyValue("data", data));
        HdlDeviceLogic.getInstance().clearInverterHomeId(mac, new LinkCallBack<Boolean>() {
            @Override
            public void onSuccess(Boolean obj) {
                uniCallbackData(null, callback);
            }
 
            @Override
            public void onError(HDLLinkException e) {
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
 
    /**
     * 逆变器【上传数据】到云端
     *
     * @param data     uni数据
     * @param callback uni回调
     */
    private void uniUploadDataToCloud(Object data, DCUniMPJSCallback callback) {
        String mac = getKeyValue("mac", getKeyValue("data", data));
        HdlDeviceLogic.getInstance().uploadDataToCloud(UserConfigManage.getInstance().getHomeId(), mac, null);
    }
 
    /**
     * 编辑逆变器时间
     *
     * @param data     uni数据
     * @param callback uni回调
     */
    private void uniEditInverterTime(Object data, DCUniMPJSCallback callback) {
        String mac = getKeyValue("mac", getKeyValue("data", data));
        String date = getKeyValue("date", getKeyValue("data", data));
        String time = getKeyValue("time", getKeyValue("data", data));
        HdlDeviceLogic.getInstance().editGatewayTime(mac, date, time, new LinkCallBack<Boolean>() {
            @Override
            public void onSuccess(Boolean obj) {
                uniCallbackData(true, callback);
            }
 
            @Override
            public void onError(HDLLinkException e) {
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
 
    /**
     * 获取逆变器时间
     *
     * @param data     uni数据
     * @param callback uni回调
     */
    private void uniGetInverterTime(Object data, DCUniMPJSCallback callback) {
        String mac = getKeyValue("mac", getKeyValue("data", data));
        HdlDeviceLogic.getInstance().getGatewayTime(mac, new LinkCallBack<DeviceTimeBean>() {
            @Override
            public void onSuccess(DeviceTimeBean deviceTimeBean) {
                if (callback != null) {
                    uniCallbackData(deviceTimeBean, callback);
                }
            }
 
            @Override
            public void onError(HDLLinkException e) {
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
 
    /**
     * 获取oid列表
     *
     * @param data     uni数据
     * @param callback uni回调
     */
    private void uniGetInverterOidList(Object data, DCUniMPJSCallback callback) {
        String mac = getKeyValue("mac", getKeyValue("data", data));
        HdlDeviceLogic.getInstance().getInverterOidList(mac, new LinkCallBack<List<OidBean>>() {
            @Override
            public void onSuccess(List<OidBean> list) {
                if (callback != null) {
                    uniCallbackData(list, callback);
                }
            }
 
            @Override
            public void onError(HDLLinkException e) {
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
 
    }
 
    /**
     * 逆变器添加到云端上
     *
     * @param data     uni数据
     * @param callback uni回调
     */
    private void uniAddInverterDeviceToCloud(Object data, DCUniMPJSCallback callback) {
        String mac = getKeyValue("mac", getKeyValue("data", data));
        String homeId = UserConfigManage.getInstance().getHomeId();
        HdlDeviceLogic.getInstance().setGatewayRemoteParam(homeId, mac, new LinkCallBack<Boolean>() {
            @Override
            public void onSuccess(Boolean obj) {
                HdlDeviceLogic.getInstance().editGatewayParam(mac, new LinkCallBack<Boolean>() {
                    @Override
                    public void onSuccess(Boolean obj) {
                        GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getLocalGateway(mac);
                        if (gatewayBean == null) {
                            uniCallbackData(null, -100, "本地找不到网关", callback);
 
                            return;
                        }
                        HdlDeviceLogic.getInstance().addInverterDeviceToCloud(homeId, mac, gatewayBean.getGatewayType(), gatewayBean.getSid(), gatewayBean.getOid(), gatewayBean.getDevice_name(), new CloudCallBeak<Boolean>() {
                            @Override
                            public void onSuccess(Boolean obj) {
                                uniCallbackData(null, callback);
                            }
 
                            @Override
                            public void onFailure(HDLException e) {
                                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
                            }
                        });
                    }
 
                    @Override
                    public void onError(HDLLinkException e) {
                        uniCallbackData(null, e.getCode(), e.getMsg(), callback);
                    }
                });
            }
 
            @Override
            public void onError(HDLLinkException e) {
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
 
    /**
     * 删除云端上逆变器
     *
     * @param data     uni数据
     * @param callback uni回调
     */
    private void uniDelInverterDevice(Object data, DCUniMPJSCallback callback) {
        String deviceId = getKeyValue("deviceId", getKeyValue("data", data));
        String homeId = UserConfigManage.getInstance().getHomeId();
        HdlDeviceLogic.getInstance().delInverterDevice(homeId, deviceId, new CloudCallBeak<Boolean>() {
            @Override
            public void onSuccess(Boolean obj) {
                uniCallbackData(null, callback);
            }
 
            @Override
            public void onFailure(HDLException e) {
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
 
 
    /**
     * 局域网搜索逆变器列表
     *
     * @param callback uni回调
     */
    private void uniSearchGateway(DCUniMPJSCallback callback) {
        HdlDeviceLogic.getInstance().searchCurrentHomeGateway(new GatewayCallBack() {
            @Override
            public void onSuccess(List<GatewayBean> gatewayBeanList) {
                uniCallbackData(gatewayBeanList, callback);
            }
 
            @Override
            public void onError(HDLLinkException e) {
                //发送失败
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
 
    }
 
    /**
     * 获取逆变器列表
     *
     * @param callback uni回调
     */
    private void uniGetCurrentHomeLocalAndCloudGatewayList(Object data, DCUniMPJSCallback callback) {
        String homeId = getKeyValue("homeId", getKeyValue("data", data));
        HdlDeviceLogic.getInstance().getCurrentHomeLocalAndCloudGatewayList(homeId, new CloudCallBeak<List<GatewayBean>>() {
            @Override
            public void onSuccess(List<GatewayBean> list) {
                GatewayBean gatewayBean = HdlDeviceLogic.getInstance().queryCurrentHomeMainGateway(list, homeId);
                if (gatewayBean != null) {
                    //进来住宅详情都要上传一次oid列表到云端;
                    HdlDeviceLogic.getInstance().uploadDataToCloud(homeId, gatewayBean.getDevice_mac(), null);
                }
                //EventBus事件分发,进入住宅开始订阅主题
                BaseEventBus baseEventBus = new BaseEventBus();
                baseEventBus.setType(HDLUniMP.UNI_EVENT_REPLY_DEVICE_LIST);
                baseEventBus.setData(list);
 
                EventBus.getDefault().post(baseEventBus);
                if (callback != null) {
                    uniCallbackData(list, callback);
                }
 
 
            }
 
            @Override
            public void onFailure(HDLException e) {
                if (callback != null) {
                    uniCallbackData(null, e.getCode(), e.getMsg(), callback);
                }
            }
        });
 
    }
 
 
    /**
     * 发送modbus协议数据
     * 透传协议
     * 下发主题:/user/${gw_id}/custom/native/${driver}/down;
     * 逆变器回复主题:/user/${gw_id}/custom/native/${driver}/down_reply;
     * Modbus ECU协议:事件ID(2个byte)->协议[固定:0,0](2个byte)->长度(2byte)->标识符[oid的addresses值](4个byte)->功能码(1个byte)->负载数据(N个byte);
     * 长度(2个byte)=标识符(4个byte)+功能码(1个byte)+负载数据(N个byte);
     * 负载数据=寄存器地址(2个byte)+寄存器长度(2个byte)+内容长度(1个byte)+内容数据(N个byte)【注意:单个写入寄存器-->去掉<寄存器长度>和<内容长度>】;
     * 寄存器长度=(内容数据/2);
     * 例子:new byte[]{00,01,00,00,00,0x09,00,00,00,01,03,00,00,00,01};
     *
     * @param data     modbus数据
     * @param callback 回调
     */
    private void sendModBus(Object data, DCUniMPJSCallback callback) {
        String tempData = getKeyValue("data", data);
        if (TextUtils.isEmpty(tempData)) {
            HdlLogLogic.print("data内容为空", false);
            return;
        }
        ModBusBean modBusBean = new Gson().fromJson(tempData, ModBusBean.class);
//        ModBusBean modBusBean = new ModBusBean();
//        modBusBean.setOid("0101050219D44A00");
//        modBusBean.setData(new byte[]{00,01,00,00,00,0x09,00,00,00,01,03,00,00,00,01});
//        if (TextUtils.isEmpty(modBusBean.getOid())) {
//            modBusBean.setOid("0101050217BBC400");
//        }
        if (modBusBean.getMac() == null || modBusBean.getData() == null) {
            HdlLogLogic.print("内容为空===oid=" + modBusBean.getMac() + " data=" + Arrays.toString(modBusBean.getData()), false);
            return;
        }
 
 
        //发送modbus协议
        HDLModBusConnect.getInstance().Send(modBusBean.getMac(), modBusBean.getData(), new ModbusCallBack() {
            @Override
            public void onSuccess(int[] data) {
                Log.d("data", Arrays.toString(data));
                uniCallbackData(data, callback);
            }
 
            @Override
            public void onError(HDLLinkException e) {
                uniCallbackData(null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
 
    //endregion
 
    //region ******一般方法******
 
    /**
     * 组装uni发送数据格式
     *
     * @param data                   附加数据(没有数据填null)
     * @param isTokenAndRefreshToken (true=底层默认添加token和refreshToken;false=不加)
     * @return JSONObject            uni方法名里面参数需要的JSONObject对象
     */
    private JSONObject createdJSONObject(JSONObject data, boolean isTokenAndRefreshToken) {
        HDLUniMP.UniCallBackBaseBean uniCallBackBaseBean = new HDLUniMP.UniCallBackBaseBean();
        try {
            if (data == null) {
                data = new JSONObject();
            }
            if (isTokenAndRefreshToken) {
                //小程序那里有自己的请求方法,需要这些数据
                data.put("token", UserConfigManage.getInstance().getToken());
                data.put("refreshToken", UserConfigManage.getInstance().getRefreshToken());
                data.put("serverAddress", AppConfigManage.getUserRegionUrl());
                data.put("appKey", AppConfigManage.getAppKey());
                data.put("appSecret", AppConfigManage.getAppSecret());
            }
            uniCallBackBaseBean.setData(data);
            return getJSONObject(uniCallBackBaseBean);
        } catch (Exception e) {
            HdlLogLogic.print("uni--->组装uni发送数据格式有异常--->" + e.getMessage(), false);
        }
        return new JSONObject();
    }
 
 
    /**
     * 组装uni发送数据格式
     * 回调数据给uni小程序,统一在这里处理;
     *
     * @param obj      附加数据
     * @param code     状态码-成功(0)或者失败(-2)
     * @param msg      结果描述的信息
     * @param callback 回调
     */
    private void uniCallbackData(Object obj, int code, String msg, DCUniMPJSCallback callback) {
        HDLUniMP.UniCallBackBaseBean uniCallBackBaseBean = new HDLUniMP.UniCallBackBaseBean();
        try {
            uniCallBackBaseBean.setCode(code);
            uniCallBackBaseBean.setMes(msg);
            uniCallBackBaseBean.setData(obj);
            if (callback != null) {
                callback.invoke(getJSONObject(uniCallBackBaseBean));
//                callback.invoke(uniCallBackBaseBean);
                HdlLogLogic.print("uni--->组装uni发送数据格式--->" + new Gson().toJson(uniCallBackBaseBean), false);
 
            }
        } catch (Exception e) {
            HdlLogLogic.print("uni===组装uni发送数据格式===" + e.getMessage(), false);
        }
 
 
    }
 
    private void uniCallbackData(Object obj, DCUniMPJSCallback callback) {
        uniCallbackData(obj, HDLLinkCode.HDL_SUCCESS.getCode(), HDLLinkCode.HDL_SUCCESS.getMsg(), callback);
    }
 
    /**
     * 当前对象转 JSONObject
     *
     * @return JSONObject
     */
    public JSONObject getJSONObject(Object obj) {
        try {
            if (obj == null) {
                return new JSONObject();
            }
            if (TextUtils.isEmpty(obj.toString())) {
                return new JSONObject();
            }
            if (obj.toString().startsWith("{") && obj.toString().endsWith("}")) {
                return new JSONObject(obj.toString());
            }
            if (obj instanceof JSONObject) {
                return (JSONObject) obj;
            }
            String json = new Gson().toJson(obj);
            return new JSONObject(json);
        } catch (Exception e) {
            return new JSONObject();
        }
    }
 
 
    /**
     * 获取 KeyValue
     *
     * @param key -
     * @param obj -
     * @return value
     */
    public String getKeyValue(String key, Object obj) {
        try {
            JSONObject jsonObject = this.getJSONObject(obj);
            if (jsonObject.has(key)) {
                return jsonObject.getString(key);
            }
            return "";
        } catch (Exception e) {
            return "";
        }
 
    }
    //endregion
 
}