wei
2021-05-10 24068547ed1396034f56c7bd34ecbd2891f00653
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
using System;
using System.Collections.Generic;
using HDL_ON.Entity;
using Shared;
 
namespace HDL_ON.UI.UI2.Intelligence.Automation
{
 
    public class LogicMethod
    {
        /// <summary>
        /// 表示是条件
        /// </summary>
        public const string condition_if = "条件";
        /// <summary>
        /// 表示是目标
        /// </summary>
        public const string target_if = "目标";
        /// <summary>
        /// 移除所有"Logic"界面
        /// </summary>
        public static void RemoveAllView()
        {
            MainPage.BasePageView.RemoveViewByTag("Logic");
        }
       
        /// <summary> Converts an array of bytes into a formatted string of hex digits (ex: E4 CA B2)</summary>
        /// <param name="data"> The array of bytes to be translated into a string of hex digits. </param>
        /// <returns> Returns a well formatted string of hex digits with spacing. </returns>
        static string byteArrayToHexString(byte[] data)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            foreach (byte b in data)
            {
                sb.Append(Convert.ToString(b, 16).PadLeft(2, '0'));
            }
 
            return sb.ToString().ToUpper();
        }
 
        /**
  * int转byte[]
  * 该方法将一个int类型的数据转换为byte[]形式,因为int为32bit,而byte为8bit所以在进行类型转换时,知会获取低8位,
  * 丢弃高24位。通过位移的方式,将32bit的数据转换成4个8bit的数据。注意 &0xff,在这当中,&0xff简单理解为一把剪刀,
  * 将想要获取的8位数据截取出来。
  * @param i 一个int数字
  * @return byte[]
  */
        public static byte[] int2ByteArray(int i)
        {
            byte[] result = new byte[4];
            result[0] = (byte)((i >> 24) & 0xFF);
            result[1] = (byte)((i >> 16) & 0xFF);
            result[2] = (byte)((i >> 8) & 0xFF);
            result[3] = (byte)(i & 0xFF);
            return result;
        }
        /// <summary>
        /// 获取时间戳
        /// </summary>
        /// <returns></returns>
        static int getTimeStamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return (int)ts.TotalSeconds;
        }
        /// <summary>
        /// 生成逻辑sid方法
        /// </summary>
        public static string NewSid()
        {
            string logicId = "";
            try
            {
                string sOidBeginsWith = "000101";//厂商 + 通讯方式
                DateTime dt = DateTime.Now;
                DateTime startTime = TimeZoneInfo.ConvertTimeToUtc(new DateTime(2020, 1, 1));
                long m = (long)((dt - startTime).TotalMilliseconds / 10);
                string sTimeSpan = byteArrayToHexString(int2ByteArray(getTimeStamp()));
 
 
 
                logicId = sOidBeginsWith + sTimeSpan;
 
                logicId += "15";
                logicId += "1501";
                //1501 物模型为逻辑, 0001 表示 1 号逻辑功能
                int maxId = 1;
 
 
                for (int i = 0; i < Logic.LogicList.Count; i++)
                {
                    string s = Logic.LogicList[i].sid.Substring(20, 4);
                    int iThisSceneId = Convert.ToInt16(s, 16);
                    if (iThisSceneId > maxId)
                        maxId = iThisSceneId;
                }
 
                logicId += (maxId + 1).ToString("X4");//逻辑号 两个byte 
                logicId += "0000";
            }
            catch
            {
                return logicId;
            }
            return logicId;
        }
        /// <summary>
        /// 封装Dictionary对象
        /// </summary>
        /// <param name="dic">Dictionary类</param>
        /// <param name="key">健</param>
        /// <param name="value">值</param>
        public static void dictionary(Dictionary<string, string> dic, string key, string value)
        {
            if (dic.ContainsKey(key)) //判断是否存在键值
            {
                //键存在移除
                dic.Remove(key);
            }
            //添加键值
            dic.Add(key, value);
        }
        /// <summary>
        /// 获取网关房间列表
        /// </summary>
        /// <returns></returns>
        public static List<HDL_ON.Entity.Room> GetGatewayRoomList()
        {
            return HDL_ON.Entity.SpatialInfo.CurrentSpatial.RoomList;
        }
        /// <summary>
        /// 获取网关房间列表
        /// </summary>
        /// <returns></returns>
        public static List<HDL_ON.Entity.Room> GetGatewayRoomList(string name)
        {
            List<Entity.Room> roomList = new List<Entity.Room>();
            Entity.Room room1 = new Entity.Room();
            room1.roomName = name;//自定义默认一个房间名为:6688
            room1.roomId = "6688";//自定义默认id用识别该房间
            roomList.Add(room1);//默认添加到房间列表里
            var roomLists = GetGatewayRoomList();
            for (int i = 0; i < roomLists.Count; i++)
            {
                var room = roomLists[i];
                var devlist = GetRoomDevice(room);
                if (devlist.Count == 0)
                {
                    //过滤掉没有设备的房间;
                    continue;
                }
                roomList.Add(room);
            }
            return roomList;
        }
        /// <summary>
        /// 获取网关设备列表
        /// </summary>
        /// <returns></returns>
        public static List<HDL_ON.Entity.Function> GetGatewayDeviceList()
        {
            return Entity.FunctionList.List.GetDeviceFunctionList();
        }
        /// <summary>
        /// 获取网关场景列表
        /// </summary>
        /// <returns></returns>
        public static List<HDL_ON.Entity.Scene> GetSceneList()
        {
            return HDL_ON.Entity.FunctionList.List.scenes;
        }
        /// <summary>
        /// 获取房间的设备列表
        /// </summary>
        /// <param name="room">当前房间</param>
        /// <returns></returns>
        public static List<HDL_ON.Entity.Function> GetRoomDevice(HDL_ON.Entity.Room room)
        {
            List<HDL_ON.Entity.Function> deviceLists = new List<Entity.Function>();
            List<HDL_ON.Entity.Function> lists = GetGatewayDeviceList();
            if (room.roomId == "6688")
            {
                //默认一个房间名:6688
                //显示网关全部设备
                deviceLists = lists;
            }
            else
            {
                for (int i = 0; i < lists.Count; i++)
                {
                    var dev = lists[i];
                    if (dev.roomIds.Find((id) => id == room.roomId) != null)
                    {
                        //添加属于这个房间的设备;
                        deviceLists.Add(dev);
                    }
 
                }
            }
            return deviceLists;
        }
        /// <summary>
        /// 获取当个设备
        /// </summary>
        /// <param name="sid">设备唯一标识</param>
        /// <returns></returns>
        public static HDL_ON.Entity.Function GetDevice(string sid)
        {
            HDL_ON.Entity.Function device = new Entity.Function() { name = "Unknown" };
            List<HDL_ON.Entity.Function> deviceLists = GetGatewayDeviceList();
            for (int i = 0; i < deviceLists.Count; i++)
            {
                var dev = deviceLists[i];
                if (dev.sid == sid)
                {
                    device = dev;
                    break;
                }
            }
            return device;
        }
 
        /// <summary>
        /// 获取当个场景
        /// </summary>
        /// <param name="sid">场景唯一标识</param>
        /// <returns></returns>
        public static HDL_ON.Entity.Scene GetSecne(string sid)
        {
            HDL_ON.Entity.Scene scene = new Entity.Scene() { name = "Unknown" };
            List<HDL_ON.Entity.Scene> sceneLists = GetSceneList();
            for (int i = 0; i < sceneLists.Count; i++)
            {
                var sce = sceneLists[i];
                if (sce.sid == sid)
                {
                    scene = sce;
                    break;
                }
            }
            return scene;
        }
        /// <summary>
        /// 获取房间名(即是=区域名称)
        /// </summary>
        /// <param name="device">设备</param>
        /// <returns></returns>
        public static string GetGetRoomName(HDL_ON.Entity.Function device)
        {
            string roomName = "";
            List<HDL_ON.Entity.Room> roomLists = GetGatewayRoomList();
            for (int i = 0; i < device.roomIds.Count; i++)
            {
                var dev = device.roomIds[i];
                var room = roomLists.Find((c) => c.roomId == dev);
                if (room != null)
                {
                    roomName += room.floorName + "." + room.roomName + ",";
                }
            }
 
            return roomName.TrimEnd(',');
        }
        #region   动一改四
        /// <summary>
        /// 获取设备类型图标
        /// </summary>
        /// <param name="functionType">设备类型</param>
        /// <returns></returns>
        public static string GetIconPath(string functionType)
        {
            string strPath = "";
            switch (functionType)
            {
                case SPK.LightSwitch:
                case SPK.LightRGB:
                case SPK.LightRGBW:
                case SPK.LightCCT:
                case SPK.LightDimming:
                    {
                        strPath = "LogicIcon/lightloguc.png";
                    }
                    break;
                case SPK.CurtainSwitch:
                case SPK.CurtainRoller:
                case SPK.CurtainTrietex:
                    {
                        strPath = "LogicIcon/curtainlogic.png";
                    }
                    break;
                case SPK.AcStandard:
                    {
                        strPath = "LogicIcon/airconditionerlogic.png";
                    }
                    break;
                case SPK.FloorHeatStandard:
                    {
                        strPath = "LogicIcon/heatlogic.png";
                    }
                    break;
                case SPK.SensorSmoke:
                case SPK.SensorWater:
                case SPK.SensorGas:
                case SPK.SensorDryContact:
                case SPK.SensorShanLan:
                case SPK.SensorDuiShe:
                case SPK.SensorPir:
                case SPK.SensorDoorWindow:
                case SPK.SensoruUtrasonic:
                    {
                        strPath = "LogicIcon/sensor.png";
                    }
                    break;
 
            }
            return strPath;
        }
        /// <summary>
        /// 设备类型的列表(灯光类,窗帘类。。。)
        /// </summary>
        /// <param name="deviceList">设备列表</param>
        /// <returns></returns>
        public static List<string> GetDeviceTypeList(List<HDL_ON.Entity.Function> deviceList)
        {
            List<string> deviceStrTypeList = new List<string>();
            deviceStrTypeList.Clear();
            var lightjosn = deviceList.Find((device) => device.spk == SPK.LightSwitch || device.spk == SPK.LightDimming || device.spk == SPK.LightCCT || device.spk == SPK.LightRGB || device.spk == SPK.LightRGBW);
            if (lightjosn != null)
            {
                deviceStrTypeList.Add(Language.StringByID(StringId.Lights));
            }
 
            var curtainjosn = deviceList.Find((device) => device.spk == SPK.CurtainSwitch || device.spk == SPK.CurtainTrietex || device.spk == SPK.CurtainRoller);
            if (curtainjosn != null)
            {
                deviceStrTypeList.Add(Language.StringByID(StringId.Curtain));
            }
 
            var ac = deviceList.Find((device) => device.spk == SPK.AcStandard);
            if (ac != null)
            {
                deviceStrTypeList.Add(Language.StringByID(StringId.AC));
            }
            var floorHeating = deviceList.Find((device) => device.spk == SPK.FloorHeatStandard);
            if (floorHeating != null)
            {
                deviceStrTypeList.Add(Language.StringByID(StringId.FloorHeating));
            }
            var sensor = deviceList.Find((device) =>
            device.spk == SPK.SensorWater
            || device.spk == SPK.SensorGas
            || device.spk == SPK.SensorSmoke
            || device.spk == SPK.SensorDryContact
            || device.spk == SPK.SensorShanLan
            || device.spk == SPK.SensorDuiShe
            || device.spk == SPK.SensorPir
            || device.spk == SPK.SensorDoorWindow
            || device.spk == SPK.SensoruUtrasonic
            );
            if (sensor != null)
            {
                deviceStrTypeList.Add(Language.StringByID(StringId.Sensor));
            }
            return deviceStrTypeList;
 
        }
        /// <summary>
        /// 设备类型FunctionType列表
        /// </summary>
        /// <param name="deviceType">设备类型(灯光类,窗帘类。)</param>
        /// <returns></returns>
        public static List<string> GetDeviceTypeFunctionList(string deviceType)
        {
            List<string> functionTypeList = new List<string>();
            if (deviceType == Language.StringByID(StringId.Lights))
            {
                functionTypeList.Add(SPK.LightSwitch);
                functionTypeList.Add(SPK.LightDimming);
                functionTypeList.Add(SPK.LightRGB);
                functionTypeList.Add(SPK.LightRGBW);
                functionTypeList.Add(SPK.LightCCT);
            }
            else if (deviceType == Language.StringByID(StringId.Curtain))
            {
                functionTypeList.Add(SPK.CurtainSwitch);
                functionTypeList.Add(SPK.CurtainRoller);
                functionTypeList.Add(SPK.CurtainTrietex);
            }
            else if (deviceType == Language.StringByID(StringId.AC))
            {
                functionTypeList.Add(SPK.AcStandard);
            }
            else if (deviceType == Language.StringByID(StringId.FloorHeating))
            {
                functionTypeList.Add(SPK.FloorHeatStandard);
            }
            else if (deviceType == Language.StringByID(StringId.Sensor))
            {
                functionTypeList.Add(SPK.SensorSmoke);
                functionTypeList.Add(SPK.SensorWater);
                functionTypeList.Add(SPK.SensorGas);
                functionTypeList.Add(SPK.SensorDryContact);
                functionTypeList.Add(SPK.SensorShanLan);
                functionTypeList.Add(SPK.SensorDuiShe);
                functionTypeList.Add(SPK.SensorPir);
                functionTypeList.Add(SPK.SensorDoorWindow);
                functionTypeList.Add(SPK.SensoruUtrasonic);
              
            }
            return functionTypeList;
 
        }
        /// <summary>
        /// 条件/目标支持设备
        /// </summary>
        /// <returns></returns>
        public static List<string> GetSupportEquipment(string if_type)
        {
            List<string> deviceTypeList = new List<string>();
            switch (if_type)
            {
                case condition_if:
                    {
                        deviceTypeList.Add(SPK.LightSwitch);
                        deviceTypeList.Add(SPK.LightRGB);
                        deviceTypeList.Add(SPK.LightRGBW);
                        deviceTypeList.Add(SPK.LightDimming);
                        deviceTypeList.Add(SPK.LightCCT);
                        deviceTypeList.Add(SPK.CurtainSwitch);
                        deviceTypeList.Add(SPK.CurtainRoller);
                        deviceTypeList.Add(SPK.CurtainTrietex);
                        deviceTypeList.Add(SPK.AcStandard);
                        deviceTypeList.Add(SPK.FloorHeatStandard);
                        deviceTypeList.Add(SPK.SensorSmoke);
                        deviceTypeList.Add(SPK.SensorWater);
                        deviceTypeList.Add(SPK.SensorGas);
                        deviceTypeList.Add(SPK.SensorDryContact);
                        deviceTypeList.Add(SPK.SensorShanLan);
                        deviceTypeList.Add(SPK.SensorDuiShe);
                        deviceTypeList.Add(SPK.SensorPir);
                        deviceTypeList.Add(SPK.SensorDoorWindow);
                        deviceTypeList.Add(SPK.SensoruUtrasonic);
                    }
                    break;
                case target_if:
                    {
                        deviceTypeList.Add(SPK.LightSwitch);
                        deviceTypeList.Add(SPK.LightRGB);
                        deviceTypeList.Add(SPK.LightRGBW);
                        deviceTypeList.Add(SPK.LightDimming);
                        deviceTypeList.Add(SPK.LightCCT);
                        deviceTypeList.Add(SPK.CurtainSwitch);
                        deviceTypeList.Add(SPK.CurtainRoller);
                        deviceTypeList.Add(SPK.CurtainTrietex);
                        deviceTypeList.Add(SPK.AcStandard);
                        deviceTypeList.Add(SPK.FloorHeatStandard);
                    }
                    break;
            }
            return deviceTypeList;
        }
        #endregion
        /// <summary>
        /// 显示的设备列表
        /// </summary>
        /// <param name="functionType">源数据列表1</param>
        /// <param name="deviceList">源数据列表2</param>
        /// <returns></returns>
        public static List<Entity.Function> GetShowDeviceList(List<string> functionType, List<HDL_ON.Entity.Function> deviceList)
        {
            List<HDL_ON.Entity.Function> devList = new List<Entity.Function>();
            for (int i = 0; i < deviceList.Count; i++)
            {
                var dev = deviceList[i];
                //过滤掉不需要显示的设备
                if (functionType.Contains(dev.spk))
                {
                    devList.Add(dev);
                }
 
            }
 
            return devList;
        }
        /// <summary>
        /// 返回最终支持显示出来的设备列表
        /// </summary>
        /// <param name="room">当前房间</param>
        /// <param name="str">判断符(表示=输入设备和输出设备)</param>
        /// <returns></returns>
        public static List<Entity.Function> GetFunctionDeviceList(Entity.Room room, string str)
        {
            List<string> functionTypeList = GetSupportEquipment(str);
            //返回房间设备列表
            var roomDeviceList = GetRoomDevice(room);
            //返回最终支持显示出来的设备列表
            var list = GetShowDeviceList(functionTypeList, roomDeviceList);
            return list;
        }
        /// <summary>
        /// 网关ID(获取嘉乐网关ID)
        /// </summary>
        public static string GatewayId
        {
            get
            {
                if (Entity.DB_ResidenceData.Instance.HomeGateway == null)
                {
                    return DriverLayer.Control.Ins.GatewayId;
                }
                return Entity.DB_ResidenceData.Instance.HomeGateway.gatewayId;
            }
        }
        /// <summary>
        /// 住宅ID
        /// </summary>
        public static string HomeId
        {
            get
            {
                return Entity.DB_ResidenceData.Instance.CurrentRegion.id;
            }
        }
        /// <summary>
        /// 是否为其他主用户分享过来的住宅
        /// </summary>
        public static bool IsOthreShare
        {
            get
            {
                return Entity.DB_ResidenceData.Instance.CurrentRegion.isOtherShare;
            }
        }
    }
}