黄学彪
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
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
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
using Shared.Phone.UserCenter;
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
 
namespace Shared.Phone
{
    /// <summary>
    /// 模板的共通逻辑类
    /// </summary>
    public class HdlTemplateCommonLogic
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 模板的共通逻辑类
        /// </summary>
        private static HdlTemplateCommonLogic m_Current = null;
        /// <summary>
        /// 模板的共通逻辑类
        /// </summary>
        public static HdlTemplateCommonLogic Current
        {
            get
            {
                if (m_Current == null)
                {
                    m_Current = new HdlTemplateCommonLogic();
                }
                return m_Current;
            }
        }
        /// <summary>
        /// 模板数据
        /// </summary>
        public TemplateMemoryData modelData = new TemplateMemoryData();
        /// <summary>
        /// 模板文件中 #start# 到 #end# 的内容(临时变量)
        /// </summary>
        private string strTempContentData = string.Empty;
        /// <summary>
        /// 本地模板文件的名字
        /// </summary>
        private const string TemplateFileName = "ModelData_Release.bin";
 
        #endregion
 
        #region ■ 加载本地模板缓存___________________
 
        /// <summary>
        /// 加载本地模板文件缓存(此方法以本地缓存为准,切换住宅时使用)
        /// </summary>
        public void LoadLocalTemplateMemoryData()
        {
            this.modelData = new TemplateMemoryData();
            //保存的路径
            string saveFile = HdlFileNameResourse.LocalTemplateDirectory;
 
            //如果当前住宅拥有选择的模板
            if (Common.Config.Instance.Home.SelectTemplate != string.Empty)
            {
                string checkFile = System.IO.Path.Combine(saveFile, TemplateFileName);
                //如果本地没有这个bin文件
                if (System.IO.File.Exists(checkFile) == false)
                {
                    //复制模板bin文件到本地的模板文件夹里
                    this.CopyTemplateFileToLocalDirectory(Common.Config.Instance.Home.SelectTemplate);
                }
            }
 
            //获取这个路径下面全部的文件
            var listFile = HdlFileLogic.Current.GetFileFromDirectory(saveFile);
 
            //模板Bin文件
            string templateBinFile = string.Empty;
            //这里是读取他上一次编辑完成之后的模板数据(也就是编辑到一半之后,退出App,下一次再编辑)
            foreach (var fileName in listFile)
            {
                if (fileName == TemplateFileName)
                {
                    //模板Bin文件
                    templateBinFile = fileName;
                    continue;
                }
                if (fileName.StartsWith("Device_") == false)
                {
                    //只要设备
                    continue;
                }
                string fileData = HdlFileLogic.Current.ReadFileTextContent(System.IO.Path.Combine(saveFile, fileName));
                if (fileData == null)
                {
                    continue;
                }
                string deviceData = string.Empty;
                ModelDeviceSaveEnum saveDiv = ModelDeviceSaveEnum.A未定义;
                //根据换行符切分数据文本
                string[] arryData = fileData.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string strData in arryData)
                {
                    //设备数据标志
                    if (strData.StartsWith("===>") == true)
                    {
                        if (deviceData != string.Empty)
                        {
                            //反序列化设备的保存文件内容
                            var tempData = this.DeserializeDeviceDataByDiv(saveDiv, deviceData);
                            //将设备模板数据添加入缓存
                            string mainKey = HdlDeviceCommonLogic.Current.GetDeviceMainKeys(tempData.DeviceMac, tempData.DeviceEpoint);
                            this.SetTemplateDeviceDataToMemmory(tempData, deviceData, mainKey, true);
                        }
                        //清空
                        deviceData = string.Empty;
                        saveDiv = (ModelDeviceSaveEnum)Convert.ToInt32(strData.Substring(4));
                        continue;
                    }
                    deviceData += strData;
                }
                if (deviceData != string.Empty)
                {
                    //反序列化设备的保存文件内容
                    var tempData = this.DeserializeDeviceDataByDiv(saveDiv, deviceData);
                    //将设备模板数据添加入缓存
                    string mainKey = HdlDeviceCommonLogic.Current.GetDeviceMainKeys(tempData.DeviceMac, tempData.DeviceEpoint);
                    this.SetTemplateDeviceDataToMemmory(tempData, deviceData, mainKey, true);
                }
            }
 
            //读取模板Bin文件
            if (templateBinFile != string.Empty)
            {
                bool hadRoom = HdlRoomLogic.Current.GetAllListRooms().Count > 1 && Common.Config.Instance.Home.FloorDics.Count == 0;
                //开始读取文件内容
                this.ReadTemplateFileMethord((strData, saveDiv, deviceType) =>
                {
                    //在加载本地缓存的前提下,处理模板文件里面的特殊内容
                    var result = this.AdjustTemplateBinFileContentOnLoadMemory(strData, hadRoom);
                    //false代表它不是特殊内容
                    if (result == false)
                    {
                        //处理模板文件里面的共通内容
                        //因为上面已经加载了指定本地的缓存,所以这里不需要添加入dicDeviceTemplateData中
                        this.AdjustTemplateBinFileCommonContent(strData, saveDiv, deviceType, false);
                    }
                });
            }
            //清空对象缓存
            this.strTempContentData = string.Empty;
 
            //加载设备和网关模板选择的数据
            this.modelData.dicDeviceTemplateSelect = new Dictionary<string, string>();
            this.modelData.dicGatewayTemplateSelect = new Dictionary<string, string>();
 
