wei
2021-03-23 d3a9358843b9539017debde289fb38e4fdf26938
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
using System;
using System.Collections.Generic;
using HDL_ON.Common;
using HDL_ON.DAL.Server;
using Shared;
 
namespace HDL_ON.Entity
{
    public class SceneApiPack
    {
        static SceneApiPack _ScenePack;
        public static SceneApiPack ScenePack
        {
            get
            {
                if(_ScenePack == null)
                {
                    _ScenePack = new SceneApiPack();
                }
                return _ScenePack;
            }
        }
 
    }
 
    public class Scene 
    {
        public Scene()
        {
            sid = NewSid();
            if (!DB_ResidenceData.Instance.CheckWhetherGatewayIdIsNull())
            {
                gatewayId = DB_ResidenceData.Instance.HomeGateway.gatewayId;
            }
        }
        /// <summary>
        /// 云端唯一ID
        /// </summary>
        public string userSceneId = null;
        /// <summary>
        /// 场景sid
        /// </summary>
        public string sid = "";
        /// <summary>
        /// 场景名称
        /// </summary>
        public string name = "";
        /// <summary>
        /// 网关ID
        /// </summary>
        public string gatewayId = "";
        /// <summary>
        /// 云端oss存储图片的路径
        /// </summary>
        public string image = "";
        /// <summary>
        /// 所属房间列表
        /// </summary>
        public List<string> roomIds = new List<string>();
        ///// <summary>
        ///// 所属房间列表
        ///// </summary>
        //public List<string> uids = new List<string>();
        /// <summary>
        /// 延时
        /// </summary>
        public string delay = "0";
        /// <summary>
        /// 场景的最好一次执行时间
        /// </summary>
        public string LastExecutionTime = "";
 
        /// <summary>
        /// 场景分组
        /// </summary>
        public string group = "1";
        /// <summary>
        /// 场景类型
        /// </summary>
        public SceneType sceneType = SceneType.OrdinaryScenes;
        /// <summary>
        /// 收藏标记
        /// </summary>
        public bool collect = false;
 
        ///// <summary>
        ///// 场景背景
        ///// </summary>
        public string ImagePath
        {
            get
            {
                if (string.IsNullOrEmpty(image))
                {
                    return "Intelligence/Gallery/scenebg1.png";
                }
                else
                {
                    return image;
                }
            }
            set
            {
                image = value;
            }
        }
        
 
        /// <summary>
        /// 场景功能列表
        /// </summary>
        public List<SceneFunction> functions = new List<SceneFunction>();
        /// <summary>
        /// 场景推送配置列表
        /// </summary>
        public List<ScenePushConfig> pushConfigs = new List<ScenePushConfig>();
 
        /// <summary>
        /// 生成逻辑sid方法
        /// </summary>
        public string NewSid()
        {
            string sceneId = "";
            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 = "00000000";
 
                byte[] arry = new byte[4];
                arry[0] = (byte)(m & 0xFF);
                arry[1] = (byte)((m & 0xFF00) >> 8);
                arry[2] = (byte)((m & 0xFF0000) >> 16);
                arry[3] = (byte)((m >> 24) & 0xFF);
                sTimeSpan = arry[0].ToString("X2") + arry[1].ToString("X2") + arry[2].ToString("X2") + arry[3].ToString("X2");
 
 
                if (sTimeSpan.Length > 8)
                {
                    sTimeSpan = sTimeSpan.Substring(0, 8);
                }
                else
                {
                    sTimeSpan = "00000000";
                }
 
                sceneId = sOidBeginsWith + sTimeSpan;
 
                sceneId += "0A";
                sceneId += "0A01";
                int maxId = 1;
 
 
                for (int i = 0; i < FunctionList.List.scenes.Count; i++)
                {
                    string s = FunctionList.List.scenes[i].sid.Substring(20, 4);
                    int iThisSceneId = Convert.ToInt16(s, 16);
                    if (iThisSceneId > maxId)
                        maxId = iThisSceneId;
                }
 
                sceneId += (maxId + 1).ToString("X4");
                sceneId += "0000";
            }
            catch
            {
                return sceneId;
            }
            return sceneId;
        }
 
