黄学彪
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
using System;
using Newtonsoft.Json.Linq;
 
namespace ZigBee.Device
{
    [System.Serializable]
    public class Rollershade : CurtainBase
    {
        public Rollershade()
        {
            this.Type = DeviceType.WindowCoveringDevice;
        }
 
        /// <summary>
        ///窗帘当前模式.
        /// WcdType=4:开合帘
        /// WcdType=0:卷帘
        /// </summary>
        public int WcdType = -1;
        /// <summary>
        /// 当前窗帘所在的位置(百分比).
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public int WcdCurrentPositionLiftPercentage = 0;
 
        /// <summary>
        /// 读取当前窗帘的模式
        /// <para>AttriButeId=0 (先调用发现属性接口,查看设备是否支持该接口的属性)</para>
        /// </summary>
        public new void ReadWcdType()
        {
            ReadAttri(Device.Cluster_ID.WindowCovering, AttriButeId.WindowCoveringType);
        }
 
        /// <summary>
        /// 读取当前窗帘模式
        /// </summary>
        /// <param name="commonDevice"></param>
        /// <param name="action"></param>
        public static void ReadWcdTypeAction(CommonDevice commonDevice, Action action)
        {
            new System.Threading.Thread(() =>
            {
                (commonDevice as Rollershade).ReadWcdType();
                var dateTime = DateTime.Now;
                while ((DateTime.Now - dateTime).TotalSeconds < 10)
                {
                    System.Threading.Thread.Sleep(100);
                    if ((commonDevice as Rollershade).WcdType != -1)
                    {
                        break;
                    }
                }
                action?.Invoke();
            })
            { IsBackground = true }.Start();
        }
 
        /// <summary>
        /// 窗帘当前倾斜度,单位:0.1°
        /// <para>AttriButeId=3 (先调用发现属性接口,查看设备是否支持该接口的属性)</para>
        /// </summary>
        public void ReadWcdCurrentPositionLift()
        {
            ReadAttri(Device.Cluster_ID.WindowCovering, AttriButeId.CurrentPositionLift);
        }
 
        /// <summary>
        /// 读取当前窗帘所在的位置(百分比)
        /// <para>AttriButeId=8 (先调用发现属性接口,查看设备是否支持该接口的属性)</para>
        /// </summary>
        public void ReadWcdCurrentPositionLiftPercentage()
        {
            ReadAttri(Device.Cluster_ID.WindowCovering, AttriButeId.CurrentPositionLiftPercentage);
        }
 
        /// <summary>
        /// 窗帘全开所在的位置,单位:cm
        /// <para>AttriButeId=16 (先调用发现属性接口,查看设备是否支持该接口的属性)</para>
        /// </summary>
        public void ReadWcdInstalledOpenLimitLift()
        {
            ReadAttri(Device.Cluster_ID.WindowCovering, AttriButeId.InstalledOpenLimitLift);
        }
 
        /// <summary>
        /// 窗帘全开所在的角度,单位:0.1°
        /// <para>AttriButeId=17 (先调用发现属性接口,查看设备是否支持该接口的属性)</para>
        /// </summary>
        public void ReadWcdInstalledClosedLimitLift()
        {
            ReadAttri(Device.Cluster_ID.WindowCovering, AttriButeId.InstalledClosedLimitLift);
        }
 
        /// <summary>
        /// 窗帘全关所在的位置,单位:cm
        /// <para>AttriButeId=18 (先调用发现属性接口,查看设备是否支持该接口的属性)</para>
        /// </summary>
        public void ReadWcdInstalledOpenLimitTilt()
        {
            ReadAttri(Device.Cluster_ID.WindowCovering, AttriButeId.InstalledOpenLimitTilt);
        }
 
        /// <summary>
        /// 窗帘全关所在的角度,单位:0.1°
        /// <para>AttriButeId=19 (先调用发现属性接口,查看设备是否支持该接口的属性)</para>
        /// </summary>
        public void ReadWcdInstalledClosedLimitTilt()
        {
            ReadAttri(Device.Cluster_ID.WindowCovering, AttriButeId.InstalledClosedLimitTilt);
        }
 
        /// <summary>
        /// 读取当前窗帘的模式
        /// <para>AttriButeId=23 (先调用发现属性接口,查看设备是否支持该接口的属性)</para>
        /// </summary>
        public void ReadWcdCurrentMode()
        {
            ReadAttri(Device.Cluster_ID.WindowCovering, AttriButeId.WindowCoveringMode);
        }
 
