JLChen
2021-12-10 739d5bad0d24c5f4913f8d8e09292b5b98991fac
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
package com.hdl.sdk.connect.socket;
 
import android.text.TextUtils;
 
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.hdl.sdk.common.config.TopicConstant;
import com.hdl.sdk.common.event.EventDispatcher;
import com.hdl.sdk.common.event.EventListener;
import com.hdl.sdk.common.exception.HDLLinkCode;
import com.hdl.sdk.common.exception.HDLLinkException;
import com.hdl.sdk.common.utils.IdUtils;
import com.hdl.sdk.common.utils.IpUtils;
import com.hdl.sdk.common.utils.LogUtils;
import com.hdl.sdk.common.utils.SPUtils;
import com.hdl.sdk.common.utils.ThreadToolUtils;
import com.hdl.sdk.common.utils.gson.GsonConvert;
import com.hdl.sdk.connect.bean.response.BaseLocalResponse;
import com.hdl.sdk.connect.bean.request.DeviceControlRequest;
import com.hdl.sdk.connect.bean.request.FunctionAttributeRequest;
import com.hdl.sdk.connect.bean.response.GatewaySearchBean;
import com.hdl.sdk.connect.bean.LinkRequest;
import com.hdl.sdk.connect.bean.LinkResponse;
import com.hdl.sdk.connect.bean.request.PropertyReadRequest;
import com.hdl.sdk.connect.bean.request.PropertyUpRequest;
import com.hdl.sdk.connect.callback.BaseCallBack;
import com.hdl.sdk.connect.callback.HDLLinkCallBack;
import com.hdl.sdk.connect.config.HDLLinkConfig;
import com.hdl.sdk.connect.protocol.LinkMessageDecoder;
import com.hdl.sdk.connect.protocol.LinkMessageEncoder;
import com.hdl.sdk.socket.SocketBoot;
import com.hdl.sdk.socket.SocketOptions;
import com.hdl.sdk.socket.client.TcpClient;
import com.hdl.sdk.socket.client.UdpClient;
import com.hdl.sdk.socket.codec.MessagePipeLine;
import com.hdl.sdk.socket.listener.ConnectStatusListener;
import com.hdl.sdk.socket.listener.SendListener;
 
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
 
/**
 * Created by Tong on 2021/9/26.
 * 1、通过Udp 组播或者广播搜索网关
 * 2、通过Udp 获取Tcp ip 端口统一8586
 */
public class HDLSocket {
 
//    public interface CallBack extends BaseCallBack {
//        void onResponse(String data);
//    }
//
//    /**
//     * udp默认组播ip
//     */
//    private static final String UDP_GROUP_IP = "239.0.168.188";
 
    /**
     * udp默认端口
     */
    private static final int UDP_PORT = 8585;
 
    /**
     * tcp默认端口
     */
    private static final int TCP_PORT = 8586;
 
    //    private String tcpIp;
//    private int tcpPort;
 
//    private int udpPort;
//    private String udpIp;
 
//    private static SocketBoot updBoot;
    private SocketBoot tcpBoot;
 
    private ConnectStatusListener statusListener;
 
//    private EventListener searchEvent;
//    private CallBack searchCallBack;
//    private ScheduledExecutorService searchGatewayThread;
//    private final AtomicInteger searchCount = new AtomicInteger(0);
 
