黄学彪
2020-12-17 9f326f4000847e6167d8166fa2f6a66f53cb3734
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
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
using Shared.Common;
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
 
namespace Shared.Phone
{
    /// <summary>
    /// 房间的逻辑
    /// </summary>
    public class HdlRoomLogic
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 房间的逻辑
        /// </summary>
        private static HdlRoomLogic m_Current = null;
        /// <summary>
        /// 房间的逻辑
        /// </summary>
        public static HdlRoomLogic Current
        {
            get
            {
                if (m_Current == null)
                {
                    m_Current = new HdlRoomLogic();
                }
                return m_Current;
            }
        }
        /// <summary>
        /// 当前主页选择的房间对象
        /// </summary>
        public Room NowMainPageRoom = null;
        /// <summary>
        /// 当前分类选择的房间对象
        /// </summary>
        public Room NowCategoryRoom = null;
        /// <summary>
        /// 所有的房间信息
        /// </summary>
        private Dictionary<string, Room> dicRooms = new Dictionary<string, Room>();
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 初始化房间信息
        /// 从文件中全部读取所有的房间数据到内存
        /// </summary>
        public void InitAllRoom()
        {
            this.NowMainPageRoom = null;
            this.NowCategoryRoom = null;
 
            //清空本地缓存
            this.dicRooms.Clear();
            //检测我的喜爱这个房间对象
            this.CheckLoveRoom();
 
            var listFile = HdlFileLogic.Current.GetRootPathListFile();
            var listEsixtMac = new List<string>();
            foreach (var fileName in listFile)
            {
                if (fileName.StartsWith("Room_") == true)
                {
                    //从文件里面获取房间对象
                    var room = this.GetRoomByFilePath(fileName);
                    if (room != null)
                    {
                        this.dicRooms[room.Id] = room;
                        //获取已经存在了的Mac
                        listEsixtMac.AddRange(room.ListDeviceMac);
                    }
                }
            }
            //检查真实物理设备的所在的房间时候有遗漏
            foreach (var room in this.dicRooms.Values)
            {
                this.CheckRealDeviceRoom(room, ref listEsixtMac);
            }
 
            //顺便刷新场景
            HdlSceneLogic.Current.ReFreshByLocal();
            //刷新房间视图列表
            this.RefreshRoomListView();
        }
 
        /// <summary>
        /// 从本地重新加载全部的房间
        /// </summary>
        public void RefreshAllRoomByLocation()
        {
            var listFile = HdlFileLogic.Current.GetRootPathListFile();
 
            //我的喜爱的房间必须要在第0位才行
            string fRoom = "Room_Favorite.json";
            if (listFile.Contains(fRoom) == true)
            {
                listFile.Remove(fRoom);
            }
 
            var listRoomFile = new List<string>();
            foreach (string fileName in listFile)
            {
                if (fileName.StartsWith("Room_"))
                {
                    string roomId = fileName.Replace("Room_", string.Empty).Replace(".json", string.Empty);
                    listRoomFile.Add(fileName);
                }
            }
            //检测楼层数据的合法性
            if (HdlUserCenterResourse.ResidenceOption.AuthorityNo == 3)
            {
                this.CheckMemberFloorData(listRoomFile);
            }
            else
            {
                this.CheckAdminFloorData(listRoomFile);
            }
 
            this.InitAllRoom();
        }
 
        /// <summary>
        /// 刷新房间视图列表
        /// </summary>
        public void RefreshRoomListView()
        {
            Application.RunOnMainThread(() =>
            {
                MainPage.LeftListRoomViewFrom.Instance?.RefreshListRoom();
            });
        }
 
        /// <summary>
        /// 检测楼层数据的合法性
        /// </summary>
        /// <param name="listRoomFile"></param>
        private void CheckAdminFloorData(List<string> listRoomFile)
        {
            //没有楼层,则无需处理
            if (Config.Instance.Home.FloorDics.Count == 0) { return; }
 
            //主人或者管理员的话,就检测是否存在未分配楼层的房间
            for (int i = 0; i < listRoomFile.Count; i++)
            {
                try
                {
                    var byteData = Global.ReadFileByHomeId(listRoomFile[i]);
                    string valueData = System.Text.Encoding.UTF8.GetString(byteData);
                    var roomTemp = Newtonsoft.Json.JsonConvert.DeserializeObject<Common.Room>(valueData);
 
                    //检测多个手机来回创建,然后又删除之后,楼层数据不能保证100%同步的问题
                    if (roomTemp.FloorId != string.Empty && Config.Instance.Home.FloorDics.ContainsKey(roomTemp.FloorId) == false)
                    {
                        //未知楼层
                        Config.Instance.Home.FloorDics[roomTemp.FloorId] = Language.StringByID(R.MyInternationalizationString.uUnKnownFloor);
                    }
                }
                catch (Exception ex) { HdlLogLogic.Current.WriteLog(ex); }
            }
        }
 
        /// <summary>
        /// 检测楼层数据的合法性
        /// </summary>
        /// <param name="listRoomFile"></param>
        private void CheckMemberFloorData(List<string> listRoomFile)
        {
            //没有楼层,则无需处理
            if (Config.Instance.Home.FloorDics.Count == 0) { return; }
 
            //成员的话,就检测是否存在没有房间的楼层
            var listEsixtFloor = new List<string>();
            for (int i = 0; i < listRoomFile.Count; i++)
            {
                try
                {
                    var byteData = Global.ReadFileByHomeId(listRoomFile[i]);
                    string valueData = System.Text.Encoding.UTF8.GetString(byteData);
                    var roomTemp = Newtonsoft.Json.JsonConvert.DeserializeObject<Common.Room>(valueData);
                    if (roomTemp.FloorId != string.Empty && listEsixtFloor.Contains(roomTemp.FloorId) == false)
                    {
                        //收集楼层
                        listEsixtFloor.Add(roomTemp.FloorId);
                    }
                }
                catch (Exception ex) { HdlLogLogic.Current.WriteLog(ex); }
            }
            var listDeleteId = new List<string>();
            foreach (var flootId in Config.Instance.Home.FloorDics.Keys)
            {
                if (listEsixtFloor.Contains(flootId) == false)
                {
                    //如果这个id不存在于任何房间的话,收集它
                    listDeleteId.Add(flootId);
                }
            }
            foreach (var deleteId in listDeleteId)
            {
                //删除这个id
                Config.Instance.Home.FloorDics.Remove(deleteId);
            }
            Config.Instance.Home.Save(false);
        }
 