        /// <summary>
        /// 读取开合帘电机方向/维护模式
        /// </summary>
        public void ReadCurtainDirectionAndMode()
        {
            ReadAttri(Device.Cluster_ID.WindowCovering, AttriButeId.WindowCoveringMode);
        }
 
        /// <summary>
        ///  读取开合帘限位开(上)限位
        /// <para>窗帘全开所在的位置,单位:cm</para>
        /// </summary>
        public void ReadWcdInstalOpenLimit()
        {
            ReadAttri(Device.Cluster_ID.WindowCovering, AttriButeId.InstalledOpenLimitLift);
        }
 
        /// <summary>
        ///  读取开合帘总长
        /// <para>窗帘总的长度,单位:cm</para>
        /// </summary>
        public void ReadWcdLength()
        {
            ReadAttri(Device.Cluster_ID.WindowCovering, AttriButeId.WindowCoveringLength);
        }
 
        /// <summary>
        ///  读取开合帘限位关(下)限位
        /// <para>窗帘全关所在的位置,单位:cm</para>
        /// </summary>
        public void ReadWcdInstalCloseLimit()
        {
            ReadAttri(Device.Cluster_ID.WindowCovering, AttriButeId.InstalledClosedLimitLift);
        }
 
        ///<summary >
        ///窗帘设备调整到指定高度.
        /// <para>comand=4 (先调用设备发现命令接口,查看设备是否支持该接口命令)</para>
        ///<para>value:窗帘要调整到的高度值处</para>
        ///<para> 参数长度(占用的字节数):2</para>
        /// </summary>
        public void WcdGoToLiftValue(int value)
        {
            var jObject = new JObject {
                { "DeviceAddr", DeviceAddr},
                { "Epoint", DeviceEpoint},
                    { "Cluster_ID", 258 },
                    { "Command", 4},
                    { "SendMode", 2 }
                };
            var data = new JObject {
                    { "Value", value }
                 };
            jObject.Add("Data", data);
            Gateway?.Send("DeviceControl", jObject.ToString());
        }
 
        ///<summary >
        ///将窗帘调整到指定的百分比处的位置.
        /// <para>comand=5 (先调用设备发现命令接口,查看设备是否支持该接口命令)</para>
        ///<para>窗帘要调整到的百分比处,范围:0~100表示 0-100%</para>
        ///<para>参数长度(占用的字节数):1</para>
        /// </summary>
        public void WcdGoToTiltValue(int value)
        {
            var jObject = new JObject {
                { "DeviceAddr", DeviceAddr},
                { "Epoint", DeviceEpoint},
                    { "Cluster_ID",258},
                    { "Command", 5},
                    { "SendMode", 2 }
                };
            var data = new JObject {
                    { "Value", value }
                 };
            jObject.Add("Data", data);
            Gateway?.Send("DeviceControl", jObject.ToString());
        }
 
        ///<summary >
        ///让窗帘设备将窗帘调整到指定的倾斜角
        /// <para>comand=7 (先调用设备发现命令接口,查看设备是否支持该接口命令)</para>
        ///<para>value:窗帘要调整到的倾斜角处 单位(0.1°)</para>
        ///<para> 参数长度(占用的字节数):2</para>
        /// </summary>
        public void WcdGoToLiftPercentpage(int value)
        {
            var jObject = new JObject {
                { "DeviceAddr", DeviceAddr},
                { "Epoint", DeviceEpoint},
                    { "Cluster_ID", 258 },
                    { "Command", 7},
                    { "SendMode", 2 }
                };
            var data = new JObject {
                    { "Value", value }
                };
            jObject.Add("Data", data);
            Gateway?.Send("DeviceControl", jObject.ToString());
        }
 
        ///<summary >
        ///让窗帘设备将窗帘调整到指定的百分比倾斜度
        /// <para>comand=8 (先调用设备发现命令接口,查看设备是否支持该接口命令)</para>
        ///<para>value:窗帘要调整到的百分比倾斜度</para>
        ///<para>范围:0-100表示0-100%</para>
        /// <para>参数长度(占用的字节数):1</para>
        /// </summary>
        public void WcdGoToTiltPercentpage(int value)
        {
            var jObject = new JObject {
                { "DeviceAddr", DeviceAddr},
                { "Epoint", DeviceEpoint},
                    { "Cluster_ID", 258},
                    { "Command", 8},
                    { "SendMode", 2 }
                };
            var data = new JObject {
                    { "Value", value }
                 };
            jObject.Add("Data", data);
            Gateway?.Send("DeviceControl", jObject.ToString());
        }
 