    private HDLSocket() {
        statusListener = new ConnectStatusListener() {
            @Override
            public void onConnecting() {
 
            }
 
            @Override
            public void onConnected() {
 
            }
 
            @Override
            public void onConnectFailed() {
 
            }
        };
//        searchEvent = new EventListener() {
//            @Override
//            public void onMessage(Object msg) {
//                try {
//                    if (msg instanceof LinkResponse) {
//                        LinkResponse linkResponse = (LinkResponse) msg;
//                        String data = linkResponse.getData();
//                        if (!TextUtils.isEmpty(data)) {
//                            final BaseLocalResponse<GatewaySearchBean> response = GsonConvert.getGson().fromJson(data, new TypeToken<BaseLocalResponse<GatewaySearchBean>>() {
//                            }.getType());
//                            GatewaySearchBean searchBean = response.getObjects();
//                            if (searchBean != null) {
////                                gatewayId = searchBean.getGatewayId();
////                                if (!TextUtils.isEmpty(gatewayId)) {
////                                    SPUtils.put(GATEWAY_KEY, gatewayId);
////                                }
////                                tcpIp = searchBean.getIp_address();
////                                if (!TextUtils.isEmpty(tcpIp)) {
////                                    SPUtils.put(TCP_IP_KEY, tcpIp);
////                                }
//                            }
//
//                            if (searchCallBack != null) {
//                                searchCallBack.onResponse(linkResponse.toString());
//                            }
//                        }
//
//                    }
//                } catch (Exception e) {
//                    if (searchCallBack != null) {
//                        searchCallBack.onError("解析失败");
//                    }
//                }
//
//
//            }
//        };
    }
 
    private static class SingletonInstance {
        private static final HDLSocket INSTANCE = new HDLSocket();
    }
 
    public static HDLSocket getInstance() {
        return SingletonInstance.INSTANCE;
    }
 
 
//    private SocketOptions getUdpOptions() {
//        final SocketOptions options = new SocketOptions();
//        final MessagePipeLine pipeLine = new MessagePipeLine();
//        pipeLine.add(new LinkMessageDecoder());
//        pipeLine.add(new LinkMessageEncoder());
//        options.setHandleMessage(pipeLine);
//        options.setEnabledHeartbeat(false);
//        return options;
//    }
 
    private SocketOptions getTcpOptions() {
        final SocketOptions options = new SocketOptions();
        final MessagePipeLine pipeLine = new MessagePipeLine();
        pipeLine.add(new LinkMessageDecoder());
        pipeLine.add(new LinkMessageEncoder());
        options.setHandleMessage(pipeLine);
        options.setEnabledHeartbeat(false);//是否开启心跳包发送检测
//        options.setHeartbeatTimeInterval(10*1000L);
//        options.setHeartbeatData("TCP");
//        options.setEnabledHeartbeat(true);//是否开启心跳包发送检测
        return options;
    }
 
    private int getUdpPort() {
        return UDP_PORT;
    }
 
    public int getTcpPort() {
        return TCP_PORT;
    }
 
    public String getTcpIp() {
        return HDLLinkConfig.getInstance().getIpAddress();
    }
 