            string fileData2 = HdlFileLogic.Current.ReadFileTextContent(HdlFileNameResourse.DeviceTemplateSelectFile);
            if (fileData2 != null)
            {
                //设备选择的模板对象(keys:本地设备的Mac value:模板中的Mac)
                this.modelData.dicDeviceTemplateSelect = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(fileData2);
            }
            fileData2 = HdlFileLogic.Current.ReadFileTextContent(HdlFileNameResourse.GatewayTemplateSelectFile);
            if (fileData2 != null)
            {
                //网关对象选择的模板对象(keys:本地网关ID,  value:模板中的网关ID)
                this.modelData.dicGatewayTemplateSelect = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(fileData2);
            }
        }
 
        /// <summary>
        /// 在加载本地缓存的前提下,处理模板文件里面的特殊内容
        /// </summary>
        /// <param name="strData">模板文件中的行数据</param>
        /// <param name="hadRoom">是否已经有了房间</param>
        private bool AdjustTemplateBinFileContentOnLoadMemory(string strData, bool hadRoom)
        {
            //场景对象
            if (strData == "#SceneTemplate END#")
            {
                if (hadRoom == false)
                {
                    //只初始化一次,有房间时代表已经不是第一次加载了
                    var scene = Newtonsoft.Json.JsonConvert.DeserializeObject<Common.SceneUI>(this.strTempContentData);
                    scene.Save();
                }
                this.strTempContentData = string.Empty;
                return true;
            }
            //楼层对象
            else if (strData == "#FloorInfo END#")
            {
                if (hadRoom == false)
                {
                    //只初始化一次,有房间时代表已经不是第一次加载了
                    Common.Config.Instance.Home.FloorDics = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(this.strTempContentData);
                    Common.Config.Instance.Home.Save(false);
                }
                this.strTempContentData = string.Empty;
                return true;
            }
            //房间对象
            else if (strData == "#RoomInfo END#")
            {
                var room = Newtonsoft.Json.JsonConvert.DeserializeObject<Common.Room>(this.strTempContentData);
                //各自端点所处的房间ID,保存起来
                foreach (var deviceKey in room.ListDevice)
                {
                    this.modelData.dicDeviceTemplateRoom[deviceKey] = room.Id;
                }
                foreach (var strMac in room.ListDeviceMac)
                {
                    //物理设备所在的区域,在这个函数外面已经初始化了这个变量
                    this.modelData.dicDeviceTemplateRealRoom[strMac] = room.Id;
                }
                if (hadRoom == false)
                {
                    //第一次加载,设备列表需要清空,选择设备模板时,才添加
                    room.ListDevice.Clear();
                    room.Save();
                }
                this.strTempContentData = string.Empty;
                return true;
            }
            //设备选择的模板
            else if (strData == "#DeviceSelectTemplate END#")
            {
                //这个东西在这个分支下不从文件中读取,从本地缓存文件当中获取
                this.strTempContentData = string.Empty;
                return true;
            }
            //网关选择的模板
            else if (strData == "#GatewaySelectTemplate END#")
            {
                //这个东西在这个分支下不从文件中读取,从本地缓存文件当中获取
                this.strTempContentData = string.Empty;
                return true;
            }
            return false;
        }
 
        #endregion
 
        #region ■ 根据模板Bin文件恢复数据____________
 
        /// <summary>
        /// 根据模板Bin文件,恢复数据(分两个函数吧,太难控制了)
        /// </summary>
        public void RecoverDataByTemplateBinFile()
        {
            //重新初始化
            this.modelData = new TemplateMemoryData();
 
            //开始读取文件内容
            this.ReadTemplateFileMethord((strData, saveDiv, deviceType) =>
            {
                //在恢复数据的前提下,处理模板文件里面的特殊内容
                var result = this.AdjustTemplateBinFileContentOnRecover(strData);
                //false代表它不是特殊内容
                if (result == false)
                {
                    //处理模板文件里面的共通内容
                    //因为是以模板来恢复住宅数据,所以这里需要添加入dicDeviceTemplateData中
                    this.AdjustTemplateBinFileCommonContent(strData, saveDiv, deviceType, true);
                }
            });
            //清空对象缓存
            this.strTempContentData = string.Empty;
 
            //删掉这两个保存选择模板的文件(这两个东西可能还存在)
            HdlFileLogic.Current.DeleteFile(HdlFileNameResourse.DeviceTemplateSelectFile);
            HdlFileLogic.Current.DeleteFile(HdlFileNameResourse.GatewayTemplateSelectFile);
 
            //再次初始化房间
            HdlRoomLogic.Current.InitAllRoom();
 
            //无模板模式时,恢复备份的时候,把备份文件删除
            if (Common.Config.Instance.Home.TemplateMode != 2)
            {
                //存放的路径
                string fullFile = System.IO.Path.Combine(HdlFileNameResourse.LocalTemplateDirectory, TemplateFileName);
                HdlFileLogic.Current.DeleteFile(fullFile);
            }
        }
 
        /// <summary>
        /// 在恢复数据的前提下,处理模板文件里面的特殊内容
        /// </summary>
        /// <param name="strData">模板文件中的行数据</param>
        private bool AdjustTemplateBinFileContentOnRecover(string strData)
        {
            //场景对象
            if (strData == "#SceneTemplate END#")
            {
                var scene = Newtonsoft.Json.JsonConvert.DeserializeObject<Common.SceneUI>(this.strTempContentData);
                scene.Save();
                this.strTempContentData = string.Empty;
                return true;
            }
            //楼层对象
            else if (strData == "#FloorInfo END#")
            {
                Common.Config.Instance.Home.FloorDics = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(this.strTempContentData);
                Common.Config.Instance.Home.Save(false);
                this.strTempContentData = string.Empty;
                return true;
            }
            //房间对象
            else if (strData == "#RoomInfo END#")
            {
                var room = Newtonsoft.Json.JsonConvert.DeserializeObject<Common.Room>(this.strTempContentData);
                room.Save();
                //各自端点所处的房间ID,保存起来
                foreach (var deviceKey in room.ListDevice)
                {
                    this.modelData.dicDeviceTemplateRoom[deviceKey] = room.Id;
                }
                foreach (var strMac in room.ListDeviceMac)
                {
                    //物理设备所在的区域,在这个函数外面已经初始化了这个变量
                    this.modelData.dicDeviceTemplateRealRoom[strMac] = room.Id;
                }
                this.strTempContentData = string.Empty;
                return true;
            }
            //设备选择的模板
            else if (strData == "#DeviceSelectTemplate END#")
            {
                this.modelData.dicDeviceTemplateSelect = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(this.strTempContentData);
                this.strTempContentData = string.Empty;
                return true;
            }
            //网关选择的模板
            else if (strData == "#GatewaySelectTemplate END#")
            {
                this.modelData.dicGatewayTemplateSelect = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(this.strTempContentData);
                this.strTempContentData = string.Empty;
                return true;
            }
            return false;
        }
 
        #endregion
 
        #region ■ 处理模板文件里面的共通内容_________
 
        /// <summary>
        /// 处理模板文件里面的共通内容
        /// </summary>
        /// <param name="strData">模板文件中的行数据</param>
        /// <param name="saveDiv">模板设备保存的区分(设备数据时有效)</param>
        /// <param name="deviceType">模板中设备的deviceType(设备数据时有效,反射用)</param>
        /// <param name="addToTemplate">是否添加到设备模板缓存中</param>
        private void AdjustTemplateBinFileCommonContent(string strData, ModelDeviceSaveEnum saveDiv, string deviceType, bool addToTemplate)
        {
            //模板基本数据
            if (strData == "#TemplateData END#")
            {
                var templateData = Newtonsoft.Json.JsonConvert.DeserializeObject<LocalModelBaseInfo>(this.strTempContentData);
                this.modelData.TemplateName = templateData.ModelName;
                this.strTempContentData = string.Empty;
                return;
            }
            //设备模板
            else if (strData == "#DeviceTemplate END#")
            {
                //反序列化设备的保存文件内容
                var tempData = this.DeserializeDeviceDataByDiv(saveDiv, this.strTempContentData);
                //将设备模板数据添加入缓存(此处特殊,不需要加入设备模板缓存中)
                string mainKey = HdlDeviceCommonLogic.Current.GetDeviceMainKeys(tempData.DeviceMac, tempData.DeviceEpoint);
                this.SetTemplateDeviceDataToMemmory(tempData, this.strTempContentData, mainKey, addToTemplate);
                this.strTempContentData = string.Empty;
                return;
            }
            //设备对象
            else if (strData == "#DeviceInfo END#")
            {
                //反序列化设备
                CommonDevice device = null;
                if (HdlCheckLogic.Current.CheckIsNumber(deviceType) == true)
                {
                    //数值型为新数据,直接转换
                    device = CommonDevice.CommonDeviceByByteString(Convert.ToInt32(deviceType), this.strTempContentData);
                }
                else
                {
                    //字符串型为旧数据,需要特殊处理
                    var myType = (DeviceType)Enum.Parse(typeof(DeviceType), deviceType);
                    device = CommonDevice.CommonDeviceByByteString((int)myType, this.strTempContentData);
                }
                if (device != null)
                {
                    if (this.modelData.dicDeviceInfo.ContainsKey(device.DeviceAddr) == false)
                    {
                        this.modelData.dicDeviceInfo[device.DeviceAddr] = new List<CommonDevice>();
                    }
                    this.modelData.dicDeviceInfo[device.DeviceAddr].Add(device);
                }
                this.strTempContentData = string.Empty;
                return;
            }
            //网关对象数据
            else if (strData == "#GatewayInfo END#")
            {
                //反序列化设备
                var gateway = Newtonsoft.Json.JsonConvert.DeserializeObject<ZbGateway>(this.strTempContentData);
                this.modelData.dicGatewayInfo[gateway.GwId] = gateway;
                this.strTempContentData = string.Empty;
                return;
            }
            //模板住宅的信息(目前在这里基本没用)
            else if (strData == "#TemplateHomeInfo END#")
            {
                this.strTempContentData = string.Empty;
                return;
            }
            this.strTempContentData += strData;
        }
 
        #endregion
 
        #region ■ 读取模板文件内容___________________
 
        /// <summary>
        /// 读取模板文件内容
        /// </summary>
        /// <param name="AdjustAction">
        /// <para>参数1:模板文件中的行数据</para>
        /// <para>参数2:模板设备保存的区分(设备数据时有效)</para>
        /// <para>参数3:板中设备的deviceType(设备数据时有效,反射用)</para>
        /// </param>
        private void ReadTemplateFileMethord(Action<string, ModelDeviceSaveEnum, string> AdjustAction)
        {
            //保存的路径
            string saveFile = HdlFileNameResourse.LocalTemplateDirectory;
            saveFile = System.IO.Path.Combine(saveFile, TemplateFileName);
 
            string fileData = HdlFileLogic.Current.ReadFileTextContent(saveFile);
            if (fileData == null)
            {
                AdjustAction = null;
                return;
            }
 
            var saveDiv = ModelDeviceSaveEnum.A未定义;
            var deviceType = string.Empty;
 
            //根据换行符切分数据文本
            string[] arryData = fileData.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string strData in arryData)
            {
                if (strData == "#START#")
                {
                    //无附加数据的【数据标题】
                    continue;
                }
                if (strData.StartsWith("#DeviceTemplate START#") == true)
                {
                    //附加数据:设备保存区分
                    saveDiv = (ModelDeviceSaveEnum)Convert.ToInt32(strData.Substring(22));
                    continue;
                }
                if (strData.StartsWith("#DeviceInfo START#") == true)
                {
                    //附加数据:设备对象类型
                    deviceType = strData.Substring(18);
                    continue;
                }
                try
                {
                    //执行数据处理
                    AdjustAction(strData, saveDiv, deviceType);
                }
                catch (Exception ex)
                {
                    HdlLogLogic.Current.WriteLog(ex, "模板bin文件出问题\r\n" + this.strTempContentData);
                    this.strTempContentData = string.Empty;
                }
            }
        }
 
        #endregion
 
        #region ■ 反序列化设备的保存文件内容_________
 
        /// <summary>
        /// 反序列化设备的保存文件内容
        /// </summary>
        /// <param name="saveDiv">保存区分</param>
        /// <param name="fileData"></param>
        /// <returns></returns>
        private TemplateDeviceDataCommon DeserializeDeviceDataByDiv(ModelDeviceSaveEnum saveDiv, string fileData)
        {
            TemplateDeviceDataCommon modelData = null;
            if (saveDiv == ModelDeviceSaveEnum.APir配置)
            {
                modelData = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelPirSensorSettion>(fileData);
            }
            else if (saveDiv == ModelDeviceSaveEnum.A空调摆风功能)
            {
                modelData = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelAcSwingModeSupport>(fileData);
            }
            else if (saveDiv == ModelDeviceSaveEnum.A空调自定义模式)
            {
                modelData = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelAcModeSupport>(fileData);
            }
            else if (saveDiv == ModelDeviceSaveEnum.A窗帘手拉控制)
            {
                modelData = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelCurtainHandPullControl>(fileData);
            }
            else if (saveDiv == ModelDeviceSaveEnum.A窗帘方向及限位)
            {
                modelData = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelCurtainDirectionAndLimite>(fileData);
            }
            else if (saveDiv == ModelDeviceSaveEnum.A端点名称)
            {
                modelData = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelDeviceEpointNameInfo>(fileData);
            }
            else if (saveDiv == ModelDeviceSaveEnum.A设备名称)
            {
                modelData = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelDeviceMacNameInfo>(fileData);
            }
            else if (saveDiv == ModelDeviceSaveEnum.A设备绑定列表)
            {
                modelData = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelDeviceBindData>(fileData);
            }
            else if (saveDiv == ModelDeviceSaveEnum.A面板亮度调节)
            {
                modelData = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelPanelBrightnessAdjustInfo>(fileData);
            }
            else if (saveDiv == ModelDeviceSaveEnum.A面板节能模式)
            {
                modelData = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelPanelEnergyModeInfo>(fileData);
            }
            else if (saveDiv == ModelDeviceSaveEnum.A面板指示灯)
            {
                modelData = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelPanelIndicatorLightInfo>(fileData);
            }
            else if (saveDiv == ModelDeviceSaveEnum.A面板震动功能)
            {
                modelData = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelPanelVibrationInfo>(fileData);
            }
            return modelData;
        }
 
        /// <summary>
        /// 将设备模板数据添加入缓存
        /// </summary>
        /// <param name="modelData">模板数据</param>
        /// <param name="fileData">设备保存在文件中的内容(可为null)</param>
        /// <param name="mainKey">添加的主键</param>
        /// <param name="addToTemplate">是否添加到设备模板缓存中</param>
        private void SetTemplateDeviceDataToMemmory(TemplateDeviceDataCommon modelData, string fileData, string mainKey, bool addToTemplate)
        {
            if (modelData == null)
            {
                return;
            }
 
            //从模板主文件中获取的设备模板信息,是不需要添加到这个变量中的
            //因为它只能慢慢一个个匹配
            if (addToTemplate == true)
            {
                if (this.modelData.dicDeviceTemplateData.ContainsKey(mainKey) == false)
                {
                    this.modelData.dicDeviceTemplateData[mainKey] = new List<TemplateDeviceDataCommon>();
                }
                this.modelData.dicDeviceTemplateData[mainKey].Add(modelData);
            }
 
            if (fileData != null)
            {
                //临时缓存:模板中各自端点所保存的内容(keys:设备主键),设备选择模板时,模板数据迁移使用,因为是引用类型,所以需要重新New
                if (this.modelData.dicDeviceFileContent.ContainsKey(mainKey) == false)
                {
                    this.modelData.dicDeviceFileContent[mainKey] = new List<TemplateDeviceContent>();
                }
                var fileCentent = new TemplateDeviceContent();
                fileCentent.saveDiv = modelData.DataSaveDiv;
                fileCentent.FileContent = fileData;
                fileCentent.DeviceMac = modelData.DeviceMac;
                this.modelData.dicDeviceFileContent[mainKey].Add(fileCentent);
            }
        }
 
        #endregion
 
        #region ■ 保存模板数据到本地相关_____________
 
        /// <summary>
        /// 保存模板数据到本地
        /// </summary>
        /// <param name="backupName">备份名称</param>
        /// <param name="saveFirmware">是否下载固件</param>
        public void SaveTemplateDataToLocation(string backupName, bool downloadFirmware = true)
        {
            //获取本地全部的模板列表的基本信息
            var localModel = this.GetLocalAllModelList();
            var fileName = this.GetNewTemplateFileName();
            foreach (var model in localModel)
            {
                //名字一样时
                if (model.ModelName == backupName)
                {
                    fileName = model.FileName;
                    //备份数据已经存在,是否覆盖?
                    this.ShowMassage(ShowMsgType.Confirm, Language.StringByID(R.MyInternationalizationString.BackUpDataIsEsixtAndPickUp), () =>
                    {
                        //将模板数据保存到到指定的文件夹中
                        this.SaveTemplateDataToLocation2(fileName, backupName, downloadFirmware);
                    });
                    return;
                }
            }
            //将模板数据保存到到指定的文件夹中
            this.SaveTemplateDataToLocation2(fileName, backupName, downloadFirmware);
        }
 
        /// <summary>
        /// 保存模板数据到本地
        /// </summary>
        /// <param name="fileName">保存文件的名字</param>
        /// <param name="backName">模板备份的名字</param>
        /// <param name="saveFirmware">是否下载固件</param>
        private void SaveTemplateDataToLocation2(string fileName, string backupName, bool downloadFirmware = true)
        {
            HdlThreadLogic.Current.RunThread(() =>
            {
                ProgressFormBar.Current.Start();
                ProgressFormBar.Current.SetMsg("正在保存模板数据");
                System.Threading.Thread.Sleep(1500);
 
                //将模板数据保存到到指定的文件夹中
                var fileFullName = this.SaveTemplateDataToFile(fileName, backupName);
                if (downloadFirmware == false)
                {
                    //本地备份保存成功
                    this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.SaveLocalBackDataSuccess));
                    ProgressFormBar.Current.Close();
                    return;
                }
                //获取升级固件文件
                var result = HdlFirmwareUpdateLogic.Current.DownLoadTemplateDeviceFirmware(fileFullName, "正在保存升级固件数据");
                if (result == -1)
                {
                    this.ShowMassage(ShowMsgType.Tip, "保存升级固件数据失败");
                    //删掉这个模板
                    HdlFileLogic.Current.DeleteFile(fileFullName);
                }
                else
                {
                    //本地备份保存成功
                    this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.SaveLocalBackDataSuccess));
                }
                ProgressFormBar.Current.Close();
            });
        }
 
        /// <summary>
        /// 在生成模板数据之前,检测模板数据
        /// </summary>
        private void CheckTempLateDataBeforCreat()
        {
            //为了保证模板里的设备数和本地的一致,所以检测一下
            //如果缺少,则添加修改Mac的数据进去
            //其他的,如果不点击各自的配置界面,则当做是默认设备原来的配置状态
 
            //获取目前已经设置设备物理名称的设备Mac
            var listMac = new HashSet<string>();
            foreach (var listData in this.modelData.dicDeviceTemplateData.Values)
            {
                if (listData.Count > 0 && listMac.Contains(listData[0].DeviceMac) == true)
                {
                    //已经加了
                    continue;
                }
                foreach (var data in listData)
                {
                    if (data.DataSaveDiv == ModelDeviceSaveEnum.A设备名称)
                    {
                        listMac.Add(data.DeviceMac);
                    }
                }
            }
            var listDevice = HdlDeviceCommonLogic.Current.listAllDevice;
            foreach (var device in listDevice)
            {
                if (listMac.Contains(device.DeviceAddr) == false)
                {
                    listMac.Add(device.DeviceAddr);
                    //重新添加Mac名字缓存
                    HdlTemplateDeviceDataLogic.Current.ReDeviceMacName(device, HdlDeviceCommonLogic.Current.GetDeviceMacName(device));
                }
                //重新添加端点名字缓存
                HdlTemplateDeviceDataLogic.Current.ReDeviceEpointName(device, HdlDeviceCommonLogic.Current.GetDeviceEpointName(device));
            }
        }
        #endregion
 
        #region ■ 获取模板对象相关___________________
 
        /// <summary>
        /// 获取本地全部的模板列表的基本信息
        /// </summary>
        /// <returns></returns>
        public List<LocalModelBaseInfo> GetLocalAllModelList()
        {
            var dicData = new Dictionary<string, List<LocalModelBaseInfo>>();
            var listTime = new List<string>();
 
            var strPath = HdlFileNameResourse.AllResidenceTemplateDirectory;
            //获取全部文件
            var arryFile = System.IO.Directory.GetFiles(strPath, "ModelData_*");
            foreach (string modelFile in arryFile)
            {
                //读取文件内容
                var textValue = HdlFileLogic.Current.ReadFileTextContent(modelFile);
                if (textValue == null)
                {
                    continue;
                }
                //从文件中获取指定的内容
                string modelBaseInfo = this.GetDataFromFileContent(textValue, "#START#", "#TemplateData END#");
                if (modelBaseInfo != string.Empty)
                {
                    var myModel = Newtonsoft.Json.JsonConvert.DeserializeObject<LocalModelBaseInfo>(modelBaseInfo);
                    myModel.FileName = modelFile.Substring(strPath.Length + 1);
                    if (dicData.ContainsKey(myModel.EditorTime) == false)
                    {
                        dicData[myModel.EditorTime] = new List<LocalModelBaseInfo>();
                        listTime.Add(myModel.EditorTime);
                    }
                    dicData[myModel.EditorTime].Add(myModel);
 
                    string homeData = this.GetDataFromFileContent(textValue, "#START#", "#TemplateHomeInfo END#");
                    if (homeData != string.Empty)
                    {
                        var homeInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<TemplateHomeInfo>(homeData);
                        myModel.ListUintContent.AddRange(homeInfo.ListUintContent);
                        myModel.ListUintName.AddRange(homeInfo.ListUintName);
                        myModel.ResidenceAddressName = homeInfo.ResidenceAddressName;
                    }
                }
            }
            //按时间排序
            listTime.Sort();
 
            var listData = new List<LocalModelBaseInfo>();
            for (int i = listTime.Count - 1; i >= 0; i--)
            {
                listData.AddRange(dicData[listTime[i]]);
            }
            return listData;
        }
 
        /// <summary>
        /// 获取云端全部的模板列表的基本信息
        /// </summary>
        /// <param name="mode">失败时是否显示tip消息</param>
        /// <returns></returns>
        public List<CloundModelBaseInfo> GetCloundAllModelList(ShowNetCodeMode mode= ShowNetCodeMode.YES)
        {
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/template/metaInfo/findAll", RestSharp.Method.POST, null);
            //检测状态码
            if (HdlCheckLogic.Current.CheckNetCode(result, mode) == false)
            {
                return new List<CloundModelBaseInfo>();
            }
 
            var dicData = new Dictionary<string, List<CloundModelBaseInfo>>();
            var listTime = new List<string>();
 
            var listCloundData = Newtonsoft.Json.JsonConvert.DeserializeObject<List<CloundModelBaseInfo>>(result.Data.ToString());
            foreach (var data in listCloundData)
            {
                //变更时间格式
                data.CreateTime = HdlCommonLogic.Current.ConvertUtcTimeToLocalTime2(data.CreateTime).ToString("yyyy.MM.dd HH:mm");
                if (dicData.ContainsKey(data.CreateTime) == false)
                {
                    dicData[data.CreateTime] = new List<CloundModelBaseInfo>();
                    listTime.Add(data.CreateTime);
                }
                dicData[data.CreateTime].Add(data);
            }
 
            //按时间排序
            listTime.Sort();
 
            var listData = new List<CloundModelBaseInfo>();
            for (int i = listTime.Count - 1; i >= 0; i--)
            {
                listData.AddRange(dicData[listTime[i]]);
            }
            return listData;
        }
 
        /// <summary>
        /// 获取一个新的模板保存文件名
        /// </summary>
        /// <returns></returns>
        public string GetNewTemplateFileName()
        {
            return "ModelData_Local_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".bin";
        }
 
        /// <summary>
        /// 获取一个新的模板保存文件名
        /// </summary>
        /// <returns></returns>
        public string GetNewTemplateFileName(DateTime dateTime)
        {
            return "ModelData_Local_" + dateTime.ToString("yyyyMMdd_HHmmss") + ".bin";
        }
 
        #endregion
 
        #region ■ 上传模板备份_______________________
 
        /// <summary>
        /// 上传模板备份(内部使用线程来执行,有转圈的界面)
        /// </summary>
        /// <param name="i_localTemplate">本地模板信息</param>
        /// <param name="i_saveName">备份名字</param>
        /// <param name="mode">失败时是否显示tip消息</param>
        public void UpLoadTemplateData(LocalModelBaseInfo i_localTemplate, string i_saveName, ShowNetCodeMode mode = ShowNetCodeMode.YES)
        {
            HdlThreadLogic.Current.RunThread(() =>
            {
                ProgressBar.Show();
 
                //获取云端的模板列表
                var listTemplate = this.GetCloundAllModelList();
                foreach (var data in listTemplate)
                {
                    if (data.TemplateName == i_saveName)
                    {
                        //模板名字已经存在
                        this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.TheTemplateNameIsEsixt));
                        ProgressBar.Close();
                        return;
                    }
                }
                //创建新的模板备份
                var templateId = this.CreatNewTemplateNameToDB(i_saveName, mode);
                if (templateId == null)
                {
                    ProgressBar.Close();
                    return;
                }
 
                //这里修改掉模板文件里面记载的模板名称
                string templateFile = System.IO.Path.Combine(HdlFileNameResourse.AllResidenceTemplateDirectory, i_localTemplate.FileName);
                string binFileData = HdlFileLogic.Current.ReadFileTextContent(templateFile);
                var arryBinFile = binFileData.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                //替换目标 这里是模板基本信息的json数据
                string strFileData = arryBinFile[1];
                var templateBaseInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<LocalModelBaseInfo>(strFileData);
                templateBaseInfo.ModelName = i_saveName;//更改掉名字
                templateBaseInfo.EditorTime = DateTime.Now.ToString("yyyy.MM.dd HH:mm");//更改掉时间
                //替换对象
                string replaceDta = Newtonsoft.Json.JsonConvert.SerializeObject(templateBaseInfo);
                binFileData = binFileData.Replace(strFileData, replaceDta);
 
                var dicQuery = new Dictionary<string, object>();
                dicQuery["fileName"] = "ModelData_Cloud_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".bin";
                dicQuery["templateId"] = templateId;
                var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/template/file/add", RestSharp.Method.POST, Encoding.UTF8.GetBytes(binFileData), dicQuery, null, CheckMode.A不检测, true, 10);
                //检测状态码
                if (HdlCheckLogic.Current.CheckNetCode(result, mode) == true)
                {
                    //上传模板成功
                    this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.UploadTemplateSuccess));
                }
                else
                {
                    //上传模板失败
                    this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.UploadTemplateFail));
                }
                ProgressBar.Close();
            });
        }
 
        /// <summary>
        /// 在云端创建新的模板名称
        /// </summary>
        /// <param name="i_saveName">新名字</param>
        /// <param name="mode">失败时是否显示tip消息</param>
        /// <returns></returns>
        private string CreatNewTemplateNameToDB(string i_saveName, ShowNetCodeMode mode = ShowNetCodeMode.YES)
        {
            var pra = new { templateName = i_saveName };
 
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/template/metaInfo/add", RestSharp.Method.POST, pra);
            //检测状态码
            if (HdlCheckLogic.Current.CheckNetCode(result, mode) == false)
            {
                return null;
            }
            var idData = Newtonsoft.Json.JsonConvert.DeserializeObject<IdInfoClass>(result.Data.ToString());
            return idData.Id;
        }
 
        #endregion
 
        #region ■ 下载模板备份_______________________
 
        /// <summary>
        /// 下载模板备份(内部是使用线程执行,有界面型进度条)
        /// </summary>
        /// <param name="i_templateId">数据库主键</param>
        /// <param name="i_SuccessAction">下载完全成功之后的回调事件,参数为保存模板的全路径(参数null代表失败)</param>
        /// <param name="mode">失败时是否显示tip消息</param>
        public void DownLoadTemplate(string i_templateId, Action<string> i_SuccessAction = null,ShowNetCodeMode mode= ShowNetCodeMode.YES)
        {
            HdlThreadLogic.Current.RunThread(() =>
            {
                ProgressFormBar.Current.Start();
                ProgressFormBar.Current.SetMsg("正在下载模板数据");
                System.Threading.Thread.Sleep(1500);
 
                var pra = new { templateId = i_templateId };
 
                //获取指定模板的列表文件
                var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/template/file/findAll", RestSharp.Method.POST, pra);
                //检测状态码
                if (HdlCheckLogic.Current.CheckNetCode(result, mode) == false)
                {
                    //下载模板失败
                    this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.DownloadTemplateFail));
                    ProgressFormBar.Current.Close();
                    i_SuccessAction?.Invoke(null);
                    return;
                }
                var listFileData = Newtonsoft.Json.JsonConvert.DeserializeObject<List<DownloadTemplateData>>(result.Data.ToString());
                if (listFileData.Count == 0)
                {
                    //下载模板失败
                    this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.DownloadTemplateFail));
                    ProgressFormBar.Current.Close();
                    i_SuccessAction?.Invoke(null);
                    return;
                }
 
                //下载模板的内容
                var pra2 = new { templateFileId = listFileData[0].Id, templateMetaInfoId = i_templateId };
                var byteContent = HdlHttpLogic.Current.RequestByteFromZigbeeHttps("home-wisdom/template/file/downOne", RestSharp.Method.POST, pra2, null, null, CheckMode.A不检测, true, 10);
                if (byteContent == null || byteContent.Length == 0)
                {
                    //下载模板失败
                    this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.DownloadTemplateFail));
                    ProgressFormBar.Current.Close();
                    i_SuccessAction?.Invoke(null);
                    return;
                }
 
                //解析这个模板的名字
                var strFileData = this.GetDataFromFileContent(Encoding.UTF8.GetString(byteContent), "#START#", "#TemplateData END#");
                var templateBaseInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<LocalModelBaseInfo>(strFileData);
 
                //检测本地的模板是否有同名的
                var listLocal = this.GetLocalAllModelList();
                string fileName = listFileData[0].FileName;
                foreach (var localData in listLocal)
                {
                    if (localData.ModelName == templateBaseInfo.ModelName)
                    {
                        //替换,直接使用本地的模板文件名字
                        fileName = localData.FileName;
                        break;
                    }
                }
 
                //存成文件
                string fileFullName = System.IO.Path.Combine(HdlFileNameResourse.AllResidenceTemplateDirectory, fileName);
                HdlFileLogic.Current.SaveByteToFile(fileFullName, byteContent);
                byteContent = null;
 
                //获取升级固件文件
                var result2 = HdlFirmwareUpdateLogic.Current.DownLoadTemplateDeviceFirmware(fileFullName, "正在获取升级固件数据");
                if (result2 == -1)
                {
                    this.ShowMassage(ShowMsgType.Tip, "获取升级固件数据失败");
                    ProgressFormBar.Current.Close();
                    i_SuccessAction?.Invoke(null);
                    return;
                }
                ProgressFormBar.Current.Close();
                i_SuccessAction?.Invoke(fileFullName);
            });
        }
 
        /// <summary>
        /// 下载模板
        /// </summary>
        private class DownloadTemplateData
        {
            /// <summary>
            /// 模板文件名字
            /// </summary>
            public string FileName = string.Empty;
            /// <summary>
            /// 主键
            /// </summary>
            public string Id = string.Empty;
        }
 
        #endregion
 
        #region ■ 删除模板备份_______________________
 
        /// <summary>
        /// 删除云端模板备份
        /// </summary>
        /// <param name="i_templateId">模板主键</param>
        /// <param name="mode">失败时是否显示tip消息</param>
        /// <returns></returns>
        public bool DeleteTemplateFromDb(string i_templateId, ShowNetCodeMode mode = ShowNetCodeMode.YES)
        {
            var pra = new { templateId = i_templateId };
 
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/template/metaInfo/delete", RestSharp.Method.POST, pra);
            if (result != null && result.Code == HttpMessageEnum.A10605)
            {
                //模板对象不存在,当做成功
                return true;
            }
            //检测状态码
            if (HdlCheckLogic.Current.CheckNetCode(result, mode) == false)
            {
                return false;
            }
            return true;
        }
 
        /// <summary>
        /// 删除本地模板备份
        /// </summary>
        /// <param name="i_baseInfo">本地模板的基本信息</param>
        /// <returns></returns>
        public void DeleteLocalTemplate(LocalModelBaseInfo i_baseInfo)
        {
            var fullFile = System.IO.Path.Combine(HdlFileNameResourse.AllResidenceTemplateDirectory, i_baseInfo.FileName);
            HdlFileLogic.Current.DeleteFile(fullFile);
        }
 
        #endregion
 
        #region ■ 生成模板数据相关___________________
 
        /// <summary>
        /// 保存模板数据到文件(返回保存文件的全路径)
        /// </summary>
        /// <param name="fileName">保存文件的名字(新建时用 GetNewTemplateFileName函数新建)</param>
        /// <param name="backName">模板备份的名字</param>
        public string SaveTemplateDataToFile(string fileName, string backUpName)
        {
            //写入文件的内容
            string writeText = string.Empty;
 
            //在生成模板数据之前,检测模板数据
            this.CheckTempLateDataBeforCreat();
 
            //生成写入文件的【模板基本数据】
            this.CreatWriteTemplateBaseData(ref writeText, backUpName);
 
            //生成写入文件的【模板住宅信息数据】
            this.CreatWriteTemplateHomeData(ref writeText);
 
            //生成写入文件的【设备模板数据】
            this.CreatWriteDeviceTemplateData(ref writeText);
 
            //生成写入文件的【设备对象数据】
            this.CreatWriteCommonDeviceData(ref writeText);
 
            //生成写入文件的【网关对象数据】
            this.CreatWriteGatewayData(ref writeText);
 
            //生成写入文件的【场景模板数据】
            this.CreatWriteSceneData(ref writeText);
 
            //生成写入文件的【房间模板数据】
            this.CrearWriteRoomTemplateData(ref writeText);
 
            //生成写入文件的【设备和网关选择的模板的数据】
            this.CrearWriteDeviceSelectTemplateData(ref writeText);
 
            //写入内容
            string saveFile = HdlFileNameResourse.AllResidenceTemplateDirectory;
            saveFile = System.IO.Path.Combine(saveFile, fileName);
 
            HdlFileLogic.Current.SaveTextToFile(saveFile, writeText);
 
            return saveFile;
        }
 
        /// <summary>
        /// 生成写入文件的【模板基本数据】
        /// </summary>
        /// <param name="writeText"></param>
        public void CreatWriteTemplateBaseData(ref string writeText, string backUpName)
        {
            var modelData = new LocalModelBaseInfo();
            modelData.EditorTime = DateTime.Now.ToString("yyyy.MM.dd HH:mm");
            modelData.ModelName = backUpName;
            modelData.FloorCount = Common.Config.Instance.Home.FloorDics.Count;
            modelData.DeviceCount = this.modelData.dicDeviceTemplateData.Count;
            //功能数
            int funcCount = 0;
            foreach (var listData in this.modelData.dicDeviceTemplateData.Values)
            {
                if (listData.Count > 0)
                {
                    var listDevice = HdlDeviceCommonLogic.Current.GetDevicesByMac(listData[0].DeviceMac, false);
                    funcCount += listDevice.Count;
                }
            }
            modelData.FunctionCount = funcCount;
 
            writeText += "#START#\r\n";
            string dataInfo = Newtonsoft.Json.JsonConvert.SerializeObject(modelData);
            writeText += dataInfo + "\r\n";
            writeText += "#TemplateData END#\r\n\r\n";
        }
 
        /// <summary>
        /// 生成写入文件的【模板住宅信息数据】
        /// </summary>
        /// <param name="writeText"></param>
        public void CreatWriteTemplateHomeData(ref string writeText)
        {
            var homeData = new TemplateHomeInfo();
            homeData.ResidenceAddressName = Common.Config.Instance.Home.ResidenceAddressName;
            homeData.ListUintName.AddRange(Common.Config.Instance.Home.ListUintName);
            homeData.ListUintContent.AddRange(Common.Config.Instance.Home.ListUintContent);
 
            writeText += "#START#\r\n";
            string dataInfo = Newtonsoft.Json.JsonConvert.SerializeObject(homeData);
            writeText += dataInfo + "\r\n";
            writeText += "#TemplateHomeInfo END#\r\n\r\n";
        }
 
        /// <summary>
        /// 生成写入文件的【设备模板数据】
        /// </summary>
        /// <param name="writeText"></param>
        public void CreatWriteDeviceTemplateData(ref string writeText)
        {
            foreach (var list in this.modelData.dicDeviceTemplateData.Values)
            {
                foreach (var data in list)
                {
                    writeText += "#DeviceTemplate START#" + (int)data.DataSaveDiv + "\r\n";
                    string dataInfo = Newtonsoft.Json.JsonConvert.SerializeObject(data);
                    writeText += dataInfo + "\r\n";
                    writeText += "#DeviceTemplate END#\r\n\r\n";
                }
            }
        }
 
        /// <summary>
        /// 生成写入文件的【设备对象数据】
        /// </summary>
        /// <param name="writeText"></param>
        public void CreatWriteCommonDeviceData(ref string writeText)
        {
            var listDevice = HdlDeviceCommonLogic.Current.listAllDevice;
            var listCheck = new HashSet<string>();
            foreach (var device in listDevice)
            {
                //设备端点
                writeText += "#DeviceInfo START#" + (int)device.Type + "\r\n";
                string dataInfo = Newtonsoft.Json.JsonConvert.SerializeObject(device);
                writeText += dataInfo + "\r\n";
                writeText += "#DeviceInfo END#\r\n\r\n";
 
                //添加Ota设备对象的缓存
                if (listCheck.Contains(device.DeviceAddr) == false)
                {
                    listCheck.Add(device.DeviceAddr);
                    var otaDevice = HdlDeviceCommonLogic.Current.GetOTADevice(device.DeviceAddr);
                    if (otaDevice != null)
                    {
                        writeText += "#DeviceInfo START#" + (int)otaDevice.Type + "\r\n";
                        string dataInfo2 = Newtonsoft.Json.JsonConvert.SerializeObject(otaDevice);
                        writeText += dataInfo2 + "\r\n";
                        writeText += "#DeviceInfo END#\r\n\r\n";
                    }
                }
            }
        }
 
        /// <summary>
        /// 生成写入文件的【网关对象数据】
        /// </summary>
        /// <param name="writeText"></param>
        public void CreatWriteGatewayData(ref string writeText)
        {
            var listGateway = HdlGatewayLogic.Current.GetAllLocalGateway();
            foreach (var gateway in listGateway)
            {
                //设备端点
                writeText += "#START#\r\n";
                string dataInfo = Newtonsoft.Json.JsonConvert.SerializeObject(gateway);
                writeText += dataInfo + "\r\n";
                writeText += "#GatewayInfo END#\r\n\r\n";
            }
        }
 
        /// <summary>
        /// 生成写入文件的【场景数据】
        /// </summary>
        /// <param name="writeText"></param>
        public void CreatWriteSceneData(ref string writeText)
        {
            //全部的场景
            var listScene = HdlSceneLogic.Current.GetAllLocalScene();
 
            foreach (var scene in listScene)
            {
                writeText += "#START#\r\n";
                string dataInfo = Newtonsoft.Json.JsonConvert.SerializeObject(scene);
                writeText += dataInfo + "\r\n";
                writeText += "#SceneTemplate END#\r\n\r\n";
            }
        }
 
        /// <summary>
        /// 生成写入文件的【房间模板数据】
        /// </summary>
        /// <param name="writeText"></param>
        public void CrearWriteRoomTemplateData(ref string writeText)
        {
            //楼层数据
            writeText += "#START#\r\n";
            string dataInfo1 = Newtonsoft.Json.JsonConvert.SerializeObject(Common.Config.Instance.Home.FloorDics);
            writeText += dataInfo1 + "\r\n";
            writeText += "#FloorInfo END#\r\n\r\n";
 
            //房间数据
            var listRoom = HdlRoomLogic.Current.GetAllListRooms();
            foreach (var room in listRoom)
            {
                writeText += "#START#\r\n";
                string dataInfo2 = Newtonsoft.Json.JsonConvert.SerializeObject(room);
                writeText += dataInfo2 + "\r\n";
                writeText += "#RoomInfo END#\r\n\r\n";
            }
        }
 
        /// <summary>
        /// 生成写入文件的【设备和网关选择的模板的数据】
        /// </summary>
        /// <param name="writeText"></param>
        public void CrearWriteDeviceSelectTemplateData(ref string writeText)
        {
            //设备选择模板的数据
            writeText += "#START#\r\n";
            string dataInfo1 = Newtonsoft.Json.JsonConvert.SerializeObject(this.modelData.dicDeviceTemplateSelect);
            writeText += dataInfo1 + "\r\n";
            writeText += "#DeviceSelectTemplate END#\r\n\r\n";
 
            //网关选择模板的数据
            writeText += "#START#\r\n";
            string dataInfo2 = Newtonsoft.Json.JsonConvert.SerializeObject(this.modelData.dicGatewayTemplateSelect);
            writeText += dataInfo2 + "\r\n";
            writeText += "#GatewaySelectTemplate END#\r\n\r\n";
        }
 
        #endregion
 
        #region ■ 强制执行的特殊函数_________________
 
        /// <summary>
        /// 强制从缓存当中生成设备和网关文件
        /// </summary>
        public void CreatDeviceAndGatewayFileFromMemoryByForce()
        {
            //原来的状态
            bool oldShowTemplate = Common.Config.Instance.Home.IsShowTemplate;
            //让它可以生成文件
            Common.Config.Instance.Home.IsShowTemplate = false;
 
            //生成设备文件
            foreach (var listDevice in this.modelData.dicDeviceInfo.Values)
            {
                foreach (var device in listDevice)
                {
                    device.ReSave();
                }
            }
            //生成网关文件
            foreach (var gateway in this.modelData.dicGatewayInfo.Values)
            {
                gateway.ReSave();
            }
            //还原状态
            Common.Config.Instance.Home.IsShowTemplate = oldShowTemplate;
        }
 
        #endregion
 
        #region ■ 设备和网关模板选择相关_____________
 
        /// <summary>
        /// 添加/修改 设备模板选择目标
        /// </summary>
        /// <param name="sourceMac">设备Mac对象</param>
        /// <param name="targetMac">目标Mac对象</param>
        public void AddDeviceTemplateSelect(string sourceMac, string targetMac)
        {
            //获取本地指定的Mac的全部设备
            var listDevice = HdlDeviceCommonLogic.Current.GetDevicesByMac(sourceMac, false);
            var otaDevice = HdlDeviceCommonLogic.Current.GetOTADevice(sourceMac);
            if (otaDevice != null)
            {
                //这里ota设备也要加进去,重中之重
                listDevice.Add(otaDevice);
            }
 
            foreach (var device in listDevice)
            {
                //模板选择的时候,他们的端点是一致的
                string localDeviceKey = HdlDeviceCommonLogic.Current.GetDeviceMainKeys(device);
                string templateDeviceKey = HdlDeviceCommonLogic.Current.GetDeviceMainKeys(targetMac, device.DeviceEpoint);
 
                if (this.modelData.dicDeviceTemplateRoom.ContainsKey(templateDeviceKey) == true
                    && (device is OTADevice) == false)
                {
                    //如果模板里面,这个端点设置有房间的话
                    HdlRoomLogic.Current.ChangedRoom(device, this.modelData.dicDeviceTemplateRoom[templateDeviceKey], false);
                }
                //如果这个端点有模板数据的话
                if (this.modelData.dicDeviceFileContent.ContainsKey(templateDeviceKey) == true)
                {
                    //如果原来它选择有模板数据的话
                    if (this.modelData.dicDeviceTemplateData.ContainsKey(localDeviceKey) == true)
                    {
                        //删除这个设备的模板保存文件
                        HdlFileLogic.Current.DeleteFile(System.IO.Path.Combine(HdlFileNameResourse.LocalTemplateDirectory, device.FilePath));
                        //移除当前端点保存的模板数据
                        this.modelData.dicDeviceTemplateData.Remove(localDeviceKey);
                    }
 
                    //反序列化设备数据
                    foreach (var strCentent in this.modelData.dicDeviceFileContent[templateDeviceKey])
                    {
                        var tempData = this.DeserializeDeviceDataByDiv(strCentent.saveDiv, strCentent.FileContent);
                        //这里需要替换掉Mac
                        tempData.DeviceMac = sourceMac;
                        //添加缓存(主键为本地设备的主键)
                        this.SetTemplateDeviceDataToMemmory(tempData, null, localDeviceKey, true);
                        //修改端点缓存名字
                        if (tempData.DataSaveDiv == ModelDeviceSaveEnum.A端点名称)
                        {
                            HdlDeviceCommonLogic.Current.SetEpointName(device, ((ModelDeviceEpointNameInfo)tempData).deviceEpointName);
                        }
                        else if (tempData.DataSaveDiv == ModelDeviceSaveEnum.A设备名称)
                        {
                            HdlDeviceCommonLogic.Current.SetMacName(device, ((ModelDeviceMacNameInfo)tempData).deviceMacName);
                        }
                    }
                }
                //不管如何,都需要则重新保存成文件
                this.SaveDeviceMemmoryData(device.DeviceAddr, device.DeviceEpoint);
                //还原及变更场景的执行目标
                //this.RecoverAndChangedSceneAdjustTarget(device, targetMac);
            }
            //更改物理设备所在的房间
            if (this.modelData.dicDeviceTemplateRealRoom.ContainsKey(targetMac) == true)
            {
                HdlRoomLogic.Current.SaveRealDeviceRoomId(listDevice, this.modelData.dicDeviceTemplateRealRoom[targetMac], false);
            }
 
            //记录缓存
            this.modelData.dicDeviceTemplateSelect[sourceMac] = targetMac;
            //保存的路径
            string fileData = Newtonsoft.Json.JsonConvert.SerializeObject(this.modelData.dicDeviceTemplateSelect);
            HdlFileLogic.Current.SaveTextToFile(HdlFileNameResourse.DeviceTemplateSelectFile, fileData);
        }
 
        /// <summary>
        /// 还原及变更场景的执行目标
        /// </summary>
        /// <param name="device">本地设备对象</param>
        /// <param name="targetMac">需要变更的模板设备的Mac</param>
        private void RecoverAndChangedSceneAdjustTarget(CommonDevice device, string targetMac)
        {
            //如果这个设备是替换选择的模板的话
            if (this.modelData.dicDeviceTemplateSelect.ContainsKey(device.DeviceAddr) == false)
            {
                return;
            }
            //全部的场景
            var listScene = HdlSceneLogic.Current.GetAllLocalScene();
 
            //还没有执行变更前,它目前选择的模板的Mac
            string oldTemplateMac = this.modelData.dicDeviceTemplateSelect[device.DeviceAddr];
            //将场景对象中,这个回路的主键,替换回原来模板中的Mac+端口
            foreach (var scene in listScene)
            {
                bool save = false;
                for (int i = 0; i < scene.AdjustTargetList.Count; i++)
                {
                    if (scene.AdjustTargetList[i].Type != 0)
                    {
                        //只处理设备绑定目标
                        continue;
                    }
                    //如果是当前回路
                    if (scene.AdjustTargetList[i].DeviceAddr == device.DeviceAddr &&
                       scene.AdjustTargetList[i].Epoint == device.DeviceEpoint)
                    {
                        //替换掉Mac
                        scene.AdjustTargetList[i].DeviceAddr = oldTemplateMac;
                        save = true;
                        continue;
                    }
                    //如果是模板目标回路
                    if (targetMac != null &&
                        scene.AdjustTargetList[i].DeviceAddr == targetMac &&
                        scene.AdjustTargetList[i].Epoint == device.DeviceEpoint)
                    {
                        //将目标的Mac变更为当前设备的Mac
                        scene.AdjustTargetList[i].DeviceAddr = device.DeviceAddr;
                        save = true;
                        continue;
                    }
                }
                if (save == true)
                {
                    //保存缓存
                    scene.Save();
                }
            }
        }
 
        /// <summary>
        /// 获取设备已经选择了的模板目标的设备的Mac(没有目标时,返回null)
        /// </summary>
        /// <param name="sourceMac">设备的Mac</param>
        /// <returns></returns>
        public string GetDeviceTemplateSelectMac(string sourceMac)
        {
            if (this.modelData.dicDeviceTemplateSelect.ContainsKey(sourceMac) == true)
            {
                return this.modelData.dicDeviceTemplateSelect[sourceMac];
            }
            return null;
        }
 
        /// <summary>
        /// 获取设备已经选择了的模板目标的设备的Mac名字(没有目标时,返回null)
        /// </summary>
        /// <param name="sourceMac">设备的Mac</param>
        /// <returns></returns>
        public string GetDeviceTemplateSelectName(string sourceMac)
        {
            if (this.modelData.dicDeviceTemplateSelect.ContainsKey(sourceMac) == true)
            {
                string tempMac = this.modelData.dicDeviceTemplateSelect[sourceMac];
                foreach (var listData in this.modelData.dicDeviceFileContent.Values)
                {
                    foreach (var data in listData)
                    {
                        if (data.DeviceMac != tempMac || data.saveDiv != ModelDeviceSaveEnum.A设备名称)
                        {
                            continue;
                        }
                        var tempModel = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelDeviceMacNameInfo>(data.FileContent);
                        return tempModel.deviceMacName;
                    }
                }
            }
            return null;
        }
 
        /// <summary>
        /// 取消设备模板的选择目标
        /// </summary>
        /// <param name="sourceMac">设备的Mac</param>
        public void RemoveDeviceTemplateSelect(string sourceMac)
        {
            ////获取本地指定的Mac的全部设备
            //var listDevice = HdlDeviceCommonLogic.Current.GetDevicesByMac(sourceMac, false);
            //foreach (var device in listDevice)
            //{
            //    //还原场景的执行目标
            //    this.RecoverAndChangedSceneAdjustTarget(device, null);
            //}
 
            //记录缓存
            this.modelData.dicDeviceTemplateSelect.Remove(sourceMac);
            //保存的路径
            string fileData = Newtonsoft.Json.JsonConvert.SerializeObject(this.modelData.dicDeviceTemplateSelect);
            HdlFileLogic.Current.SaveTextToFile(HdlFileNameResourse.DeviceTemplateSelectFile, fileData);
        }
 
        /// <summary>
        /// 添加/修改 网关模板选择目标
        /// </summary>
        /// <param name="sourceGwid">网关id</param>
        /// <param name="targetGwid">目标网关id(模板)</param>
        public void AddGatewayTemplateSelect(string sourceGwid, string targetGwid)
        {
            //记录缓存
            this.modelData.dicGatewayTemplateSelect[sourceGwid] = targetGwid;
            //保存的路径
            string fileData = Newtonsoft.Json.JsonConvert.SerializeObject(this.modelData.dicGatewayTemplateSelect);
            HdlFileLogic.Current.SaveTextToFile(HdlFileNameResourse.GatewayTemplateSelectFile, fileData);
 
            //变更网关房间
            if (this.modelData.dicDeviceTemplateRealRoom.ContainsKey(targetGwid) == true)
            {
                HdlRoomLogic.Current.ChangedGatewayRoom(sourceGwid, this.modelData.dicDeviceTemplateRealRoom[targetGwid]);
            }
            //变更网关名字
            //if (this.modelData.dicGatewayInfo.ContainsKey(targetGwid) == true)
            //{
            //    var localGateway = HdlGatewayLogic.Current.GetLocalGateway(sourceGwid);
            //    string gwName = HdlGatewayLogic.Current.GetGatewayName(this.modelData.dicGatewayInfo[targetGwid]);
            //    HdlGatewayLogic.Current.ReName(localGateway, gwName);
            //}
        }
 
        /// <summary>
        /// 取消网关模板选择目标
        /// </summary>
        /// <param name="sourceGwid">网关id</param>
        /// <param name="targetGwid">目标网关id</param>
        public void RemoveGatewayTemplateSelect(string sourceGwid)
        {
            //记录缓存
            this.modelData.dicGatewayTemplateSelect.Remove(sourceGwid);
            //保存的路径
            string fileData = Newtonsoft.Json.JsonConvert.SerializeObject(this.modelData.dicGatewayTemplateSelect);
            HdlFileLogic.Current.SaveTextToFile(HdlFileNameResourse.GatewayTemplateSelectFile, fileData);
        }
 
        /// <summary>
        /// 获取网关已经选择了的模板目标的网关的名字(没有目标时,返回null)
        /// </summary>
        /// <param name="sourceMac">网关id</param>
        /// <returns></returns>
        public string GetGatewayTemplateSelectName(string sourceGwid)
        {
            if (this.modelData.dicGatewayTemplateSelect.ContainsKey(sourceGwid) == true)
            {
                string tempMac = this.modelData.dicGatewayTemplateSelect[sourceGwid];
                if (this.modelData.dicGatewayInfo.ContainsKey(tempMac) == true)
                {
                    string gwName = HdlGatewayLogic.Current.GetGatewayName(this.modelData.dicGatewayInfo[tempMac]);
                    return gwName;
                }
            }
            return null;
        }
 
        /// <summary>
        /// 获取网关已经选择了的模板目标的网关的名字(没有目标时,返回null)
        /// </summary>
        /// <param name="sourceMac">网关id</param>
        /// <returns></returns>
        public string GetGatewayTemplateSelectId(string sourceGwid)
        {
            if (this.modelData.dicGatewayTemplateSelect.ContainsKey(sourceGwid) == true)
            {
                return this.modelData.dicGatewayTemplateSelect[sourceGwid];
            }
            return null;
        }
 
        /// <summary>
        /// 获取模板中全部网关的名字
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> GetAllGatewayTemplateName()
        {
            var dic = new Dictionary<string, string>();
            foreach (string gwId in this.modelData.dicGatewayInfo.Keys)
            {
                string gwName = HdlGatewayLogic.Current.GetGatewayName(this.modelData.dicGatewayInfo[gwId]);
                dic[gwId] = gwName;
            }
            return dic;
        }
 
        /// <summary>
        /// 获取指定网关能够选择的模板名字
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> GetGatewayCanSelectTemplateName(ZbGateway zbGateway)
        {
            var dic = new Dictionary<string, string>();
            foreach (var zbway in this.modelData.dicGatewayInfo.Values)
            {
                if (zbGateway.LinuxImageType == zbway.LinuxImageType)
                {
                    string gwName = HdlGatewayLogic.Current.GetGatewayName(zbway);
                    dic[zbway.GwId] = gwName;
                }
            }
            return dic;
        }
 
        /// <summary>
        /// 清除全部已经已经选择好了模板对象的设备和网关
        /// </summary>
        public void ClearAllSelectDeviceAndGateway()
        {
            //重新初始化
            this.modelData.dicDeviceTemplateSelect = new Dictionary<string, string>();
            this.modelData.dicGatewayTemplateSelect = new Dictionary<string, string>();
            //删掉这两个保存选择模板的文件
            HdlFileLogic.Current.DeleteFile(HdlFileNameResourse.DeviceTemplateSelectFile);
            HdlFileLogic.Current.DeleteFile(HdlFileNameResourse.GatewayTemplateSelectFile);
        }
 
        #endregion
 
        #region ■ 获取能够选择的模板_________________
 
        /// <summary>
        /// 获取能够选择的模板
        /// </summary>
        /// <param name="localDevice">本地设备</param>
        /// <returns></returns>
        public List<TemplateCanSelectContent> GetCanSelectDeviceTemplate(CommonDevice localDevice)
        {
            var listCanSelect = new List<TemplateCanSelectContent>();
            if (this.modelData.dicGatewayTemplateSelect.ContainsKey(localDevice.CurrentGateWayId) == false)
            {
                //该网关没有匹配模板,不提供模板选择
                return listCanSelect;
            }
            var listHadSelect = new HashSet<string>();
            foreach (var localMac in this.modelData.dicDeviceTemplateSelect.Keys)
            {
                //它自己的话,可以显示(因为有个取消绑定的功能)
                if (localMac != localDevice.DeviceAddr)
                {
                    //目前已经被选择了的模板Mac
                    listHadSelect.Add(this.modelData.dicDeviceTemplateSelect[localMac]);
                }
            }
 
            var listCheck = new HashSet<string>();
            //设备的模块ID
            string modelId = this.GetDeviceModelId(localDevice.DeviceAddr);
            //模板中的网关ID
            string gatewayTemplateId = this.modelData.dicGatewayTemplateSelect[localDevice.CurrentGateWayId];
            foreach (var mainKey in this.modelData.dicDeviceFileContent.Keys)
            {
                var listData = this.modelData.dicDeviceFileContent[mainKey];
                foreach (var data in listData)
                {
                    if (data.saveDiv != ModelDeviceSaveEnum.A设备名称)
                    {
                        //只获取设备mac名称的模板数据
                        continue;
                    }
                    if (listHadSelect.Contains(data.DeviceMac) == true
                        || listCheck.Contains(data.DeviceMac) == true)
                    {
                        //如果这个模板已经被其他设备选择了,或者这个Mac已经处理了,则break
                        break;
                    }
                    if (this.modelData.dicDeviceInfo.ContainsKey(data.DeviceMac) == false
                        || this.modelData.dicDeviceInfo[data.DeviceMac][0].CurrentGateWayId != gatewayTemplateId
                        || this.modelData.dicDeviceInfo[data.DeviceMac][0].ModelIdentifier != modelId)
                    {
                        //该模板不是这个网关的,或者模块ID不一样的
                        break;
                    }
                    if (localDevice.DriveCode != 0)
                    {
                        //如果是虚拟设备,则如果不是同一个端点,则不能作为模板对象
                        string checkKey = HdlDeviceCommonLogic.Current.GetDeviceMainKeys(data.DeviceMac, localDevice.DeviceEpoint);
                        if (checkKey != mainKey)
                        {
                            break;
                        }
                    }
                    listCheck.Add(data.DeviceMac);
 
                    var info = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelDeviceMacNameInfo>(data.FileContent);
                    var selectData = new TemplateCanSelectContent();
                    selectData.DeviceMac = data.DeviceMac;
                    selectData.DeviceName = info.deviceMacName;
 
                    Common.Room room = null;
                    if (this.modelData.dicDeviceTemplateRealRoom.ContainsKey(data.DeviceMac) == true)
                    {
                        room = HdlRoomLogic.Current.GetRoomById(this.modelData.dicDeviceTemplateRealRoom[data.DeviceMac]);
                    }
                    selectData.RoomName = HdlRoomLogic.Current.GetRoomName(room);
 
                    listCanSelect.Add(selectData);
                }
            }
            return listCanSelect;
        }
 
        #endregion
 
        #region ■ 删除设备___________________________
 
        /// <summary>
        /// 删除设备
        /// </summary>
        /// <param name="device"></param>
        public void DeleteDevice(CommonDevice device)
        {
            //删除保存文件
            string saveFile = HdlFileNameResourse.LocalTemplateDirectory;
            saveFile = System.IO.Path.Combine(saveFile, device.FilePath);
            HdlFileLogic.Current.DeleteFile(saveFile);
 
            //移除模板缓存
            this.modelData.dicDeviceTemplateData.Remove(HdlDeviceCommonLogic.Current.GetDeviceMainKeys(device));
            //移除设备选择缓存
            if (this.modelData.dicDeviceTemplateSelect.ContainsKey(device.DeviceAddr) == true)
            {
                this.modelData.dicDeviceTemplateSelect.Remove(device.DeviceAddr);
                HdlFileLogic.Current.SaveFileContent(HdlFileNameResourse.DeviceTemplateSelectFile, this.modelData.dicDeviceTemplateSelect);
            }
        }
 
        #endregion
 
        #region ■ 保存设备缓存_______________________
 
        /// <summary>
        /// 保存设备缓存(考虑有的设备用的是200端点,所以这里最好不用设备对象作为参数)
        /// </summary>
        /// <param name="deviceMac">设备mac</param>
        /// <param name="deviceEpoint">设备Epoint</param>
        public void SaveDeviceMemmoryData(string deviceMac, int deviceEpoint)
        {
            string mainkey = HdlDeviceCommonLogic.Current.GetDeviceMainKeys(deviceMac, deviceEpoint);
            if (this.modelData.dicDeviceTemplateData.ContainsKey(mainkey) == false)
            {
                return;
            }
 
            //保存路径
            string saveFile = HdlFileNameResourse.LocalTemplateDirectory;
            saveFile = System.IO.Path.Combine(saveFile, "Device_" + mainkey);
 
            var listData = this.modelData.dicDeviceTemplateData[mainkey];
            if (listData.Count == 0)
            {
                //删除掉这个文件
                HdlFileLogic.Current.DeleteFile(saveFile);
                return;
            }
            //写入文件的内容
            string writeText = string.Empty;
            foreach (var data in listData)
            {
                writeText += "===>" + (int)data.DataSaveDiv + "\r\n";
                string dataInfo = Newtonsoft.Json.JsonConvert.SerializeObject(data);
                writeText += dataInfo + "\r\n";
            }
            //写入内容
            HdlFileLogic.Current.SaveTextToFile(saveFile, writeText);
        }
 
        #endregion
 
        #region ■ 获取需要升级的设备对象_____________
 
        /// <summary>
        /// 获取需要升级的设备对象,按网关分组(key:网关ID value的key:本地Ota设备的Mac value:升级固件地址)
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, Dictionary<string, string>> GetNeedToUpdateDevice()
        {
            var dicGatewayDevice = new Dictionary<string, Dictionary<string, string>>();
            //循环设备匹配的模板
            foreach (var localMac in this.modelData.dicDeviceTemplateSelect.Keys)
            {
                string tempMac = this.modelData.dicDeviceTemplateSelect[localMac];
                if (this.modelData.dicDeviceInfo.ContainsKey(tempMac) == false)
                {
                    //应该不会进来,即使进来,我也不知道为什么
                    continue;
                }
                //取本地Ota设备对象
                var localOta = HdlDeviceCommonLogic.Current.GetOTADevice(localMac);
                if (localOta == null)
                {
                    //应该不会进来,即使进来,我也不知道为什么
                    continue;
                }
                foreach (var tempDevice in this.modelData.dicDeviceInfo[tempMac])
                {
                    //取模板Ota设备对象
                    if (tempDevice is OTADevice)
                    {
                        //只要两者的固件版本不一样,并且本地有这个升级固件,则都需要升级
                        if (localOta.ImgVersion != tempDevice.ImgVersion
                            && HdlFirmwareUpdateLogic.Current.IsEsixtDeviceFirmwareFile((OTADevice)tempDevice) == true)
                        {
                            //按网关分组
                            if (dicGatewayDevice.ContainsKey(localOta.CurrentGateWayId) == false)
                            {
                                dicGatewayDevice[localOta.CurrentGateWayId] = new Dictionary<string, string>();
                            }
                            var dicDevice = dicGatewayDevice[localOta.CurrentGateWayId];
                            dicDevice[localMac] = HdlFirmwareUpdateLogic.Current.GetDeviceFirmwareFile((OTADevice)tempDevice);
                        }
                        break;
                    }
                }
            }
 
            return dicGatewayDevice;
        }
 
        /// <summary>
        /// 获取需要升级的网关对象(key:本地网关的id value:升级固件地址,第一位是Linux,第二位是协调器,之后都是虚拟驱动)
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, List<GatewayNeedUpdateInfo>> GetNeedToUpdateGateway()
        {
            var dicGateway = new Dictionary<string, List<GatewayNeedUpdateInfo>>();
            //循环网关匹配的模板
            foreach (var localId in this.modelData.dicGatewayTemplateSelect.Keys)
            {
                string tempId = this.modelData.dicGatewayTemplateSelect[localId];
                if (this.modelData.dicGatewayInfo.ContainsKey(tempId) == false)
                {
                    //应该不会进来,即使进来,我也不知道为什么
                    continue;
                }
                //取本地网关对象
                var localGateway = HdlGatewayLogic.Current.GetLocalGateway(localId);
                if (localGateway == null)
                {
                    //应该不会进来,即使进来,我也不知道为什么
                    continue;
                }
                var tempGateway = this.modelData.dicGatewayInfo[tempId];
                //初始化容器
                var listUpdateInfo = new List<GatewayNeedUpdateInfo>() { null, null };
 
                bool needUpdate = false;
                //Linux版本比较
                if (tempGateway.LinuxFirmwareVersion != localGateway.LinuxFirmwareVersion)
                {
                    //Linux升级固件文件全路径
                    string updateFile = HdlFirmwareUpdateLogic.Current.GetGatewayLinuxFirmwareFile(tempGateway);
                    if (System.IO.File.Exists(updateFile) == true)
                    {
                        //如果存在的话
                        var info = new GatewayNeedUpdateInfo();
                        info.Div = 1;
                        info.FullFileName = updateFile;
                        listUpdateInfo[0] = info;
                        needUpdate = true;
                    }
                }
                //协调器版本比较
                if (tempGateway.CoordinatorFirmwareVersion != localGateway.CoordinatorFirmwareVersion)
                {
                    //协调器升级固件文件全路径
                    string updateFile = HdlFirmwareUpdateLogic.Current.GetGatewayCoordinatorFirmwareFile(tempGateway);
                    if (System.IO.File.Exists(updateFile) == true)
                    {
                        //如果存在的话
                        var info = new GatewayNeedUpdateInfo();
                        info.Div = 2;
                        info.FullFileName = updateFile;
                        listUpdateInfo[1] = info;
                        needUpdate = true;
                    }
                }
                //虚拟驱动比较
                if (HdlGatewayLogic.Current.CheckGatewayHadDriveCode(localGateway) == true)
                {
                    foreach (var localCode in localGateway.DriveCodeList)
                    {
                        foreach (var tempCode in tempGateway.DriveCodeList)
                        {
                            //防止它放的顺序不样
                            if (localCode.DriveCode == tempCode.DriveCode && localCode.DriveFwVersion != tempCode.DriveFwVersion)
                            {
                                //虚拟驱动升级固件文件全路径
                                string updateFile = HdlFirmwareUpdateLogic.Current.GetGatewayDriveCodeFirmwareFile(tempCode);
                                if (System.IO.File.Exists(updateFile) == true)
                                {
                                    //如果存在的话
                                    var info = new GatewayNeedUpdateInfo();
                                    info.Div = 3;
                                    info.DriveCode = tempCode.DriveCode;
                                    info.FullFileName = updateFile;
                                    listUpdateInfo.Add(info);
                                    needUpdate = true;
                                }
                            }
                        }
                    }
                }
                if (listUpdateInfo.Count == 2)
                {
                    listUpdateInfo.Add(null);
                }
                //添加目标缓存
                if (needUpdate == true)
                {
                    dicGateway[localId] = listUpdateInfo;
                }
            }
 
            return dicGateway;
        }
 
        #endregion
 
        #region ■ 从模板文件中获取对象(外部调用)_____
 
        /// <summary>
        /// 从模板文件中,获取设备和网关对象
        /// </summary>
        /// <param name="fullFileName">模板文件的全路径</param>
        /// <param name="listDevice">ota设备列表</param>
        /// <param name="listGateway">网关列表</param>
        public void GetDeviceObjectFromTemplate(string fullFileName, ref List<OTADevice> listDevice, ref List<ZbGateway> listGateway)
        {
            var fileData = HdlFileLogic.Current.ReadFileTextContent(fullFileName);
            if (fileData == null)
            {
                return;
            }
            var deviceType = string.Empty;
            var strTempContentData = string.Empty;
 
            //根据换行符切分数据文本
            string[] arryData = fileData.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string strData in arryData)
            {
                if (strData == "#START#")
                {
                    //无附加数据的【数据标题】
                    strTempContentData = string.Empty;
                    continue;
                }
                if (strData.StartsWith("#DeviceInfo START#") == true)
                {
                    //附加数据:设备对象类型
                    deviceType = strData.Substring(18);
                    strTempContentData = string.Empty;
                    continue;
                }
                try
                {
                    //设备对象
                    if (strData == "#DeviceInfo END#")
                    {
                        //反序列化设备
                        if (deviceType == "OtaDevice" || deviceType == "OtaPanelDevice")
                        {
                            var device = Newtonsoft.Json.JsonConvert.DeserializeObject<OTADevice>(strTempContentData);
                            if (device != null)
                            {
                                listDevice.Add(device);
                            }
                        }
 
                        strTempContentData = string.Empty;
                        continue;
                    }
                    //网关对象数据
                    else if (strData == "#GatewayInfo END#")
                    {
                        //反序列化设备
                        var gateway = Newtonsoft.Json.JsonConvert.DeserializeObject<ZbGateway>(strTempContentData);
                        if (gateway != null)
                        {
                            listGateway.Add(gateway);
                        }
                        strTempContentData = string.Empty;
                        return;
                    }
                    strTempContentData += strData;
                }
                catch (Exception ex)
                {
                    HdlLogLogic.Current.WriteLog(ex, "模板bin文件出问题\r\n" + strTempContentData);
                    strTempContentData = string.Empty;
                }
            }
        }
 
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 复制模板bin文件到本地的模板文件夹里
        /// </summary>
        /// <param name="templateFileName">模板文件的名字(全住宅存放的模板)</param>
        public void CopyTemplateFileToLocalDirectory(string templateFileName)
        {
            string sourceFile = System.IO.Path.Combine(HdlFileNameResourse.AllResidenceTemplateDirectory, templateFileName);
 
            this.CopyTemplateFileToLocalDirectory2(sourceFile);
        }
 
        /// <summary>
        /// 复制模板bin文件到本地的模板文件夹里
        /// </summary>
        /// <param name="fullTemplateName">模板文件的全路径</param>
        public void CopyTemplateFileToLocalDirectory2(string fullTemplateName)
        {
            if (System.IO.File.Exists(fullTemplateName) == false)
            {
                return;
            }
 
            //保存的路径
            string targetFile = HdlFileNameResourse.LocalTemplateDirectory;
            targetFile = System.IO.Path.Combine(targetFile, TemplateFileName);
 
            try { System.IO.File.Copy(fullTemplateName, targetFile, true); }
            catch (Exception ex) { HdlLogLogic.Current.WriteLog(ex, "复制模板bin文件失败"); }
        }
 
        /// <summary>
        /// 删除本地全部的模板缓存文件
        /// </summary>
        public void DeleteAllLocalFile()
        {
            //获取这个路径下面全部的文件
            var listFile = HdlFileLogic.Current.GetFileFromDirectory(HdlFileNameResourse.LocalTemplateDirectory, false); ;
            foreach (var file in listFile)
            {
                HdlFileLogic.Current.DeleteFile(file);
            }
        }
 
        /// <summary>
        /// 获取模板中的设备数
        /// </summary>
        /// <returns></returns>
        public int GetTemplateDeviceCount()
        {
            return this.modelData.dicDeviceInfo.Count;
        }
 
        /// <summary>
        /// 检测设备模板数和当前设备选择的模板数是否一致
        /// </summary>
        /// <returns></returns>
        public bool CheckTemplateDeviceCountAndSelectCountIsEqual()
        {
            return this.modelData.dicDeviceInfo.Count == this.modelData.dicDeviceTemplateSelect.Count;
        }
 
        /// <summary>
        /// 获取文件里指定的内容
        /// </summary>
        /// <param name="fileContrnt">文件文本</param>
        /// <param name="startFlage">开始字符</param>
        /// <param name="endFlage">结束字符</param>
        /// <returns></returns>
        private string GetDataFromFileContent(string fileContrnt, string startFlage, string endFlage)
        {
            string[] arryValue = fileContrnt.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            string modelBaseInfo = string.Empty;
            bool getData = false;
            bool success = false;
            foreach (var strValue in arryValue)
            {
                //开始
                if (strValue == startFlage)
                {
                    getData = true;
                    //同一开始字符的东西很多
                    modelBaseInfo = string.Empty;
                    continue;
                }
                //结束
                if (strValue == endFlage)
                {
                    success = true;
                    break;
                }
                if (getData == true)
                {
                    modelBaseInfo += strValue;
                }
            }
            return success == true ? modelBaseInfo : string.Empty;
        }
 
        /// <summary>
        /// 获取模块ID
        /// </summary>
        /// <param name="deviceMac"></param>
        /// <returns></returns>
        private string GetDeviceModelId(string deviceMac)
        {
            var listDevice = HdlDeviceCommonLogic.Current.GetDevicesByMac(deviceMac);
            foreach (var device in listDevice)
            {
                if (device.ModelIdentifier != string.Empty)
                {
                    return device.ModelIdentifier;
                }
            }
            return string.Empty;
        }
 
        /// <summary>
        /// 获取设备保存的模板对象(考虑有的设备用的是200端点,所以这里最好不用设备对象作为参数)
        /// </summary>
        /// <param name="device"></param>
        /// <param name="saveEnum"></param>
        /// <returns></returns>
        public TemplateDeviceDataCommon GetDeviceModelDataClass(string deviceMac, int deviceEpoint, ModelDeviceSaveEnum saveEnum, TemplateDeviceDataCommon newClass)
        {
            string mainkey = HdlDeviceCommonLogic.Current.GetDeviceMainKeys(deviceMac, deviceEpoint);
 
            //创建存储空间
            if (this.modelData.dicDeviceTemplateData.ContainsKey(mainkey) == false)
            {
                this.modelData.dicDeviceTemplateData[mainkey] = new List<TemplateDeviceDataCommon>();
            }
            foreach (var data in this.modelData.dicDeviceTemplateData[mainkey])
            {
                //如果是已经存在了的
                if (data.DataSaveDiv == saveEnum)
                {
                    return data;
                }
            }
            //新建一个新的对象
            newClass.DataSaveDiv = saveEnum;
            newClass.DeviceEpoint = deviceEpoint;
            newClass.DeviceMac = deviceMac;
 
            //默认创建一个索引位
            newClass.ListReceiveResult.Add(string.Empty);
            newClass.ListReceiveTopic.Add(string.Empty);
            newClass.ListSendTopic.Add(string.Empty);
 
            this.modelData.dicDeviceTemplateData[mainkey].Add(newClass);
 
            return newClass;
        }
 
        /// <summary>
        /// 显示信息框
        /// </summary>
        /// <param name="msgType">信息类型</param>
        /// <param name="msg">信息</param>
        /// <param name="action">单击确认后执行的回调函数</param>
        /// <param name="buttonText">按钮的文本</param>
        private void ShowMassage(ShowMsgType msgType, string msg, Action action = null, string buttonText = null)
        {
            HdlMessageLogic.Current.ShowMassage(msgType, msg, action, buttonText);
        }
 
        #endregion
    }
}