wxr
2020-09-10 af1cb3ecd0f4b0589e00b28f7f9edccf39e6e12b
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;
using HDL_ON;
using Shared;
 
namespace HDL_ON.Entity
{
    /// <summary>
    /// 功能属性
    /// 属性字段解析:attri :属性内容,value 属性的值,max 最大值 min 最小值
    /// </summary>
    public class Trait
    {
        /// <summary>
        /// 属性名称
        /// </summary>
        public string name;
        List<string> _value_key;
        /// <summary>
        /// 属性的值列表
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public List<string> value_key {
            get
            {
                return _value_key;
            }
            set
            {
                _value_key = value;
            }
        }
        /// <summary>
        /// 最大值
        /// </summary>
        public int max;
        /// <summary>
        /// 最小值
        /// </summary>
        public int min;
 
        /// <summary>
        /// 数据类型
        /// </summary>
        public string data_type = "";
 
        /// <summary>
        /// 当前值
        /// </summary>
        public object value = new object();
 
 
        //----app自定义
        /// <summary>
        /// 属性值单位
        /// </summary>
        string _uintString;
        [Newtonsoft.Json.JsonIgnore]
        public string uintString
        {
            get
            {
                if(!string.IsNullOrEmpty( _uintString))
                {
                    return _uintString;
                }
                var us = "";
                switch(name)
                {
                    case "temperature":
                        us = "°C";
                        break;
                    case "percent":
                    case "brightness":
                        us = "%";
                        break;
                }
                return us;
            }
        }
 
        /// <summary>
        /// 属性名称显示文本
        /// 中英文显示
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public string name_text
        {
            get
            {
                string text = "";
                switch (name)
                {
                    case "on_off":
                        text = Language.StringByID(StringId.OnOff);
                        break;
                    case "brightness":
                        text = Language.StringByID(StringId.Brightness);
                        break;
                    case "color":
                        text = Language.StringByID(StringId.ColorValue);
                        break;
                    case "mode":
                        text = Language.StringByID(StringId.Mode);
                        break;
                    case "fan":
                        text = Language.StringByID(StringId.FanSpeed);
                        break;
                    case "temperature":
                        text = Language.StringByID(StringId.Temp);
                        break;
                    case "delay":
                        text = Language.StringByID(StringId.Delay);
                        break;
                    case "cct":
                        text = "CCT";
                        break;
                    //case "fade_time":
                    case "percent":
                        text = Language.StringByID(StringId.PercentAdjustment);
                        break;
                        //case "lock":
                        //case "ico":
                        //case "swing":
                        //case "set_ point":
                        //case "pm25":
                        //case "volume":
                        //case "vol_step":
                        //case "source":
                        //case "treble":
                        //case "bass":
                        //case "playlist":
                        //case "song_name":
                        //case "current_status":
                        //case "enable":
                        //case "lux":
                        //case "adjust_value":
                        //case "range":
                        //case "humidity":
                        //case "type":
                        //case "state":
                        //case "sensitivity":
                        //case "pm25value":
                }
                return text;
            }
        }
 
        /// <summary>
        /// 获取当前属性的显示文本
        /// 中英文显示
        /// </summary>
        public string GetCurValueText()
        {
            return GetValueText(value.ToString());
        }
 
