wxr
2020-05-11 7f233a10afa6882b4d9531185af60e8843bc1af2
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
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;
using HDL_ON;
 
namespace HDL_ON.Entity
{
    /// <summary>
    /// 功能属性
    /// 属性字段解析:attri :属性内容,value 属性的值,max 最大值 min 最小值
    /// </summary>
    public class Trait
    {
        /// <summary>
        /// 属性内容
        /// </summary>
        public string attri;
        /// <summary>
        /// 属性的值
        /// </summary>
        public List<string> value;
        /// <summary>
        /// 最大值
        /// </summary>
        public int max;
        /// <summary>
        /// 最小值
        /// </summary>
        public int min;
 
        /// <summary>
        /// 当前值
        /// app软件自定义属性
        /// 用来记录当前状态
        /// </summary>
        public object curValues;
    }
 
    public class Function
    {
        /*
        HDL统一协议格式:16bytes
        厂商代号:2bytes
        通讯方式:有线/无线/ZIGBEE等1bytes
        产品识别码:以添加设备的年月日时分秒 + 随机数  8bytes  Category
        功能类别:1bytes
        数据表索引地址:2bytes
        回路:2bytes
        举例: 0001-00-2019101209001234-01-0001-0001
        */
        public Function()
        {
            //dicPropert = new Dictionary<string, string>();
            //if (propertList == null || propertList.Count == 0)
            //{
            //    foreach (var t in propertList)
            //    {
            //        dicPropert.TryAdd(t.attri,t.min.ToString());
            //    }
            //}
        }
        #region base info
        /// <summary>
        /// 功能ID
        /// 厂商代码    2bytes;     01    HDL
        /// 通讯方式    1byte ;     01    HDL Bus; 02    Zigbee;03    KNX;04    Z-Wave
        /// 产品GUID    8bytes
        /// 产品类别    1byte ;     01  调光器;02继电器;03干接点模块;04传感器;05面板
        /// 物模型类型2bytes      01 开关类    --01开关;02插座
        ///                        02    照明     --01开关类;02调光类;03色温;04LED
        ///                        03    遮阳     --01窗帘电机;02百叶窗;03开合帘;04卷帘
        ///                        04    恒温器    --01空调;02地暖;03毛细空调
        ///                     05    新风        
        ///                     06    影音        
        ///                     07    音乐        
        ///                        08    能源        
        ///                        09    安防
        /// 通道号    2bytes
        /// Key_id  2bytes
        /// </summary>
        public string sid = "00010112345678901234560101230123AABB";
 
        /// <summary>
        /// A协议功能的特性
        /// 如:是AC功能:特性:on_off,mode,fan,temperature
        /// attri
        /// </summary>
        public List<Trait> propertList = new List<Trait>();
 
        /// <summary>
        /// 房间ID列表
        /// 该功能添加到到房间列表
        /// </summary>
        public List<string> roomIdList = new List<string>();
 
        /// <summary>
        /// bus协议数据格式
        /// 使用A协议控制时,改属性为空
        /// </summary>
        public BusData bus_Data;
        #endregion
 
 
        ///// <summary>
        ///// 功能附带的属性与值的列表
        ///// </summary>
        //public Dictionary<string, string> dicPropert;
 
        /// <summary>
        /// 功能类别
        /// 如:空调类、灯光类、窗帘类
        /// </summary>
        public FunctionCategory functionCategory
        {
            get
            {
                try
                {
                    var _functionCategoryString = sid.Substring(24, 2);
                    var _functionCategory = Convert.ToInt32(_functionCategoryString, 16);
                    return (FunctionCategory)Enum.ToObject(typeof(FunctionCategory), _functionCategory);
                }
                catch (Exception ex)
                {
                    MainPage.Log($"get FunctionCategory error : {ex.Message}");
                    return FunctionCategory.UnKown;
                }
            }
        }
 
        public FunctionType functionType
        {
            get
            {
                var _functionTypeString = sid.Substring(24, 4);
                return (FunctionType)Enum.ToObject(typeof(FunctionType), Convert.ToInt32(_functionTypeString, 16));
            }
        }
 
