黄学彪
2020-09-22 ade5917841b0fdcb1df7353ef7c56b1a1bdc9282
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
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
 
namespace Shared.Phone.UserCenter
{
    /// <summary>
    /// 设备硬件信息的逻辑
    /// </summary>
    public class HdlDeviceHardInfoLogic
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 设备硬件信息的逻辑
        /// </summary>
        private static HdlDeviceHardInfoLogic m_Current = null;
        /// <summary>
        /// 设备硬件信息的逻辑
        /// </summary>
        public static HdlDeviceHardInfoLogic Current
        {
            get
            {
                if (m_Current == null)
                {
                    m_Current = new HdlDeviceHardInfoLogic();
                }
                return m_Current;
            }
        }
 
        /// <summary>
        /// 设备获取硬件信息后的回调函数
        /// </summary>
        private Dictionary<string, Action<CommonDevice, CommonDevice.DeviceStatusReportData>> dicDeviceHardInfoBackAction = new Dictionary<string, Action<CommonDevice, CommonDevice.DeviceStatusReportData>>();
        /// 获取硬件信息的对象设备
        /// </summary>
        private HashSet<string> hsGetHardInfoDevice = new HashSet<string>();
 
        #endregion
 
        #region ■ 主入口函数_________________________
 
        /// <summary>
        /// 读取以及设置设备硬件信息
        /// </summary>
        /// <param name="device">设备回路</param>
        /// <param name="backAction">回调函数</param>
        public void SetAllHardFirmwareInfoToDevice(CommonDevice device, Action<CommonDevice, CommonDevice.DeviceStatusReportData> backAction = null)
        {
            if (device == null)
            {
                return;
            }
            lock (hsGetHardInfoDevice)
            {
                //先移除
                this.RemoveDeviceHardInfoThread(device);
 
                string mainkeys = Common.LocalDevice.Current.GetDeviceMainKeys(device);
                this.hsGetHardInfoDevice.Add(mainkeys);
 
                if (backAction != null)
                {
                    //回调函数
                    this.dicDeviceHardInfoBackAction[mainkeys] = backAction;
                }
                //设置设备的硬件信息(需要等待推送后才会更改)
                this.SetHardFirmwareInfoToDevice(device);
            }
        }
 
        /// <summary>
        /// 设置设备的硬件信息(需要等待推送后才会更改)
        /// </summary>
        /// <param name="device"></param>
        private void SetHardFirmwareInfoToDevice(CommonDevice device)
        {
            if (HdlGatewayReceiveLogic.Current.IsEsixt("DeviceGetHardFirmwareInfo") == false)
            {
                //添加事件
                HdlGatewayReceiveLogic.Current.AddAttributeEvent("DeviceGetHardFirmwareInfo", ReceiveComandDiv.A设备属性上报, this.SetHardFirmwareInfoByInterfaceResult);
            }
            //发送命令
            this.SetHardFirmwareInfoComand(device);
        }
 
        #endregion
 
        #region ■ 发送命令___________________________
 