    public String getGatewayId() {
        return HDLLinkConfig.getInstance().getGatewayId();
    }
 
 
//    private String getUdpIp() {
//        if (TextUtils.isEmpty(udpIp)) {
//            udpIp = UDP_GROUP_IP;
//        }
//        return udpIp;
//    }
 
//    public void searchGateway() {
//        searchGateway(null);
//    }
//
//    /**
//     * 组播搜索
//     */
//    public void searchGateway(CallBack callBack) {
////        gatewayId = "";//重置网关ID
////        this.searchCallBack = callBack;
////
////        if (searchGatewayThread != null) {
////            searchGatewayThread.shutdownNow();
////        }
////
////
////        new Thread(new Runnable() {
////            @Override
////            public void run() {
////                while (TextUtils.isEmpty(gatewayId)) {
////                    //搜索网关
////                    searchGateway(IdUtils.getUUId(), searchEvent);
////                    try {
////                        Thread.sleep(1000L);
////                    } catch (InterruptedException e) {
////                        e.printStackTrace();
////                    }
////                }
////            }
////        }).start();
//    }
//
//
//    /**
//     * 通过组播搜索网关
//     */
//    public void searchGateway(String msgId, EventListener eventListener) {
//        searchGateway(getUdpIp(), getUdpPort(), msgId, eventListener);
//    }
//
//    /**
//     * 通过广播搜索网关
//     */
//    public void searchGatewayByBroadcast(String msgId, EventListener eventListener) {
//        searchGateway(IpUtils.getBroadcastAddress(), getUdpPort(), msgId, eventListener);
//    }
//
//    /**
//     * 默认是组播搜索网关
//     */
//    public void searchGateway(String ip, int port, String msgId, EventListener eventListener) {
//
//        if (updBoot == null) {
//            updBoot = UdpClient.init(ip, port, getUdpOptions());
//            updBoot.connect();
//        }
//
//
//        String time = String.valueOf(System.currentTimeMillis());
//        JsonObject jsonObject = new JsonObject();
//        jsonObject.addProperty("id", msgId);
//        jsonObject.addProperty("time_stamp", time);
//
//        EventDispatcher.getInstance().registerIo(TopicConstant.GATEWAY_SEARCH_REPLY, eventListener);
//        LinkRequest message = new LinkRequest(TopicConstant.GATEWAY_SEARCH,
//                jsonObject.toString());
//
//        try {
//            updBoot.sendMsg(message.getSendBytes());
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
//
//    }
 
 
    /**
     * 获取设备列表
     */
    public void getDeviceList(HDLLinkCallBack callBack) {
        if (!TextUtils.isEmpty(getGatewayId()) && !TextUtils.isEmpty(getTcpIp())) {
            String time = String.valueOf(System.currentTimeMillis());
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("id", IdUtils.getUUId());
            jsonObject.addProperty("time_stamp", time);
 
            String topic = String.format(TopicConstant.GET_DEVICE_LIST, getGatewayId());
 
            LinkRequest message = new LinkRequest(topic,
                    jsonObject.toString());
 
            String replyTopic = String.format(TopicConstant.GET_DEVICE_LIST_REPLY, getGatewayId());
            try {
                sendMsg(message.getSendBytes(), replyTopic, callBack, new SendListener() {
                    @Override
                    public void onSucceed() {
 
                    }
 
                    @Override
                    public void onError() {
                        if (callBack != null) {
                            callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_GET_DEVICE_LIST_ERROR));
                        }
                    }
                });
            } catch (Exception e) {
                if (callBack != null) {
                    callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_GET_DEVICE_LIST_ERROR));
                }
            }
        } else {
            if (callBack != null) {
                callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_UNAUTHORIZED_ERROR));
            }
        }
    }
 
 
    /**
     * 获取功能列表
     */
    public void getFunctionList(HDLLinkCallBack callBack) {
        if (!TextUtils.isEmpty(getGatewayId()) && !TextUtils.isEmpty(getTcpIp())) {
            String time = String.valueOf(System.currentTimeMillis());
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("id", IdUtils.getUUId());
            jsonObject.addProperty("time_stamp", time);
 
            String topic = String.format(TopicConstant.GET_FUNCTION_LIST, getGatewayId());
 
            LinkRequest message = new LinkRequest(topic,
                    jsonObject.toString());
 
            String replyTopic = String.format(TopicConstant.GET_FUNCTION_LIST_REPLY, getGatewayId());
            try {
                sendMsg(message.getSendBytes(), replyTopic, callBack, new SendListener() {
                    @Override
                    public void onSucceed() {
 
                    }
 
                    @Override
                    public void onError() {
                        if (callBack != null) {
                            callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_GET_FUNCTION_LIST_ERROR));
                        }
                    }
                });
            } catch (Exception e) {
                if (callBack != null) {
                    callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_GET_FUNCTION_LIST_ERROR));
                }
            }
        } else {
            if (callBack != null) {
                callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_UNAUTHORIZED_ERROR));
            }
        }
    }
 
 
    /**
     * 获取功能属性
     *
     * @param sids
     * @param callBack
     */
    public void getFunctionAttribute(List<String> sids, HDLLinkCallBack callBack) {
        if (!TextUtils.isEmpty(getGatewayId()) && !TextUtils.isEmpty(getTcpIp())) {
            String time = String.valueOf(System.currentTimeMillis());
 
            final BaseLocalResponse<List<FunctionAttributeRequest>> data = new BaseLocalResponse<>();
            data.setId(IdUtils.getUUId());
            data.setTime_stamp(time);
            List<FunctionAttributeRequest> list = new ArrayList<>();
            for (String s : sids) {
                list.add(new FunctionAttributeRequest(s));
            }
            data.setObjects(list);
 
            String topic = String.format(TopicConstant.GET_FUNCTION_ATTRIBUTE, getGatewayId());
            LinkRequest message = new LinkRequest(topic,
                    GsonConvert.getGson().toJson(data));
 
            String replyTopic = String.format(TopicConstant.GET_FUNCTION_ATTRIBUTE_REPLY, getGatewayId());
            try {
                sendMsg(message.getSendBytes(), replyTopic, callBack, new SendListener() {
                    @Override
                    public void onSucceed() {
 
                    }
 
                    @Override
                    public void onError() {
                        if (callBack != null) {
                            callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_GET_FUNCTION_PROPERTIES_ERROR));
                        }
                    }
                });
            } catch (Exception e) {
                if (callBack != null) {
                    callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_GET_FUNCTION_PROPERTIES_ERROR));
                }
            }
        } else {
            if (callBack != null) {
                callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_UNAUTHORIZED_ERROR));
            }
        }
    }
 
    /**
     * 设备控制
     */
    public void propertyDown(List<DeviceControlRequest> request, HDLLinkCallBack callBack) {
 
        if (!TextUtils.isEmpty(getGatewayId()) && !TextUtils.isEmpty(getTcpIp())) {
            String time = String.valueOf(System.currentTimeMillis());
 
            final BaseLocalResponse<List<DeviceControlRequest>> data = new BaseLocalResponse<>();
            data.setId(IdUtils.getUUId());
            data.setTime_stamp(time);
            data.setObjects(request);
 
            String topic = String.format(TopicConstant.PROPERTY_DOWN, getGatewayId());
            LinkRequest message = new LinkRequest(topic,
                    GsonConvert.getGson().toJson(data));
 
            String replyTopic = String.format(TopicConstant.PROPERTY_DOWN_REPLY, getGatewayId());
            try {
                sendMsg(message.getSendBytes(), replyTopic, callBack, new SendListener() {
                    @Override
                    public void onSucceed() {
 
                    }
 
                    @Override
                    public void onError() {
                        if (callBack != null) {
                            callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_SEND_ERROR));
                        }
                    }
                });
            } catch (Exception e) {
                if (callBack != null) {
                    callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_SEND_ERROR));
                }
            }
        } else {
            if (callBack != null) {
                callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_SEND_ERROR));
            }
        }
 
    }
 
