黄学彪
2019-11-25 5727cf0b9b54da0a191dd1e23cb5abf21320fbff
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
using System;
using System.Collections.Generic;
using Shared.Phone.UserCenter;
 
namespace Shared.Common
{
    /// <summary>
    /// 住宅文件
    /// </summary>
    [System.Serializable]
    public class House
    {
        #region ◆ 变量____________________________
        /// <summary>
        /// 住宅文件
        /// </summary>
        /// <value>The file path.</value>
        [Newtonsoft.Json.JsonIgnore]
        public string FileName
        {
            get
            {
                return $"House_{Id}.json";
            }
        }
 
        /// <summary>
        /// 住宅id--使用云端提供的住宅唯一Id
        /// </summary>
        public string Id = string.Empty;
 
        /// <summary>
        /// 住宅名称
        /// </summary>
        public string Name = string.Empty;
 
        /// <summary>
        /// 是否为其他主用户分享过来的住宅
        /// </summary>
        public bool IsOthreShare;
        /// <summary>
        /// 当前住宅是其他主帐号分享过来的主帐号的分布式Id
        /// </summary>
        public string MainUserDistributedMark;
 
        /// <summary>
        /// 仅子账号登陆的时候使用,当【IsOthreShare】为"true",并且【AccountType】为"1"时,该账号拥有管理员权限
        /// </summary>
        public int AccountType;
        /// <summary>
        /// 经度
        /// </summary>
        public double Longitude = 0;
        /// <summary>
        /// 纬度
        /// </summary>
        public double Latitude = 0;
 
        /// <summary>
        /// 房间路径列表
        /// </summary>
        public List<string> RoomFilePathList = new List<string> { };
 
        /// <summary>
        /// 楼层字典
        /// key:FloorId
        /// value:FloorName
        /// </summary>
        public Dictionary<string,string> FloorDics = new Dictionary<string,string> { };
        /// <summary>
        /// 当前楼层
        /// </summary>
        public string CurrentFloorId;
 
        /// <summary>
        /// 全局场景路径列表---备用
        /// </summary>
        public List<string> SceneFilePathList = new List<string> { };
        /// <summary>
        /// 设备路径列表---备用
        /// </summary>
        public List<string> DeviceFilePathList = new List<string> { };
        /// <summary>
        /// 功能路径列表---备用
        /// </summary>
        public List<string> FunctionFilePathList = new List<string> { };
        /// <summary>
        /// 通用标识--备用
        /// </summary>
        public object Tag;
        /// <summary>
        /// 期、栋、层等区域---备用
        /// </summary>
        public Dictionary<string, string> LocationInfoList = new Dictionary<string, string> { };
 
        #endregion
 
        #region ◆ 住宅____________________________
 
        #region ◆ 添加住宅_________________________
 
        /// <summary>
        /// 添加住宅
        /// </summary>
        /// <param name="houseId">住宅id</param>
        /// <param name="houseName">住宅名称.</param>
        /// <param name="numHomeId">住宅数字型id</param>
        public static void AddHouse(string houseId, string houseName, int numHomeId)
        {
            AddHouse(houseId, houseName, false, 0);
        }
 
        /// <summary>
        /// 添加住宅
        /// </summary>
        /// <param name="houseId">住宅id</param>
        /// <param name="houseName">住宅名称.</param>
        /// <param name="isOthreShare">是否为其他主用户分享过来的住宅</param>
        /// <param name="accountType">仅子账号登陆的时候使用,当【IsOthreShare】为"true",并且【AccountType】为"1"时,该账号拥有管理员权限</param>
        public static void AddHouse(string houseId, string houseName, bool isOthreShare, int accountType)
        {
            var home = new House
            {
                Id = houseId,
                Name = houseName,
                IsOthreShare = isOthreShare,
                AccountType = accountType
            };
            //创建文件夹
            Global.CreateHomeDirectory(houseId);
            home.Save();
            Config.Instance.HomeFilePathList.Add(home.FileName);
            Config.Instance.Save();
        }
 
 
        #endregion
 
