HDL Home App 第二版本 旧平台金堂用 正在使用
xm
2019-07-16 b910cb79c9b5bcc204022a3cf9e6950f0a64dfbd
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
using Shared.Common;
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
 
namespace Shared.Phone.UserCenter
{
    /// <summary>
    /// 设备属性上报的逻辑类
    /// </summary>
    public class DeviceAttributeLogic : ZigBee.Common.IStatus
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 属性上报的逻辑
        /// </summary>
        private static DeviceAttributeLogic m_Current = null;
        /// <summary>
        /// 属性上报的逻辑
        /// </summary>
        public static DeviceAttributeLogic Current
        {
            get
            {
                if (m_Current == null)
                {
                    m_Current = new DeviceAttributeLogic();
                }
                return m_Current;
            }
            set
            {
                m_Current = value;
            }
        }
 
        /// <summary>
        /// 锁
        /// </summary>
        private object objLock = new object();
        /// <summary>
        /// 事件集合
        /// </summary>
        private Dictionary<string, Action<CommonDevice>> dicEvent = new Dictionary<string, Action<CommonDevice>>();
        /// <summary>
        /// 命令区分
        /// </summary>
        private Dictionary<string, string> dicCommandDiv = new Dictionary<string, string>();
        /// <summary>
        /// 安防的上报(这个东西有点特殊)
        /// </summary>
        private Dictionary<string, Action<CommonDevice, bool>> dicSafetyEvent = new Dictionary<string, Action<CommonDevice, bool>>();
 
        #endregion
 
        #region ■ 添加监听___________________________
 
        /// <summary>
        /// 添加监视安防设备报警的事件(不需要再执行任何操作)
        /// </summary>
        /// <param name="mainKeys">标识事件的主键(可以随便填,主要是针对多个界面一起使用的情况)</param>
        /// <param name="action">回调函数,参数1:报警的设备。参数2:是否是安防设备报警</param>
        public void AddSafetyAlarmEvent(string mainKeys, Action<CommonDevice, bool> action)
        {
            //以防万一,添加全局安防上报监听
            this.AddSaveSafetyAlarmInfoEvent();
            this.dicSafetyEvent[mainKeys] = action;
        }
 
        #endregion
 
        #region ■ 移除监听___________________________
 
        /// <summary>
        /// 移除事件
        /// </summary>
        /// <param name="mainKeys">标识事件的主键(可以随便填,主要是针对多个界面一起使用的情况)</param>
        public void RemoveEvent(string mainKeys)
        {
            lock (objLock)
            {
                if (this.dicEvent.ContainsKey(mainKeys) == true)
                {
                    this.dicEvent.Remove(mainKeys);
                    this.dicCommandDiv.Remove(mainKeys);
                }
                if (this.dicSafetyEvent.ContainsKey(mainKeys) == true)
                {
                    this.dicSafetyEvent.Remove(mainKeys);
                }
                if (this.dicEvent.Count == 0)
                {
                    ZigBee.Device.ZbGateway.StatusList.Remove(this);
                }
            }
        }
 
        /// <summary>
        /// 移除全部的事件
        /// </summary>
        public void RemoveAllEvent()
        {
            lock (objLock)
            {
                this.dicEvent.Clear();
                this.dicCommandDiv.Clear();
            }
        }
 
        #endregion
 
        #region ■ 获取版本___________________________
 
        /// <summary>
        /// 添加读取设备固件版本的事件(之后调用SetVersionComand发送命令,最后用SetFirmwareVersion设置值)
        /// </summary>
        /// <param name="mainKeys">标识事件的主键(可以随便填,主要是针对多个界面一起使用的情况)</param>
        /// <param name="action">回调函数</param>
        public void AddFirmwareVersionEvent(string mainKeys, Action<CommonDevice> action)
        {
            lock (objLock)
            {
                if (this.dicEvent.Count == 0)
                {
                    ZigBee.Device.ZbGateway.StatusList.Add(this);
                }
                this.dicEvent[mainKeys] = action;
                this.dicCommandDiv[mainKeys] = "DeviceStatusReport";
            }
        }
 
        /// <summary>
        /// 发送获取固件版本信息的命令
        /// </summary>
        /// <param name="device"></param>
        public void SetFirmwareVersionComand(OTADevice device)
        {
            var jObject = new Newtonsoft.Json.Linq.JObject
            {
                { "DeviceAddr",device.DeviceAddr },
                { "Epoint", device.DeviceEpoint },
                { "Cluster_ID", (int)Cluster_ID.Ota },
                { "Command", 108 }
            };
            var attriBute = new Newtonsoft.Json.Linq.JArray
            {
               new Newtonsoft.Json.Linq.JObject
               {
                 { "AttriButeId", (int)AttriButeId.ImgVersion}
               },
               new Newtonsoft.Json.Linq.JObject
               {
                 { "AttriButeId", (int)AttriButeId.mgHWversion}
               },
               new Newtonsoft.Json.Linq.JObject
               {
                 { "AttriButeId", (int)AttriButeId.ImgTypeId}
               }
            };
            var data = new Newtonsoft.Json.Linq.JObject { { "AttriBute", attriBute } };
            jObject.Add("Data", data);
            device.Gateway?.Send(("GetDeviceStatus"), jObject.ToString());
        }
 
