xm
2020-07-20 b02e8275a21dc06bf54b66273485d44e007a2616
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
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
 
namespace Shared.Phone.UserCenter
{
    /// <summary>
    /// 设备绑定的逻辑
    /// </summary>
    public class HdlDeviceBindLogic
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 设备绑定的逻辑
        /// </summary>
        private static HdlDeviceBindLogic m_Current = null;
        /// <summary>
        /// 设备绑定的逻辑
        /// </summary>
        public static HdlDeviceBindLogic Current
        {
            get
            {
                if (m_Current == null)
                {
                    m_Current = new HdlDeviceBindLogic();
                }
                return m_Current;
            }
        }
 
        #endregion
 
        #region ■ 获取设备下面绑定的设备_____________
 
        /// <summary>
        /// 获取设备下面绑定的设备(错误时返回null),这个是给pir传感器用的
        /// </summary>
        /// <param name="mainDevice">设备对象</param>
        /// <returns></returns>
        public List<CommonDevice> GetBindTargetDevice(CommonDevice mainDevice)
        {
            var result = this.GetDeviceBindAsync(mainDevice);
            if (result == null || result.getAllBindResponseData == null)
            {
                //获取设备的绑定目标失败
                string msg = Language.StringByID(R.MyInternationalizationString.uGetDeviceBindTargetFail);
                //拼接上【网关回复超时】的Msg
                msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result);
 
                this.ShowErrorMsg(msg);
                return null;
            }
            var listDevice = new List<CommonDevice>();
            foreach (var data in result.getAllBindResponseData.BindList)
            {
                CommonDevice deviceTemp = Common.LocalDevice.Current.GetDevice(data.BindMacAddr, data.BindEpoint);
                if (deviceTemp == null)
                {
                    continue;
                }
                listDevice.Add(deviceTemp);
            }
            return listDevice;
        }
 
        /// <summary>
        ///获取所有绑定
        /// </summary>
        public BindObj.GetDeviceBindResponseAllData GetDeviceBindAsync(CommonDevice device)
        {
            //如果当前住宅是虚拟住宅
            if (Common.Config.Instance.Home.IsVirtually == true)
            {
                var listBind = ModelData.DeviceModelDataLogic.Current.GetDeviceBindList(device, "Bind/GetDeviceBind");
                return new BindObj.GetDeviceBindResponseAllData()
                {
                    getAllBindResponseData = new BindObj.GetDeviceBindResponseData
                    {
                        Result = 0,
                        BindList = listBind
                    }
                };
            }
 
            var jObject = new JObject { { "DeviceAddr", device.DeviceAddr }, { "Epoint", device.DeviceEpoint }, { "Cluster_ID", 0 }, { "Command", 5007 } };
            string sendData = jObject.ToString();
            //发送
            var result = HdlDeviceCommonLogic.Current.SendJobjectDataToGateway(device, "Bind/GetDeviceBind", sendData, "Bind/GetDeviceBind_Respon", 6);
            if (result.ErrorMsg != null || result.ErrorMsgDiv == 0)
            {
                return null;
            }
            var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<BindObj.GetDeviceBindResponseData>(result.ReceiptData);
            return new BindObj.GetDeviceBindResponseAllData() { getAllBindResponseData = tempData };
        }
 
        #endregion
 
        #region ■ 执行绑定设备目标___________________
 
        /// <summary>
        /// 绑定设备的目标(返回成功设置的设备,错误时,返回null)
        /// </summary>
        /// <param name="mainDevice">设备对象</param>
        /// <param name="listDevice">要绑定的目标设备</param>
        /// <param name="BindCluster">BindCluster</param>
        /// <returns></returns>
        public List<CommonDevice> BindDeviceTarget(CommonDevice mainDevice, List<CommonDevice> listDevice, int BindCluster = 6)
        {
            if (listDevice.Count == 0)
            {
                return new List<CommonDevice>();
            }
 
            var dicDevice = new Dictionary<string, CommonDevice>();
 
            //组装数据
            var addData = new IASZone.AddBindData();
            addData.DeviceAddr = mainDevice.DeviceAddr;
            addData.Epoint = mainDevice.DeviceEpoint;
            foreach (var device in listDevice)
            {
                var info = new IASZone.AddBindListObj();
                info.BindCluster = BindCluster;
                info.BindMacAddr = device.DeviceAddr;
                info.BindEpoint = device.DeviceEpoint;
                info.BindType = 0;
 
                addData.BindList.Add(info);
 
                //返回成功设备的时候使用
                string mainkeys = Common.LocalDevice.Current.GetDeviceMainKeys(device);
                dicDevice[mainkeys] = device;
            }
            //执行绑定
            var result = this.AddDeviceBindAsync(addData);
            if (result == null || result.addedDeviceBindResponseData == null)
            {
                //绑定目标设置失败
                string msg = Language.StringByID(R.MyInternationalizationString.uSetBindTargetsFail);
                //拼接上【网关回复超时】的Msg
                msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result);
 
                this.ShowErrorMsg(msg);
                return null;
            }
 
            var listSuccess = new List<CommonDevice>();
            foreach (var data in result.addedDeviceBindResponseData.BindList)
            {
                string mainkeys = Common.LocalDevice.Current.GetDeviceMainKeys(data.BindMacAddr, data.BindEpoint);
                if (dicDevice.ContainsKey(mainkeys) == true)
                {
                    listSuccess.Add(dicDevice[mainkeys]);
                }
            }
 
            if (listSuccess.Count == 0)
            {
                //绑定目标设置失败
                string msg = Language.StringByID(R.MyInternationalizationString.uSetBindTargetsFail);
                this.ShowTipMsg(msg);
            }
            return listSuccess;
        }
 
        /// <summary>
        ///设备绑定
        /// </summary>
        public BindObj.AddedDeviceBindResponseAllData AddDeviceBindAsync(BindObj.AddBindData addBindData)
        {
            //如果当前住宅是虚拟住宅
            if (Common.Config.Instance.Home.IsVirtually == true)
            {
                return ModelData.DeviceModelDataLogic.Current.AddDeviceBindList(addBindData, "Bind/GetDeviceBind");
            }
 
            var device = Common.LocalDevice.Current.GetDevice(addBindData.DeviceAddr, addBindData.Epoint);
            //获取发送的命令字符
            var sendData = this.GetAddDeviceListBindCommandText(addBindData);
            //发送
            var result = HdlDeviceCommonLogic.Current.SendJobjectDataToGateway(device, "Bind/SetBind", sendData, "Bind/SetBind_Respon", 20);
            if (result.ErrorMsg != null || result.ErrorMsgDiv == 0)
            {
                return null;
            }
 
            //修改缓存
            ModelData.DeviceModelDataLogic.Current.AddDeviceBindList(addBindData, "Bind/GetDeviceBind");
 
            var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<BindObj.AddedDeviceBindResponseData>(result.ReceiptData);
            return new BindObj.AddedDeviceBindResponseAllData() { addedDeviceBindResponseData = tempData };
        }
 
        /// <summary>
        /// 获取添加设备绑定的命令字符
        /// </summary>
        /// <param name="addBindData"></param>
        /// <returns></returns>
        public string GetAddDeviceListBindCommandText(BindObj.AddBindData addBindData)
        {
            var jObject = new JObject { { "DeviceAddr", addBindData.DeviceAddr }, { "Epoint", addBindData.Epoint }, { "Cluster_ID", 0 }, { "Command", 5001 } };
            var bindList = new JArray { };
            foreach (var bindInfo in addBindData.BindList)
            {
                if (bindInfo.BindType == 0)
                {
                    var dInfo = new JObject{{ "BindMacAddr",bindInfo.BindMacAddr},{ "BindEpoint", bindInfo.BindEpoint} ,
                            { "BindCluster", bindInfo.BindCluster} ,{ "BindType",bindInfo.BindType}};
                    bindList.Add(dInfo);
                }
                else if (bindInfo.BindType == 1)
                {
                    var dInfo = new JObject{{ "BindCluster", bindInfo.BindCluster} ,{ "BindType",bindInfo.BindType},
                            { "BindScenesId", bindInfo.BindScenesId}};
                    bindList.Add(dInfo);
                }
            }
            var data = new JObject { { "BindName", addBindData.BindName }, { "BindList", bindList } };
            jObject.Add("Data", data);
            return jObject.ToString();
        }
 
        #endregion
 
        #region ■ 删除设备绑定的目标_________________
 
        /// <summary>
        /// 删除设备绑定的目标
        /// </summary>
        /// <param name="mainDevice">设备对象</param>
        /// <param name="deleteDevice">要删除的绑定目标设备</param>
        /// <param name="BindCluster">BindCluster</param>
        /// <returns></returns>
        public bool DeleteDeviceTarget(CommonDevice mainDevice, CommonDevice deleteDevice, int BindCluster = 6)
        {
            //组装数据
            var deleteData = new IASZone.DelDeviceBindData();
            deleteData.DeviceAddr = mainDevice.DeviceAddr;
            deleteData.Epoint = mainDevice.DeviceEpoint;
 
            var info = new IASZone.RemoveBindListObj();
            info.BindCluster = BindCluster;
            info.BindMacAddr = deleteDevice.DeviceAddr;
            info.BindEpoint = deleteDevice.DeviceEpoint;
            info.BindType = 0;
 
            deleteData.RemoveBindList.Add(info);
 
            //执行删除
            var result = this.DelDeviceBindAsync(deleteData);
            if (result == null || result.delDeviceBindResponseData == null)
            {
                //删除绑定目标失败
                string msg = Language.StringByID(R.MyInternationalizationString.uDeleteBindTargetsFail);
                //拼接上【网关回复超时】的Msg
                msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result);
 
                this.ShowErrorMsg(msg);
                return false;
            }
 
            foreach (var data in result.delDeviceBindResponseData.RemoveBindList)
            {
                //0:成功  1:设备不在绑定列表中 ,也可以代表成功
                if (data.Result == 0 || data.Result == 1 || data.Result == 5)
                {
                    return true;
                }
                //3:失败,在等待节点设备确认是否解除绑定成功
                else if (data.Result == 3)
                {
                    //其他绑定目标正在删除中,请稍后再试
                    string msg = Language.StringByID(R.MyInternationalizationString.uOtherBindTargetsIsDelettingPleaseWait);
                    this.ShowErrorMsg(msg);
                    return false;
                }
                //2:未知,由节点设备反馈发送“Bind/BindResult”主题消息确定是否成功
                else if (data.Result == 2)
                {
                    if (result.removeBindResultResponseData == null)
                    {
                        //删除绑定目标失败
                        string msg = Language.StringByID(R.MyInternationalizationString.uDeleteBindTargetsFail);
                        this.ShowErrorMsg(msg);
                        return false;
                    }
                    else
                    {
                        //成功
                        if (result.removeBindResultResponseData.Result == 0)
                        {
                            return true;
                        }
                        //136:控制设备本地绑定列表中无此绑定
                        else if (result.removeBindResultResponseData.Result == 136)
                        {
                            //这个可以当做成功
                            return true;
                        }
                        else
                        {
                            //删除绑定目标失败
                            string msg = Language.StringByID(R.MyInternationalizationString.uDeleteBindTargetsFail);
                            this.ShowErrorMsg(msg);
                            return false;
                        }
                    }
                }
            }
            return false;
        }
 
        /// <summary>
        /// 删除设备绑定的目标
        /// </summary>
        /// <param name="mainDevice">设备对象</param>
        /// <param name="listDeleteDevice">要删除的绑定目标设备</param>
        /// <param name="BindCluster">BindCluster</param>
        /// <returns>返回的是成功删除的设备</returns>
        public List<CommonDevice> DeleteDeviceTarget(CommonDevice mainDevice, List<CommonDevice> listDeleteDevice, int BindCluster = 6)
        {
            //组装数据
            var deleteData = new IASZone.DelDeviceBindData();
            deleteData.DeviceAddr = mainDevice.DeviceAddr;
            deleteData.Epoint = mainDevice.DeviceEpoint;
 
            var dicDevice = new Dictionary<string, CommonDevice>();
            foreach (var device in listDeleteDevice)
            {
                var info = new IASZone.RemoveBindListObj();
                info.BindCluster = BindCluster;
                info.BindMacAddr = device.DeviceAddr;
                info.BindEpoint = device.DeviceEpoint;
                info.BindType = 0;
 
                deleteData.RemoveBindList.Add(info);
                //返回成功设备的时候使用
                string mainkeys = Common.LocalDevice.Current.GetDeviceMainKeys(device);
                dicDevice[mainkeys] = device;
            }
            var listSuccess = new List<CommonDevice>();
 
            var result = this.DelDeviceBindAsync(deleteData);
            if (result == null || result.delDeviceBindResponseData == null)
            {
                //删除绑定目标失败
                string msg = Language.StringByID(R.MyInternationalizationString.uDeleteBindTargetsFail);
                //拼接上【网关回复超时】的Msg
                msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result);
 
                this.ShowErrorMsg(msg);
                return listSuccess;
            }
 
            foreach (var data in result.delDeviceBindResponseData.RemoveBindList)
            {
                string mainkeys = Common.LocalDevice.Current.GetDeviceMainKeys(data.BindMacAddr, data.BindEpoint);
                //0:成功  1:设备不在绑定列表中 ,也可以代表成功
                if (data.Result == 0 || data.Result == 1)
                {
                    listSuccess.Add(dicDevice[mainkeys]);
                }
                //3:失败,在等待节点设备确认是否解除绑定成功
                else if (data.Result == 3)
                {
                    //设备名称 其他绑定目标正在删除中,请稍后再试
                    string msg = Common.LocalDevice.Current.GetDeviceEpointName(dicDevice[mainkeys]) + "\r\n";
                    msg += Language.StringByID(R.MyInternationalizationString.uOtherBindTargetsIsDelettingPleaseWait);
                    this.ShowErrorMsg(msg);
                }
                //2:未知,由节点设备反馈发送“Bind/BindResult”主题消息确定是否成功
                else if (data.Result == 2)
                {
                    if (result.removeBindResultResponseData == null)
                    {
                        //设备名称 删除绑定目标失败
                        string msg = Common.LocalDevice.Current.GetDeviceEpointName(dicDevice[mainkeys]) + "\r\n";
                        msg += Language.StringByID(R.MyInternationalizationString.uDeleteBindTargetsFail);
                        this.ShowErrorMsg(msg);
                    }
                    else
                    {
                        //成功
                        if (result.removeBindResultResponseData.Result == 0)
                        {
                            listSuccess.Add(dicDevice[mainkeys]);
                        }
                        //136:控制设备本地绑定列表中无此绑定
                        else if (result.removeBindResultResponseData.Result == 136)
                        {
                            //这个可以当做成功
                            listSuccess.Add(dicDevice[mainkeys]);
                        }
                        else
                        {
                            //设备名称 删除绑定目标失败
                            string msg = Common.LocalDevice.Current.GetDeviceEpointName(dicDevice[mainkeys]) + "\r\n";
                            msg += Language.StringByID(R.MyInternationalizationString.uDeleteBindTargetsFail);
                            this.ShowErrorMsg(msg);
                        }
                    }
                }
            }
            return listSuccess;
        }
 
        /// <summary>
        /// 删除设备的绑定目标
        /// </summary>
        /// <param name="delDeviceBindData"></param>
        /// <returns></returns>
        public BindObj.DelDeviceBindResponseAllData DelDeviceBindAsync(BindObj.DelDeviceBindData delDeviceBindData)
        {
            //如果当前住宅是虚拟住宅
            if (Common.Config.Instance.Home.IsVirtually == true)
            {
                return ModelData.DeviceModelDataLogic.Current.DeleteDeviceBindList(delDeviceBindData);
            }
            var device = Common.LocalDevice.Current.GetDevice(delDeviceBindData.DeviceAddr, delDeviceBindData.Epoint);
            //获取需要发送的数据
            var sendData = this.GetDeleteDeviceBindCommandText(delDeviceBindData);
            //发送命令
            var result = this.SendDeleteBindJobjectDataToGateway(device, " Bind/RemoveBind",
                sendData, new List<string>() { "Bind/RemoveBind_Respon", "Bind/RemoveBindResult" }, 6);
 
            if (result.ErrorMsg != null || result.ErrorMsgDiv == 0)
            {
                return null;
            }
            var responseData = new BindObj.DelDeviceBindResponseAllData();
            if (result.listReceiptData[0] != null)
            {
                var tempData1 = Newtonsoft.Json.JsonConvert.DeserializeObject<BindObj.DelDeviceBindResponseData>(result.listReceiptData[0]);
                responseData.delDeviceBindResponseData = tempData1;
            }
 
            if (result.listReceiptData[1] != null)
            {
                var tempData2 = Newtonsoft.Json.JsonConvert.DeserializeObject<BindObj.RemoveBindResultResponseData>(result.listReceiptData[1]);
                responseData.removeBindResultResponseData = tempData2;
            }
 
            //修改缓存
            ModelData.DeviceModelDataLogic.Current.DeleteDeviceBindList(delDeviceBindData);
 
            return responseData;
        }
 
        /// <summary>
        ///删除一个控制设备所有绑定信息
        /// </summary>
        public BindObj.ClearBindInfoResponseAllData ClearBindInfoAsync(CommonDevice device)
        {
            //如果当前住宅是虚拟住宅
            if (Common.Config.Instance.Home.IsVirtually == true)
            {
                return ModelData.DeviceModelDataLogic.Current.ClearDeviceAllBind(device);
            }
 
            var jObject = new JObject { { "DeviceAddr", device.DeviceAddr }, { "Epoint", device.DeviceEpoint }, { "Cluster_ID", 0 }, { "Command", 5006 } };
            var sendData = jObject.ToString();
            var result = HdlDeviceCommonLogic.Current.SendJobjectDataToGateway(device, "Bind/ClearBindInfo", sendData, "Bind/ClearBindInfo_Respon", 25);
            if (result.ErrorMsg != null || result.ErrorMsgDiv == 0)
            {
                return null;
            }
 
            //修改缓存
            ModelData.DeviceModelDataLogic.Current.ClearDeviceAllBind(device);
 
            var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<BindObj.ClearBindInfoResponseData>(result.ReceiptData);
            return new BindObj.ClearBindInfoResponseAllData { clearBindInfoResponseData = tempData };
        }
 
 
        /// <summary>
        /// 获取删除设备绑定列表的命令字符
        /// </summary>
        /// <param name="delDeviceBindData"></param>
        /// <returns></returns>
        public string GetDeleteDeviceBindCommandText(BindObj.DelDeviceBindData delDeviceBindData)
        {
            var jObject = new JObject { { "DeviceAddr", delDeviceBindData.DeviceAddr }, { "Epoint", delDeviceBindData.Epoint }, { "Cluster_ID", 0 }, { "Command", 5003 } };
            var removeBindList = new JArray { };
            foreach (var removeBindInfo in delDeviceBindData.RemoveBindList)
            {
                //删除设备
                if (removeBindInfo.BindType == 0)
                {
                    var dInfo = new JObject{{ "BindMacAddr",removeBindInfo.BindMacAddr},{ "BindEpoint", removeBindInfo.BindEpoint} ,
                         { "BindCluster", removeBindInfo.BindCluster} ,{ "BindType",removeBindInfo.BindType}};
                    removeBindList.Add(dInfo);
                }
                //删除场景
                else if (removeBindInfo.BindType == 1)
                {
                    var dInfo = new JObject{{ "BindCluster", removeBindInfo.BindCluster} ,{ "BindType",removeBindInfo.BindType},
                         { "BindScenesId", removeBindInfo.BindScenesId}};
                    removeBindList.Add(dInfo);
                }
            }
            var data = new JObject { { "RemoveBindList", removeBindList } };
            jObject.Add("Data", data);
 
            return jObject.ToString();
        }
 
        /// <summary>
        /// 发送删除绑定的数据到网关,并接受网关返回的数据,listReceiptData为返回值(删除绑定的接口很特殊,需要独立出来)
        /// </summary>
        /// <param name="device">设备对象</param>
        /// <param name="sendTopic">发送的主题</param>
        /// <param name="sendData">需要发送的数据  JObject.ToString()的东西</param>
        /// <param name="listReceiptTopic">指定接收哪些主题</param>
        /// <param name="waitTime">超时时间(秒)</param>
        /// <returns>网关返回的数据</returns>
        private ReceiptGatewayResult SendDeleteBindJobjectDataToGateway(CommonDevice device, string sendTopic, string sendData, List<string> listReceiptTopic, int waitTime = 3)
        {
            var reResult = new ReceiptGatewayResult();
            reResult.listReceiptData = new string[listReceiptTopic.Count];
            var myGateway = device.Gateway;
            if (myGateway == null)
            {
                //获取网关对象失败
                reResult.ErrorMsg = Language.StringByID(R.MyInternationalizationString.uGetGatewayTagartFail);
                reResult.ErrorMsgDiv = -1;
                return reResult;
            }
            //网关ID
            string gatewayID = device.Gateway.GwId;
            //错误主题
            string errorTopic = gatewayID + "/" + "Error_Respon";
            //检测对象的主题
            for (int i = 0; i < listReceiptTopic.Count; i++)
            {
                listReceiptTopic[i] = gatewayID + "/" + listReceiptTopic[i];
            }
 
            bool canbreak = false;
            BindObj.DelDeviceBindResponseData delDeviceBindData = null;
            BindObj.RemoveBindResultResponseData removeBindResult = null;
 
            Action<string, string> receiptAction = (topic, message) =>
            {
                var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
 
                //网关回复错误
                if (topic == errorTopic)
                {
                    var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonDevice.ErrorResponData>(jobject["Data"].ToString());
                    reResult.ErrorMsg = HdlCheckLogic.Current.CheckCommonErrorCode(temp.Error);
                }
                //如果是指定的主题
                for (int i = 0; i < listReceiptTopic.Count; i++)
                {
                    if (topic == listReceiptTopic[i])
                    {
                        string deviceMac = jobject["DeviceAddr"].ToString();
                        int deviceEpoint = Convert.ToInt32(jobject["Epoint"].ToString());
                        if (device.DeviceAddr != deviceMac)
                        {
                            //不是同一个东西Mac
                            return;
                        }
                        if (device.DeviceEpoint != deviceEpoint && deviceEpoint != 200)
                        {
                            //不是同一个东西,这里应该需要特殊处理200端点
                            return;
                        }
                        reResult.listReceiptData[i] = jobject["Data"].ToString();
 
                        if (i == 0)
                        {
                            //"Bind/RemoveBind_Respon"主题
                            delDeviceBindData = Newtonsoft.Json.JsonConvert.DeserializeObject<BindObj.DelDeviceBindResponseData>(reResult.listReceiptData[i]);
                        }
                        else if (i == 1)
                        {
                            //"Bind/RemoveBindResult"主题
                            removeBindResult = Newtonsoft.Json.JsonConvert.DeserializeObject<BindObj.RemoveBindResultResponseData>(reResult.listReceiptData[i]);
                        }
 
                        if (0 < delDeviceBindData.RemoveBindList.FindAll((obj) => obj.BindType == 0 && obj.Result == 1).Count)
                        {
                            canbreak = true;
                        }
                        else if (0 < delDeviceBindData.RemoveBindList.FindAll((obj) => obj.BindType == 0 && obj.Result == 3).Count)
                        {
                            if (removeBindResult != null)
                            {
                                canbreak = true;
                            }
                        }
                        else if (0 < delDeviceBindData.RemoveBindList.FindAll((obj) => obj.BindType == 0 && obj.Result == 2).Count)
                        {
                            if (removeBindResult != null)
                            {
                                canbreak = true;
                            }
                        }
                        else
                        {
                            canbreak = true;
                        }
                    }
                }
 
            };
            myGateway.Actions += receiptAction;
            //发送数据
            myGateway.Send(sendTopic, sendData);
 
            //超时时间
            int TimeOut = 0;
            waitTime = 20 * waitTime;
            while (canbreak == false && TimeOut < waitTime)
            {
                //全部接收才退出
                System.Threading.Thread.Sleep(50);
                TimeOut++;
            }
            myGateway.Actions -= receiptAction;
            receiptAction = null;
            if (canbreak == false)
            {
                //超时
                reResult.ErrorMsgDiv = 0;
            }
            else
            {
                //正常接收到网关返回的数据
                if (reResult.listReceiptData.Length == 1)
                {
                    //如果只有一个主题,则替换变量
                    reResult.ReceiptData = reResult.listReceiptData[0];
                    reResult.listReceiptData = null;
                }
            }
 
            return reResult;
        }
 
 
        #endregion
 
        #region ■ 配置面板功能_______________________
 
        ///<summary >
        ///配置面板功能
        ///<para>value:给面板配置的模式</para>
        /// </summary>
        public CommonDevice.SetWritableValueResponAllData ConfigureHdlKeyValueAsync(CommonDevice device, Panel.KeyMode value, int clusterID = 6)
        {
            //如果当前住宅是虚拟住宅
            if (Common.Config.Instance.Home.IsVirtually == true)
            {
                return ModelData.DeviceModelDataLogic.Current.ConfigurePanelKeyModel(device, value, clusterID, "SetWritableValue");
            }
            //获取发送数据
            var sendData = this.GetConfigureHdlKeyCommandText(device.DeviceAddr, device.DeviceEpoint, value, clusterID);
            var result = HdlDeviceCommonLogic.Current.SendJobjectDataToGateway(device, "SetWritableValue", sendData, "SetWritableValue_Respon");
            if (result.ErrorMsg != null || result.ErrorMsgDiv == 0)
            {
                return null;
            }
 
            //修改缓存
            ModelData.DeviceModelDataLogic.Current.ConfigurePanelKeyModel(device, value, clusterID, "SetWritableValue");
 
            var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonDevice.SetWritableValueResponData>(result.ReceiptData);
            return new CommonDevice.SetWritableValueResponAllData { setWritableValueResponData = tempData };
        }
 
        /// <summary>
        /// 读取面板的配置信息
        /// </summary>
        public Panel.PanelConfigureInfoResponAllData ReadPanelConfigureInfoAsync(CommonDevice device)
        {
            //如果当前住宅是虚拟住宅
            if (Common.Config.Instance.Home.IsVirtually == true)
            {
                return ModelData.DeviceModelDataLogic.Current.ReadPanelConfigureKeyModel(device);
            }
 
            var JObject = new JObject {{ "DeviceAddr",device.DeviceAddr },{ "Epoint", device.DeviceEpoint },
                { "Cluster_ID", (int)Cluster_ID.HdlKey },{ "Command", 108 }};
            var attriBute = new JArray { new JObject { { "AttriButeId", (int)AttriButeId.HdlKey } } };
            var data = new JObject { { "AttriBute", attriBute } };
            JObject.Add("Data", data);
 
            //发送数据
            var sendData = JObject.ToString();
            string receiptTopic = "DeviceStatusReport/" + device.DeviceAddr + "/" + device.DeviceEpoint + "/" + (int)Cluster_ID.HdlKey + "/" + (int)AttriButeId.HdlKey;
            var result = HdlDeviceCommonLogic.Current.SendJobjectDataToGateway(device, "GetDeviceStatus", sendData, receiptTopic);
            if (result.ErrorMsg != null || result.ErrorMsgDiv == 0)
            {
                return null;
            }
 
            var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<Panel.DeviceStatusReportData>(result.ReceiptData);
            return new Panel.PanelConfigureInfoResponAllData { deviceStatusReportData = tempData };
        }
 
        /// <summary>
        /// 获取配置面板功能的命令字符
        /// </summary>
        /// <param name="device">设备对象</param>
        /// <param name="value">键值</param>
        /// <param name="clusterID">镞ID</param>
        /// <returns></returns>
        public string GetConfigureHdlKeyCommandText(string DeviceAddr, int DeviceEpoint, Panel.KeyMode value, int clusterID = 6)
        {
            var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", clusterID }, { "Command", 120 } };
            var data = new JObject { { "Undivided", 0 }, { "AttributeId", 6533 }, { "AttributeDataType", 33 }, { "AttributeData", (int)value } };
            jObject.Add("Data", data);
            return jObject.ToString();
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 显示错误信息窗口
        /// </summary>
        /// <param name="msg"></param>
        private void ShowErrorMsg(string msg)
        {
            Application.RunOnMainThread(() =>
            {
                var contr = new ShowMsgControl(ShowMsgType.Error, msg);
                contr.Show();
            });
        }
 
        /// <summary>
        /// 显示Tip信息窗口
        /// </summary>
        /// <param name="msg"></param>
        private void ShowTipMsg(string msg)
        {
            Application.RunOnMainThread(() =>
            {
                var contr = new UserCenter.ShowMsgControl(ShowMsgType.Tip, msg);
                contr.Show();
            });
        }
 
        #endregion
    }
}