        /// <summary>
        /// 获取指定属性的显示文本
        /// 中英文显示
        /// </summary>
        public string GetValueText(string value)
        {
            string text = "";
            value = value.Replace("{}", "");
            switch (name)
            {
                case "on_off":
                    text = value == "on" ? Language.StringByID(StringId.On) : Language.StringByID(StringId.OFF);
                    break;
                case "temperature":
                case "brightness":
                case "percent":
                    if (value == "")
                    {
                        this.value = this.min;
                        text = this.min.ToString();
                    }
                    else
                    {
                        text = value;
                    }
                    break;
                //case "color":
                case "mode":
                    switch (value)
                    {
                        //----空调
                        case "auto":
                            text = Language.StringByID(StringId.Auto);
                            break;
                        case "cool":
                            text = Language.StringByID(StringId.Cool);
                            break;
                        case "heat":
                            text = Language.StringByID(StringId.Heat);
                            break;
                        case "dry":
                            text = Language.StringByID(StringId.Dry);
                            break;
                        case "fan":
                            text = Language.StringByID(StringId.AirSupply);
                            break;
                        //-----地热
                        case "day":
                            text = Language.StringByID(StringId.Day);
                            break;
                        case "night":
                            text = Language.StringByID(StringId.Night);
                            break;
                        case "away":
                            text = Language.StringByID(StringId.Away);
                            break;
                        case "normal":
                            text = Language.StringByID(StringId.Normal);
                            break;
                        case "timer":
                            text = Language.StringByID(StringId.Timer);
                            break;
                        default:
                            if (this.value_key.Contains("cool"))
                            {
                                this.value = "cool";
                                text = Language.StringByID(StringId.Cool);
                            }
                            else if (this.value_key.Contains("day"))
                            {
                                this.value = "day";
                                text = Language.StringByID(StringId.Day);
                            }
                            else
                            {
                                foreach (var v in this.value_key)
                                {
                                    this.value = v;
                                    text = GetCurValueText();
                                }
                            }
                            break;
                    }
                    break;
                case "fan":
                    switch (value)
                    {
                        case "high":
                            text = Language.StringByID(StringId.HighWindSpeed);
                            break;
                        case "medium":
                            text = Language.StringByID(StringId.MiddleWindSpeed);
                            break;
                        case "low":
                            text = Language.StringByID(StringId.LowWindSpeed);
                            break;
                        case "auto":
                            text = Language.StringByID(StringId.Auto);
                            break;
                        default:
                            if (this.value_key.Contains("low"))
                            {
                                this.value = "low";
                                text = Language.StringByID(StringId.LowWindSpeed);
                            }
                            else
                            {
                                foreach (var v in this.value_key)
                                {
                                    this.value = v;
                                    text = GetCurValueText();
                                }
                            }
                            break;
                    }
                    break;
                //case "delay":
                //case "cct":
                //case "fade_time":
                //case "lock":
                //case "ico":
                //case "swing":
                //case "set_ point":
                //case "pm25":
                //case "volume":
                //case "vol_step":
                //case "source":
                //case "treble":
                //case "bass":
                //case "playlist":
                //case "song_name":
                //case "current_status":
                //case "enable":
                //case "lux":
                //case "adjust_value":
                //case "range":
                //case "humidity":
                //case "type":
                //case "state":
                //case "sensitivity":
                //case "pm25value":
                case "high":
                    text = Language.StringByID(StringId.HighWindSpeed);
                    break;
                case "medium":
                    text = Language.StringByID(StringId.MiddleWindSpeed);
                    break;
                case "low":
                    text = Language.StringByID(StringId.LowWindSpeed);
                    break;
                case "auto":
                    text = Language.StringByID(StringId.Auto);
                    break;
            }
            return text;
        }
    }
    /// <summary>
    /// 设备功能oid
    /// </summary>
    public class FunctionOid
    {
        public string oid;
        public string name;
        public string machine_id;
        public string net_id;
        public string dev_id;
        public string channels;
    }
 
 
    public class Function
    {
        public Function()
        {
        }
        /// <summary>
        /// 数据存储文件名
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public string savePath
        {
            get
            {
                return "FunctionData_" + sid;
            }
        }
 
