黄学彪
2020-12-17 9f326f4000847e6167d8166fa2f6a66f53cb3734
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
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
namespace ZigBee.Device
{
    [System.Serializable]
    public class AC : CommonDevice
    {
        public AC()
        {
            this.Type = DeviceType.Thermostat;
        }
 
        /// <summary>
        ///当前室内温度
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public int currentLocalTemperature = 0;
        /// <summary>
        /// 当前加热度数
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public int currentHeatingSetpoint = 0;
        /// <summary>
        /// 当前制冷度数
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public int currentCoolingSetpoint = 0;
        /// <summary>
        /// 当前自动温度
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public int currentAutoSetpoint;
        /// <summary>
        /// 恒温设备当前模式
        /// <para>恒温设备具备功能,SystemMode Attribute Values如下</para>
        /// <para>0:Off </para>
        /// <para>1:Auto </para>
        /// <para>3:Cool </para>
        /// <para>4:Heat </para>
        /// <para>5:Emergency heating </para>
        /// <para>6:Precooling</para>
        /// <para>7:Fan only </para>
        /// <para>8:Dry </para>
        /// <para>9:Sleep</para>
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public int currentSystemMode = 0;
        /// <summary>
        /// 风扇当前模式
        /// <para>0:Off </para>
        /// <para>1:Low </para>
        /// <para>2:Medium</para>
        /// <para>3:High </para>
        /// <para>4:On </para>
        /// <para>5:Auto </para>
        /// <para>6:Smart</para>
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public int currentFanMode = 0;
        /// <summary>
        /// 风扇zz扫风当前模式
        /// <para>0:1挡 </para>
        /// <para>1:2</para>
        /// <para>2:3</para>
        /// <para>3:4 </para>
        /// <para>4:5 </para>
        /// <para>7:Auto </para>
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public int currentFanSwingMode = 0;
        /// <summary>
        /// <para>自定义空调支持的模式(默认全开)</para>
        /// <para>数组索引 -> 0:制冷 1:制热 2:送风 3:除湿 4:自动</para>
        /// <para>值 -> 1:代表使用  0:代表不使用</para>
        /// </summary>
        public int[] listSupportMode = new int[5] { 1, 1, 1, 1, 1 };
        /// <summary>
        /// 是否启用摆风功能 false:不使用摆风功能 true:使用摆风功能
        /// </summary>
        public bool UseSwingFunction = true;
        /// <summary>
        /// 空调模块版本
        /// </summary>
        public string AcModelVersion = string.Empty;
        /// <summary>
        /// 过滤网清洗状态
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public bool CleanStatu = false;
 
        /// <summary>
        /// 获取当前室内温度.
        /// <para>AttriButeId=0 (先调用发现属性接口,查看设备是否支持该接口的属性)</para>
        /// </summary>
        public void ReadLocalTemperature()
        {
            ReadAttri(Device.Cluster_ID.Thermostat, AttriButeId.LocalTemperature);
        }
 
        /// <summary>
        /// 获取当前制冷度数.
        /// <para>AttriButeId=17 (先调用发现属性接口,查看设备是否支持该接口的属性)</para>
        /// </summary>
        public void ReadCoolingSetpoint()
        {
            ReadAttri(Device.Cluster_ID.Thermostat, AttriButeId.CoolingSetpoint);
        }
 
        /// <summary>
        /// 获取当前加热度数.
        /// <para>AttriButeId=18 (先调用发现属性接口,查看设备是否支持该接口的属性)</para>
        /// </summary>
        public void ReadHeatingSetpoint()
        {
            ReadAttri(Device.Cluster_ID.Thermostat, AttriButeId.HeatingSetpoint);
        }
 
 
        /// <summary>
        /// 获取当前自动度数.
        /// <para>AttriButeId=18 (先调用发现属性接口,查看设备是否支持该接口的属性)</para>
        /// </summary>
        public void ReadAutoSetpoint()
        {
            ReadAttri(Device.Cluster_ID.Thermostat, AttriButeId.AutoSetpoint);
        }
 