        #region ◆ 删除住宅_________________________
 
        /// <summary>
        /// 删除住宅
        /// </summary>
        /// <param name="filePath">File path.</param>
        public static void DeleteHouse(string filePath)
        {
            var delPath = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Config.Instance.Guid, GetHouseIdByFilePath(filePath));
            if (System.IO.Directory.Exists(delPath) == false)
            {
                return;
            }
            var fileList = GetHouseFileListByFilePath(filePath);
            foreach (var file in fileList)
            {
                //删除文件
                System.IO.File.Delete(System.IO.Path.Combine(delPath, file));
            }
            //删除文件夹
            System.IO.Directory.Delete(delPath, true);
            //HomeFilePathList中删除该列表
            Common.Config.Instance.HomeFilePathList.RemoveAll((obj) => obj == filePath);
            Config.Instance.Save();
        }
 
        /// <summary>
        /// 删除住宅
        /// </summary>
        /// <param name="houseId">House identifier.</param>
        public static void DeleteHouseByHouseId(string houseId)
        {
            DeleteHouse(GetHouseFilePathByHouseId(houseId));
        }
 
        #endregion
 
        #region ◆ 修改住宅_________________________
 
        /// <summary>
        /// 修改住宅.
        /// </summary>
        /// <param name="houseId">House identifier.</param>
        /// <param name="houseName">House name.</param>
        public static void EditorHouseByHouseId(string houseId, string houseName)
        {
            var home = GetHouseByHouseId(houseId);
            if (home == null)
            {
                return;
            }
            home.Name = houseName;
            home.Save();
            Config.Instance.Save();
        }
 
        #endregion
 
