黄学彪
2020-12-16 0d9f64668fd7350d6a21fd157e32009a96d98134
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
using Shared.Common;
using System;
using System.Collections.Generic;
using System.Text;
 
namespace Shared.Phone
{
    /// <summary>
    /// 住宅对象的逻辑
    /// </summary>
    public class HdlResidenceLogic
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 住宅对象的逻辑
        /// </summary>
        private static HdlResidenceLogic m_Current = null;
        /// <summary>
        /// 住宅对象的逻辑
        /// </summary>
        public static HdlResidenceLogic Current
        {
            get
            {
                if (m_Current == null)
                {
                    m_Current = new HdlResidenceLogic();
                }
                return m_Current;
            }
        }
 
        #endregion
 
        #region ■ 创建新住宅_________________________
 
        /// <summary>
        /// 创建新的住宅(返回住宅id,null代表失败)
        /// </summary>
        /// <param name="residenceName">住宅名称</param>
        /// <param name="listUintName">自定义单元的标题名称(homeApp设置为null)</param>
        /// <param name="listUintContent">自定义单元的内容(homeApp设置为null)</param>
        /// <param name="TemplateMode">1:无模板有设备  2:有模板有设备  3:无模板无设备</param>
        public string CreatNewResidence(string residenceName, List<string> listUintName,
            List<string> listUintContent, int TemplateMode = 1)
        {
            var pra = new { homeId = 0, homeName = residenceName, homeType = "ZIGBEE" };
            //添加住宅
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/home/add", RestSharp.Method.POST, pra);
            //检测状态码
            if (HdlCheckLogic.Current.CheckNetCode(result, ShowNetCodeMode.YES) == false)
            {
                return null;
            }
            var newInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<IdInfoClass>(result.Data.ToString());
 
            if (listUintName != null)
            {
                //编辑住宅区域
                var success = this.EditorResidenceArea(newInfo.Id, listUintName, listUintContent);
                if (success == false)
                {
                    //编辑住宅区域失败
                    HdlMessageLogic.Current.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uEditorResidenceAreaFail));
                }
            }
 
            //添加住宅到缓存
            this.AddHouseToMemmory(newInfo.Id, residenceName, listUintName,
                listUintContent, TemplateMode);
 
            return newInfo.Id;
        }
 
        /// <summary>
        /// 添加住宅到缓存
        /// </summary>
        /// <param name="houseId">住宅id</param>
        /// <param name="houseName">住宅名称.</param>
        /// <param name="listUintName">自定义单元的标题名称(homeApp设置为null)</param>
        /// <param name="listUintContent">自定义单元的内容(homeApp设置为null)</param>
        /// <param name="TemplateMode">1:无模板有设备  2:有模板有设备  3:无模板无设备</param>
        /// <param name="isOthreShare">是否为其他主用户分享过来的住宅</param>
        /// <param name="accountType">仅子账号登陆的时候使用,当【IsOthreShare】为"true",并且【AccountType】为"1"时,该账号拥有管理员权限</param>
        public void AddHouseToMemmory(string houseId, string houseName,
            List<string> listUintName, List<string> listUintContent, int TemplateMode = 1,
            bool isOthreShare = false, int accountType = 0)
        {
            var home = new Common.House();
            home.Id = houseId;
            home.Name = houseName;
            home.IsOtherShare = isOthreShare;
            home.AccountType = accountType;
 
            if (listUintName != null)
            {
                home.ListUintName.AddRange(listUintName);
                home.ListUintContent.AddRange(listUintContent);
            }
            home.TemplateMode = TemplateMode;
            if (TemplateMode == 3)
            {
                //标记为虚拟住宅
                home.IsVirtually = true;
            }
 
            //创建文件夹
            Common.Global.CreateHomeDirectory(houseId);
            home.Save();
            Common.Config.Instance.HomeFilePathList.Add(home.FileName);
            Common.Config.Instance.Save();
            //如果当前没有住宅的话
            if (Common.Config.Instance.Home.Id == string.Empty)
            {
                Common.Config.Instance.Home = this.GetHouseByFilePath(home.FileName);
                Common.Config.Instance.HomeId = home.Id;
            }
        }
 
 
        #endregion
 
        #region ■ 切换住宅___________________________
 
        /// <summary>
        /// 切换住宅(注:它只切换内存,界面并未处理)
        /// </summary>
        /// <param name="residenceId"></param>
        /// <returns></returns>
        public bool SwitchResidence(string residenceId)
        {
            //打开进度条
            ProgressBar.Show();
 
            Config.Instance.HomeId = residenceId;
            //重新初始化住宅对象
            Config.Instance.Home = this.GetHouseByHouseId(residenceId);
            Config.Instance.Save();
            //刷新个人中心的内存及线程
            bool result = HdlUserCenterLogic.Current.InitUserCenterMenmoryAndThread(false);
 
            //关闭进度条
            ProgressBar.Close();
 
            return result;
        }
 
        #endregion
 
        #region ■ 编辑住宅___________________________
 
        /// <summary>
        /// 编辑住宅
        /// </summary>
        /// <param name="residenceId">住宅id</param>
        /// <param name="residenceName">住宅名称</param>
        /// <param name="listUintName">自定义单元的标题名称(homeApp设置为null)</param>
        /// <param name="listUintContent">自定义单元的内容(homeApp设置为null)</param>
        /// <param name="TemplateMode">1:无模板有设备  2:有模板有设备  3:无模板无设备</param>
        /// <returns></returns>
        public bool EditorResidenceName(string residenceId, string residenceName,
            List<string> listUintName, List<string> listUintContent, int TemplateMode = 1)
        {
            var pra = new { homeId = Config.Instance.Home.Id, homeType = "ZIGBEE", homeName = residenceName };
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/home/update", RestSharp.Method.POST, pra, null, null, CheckMode.A账号权限);
            //检测状态码
            if (HdlCheckLogic.Current.CheckNetCode(result, ShowNetCodeMode.YES) == false)
            {
                return false;
            }
            if (listUintName != null)
            {
                //编辑住宅区域
                var success = this.EditorResidenceArea(residenceId, listUintName, listUintContent);
                if (success == false)
                {
                    //编辑住宅区域失败
                    HdlMessageLogic.Current.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uEditorResidenceAreaFail));
                }
            }
            //刷新内存的住宅名
            this.EditorHouseByHouseId(residenceId, residenceName, listUintName, listUintContent, TemplateMode);
 
            return true;
        }
 
        /// <summary>
        /// 编辑缓存住宅
        /// </summary>
        /// <param name="residenceId">住宅id</param>
        /// <param name="residenceName">住宅名称</param>
        /// <param name="listUintName">自定义单元的标题名称(homeApp设置为null)</param>
        /// <param name="listUintContent">自定义单元的内容(homeApp设置为null)</param>
        /// <param name="TemplateMode">1:无模板有设备  2:有模板有设备  3:无模板无设备</param>
        public void EditorHouseByHouseId(string residenceId, string residenceName,
            List<string> listUintName, List<string> listUintContent, int TemplateMode = 1)
        {
            House home = Config.Instance.Home;
            if (Config.Instance.Home.Id != residenceId)
            {
                //从缓存中获取
                home = GetHouseByHouseId(residenceId);
            }
            if (home == null)
            {
                return;
            }
            home.Name = residenceName;
            if (listUintName != null)
            {
                home.ListUintName.Clear();
                home.ListUintName.AddRange(listUintName);
 
                home.ListUintContent.Clear();
                home.ListUintContent.AddRange(listUintContent);
            }
            home.TemplateMode = TemplateMode;
            if (TemplateMode == 3)
            {
                //标记为虚拟住宅
                home.IsVirtually = true;
            }
            home.Save();
            //住宅修改名称的话,主页需要重新刷新
            UserView.UserPage.Instance.RefreshAllForm = true;
        }
 
        /// <summary>
        /// 编辑住宅区域
        /// </summary>
        /// <param name="residenceId">住宅id</param>
        /// <param name="listUintName">自定义单元的标题名称</param>
        /// <param name="listUintContent">自定义单元的内容</param>
        /// <returns></returns>
        private bool EditorResidenceArea(string residenceId, List<string> listUintName, List<string> listUintContent)
        {
            //☆マーク☆ 住宅区域
            return true;
        }
 
        #endregion
 
        #region ■ 编辑住宅经纬度_____________________
 
        /// <summary>
        /// 编辑住宅经纬度
        /// </summary>
        /// <param name="i_latitude">纬度</param>
        /// <param name="i_longitude">经度</param>
        /// <param name="i_addresName">位置名称</param>
        /// <param name="mode">失败时是否显示tip消息</param>
        /// <returns></returns>
        public bool EditorResidenceLongitudeAndLatitude(double i_latitude, double i_longitude, string i_addresName, ShowNetCodeMode mode = ShowNetCodeMode.YES)
        {
            //如果不是虚拟住宅,才更新云端
            if (Config.Instance.Home.IsVirtually == true)
            {
                //保存缓存
                Config.Instance.Home.Longitude = i_longitude;
                Config.Instance.Home.Latitude = i_latitude;
                Config.Instance.Home.ResidenceAddressName = i_addresName;
                Config.Instance.Home.Save();
                return true;
            }
 
            var pra = new { homeId = Config.Instance.Home.Id, homeType = "ZIGBEE", latitude = i_latitude, longitude = i_longitude };
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/home/update", RestSharp.Method.POST, pra, null, null, CheckMode.A账号权限);
            //检测状态码
            if (HdlCheckLogic.Current.CheckNetCode(result, mode) == false)
            {
                return false;
            }
            //保存缓存
            Config.Instance.Home.Longitude = i_longitude;
            Config.Instance.Home.Latitude = i_latitude;
            Config.Instance.Home.ResidenceAddressName = i_addresName;
            Config.Instance.Home.Save();
 
            //设置全部网关的住宅地址
            var listGateway = HdlGatewayLogic.Current.GetAllLocalGateway();
            foreach (var gateway in listGateway)
            {
                //不判断是否失败
                HdlGatewayLogic.Current.SetGatewaySite(gateway.GwId, i_longitude, i_latitude, ShowErrorMode.NO);
            }
 
            return true;
        }
 
        #endregion
 
        #region ■ 获取住宅___________________________
 
        /// <summary>
        /// 通过【id】获取住宅
        /// </summary>
        /// <returns>The house by house identifier.</returns>
        /// <param name="houseId">住宅id</param>
        public House GetHouseByHouseId(string houseId)
        {
            var path = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Config.Instance.Guid, houseId, $"House_{houseId}.json");
            var file = HdlFileLogic.Current.ReadFileByteContent(path);
            if (file == null)
            {
                return null;
            }
            return Newtonsoft.Json.JsonConvert.DeserializeObject<House>(System.Text.Encoding.UTF8.GetString(file));
        }
 
        /// <summary>
        /// 通过【文件路径】获取住宅
        /// </summary>
        /// <returns>The house by file path.</returns>
        /// <param name="filePath">文件路径</param>
        public House GetHouseByFilePath(string filePath)
        {
            var path = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Config.Instance.Guid, GetHouseIdByFilePath(filePath), filePath);
            var file = HdlFileLogic.Current.ReadFileByteContent(path);
            if (file == null)
            {
                return null;
            }
            return Newtonsoft.Json.JsonConvert.DeserializeObject<House>(System.Text.Encoding.UTF8.GetString(file));
        }
 
        #endregion
 
        #region ■ 获取云端住宅列表___________________
 
        /// <summary>
        /// 登陆的时候,获取云端住宅列表(0:当前没有网络的情况下加载本地住宅  1:通过联网加载及校验本地住宅)
        /// </summary>
        /// <param name="mode">失败时是否显示tip消息</param>
        /// <returns></returns>
        public int GetHomeListsFromDbOnLogin(ShowNetCodeMode mode = ShowNetCodeMode.YES)
        {
            //获取云端住宅数据
            var listHouseData = this.GetHomeListsFromDb(mode);
            if (listHouseData == null)
            {
                return 0;
            }
 
            //清空当前住宅列表
            Config.Instance.HomeFilePathList.Clear();
            var listHouse = new List<House>();
            foreach (var residence in listHouseData)
            {
                //刷新住宅对象的缓存
                var house = this.RefreshResidenceMemory(residence);
 
                listHouse.Add(house);
            }
 
            //检测本地的住宅文件是否合法
            var listLocal = this.GetAllLocalResidenceListByDirectory();
            foreach (var myHouse in listLocal)
            {
                //如果本地存在不属于他的住宅文件,则删除
                if (myHouse.HouseDataDiv == 1 && Config.Instance.HomeFilePathList.Contains(myHouse.FileName) == false)
                {
                    string housePath = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Config.Instance.Guid, myHouse.Id);
                    HdlFileLogic.Current.DeleteDirectory(housePath);
                }
            }
 
            //如果切换了账号,或者原来的id不存在,则重置住宅ID
            if (Config.Instance.TheSameLoginAccount == false ||
                Config.Instance.HomeFilePathList.Find((obj) => obj == $"House_{Config.Instance.HomeId}.json") == null)
            {
                Config.Instance.HomeId = listHouse.Count > 0 ? listHouse[0].Id : string.Empty;
                foreach (var house in listHouse)
                {
                    //初始选择它自己的住宅
                    if (house.IsOtherShare == false)
                    {
                        Config.Instance.HomeId = house.Id;
                        Config.Instance.Home = GetHouseByHouseId(house.Id);
                        break;
                    }
                }
            }
            Config.Instance.Save();
            return 1;
        }
 
        /// <summary>
        /// 获取云端住宅列表
        /// </summary>
        /// <param name="mode">失败时是否显示tip消息</param>
        /// <returns></returns>
        public List<ResidenceInformation> GetHomeListsFromDb(ShowNetCodeMode mode = ShowNetCodeMode.YES)
        {
            var pra = new { homeType = "ZIGBEE" };
 
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/home/list", RestSharp.Method.POST, pra);
            //检测状态码
            if (HdlCheckLogic.Current.CheckNetCode(result, mode) == false)
            {
                return null;
            }
            var listTemp = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ResidenceInformation>>(result.Data.ToString());
            var listData = new List<ResidenceInformation>();
            foreach (var data in listTemp)
            {
                if (data.HomeType == "ZIGBEE")
                {
                    listData.Add(data);
                }
            }
            return this.SortHouse(listData);
        }
 
        /// <summary>
        /// 刷新住宅对象的缓存
        /// </summary>
        /// <param name="i_residence">云端返回的数据转换成的数据</param>
        public House RefreshResidenceMemory(ResidenceInformation i_residence)
        {
            string fileName = $"House_{i_residence.Id}.json";
            if (Config.Instance.HomeFilePathList.Contains(fileName) == false)
            {
                Config.Instance.HomeFilePathList.Add(fileName);
            }
            var house = this.GetHouseByHouseId(i_residence.Id);
            if (house == null)
            {
                house = new House();
            }
            house.Id = i_residence.Id;
            house.Name = i_residence.HomeName;
            house.IsOtherShare = i_residence.IsOtherShare;
            house.AccountType = i_residence.AccountType == "ADMIN" ? 1 : 0;
            house.Longitude = i_residence.Longitude;
            house.Latitude = i_residence.Latitude;
            house.IsRemoteControl = i_residence.IsRemoteControl;
            house.LastEditorTime = HdlCommonLogic.Current.ConvertUtcTimeToLocalTime2(i_residence.ModifyTime).ToString("yyyy/MM/dd HH:mm");
 
            Global.CreateHomeDirectory(i_residence.Id);
 
            house.Save(false);
 
            return house;
        }
 
        /// <summary>
        /// 从云端,刷新住宅的区域(true:会刷新i_house的List变量, false:什么都不处理)
        /// </summary>
        /// <param name="i_homeId">住宅ID</param>
        public bool RefreshHomeAreaFromDb(string i_homeId)
        {
            var house = this.GetHouseByHouseId(i_homeId);
            if (house == null)
            {
                return false;
            }
            //var pra2 = new
            //{
            //    RequestVersion = CommonPage.RequestVersion,
            //    RequestSource = 5,
            //    LoginAccessToken = Common.Config.Instance.Token,
            //    HomeId = i_house.Id
            //};
            //var result = UserCenterLogic.GetResponseDataByRequestHttps("App/GetHomeDirectory", false, pra2, new List<string> { "NotCheck" });
            //if (result != null)
            //{
            //    var listData = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ResidenceAreaInfo>>(result);
            //    //初始化
            //    i_house.ListUintContent = new List<string>();
            //    i_house.ListUintName = new List<string>();
            //    foreach (var data in listData)
            //    {
            //        i_house.ListUintName.Add(data.Tag);
            //        i_house.ListUintContent.Add(data.Remark);
            //    }
            //    return true;
            //}
            return false;
        }
 
        #endregion
 
        #region ■ 获取本地住宅列表___________________
 
        /// <summary>
        /// 获取本地住宅列表
        /// </summary>
        /// <returns></returns>
        public List<Common.House> GetLocalResidenceList()
        {
            //如果是虚拟住宅,则从根目录中获取
            if (Common.Config.Instance.Home.IsVirtually == true)
            {
                //从文件夹中获取全部的住宅对象
                return this.GetAllLocalResidenceListByDirectory();
            }
 
            var listHome = new List<Common.House>();
            foreach (var housePath in Common.Config.Instance.HomeFilePathList)
            {
                var home = this.GetHouseByFilePath(housePath);
                if (home == null)
                {
                    continue;
                }
                listHome.Add(home);
            }
            return this.SortHouse(listHome);
        }
 
        /// <summary>
        /// 从文件夹中获取全部的住宅对象
        /// </summary>
        /// <param name="getGatewayId">是否获取网关ID(存在listGatewayId里面)</param>
        /// <returns></returns>
        public List<House> GetAllLocalResidenceListByDirectory(bool getGatewayId = false)
        {
            var strPath = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Common.Config.Instance.Guid);
            var listHome = new List<Common.House>();
            //获取全部的文件夹
            var listDirectory = new List<string>();
            var arryDirs = System.IO.Directory.GetDirectories(strPath);
            foreach (var file in arryDirs)
            {
                string[] arry = file.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
                listDirectory.Add(arry[arry.Length - 1]);
            }
            foreach (var myDir in listDirectory)
            {
                //获取各个文件夹里面的住宅文件
                string nowPath = System.IO.Path.Combine(strPath, myDir);
                var arryHouse = System.IO.Directory.GetFiles(nowPath, "House_*");
                if (arryHouse.Length > 0)
                {
                    //读取文件内容
                    var textValue = HdlFileLogic.Current.ReadFileTextContent(System.IO.Path.Combine(nowPath, arryHouse[0]));
                    if (textValue == null)
                    {
                        continue;
                    }
                    var myHouse = Newtonsoft.Json.JsonConvert.DeserializeObject<Common.House>(textValue);
                    listHome.Add(myHouse);
                    //如果读取网关id的话
                    if (getGatewayId == true)
                    {
                        //标识初始化完成
                        myHouse.listGatewayId = new List<string>();
                        //获取网关文件
                        var arryGateway = System.IO.Directory.GetFiles(nowPath, "Gateway_*");
                        foreach (var myFile in arryGateway)
                        {
                            string[] Arry = myFile.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries);
                            if (Arry.Length >= 3)
                            {
                                //获取其网关id
                                myHouse.listGatewayId.Add(Arry[2]);
                            }
                        }
                    }
                }
            }
            return this.SortHouse(listHome);
        }
 
        /// <summary>
        /// 从文件夹中获取全部备份住宅对象
        /// </summary>
        /// <returns></returns>
        public List<House> GetAllLocalBackupResidenceListByDirectory()
        {
            var strPath = System.IO.Path.Combine(IO.FileUtils.RootPath, Config.Instance.Guid);
            var listHome = new List<House>();
            //获取全部的文件夹
            var listDirectory = new List<string>();
            var arryDirs = System.IO.Directory.GetDirectories(strPath);
            foreach (var file in arryDirs)
            {
                string[] arry = file.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
                listDirectory.Add(arry[arry.Length - 1]);
            }
            foreach (var myDir in listDirectory)
            {
                if (myDir.StartsWith("BackupResidenceData") == false
                    && myDir.StartsWith("DownLoadResidenceData") == false)
                {
                    //只要手动备份和云端下载的
                    continue;
                }
                //获取各个文件夹里面的住宅文件
                string nowPath = System.IO.Path.Combine(strPath, myDir);
                var arryHouse = System.IO.Directory.GetFiles(nowPath, "House_*");
                if (arryHouse.Length > 0)
                {
                    //读取文件内容
                    var textValue = HdlFileLogic.Current.ReadFileTextContent(System.IO.Path.Combine(nowPath, arryHouse[0]));
                    if (textValue == null)
                    {
                        continue;
                    }
                    var myHouse = Newtonsoft.Json.JsonConvert.DeserializeObject<House>(textValue);
                    if (myHouse.Id != Config.Instance.Home.Id)
                    {
                        //不是这个住宅的备份
                        continue;
                    }
                    if (myDir.StartsWith("BackupResidenceData") == true)
                    {
                        //此住宅数据是临时备份数据
                        myHouse.HouseDataDiv = 2;
                        myHouse.SaveDirctoryName = myDir;
                    }
                    else if (myDir.StartsWith("DownLoadResidenceData") == true)
                    {
                        //此住宅数据是从云端下载的备份数据
                        myHouse.HouseDataDiv = 3;
                        myHouse.SaveDirctoryName = myDir;
                    }
                    listHome.Add(myHouse);
                }
            }
            return listHome;
        }
 
        #endregion
 
        #region ■ 住宅排序___________________________
 
        /// <summary>
        /// 住宅排序
        /// </summary>
        /// <param name="i_listHouse"></param>
        /// <returns></returns>
        public List<House> SortHouse(List<House> i_listHouse)
        {
            //从一堆文字中,获取这一堆文字里面数字字符串的最长长度
            var listName = new List<string>();
            foreach (var house in i_listHouse)
            {
                listName.Add(house.Name);
            }
            int numberLength = HdlCommonLogic.Current.GetNumberMaxLength(listName);
 
            var listSort = new List<string[]>();
            var dicHouse = new Dictionary<string, House>();
            foreach (var house in i_listHouse)
            {
                //临时缓存
                dicHouse[house.Id] = house;
 
                var strArry = new string[2];
                strArry[0] = house.Id;
                strArry[1] = string.Empty;
 
                string value = string.Empty;
                foreach (var c in house.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 listSortHouse = new List<House>();
            foreach (var strArry in listSort)
            {
                listSortHouse.Add(dicHouse[strArry[0]]);
            }
            return listSortHouse;
        }
 
        /// <summary>
        /// 云端住宅排序
        /// </summary>
        /// <param name="i_listData"></param>
        /// <returns></returns>
        public List<ResidenceInformation> SortHouse(List<ResidenceInformation> i_listData)
        {
            //从一堆文字中,获取这一堆文字里面数字字符串的最长长度
            var listName = new List<string>();
            foreach (var house in i_listData)
            {
                listName.Add(house.HomeName);
            }
            int numberLength = HdlCommonLogic.Current.GetNumberMaxLength(listName);
 
            var listSort = new List<string[]>();
            var dicHouse = new Dictionary<string, ResidenceInformation>();
            foreach (var house in i_listData)
            {
                //临时缓存
                dicHouse[house.Id] = house;
 
                var strArry = new string[2];
                strArry[0] = house.Id;
                strArry[1] = string.Empty;
 
                string value = string.Empty;
                foreach (var c in house.HomeName)
                {
                    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 listSortHouse = new List<ResidenceInformation>();
            foreach (var strArry in listSort)
            {
                listSortHouse.Add(dicHouse[strArry[0]]);
            }
            return listSortHouse;
        }
 
        #endregion
 
        #region ■ 删除住宅___________________________
 
        /// <summary>
        /// 删除云端住宅
        /// </summary>
        /// <param name="houseId">删除指定住宅</param>
        /// <returns></returns>
        public bool DeleteCloundHouse(string houseId)
        {
            var pra = new { homeId = houseId };
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/home/delete", RestSharp.Method.POST, pra);
            //检测状态码
            if (HdlCheckLogic.Current.CheckNetCode(result, ShowNetCodeMode.YES) == false)
            {
                return false;
            }
            return true;
        }
 
        /// <summary>
        /// 删除住宅
        /// </summary>
        /// <param name="filePath">File path.</param>
        public void DeleteHouseMemmory(string houseId)
        {
            string filePath = this.GetHouseFilePathByHouseId(houseId);
            var delPath = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Config.Instance.Guid, GetHouseIdByFilePath(filePath));
            if (System.IO.Directory.Exists(delPath) == false)
            {
                return;
            }
            var fileList = GetHouseFileListByHomeId(houseId);
            foreach (var file in fileList)
            {
                //删除文件
                System.IO.File.Delete(System.IO.Path.Combine(delPath, file));
            }
            //删除文件夹
            System.IO.Directory.Delete(delPath, true);
            //HomeFilePathList中删除该列表
            Common.Config.Instance.HomeFilePathList.RemoveAll((obj) => obj == filePath);
            Config.Instance.Save();
        }
 
        #endregion
 
        #region ■ 获取该住宅的文件列表_______________
 
        /// <summary>
        /// 通过【houseId】获取该住宅的文件列表
        /// </summary>
        /// <returns>The house file list by home identifier.</returns>
        /// <param name="houseId">House identifier.</param>
        public List<string> GetHouseFileListByHomeId(string houseId)
        {
            var list = new List<string> { };
            var path = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Config.Instance.Guid, houseId);
            if (!System.IO.Directory.Exists(path))
            {
                return new List<string> { };
            }
            var files = System.IO.Directory.GetFiles(path);
            foreach (var file in files)
            {
                var f = file.Substring(path.Length + 1);
                System.Console.WriteLine(f);
                list.Add(f);
            }
            return list;
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 获取楼层名称
        /// </summary>
        /// <param name="i_FloorId">楼层ID</param>
        /// <returns></returns>
        public string GetFloorNameById(string i_FloorId)
        {
            if (i_FloorId == null) { return string.Empty; }
            if (Config.Instance.Home.FloorDics.ContainsKey(i_FloorId) == false)
            {
                return string.Empty;
            }
            return Config.Instance.Home.FloorDics[i_FloorId];
        }
 
        /// <summary>
        /// 通过【文件路径】获取住宅id
        /// </summary>
        /// <returns>The house identifier by file path.</returns>
        /// <param name="filePath">文件路径</param>
        public string GetHouseIdByFilePath(string filePath)
        {
            string[] sArray = filePath.Split(new string[] { "House_", ".json" }, StringSplitOptions.RemoveEmptyEntries);
            if (sArray.Length >= 1)
            {
                return sArray[0];
            }
            return null;
        }
 
        /// <summary>
        /// 通过【住宅id】获取住宅路径
        /// </summary>
        /// <returns>The house identifier by file path.</returns>
        /// <param name="houseId">住宅id</param>
        public string GetHouseFilePathByHouseId(string houseId)
        {
            return $"House_{houseId}.json";
        }
 
        /// <summary>
        /// 获取住宅的区域文本
        /// </summary>
        /// <param name="i_house"></param>
        /// <returns></returns>
        public string GetResidenceAreaText(House i_house)
        {
            return this.GetResidenceAreaText(i_house.ListUintName, i_house.ListUintContent);
        }
 
        /// <summary>
        /// 获取住宅的区域文本
        /// </summary>
        /// <param name="i_ListUintName">单元标题</param>
        /// <param name="i_ListUintContent">单元内容</param>
        /// <returns></returns>
        public string GetResidenceAreaText(List<string> i_ListUintName, List<string> i_ListUintContent)
        {
            string textArea = string.Empty;
            for (int i = 0; i < i_ListUintName.Count; i++)
            {
                textArea += i_ListUintContent[i] + i_ListUintName[i] + "_";
            }
            return textArea.TrimEnd('_');
        }
 
        #endregion
    }
}