//    /**
//     * 状态上报
//     */
//    public void propertyUp(List<PropertyUpRequest> request, CallBack callBack) {
//        if (!TextUtils.isEmpty(getGatewayId()) && !TextUtils.isEmpty(getTcpIp())) {
//            String time = String.valueOf(System.currentTimeMillis());
//
//            final BaseLocalResponse<List<PropertyUpRequest>> data = new BaseLocalResponse<>();
//            data.setId(IdUtils.getUUId());
//            data.setTime_stamp(time);
//            data.setObjects(request);
//
//
//            String topic = String.format(TopicConstant.PROPERTY_UP, getGatewayId());
//            LinkRequest message = new LinkRequest(topic,
//                    GsonConvert.getGson().toJson(request));
//
//            String replyTopic = String.format(TopicConstant.PROPERTY_UP_REPLY, getGatewayId());
//            try {
//                sendMsg(message.getSendBytes(), replyTopic, callBack, new SendListener() {
//                    @Override
//                    public void onSucceed() {
//
//                    }
//
//                    @Override
//                    public void onError() {
//                        if (callBack != null) {
//                            callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_SEND_ERROR));
//                        }
//                    }
//                });
//            } catch (Exception e) {
//                if (callBack != null) {
//                    callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_SEND_ERROR));
//                }
//            }
//        } else {
//            if (callBack != null) {
//                callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_SEND_ERROR));
//            }
//        }
//    }
 
    /**
     * 读取状态
     */
    public void propertyRead(List<String> sids, HDLLinkCallBack callBack) {
        if (!TextUtils.isEmpty(getGatewayId()) && !TextUtils.isEmpty(getTcpIp())) {
            String time = String.valueOf(System.currentTimeMillis());
            final BaseLocalResponse<List<PropertyReadRequest>> data = new BaseLocalResponse<>();
            data.setId(IdUtils.getUUId());
            data.setTime_stamp(time);
 
            List<PropertyReadRequest> list = new ArrayList<>();
            for (String s : sids) {
                list.add(new PropertyReadRequest(s));
            }
            data.setObjects(list);
 
            String topic = String.format(TopicConstant.PROPERTY_READ, getGatewayId());
            LinkRequest message = new LinkRequest(topic,
                    GsonConvert.getGson().toJson(data));
 
            String replyTopic = String.format(TopicConstant.PROPERTY_READ_REPLY, getGatewayId());
            try {
                sendMsg(message.getSendBytes(), replyTopic, callBack, new SendListener() {
                    @Override
                    public void onSucceed() {
 
                    }
 
                    @Override
                    public void onError() {
                        if (callBack != null) {
                            callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_SEND_ERROR));
                        }
                    }
                });
            } catch (Exception e) {
                if (callBack != null) {
                    callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_SEND_ERROR));
                }
            }
        } else {
            if (callBack != null) {
                callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_SEND_ERROR));
            }
        }
    }
 
    public SocketBoot getTcp() throws RuntimeException {
        if (TextUtils.isEmpty(getTcpIp())) {
            throw new RuntimeException("请搜索网关");
        }
        if (tcpBoot == null) {
            tcpBoot = TcpClient.init(getTcpIp(), getTcpPort(), getTcpOptions());
        }
        return tcpBoot;
    }
 
    /**
     * 获取场景列表
     */
    public void getSceneList(HDLLinkCallBack callBack) {
        if (!TextUtils.isEmpty(getGatewayId()) && !TextUtils.isEmpty(getTcpIp())) {
            String time = String.valueOf(System.currentTimeMillis());
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("id", IdUtils.getUUId());
            jsonObject.addProperty("time_stamp", time);
 
            String topic = String.format(TopicConstant.SCENE_LIST_GET, getGatewayId());
 
            LinkRequest message = new LinkRequest(topic,
                    jsonObject.toString());
            String replyTopic = topic + "_reply";
            try {
                sendMsg(message.getSendBytes(), replyTopic, callBack, new SendListener() {
                    @Override
                    public void onSucceed() {
 
                    }
 
                    @Override
                    public void onError() {
                        if (callBack != null) {
                            callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_GET_FUNCTION_LIST_ERROR));
                        }
                    }
                });
            } catch (Exception e) {
                if (callBack != null) {
                    callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_GET_FUNCTION_LIST_ERROR));
                }
            }
        } else {
            if (callBack != null) {
                callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_UNAUTHORIZED_ERROR));
            }
        }
    }
 
    /**
     *  场景控制
     * @param sids 场景sid列表
     * @param callBack 回调
     */
    public void controlScene(List<String> sids, HDLLinkCallBack callBack) {
        if (!TextUtils.isEmpty(getGatewayId()) && !TextUtils.isEmpty(getTcpIp())) {
            String time = String.valueOf(System.currentTimeMillis());
            final BaseLocalResponse<List<PropertyReadRequest>> data = new BaseLocalResponse<>();
            data.setId(IdUtils.getUUId());
            data.setTime_stamp(time);
 
            List<PropertyReadRequest> list = new ArrayList<>();
            for (String s : sids) {
                list.add(new PropertyReadRequest(s));
            }
            data.setObjects(list);
 
            String topic = String.format(TopicConstant.SCENE_CONTROL, getGatewayId());
            LinkRequest message = new LinkRequest(topic,
                    GsonConvert.getGson().toJson(data));
 
            String replyTopic = topic + "_reply";
            try {
                sendMsg(message.getSendBytes(), replyTopic, callBack, new SendListener() {
                    @Override
                    public void onSucceed() {
 
                    }
 
                    @Override
                    public void onError() {
                        if (callBack != null) {
                            callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_SEND_ERROR));
                        }
                    }
                });
            } catch (Exception e) {
                if (callBack != null) {
                    callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_SEND_ERROR));
                }
            }
        } else {
            if (callBack != null) {
                callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_SEND_ERROR));
            }
        }
    }
 