        /// <summary>
        /// 获取设备添加到房间的房间名称
        /// </summary>
        /// <returns></returns>
        public string GetRoomListName()
        {
            string roomNameList = "";
            foreach (var roomId in roomIds)
            {
                var findRoom = SpatialInfo.CurrentSpatial.RoomList.Find(obj => obj.roomId == roomId);
                if (findRoom == null)
                {
                    continue;
                }
                if (roomNameList != "")
                {
                    roomNameList += ",";
                }
                roomNameList += findRoom.floorName + findRoom.roomName;
            }
            if (roomNameList == "" )
            {
                roomNameList = Shared.Language.StringByID(StringId.WholeZone);
            }
            return roomNameList;
        }
 
        /// <summary>
        /// 数据存储文件名
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public string savePath
        {
            get
            {
                return "SceneData_" + sid;
            }
        }
        /// <summary>
        /// 场景执行\倒计时
        /// </summary>
        /// <param name="btnShowDelay"></param>
        public void SceneCountDown(Button btnShowDelay)
        {
            //场景正在倒计时执行中
            if (LastExecutionTime != "" && Convert.ToDouble(LastExecutionTime) + Convert.ToDouble(delay) > Convert.ToDouble(Utlis.GetTimestamp(false)))
            {
                int time = 0;
                int.TryParse(Convert.ToInt32(
                    Convert.ToDouble(LastExecutionTime) + Convert.ToDouble(delay) - Convert.ToDouble(Utlis.GetTimestamp(false))
                    ).ToString(), out time);
                if (time > 0)
                {
                    new System.Threading.Thread(() =>
                    {
                        if (time > 0)
                        {
                            while (time > 0)
                            {
                                Application.RunOnMainThread(() =>
                                {
                                    if (time > 60)
                                    {
                                        btnShowDelay.Text = (time / 60).ToString() + "min" + time % 60 + "s";
                                    }
                                    else
                                    {
                                        btnShowDelay.Text = time.ToString() + "s";
                                    }
                                });
                                System.Threading.Thread.Sleep(1000);
                                time--;
                            }
                            Application.RunOnMainThread(() =>
                            {
                                btnShowDelay.Text = "";
                            });
                        }
                    })
                    { IsBackground = true, Priority = System.Threading.ThreadPriority.BelowNormal }.Start();
                }
            }
        }
        /// <summary>
        /// 保存场景数据文件
        /// </summary>
        public void SaveSceneFile()
        {
            var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
            FileUtlis.Files.WriteFileByBytes(savePath, ssd);
            MainPage.Log($"Save Scene Data : {savePath}");
        }
 
        /// <summary>
        /// 收藏场景
        /// </summary>
        /// <returns></returns>
        public void CollectScene()
        {
            var result = StateCode.SUCCESS;
            new System.Threading.Thread(() =>
            {
                if (collect)
                {
                    result = ApiUtlis.Ins.HttpRequest.CollectScene(userSceneId);
                }
                else
                {
                    result = ApiUtlis.Ins.HttpRequest.CancelCollectScene(userSceneId);
                }
                //提示错误
                if (result != StateCode.SUCCESS)
                {
                    Application.RunOnMainThread(() =>
                    {
                        collect = !collect;
                        IMessageCommon.Current.ShowErrorInfoAlter(result);
                    });
                }
            })
            { IsBackground = true }.Start();
        }
 
