黄学彪
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
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
 
namespace Shared.Phone.UserCenter
{
    /// <summary>
    /// 设备固定信息的逻辑
    /// </summary>
    public class HdlDeviceFixedAttributeLogic
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 设备固定信息的逻辑
        /// </summary>
        private static HdlDeviceFixedAttributeLogic m_Current = null;
        /// <summary>
        /// 设备固定信息的逻辑
        /// </summary>
        public static HdlDeviceFixedAttributeLogic Current
        {
            get
            {
                if (m_Current == null)
                {
                    m_Current = new HdlDeviceFixedAttributeLogic();
                }
                return m_Current;
            }
        }
 
        /// 需要获取固定属性的对象设备
        /// </summary>
        private HashSet<string> hsGetHardInfoDevice = new HashSet<string>();
 
        #endregion
 
        #region ■ 主入口函数_________________________
 
        /// <summary>
        /// 读取以及设置设备固定属性
        /// </summary>
        /// <param name="device">设备回路</param>
        public void SetAllFixedAttributeToDevice(CommonDevice device)
        {
            if (device == null)
            {
                return;
            }
            lock (this.hsGetHardInfoDevice)
            {
                //先移除
                string mainKeys = Common.LocalDevice.Current.GetDeviceMainKeys(device);
                if (this.hsGetHardInfoDevice.Contains(mainKeys) == true)
                {
                    this.hsGetHardInfoDevice.Remove(mainKeys);
                }
                if (HdlGatewayReceiveLogic.Current.IsEsixt("DeviceGetFixedAttribute") == false)
                {
                    //添加事件
                    HdlGatewayReceiveLogic.Current.AddAttributeEvent("DeviceGetFixedAttribute",  ReceiveComandDiv.A设备属性上报, this.SetFixedAttributeByInterfaceResult);
                }
                //发送命令
                this.SeFixedAttributeComand(device);
            }
        }
 
        #endregion
 
        #region ■ 发送命令___________________________
 
        /// <summary>
        /// 发送获取固定属性的命令
        /// </summary>
        /// <param name="device"></param>
        private void SeFixedAttributeComand(CommonDevice device)
        {
            Newtonsoft.Json.Linq.JObject jObject = null;
            Newtonsoft.Json.Linq.JArray attriBute = null;
            //窗帘
            if (device.Type == DeviceType.WindowCoveringDevice)
            {
                this.GetCurtainComand(device, ref jObject, ref attriBute);
            }
            if (jObject == null)
            {
                //不需要发送
                return;
            }
            string mainkeys = Common.LocalDevice.Current.GetDeviceMainKeys(device);
            this.hsGetHardInfoDevice.Add(mainkeys);
            //发送
            var data = new Newtonsoft.Json.Linq.JObject { { "AttriBute", attriBute } };
            jObject.Add("Data", data);
            device.Gateway?.Send(("GetDeviceStatus"), jObject.ToString());
        }
 
        #endregion
 
        #region ■ 获取窗帘命令_______________________
 
        /// <summary>
        /// 获取窗帘命令
        /// </summary>
        /// <param name="device">设备对象</param>
        /// <param name="jObject">标题数据</param>
        /// <param name="attriBute">属性数据</param>
        private void GetCurtainComand(CommonDevice device, ref Newtonsoft.Json.Linq.JObject jObject, ref Newtonsoft.Json.Linq.JArray attriBute)
        {
            jObject = new Newtonsoft.Json.Linq.JObject
            {
               { "DeviceAddr",device.DeviceAddr },
               { "Epoint", device.DeviceEpoint },
               { "Cluster_ID", (int)Cluster_ID.WindowCovering },
               { "Command", 108 }
            };
            attriBute = new Newtonsoft.Json.Linq.JArray
            {
               new Newtonsoft.Json.Linq.JObject
               {
                  { "AttriButeId", (int)AttriButeId.WindowCoveringType }
               }
            };
        }
 
        #endregion
 
        #region ■ 设置属性(主函数)___________________
 
        /// <summary>
        /// 设置设备的固定属性   -1:异常  0:推送的这个东西并不是需要的  1:正常
        /// </summary>
        /// <param name="report">上报信息</param>
        /// <param name="device">设备对象</param>
        /// <returns></returns>
        private int SetFixedAttribute(CommonDevice.DeviceStatusReportData report, CommonDevice device)
        {
            if (report == null)
            {
                return -1;
            }
            //窗帘
            if (device.Type == DeviceType.WindowCoveringDevice)
            {
                return this.SetCurtainAttribute(report, (Rollershade)device);
            }
            return 0;
        }
 
        #endregion
 
        #region ■ 设置窗帘属性_______________________
 
        /// <summary>
        /// 设置窗帘属性  -1:异常  0:推送的这个东西并不是需要的  1:正常
        /// </summary>
        /// <param name="report">上报信息</param>
        /// <param name="device">设备对象</param>
        /// <returns></returns>
        private int SetCurtainAttribute(CommonDevice.DeviceStatusReportData report, Rollershade device)
        {
            if (report.CluterID != (int)Cluster_ID.WindowCovering || device == null)
            {
                return -1;
            }
            //属性是否改变
            bool AttriButeChanged = false;
            int attriButeId = (int)AttriButeId.WindowCoveringType;
            foreach (var data in report.AttriBute)
            {
                if (data.AttributeId == attriButeId)
                {
                    AttriButeChanged = true;
                    device.WcdType = data.AttriButeData;
                }
            }
            if (AttriButeChanged == true)
            {
                //属性已经改变,则保存
                return 1;
            }
            return 0;
        }
 
        #endregion
 
        #region ■ 接口推送处理_______________________
 
        /// <summary>
        /// 根据接口推送的信息,设置设备硬件信息
        /// </summary>
        /// <param name="device"></param>
        private void SetFixedAttributeByInterfaceResult(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.SetFixedAttribute(device.DeviceStatusReport, localDevice) != 1)
                {
                    return;
                }
                //保存
                localDevice.ReSave();
                //移除
                this.hsGetHardInfoDevice.Remove(mainKeys);
            }
        }
 
        #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);
                }
            }
        }
 
        #endregion
    }
}