黄学彪
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
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
 
namespace Shared.Phone.UserCenter
{
    /// <summary>
    /// 设备镜像的逻辑
    /// </summary>
    public class HdlDeviceImageInfoLogic
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 设备镜像的逻辑
        /// </summary>
        private static HdlDeviceImageInfoLogic m_Current = null;
        /// <summary>
        /// 设备镜像的逻辑
        /// </summary>
        public static HdlDeviceImageInfoLogic Current
        {
            get
            {
                if (m_Current == null)
                {
                    m_Current = new HdlDeviceImageInfoLogic();
                }
                return m_Current;
            }
        }
 
        /// <summary>
        /// OTA设备获取镜像后的回调函数
        /// </summary>
        private Dictionary<string, Action<CommonDevice, CommonDevice.DeviceStatusReportData>> dicOtaBackAction = new Dictionary<string, Action<CommonDevice, CommonDevice.DeviceStatusReportData>>();
 
        #endregion
 
        #region ■ 主入口函数_________________________
 
        /// <summary>
        /// 设置设备全部的镜像信息
        /// </summary>
        /// <param name="tADevice"></param>
        /// <param name="backAction"></param>
        public void SetAllImageInfoToOtaDevice(OTADevice tADevice, Action<CommonDevice, CommonDevice.DeviceStatusReportData> backAction = null)
        {
            if (tADevice == null)
            {
                return;
            }
            lock (dicOtaBackAction)
            {
                //先移除
                this.RemoveDeviceFirmwareVersionThread(tADevice);
 
                if (backAction != null)
                {
                    this.dicOtaBackAction[tADevice.DeviceAddr] = backAction;
                }
                //设置设备的固件版本号(需要等待推送后才会更改)
                this.SetFirmwareVersionToOtaDevice(tADevice);
            }
        }
 
        /// <summary>
        /// 设置设备的固件版本号(需要等待推送后才会更改)
        /// </summary>
        /// <param name="tADevice"></param>
        private void SetFirmwareVersionToOtaDevice(OTADevice tADevice)
        {
            if (HdlGatewayReceiveLogic.Current.IsEsixt("DeviceAutoGetFirmwareVersion") == false)
            {
                //添加事件
                HdlGatewayReceiveLogic.Current.AddAttributeEvent("DeviceAutoGetFirmwareVersion", ReceiveComandDiv.A设备属性上报, this.SetFirmwareVersionByInterfaceResult);
            }
            //发送命令
            this.SetFirmwareVersionComand(tADevice);
        }
 
        #endregion
 
        #region ■ 发送命令___________________________
 
        /// <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());
        }
 
        #endregion
 
        #region ■ 设置属性___________________________
 
        /// <summary>
        /// 设置设备的固件版本   -1:异常  0:推送的这个东西并不是固件信息  1:正常
        /// </summary>
        /// <param name="report">上报信息</param>
        /// <param name="device">需要修改的设备对象</param>
        /// <returns></returns>
        public int SetFirmwareVersion(CommonDevice.DeviceStatusReportData report, CommonDevice device)
        {
            if (report == null)
            {
                return -1;
            }
 
            //是否是正确的推送数据
            bool isRightData = false;
            foreach (var data in report.AttriBute)
            {
                //镜像版本
                if (data.AttributeId == (int)AttriButeId.ImgVersion)
                {
                    isRightData = true;
                    device.ImgVersion = data.AttriButeData;
                }
                //硬件版本
                if (data.AttributeId == (int)AttriButeId.mgHWversion)
                {
                    isRightData = true;
                    device.HwVersion = data.AttriButeData;
                }
                //镜像ID
                if (data.AttributeId == (int)AttriButeId.ImgTypeId)
                {
                    isRightData = true;
                    device.ImgTypeId = data.AttriButeData;
                }
            }
            if (isRightData == false)
            {
                //这个不是固件信息的推送
                return 0;
            }
            return 1;
        }
 
        #endregion
 
        #region ■ 接口推送处理_______________________
 
        /// <summary>
        /// 根据接口推送的信息,设置固件版本号
        /// </summary>
        /// <param name="device"></param>
        private void SetFirmwareVersionByInterfaceResult(CommonDevice device)
        {
            var otaDevice = Common.LocalDevice.Current.GetOTADevice(device.DeviceAddr, device.DeviceEpoint);
            if (otaDevice != null)
            {
                //设置固件版本信息
                this.SetFirmwareVersion(device.DeviceStatusReport, otaDevice);
                otaDevice.ReSave();
            }
            lock (dicOtaBackAction)
            {
                if (this.dicOtaBackAction.ContainsKey(device.DeviceAddr) == true)
                {
                    var action = this.dicOtaBackAction[device.DeviceAddr];
                    //调用回调函数
                    action?.Invoke(device, device.DeviceStatusReport);
                    //然后移除
                    this.dicOtaBackAction.Remove(device.DeviceAddr);
                    action = null;
                }
            }
        }
        #endregion
 
        #region ■ 移除监听线程_______________________
 
        /// <summary>
        /// 移除获取设备固件信息的监听线程
        /// </summary>
        /// <param name="device"></param>
        public void RemoveDeviceFirmwareVersionThread(CommonDevice device)
        {
            lock (dicOtaBackAction)
            {
                if (this.dicOtaBackAction.ContainsKey(device.DeviceAddr) == true)
                {
                    var action = this.dicOtaBackAction[device.DeviceAddr];
                    //然后移除
                    this.dicOtaBackAction.Remove(device.DeviceAddr);
                    action = null;
                }
            }
        }
 
        #endregion
    }
}