        /// <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());
        }
 
        #endregion
 
        #region ■ 设置属性___________________________
 
        /// <summary>
        /// 设置设备的硬件信息 -1:异常  0:推送的这个东西并不是硬件信息  1:正常
        /// </summary>
        /// <param name="report">上报信息</param>
        /// <param name="device">设备对象</param>
        /// <returns></returns>
        public int SetHardFirmwareInfo(CommonDevice.DeviceStatusReportData report, CommonDevice device)
        {
            if (report == null)
            {
                return -1;
            }
            //属性是否改变
            bool AttriButeChanged = false;
            //是否是正确的推送数据
            bool isRightData = false;
            foreach (var data in report.AttriBute)
            {
                //生产商名字
                if (data.AttributeId == 4)
                {
                    isRightData = true;
                    if (data.AttriButeDataHex.Length > 2)
                    {
                        var value = UserCenterLogic.TranslateHexadecimalIntoText(data.AttriButeDataHex.Substring(2));
                        if (device.ManufacturerName != value)
                        {
                            //属性变更了
                            AttriButeChanged = true;
                        }
                        device.ManufacturerName = value;
                    }
                }
                //型号码(也叫模块ID)
                if (data.AttributeId == 5)
                {
                    isRightData = true;
                    if (data.AttriButeDataHex.Length > 2)
                    {
                        var value = UserCenterLogic.TranslateHexadecimalIntoText(data.AttriButeDataHex.Substring(2));
                        if (device.ModelIdentifier != value)
                        {
                            //属性变更了
                            AttriButeChanged = true;
                        }
                        device.ModelIdentifier = value;
                    }
                }
                //生产日期
                if (data.AttributeId == 6)
                {
                    isRightData = true;
                    if (data.AttriButeDataHex.Length > 2)
                    {
                        var value = UserCenterLogic.TranslateHexadecimalIntoText(data.AttriButeDataHex.Substring(2));
                        if (device.ProductionDate != value)
                        {
                            //属性变更了
                            AttriButeChanged = true;
                        }
                        device.ProductionDate = value;
                    }
                }
                //电源
                if (data.AttributeId == 7)
                {
                    isRightData = true;
                    device.PowerSource = data.AttriButeData;
                }
                //序列号
                if (data.AttributeId == 13)
                {
                    isRightData = true;
                    if (data.AttriButeDataHex.Length > 2)
                    {
                        string value;
                        if (Common.LocalDevice.Current.IsHdlDevice(device) == false)
                        {
                            //第三方设备
                            value = data.AttriButeDataHex.Substring(2);
                        }
                        else
                        {
                            //河东设备
                            value = UserCenterLogic.TranslateHexadecimalIntoText(data.AttriButeDataHex.Substring(2));
                        }
                        if (device.SerialNumber != value)
                        {
                            //属性变更了
                            AttriButeChanged = true;
                        }
                        device.SerialNumber = value;
                    }
                }
            }
            if (isRightData == false)
            {
                //这个不是硬件信息的推送
                return 0;
            }
 
            //如果属性变更了
            if (AttriButeChanged == true)
            {
                if (device.IsCustomizeImage == false)
                {
                    //UI重新生成
                    device.IconPath = string.Empty;
                    device.ReSave();
                }
            }
            return 1;
        }
 
        #endregion
 
        #region ■ 接口推送处理_______________________
 
        /// <summary>
        /// 根据接口推送的信息,设置设备硬件信息
        /// </summary>
        /// <param name="device"></param>
        private void SetHardFirmwareInfoByInterfaceResult(CommonDevice device)
        {
            string mainKeys = Common.LocalDevice.Current.GetDeviceMainKeys(device);
            if (this.hsGetHardInfoDevice.Contains(mainKeys) == false)
            {
                //推送的这个东西并不是指定的设备
                return;
            }
            //设置设备硬件信息
            var localDevice = Common.LocalDevice.Current.GetDevice(mainKeys);
            if (localDevice == null)
            {
                return;
            }
            lock (hsGetHardInfoDevice)
            {
                //-1:异常  0:推送的这个东西并不是硬件信息  1:正常
                if (this.SetHardFirmwareInfo(device.DeviceStatusReport, localDevice) != 1)
                {
                    return;
                }
                localDevice.ReSave();
 
                this.hsGetHardInfoDevice.Remove(mainKeys);
                if (this.dicDeviceHardInfoBackAction.ContainsKey(mainKeys) == true)
                {
                    var action = this.dicDeviceHardInfoBackAction[mainKeys];
                    //调用回调函数
                    action?.Invoke(device, device.DeviceStatusReport);
                    //然后移除
                    this.dicDeviceHardInfoBackAction.Remove(mainKeys);
                    action = null;
                }
            }
        }
 
        #endregion
 
        #region ■ 移除监听线程_______________________
 
        /// <summary>
        /// 移除获取设备硬件信息的监听线程
        /// </summary>
        /// <param name="device"></param>
        public void RemoveDeviceHardInfoThread(CommonDevice device)
        {
            lock (hsGetHardInfoDevice)
            {
                string mainKeys = Common.LocalDevice.Current.GetDeviceMainKeys(device);
                if (this.hsGetHardInfoDevice.Contains(mainKeys) == true)
                {
                    this.hsGetHardInfoDevice.Remove(mainKeys);
                }
                if (this.dicDeviceHardInfoBackAction.ContainsKey(mainKeys) == true)
                {
                    var action = this.dicDeviceHardInfoBackAction[mainKeys];
                    //然后移除
                    this.dicDeviceHardInfoBackAction.Remove(mainKeys);
                    action = null;
                }
            }
        }
 
        #endregion
    }
}