        /// <summary>
        /// 备注
        /// </summary>
        public string name;
        /// <summary>
        /// A协议厂商代码
        /// </summary>
        public string vendor_code
        {
            get
            {
                string vendorCode = "HDL";
                var code = sid.Substring(0, 4);
                if (code == "0001")
                    vendorCode = "HDL";
                return vendorCode;
            }
        }
 
        /// <summary>
        /// 最后控制的一次状态
        /// </summary>
        public string lastState = "";
 
        [Newtonsoft.Json.JsonIgnore]
        public Trait trait_on_off;
        /// <summary>
        /// 开关状态
        /// 0:关
        /// 1:开
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public string on_off
        {
            get
            {
                try
                {
                    if (trait_on_off == null)
                    {
                        trait_on_off = propertList.Find((obj) => obj.attri == "on_off");
                        //找不到属性需要声明一个,防止报错闪退
                        if (trait_on_off == null)
                        {
                            trait_on_off = new Trait()
                            {
                                attri = "on_off",
                                value = new List<string> { "on", "off" },
                                max = 1,
                                min = 0,
                            };
                        }
                        trait_on_off.curValues = trait_on_off.value[trait_on_off.min];
                    }
                    return trait_on_off.curValues.ToString();
                }
                catch
                {
                    MainPage.Log("on_off 数据获取失败.");
                    propertList.Add( new Trait()
                    {
                        attri = "on_off",
                        value = new List<string> { "on", "off" },
                        max = 1,
                        min = 0,
                    });
                    return "off";
                }
            }
            set
            {
                try
                {
                    if (trait_on_off == null)
                    {
                        trait_on_off = propertList.Find((obj) => obj.attri == "on_off");
                        //找不到属性需要声明一个,防止报错闪退
                        if (trait_on_off == null)
                        {
                            trait_on_off = new Trait()
                            {
                                attri = "on_off",
                                value = new List<string> { "on", "off" },
                                max = 1,
                                min = 0,
                            };
                        }
                        trait_on_off.curValues = trait_on_off.min;
                    }
                    trait_on_off.curValues = value;
                }
                catch (Exception ex)
                {
                    MainPage.Log($"on_off 数据刷新失败: {ex.Message}");
                    propertList.Add(new Trait()
                    {
                        attri = "on_off",
                        value = new List<string> { "on", "off" },
                        max = 1,
                        min = 0,
                    });
                }
            }
        }
 
        /// <summary>
        /// 是否收藏
        /// </summary>
        public bool collection = false;
 
        /// <summary>
        /// 使用次数
        /// </summary>
        public double usageCount = 0;
        /// <summary>
        /// 使用频率
        /// </summary>
        public double usageFrequency {
            get
            {
                return usageCount / 7;
            }
        }
        /// <summary>
        /// 固定的序号
        /// </summary>
        public int fixedSerialNumber = int.MaxValue;
 
        /// <summary>
        /// 获取A协议控制字符串
        /// </summary>
        /// <param name="command">控制命令:write,read</param>
        /// <returns></returns>
        public virtual JObject GetSendJObject(CommandType_A command)
        {
            var sendJob = new JObject { { "vendor_code", vendor_code }, { "Command", command.ToString() }, { "Type", "device" }, };
            var data = new JObject { { "sid", sid } };
            sendJob.Add("objects", data);
            return sendJob;
        }
 
        public string GetBusId ()
        {
            string busId = "";
            if (bus_Data != null)
            {
                busId = bus_Data.SubnetID + "_" + bus_Data.DeviceID + "_" + bus_Data.LoopID;
            }
            return busId;
        }
 
        /// <summary>
        /// 获取设备添加到房间的房间名称
        /// </summary>
        /// <returns></returns>
        public string GetRoomListName()
        {
            string roomNameList = "";
            foreach(var roomId in roomIdList)
            {
                var findRoom = DB_ResidenceData.residenceData.rooms.Find(obj => obj.sid == roomId);
                if (findRoom == null)
                    continue;
                if(roomNameList != "")
                {
                    roomNameList += ",";
                }
                roomNameList += findRoom.floor + "·" + findRoom.name;
            }
            return roomNameList;
        }
 
 
        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime refreshTime = DateTime.MinValue;
    }
}