黄学彪
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
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
using Shared.Common;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using ZigBee.Device;
 
namespace Shared.Phone.UserCenter
{
    /// <summary>
    /// 场景的逻辑
    /// </summary>
    public class HdlSceneLogic
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 场景的逻辑
        /// </summary>
        private static HdlSceneLogic m_Current = null;
        /// <summary>
        /// 场景的逻辑
        /// </summary>
        public static HdlSceneLogic Current
        {
            get
            {
                if (m_Current == null)
                {
                    m_Current = new HdlSceneLogic();
                }
                return m_Current;
            }
        }
 
        /// <summary>
        /// 场景对象
        /// </summary>
        private Dictionary<int, SceneUI> dicScenes = new Dictionary<int, SceneUI>();
 
        #endregion
 
        #region ■ 刷新场景___________________________
 
        /// <summary>
        /// 从本地刷新场景
        /// </summary>
        public void ReFreshByLocal()
        {
            //清空场景
            this.dicScenes.Clear();
            //获取全部文件
            var listFile = this.GetAllSceneFile();
            foreach (string fileName in listFile)
            {
                try
                {
                    var jsonInfo = Encoding.UTF8.GetString(Global.ReadFileByHomeId(fileName));
                    var tempScene = Newtonsoft.Json.JsonConvert.DeserializeObject<SceneUI>(jsonInfo);
                    if (tempScene == null)
                    {
                        continue;
                    }
                    this.dicScenes[tempScene.Id] = tempScene;
                }
                catch(Exception ex)
                {
                    HdlLogLogic.Current.WriteLog(ex);
                    try
                    {
                        //删掉错误文件
                        Global.DeleteFilebyHomeId(fileName);
                    }
                    catch { }
                }
            }
        }
 
        /// <summary>
        /// 刷新本地的全部场景
        /// </summary>
        public bool RefreshSceneUIList()
        {
            //获取网关存在的场景
            var sceneList = this.RefreshSceneListFromGateway();
            if (sceneList == null)
            {
                return false;
            }
            var listEsxit = new HashSet<int>();
            foreach (var scene in sceneList)
            {
                if (scene == null)
                {
                    continue;
                }
                listEsxit.Add(scene.ScenesId);
                //刷新scene的信息
                this.RefreshScene(scene);
            }
 
            var listDelete = new List<SceneUI>();
            foreach (var scId in this.dicScenes.Keys)
            {
                if (listEsxit.Contains(scId) == false)
                {
                    //删除的对象
                    listDelete.Add(this.dicScenes[scId]);
                }
            }
            foreach (var sceneui in listDelete)
            {
                //执行删除
                this.DeleteLocalScene(sceneui);
            }
 
            return true;
        }
 
        /// <summary>
        /// 刷新scene的信息
        /// </summary>
        /// <param name="sceneInfo"></param>
        private void RefreshScene(Scene.GetSceneAllInfo sceneInfo)
        {
            if (this.dicScenes.ContainsKey(sceneInfo.ScenesId) == true)
            {
                var localScene = this.dicScenes[sceneInfo.ScenesId];
                //只刷新名字和绑定表
                localScene.Name = sceneInfo.ScenesName;
                localScene.AdjustTargetList.Clear();
                localScene.AdjustTargetList.AddRange(sceneInfo.DeviceList);
                localScene.Save();
            }
            else
            {
                var newScene = new SceneUI();
                newScene.Name = sceneInfo.ScenesName;
                newScene.IconPath = "SceneIcon/0.png";
                newScene.Id = sceneInfo.ScenesId;
                newScene.AdjustTargetList.AddRange(sceneInfo.DeviceList);
                //添加缓存
                newScene.Save();
                //加入缓存
                this.dicScenes[newScene.Id] = newScene;
                //备份
                HdlAutoBackupLogic.AddOrEditorFile(newScene.FileName);
            }
 
        }
 
