mac
2023-10-10 6e418105015e0ca91fe2240bb4a3584fd165ddd7
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
package com.hdl.photovoltaic.other;
 
 
import android.text.TextUtils;
import android.util.Log;
 
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.hdl.linkpm.sdk.core.exception.HDLException;
import com.hdl.photovoltaic.HDLApp;
import com.hdl.photovoltaic.bean.BaseEventBus;
import com.hdl.photovoltaic.bean.ModBusBean;
import com.hdl.photovoltaic.config.ConstantManage;
import com.hdl.photovoltaic.config.UserConfigManage;
import com.hdl.photovoltaic.listener.CloudCallBeak;
import com.hdl.photovoltaic.listener.LinkCallBack;
import com.hdl.photovoltaic.ui.bean.OidBean;
import com.hdl.photovoltaic.uni.HDLUniMP;
import com.hdl.photovoltaic.uni.HDLUniMPSDKManager;
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.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.core.utils.mqtt.MqttRecvClient;
import com.hdl.sdk.link.gateway.HDLLinkLocalGateway;
 
import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;
 
 
import java.util.Arrays;
import java.util.List;
 
import io.dcloud.feature.unimp.DCUniMPJSCallback;
 
/**
 * 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 event    -uni小程序发来数据
     * @param data     JSONObject com.alibaba.fastjson解析处理
     * @param callback callback.invoke(JSONObject) 用com.alibaba.fastjson
     */
    @Override
    public void onOtherUniMPEventReceive(String appId, String event, 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(event)) {
                //住宅模块
                switch (type) {
                    //创建电站
                    case HDLUniMP.UNI_EVENT_REPLY_HOME_CREATION: {
 
                        //EventBus事件分发
                        BaseEventBus baseEventBus = new BaseEventBus();
                        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;
 
                }
                if (callback != null) {
                    uniCallbackData(null, callback);
                }
            } else if (HDLUniMP.UNI_EVENT_REPLY_DEVICE_MODEL.equals(event)) {
                //设备模块
                switch (type) {
                    //添加逆变器到云端
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_ADD: {
                        this.uniAddInverterDeviceToCloud(data, callback);
 
                    }
                    //删除云端逆变器
                    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(callback);
                    }
                    break;
                    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(event)) {
                WifiUtils wifiUtils = new WifiUtils(HDLApp.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;
 
                }
            }
            HdlLogLogic.print("uni===原生接收uni发来的数据", event + "\r\n" + data);
        } catch (Exception e) {
            HdlLogLogic.print("uni===原生接收uni发来的数据", e.getMessage());
        }
 
    }
 
 
    /**
     * 原生打开uni指定页面
     *
     * @param path       打开路径
     * @param jsonObject 附件数据(没有数据填null)
     */
    public void openUniMP(String path, JSONObject jsonObject) {
 
        JSONObject json = this.createdJsonDate(jsonObject, true);
        HdlLogLogic.print("uni===组装uni发送数据格式", json.toString());
        HDLUniMPSDKManager.getInstance().openUniMP(HDLUniMP.UNI_APP_ID, path, json, HdlUniLogic.this);
    }
 
    /**
     * 原生【主动】向小程序发送通知事件
     * 注意:需要提前小程序在运行才可成功
     *
     * @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());
        }
    }
 
    /**
     * 检测是否当前页面注册的callback,是的话则移除
     */
    public void checkRemoveOtherUniMPEventCallBack() {
        HDLUniMPSDKManager.getInstance().checkRemoveOtherUniMPEventCallBack(this);
    }
    //endregion
 
    //region ******uni逻辑方法******
 
    /**
     * 逆变器清空住宅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, -100, e.getMessage(), callback);
            }
        });
    }
 
    /**
     * 逆变器上传数据到云端
     *
     * @param data     uni数据
     * @param callback uni回调
     */
    private void uniUploadDataToCloud(Object data, DCUniMPJSCallback callback) {
        String mac = getKeyValue("mac", getKeyValue("data", data));
        HdlDeviceLogic.getInstance().uploadDataToCloud(mac, null);
    }
 
    /**
     * 编辑逆变器时间
     *
     * @param data     uni数据
     * @param callback uni回调
     */
    private void uniEditInverterTime(Object data, DCUniMPJSCallback callback) {
        String mac = getKeyValue("mac", getKeyValue("data", data));
        HdlDeviceLogic.getInstance().editGatewayTime(mac, new JsonObject(), null);
    }
 
    /**
     * 获取逆变器时间
     *
     * @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<Boolean>() {
            @Override
            public void onSuccess(Boolean obj) {
 
            }
 
            @Override
            public void onError(HDLLinkException e) {
 
            }
        });
    }
 
    /**
     * 获取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, -2, e.getMsg(), callback);
            }
        });
    }
 
    /**
     * 逆变器添加到云端上
     *
     * @param data     uni数据
     * @param callback uni回调
     */
    private void uniAddInverterDeviceToCloud(Object data, DCUniMPJSCallback callback) {
        String mac = getKeyValue("mac", getKeyValue("data", data));
        HdlDeviceLogic.getInstance().setGatewayRemoteParam(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(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, -100, e.getMessage(), callback);
                                    }
                                });
                    }
 
                    @Override
                    public void onError(HDLLinkException e) {
                        uniCallbackData(null, -100, e.getMessage(), callback);
                    }
                });
            }
 
            @Override
            public void onError(HDLLinkException e) {
                uniCallbackData(null, -100, e.getMessage(), callback);
            }
        });
    }
 
    /**
     * 逆变器添加到云端上
     *
     * @param data     uni数据
     * @param callback uni回调
     */
    private void uniDelInverterDevice(Object data, DCUniMPJSCallback callback) {
        String deviceId = getKeyValue("deviceId", getKeyValue("data", data));
        HdlDeviceLogic.getInstance().delInverterDevice(deviceId, new CloudCallBeak<Boolean>() {
            @Override
            public void onSuccess(Boolean obj) {
                uniCallbackData(null, callback);
            }
 
            @Override
            public void onFailure(HDLException e) {
                uniCallbackData(null, -100, e.getMessage(), callback);
            }
        });
    }
 
 
    /**
     * 局域网搜索逆变器列表
     *
     * @param callback uni回调
     */
    private void uniSearchGateway(DCUniMPJSCallback callback) {
        HdlDeviceLogic.getInstance().searchGateway(new GatewayCallBack() {
            @Override
            public void onSuccess(List<GatewayBean> gatewayBeanList) {
                uniCallbackData(gatewayBeanList, callback);
            }
 
            @Override
            public void onError(HDLLinkException e) {
                //发送失败
                uniCallbackData(null, e.getCode(), "", callback);
            }
        });
 
    }
 
    /**
     * 获取逆变器列表
     *
     * @param callback uni回调
     */
    private void uniGetCurrentHomeLocalAndCloudGatewayList(DCUniMPJSCallback callback) {
        HdlDeviceLogic.getInstance().getCurrentHomeLocalAndCloudGatewayList(new CloudCallBeak<List<GatewayBean>>() {
            @Override
            public void onSuccess(List<GatewayBean> list) {
                if (callback != null) {
                    uniCallbackData(list, callback);
                    //EventBus事件分发
                    BaseEventBus baseEventBus = new BaseEventBus();
                    baseEventBus.setType(HDLUniMP.UNI_EVENT_REPLY_DEVICE_LIST);
                    baseEventBus.setData(list);
                    EventBus.getDefault().post(baseEventBus);
                }
            }
 
            @Override
            public void onFailure(HDLException e) {
                if (callback != null) {
                    uniCallbackData(null, -2, e.getMessage(), callback);
                }
            }
        });
 
    }
 
 
    /**
     * 发送modbus协议数据
     *
     * @param data     modbus数据
     * @param callback 回调
     */
    void sendModBus(Object data, DCUniMPJSCallback callback) {
        String tempData = getKeyValue("data", data);
        if (TextUtils.isEmpty(tempData)) {
            HdlLogLogic.print("data内容为空");
            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=" + modBusBean.getData());
            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
     */
    private JSONObject createdJsonDate(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());
            }
            uniCallBackBaseBean.setData(data);
            return getJSONObject(uniCallBackBaseBean);
        } catch (Exception e) {
            HdlLogLogic.print("uni===组装uni发送数据格式", e.getMessage());
        }
        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);
 
            }
        } catch (Exception e) {
            HdlLogLogic.print("uni===组装uni发送数据格式", e.getMessage());
        }
 
 
    }
 
    private void uniCallbackData(Object obj, DCUniMPJSCallback callback) {
        uniCallbackData(obj, HDLLinkCode.HDL_SUCCESS.getCode(), HDLLinkCode.HDL_SUCCESS.getMsg(), callback);
    }
 
    /**
     * 当前对象转 JSONObject
     *
     * @return JSONObject
     */
    private 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
     */
    private 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
 
}