        /// <summary>
        /// 编辑场景
        /// </summary>
        public string EditScene()
        {
            var pm = new HttpServerRequest();
            var revPack = pm.EditScene(this);
            if (revPack.Code == StateCode.SUCCESS)
            {
                var scenes = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Scene>>(revPack.Data.ToString());
                var tempScene = scenes.Find((obj) => obj.sid == sid);
                if (tempScene != null)
                {
                    var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(tempScene));
                    Common.FileUtlis.Files.WriteFileByBytes(savePath, ssd);
                }
            }
            return revPack.Code;
        }
 
        /// <summary>
        /// 修改场景
        /// </summary>
        public string UpdateScene()
        {
            var pm = new HttpServerRequest();
            var revPack = pm.EditScene(this);
            if (revPack.Code == StateCode.SUCCESS)
            {
                var scenes = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Scene>>(revPack.Data.ToString());
                var tempScene = scenes.Find((obj) => obj.sid == sid);
                if (tempScene != null)
                {
                    var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(tempScene));
                    Common.FileUtlis.Files.WriteFileByBytes(savePath, ssd);
                }
            }
            return revPack.Code;
        }
 
        /// <summary>
        /// 延时显示的文本
        /// </summary>
        public string GetDelayText()
        {
            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;
        }
        /// <summary>
        /// 云端数据创建的时间
        /// </summary>
        public string createTime = "";
        /// <summary>
        /// 云端数据修改的最后时间
        /// </summary>
        public string modifyTime = "";
    }
    /// <summary>
    /// 场景功能对象
    /// </summary>
    public class SceneFunction
    {
        public string sid = "";
 
        public List<SceneFunctionStatus> status = new List<SceneFunctionStatus>();
        /// <summary>
        /// 功能延时
        /// </summary>
        public string delay = "0";
 
        Function _localFunction;
        /// <summary>
        /// 本地对应的功能
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public Function localFunction
        {
            get
            {
                if (_localFunction == null)
                {
                    _localFunction = ConvertFunctionObject();
                }
                return _localFunction;
            }
        }
 
        /// <summary>
        /// 转换成功能对象
        /// </summary>
        /// <returns></returns>
        Function ConvertFunctionObject()
        {
            var localFunction = FunctionList.List.GetDeviceFunctionList().Find((obj) => obj.sid == sid);
            return localFunction;
        }
 
    }
 
    /// <summary>
    /// 场景功能属性
    /// </summary>
    public class SceneFunctionStatus
    {
        public string key = "";
        public string value = "";
 
        /// <summary>
        /// 属性名称显示文本
        /// 中英文显示
        /// </summary>
        public string GetNameText()
        {
            string text = "";
            switch (key)
            {
                case FunctionAttributeKey.OnOff:
                    text = Language.StringByID(StringId.OnOff);
                    break;
                case FunctionAttributeKey.Brightness:
                    text = Language.StringByID(StringId.Brightness);
                    break;
                case FunctionAttributeKey.RGB:
                    text = Language.StringByID(StringId.ColorValue);
                    break;
                case FunctionAttributeKey.Mode:
                    text = Language.StringByID(StringId.Mode);
                    break;
                case FunctionAttributeKey.FanSpeed:
                    text = Language.StringByID(StringId.FanSpeed);
                    break;
                case FunctionAttributeKey.SetTemp:
                    text = Language.StringByID(StringId.Temp);
                    break;
                case FunctionAttributeKey.RoomTemp:
                    text = Language.StringByID(StringId.IndoorTemp);
                    break;
                case FunctionAttributeKey.Delay:
                    text = Language.StringByID(StringId.Delay);
                    break;
                case FunctionAttributeKey.CCT:
                    text = "CCT";
                    break;
                case FunctionAttributeKey.Percent:
                    text = Language.StringByID(StringId.PercentAdjustment);
                    break;
                case FunctionAttributeKey.FadeTime:
                    text = Language.StringByID(StringId.FadeSpeed);
                    break;
            }
            return text;
        }
 
 
        /// <summary>
        /// 属性值单位
        /// </summary>
        public string GetUintString()
        {
            var us = "";
            switch (key)
            {
                case FunctionAttributeKey.SetTemp:
                case FunctionAttributeKey.RoomTemp:
                    us = "°C";
                    break;
                case FunctionAttributeKey.Percent:
                case FunctionAttributeKey.Brightness:
                    us = "%";
                    break;
            }
            return us;
        }
 
 
        /// <summary>
        /// 获取指定属性的显示文本
        /// 中英文显示
        /// </summary>
        public string GetValueText()
        {
            string text = "";
            switch (key)
            {
                case FunctionAttributeKey.OnOff:
                    text = value == "on" ? Language.StringByID(StringId.On) : Language.StringByID(StringId.OFF);
                    break;
                case FunctionAttributeKey.SetTemp:
                case FunctionAttributeKey.RoomTemp:
                case FunctionAttributeKey.Brightness:
                case FunctionAttributeKey.Percent:
                    if (value == "")
                    {
                        value = "0";
                    }
                    text = value;
                    break;
                case FunctionAttributeKey.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;
                    }
                    break;
                case FunctionAttributeKey.FanSpeed:
                    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:
                            text = "low";
                            break;
                    }
                    break;
                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>
    /// 场景推送配置
    /// </summary>
    public class ScenePushConfig
    {
        /// <summary>
        /// 推送方式
        /// </summary>
        public string pushMethod = "";
        /// <summary>
        /// 推送内容
        /// </summary>
        public string pushContent = "";
        /// <summary>
        /// 推送目标集合
        /// </summary>
        public List<string> pushTarget = new List<string>();
    }
 
}