黄学彪
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
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
 
namespace ZigBee.Device
{
    [System.Serializable]
    public class IASZone : BindObj
    {
        public IASZone()
        {
            this.Type = DeviceType.IASZone;
        }
 
        #region IAS安防信息上报.
        /// <summary>
        /// 上一次访问iASInfo的时间
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        private DateTime oldiASInfoTime = DateTime.Now;
        /// <summary>
        /// IAS安防信息上报
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        private IASInfoData m_iASInfo = null;
        /// <summary>
        /// IAS安防信息上报(这个东西30秒之后就清掉)
        /// <para>当安防设备属性状态改变时候(例如一氧化碳传感器检测到一氧化碳气体),设备将上报属性状态变化数据。</para>
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public IASInfoData iASInfo
        {
            get
            {
                if (m_iASInfo != null && (DateTime.Now - oldiASInfoTime).TotalMilliseconds > 30 * 1000)
                {
                    //这个东西30秒之后就清掉
                    m_iASInfo = null;
                    return null;
                }
                return m_iASInfo;
            }
            set
            {
                //记录起更新时间
                this.oldiASInfoTime = DateTime.Now;
                m_iASInfo = value;
            }
        }
 
        /// <summary>
        /// IAS安防信息上报
        /// <para>当安防设备属性状态改变时候(例如一氧化碳传感器检测到一氧化碳气体),设备将上报属性状态变化数据。</para>
        /// </summary>
        [System.Serializable]
        public class IASInfoData
        {
            /// <summary>
            ///设备状态()
            /// <para>下面的 Alarm1到BatteryDefect实则是对ZoneStatus二进制格式的解析</para>
            /// </summary>
            public int ZoneStatus;
            /// <summary>
            ///自定义设备状态
            /// </summary>
            public int ExtendedStatus;
            /// <summary>
            ///区域ID
            /// </summary>
            public int ZoneId;
            /// <summary>
            ///在设备状态改变之后延时Delay(单位:1/4秒)
            /// </summary>
            public int Delay;
            /// <summary>
            /// hdl
            ///1 - opened or alarmed
            ///0 - closed or not alarmed
            /// </summary>
            public int Alarm1;
            /// <summary>
            ///1 - opened or alarmed
            ///0 - closed or not alarmed
            /// </summary>
            public int Alarm2;
            /// <summary>
            ///1 - Tampered
            ///0 - Not tampered
            /// </summary>
            public int Tamper;
            /// <summary>
            ///1 - Low battery
            ///0 - Battery OK
            /// </summary>
            public int Battery;
            /// <summary>
            ///1 - Reports
            ///0 - Does not report
            /// </summary>
            public int SupervisionReports;
            /// <summary>
            ///1 - Reports restore
            ///0 - Does not report restore
            /// </summary>
            public int RestoreReports;
 
            /// <summary>
            ///1 - Trouble/Failure
            ///0 - OK
            /// </summary>
            public int Trouble;
            /// <summary>
            ///1 - AC/Mains fault
            ///0 - AC/Mains OK
            /// </summary>
            public int MaAC_Mainsins;
            /// <summary>
            ///1 - Sensor is in test mode
            ///0 - Sensor is operation mode
            /// </summary>
            public int Test;
            /// <summary>
            ///1 - Sensor detects a defective battery
            ///0 - Sensor battery is functioning normally
            /// </summary>
            public int BatteryDefect;
        }
        #endregion
 
        #region 获取PIR光感等级(光照能力值).
        ///<summary >
        ///获取PIR光感等级(光照能力值)
        /// <para>reserve:0-ff</para>
        /// </summary>
        public async System.Threading.Tasks.Task<PirLightAbilitySizeInfo> GetPIRLightAbilitySizeAsync(string reserve = "01")
        {
            PirLightAbilitySizeInfo result = null;
            if (Gateway == null)
            {
                result = new PirLightAbilitySizeInfo { errorMessageBase = "当前没有网关" };
                return result;
            }
            return await System.Threading.Tasks.Task.Run(async () =>
            {
                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)
                        {
                            result = new PirLightAbilitySizeInfo { errorMessageBase = "网关错误回复,且数据是空" };
                        }
 
                        else
                        {
                            result = new PirLightAbilitySizeInfo { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) };
                        }
                    }
 