        #region 设置开合帘限位参数.
        /// <summary>
        /// 设置开合帘上下限位
        /// </summary>
        public async System.Threading.Tasks.Task<SetWritableValueResponAllData> SetCurtainInstalledLimitAsync(CurtainPrivateInstalledLimi installedLimit)
        {
            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 };
                            System.Console.WriteLine($"UI收到通知后的主题_{ topic}");
                        }
                    }
                };
                Gateway.Actions += action;
                System.Console.WriteLine("SetWritableValue_Actions 启动" + "_" + System.DateTime.Now.ToString());
 
                try
                {
                    int tempLimit = 0;
                    switch (installedLimit)
                    {
                        case CurtainPrivateInstalledLimi.UpLimit:
                            tempLimit = 2;
                            break;
                        case CurtainPrivateInstalledLimi.DownLimit:
                            tempLimit = 1;
                            break;
                    }
                    var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", 258 }, { "Command", 120 } };
                    var data = new JObject { { "Undivided", 0 }, { "AttributeId", 0xea01 }, { "AttributeDataType", 32 }, { "AttributeData", tempLimit } };
                    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;
                System.Console.WriteLine("SetWritableValue_Actions 退出" + System.DateTime.Now.ToString());
 
                return d;
            });
        }
 
        /// <summary>
        /// 开合帘限位
        /// </summary>
        public enum CurtainPrivateInstalledLimi
        {
            /// <summary>
            /// 窗帘全开所在的位置(上限位)
            /// </summary>
            UpLimit = 2,
            /// <summary>
            /// 窗帘全关所在的位置(下限位)
            /// </summary>
            DownLimit = 1,
 
        }
        #endregion
 
        #region 设置恢复窗帘行程限位.
        /// <summary>
        /// 设置恢复窗帘行程限位
        /// </summary>
        public async System.Threading.Tasks.Task<SetWritableValueResponAllData> RestoreCurtainLimitAsync()
        {
            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 };
                            System.Console.WriteLine($"UI收到通知后的主题_{ topic}");
                        }
                    }
                };
                Gateway.Actions += action;
                System.Console.WriteLine("SetWritableValue_Actions 启动" + "_" + System.DateTime.Now.ToString());
 
                try
                {
 
                    var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", 258 }, { "Command", 120 } };
                    var data = new JObject { { "Undivided", 0 }, { "AttributeId", 0xea02 }, { "AttributeDataType", 32 }, { "AttributeData", 1 } };
                    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;
                System.Console.WriteLine("SetWritableValue_Actions 退出" + System.DateTime.Now.ToString());
 
                return d;
            });
        }
 
        /// <summary>
        /// 开合帘限位
        /// </summary>
        enum CurtainPrivateAttribute
        {
            /// <summary>
            /// 窗帘行程限位
            /// </summary>
            AttributeLimit = 0xea01,
            /// <summary>
            /// 设置恢复窗帘行程限位
            /// </summary>
            RestoreLimit = 0xea02,
 
        }
        #endregion
 
        #region 删除窗帘限位
        /// <summary>
        /// 删除窗帘限位
        /// </summary>
        public async System.Threading.Tasks.Task<SetWritableValueResponAllData> DeleteCurtainLimitsAsync(LimiType limiType)
        {
            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 };
                            System.Console.WriteLine($"UI收到通知后的主题_{ topic}");
                        }
                    }
                };
                Gateway.Actions += action;
                System.Console.WriteLine("SetWritableValue_Actions 启动" + "_" + System.DateTime.Now.ToString());
 
                try
                {
                    int tempLimit = 0;
                    switch (limiType)
                    {
                        case LimiType.DownLimit:
                            tempLimit = 2;
                            break;
                        case LimiType.UpLimit:
                            tempLimit = 8;
                            break;
                    }
                    var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", 258 }, { "Command", 120 } };
                    var data = new JObject { { "Undivided", 0 }, { "AttributeId", 0xea02 }, { "AttributeDataType", 32 }, { "AttributeData", tempLimit } };
                    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;
                System.Console.WriteLine("SetWritableValue_Actions 退出" + System.DateTime.Now.ToString());
 
                return d;
            });
        }
 
        /// <summary>
        /// 删除窗帘开合帘限位
        /// </summary>
        public enum LimiType
        {
            /// <summary>
            /// 删除窗帘上限位
            /// </summary>
            UpLimit = 0x08,
            /// <summary>
            /// 删除窗帘下限位
            /// </summary>
            DownLimit = 0x02,
 
        }
        #endregion
    }
}