        #region ◆ 获取住宅_________________________
        /// <summary>
        /// 通过【id】获取住宅
        /// </summary>
        /// <returns>The house by house identifier.</returns>
        /// <param name="houseId">住宅id</param>
        public static House GetHouseByHouseId(string houseId)
        {
            var path = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Config.Instance.Guid, houseId, $"House_{houseId}.json");
            var file = Shared.IO.FileUtils.ReadFile(path);
            if (file == null)
            {
                return null;
            }
            return Newtonsoft.Json.JsonConvert.DeserializeObject<House>(System.Text.Encoding.UTF8.GetString(file));
        }
 
        /// <summary>
        /// 通过【文件路径】获取住宅
        /// </summary>
        /// <returns>The house by file path.</returns>
        /// <param name="filePath">文件路径</param>
        public static House GetHouseByFilePath(string filePath)
        {
            var path = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Config.Instance.Guid, GetHouseIdByFilePath(filePath), filePath);
            var file = Shared.IO.FileUtils.ReadFile(path);
            if (file == null)
            {
                return null;
            }
            return Newtonsoft.Json.JsonConvert.DeserializeObject<House>(System.Text.Encoding.UTF8.GetString(file));
        }
 
        #endregion
 
        #region ◆ 获取住宅列表_____________________
        /// <summary>
        /// Gets the home lists.获取住宅列表
        /// </summary>
        public static async System.Threading.Tasks.Task<List<string>> GetHomeLists()
        {
            //清空当前住宅列表
            Config.Instance.HomeFilePathList.Clear();
            var pageSetting = new SendDataToServer.ResidenceListPageSettingObj()
            {
                PageSize = CommonPage.PageSize
            };
 
            var reqDto = new SendDataToServer.ResidenceListObj()
            {
                LoginAccessToken = Config.Instance.Token,
                PageSetting = pageSetting
            };
            var requestObj = new SendDataToServer.ResidenceListReqDto()
            {
                ReqDto = reqDto,
                RequestVersion = CommonPage.RequestVersion,
            };
            try
            {
                var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject(requestObj);
                var revertObj = await CommonPage.Instance.RequestHttpsZigbeeAsync("App/GetHomePager", System.Text.Encoding.UTF8.GetBytes(requestJson));
                if (revertObj == null)
                {
                    return null;
                }
                if (revertObj.StateCode.ToUpper() == "SUCCESS")
                {
                    var responseDataObj = Newtonsoft.Json.JsonConvert.DeserializeObject<Shared.Common.ResponseEntity.ResidenceRes>(revertObj.ResponseData.ToString());
                    if (responseDataObj.TotalCount == 0)
                    {
                        //当住宅为空时先提示用户新建住宅
                        var alert = new Alert(Language.StringByID(R.MyInternationalizationString.TIP), Language.StringByID(R.MyInternationalizationString.CurrentlyTheUserIshHouseIsEmptyPleaseBuildANewHouseFirst), Language.StringByID(R.MyInternationalizationString.Close), Language.StringByID(R.MyInternationalizationString.OK));
                        alert.Show();
                        alert.ResultEventHandler += (sender, e) =>
                        {
                            if (e)
                            {
                                //
                            }
                        };
                    }
                    else
                    {
                        if (Config.Instance.HomeId == string.Empty && responseDataObj.PageData.Count > 0)
                        {
                            //赋一个初始值
                            Config.Instance.HomeId = responseDataObj.PageData[0].Id;
                        }
                        foreach (var residence in responseDataObj.PageData)
                        {
                            Config.Instance.HomeFilePathList.Add($"House_{residence.Id}.json");
                            var house = GetHouseByHouseId(residence.Id);
                            if (house == null)
                            {
                                house = new House
                                {
                                    Id = residence.Id,
                                    Name = residence.Name,
                                    IsOthreShare = residence.IsOthreShare,
                                    AccountType = residence.AccountType,
                                    MainUserDistributedMark = residence.MainUserDistributedMark,
                                    Longitude = residence.Longitude,
                                    Latitude = residence.Latitude
                                };
                            }
                            else
                            {
                                house.Id = residence.Id;
                                house.Name = residence.Name;
                                house.IsOthreShare = residence.IsOthreShare;
                                house.AccountType = residence.AccountType;
                                house.MainUserDistributedMark = residence.MainUserDistributedMark;
                                house.Longitude = residence.Longitude;
                                house.Latitude = residence.Latitude;
                            }
                            Global.CreateHomeDirectory(residence.Id);
                            house.Save();
                        }
                        //匹配当前住宅
                        if (Config.Instance.HomeFilePathList.Find((obj) => obj == $"House_{Config.Instance.HomeId}.json") == null)
                        {
                            Config.Instance.HomeId = GetHouseIdByFilePath(Config.Instance.HomeFilePathList[0]);
                        }
                        Config.Instance.Save();
                        //检测住宅对象
                        if (Config.Instance.Home.Id == string.Empty)
                        {
                            Config.Instance.Home = House.GetHouseByHouseId(Config.Instance.HomeId);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine($"获取失败{ex.Message}");
            }
            finally
            {
            }
            return Config.Instance.HomeFilePathList;
        }
 
        #endregion
 
        #region ◆ 获取住宅id_______________________
        /// <summary>
        /// 通过【文件路径】获取住宅id
        /// </summary>
        /// <returns>The house identifier by file path.</returns>
        /// <param name="filePath">文件路径</param>
        public static string GetHouseIdByFilePath(string filePath)
        {
            string[] sArray = filePath.Split(new string[] { "House_", ".json" }, StringSplitOptions.RemoveEmptyEntries);
            if (sArray.Length >= 1)
            {
                return sArray[0];
            }
            return null;
        }
 
        #endregion
 
        #region ◆ 获取住宅路径_____________________
        /// <summary>
        /// 通过【住宅id】获取住宅路径
        /// </summary>
        /// <returns>The house identifier by file path.</returns>
        /// <param name="houseId">住宅id</param>
        public static string GetHouseFilePathByHouseId(string houseId)
        {
            if (string.IsNullOrEmpty(houseId))
            {
                return null;
            }
            return $"House_{houseId}.json";
        }
 
        #endregion
 
        #region ◆ 获取该住宅的文件列表______________
 
        /// <summary>
        /// 通过【houseId】获取该住宅的文件列表
        /// </summary>
        /// <returns>The house file list by home identifier.</returns>
        /// <param name="houseId">House identifier.</param>
        public static List<string> GetHouseFileListByHomeId(string houseId)
        {
            var list = new List<string> { };
            var path = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Config.Instance.Guid, houseId);
            if (!System.IO.Directory.Exists(path))
            {
                return new List<string> { };
            }
            var files = System.IO.Directory.GetFiles(path);
            foreach (var file in files)
            {
                var f = file.Substring(path.Length + 1);
                System.Console.WriteLine(f);
                list.Add(f);
            }
            return list;
        }
 
        /// <summary>
        /// 通过【文件路径】获取住宅下文件列表
        /// </summary>
        /// <returns>The house file list by file path.</returns>
        /// <param name="filePath">File path.</param>
        public static List<string> GetHouseFileListByFilePath(string filePath)
        {
            return GetHouseFileListByHomeId(GetHouseIdByFilePath(filePath));
        }
 
        #endregion
 
        #endregion
 
        #region ◆ 楼层____________________________
 
        /// <summary>
        /// GetCurrentFloorName
        /// </summary>
        /// <returns></returns>
        public string GetCurrentFloorName
        {
            get
            {
                return GetFloorNameById(CurrentFloorId);
            }
        }
 
        /// <summary>
        /// 获取楼层名称
        /// </summary>
        /// <param name="floorId"></param>
        /// <returns></returns>
        public string GetFloorNameById(string floorId)
        {
            if (Config.Instance.Home.FloorDics.Count == 0)
            {
                return null;
            }
 
            foreach (var floor in Config.Instance.Home.FloorDics)
            {
                if (floorId == floor.Key)
                {
                    return floor.Value;
                }
            }
            return null;
        }
 
        #endregion
 
        #region ◆ 房间____________________________
 
        #region ◆ 添加房间路径_________________________
 
        /// <summary>
        /// 添加【房间路径】到房间路径列表
        /// </summary>
        /// <returns><c>true</c>, if room list file path was added, <c>false</c> otherwise.</returns>
        /// <param name="roomFilePath">Room file path.</param>
        public bool AddRoomListFilePath(string roomFilePath)
        {
            if (RoomFilePathList.Contains(roomFilePath))
            {
                return false;
            }
            RoomFilePathList.Add(roomFilePath);
            Save();
            return true;
        }
 
        #endregion
 
        #region ◆ 删除房间路径_________________________
 
        /// <summary>
        /// 移除房间路径
        /// </summary>
        /// <returns><c>true</c>, if room list file path was added, <c>false</c> otherwise.</returns>
        /// <param name="roomFilePath">Room file path.</param>
        public bool RemoveRoomListFilePath(string roomFilePath)
        {
            if (!RoomFilePathList.Contains(roomFilePath))
            {
                return false;
            }
            RoomFilePathList.Remove(roomFilePath);
            Save();
            return true;
        }
 
        #endregion
 
        #endregion
 
        #region ◆ 保存____________________________
 
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="autoBackup">是否备份</param>
        public void Save(bool autoBackup = true)
        {
            var path = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Config.Instance.Guid, Id);
            //如果没有存在住宅目录,先创建
            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
            path = System.IO.Path.Combine(path, FileName);
            Shared.IO.FileUtils.WriteFileByBytes(path, System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this)));
            if (autoBackup == true && Id == Config.Instance.HomeId)
            {
                HdlAutoBackupLogic.AddOrEditorFile(FileName);
            }
        }
        #endregion
    }
}