        /// <summary>
        /// 获取恒温设备类型(******测试设备返回的AttriBute属性是空)
        /// <para>AttriButeId=27 (先调用发现属性接口,查看设备是否支持该接口的属性)</para>
        /// </summary>
        public void ReadControlSequenceOfOperation()
        {
            ReadAttri(Device.Cluster_ID.Thermostat, AttriButeId.ControlSequenceOfOperation);
        }
 
        /// <summary>
        /// 获取恒温设备当前工作模式
        /// <para>AttriButeId=28 (先调用发现属性接口,查看设备是否支持该接口的属性)</para>
        /// </summary>
        public void ReadSystemMode()
        {
            ReadAttri(Device.Cluster_ID.Thermostat, AttriButeId.SystemMode);
        }
 
        /// <summary>
        /// 获取风扇模式。
        /// <para>AttriButeId=0 (先调用发现属性接口,查看设备是否支持该接口的属性)</para>
        /// </summary>
        public void ReadFanMode()
        {
            ReadAttri(Device.Cluster_ID.FanControl, AttriButeId.FanMode);
        }
 
        /// <summary>
        /// 获取风扇拥有的工作模式种类.
        /// <para>AttriButeId=1 (先调用发现属性接口,查看设备是否支持该接口的属性)</para>
        /// </summary>
        public void ReadFanModeSequence()
        {
            ReadAttri(Device.Cluster_ID.FanControl, AttriButeId.FanModeSequence);
        }
 
        /// <summary>
        /// 获取空调自定义设置的支持模式
        /// </summary>
        public void ReadModeSupport()
        {
            ReadAttri(Device.Cluster_ID.Thermostat, AttriButeId.AcModeSupport);
        }
 
        /// <summary>
        /// 过滤网清洗状态
        /// </summary>
        public void ReadCleanStatu()
        {
            ReadAttri(Device.Cluster_ID.Thermostat, AttriButeId.CleanStatu);
        }
 
        /// <summary>
        /// 获取是否启用空调摆风模式的状态(打开 或者 关闭)
        /// </summary>
        public void ReadUseSwingFunctionStatu()
        {
            ReadAttri(Device.Cluster_ID.FanControl, AttriButeId.UseAcSwingFunctionStatu);
        }
 