                    if (topic == gatewayID + "/" + "ZbDataPassthrough")
                    {
                        var clientDataPassthroughResponseData = Newtonsoft.Json.JsonConvert.DeserializeObject<ClientDataPassthroughResponseData>(jobject["Data"].ToString());
 
                        if (clientDataPassthroughResponseData == null)
                        {
                            result = new PirLightAbilitySizeInfo { errorMessageBase = "网关返回的数据为空" };
                        }
                        else
                        {
                            if (clientDataPassthroughResponseData?.PassData != null)
                            {
                                var data = clientDataPassthroughResponseData.PassData;
                                if (data.Length == 12)
                                {
                                    var command = data[4].ToString() + data[5].ToString() + data[2].ToString() + data[3].ToString();
                                    if (command == "0304")
                                    {
                                        var cou = data[10].ToString() + data[11].ToString();
                                        int countTemp = Convert.ToInt32(cou, 16);
                                        result = new PirLightAbilitySizeInfo { LightLevelCount = countTemp };
                                        System.Console.WriteLine($"UI收到通知后的主题_command:0303_{ topic}");
                                    }
                                }
                            }
                        }
                    }
                };
 
                Gateway.Actions += action;
                System.Console.WriteLine("ClientDataPassthrough_Actions 启动" + System.DateTime.Now.ToString());
 
                try
                {
                    var passData = SendPIRLightAbilitySizeData(reserve);
                    var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", 200 }, { "Cluster_ID", 64513 }, { "Command", 0 } };
                    var data = new JObject { { "PassData", passData } };
                    jObject.Add("Data", data);
                    Gateway.Send(("ClientDataPassthrough"), jObject.ToString());
                }
                catch { }
 