        /// <summary>
        /// 从网关重新刷新场景列表
        /// </summary>
        /// <returns></returns>
        private List<Scene.GetSceneAllInfo> RefreshSceneListFromGateway()
        {
            var mainGateway = ZbGateway.MainGateWay;
            if (mainGateway == null)
            {
                //获取网关对象失败
                this.ShowTipMsg(Language.StringByID(R.MyInternationalizationString.uGetGatewayTagartFail));
                return null;
            }
 
            //超时时间
            int TimeOut = 0;
            bool receiptAll = false;
 
            var listScene = new List<Scene.GetSceneAllInfo>();
            Action<string, string> action = (topic, message) =>
            {
                var gatewayID = topic.Split('/')[0];
                var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
 
                if (topic == gatewayID + "/" + "Scene/GetAllInfo_Respon")
                {
                    var sceneGetAllInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<Scene.GetSceneAllInfo>(jobject["Data"].ToString());
                    listScene.Add(sceneGetAllInfo);
 
                    if (sceneGetAllInfo.ScenesNum == sceneGetAllInfo.ScenesSum)
                    {
                        //接收完成
                        receiptAll = true;
                        TimeOut = 0;
                    }
                }
            };
            mainGateway.Actions += action;
 
            var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 807 } };
            mainGateway.Send(("Scene/GetAllInfo"), System.Text.Encoding.UTF8.GetBytes(jObject.ToString()));
 
            int waitTime = 20 * 6;
            while (receiptAll == false && TimeOut < waitTime)
            {
                //全部接收才退出
                System.Threading.Thread.Sleep(50);
                TimeOut++;
            }
            mainGateway.Actions -= action;
            action = null;
            if (receiptAll == false)
            {
                //网络不稳定,场景列表信息缺损
                this.ShowTipMsg(Language.StringByID(R.MyInternationalizationString.uNetworkUnStableAndSceneInfoIsNotFull));
                return null;
            }
 
            return listScene;
        }
 
        #endregion
 
        #region ■ 添加场景___________________________
 
        /// <summary>
        /// 添加新场景到网关(返回null代表出错)
        /// </summary>
        /// <param name="sceneName">场景名称</param>
        /// <param name="listAdjustTarget">执行目标</param>
        /// <returns></returns>
        public async Task<SceneUI> AddNewSceneToGateway(string sceneName, List<Scene.DeviceListData> listAdjustTarget)
        {
            int NewScenesId = -1;
            //如果当前住宅不是虚拟住宅
            if (Common.Config.Instance.Home.IsVirtually == false)
            {
                var result1 = await Scene.GetSceneNewIdAsync(sceneName);
                //共通错误检测
                string error = HdlCheckLogic.Current.CheckCommonErrorCode(result1);
                if (error != null)
                {
                    this.ShowErrorMsg(error);
                    return null;
                }
                if (result1 == null || result1.getSceneIdData == null)
                {
                    //添加场景失败
                    string msg1 = Language.StringByID(R.MyInternationalizationString.AddSceneFail);
                    //拼接上【网关回复超时】的Msg
                    msg1 = UserCenterLogic.CombineGatewayTimeOutMsg(msg1, result1);
                    this.ShowTipMsg(msg1);
                    return null;
                }
                NewScenesId = result1.getSceneIdData.NewScenesId;
            }
            else
            {
                //虚拟场景ID
                NewScenesId = Convert.ToInt32(DateTime.Now.ToString("HHmmss"));
            }
            //添加执行目标
            var listSuccess = await this.AddTargetToScene(NewScenesId, listAdjustTarget);
            if (listSuccess == null)
            {
                return null;
            }
 
            //创建场景对象
            var newScene = new SceneUI();
            newScene.Name = sceneName;
            newScene.Id = NewScenesId;
            newScene.AdjustTargetList.AddRange(listSuccess);
            //添加缓存
            newScene.Save();
            //加入缓存
            this.dicScenes[newScene.Id] = newScene;
 
            return newScene;
        }
 
        /// <summary>
        /// 添加执行目标到指定的场景(返回成功添加的执行目标)
        /// </summary>
        /// <param name="sceneId">场景Id</param>
        /// <param name="listAdjustTarget">执行目标</param>
        /// <returns></returns>
        private async Task<List<Scene.DeviceListData>> AddTargetToScene(int sceneId, List<Scene.DeviceListData> listAdjustTarget)
        {
            //如果当前住宅是虚拟住宅的话
            if (Common.Config.Instance.Home.IsVirtually == true)
            {
                //不需要更新网关
                return listAdjustTarget;
            }
            var listSuccess = new List<Scene.DeviceListData>();
            for (int i = 0; i < listAdjustTarget.Count; i++)
            {
                var data = listAdjustTarget[i];
                var addData = new Scene.AddSceneMemberData();
                if (data.Type == 0)
                {
                    //设备
                    addData.Type = 0;
                    addData.ScenesId = sceneId;
                    addData.DeviceAddr = data.DeviceAddr;
                    addData.Epoint = data.Epoint;
                    addData.TaskList = data.TaskList;
                    addData.DelayTime = 0;
                    addData.MemberNumber = i + 1;
                }
                else if (data.Type == 1)
                {
                    //场景
                    addData.Type = 1;
                    addData.ScenesId = sceneId;
                    addData.ElseScenesId = data.ElseScenesId;
                    addData.DelayTime = 0;
                    addData.MemberNumber = i + 1;
                }
                else
                {
                    //延时
                    addData.Type = 2;
                    addData.ScenesId = sceneId;
                    addData.DelayTime = data.DelayTime;
                    addData.MemberNumber = i + 1;
                }
 
                //添加新成员 返回结果
                var result2 = await Scene.AddSceneMemberAsync(addData);
                if (result2 == null || result2.addSceneMemberResponseData == null
                    || result2.addSceneMemberResponseData.Result != 1)
                {
                    continue;
                }
                //拥有成功的
                listSuccess.Add(data);
            }
            if (listSuccess.Count == 0)
            {
                //添加执行目标失败
                string msg1 = Language.StringByID(R.MyInternationalizationString.uAddAdjustTargetFail);
                this.ShowTipMsg(msg1);
                return null;
            }
            //如果不是全部成功的话,提示
            if (listSuccess.Count != listAdjustTarget.Count)
            {
                //个别执行目标添加失败
                string msg1 = Language.StringByID(R.MyInternationalizationString.uSomeAdjustTargetAddFail);
                this.ShowTipMsg(msg1);
            }
            return listSuccess;
        }
 
        /// <summary>
        /// 添加场景(这个单纯只改房间)
        /// </summary>
        /// <param name="scene">Scene.</param>
        public void AddSceneToRoom(Room i_room, SceneUI scene)
        {
            //添加缓存
            if (i_room.ListSceneId.Contains(scene.Id) == false)
            {
                i_room.ListSceneId.Add(scene.Id);
                i_room.Save();
            }
        }
 
        /// <summary>
        /// 改变场景房间
        /// </summary>
        /// <param name="scene">场景对象</param>
        /// <param name="newRoomId">新的房间的ID</param>
        public void ChangedSceneRoom(SceneUI scene, string newRoomId)
        {
            var room = HdlRoomLogic.Current.GetRoomBySceneId(scene.Id);
            if (room != null)
            {
                //房间没有改变
                if (room.Id == newRoomId)
                {
                    return;
                }
                //从房间中移除缓存
                this.DeleteSceneFromRoom(room, scene);
            }
            //添加进新房间
            var newRoom = HdlRoomLogic.Current.GetRoomById(newRoomId);
            this.AddSceneToRoom(newRoom, scene);
        }
 
        /// <summary>
        /// 添加搜藏场景
        /// </summary>
        /// <param name="scene">Scene.</param>
        public void AddLoveScene(SceneUI scene)
        {
            var nowRoom = HdlRoomLogic.Current.GetLoveRoom();
            //添加缓存
            if (nowRoom.ListSceneId.Contains(scene.Id) == false)
            {
                nowRoom.ListSceneId.Add(scene.Id);
                nowRoom.Save();
            }
        }
 
        /// <summary>
        /// 添加虚拟场景
        /// </summary>
        /// <param name="sceneId">指定场景id,如果为-1,则会内部自动编号(可能会重复)</param>
        /// <param name="sceneName">场景名字</param>
        /// <param name="listAdjustTarget">绑定目标</param>
        /// <returns></returns>
        public SceneUI AddVirtualScene(int sceneId, string sceneName, List<Scene.DeviceListData> listAdjustTarget)
        {
            if (sceneId == -1)
            {
                sceneId = Convert.ToInt32(DateTime.Now.ToString("HHmmss"));
            }
            //创建场景对象
            var newScene = new SceneUI();
            newScene.Name = sceneName;
            newScene.IconPath = "SceneIcon/0.png";
            //虚拟场景ID
            newScene.Id = sceneId;
            if (listAdjustTarget != null)
            {
                newScene.AdjustTargetList.AddRange(listAdjustTarget);
            }
            //添加缓存
            newScene.Save();
            //加入缓存
            this.dicScenes[newScene.Id] = newScene;
 
            return newScene;
        }
 
        #endregion
 
        #region ■ 修改场景___________________________
 
        /// <summary>
        /// 修改网关的场景
        /// </summary>
        /// <param name="sceneUI">场景对象</param>
        /// <param name="listAdjustTarget">全部的执行目标</param>
        /// <returns></returns>
        public async Task<bool> EditorSceneFromGateway(SceneUI sceneUI, List<Scene.DeviceListData> listAdjustTarget)
        {
            //如果当前住宅是虚拟住宅的话
            if (Common.Config.Instance.Home.IsVirtually == true)
            {
                //清空场景的执行目标列表的缓存
                sceneUI.AdjustTargetList.Clear();
                sceneUI.AdjustTargetList.AddRange(listAdjustTarget);
                sceneUI.Save();
                return true;
            }
 
            //先清空全部的执行目标
            var deleteData = new Scene.SceneRemoveMemberData();
            var deleteTargetData = new List<Scene.RemoveSceneDeviceListInfo>();
            for (int i = 0; i < sceneUI.AdjustTargetList.Count; i++)
            {
                var data = sceneUI.AdjustTargetList[i];
                var removeData = new Scene.RemoveSceneDeviceListInfo();
                if (data.Type == 0)
                {
                    //设备
                    removeData.Type = 0;
                    removeData.DeviceAddr = data.DeviceAddr;
                    removeData.Epoint = data.Epoint;
                    removeData.MemberNumber = data.MemberNumber;
                }
                else if (data.Type == 1)
                {
                    //场景
                    removeData.Type = 1;
                    removeData.ElseScenesId = data.ElseScenesId;
                    removeData.MemberNumber = data.MemberNumber;
                }
                else
                {
                    //延时
                    removeData.Type = 2;
                    removeData.DelayTime = data.DelayTime;
                    removeData.MemberNumber = data.MemberNumber;
                }
                //拥有成功的
                deleteTargetData.Add(removeData);
            }
            deleteData.DeviceList = deleteTargetData;
            deleteData.ScenesId = sceneUI.Id;
            //执行清空
            var result = await Scene.RemoveSceneMemberAsync(deleteData);
            if (result == null || result.removeSceneMemberResponseData == null)
            {
                //初始化执行目标失败
                string msg1 = Language.StringByID(R.MyInternationalizationString.uInitAdjustTargetFail);
                //拼接上【网关回复超时】的Msg
                msg1 = UserCenterLogic.CombineGatewayTimeOutMsg(msg1, result);
                this.ShowTipMsg(msg1);
                return false;
            }
            if (result.removeSceneMemberResponseData.Result != 0)
            {
                //初始化执行目标失败
                string msg1 = Language.StringByID(R.MyInternationalizationString.uInitAdjustTargetFail);
                this.ShowTipMsg(msg1);
                return false;
            }
            //清空场景的执行目标列表的缓存
            sceneUI.AdjustTargetList.Clear();
 
            //然后重新添加
            var listSuccess = await this.AddTargetToScene(sceneUI.Id, listAdjustTarget);
            if (listSuccess == null)
            {
                return false;
            }
            sceneUI.AdjustTargetList.AddRange(listSuccess);
            sceneUI.Save();
 
            return true;
        }
 
        /// <summary>
        /// 修改网关的场景名称
        /// </summary>
        /// <param name="scene">场景对象</param>
        /// <param name="newName">新的名字</param>
        /// <returns></returns>
        public async Task<bool> EditorSceneNameFromGateway(SceneUI scene, string newName)
        {
            //如果当前住宅是虚拟住宅的话
            if (Common.Config.Instance.Home.IsVirtually == true)
            {
                //不需要更新网关
                return true;
            }
            var result1 = await Scene.RenameSceneAsync(scene.Id, newName);
            //共通错误检测
            string error = HdlCheckLogic.Current.CheckCommonErrorCode(result1);
            if (error != null)
            {
                this.ShowErrorMsg(error);
                return false;
            }
            if (result1 == null || result1.sceneRenameResponseData == null)
            {
                //修改场景名称失败
                string msg1 = Language.StringByID(R.MyInternationalizationString.uEditorSceneNameFail);
                //拼接上【网关回复超时】的Msg
                msg1 = UserCenterLogic.CombineGatewayTimeOutMsg(msg1, result1);
                this.ShowTipMsg(msg1);
                return false;
            }
            if (result1.sceneRenameResponseData.Result == 2)
            {
                //目标场景不存在
                string msg1 = Language.StringByID(R.MyInternationalizationString.uTargetSceneIsNotExsit);
                this.ShowTipMsg(msg1);
                return false;
            }
            if (result1.sceneRenameResponseData.Result != 1)
            {
                //修改场景名称失败
                string msg1 = Language.StringByID(R.MyInternationalizationString.uEditorSceneNameFail);
                this.ShowTipMsg(msg1);
                return false;
            }
            return true;
        }
 
        #endregion
 
        #region ■ 删除场景___________________________
 
        /// <summary>
        /// 删除本地场景数据
        /// </summary>
        /// <param name="sceneUI">Scene user interface.</param>
        public void DeleteLocalScene(SceneUI sceneUI)
        {
            //移除缓存
            this.dicScenes.Remove(sceneUI.Id);
            var room = HdlRoomLogic.Current.GetRoomBySceneId(sceneUI.Id);
            if (room != null)
            {
                room.ListSceneId.Remove(sceneUI.Id);
                room.Save();
            }
 
            //移除喜爱
            var roomLove = HdlRoomLogic.Current.GetLoveRoom();
            if (roomLove.ListSceneId.Contains(sceneUI.Id) == true)
            {
                roomLove.ListSceneId.Remove(sceneUI.Id);
                roomLove.Save();
            }
 
            //删除文件
            if (Global.IsExistsByHomeId(sceneUI.FileName) == true)
            {
                Global.DeleteFilebyHomeId(sceneUI.FileName);
                HdlAutoBackupLogic.DeleteFile(sceneUI.FileName);
            }
            //删除图片
            if (sceneUI.IconPathType == 1 || sceneUI.IconPathType == 2)
            {
                if (Global.IsExistsByHomeId(sceneUI.IconPath))
                {
                    Global.DeleteFilebyHomeId(sceneUI.IconPath);
                    HdlAutoBackupLogic.DeleteFile(sceneUI.IconPath);
                }
            }
        }
 
        /// <summary>
        /// 删除场景(这个只移除内存)
        /// </summary>
        /// <param name="scene"></param>
        public void DeleteSceneFromRoom(Room i_room, SceneUI scene)
        {
            //移除缓存
            if (i_room.ListSceneId.Contains(scene.Id) == true)
            {
                i_room.ListSceneId.Remove(scene.Id);
                i_room.Save();
            }
        }
 
        /// <summary>
        /// 删除搜藏场景
        /// </summary>
        /// <param name="scene"></param>
        public void DeleteLoveScene(SceneUI scene)
        {
            var nowRoom = HdlRoomLogic.Current.GetLoveRoom();
            //移除缓存
            if (nowRoom.ListSceneId.Contains(scene.Id) == true)
            {
                nowRoom.ListSceneId.Remove(scene.Id);
                nowRoom.Save();
            }
        }
 
        #endregion
 
        #region ■ 获取场景___________________________
 
        /// <summary>
        /// 通过场景id获取场景
        /// </summary>
        /// <returns>The scene UIB y scene identifier.</returns>
        /// <param name="sceneId">Scene identifier.</param>
        public SceneUI GetSceneUIBySceneId(int sceneId)
        {
            if (this.dicScenes.ContainsKey(sceneId) == true)
            {
                return this.dicScenes[sceneId];
            }
            return null;
        }
 
        /// <summary>
        /// 获取该楼层所有场景
        /// </summary>
        /// <param name="floorId"></param>
        /// <returns></returns>
        public List<SceneUI> GetSameFloorScenes(string floorId)
        {
            List<SceneUI> sceneUIs = new List<SceneUI>();
            var rooms = HdlRoomLogic.Current.GetRoomsByFloorId(floorId);
            foreach (var r in rooms)
            {
                foreach (int sceneId in r.ListSceneId)
                {
                    if (this.dicScenes.ContainsKey(sceneId) == true)
                    {
                        sceneUIs.Add(this.dicScenes[sceneId]);
                    }
                }
            }
            return sceneUIs;
        }
 
        /// <summary>
        /// 根据场景Id,获取楼层和房间名字(楼层,房间名字)
        /// </summary>
        /// <returns></returns>
        public string GetZoneById(int sceneId)
        {
            var room = HdlRoomLogic.Current.GetRoomBySceneId(sceneId);
            if (room == null)
            {
                return null;
            }
            var floorName = HdlResidenceLogic.Current.GetFloorNameById(room.FloorId);
            //更改代码:如果floorName=“”也要判断,否则有“,”
            if (string.IsNullOrEmpty(floorName))
            {
                return room.Name;
            }
            return $"{floorName},{room.Name}";
        }
 
        /// <summary>
        /// 获取未分配区域场景
        /// </summary>
        /// <returns></returns>
        public List<SceneUI> GetUnalloctedScenes()
        {
            List<SceneUI> sceneUIs = new List<SceneUI>();
            //已经存在的场景
            var listEsxit = new HashSet<int>();
            var listAllRoom = HdlRoomLogic.Current.GetAllListRooms();
            foreach (var room in listAllRoom)
            {
                if (room.IsLove == true)
                {
                    //不包含收藏房间
                    continue;
                }
                foreach (int sceneId in room.ListSceneId)
                {
                    if (listEsxit.Contains(sceneId) == false)
                    {
                        listEsxit.Add(sceneId);
                    }
                }
            }
            foreach (var myScene in this.dicScenes.Values)
            {
                if (listEsxit.Contains(myScene.Id) == false)
                {
                    sceneUIs.Add(myScene);
                }
            }
            return sceneUIs;
        }
 
        /// <summary>
        /// 获取全部房间的全部场景
        /// </summary>
        /// <returns></returns>
        public List<SceneUI> GetAllRoomSceneList()
        {
            List<SceneUI> sceneUIs = new List<SceneUI>();
            var listAllRoom = HdlRoomLogic.Current.GetAllListRooms();
            foreach (var room in listAllRoom)
            {
                if (room.IsLove == true)
                {
                    //不包含收藏房间
                    continue;
                }
                foreach (int sceneId in room.ListSceneId)
                {
                    if (this.dicScenes.ContainsKey(sceneId) == true)
                    {
                        sceneUIs.Add(this.dicScenes[sceneId]);
                    }
                }
            }
            
            return sceneUIs;
        }
 
        /// <summary>
        /// 获取指定房间的全部场景
        /// </summary>
        /// <returns></returns>
        public List<SceneUI> GetRoomSceneList(Room i_room)
        {
            List<SceneUI> sceneUIs = new List<SceneUI>();
            foreach (int sceneId in i_room.ListSceneId)
            {
                if (this.dicScenes.ContainsKey(sceneId) == true)
                {
                    sceneUIs.Add(this.dicScenes[sceneId]);
                }
            }
 
            return sceneUIs;
        }
 
        #endregion
 
        #region ■ 调用场景___________________________
 
        /// <summary>
        /// 调用场景
        /// </summary>
        /// <param name="scene"></param>
        /// <returns></returns>
        public async Task<bool> ControlScene(SceneUI scene)
        {
            if (scene.RemainTime > 0)
            {
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //该场景正在延时,请稍后
                    var msgContr = new ShowMsgControl(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.TheSceneIsDelaying));
                    msgContr.Show();
                });
                return false;
            }
            //如果是虚拟住宅
            if (Common.Config.Instance.Home.IsVirtually == true)
            {
                //不调用网关
                return true;
            }
 
            //执行调用场景
            var result = await Scene.ControlSceneAsync(scene.Id, scene.SceneDelayTime);
            if (result == null || result.sceneOpenData == null)
            {
                //控制场景失败
                string msg = Language.StringByID(R.MyInternationalizationString.ControlSceneFail);
                //拼接上【网关回复超时】的Msg
                msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result);
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //该场景正在延时,请稍后
                    var msgContr = new ShowMsgControl(ShowMsgType.Tip, msg);
                    msgContr.Show();
                });
                return false;
            }
            if (result.sceneOpenData.Result == 0)
            {
                //控制场景失败
                string msg = Language.StringByID(R.MyInternationalizationString.ControlSceneFail);
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //该场景正在延时,请稍后
                    var msgContr = new ShowMsgControl(ShowMsgType.Tip, msg);
                    msgContr.Show();
                });
                return false;
            }
            return true;
        }
 
        #endregion
 
        #region ■ 获取场景的执行目标列表_____________
 
        /// <summary>
        /// 获取场景的执行目标列表
        /// </summary>
        /// <param name="i_scene">场景对象</param>
        /// <returns></returns>
        public async Task<List<Scene.DeviceListData>> GetAdjustTargetList(SceneUI i_scene)
        {
            //如果住宅为虚拟住宅
            if (Common.Config.Instance.Home.IsVirtually == true)
            {
                var listAdjustTarget = new List<Scene.DeviceListData>();
                listAdjustTarget.AddRange(i_scene.AdjustTargetList);
                return listAdjustTarget;
            }
 
            var result = await Scene.GetSceneDeviceListAsync(i_scene.Id);
            //共通错误检测
            string error = HdlCheckLogic.Current.CheckCommonErrorCode(result);
            if (error != null)
            {
                this.ShowErrorMsg(error);
                return null;
            }
            if (result == null || result.getSceneDeviceListInfo == null)
            {
                //获取执行目标失败
                string msg = Language.StringByID(R.MyInternationalizationString.uGetAdjustTargetFail);
                //拼接上【网关回复超时】的Msg
                msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result);
 
                this.ShowTipMsg(msg);
                return null;
            }
            i_scene.AdjustTargetList = result.getSceneDeviceListInfo.DeviceList;
            //保存缓存
            i_scene.Save(false);
 
            var listData = new List<Scene.DeviceListData>();
            listData.AddRange(result.getSceneDeviceListInfo.DeviceList);
 
            return listData;
        }
        #endregion
 
        #region ■ 延时时间线程_______________________
 
        /// <summary>
        /// 开启延时时间线程(旨在全部地方的同一场景时间同步)
        /// </summary>
        public void StartDelayTimeThread(SceneUI i_scene)
        {
            if (i_scene.RemainThreadAction == true)
            {
                //线程在运行中
                return;
            }
            i_scene.RemainThreadAction = true;
 
            HdlThreadLogic.Current.RunThread(() =>
            {
                while (i_scene.RemainTime > 0)
                {
                    System.Threading.Thread.Sleep(1000);
                    i_scene.RemainTime--;
                }
                i_scene.RemainThreadAction = false;
            });
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 获取本地全部的场景文件
        /// </summary>
        /// <returns></returns>
        public List<string> GetAllSceneFile()
        {
            List<string> listSceneFile = new List<string>();
            List<string> listAllFile = Global.FileListByHomeId();
 
            foreach (string file in listAllFile)
            {
                if (file.StartsWith("Scene_") == false)
                {
                    //如果不是场景文件
                    continue;
                }
                listSceneFile.Add(file);
            }
            return listSceneFile;
        }
 
        /// <summary>
        /// 显示错误信息窗口
        /// </summary>
        /// <param name="msg"></param>
        private void ShowErrorMsg(string msg)
        {
            Application.RunOnMainThread(() =>
            {
                var contr = new ShowMsgControl(ShowMsgType.Error, msg);
                contr.Show();
            });
        }
 
        /// <summary>
        /// 显示Tip信息窗口
        /// </summary>
        /// <param name="msg"></param>
        private void ShowTipMsg(string msg)
        {
            Application.RunOnMainThread(() =>
            {
                var contr = new ShowMsgControl(ShowMsgType.Tip, msg);
                contr.Show();
            });
        }
 
        #endregion
    }
}