        /// <summary>
        /// 检查真实物理设备的所在的房间时候有遗漏
        /// </summary>
        /// <param name="i_room">房间对象</param>
        /// <param name="listEsixtMac">已经存在了的Mac</param>
        private void CheckRealDeviceRoom(Room i_room, ref List<string> listEsixtMac)
        {
            //不考虑收藏
            if (i_room.IsLove == true) { return; }
 
            bool save = false;
            foreach (var deviceKey in i_room.ListDevice)
            {
                string deviceMac = deviceKey.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries)[0].Trim();
                //如果这个Mac并没有加到它自己这个房间,或者其他房间,则添加进去
                if (i_room.ListDeviceMac.Contains(deviceMac) == false && listEsixtMac.Contains(deviceMac) == false)
                {
                    listEsixtMac.Add(deviceMac);
                    i_room.ListDeviceMac.Add(deviceMac);
                    save = true;
                }
            }
            if (save == true)
            {
                i_room.Save();
            }
        }
 
        #endregion
 
        #region ■ 添加房间___________________________
 
        /// <summary>
        /// 增加房间
        /// </summary>
        /// <param name="room">Room.</param>
        /// <param name="rorefreshRoomViewom">是否刷新房间视图列表界面</param>
        public void AddRoom(Room room, bool refreshRoomView = true)
        {
            if (Global.IsExistsByHomeId(room.FileName) == true)
            {
                return;
            }
            //添加到缓存
            this.dicRooms[room.Id] = room;
            //生成文件
            room.Save();
            //备份
            HdlBackupLogic.Current.AddOrEditorAutoBackFileStatu(room.FileName);
            if (refreshRoomView == true)
            {
                //刷新房间视图列表
                this.RefreshRoomListView();
            }
        }
 
        #endregion
 
        #region ■ 删除房间___________________________
 
        /// <summary>
        /// 删除房间
        /// </summary>
        /// <param name="roomId">房间ID</param>
        /// <param name="refreshLeftView">是否刷新左滑界面(此变量目前是给删除全部房间用的)</param>
        /// <returns></returns>
        public void RemoveRoom(string roomId, bool refreshLeftView = true)
        {
            if (this.NowMainPageRoom != null && this.NowMainPageRoom.Id == roomId)
            {
                //当删除的是主页的房间的时候
                this.NowMainPageRoom = this.GetLoveRoom();
            }
            if (this.NowCategoryRoom != null && this.NowCategoryRoom.Id == roomId)
            {
                //当删除的是分类的房间的时候
                this.NowCategoryRoom = this.GetLoveRoom();
            }
 
            //根据房间Id,获取房间对象
            var room = this.GetRoomById(roomId);
            if (room == null)
            {
                return;
            }
            //删除来自拍照或者系统图库的房间背景图片
            if (room.BackgroundImageType == 1 || room.BackgroundImageType == 2)
            {
                //删除掉原来的自定义图片
                if (Global.IsExistsByHomeId(room.BackgroundImage) == true)
                {
                    Global.DeleteFilebyHomeId(room.BackgroundImage);
                    //删除备份
                    HdlBackupLogic.Current.DeleteAutoBackFileStatu(room.BackgroundImage);
                }
            }
            //我的喜爱
            var loveRoom = this.GetLoveRoom();
            if (loveRoom != null)
            {
                //移除我的喜爱里面的设备
                for (int i = 0; i < room.ListDevice.Count; i++)
                {
                    loveRoom.ListDevice.Remove(room.ListDevice[i]);
                }
                //移除我的喜爱里面的场景
                for (int i = 0; i < room.ListSceneId.Count; i++)
                {
                    loveRoom.ListSceneId.Remove(room.ListSceneId[i]);
                }
                loveRoom.Save(false);
            }
            this.dicRooms.Remove(roomId);
 
            string roomFilePath = room.FileName;
            if (Global.IsExistsByHomeId(roomFilePath) == true)
            {
                //删除文件
                Global.DeleteFilebyHomeId(roomFilePath);
            }
            HdlBackupLogic.Current.DeleteAutoBackFileStatu(roomFilePath);
 
            if (refreshLeftView == true)
            {
                //刷新房间视图列表
                this.RefreshRoomListView();
            }
        }
 
        /// <summary>
        /// 删除全部的房间
        /// </summary>
        public void DeleteAllRoom()
        {
            var listRoomId = new List<string>();
            foreach (var room in this.dicRooms.Values)
            {
                if (room.IsLove == false)
                {
                    listRoomId.Add(room.Id);
                }
            }
            foreach (var roomId in listRoomId)
            {
                this.RemoveRoom(roomId, false);
            }
        }
 
        #endregion
 
        #region ■ 获取房间___________________________
 
        /// <summary>
        /// 获取喜爱房间
        /// </summary>
        /// <returns></returns>
        public Room GetLoveRoom()
        {
            //检测我的喜爱这个房间对象
            this.CheckLoveRoom();
 
            return this.dicRooms["Favorite"];
        }
 
        /// <summary>
        /// 通过路径获取房间
        /// </summary>
        /// <returns>The room by file path.</returns>
        /// <param name="roomFilePath">Room file path.</param>
        private Room GetRoomByFilePath(string roomFilePath)
        {
            try
            {
                var roomFile = Global.ReadFileByHomeId(roomFilePath);
                var nowRoom = Newtonsoft.Json.JsonConvert.DeserializeObject<Room>(Encoding.UTF8.GetString(roomFile));
 
                return nowRoom;
            }
            catch (Exception ex)
            {
                HdlLogLogic.Current.WriteLog(ex);
                return null;
            }
        }
 
        /// <summary>
        /// 根据房间Id,获取房间对象
        /// </summary>
        /// <returns>The room by name.</returns>
        /// <param name="roomId">房间ID</param>
        public Room GetRoomById(string roomId)
        {
            if (this.dicRooms.ContainsKey(roomId) == false)
            {
                return null;
            }
            return this.dicRooms[roomId];
        }
 
        /// <summary>
        /// 根据设备获取房间名字(楼层+房间名)
        /// </summary>
        /// <returns>房间名</returns>
        /// <param name="device">设备对象</param>
        public string GetRoomNameByDevice(CommonDevice device)
        {
            var room = this.GetRoomByDevice(device);
            return this.GetRoomName(room);
        }
 
        /// <summary>
        /// 获取房间名字(楼层+房间名)
        /// </summary>
        /// <returns>房间名</returns>
        /// <param name="room">房间对象</param>
        public string GetRoomName(Room room)
        {
            if (room == null)
            {
                //未分配区域
                return Language.StringByID(R.MyInternationalizationString.uDeviceNotAssignedRoom);
            }
            if (Config.Instance.Home.FloorDics.ContainsKey(room.FloorId) == true)
            {
                //(楼层+房间名)
                return Config.Instance.Home.FloorDics[room.FloorId] + " " + room.Name;
            }
            return room.Name;
        }
 
        /// <summary>
        /// 获取设备所在的房间(没有设置有房间则返回null)
        /// </summary>
        /// <returns>The room by device.</returns>
        /// <param name="device">设备对象</param>
        public Room GetRoomByDevice(CommonDevice device)
        {
            string mainKeys = HdlDeviceCommonLogic.Current.GetDeviceMainKeys(device);
            foreach (var room in this.dicRooms.Values)
            {
                if (room.IsLove == true)
                {
                    //喜爱房间不处理
                    continue;
                }
                if (room.ListDevice.Contains(mainKeys) == true)
                {
                    return room;
                }
            }
            return null;
        }
 
        /// <summary>
        /// 通过场景id获取房间名
        /// </summary>
        /// <returns>The room name by scene identifier.</returns>
        /// <param name="sceneId">Scene identifier.</param>
        public string GetRoomNameBySceneId(int sceneId)
        {
            var room = GetRoomBySceneId(sceneId);
            if (room == null)
            {
                return null;
            }
            return room.Name;
        }
 
        /// <summary>
        /// 通过场景id获取房间对象
        /// </summary>
        /// <returns>The room  by scene identifier.</returns>
        /// <param name="sceneId">Scene identifier.</param>
        public Room GetRoomBySceneId(int sceneId)
        {
            foreach (var room in this.dicRooms.Values)
            {
                if (room.IsLove == true)
                {
                    //喜爱房间不处理
                    continue;
                }
                if (room.ListSceneId.Contains(sceneId) == true)
                {
                    return room;
                }
            }
            return null;
        }
 
        /// <summary>
        /// 获取当前楼层的房间
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<Room> GetRoomsByFloorId(string id)
        {
            var listRoom = new List<Room>();
            if (Config.Instance.Home.FloorDics.Count == 0)
            {
                //没有楼层
                foreach (var room in this.dicRooms.Values)
                {
                    listRoom.Add(room);
                }
            }
            else
            {
                foreach (var room in this.dicRooms.Values)
                {
                    if (room.FloorId == id)
                    {
                        listRoom.Add(room);
                    }
                }
            }
            return this.SortRoom(listRoom);
        }
 
        /// <summary>
        /// 获取全部的房间
        /// </summary>
        /// <returns></returns>
        public List<Room> GetAllListRooms()
        {
            var listRoom = new List<Room>();
            foreach (var room in this.dicRooms.Values)
            {
                listRoom.Add(room);
            }
            return this.SortRoom(listRoom);
        }
 
        /// <summary>
        /// 获取当前楼层的房间(拼接了【常用】在第一位)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<Room> GetRoomsByFloorIdAppendLoveRoom(string id)
        {
            var listRoom = new List<Room>();
            if (Config.Instance.Home.FloorDics.Count == 0)
            {
                //没有楼层
                foreach (var room in this.dicRooms.Values)
                {
                    if (room.IsLove == false)
                    {
                        listRoom.Add(room);
                    }
                }
            }
            else
            {
                foreach (var room in this.dicRooms.Values)
                {
                    if (room.FloorId == id && room.IsLove == false)
                    {
                        listRoom.Add(room);
                    }
                }
            }
            listRoom = this.SortRoom(listRoom);
 
            listRoom.Insert(0, GetLoveRoom());
            return listRoom;
        }
 
        /// <summary>
        /// 获取网关所在的房间
        /// </summary>
        /// <param name="gatewayId">网关ID</param>
        /// <returns></returns>
        public Room GetRoomByGateway(string gatewayId)
        {
            foreach (var room in this.dicRooms.Values)
            {
                if (room.ListDeviceMac.Contains(gatewayId) == true)
                {
                    return room;
                }
            }
            return null;
        }
 
        /// <summary>
        /// 变更网关房间
        /// </summary>
        /// <param name="gwId">网关Id</param>
        /// <param name="roomId">房间ID</param>
        public void ChangedGatewayRoom(string gwId, string roomId)
        {
            var room = this.GetRoomById(roomId);
            if (room != null && room.ListDeviceMac.Contains(gwId) == false)
            {
                room.ListDeviceMac.Add(gwId);
                room.Save();
            }
        }
 
        #endregion
 
        #region ■ 物理设备所属房间___________________
 
        /// <summary>
        /// 保存物理设备所属房间的记录
        /// </summary>
        /// <param name="listDevice">需要保存的设备对象</param>
        /// <param name="roomId">需要保存的哪个设备的房间ID</param>
        /// <param name="saveRoadDevice">如果只有一个回路,是否把回路的房间一起修改</param>
        public void SaveRealDeviceRoomId(List<CommonDevice> listDevice, string roomId, bool saveRoadDevice = true)
        {
            if (listDevice == null || listDevice.Count == 0)
            {
                return;
            }
            //如果设备只有一个回路,如果改变了真实设备区域,则它的回路的区域也一起改了
            if (saveRoadDevice == true && listDevice.Count == 1)
            {
                if ((listDevice[0] is OTADevice) == false)
                {
                    //ota设备不需要处理
                    this.ChangedRoom(listDevice[0], roomId, false);
                }
            }
            if (roomId == string.Empty)
            {
                //真实物理设备所在的房间
                var realRoom = this.GeteRealDeviceRoom(listDevice[0]);
                if (realRoom != null && realRoom.ListDeviceMac.Contains(listDevice[0].DeviceAddr) == true)
                {
                    //选择的是未分配
                    realRoom.ListDeviceMac.Remove(listDevice[0].DeviceAddr);
                    realRoom.Save();
                }
            }
            else
            {
                //真实物理设备所在的房间
                var realRoom = this.GetRoomById(roomId);
                if (realRoom != null && realRoom.ListDeviceMac.Contains(listDevice[0].DeviceAddr) == false)
                {
                    realRoom.ListDeviceMac.Add(listDevice[0].DeviceAddr);
                    realRoom.Save();
                }
            }
        }
 
        /// <summary>
        /// 获取真实物理设备的房间名字
        /// </summary>
        /// <param name="device">设备的某一个回路</param>
        /// <returns></returns>
        public string GeteRealDeviceRoomName(CommonDevice device)
        {
            var room = this.GeteRealDeviceRoom(device);
            if (room == null)
            {
                //未分配区域
                return Language.StringByID(R.MyInternationalizationString.uDeviceNotAssignedRoom);
            }
            return this.GetRoomName(room);
        }
 
        /// <summary>
        /// 获取真实物理设备属于哪个房间
        /// </summary>
        /// <param name="device">设备的某一个回路</param>
        /// <returns></returns>
        public Room GeteRealDeviceRoom(CommonDevice device)
        {
            foreach (var room in this.dicRooms.Values)
            {
                if (room.IsLove == true)
                {
                    //喜爱房间不处理
                    continue;
                }
                if (room.ListDeviceMac.Contains(device.DeviceAddr) == true)
                {
                    return room;
                }
            }
            return null;
        }
 
        /// <summary>
        /// 将真实物理设备从房间中移除
        /// </summary>
        /// <param name="device">随便一个回路</param>
        public void DeleteRealDeviceFromRoom(CommonDevice device)
        {
            //将真实物理设备从房间中移除
            this.DeleteRealDeviceFromRoom(new List<string>() { device.DeviceAddr });
        }
 
        /// <summary>
        /// 将真实物理设备从房间中移除
        /// </summary>
        /// <param name="listMac">设备Mac地址</param>
        public void DeleteRealDeviceFromRoom(List<string> listMac)
        {
            foreach (var room in this.dicRooms.Values)
            {
                bool save = false;
                foreach (var strMac in listMac)
                {
                    if (room.ListDeviceMac.Contains(strMac) == true)
                    {
                        room.ListDeviceMac.Remove(strMac);
                        save = true;
                    }
                }
                if (save == true)
                {
                    room.Save();
                }
            }
        }
 
        #endregion
 
        #region ■ 房间方法___________________________
 
        /// <summary>
        /// 设备的房间变更
        /// </summary>
        /// <param name="device">设备对象</param>
        /// <param name="roomId">新房间Id</param>
        /// <param name="saveRealRoom">是否修改真实物理设备的房间,不出意外,这个值默认为true即可</param>
        public void ChangedRoom(CommonDevice device, string roomId, bool saveRealRoom = true)
        {
            //选择的是未分配
            if (roomId == string.Empty)
            {
                //从原来的房间移除设备
                this.DeleteDevice(device);
                //移除我的喜爱
                this.DeleteLoveDevice(device);
                //设备改变房间的话,主页需要重新刷新
                UserView.UserPage.Instance.RefreshAllForm = true;
                return;
            }
            //房间是否修改
            if (this.IsRoomChanged(device, roomId) == false)
            {
                return;
            }
            //从原来的房间移除设备,这里不删除真实设备的房间
            this.DeleteDevice(device, false);
 
            //添加到新的房间
            var room = this.GetRoomById(roomId);
            if (room != null)
            {
                this.AddDevice(room, device, saveRealRoom);
            }
            //设备改变房间的话,主页需要重新刷新
            UserView.UserPage.Instance.RefreshAllForm = true;
        }
 
        /// <summary>
        /// 房间名字是否有修改
        /// </summary>
        /// <param name="device">设备对象</param>
        /// <param name="roomId">新房间Id</param>
        /// <returns></returns>
        public bool IsRoomChanged(CommonDevice device, string roomId)
        {
            var room = this.GetRoomByDevice(device);
            if (room == null || room.Id != roomId)
            {
                return true;
            }
            return false;
        }
 
        #endregion
 
        #region ■ 添加设备___________________________
 
        /// <summary>
        /// 添加设备
        /// </summary>
        /// <param name="i_room">房间对象</param>
        /// <param name="device">要添加的设备对象</param>
        /// <param name="saveRealRoom">是否修改真实物理设备的房间,不出意外,这个值默认为true即可</param>
        public void AddDevice(Room i_room, CommonDevice device, bool saveRealRoom)
        {
            if (device == null)
            {
                return;
            }
            //设备信息保存到本地
            device.Save();
 
            string mainkeys = HdlDeviceCommonLogic.Current.GetDeviceMainKeys(device);
            if (i_room.ListDevice.Contains(mainkeys) == false)
            {
                //保存到本地
                i_room.ListDevice.Add(mainkeys);
                i_room.Save();
 
                if (i_room.IsLove == false && saveRealRoom == true && HdlDeviceCommonLogic.Current.GetDevicesCountByMac(device.DeviceAddr) == 1)
                {
                    //如果只有一个回路,则修改真实物理设备的房间
                    this.SaveRealDeviceRoomId(new List<CommonDevice>() { device }, i_room.Id, false);
                }
            }
        }
 
        /// <summary>
        /// 添加喜爱设备
        /// </summary>
        /// <param name="device">要添加的设备对象</param>
        public void AddLoveDevice(CommonDevice device)
        {
            if (device == null)
            {
                return;
            }
            //我的喜爱
            var loveRoom = this.GetLoveRoom();
            if (loveRoom != null)
            {
                string mainkeys = HdlDeviceCommonLogic.Current.GetDeviceMainKeys(device);
                if (loveRoom.ListDevice.Contains(mainkeys) == false)
                {
                    //保存到本地
                    loveRoom.ListDevice.Add(mainkeys);
                    loveRoom.Save();
                    //添加收藏设备时,需要刷新主页
                    UserView.UserPage.Instance.RefreshAllForm = true;
                }
            }
        }
 
        #endregion
 
        #region ■ 删除设备___________________________
 
        /// <summary>
        /// 删除设备(这个函数不删除我的喜爱)
        /// </summary>
        /// <param name="device">要删除的设备对象</param>
        /// <param name="deleteReal">是否删除真实物理设备的房间</param>
        public void DeleteDevice(CommonDevice device, bool deleteReal = true)
        {
            if (device == null)
            {
                return;
            }
            //如果它只有一个回路
            if (deleteReal == true && HdlDeviceCommonLogic.Current.GetDevicesCountByMac(device.DeviceAddr) == 1)
            {
                //删除掉它的真实物理设备的所在位置
                HdlRoomLogic.Current.DeleteRealDeviceFromRoom(device);
            }
 
            //根据设备,获取所在的房间
            var room = this.GetRoomByDevice(device);
            if (room == null)
            {
                return;
            }
            //移除缓存
            string mainkeys = HdlDeviceCommonLogic.Current.GetDeviceMainKeys(device);
            room.ListDevice.Remove(mainkeys);
            room.Save();
            //更改自动备份
            HdlBackupLogic.Current.AddOrEditorAutoBackFileStatu(room.FileName);
 
            //递归:删除掉以前的旧数据导致的多个房间的问题
            this.DeleteDevice(device, deleteReal);
        }
 
        /// <summary>
        /// 删除我的喜爱的设备
        /// </summary>
        /// <param name="device">要删除的设备对象</param>
        public void DeleteLoveDevice(CommonDevice device)
        {
            if (device == null)
            {
                return;
            }
            //我的喜爱
            var loveRoom = this.GetLoveRoom();
            if (loveRoom != null)
            {
                //移除缓存
                string mainkeys = HdlDeviceCommonLogic.Current.GetDeviceMainKeys(device);
                loveRoom.ListDevice.Remove(mainkeys);
                loveRoom.Save();
                //更改自动备份
                HdlBackupLogic.Current.AddOrEditorAutoBackFileStatu(loveRoom.FileName);
                //添加收藏设备时,需要刷新主页
                UserView.UserPage.Instance.RefreshAllForm = true;
            }
        }
 
        #endregion
 
        #region ■ 获取设备___________________________
 
        /// <summary>
        /// 获取当前房间下的全部设备
        /// </summary>
        /// <returns></returns>
        public List<CommonDevice> GetRoomListDevice(Room i_room)
        {
            var dicSort = new Dictionary<string, List<int>>();
            foreach (var mainKeys in i_room.ListDevice)
            {
                var device = HdlDeviceCommonLogic.Current.GetDevice(mainKeys);
                if (device != null)
                {
                    if (dicSort.ContainsKey(device.DeviceAddr) == false)
                    {
                        dicSort[device.DeviceAddr] = new List<int>();
                    }
                    dicSort[device.DeviceAddr].Add(device.DeviceEpoint);
                }
            }
            var listDevice = new List<CommonDevice>();
            foreach (var strMac in dicSort.Keys)
            {
                var listEpont = dicSort[strMac];
                //排序
                listEpont.Sort();
                for (int i = 0; i < listEpont.Count; i++)
                {
                    var device = HdlDeviceCommonLogic.Current.GetDevice(strMac, listEpont[i]);
                    listDevice.Add(device);
                }
            }
            return listDevice;
        }
 
        /// <summary>
        /// 获取房间设备类型
        /// </summary>
        /// <param name="room"></param>
        /// <returns></returns>
        public List<DeviceType> GetDeviceTypes(Room i_room)
        {
            var typeList = new List<DeviceType>();
            foreach (var mainKeys in i_room.ListDevice)
            {
                var device = HdlDeviceCommonLogic.Current.GetDevice(mainKeys);
                if (device != null)
                {
                    typeList.Add(device.Type);
                }
            }
            return typeList;
        }
 
        /// <summary>
        /// 获取该类型的设备
        /// </summary>
        /// <param name="room"></param>
        /// <param name="deviceType"></param>
        /// <returns></returns>
        public List<CommonDevice> GetRoomListDevice(Room room, DeviceType deviceType)
        {
            List<CommonDevice> typeList = new List<CommonDevice>();
            foreach (var mainKeys in room.ListDevice)
            {
                var device = HdlDeviceCommonLogic.Current.GetDevice(mainKeys);
                if (device != null && device.Type == deviceType)
                {
                    typeList.Add(device);
                }
            }
 
            return typeList;
        }
 
        /// <summary>
        /// 获取未分配区域设备
        /// </summary>
        /// <returns></returns>
        public List<CommonDevice> GetUnalloctedDevice()
        {
            var listDevice = new List<CommonDevice>();
 
            //已经存在的设备
            var listEsxit = new HashSet<string>();
            foreach (var room in this.dicRooms.Values)
            {
                if (room.IsLove == true)
                {
                    //不包含收藏房间
                    continue;
                }
                foreach (string mainkeys in room.ListDevice)
                {
                    if (listEsxit.Contains(mainkeys) == false)
                    {
                        listEsxit.Add(mainkeys);
                    }
                }
            }
 
            //所有设备
            var commonDeviceList = HdlDeviceCommonLogic.Current.listAllDevice;
            foreach (var device in commonDeviceList)
            {
                //判断该设备能否显示在主页
                if (HdlDeviceCommonLogic.Current.CanShowInHomeHomeMainPage(device) == false)
                {
                    continue;
                }
                string mainkeys = HdlDeviceCommonLogic.Current.GetDeviceMainKeys(device);
                if (listEsxit.Contains(mainkeys) == false)
                {
                    listDevice.Add(device);
                }
            }
            return listDevice;
        }
 
        /// <summary>
        /// 获取全部房间全部的设备
        /// </summary>
        /// <returns></returns>
        public List<CommonDevice> GetAllRoomListDevice()
        {
            var listDevice = new List<CommonDevice>();
            foreach (var room in this.dicRooms.Values)
            {
                if (room.IsSharedRoom || room.IsLove)
                {
                    continue;
                }
                listDevice.AddRange(this.GetRoomListDevice(room));
            }
            return listDevice;
        }
 
        #endregion
 
        #region ■ 设备是否收藏_______________________
 
        /// <summary>
        /// 是否是收藏设备
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public bool IsCollectInRoom(CommonDevice device)
        {
            string mainkeys = HdlDeviceCommonLogic.Current.GetDeviceMainKeys(device);
            //喜爱房间
            var room = this.GetLoveRoom();
            return room.ListDevice.Contains(mainkeys);
        }
 
        /// <summary>
        /// 是否是收藏设备
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public bool IsCollectInRoom(SceneUI scene)
        {
            //喜爱房间
            var room = this.GetLoveRoom();
            return room.ListSceneId.Contains(scene.Id);
        }
 
        #endregion
 
        #region ■ 楼层和房间顺序相关_________________
 
        /// <summary>
        /// 根据楼层的主键获取排序后的房间列表(支持无楼层模式,不获取收藏房间)
        /// </summary>
        /// <param name="i_floorKeys">楼层的主键</param>
        /// <param name="getShard">分享的房间是否也获取</param>
        /// <returns></returns>
        public List<Room> GetFloorSortRoom(string i_floorKeys, bool getShard = true)
        {
            if (i_floorKeys == null)
            {
                i_floorKeys = string.Empty;
            }
            var listRoom = new List<Room>();
            foreach (var room in this.dicRooms.Values)
            {
                if (room.FloorId != i_floorKeys && i_floorKeys != string.Empty
                    || room.IsLove == true)
                {
                    //不是同一个楼层,不要我的喜爱
                    continue;
                }
                if (getShard == false && room.IsSharedRoom == true)
                {
                    //不要分享的房间
                    continue;
                }
                listRoom.Add(room);
            }
            return this.SortRoom(listRoom);
        }
 
        /// <summary>
        /// 排序房间对象
        /// </summary>
        /// <param name="listRoom"></param>
        /// <returns></returns>
        public List<Room> SortRoom(List<Room> listRoom)
        {
            //从一堆文字中,获取这一堆文字里面数字字符串的最长长度
            var listName = new List<string>();
            foreach (var room in listRoom)
            {
                listName.Add(room.Name);
            }
            int numberLength = HdlCommonLogic.Current.GetNumberMaxLength(listName);
 
            var listSort = new List<string[]>();
            foreach (var room in listRoom)
            {
                var strArry = new string[2];
                strArry[0] = room.Id;
                strArry[1] = string.Empty;
 
                string value = string.Empty;
                foreach (var c in room.Name)
                {
                    if (char.IsNumber(c) == true)
                    {
                        //数字
                        value += c.ToString();
                        continue;
                    }
                    else if (value != string.Empty)
                    {
                        //如果房间名字带有数字的话,则左边加零,因为这里有个排序的问题
                        strArry[1] += value.PadLeft(numberLength, '0');
                        value = string.Empty;
                    }
                    strArry[1] += c.ToString();
                }
                if (value != string.Empty)
                {
                    //以数字结尾的话
                    strArry[1] += value.PadLeft(numberLength, '0');
                }
                listSort.Add(strArry);
            }
            //排序
            listSort.Sort((obj1, obj2) =>
            {
                if (obj1[1].CompareTo(obj2[1]) > 0)
                {
                    return 1;
                }
                return -1;
            });
            var listSortRoom = new List<Room>();
            foreach (var strArry in listSort)
            {
                var room = this.GetRoomById(strArry[0]);
                listSortRoom.Add(room);
            }
            return listSortRoom;
        }
 
        /// <summary>
        /// 获取排序后的楼层
        /// </summary>
        /// <param name="i_dicFloor">需要排序的楼层集合,如果设置为null,则会返回当前住宅排序了的楼层</param>
        /// <returns></returns>
        public Dictionary<string, string> GetFloorSortList(Dictionary<string, string> i_dicFloor = null)
        {
            if (i_dicFloor == null)
            {
                i_dicFloor = Config.Instance.Home.FloorDics;
            }
            //没有楼层
            if (i_dicFloor.Count == 0)
            {
                return new Dictionary<string, string>();
            }
            //从一堆文字中,获取这一堆文字里面数字字符串的最长长度
            var listName = new List<string>();
            foreach (var floorName in i_dicFloor.Values)
            {
                listName.Add(floorName);
            }
            int numberLength = HdlCommonLogic.Current.GetNumberMaxLength(listName);
 
            var listSort = new List<string[]>();
            foreach (var floorId in i_dicFloor.Keys)
            {
                var strArry = new string[2];
                strArry[0] = floorId;
                strArry[1] = string.Empty;
 
                string value = string.Empty;
                string floorName = i_dicFloor[floorId];
                foreach (var c in floorName)
                {
                    if (char.IsNumber(c) == true)
                    {
                        //数字
                        value += c.ToString();
                        continue;
                    }
                    else if (value != string.Empty)
                    {
                        //如果房间名字带有数字的话,则左边加零,因为这里有个排序的问题
                        strArry[1] += value.PadLeft(numberLength, '0');
                        value = string.Empty;
                    }
                    strArry[1] += c.ToString();
                }
                if (value != string.Empty)
                {
                    //以数字结尾的话
                    strArry[1] += value.PadLeft(numberLength, '0');
                }
                listSort.Add(strArry);
            }
            //楼层排序
            listSort.Sort((obj1, obj2) =>
            {
                if (obj1[1].CompareTo(obj2[1]) > 0)
                {
                    return 1;
                }
                return -1;
            });
            var dic = new Dictionary<string, string>();
            foreach (var strArry in listSort)
            {
                dic[strArry[0]] = i_dicFloor[strArry[0]];
            }
            return dic;
        }
 
        #endregion
 
        #region ■ 发送房间信息给网关_________________
 
        /// <summary>
        /// 发送房间信息给网关
        /// </summary>
        public void SetRoomInfoToGateway()
        {
            if (HdlUserCenterResourse.ResidenceOption.AuthorityNo != 1 && HdlUserCenterResourse.ResidenceOption.AuthorityNo != 2)
            {
                //不是主人和管理员,则不处理
                return;
            }
            var realMain = ZbGateway.MainGateWay;
            if (realMain == null || Common.Config.Instance.Home.IsVirtually == true)
            {
                //没有主网关,则不理
                return;
            }
            //获取房间全部的设备对象
            string strSendDeviceRoomInfo = string.Empty;
            foreach (var room in this.dicRooms.Values)
            {
                if (room.IsSharedRoom || room.IsLove)
                {
                    continue;
                }
                var listDevice = this.GetRoomListDevice(room);
                //发送给网关的设备房间json
                foreach (var device in listDevice)
                {
                    if (strSendDeviceRoomInfo != string.Empty)
                    {
                        strSendDeviceRoomInfo += "\r\n";
                    }
                    strSendDeviceRoomInfo += " " + this.SetDoublMark(device.DeviceAddr) + " ";
                    strSendDeviceRoomInfo += this.SetDoublMark("0x" + Convert.ToString(device.DeviceEpoint, 16).PadLeft(2, '0')) + " ";
                    strSendDeviceRoomInfo += this.SetDoublMark(HdlDeviceCommonLogic.Current.GetDeviceEpointName(device).Replace(HdlUserCenterResourse.douMarks, string.Empty)) + " ";
                    strSendDeviceRoomInfo += this.SetDoublMark(room.Id);
                }
            }
 
            var listAreaSpaceInfo = new List<AreaSpaceInfo>();
            //首先添加住宅
            var houseInfo = new AreaSpaceInfo();
            houseInfo.name = Common.Config.Instance.Home.Name;
            houseInfo.uid = Common.Config.Instance.Home.Id;
            houseInfo.parentId = "null";
            listAreaSpaceInfo.Add(houseInfo);
            //然后添加楼层
            foreach (var floorId in Common.Config.Instance.Home.FloorDics.Keys)
            {
                var floorInfo = new AreaSpaceInfo();
                floorInfo.name = Common.Config.Instance.Home.FloorDics[floorId];
                floorInfo.uid = floorId;
                floorInfo.parentId = Common.Config.Instance.Home.Id;
                listAreaSpaceInfo.Add(floorInfo);
            }
            //然后添加房间
            var listRoom = this.GetAllListRooms();
            foreach (var room in listRoom)
            {
                if (room.IsLove == true) { continue; }
 
                var roomInfo = new AreaSpaceInfo();
                roomInfo.name = room.Name;
                roomInfo.uid = room.Id;
                if (Common.Config.Instance.Home.FloorDics.ContainsKey(room.FloorId) == true)
                {
                    roomInfo.parentId = room.FloorId;
                }
                else
                {
                    roomInfo.parentId = Common.Config.Instance.Home.Id;
                }
                listAreaSpaceInfo.Add(roomInfo);
            }
            //然后添加网关房间信息
            var gatewayInfo = new GatewayAreaSpaceInfo();
 
            var loaclGateway = HdlGatewayLogic.Current.GetLocalGateway(realMain.GwId);
            if (loaclGateway == null || loaclGateway.GwMac == string.Empty)
            {
                //没有mac,或者找不到对象,则不上传
                return;
            }
            string gwMac = loaclGateway.GwMac.Replace(":", string.Empty);
            gatewayInfo.uid = "000101" + gwMac.Substring(2) + "07";
            gatewayInfo.name = HdlGatewayLogic.Current.GetGatewayName(loaclGateway);
 
            var roomGateway = this.GetRoomByGateway(loaclGateway.GwId);
            if (roomGateway != null)
            {
                gatewayInfo.parentId = roomGateway.Id;
            }
            gatewayInfo.hwInfo = new GatewayHwInfo();
            gatewayInfo.pkgInfo = new GatewayPkgInfo { version = loaclGateway.LinuxFirmwareVersion.ToString() };
            gatewayInfo.swInfo = new List<GatewaySwInfo> { new GatewaySwInfo { ver = loaclGateway.LinuxFirmwareVersion.ToString() } };
            gatewayInfo.ip = loaclGateway.GwIP;
            gatewayInfo.mac = loaclGateway.GwMac;
 
            var data = new { spaces = listAreaSpaceInfo, gateway = gatewayInfo };
            string strSendRoomInfo = Newtonsoft.Json.JsonConvert.SerializeObject(data);
 
            HdlThreadLogic.Current.RunThread(async () =>
            {
                //发送App的房间备份信息到网关(将来可能有用)
                await this.SetAppRoomInfoBackUpToGateway();
 
                //发送设备区域信息
                if (strSendDeviceRoomInfo != string.Empty)
                {
                    //创建文件对象
                    var result0 = await realMain.CreateFileAsync("DeviceRoomInfo.json");
                    if (result0 == null)
                    {
                        return;
                    }
                    if (result0.Result != 0 && result0.Result != 2)
                    {
                        //如果是2,允许上传
                        return;
                    }
                    //发送数据流
                    var byteData = ASCIIEncoding.UTF8.GetBytes(strSendDeviceRoomInfo);
                    var result1 = await realMain.SendFileAsync(byteData);
                    if (result1 == null || result1.Result != 0)
                    {
                        return;
                    }
                }
 
                //创建文件对象
                var result3 = await realMain.CreateFileAsync("space.json");
                if (result3 == null)
                {
                    return;
                }
                if (result3.Result != 0 && result3.Result != 2)
                {
                    //如果是2,允许上传
                    return;
                }
 
                //发送区域信息
                var byteData2 = ASCIIEncoding.UTF8.GetBytes(strSendRoomInfo);
                var result4 = await realMain.SendFileAsync(byteData2);
            });
        }
 
        /// <summary>
        /// 发送App的房间备份信息到网关(将来可能有用)
        /// </summary>
        /// <returns></returns>
        private async System.Threading.Tasks.Task SetAppRoomInfoBackUpToGateway()
        {
            //生成房间和楼层的json
            var roomInfo = string.Empty;
            HdlTemplateCommonLogic.Current.CrearWriteRoomTemplateData(ref roomInfo);
 
            var realMain = ZbGateway.MainGateWay;
            //创建文件对象
            var result0 = await realMain.CreateFileAsync("AppFloorRoomInfo.json");
            if (result0 == null)
            {
                return;
            }
            if (result0.Result != 0 && result0.Result != 2)
            {
                //如果是2,允许上传
                return;
            }
            //发送数据流
            var byteData = ASCIIEncoding.UTF8.GetBytes(roomInfo);
            var result1 = await realMain.SendFileAsync(byteData);
            if (result1 == null || result1.Result != 0)
            {
                return;
            }
        }
 
        /// <summary>
        /// 设置双引号
        /// </summary>
        /// <param name="i_text"></param>
        /// <returns></returns>
        private string SetDoublMark(string i_text)
        {
            return HdlUserCenterResourse.douMarks + i_text + HdlUserCenterResourse.douMarks;
        }
 
        /// <summary>
        /// 空间区域信息
        /// </summary>
        private class AreaSpaceInfo
        {
            /// <summary>
            /// 住宅ID 或者 楼层ID 或者 房间ID
            /// </summary>
            public string uid = string.Empty;
            /// <summary>
            /// 住宅名称 或者 楼层名称 或者 房间名称
            /// </summary>
            public string name = string.Empty;
            /// <summary>
            /// 当前ID的父ID,如果没有,则为带双引号的"null" 比如 楼层ID就填住宅ID  房间ID就填楼层ID
            /// </summary>
            public string parentId = string.Empty;
        }
 
        /// <summary>
        /// 网关空间区域信息
        /// </summary>
        private class GatewayAreaSpaceInfo
        {
            /// <summary>
            /// 【000101】+绑定网关的时间戳转16进制+【07】
            /// </summary>
            public string uid = string.Empty;
            /// <summary>
            /// 这个固定
            /// </summary>
            public string name = "hdl_bus_gatway";
            /// <summary>
            /// 网关所在的房间ID
            /// </summary>
            public string parentId = string.Empty;
            /// <summary>
            /// 这个固定
            /// </summary>
            public int deviceType = 61184;
            /// <summary>
            /// 网关硬件信息
            /// </summary>
            public GatewayHwInfo hwInfo = null;
            /// <summary>
            /// 网关标准信息
            /// </summary>
            public GatewayPkgInfo pkgInfo = null;
            /// <summary>
            /// 网关固件版本
            /// </summary>
            public List<GatewaySwInfo> swInfo = null;
            /// <summary>
            /// 网关IP
            /// </summary>
            public string ip = string.Empty;
            /// <summary>
            /// 网关的Mac
            /// </summary>
            public string mac = string.Empty;
        }
 
        /// <summary>
        /// 网关硬件信息
        /// </summary>
        private class GatewayHwInfo
        {
            /// <summary>
            /// 这个固定
            /// </summary>
            public string brand = "MIPS";
            /// <summary>
            /// 这个固定
            /// </summary>
            public string model = "MT7688";
        }
 
        /// <summary>
        /// 网关标准信息
        /// </summary>
        private class GatewayPkgInfo
        {
            /// <summary>
            /// 这个是Linux的固件版本
            /// </summary>
            public string version = string.Empty;
        }
 
        /// <summary>
        /// 网关固件版本
        /// </summary>
        private class GatewaySwInfo
        {
            /// <summary>
            /// 这个固定
            /// </summary>
            public string name = "HDL";
            /// <summary>
            /// 这个是Linux的固件版本
            /// </summary>
            public string ver = string.Empty;
        }
 
        #endregion
 
        #region ■ 克隆房间对象_______________________
 
        /// <summary>
        /// 克隆房间对象
        /// </summary>
        /// <returns></returns>
        public Room CloneRoomClass(Room i_room)
        {
            var newRoom = new Room();
            //克隆属性
            newRoom.Id = i_room.Id;
            newRoom.FloorId = i_room.FloorId;
            newRoom.TemperatrueDevice = i_room.TemperatrueDevice;
            newRoom.HumidityDevice = i_room.HumidityDevice;
            newRoom.Name = i_room.Name;
            newRoom.BackgroundImage = i_room.BackgroundImage;
            newRoom.BackgroundImageType = i_room.BackgroundImageType;
 
            return newRoom;
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 获取房间总数
        /// </summary>
        /// <returns></returns>
        public int GetRoomCount()
        {
            return this.dicRooms.Count;
        }
 
        /// <summary>
        /// 检测我的喜爱这个房间对象
        /// </summary>
        private void CheckLoveRoom()
        {
            if (this.dicRooms.ContainsKey("Favorite") == true)
            {
                return;
            }
            //读取本地我的喜爱文件
            string favoriteFile = System.IO.Path.Combine(Common.Config.Instance.FullPath, "Room_Favorite.json");
            var fileContent = HdlFileLogic.Current.ReadFileTextContent(favoriteFile);
            if (fileContent != null)
            {
                var love = Newtonsoft.Json.JsonConvert.DeserializeObject<Room>(fileContent);
                this.dicRooms["Favorite"] = love;
            }
            else
            {
                //默认添加喜爱的房间--禁止修改房间名
                var love = new Room { Name = Language.StringByID(R.MyInternationalizationString.Favorite), BackgroundImage = "RoomIcon/0.jpg", Id = "Favorite" };
                love.Save(false);
                this.dicRooms["Favorite"] = love;
            }
        }
 
        #endregion
    }
}