        #region base info
        /// <summary>
        /// HDL统一协议格式:14bytes
        /// 举例: 来源   厂商代码 通讯方式  产品时间戳   产品类别 物模型类  通道号   大小类别
        //       1byte    1byte    1byte      4byte        1byte    2byte   2byte   2byte
        /// 来源:00    默认原生态系统数据 、01    网关或者其他A设备、02    调试软件、03    APP应用程序、04    第三方网关或者平台
        /// 厂商代码:01    HDL
        /// 通讯方式:01    HDL Bus、02    Zigbee、03 KNX、04 Z-Wave
        /// 产品时间戳:4bytes    以2020年1月1日算出的时间戳0.1s为单位
        /// 产品类别:01    调光器、02 继电器、03    干接点模块、04    传感器、05 面板
        /// 物模型类型:
        ///     01    开关类:01 开关、02 插座、03    
        ///     02    照明: 01 开关、02    调光、03    色温、04    LED
        ///     03    遮阳: 01    窗帘电机、02 百叶窗、03 开合帘、04 卷帘
        ///     04    恒温器:01 空调、02 地暖、03 毛细空调
        ///     05    新风        
        ///     06    影音        
        ///     07    音乐        
        ///     08    能源        
        ///     09    安防
        /// 大类别    1bytes    (预留)
        /// 小类别    1byte    (预留)
        /// </summary>
        public string sid = "0301011234567801012301230123";
 
        /// <summary>
        /// A协议功能的特性
        /// 如:是AC功能:特性:on_off,mode,fan,temperature
        /// attri
        /// </summary>
        public List<Trait> function = new List<Trait>();
 
        /// <summary>
        /// 房间ID列表
        /// 该功能添加到到房间列表
        /// </summary>
        public List<string> roomIdList = new List<string>();
 
        /// <summary>
        /// bus协议数据格式
        /// 使用A协议控制时,改属性为空
        /// </summary>
        public BusData bus_Data;
        /// <summary>
        /// 延时
        /// </summary>
        public int delay = 0;
        /// <summary>
        /// 延时显示的文本
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public string delayText
        {
            get
            {
                string text = "";
                switch (delay)
                {
                    case 0:
                        text = Language.StringByID(StringId.NoDelay);
                        break;
                    case 30:
                        text = "30s";
                        break;
                    case 60:
                        text = "1min";
                        break;
                    case 120:
                        text = "2min";
                        break;
                    case 300:
                        text = "5min";
                        break;
                }
                return text;
            }
        }
        #endregion
 
        /// <summary>
        /// 功能类别
        /// 如:空调类、灯光类、窗帘类
        /// </summary>
        public FunctionCategory functionCategory
        {
            get
            {
                try
                {
                    var _functionCategoryString = sid.Substring(16, 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(16, 4);
                return (FunctionType)Enum.ToObject(typeof(FunctionType), Convert.ToInt32(_functionTypeString, 16));
            }
        }
 
        /// <summary>
        /// 备注
        /// </summary>
        public string name;
 
        /// <summary>
        /// 最后控制的一次状态
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public string lastState = "";
 
        Trait _trait_on_off;
        [Newtonsoft.Json.JsonIgnore]
        public Trait trait_on_off
        {
            get
            {
                if (_trait_on_off == null)
                {
                    _trait_on_off = function.Find((obj) => obj.name == "on_off");
                    //找不到属性需要声明一个,防止报错闪退
                    if (_trait_on_off == null)
                    {
                        _trait_on_off = new Trait()
                        {
                            name = "on_off",
                            value_key = new List<string> { "on", "off" },
                            max = 1,
                            min = 0,
                        };
                    }
                    _trait_on_off.value = "on";
                }
                return _trait_on_off;
            }
            //set
            //{
            //    _trait_on_off = value;
            //}
        }
 
        /// <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;
 
        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.rooms.Find(obj => obj.sid == roomId);
                if (findRoom == null)
                    continue;
                if(roomNameList != "")
                {
                    roomNameList += ",";
                }
                roomNameList += findRoom.floorName + findRoom.name;
            }
            if (roomNameList == "" && functionType == FunctionType.Scene)
            {
                roomNameList = Language.StringByID(StringId.WholeHouseScene);
            }
            return roomNameList;
        }
        /// <summary>
        /// 保存功能数据
        /// </summary>
        public void SaveFunctionData()
        {
            var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
            FileUtils.WriteFileByBytes("FunctionData_" + this.sid, ssd);
            MainPage.Log($"Save FunctionData {this.functionType} : {this.sid}");
        }
 
        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime refreshTime = DateTime.MinValue;
    }
}