        /// <summary>
        /// 扫风模式
        /// </summary>
        public enum FanSwingMode
        {
            /// <summary>
            /// 一档
            /// </summary>
            First = 0,
            /// <summary>
            /// 二挡
            /// </summary>
            Second = 1,
            /// <summary>
            /// 三挡
            /// </summary>
            Thrid = 2,
            /// <summary>
            /// 四挡
            /// </summary>
            Fourth = 3,
            /// <summary>
            /// 五档
            /// </summary>
            Fifth = 4,
            /// <summary>
            /// 自动
            /// </summary>
            Auto = 7
        }
        /// <summary>
        /// 获取扫风模式
        /// </summary>
        public void ReadSystemFansSwingMode()
        {
            ReadAttri(Device.Cluster_ID.FanSwing, AttriButeId.FanSwing);
        }
        /// <summary>
        /// 设置扫风模式
        /// </summary>
        /// <param name="fanSwing"></param>
        /// <returns></returns>
        public async System.Threading.Tasks.Task<SetWritableValueResponAllData> SetFanSwingAsyncMode(FanSwingMode fanSwing)
        {
            if (Gateway == null)
            {
                return null;
            }
            return await System.Threading.Tasks.Task.Run(async () =>
            {
                SetWritableValueResponAllData d = null;
                Action<string, string> action = (topic, message) =>
                {
                    var gatewayID = topic.Split('/')[0];
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
 
                    if (topic == gatewayID + "/" + "Error_Respon")
                    {
                        var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonDevice.ErrorResponData>(jobject["Data"].ToString());
 
                        if (temp == null)
                        {
                            d = new SetWritableValueResponAllData { errorMessageBase = "网关错误回复,且数据是空" };
                        }
                        else
                        {
                            d = new SetWritableValueResponAllData { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) };
                        }
                    }
 
                    if (topic == gatewayID + "/" + "SetWritableValue_Respon")
                    {
                        var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<SetWritableValueResponData>(jobject["Data"].ToString());
 
                        if (tempData == null)
                        {
                            d = new SetWritableValueResponAllData { errorMessageBase = "网关返回的数据为空" };
                        }
                        else
                        {
                            d = new SetWritableValueResponAllData { setWritableValueResponData = tempData };
                            DebugPrintLog($"UI收到通知后的主题_{ topic}");
                        }
                    }
                };
                Gateway.Actions += action;
                DebugPrintLog("SetWritableValue_Actions 启动" + "_" + System.DateTime.Now.ToString());
                try
                {
                    var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", 514 }, { "Command", 120 } };
                    var data = new JObject { { "Undivided", 0 }, { "AttributeId", 4096 }, { "AttributeDataType", 48 }, { "AttributeData", (int)fanSwing } };
                    jObject.Add("Data", data);
                    Gateway.Send("SetWritableValue", jObject.ToString());
                }
                catch { }
 
                var dateTime = DateTime.Now;
                while ((DateTime.Now - dateTime).TotalMilliseconds < WaitReceiveDataTime)
                {
                    await System.Threading.Tasks.Task.Delay(10);
                    if (d != null)
                    {
                        break;
                    }
                }
                if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime)
                {
                    d = new SetWritableValueResponAllData { errorMessageBase = " 回复超时,请重新操作" };
                }
                Gateway.Actions -= action;
                DebugPrintLog("SetWritableValue_Actions 退出" + System.DateTime.Now.ToString());
                return d;
            });
        }
 
 
        ///<summary >
        ///设置制冷度数.
        /// </summary>
        public async System.Threading.Tasks.Task<SetWritableValueResponAllData> SetCoolingTemperatureAsync(int temperature)
        {
            if (Gateway == null)
            {
                return null;
            }
            return await System.Threading.Tasks.Task.Run(async () =>
            {
                SetWritableValueResponAllData d = null;
                Action<string, string> action = (topic, message) =>
                {
                    var gatewayID = topic.Split('/')[0];
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
 
                    if (topic == gatewayID + "/" + "Error_Respon")
                    {
                        var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonDevice.ErrorResponData>(jobject["Data"].ToString());
 
                        if (temp == null)
                        {
                            d = new SetWritableValueResponAllData { errorMessageBase = "网关错误回复,且数据是空" };
                        }
                        else
                        {
                            d = new SetWritableValueResponAllData { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) };
                        }
                    }
 
                    if (topic == gatewayID + "/" + "SetWritableValue_Respon")
                    {
                        var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<SetWritableValueResponData>(jobject["Data"].ToString());
 
                        if (tempData == null)
                        {
                            d = new SetWritableValueResponAllData { errorMessageBase = "网关返回的数据为空" };
                        }
                        else
                        {
                            d = new SetWritableValueResponAllData { setWritableValueResponData = tempData };
                            DebugPrintLog($"UI收到通知后的主题_{ topic}");
                        }
                    }
                };
                Gateway.Actions += action;
                DebugPrintLog("SetWritableValue_Actions 启动" + "_" + System.DateTime.Now.ToString());
                try
                {
                    var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", 513 }, { "Command", 120 } };
                    var data = new JObject { { "Undivided", 0 }, { "AttributeId", 17 }, { "AttributeDataType", 41 }, { "AttributeData", (int)temperature } };
                    jObject.Add("Data", data);
                    Gateway.Send("SetWritableValue", jObject.ToString());
                }
                catch { }
 
                var dateTime = DateTime.Now;
                while ((DateTime.Now - dateTime).TotalMilliseconds < WaitReceiveDataTime)
                {
                    await System.Threading.Tasks.Task.Delay(10);
                    if (d != null)
                    {
                        break;
                    }
                }
                if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime)
                {
                    d = new SetWritableValueResponAllData { errorMessageBase = " 回复超时,请重新操作" };
                }
                Gateway.Actions -= action;
                DebugPrintLog("SetWritableValue_Actions 退出" + System.DateTime.Now.ToString());
                return d;
            });
        }
 
        ///<summary >
        ///设置制热度数.
        /// </summary>
        public async System.Threading.Tasks.Task<SetWritableValueResponAllData> SetHeatingTemperatureAsync(int temperature)
        {
            if (Gateway == null)
            {
                return null;
            }
            return await System.Threading.Tasks.Task.Run(async () =>
            {
                SetWritableValueResponAllData d = null;
                Action<string, string> action = (topic, message) =>
                {
                    var gatewayID = topic.Split('/')[0];
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
 
                    if (topic == gatewayID + "/" + "Error_Respon")
                    {
                        var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonDevice.ErrorResponData>(jobject["Data"].ToString());
 
                        if (temp == null)
                        {
                            d = new SetWritableValueResponAllData { errorMessageBase = "网关错误回复,且数据是空" };
                        }
                        else
                        {
                            d = new SetWritableValueResponAllData { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) };
                        }
                    }
 
                    if (topic == gatewayID + "/" + "SetWritableValue_Respon")
                    {
                        var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<SetWritableValueResponData>(jobject["Data"].ToString());
 
                        if (tempData == null)
                        {
                            d = new SetWritableValueResponAllData { errorMessageBase = "网关返回的数据为空" };
                        }
                        else
                        {
                            d = new SetWritableValueResponAllData { setWritableValueResponData = tempData };
                            DebugPrintLog($"UI收到通知后的主题_{ topic}");
                        }
                    }
                };
                Gateway.Actions += action;
                DebugPrintLog("SetWritableValue_Actions 启动" + "_" + System.DateTime.Now.ToString());
                try
                {
                    var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", 513 }, { "Command", 120 } };
                    var data = new JObject { { "Undivided", 0 }, { "AttributeId", 18 }, { "AttributeDataType", 41 }, { "AttributeData", (int)temperature } };
                    jObject.Add("Data", data);
                    Gateway.Send("SetWritableValue", jObject.ToString());
                }
                catch { }
 
                var dateTime = DateTime.Now;
                while ((DateTime.Now - dateTime).TotalMilliseconds < WaitReceiveDataTime)
                {
                    await System.Threading.Tasks.Task.Delay(10);
                    if (d != null)
                    {
                        break;
                    }
                }
                if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime)
                {
                    d = new SetWritableValueResponAllData { errorMessageBase = " 回复超时,请重新操作" };
                }
                Gateway.Actions -= action;
                DebugPrintLog("SetWritableValue_Actions 退出" + System.DateTime.Now.ToString());
                return d;
            });
        }
 
        ///<summary >
        ///设置自动度数.
        /// </summary>
        public async System.Threading.Tasks.Task<SetWritableValueResponAllData> SetAutoTemperatureAsync(int temperature)
        {
            if (Gateway == null)
            {
                return null;
            }
            return await System.Threading.Tasks.Task.Run(async () =>
            {
                SetWritableValueResponAllData d = null;
                Action<string, string> action = (topic, message) =>
                {
                    var gatewayID = topic.Split('/')[0];
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
 
                    if (topic == gatewayID + "/" + "Error_Respon")
                    {
                        var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonDevice.ErrorResponData>(jobject["Data"].ToString());
 
                        if (temp == null)
                        {
                            d = new SetWritableValueResponAllData { errorMessageBase = "网关错误回复,且数据是空" };
                        }
                        else
                        {
                            d = new SetWritableValueResponAllData { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) };
                        }
                    }
 
                    if (topic == gatewayID + "/" + "SetWritableValue_Respon")
                    {
                        var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<SetWritableValueResponData>(jobject["Data"].ToString());
 
                        if (tempData == null)
                        {
                            d = new SetWritableValueResponAllData { errorMessageBase = "网关返回的数据为空" };
                        }
                        else
                        {
                            d = new SetWritableValueResponAllData { setWritableValueResponData = tempData };
                            DebugPrintLog($"UI收到通知后的主题_{ topic}");
                        }
                    }
                };
                Gateway.Actions += action;
                DebugPrintLog("SetWritableValue_Actions 启动" + "_" + System.DateTime.Now.ToString());
                try
                {
                    var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", 513 }, { "Command", 120 } };
                    var data = new JObject { { "Undivided", 0 }, { "AttributeId", 4096 }, { "AttributeDataType", 41 }, { "AttributeData", (int)temperature } };
                    jObject.Add("Data", data);
                    Gateway.Send("SetWritableValue", jObject.ToString());
                }
                catch { }
 
                var dateTime = DateTime.Now;
                while ((DateTime.Now - dateTime).TotalMilliseconds < WaitReceiveDataTime)
                {
                    await System.Threading.Tasks.Task.Delay(10);
                    if (d != null)
                    {
                        break;
                    }
                }
                if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime)
                {
                    d = new SetWritableValueResponAllData { errorMessageBase = " 回复超时,请重新操作" };
                }
                Gateway.Actions -= action;
                DebugPrintLog("SetWritableValue_Actions 退出" + System.DateTime.Now.ToString());
                return d;
            });
        }
 
        ///<summary >
        ///设置恒温器设备当前工作模式.
        /// </summary>
        public async System.Threading.Tasks.Task<SetWritableValueResponAllData> SetSystemModeAsync(AcMode acMode)
        {
            if (Gateway == null)
            {
                return null;
            }
            return await System.Threading.Tasks.Task.Run(async () =>
            {
                SetWritableValueResponAllData d = null;
                Action<string, string> action = (topic, message) =>
                {
                    var gatewayID = topic.Split('/')[0];
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
 
                    if (topic == gatewayID + "/" + "Error_Respon")
                    {
                        var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonDevice.ErrorResponData>(jobject["Data"].ToString());
 
                        if (temp == null)
                        {
                            d = new SetWritableValueResponAllData { errorMessageBase = "网关错误回复,且数据是空" };
                        }
                        else
                        {
                            d = new SetWritableValueResponAllData { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) };
                        }
                    }
 
                    if (topic == gatewayID + "/" + "SetWritableValue_Respon")
                    {
                        var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<SetWritableValueResponData>(jobject["Data"].ToString());
 
                        if (tempData == null)
                        {
                            d = new SetWritableValueResponAllData { errorMessageBase = "网关返回的数据为空" };
                        }
                        else
                        {
                            d = new SetWritableValueResponAllData { setWritableValueResponData = tempData };
                            DebugPrintLog($"UI收到通知后的主题_{ topic}");
                        }
                    }
                };
                Gateway.Actions += action;
                DebugPrintLog("SetWritableValue_Actions 启动" + "_" + System.DateTime.Now.ToString());
                try
                {
                    var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", 513 }, { "Command", 120 } };
                    var data = new JObject { { "Undivided", 0 }, { "AttributeId", 28 }, { "AttributeDataType", 48 }, { "AttributeData", (int)acMode } };
                    jObject.Add("Data", data);
                    Gateway.Send("SetWritableValue", jObject.ToString());
                }
                catch { }
 
                var dateTime = DateTime.Now;
                while ((DateTime.Now - dateTime).TotalMilliseconds < WaitReceiveDataTime)
                {
                    await System.Threading.Tasks.Task.Delay(10);
                    if (d != null)
                    {
                        break;
                    }
                }
                if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime)
                {
                    d = new SetWritableValueResponAllData { errorMessageBase = " 回复超时,请重新操作" };
                }
                Gateway.Actions -= action;
                DebugPrintLog("SetWritableValue_Actions 退出" + System.DateTime.Now.ToString());
                return d;
            });
        }
 
        public enum AcMode
        {
            /// <summary>
            /// 关闭模式(测试恒温面板时发现:2,5,6,7,8,9都是可以打开的)
            /// </summary>
            Off = 0,
            /// <summary>
            /// 自动模式
            /// </summary>
            Auto = 1,
            /// <summary>
            /// 制冷模式
            /// </summary>
            Cool = 3,
            /// <summary>
            /// 制热模式
            /// </summary>
            Heat = 4,
            /// <summary>
            /// 紧急制热模式
            /// </summary>
            EmergencyHeating = 5,
            /// <summary>
            /// 预冷模式
            /// </summary>
            Precooling = 6,
            /// <summary>
            /// 只有风速模式
            /// </summary>
            FanOnly = 7,
            /// <summary>
            /// 干燥模式
            /// </summary>
            Dry = 8,
            /// <summary>
            /// 睡眠模式
            /// </summary>
            Sleep = 9
        }
 
        ///<summary >
        ///设置恒温器设备当前风速.
        /// </summary>
        public async System.Threading.Tasks.Task<SetWritableValueResponAllData> SetFanModeAsync(FanMode fanMode)
        {
            if (Gateway == null)
            {
                return null;
            }
            return await System.Threading.Tasks.Task.Run(async () =>
            {
                SetWritableValueResponAllData d = null;
                Action<string, string> action = (topic, message) =>
                {
                    var gatewayID = topic.Split('/')[0];
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
 
                    if (topic == gatewayID + "/" + "Error_Respon")
                    {
                        var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonDevice.ErrorResponData>(jobject["Data"].ToString());
 
                        if (temp == null)
                        {
                            d = new SetWritableValueResponAllData { errorMessageBase = "网关错误回复,且数据是空" };
                        }
                        else
                        {
                            d = new SetWritableValueResponAllData { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) };
                        }
                    }
 
                    if (topic == gatewayID + "/" + "SetWritableValue_Respon")
                    {
                        var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<SetWritableValueResponData>(jobject["Data"].ToString());
 
                        if (tempData == null)
                        {
                            d = new SetWritableValueResponAllData { errorMessageBase = "网关返回的数据为空" };
                        }
                        else
                        {
                            d = new SetWritableValueResponAllData { setWritableValueResponData = tempData };
                            DebugPrintLog($"UI收到通知后的主题_{ topic}");
                        }
                    }
                };
                Gateway.Actions += action;
                DebugPrintLog("SetWritableValue_Actions 启动" + "_" + System.DateTime.Now.ToString());
                try
                {
                    var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", 514 }, { "Command", 120 } };
                    var data = new JObject { { "Undivided", 0 }, { "AttributeId", 0 }, { "AttributeDataType", 48 }, { "AttributeData", (int)fanMode } };
                    jObject.Add("Data", data);
                    Gateway.Send("SetWritableValue", jObject.ToString());
                }
                catch { }
 
                var dateTime = DateTime.Now;
                while ((DateTime.Now - dateTime).TotalMilliseconds < WaitReceiveDataTime)
                {
                    await System.Threading.Tasks.Task.Delay(10);
                    if (d != null)
                    {
                        break;
                    }
                }
                if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime)
                {
                    d = new SetWritableValueResponAllData { errorMessageBase = " 回复超时,请重新操作" };
                }
                Gateway.Actions -= action;
                DebugPrintLog("SetWritableValue_Actions 退出" + System.DateTime.Now.ToString());
                return d;
            });
        }
 
        public enum FanMode
        {
            /// <summary>
            /// 关闭风速
            /// </summary>
            Off = 0,
            /// <summary>
            /// 低风
            /// </summary>
            Low = 1,
            /// <summary>
            /// 中风
            /// </summary>
            Medium = 2,
            /// <summary>
            /// 高风
            /// </summary>
            High = 3,
            /// <summary>
            /// 打开风速
            /// </summary>
            On = 4,
            /// <summary>
            /// 自动
            /// </summary>
            Auto = 5,
            /// <summary>
            /// 聪明fe
            /// </summary>
            Smart = 6,
        }
 
        ///<summary >
        ///设置恒温设备加热或制冷度数(没有测试成功)
        /// <para>mode要调节的模式 0:加热;1:制冷;2:自动调节</para>
        ///<para>temperature:要变化的度数,单位:0.1℃</para>
        /// </summary>
        public void SetTemperature(int mode, int temperature)
        {
            var jObject = new JObject {
                { "DeviceAddr", DeviceAddr},
                { "Epoint", DeviceEpoint},
                    { "Cluster_ID",513},
                    { "Command", 0},
                    { "SendMode", 2 }
                };
            var data = new JObject {
                    { "Mode", mode },
                { "Amount", temperature }
                 };
            jObject.Add("Data", data);
            Gateway?.Send("DeviceControl", jObject.ToString());
        }
 
        /// <summary>
        /// 关闭
        /// </summary>
        /// <returns>The close.</returns>
        public async System.Threading.Tasks.Task<SetWritableValueResponAllData> Close()
        {
            return await SetSystemModeAsync(AcMode.Off);
        }
 
        /// <summary>
        /// 开启
        /// </summary>
        /// <returns>The open.</returns>
        /// <param name="acMode">Ac mode.</param>
        public async System.Threading.Tasks.Task<SetWritableValueResponAllData> Open(AcMode acMode = AcMode.Cool)
        {
            return await SetSystemModeAsync(acMode);
        }
    }
}