        /// <summary>
        /// 设置设备的固件版本
        /// </summary>
        /// <param name="report">上报信息</param>
        /// <param name="device">设备对象</param>
        /// <returns></returns>
        public bool SetFirmwareVersion(CommonDevice.DeviceStatusReportData report, CommonDevice device)
        {
            if (report == null)
            {
                return false;
            }
            foreach (var data in report.AttriBute)
            {
                //镜像版本
                if (data.AttributeId == (int)AttriButeId.ImgVersion)
                {
                    device.ImgTypeId = data.AttriButeData;
                }
                //硬件版本
                if (data.AttributeId == (int)AttriButeId.mgHWversion)
                {
                    device.HwVersion = data.AttriButeData;
                }
                //镜像版本
                if (data.AttributeId == (int)AttriButeId.ImgTypeId)
                {
                    device.ImgTypeId = data.AttriButeData;
                }
            }
            return true;
        }
 
        #endregion
 
        #region ■ 获取硬件信息_______________________
 
        /// <summary>
        /// 添加获取硬件信息的事件(之后调用SetHardFirmwareInfoComand发送命令,最后用SetHardFirmwareInfo设置值)
        /// </summary>
        /// <param name="mainKeys">标识事件的主键(可以随便填,主要是针对多个界面一起使用的情况)</param>
        /// <param name="action">回调函数</param>
        public void AddHardFirmwareInfoEvent(string mainKeys, Action<CommonDevice> action)
        {
            lock (objLock)
            {
                if (this.dicEvent.Count == 0)
                {
                    ZigBee.Device.ZbGateway.StatusList.Add(this);
                }
                this.dicEvent[mainKeys] = action;
                this.dicCommandDiv[mainKeys] = "DeviceStatusReport";
            }
        }
 
        /// <summary>
        /// 发送获取硬件信息的命令
        /// </summary>
        /// <param name="device"></param>
        public void SetHardFirmwareInfoComand(CommonDevice device)
        {
            var jObject = new Newtonsoft.Json.Linq.JObject
            {
                { "DeviceAddr",device.DeviceAddr },
                { "Epoint", device.DeviceEpoint },
                { "Cluster_ID", (int)Cluster_ID.Basic },
                { "Command", 108 }
            };
            var attriBute = new Newtonsoft.Json.Linq.JArray
            {
               new Newtonsoft.Json.Linq.JObject
               {
                 { "AttriButeId", 4}
               },
               new Newtonsoft.Json.Linq.JObject
               {
                 { "AttriButeId", 5}
               },
               new Newtonsoft.Json.Linq.JObject
               {
                 { "AttriButeId", 6}
               },
                  new Newtonsoft.Json.Linq.JObject
               {
                 { "AttriButeId", 7}
               },
               new Newtonsoft.Json.Linq.JObject
               {
                 { "AttriButeId", 13}
               }
            };
            var data = new Newtonsoft.Json.Linq.JObject { { "AttriBute", attriBute } };
            jObject.Add("Data", data);
            device.Gateway?.Send(("GetDeviceStatus"), jObject.ToString());
        }
 
        /// <summary>
        /// 设置设备的硬件信息
        /// </summary>
        /// <param name="report">上报信息</param>
        /// <param name="device">设备对象</param>
        /// <returns></returns>
        public bool SetHardFirmwareInfo(CommonDevice.DeviceStatusReportData report, CommonDevice device)
        {
            if (report == null)
            {
                return false;
            }
            foreach (var data in report.AttriBute)
            {
                //生产商名字
                if (data.AttributeId == 4)
                {
                    if (data.AttriButeDataHex.Length > 2)
                    {
                        device.ManufacturerName = UserCenterLogic.TranslateHexadecimalIntoText(data.AttriButeDataHex.Substring(2));
                    }
                }
                //型号码(也叫模块ID)
                if (data.AttributeId == 5)
                {
                    if (data.AttriButeDataHex.Length > 2)
                    {
                        device.ModelIdentifier = UserCenterLogic.TranslateHexadecimalIntoText(data.AttriButeDataHex.Substring(2));
                    }
                }
                //生产日期
                if (data.AttributeId == 6)
                {
                    if (data.AttriButeDataHex.Length > 2)
                    {
                        device.ProductionDate = UserCenterLogic.TranslateHexadecimalIntoText(data.AttriButeDataHex.Substring(2));
                    }
                }
                //电源
                if (data.AttributeId == 7)
                {
                    device.PowerSource = data.AttriButeData;
                }
                //序列号
                if (data.AttributeId == 13)
                {
                    if (data.AttriButeDataHex.Length > 2)
                    {
                        device.SerialNumber = UserCenterLogic.TranslateHexadecimalIntoText(data.AttriButeDataHex.Substring(2));
                    }
                }
            }
            return true;
        }
 