//    /**
//     * 清空缓存
//     */
//    public void clearCache() {
//        SPUtils.remove(TCP_IP_KEY);
//        SPUtils.remove(GATEWAY_KEY);
//    }
 
    /**
     * 通用TCP发送指令
     * 1秒没响应就让他重新发送,重试3次
     *
     * @param topic    发送数据
     * @param bodyStr  回复的主题
     * @param callBack 回调
     */
    public void tcpSendMsg(String topic, String bodyStr, HDLLinkCallBack callBack) {
        try {
            LinkRequest message = new LinkRequest(topic, bodyStr);
            String replyTopic = topic + "_reply";
            sendMsg(message.getSendBytes(), replyTopic, callBack, new SendListener() {
                @Override
                public void onSucceed() {
 
                }
 
                @Override
                public void onError() {
                    if (callBack != null) {
                        callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_SEND_ERROR));
                    }
                }
            });
        } catch (Exception e) {
            if (callBack != null) {
                callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_SEND_ERROR));
            }
        }
    }
 
    /**
     * 通用TCP发送指令 只发一次,不监听回复,不重发
     *
     * @param topic    发送数据
     * @param bodyStr  回复的主题
     */
    public void tcpSendMsg(String topic, String bodyStr) {
        try {
            if (TextUtils.isEmpty(topic) || TextUtils.isEmpty(bodyStr)) {
                LogUtils.e("udpSendMsg", "参数不能为空");
                return;
            }
            LinkRequest message = new LinkRequest(topic, bodyStr);
            getTcp().sendMsg(message.getSendBytes());
        } catch (Exception e) {
            LogUtils.e("tcpSendMsg", "发送失败 :"+e.getMessage());
        }
    }
 
    /**
     * 发送指令
     * 1秒没响应就让他重新发送,重试3次
     */
    public void sendMsg(byte[] data, String eventTag, HDLLinkCallBack callBack, SendListener sendListener) {
 
        try {
            final AtomicInteger sendCount = new AtomicInteger(0);
 
            final ScheduledExecutorService threadPool = ThreadToolUtils.getInstance().newScheduledThreadPool(1);
            final EventListener eventListener = new EventListener() {
                @Override
                public void onMessage(Object msg) {
                    if (msg instanceof LinkResponse) {
                        LogUtils.e("sendMsg onSuccess");
                        if (callBack != null) {
                            callBack.onSuccess(msg.toString());
                        }
                        threadPool.shutdownNow();
                    }
                    LogUtils.e("sendMsg eventListener remove");
                    EventDispatcher.getInstance().remove(eventTag, this);
                }
            };
 
            threadPool.scheduleWithFixedDelay(new Runnable() {
                @Override
                public void run() {
                    if (sendCount.get() < 3) {
                        sendCount.set(sendCount.get() + 1);
                        getTcp().sendMsg(data);
                    } else {
                        threadPool.shutdownNow();
                        LogUtils.e("sendMsg eventListener remove");
                        EventDispatcher.getInstance().remove(eventTag, eventListener);
                        ThreadToolUtils.getInstance().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (callBack != null) {
                                    callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_SEND_ERROR));
                                }
                            }
                        });
                    }
                }
            }, 1000, 500, TimeUnit.MILLISECONDS);
            EventDispatcher.getInstance().register(eventTag, eventListener);
            getTcp().sendMsg(data, new SendListener() {
                @Override
                public void onSucceed() {
                    if (sendListener != null) {
                        sendListener.onSucceed();
                    }
                }
 
                @Override
                public void onError() {
                    if (sendListener != null) {
                        sendListener.onError();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            ThreadToolUtils.getInstance().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (callBack != null) {
                        callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_SEND_ERROR));
                    }
                }
            });
        }
 
    }
 
}