                var dateTime = DateTime.Now;
                while ((DateTime.Now - dateTime).TotalMilliseconds < 9000)//WaitReceiveDataTime)
                {
                    await System.Threading.Tasks.Task.Delay(10);
                    if (result != null)
                    {
                        break;
                    }
                }
                if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime)
                {
                    result = new PirLightAbilitySizeInfo { errorMessageBase = " 回复超时,请重新操作" };
                }
                Gateway.Actions -= action;
                System.Console.WriteLine("ClientDataPassthrough_Actions 退出" + System.DateTime.Now.ToString());
 
                return result;
            });
        }
 
        /// <summary>
        /// 发送PIR光感等级(光照能力值)数据
        /// <para>发配置按键指示灯颜色命令时,此时为发送到网关的透传数据</para>
        /// </summary>
        string SendPIRLightAbilitySizeData(string reserve)
        {
            string data = "";
            string dataLength = "05";
            string dataComand1 = "03";
            string dataComand2 = "03";
            string dataSerialNum = "01";
            string addDataLength = "01";
            string reserveData = reserve;
 
            try
            {
                data = dataLength + dataComand1 + dataComand2 + dataSerialNum + addDataLength +
                    reserveData;
            }
            catch { };
 
            return data;
        }
 
        /// <summary>
        /// PIR配置参数回复
        /// </summary>
        [System.Serializable]
        public class PirLightAbilitySizeInfo : ErrorResponCommon
        {
            /// <summary>
            /// 光感等级个数(Lux能力)
            /// <para>有几个等级就显示几个刻度</para>
            /// </summary>
            public int LightLevelCount = -1;
        }
 
        #endregion
 
        #region PIR传感器参数配置
 
        /// <summary>
        /// PIR配置参数回复
        /// </summary>
        [System.Serializable]
        public class ParamatesInfo : ErrorResponCommon
        {
            /// <summary>
            /// PIR配置参数
            /// </summary>
            public ConfigureParamates configureParamates;
        }
 
        /// <summary>
        /// PIR配置参数
        /// </summary>
        [System.Serializable]
        public class ConfigureParamates
        {
            /// <summary>
            /// 亮度(光照度)使能
            /// <para> false(0):不使能</para>
            /// <para>true(1):使能</para>
            /// </summary>
            public bool levelEnable;
            /// <summary>
            /// 光照度等级
            /// <para>值:0-10</para>
            /// </summary>
            public int levelSize = -1;
            /// <summary>
            ///控制设备使能位
            ///<para> false(0):不使能</para>
            /// <para>true)1):使能</para>
            /// </summary>
            public bool controlDevEnable;
            /// <summary>
            /// 触发开灯到触发关灯的时间间隔
            /// <para>0-65535 单位秒</para>
            /// </summary>
            public int transitionTime;
            /// <summary>
            /// IAS上报周期
            /// <para>10-0xffff 秒</para>
            /// </summary>
            public int iasReportPeriod;
            /// <summary>
            /// 光照度等级
            /// <para>0:半自动模式</para>
            /// <para>1:自动模式</para>
            /// </summary>
            public int mode = 0;
            /// <summary>
            /// 开灯类型
            /// <para>值:0:调光</para>
            ///<para>1:开关 </para>
            /// </summary>
            public int type = 0;
            /// <summary>
            /// 调光模式,到达开灯亮度的时间
            /// <para>值:0-10 秒</para>
            /// </summary>
            public int dimmerOnTime = 0;
            /// <summary>
            /// 调光模式,关灯到达0%所需要的时间
            /// <para>值:0-10 秒</para>
            /// </summary>
            public int dimmerOffTime = 0;
            /// <summary>
            /// 调光模式开灯的亮度
            /// <para>0-0xff (预留)</para>
            /// </summary>
            public int dimmerLevel = 0;
        }
 
        #endregion
 
        #region 获取PIR lux值(Lux值)
        ///<summary >
        ///获取PIR lux值(Lux值)
        /// <para>reserve:0-ff</para>
        /// </summary>
        public async System.Threading.Tasks.Task<PirLuxAbilitySizeInfo> GetPirLuxAbilitySizeAsync(string reserve = "01")
        {
            PirLuxAbilitySizeInfo result = null;
            if (Gateway == null)
            {
                result = new PirLuxAbilitySizeInfo { errorMessageBase = "当前没有网关" };
                return result;
            }
            return await System.Threading.Tasks.Task.Run(async () =>
            {
                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)
                        {
                            result = new PirLuxAbilitySizeInfo { errorMessageBase = "网关错误回复,且数据是空" };
                        }
 
                        else
                        {
                            result = new PirLuxAbilitySizeInfo { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) };
                        }
                    }
 
                    if (topic == gatewayID + "/" + "ZbDataPassthrough")
                    {
                        var clientDataPassthroughResponseData = Newtonsoft.Json.JsonConvert.DeserializeObject<ClientDataPassthroughResponseData>(jobject["Data"].ToString());
 
                        if (clientDataPassthroughResponseData == null)
                        {
                            result = new PirLuxAbilitySizeInfo { errorMessageBase = "网关返回的数据为空" };
                        }
                        else
                        {
                            if (clientDataPassthroughResponseData?.PassData != null)
                            {
                                var data = clientDataPassthroughResponseData.PassData;
                                if (data.Length == 12)
                                {
                                    var command = data[4].ToString() + data[5].ToString() + data[2].ToString() + data[3].ToString();
                                    if (command == "0306")
                                    {
                                        var cou = data[10].ToString() + data[11].ToString();
                                        int countTemp = Convert.ToInt32(cou, 16);
                                        result = new PirLuxAbilitySizeInfo { pirLux = countTemp };
                                        System.Console.WriteLine($"UI收到通知后的主题_command:0303_{ topic}");
                                    }
                                }
                            }
                        }
                    }
                };
 
                Gateway.Actions += action;
                System.Console.WriteLine("ClientDataPassthrough_Actions 启动" + System.DateTime.Now.ToString());
 
                try
                {
                    var passData = SendPIRLuxAbilitySizeData(reserve);
                    var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", 200 }, { "Cluster_ID", 64513 }, { "Command", 0 } };
                    var data = new JObject { { "PassData", passData } };
                    jObject.Add("Data", data);
                    Gateway.Send(("ClientDataPassthrough"), jObject.ToString());
                }
                catch { }
 
                var dateTime = DateTime.Now;
                while ((DateTime.Now - dateTime).TotalMilliseconds < 9000)//WaitReceiveDataTime)
                {
                    await System.Threading.Tasks.Task.Delay(10);
                    if (result != null)
                    {
                        break;
                    }
                }
                if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime)
                {
                    result = new PirLuxAbilitySizeInfo { errorMessageBase = " 回复超时,请重新操作" };
                }
                Gateway.Actions -= action;
                System.Console.WriteLine("ClientDataPassthrough_Actions 退出" + System.DateTime.Now.ToString());
 
                return result;
            });
        }
 
        /// <summary>
        ///获取PIR lux值
        /// </summary>
        string SendPIRLuxAbilitySizeData(string reserve)
        {
            string data = "";
            string dataLength = "05";
            string dataComand1 = "05";
            string dataComand2 = "03";
            string dataSerialNum = "01";
            string addDataLength = "01";
            string reserveData = reserve;
 
            try
            {
                data = dataLength + dataComand1 + dataComand2 + dataSerialNum + addDataLength +
                    reserveData;
            }
            catch { };
 
            return data;
        }
 
        /// <summary>
        /// PIR配置参数回复
        /// </summary>
        [System.Serializable]
        public class PirLuxAbilitySizeInfo
        {
            /// <summary>
            /// 错误信息
            /// </summary>
            public string errorMessageBase;
            /// <summary>
            /// 网关信息错误反馈
            /// <para>当网关接收到客户端信息后,出现以下异常情况将反馈错误。</para>
            /// </summary>
            public ErrorResponData errorResponData;
            /// <summary>
            /// 当前Lux值 
            /// <para>0-0xff Lux</para>
            /// </summary>
            public int pirLux = -1;
        }
 
        #endregion
    }
}