        #endregion
 
        #region ■ 获取在线状态_______________________
 
        /// <summary>
        /// 添加读取设备在线状态的事件(之后调用SetOnlineComand发送命令,返回即在线)
        /// </summary>
        /// <param name="mainKeys">标识事件的主键(可以随便填,主要是针对多个界面一起使用的情况)</param>
        /// <param name="action">回调函数</param>
        public void AddReadOnlineEvent(string mainKeys, Action<CommonDevice> action)
        {
            lock (objLock)
            {
                if (this.dicEvent.Count == 0)
                {
                    ZigBee.Device.ZbGateway.StatusList.Add(this);
                }
                this.dicEvent[mainKeys] = action;
                this.dicCommandDiv[mainKeys] = "DeviceStatusReport";
            }
        }
 
        /// <summary>
        /// 发送获取在线状态的命令
        /// </summary>
        /// <param name="device"></param>
        public void SetOnlineComand(CommonDevice device)
        {
            device.ReadAttri(Cluster_ID.Identify, AttriButeId.Switch);
        }
 
        /// <summary>
        /// 添加网关自动推送设备在线状态的事件(不需要发送命令)
        /// </summary>
        /// <param name="mainKeys">标识事件的主键(可以随便填,主要是针对多个界面一起使用的情况)</param>
        /// <param name="action">回调函数</param>
        public void AddReceiveDeviceOnlinePushEvent(string mainKeys, Action<CommonDevice> action)
        {
            lock (objLock)
            {
                if (this.dicEvent.Count == 0)
                {
                    ZigBee.Device.ZbGateway.StatusList.Add(this);
                }
                this.dicEvent[mainKeys] = action;
                this.dicCommandDiv[mainKeys] = "OnlineStatusChange";
            }
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 是否存在指定的事件
        /// </summary>
        /// <param name="mainkeys"></param>
        /// <returns></returns>
        public bool IsEsixt(string mainkeys)
        {
            return this.dicCommandDiv.ContainsKey(mainkeys);
        }
 
        #endregion
 
        #region ■ 保存安防信息_______________________
 
        /// <summary>
        /// 添加保存安防设备报警的事件(不需要再执行任何操作,并且永久存在)
        /// </summary>
        public void AddSaveSafetyAlarmInfoEvent()
        {
            lock (objLock)
            {
                if (this.dicEvent.Count == 0)
                {
                    ZigBee.Device.ZbGateway.StatusList.Add(this);
                }
 
                if (this.dicEvent.ContainsKey("SaveSafetyAlarmInfo") == false)
                {
                    this.dicEvent["SaveSafetyAlarmInfo"] = this.SaveSafetyAlarmInfo;
                    this.dicCommandDiv["SaveSafetyAlarmInfo"] = "IASInfoReport";
                }
            }
        }
 
        /// <summary>
        /// 保存安防信息
        /// </summary>
        /// <param name="device"></param>
        private void SaveSafetyAlarmInfo(CommonDevice device)
        {
            if (Common.LocalGateway.Current.IsGatewayExist(device.CurrentGateWayId) == true)
            {
                //保存安防报警信息到本地
                bool result = Common.LocalSafeguard.Current.SaveSafeguardAlarmInfo(device);
                foreach (var action in this.dicSafetyEvent.Values)
                {
                    action(device, result);
                }
            }
        }
 
        #endregion
 
        #region ■ 实现接口___________________________
 
        /// <summary>
        /// 设备状态通知
        /// </summary>
        /// <param name="common"></param>
        /// <param name="typeTag"></param>
        public void DeviceInfoChange(CommonDevice common, string typeTag)
        {
            lock (objLock)
            {
                if (common == null || string.IsNullOrEmpty(common.DeviceAddr) == true)
                {
                    //我也不知道这有没有可能
                    return;
                }
                var list = new List<Action<CommonDevice>>();
                foreach (string keys in this.dicEvent.Keys)
                {
                    if (this.dicCommandDiv[keys] != typeTag)
                    {
                        //命令区分不一致,则不调用回调函数
                        continue;
                    }
                    //命令区分一致时,则调用回调函数
                    list.Add(this.dicEvent[keys]);
                }
                //有可能在回调函数中移除了事件,导致报错,所以先收集,再调用
                foreach (var action in list)
                {
                    action(common);
                }
            }
        }
 
        /// <summary>
        /// 不使用
        /// </summary>
        /// <param name="common"></param>
        public void Changed(CommonDevice common)
        {
        }
 
        /// <summary>
        /// 不使用
        /// </summary>
        public void ChangedILogicStatus(ZigBee.Device.Logic logic)
        {
        }
 
        /// <summary>
        /// 不使用
        /// </summary>
        public void ChangedISceneStatus(Scene scene)
        {
        }
        #endregion
    }
}