黄学彪
2020-09-22 ade5917841b0fdcb1df7353ef7c56b1a1bdc9282
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
using Shared.Common;
using System;
using System.Collections.Generic;
using System.Text;
 
namespace Shared.Phone.UserCenter
{
    /// <summary>
    /// 住宅对象的逻辑
    /// </summary>
    public class HdlResidenceLogic
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 住宅对象的逻辑
        /// </summary>
        private static HdlResidenceLogic m_Current = null;
        /// <summary>
        /// 住宅对象的逻辑
        /// </summary>
        public static HdlResidenceLogic Current
        {
            get
            {
                if (m_Current == null)
                {
                    m_Current = new HdlResidenceLogic();
                }
                return m_Current;
            }
        }
 
        #endregion
 
        #region ■ 创建新住宅_________________________
 
        /// <summary>
        /// 创建新的住宅(返回住宅id,null代表失败)
        /// </summary>
        /// <param name="residenceName">住宅名称</param>
        public string CreatNewResidence(string residenceName)
        {
            var Pra = new AddResidencePra();
            Pra.Name = residenceName;
            //添加住宅
            string resultData = UserCenterLogic.GetResponseDataByRequestHttps("App/AddHome", false, Pra);
            if (string.IsNullOrEmpty(resultData) == true)
            {
                return null;
            }
            var newInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NewResidenceInfo>>(resultData);
 
            //添加住宅到缓存
            this.AddHouseToMemmory(newInfo[0].Id, residenceName);
 
            return newInfo[0].Id;
        }
 
        /// <summary>
        /// 添加住宅到缓存
        /// </summary>
        /// <param name="houseId">住宅id</param>
        /// <param name="houseName">住宅名称.</param>
        /// <param name="isOthreShare">是否为其他主用户分享过来的住宅</param>
        /// <param name="accountType">仅子账号登陆的时候使用,当【IsOthreShare】为"true",并且【AccountType】为"1"时,该账号拥有管理员权限</param>
        public void AddHouseToMemmory(string houseId, string houseName,
            bool isOthreShare = false, int accountType = 0)
        {
            var home = new Common.House();
            home.Id = houseId;
            home.Name = houseName;
            home.IsOthreShare = isOthreShare;
            home.AccountType = accountType;
 
            //创建文件夹
            Common.Global.CreateHomeDirectory(houseId);
            home.Save();
            Common.Config.Instance.HomeFilePathList.Add(home.FileName);
            Common.Config.Instance.Save();
            //如果当前没有住宅的话
            if (Common.Config.Instance.Home.Id == string.Empty)
            {
                Common.Config.Instance.Home = this.GetHouseByFilePath(home.FileName);
                Common.Config.Instance.HomeId = home.Id;
            }
        }
 
 
        #endregion
 
        #region ■ 切换住宅___________________________
 
        /// <summary>
        /// 切换住宅(注:它只切换内存,界面并未处理)
        /// </summary>
        /// <param name="residenceId"></param>
        /// <returns></returns>
        public void SwitchResidence(string residenceId)
        {
            //打开进度条
            ProgressBar.Show();
 
            Config.Instance.HomeId = residenceId;
            //重新初始化住宅对象
            Config.Instance.Home = this.GetHouseByHouseId(residenceId);
            Config.Instance.Save();
            //刷新个人中心的内存及线程
            UserCenterLogic.InitUserCenterMenmoryAndThread(false);
 
            //关闭进度条
            ProgressBar.Close();
        }
 
        #endregion
 
        #region ■ 编辑住宅___________________________
 
        /// <summary>
        /// 编辑住宅
        /// </summary>
        /// <param name="residenceId">住宅id</param>
        /// <param name="residenceName">住宅名称</param>
        /// <returns></returns>
        public bool EditorResidenceName(string residenceId, string residenceName)
        {
            var Pra = new EditorResidencePra();
            Pra.HomeId = residenceId;
            Pra.Name = residenceName;
            Pra.IsOtherAccountCtrl = false;
            Pra.LoginAccessToken = Config.Instance.Token;
 
            //编辑住宅
            bool flage = UserCenterLogic.GetResultStatuByRequestHttps("App/EditHome", false, Pra);
            if (flage == true)
            {
                //刷新内存的住宅名
                this.EditorHouseByHouseId(residenceId, residenceName);
            }
            return flage;
        }
 
        /// <summary>
        /// 编辑缓存住宅
        /// </summary>
        /// <param name="residenceId">住宅id</param>
        /// <param name="residenceName">住宅名称</param>
        public void EditorHouseByHouseId(string residenceId, string residenceName)
        {
            if (Config.Instance.Home.Id == residenceId)
            {
                Config.Instance.Home.Name = residenceName;
            }
 
            var home = GetHouseByHouseId(residenceId);
            if (home == null)
            {
                return;
            }
            home.Name = residenceName;
            home.Save();
            //住宅修改名称的话,主页需要重新刷新
            UserView.UserPage.Instance.RefreshForm = true;
        }
 
 
        #endregion
 
        #region ■ 获取住宅___________________________
 
        /// <summary>
        /// 通过【id】获取住宅
        /// </summary>
        /// <returns>The house by house identifier.</returns>
        /// <param name="houseId">住宅id</param>
        public House GetHouseByHouseId(string houseId)
        {
            var path = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Config.Instance.Guid, houseId, $"House_{houseId}.json");
            var file = 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 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>
        /// 获取云端住宅列表
        /// </summary>
        public List<string> GetHomeListsFromDb()
        {
            if (HdlWifiLogic.Current.CanAccessHttp == false)
            {
                //当前无法联网
                return Config.Instance.HomeFilePathList;
            }
            var requestObj = new SendDataToServer.ResidenceListReqDto()
            {
                RequestVersion = CommonPage.RequestVersion,
                ReqDto = new SendDataToServer.ResidenceListObj()
                {
                    LoginAccessToken = Config.Instance.Token,
                    PageSetting = new SendDataToServer.ResidenceListPageSettingObj { PageSize = 999 }
                }
            };
            try
            {
                var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject(requestObj);
                var revertObj = 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)
                    {
                        //清空当前住宅列表
                        Config.Instance.HomeFilePathList.Clear();
                        var listHouse = new List<House>();
                        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(false);
                            listHouse.Add(house);
                        }
                        //如果切换了账号,或者原来的id不存在,则重置住宅ID
                        if (Config.Instance.TheSameLoginAccount == false ||
                            Config.Instance.HomeFilePathList.Find((obj) => obj == $"House_{Config.Instance.HomeId}.json") == null)
                        {
                            Config.Instance.HomeId = listHouse[0].Id;
                            foreach (var house in listHouse)
                            {
                                //初始选择它自己的住宅
                                if (house.IsOthreShare == false)
                                {
                                    Config.Instance.HomeId = house.Id;
                                    Config.Instance.Home = GetHouseByHouseId(house.Id);
                                    break;
                                }
                            }
                        }
                        Config.Instance.Save();
                    }
                }
            }
            catch (Exception ex)
            {
                HdlLogLogic.Current.WriteLog(ex);
            }
            return Config.Instance.HomeFilePathList;
        }
 
        #endregion
 
        #region ■ 获取本地住宅列表___________________
 
        /// <summary>
        /// 获取本地住宅列表
        /// </summary>
        /// <returns></returns>
        public List<Common.House> GetLocalResidenceList()
        {
            //如果是虚拟住宅,则从根目录中获取
            if (Common.Config.Instance.Home.IsVirtually == true)
            {
                //从文件夹中获取全部的住宅对象
                return this.GetAllLocalResidenceListByDirectory();
            }
 
            var listHome = new List<Common.House>();
            foreach (var housePath in Common.Config.Instance.HomeFilePathList)
            {
                var home = this.GetHouseByFilePath(housePath);
                if (home == null)
                {
                    continue;
                }
                listHome.Add(home);
            }
            return listHome;
        }
 
        /// <summary>
        /// 从文件夹中获取全部的住宅对象
        /// </summary>
        /// <returns></returns>
        public List<Common.House> GetAllLocalResidenceListByDirectory()
        {
            var strPath = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Common.Config.Instance.Guid);
            var listHome = new List<Common.House>();
            //获取全部的文件夹
            var listDirectory = new List<string>();
            var arryDirs = System.IO.Directory.GetDirectories(strPath);
            foreach (var file in arryDirs)
            {
                string[] arry = file.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
                listDirectory.Add(arry[arry.Length - 1]);
            }
            foreach (var myDir in listDirectory)
            {
                //获取各个文件夹里面的住宅文件
                string nowPath = System.IO.Path.Combine(strPath, myDir);
                var arryHouse = System.IO.Directory.GetFiles(nowPath, "House_*");
                if (arryHouse.Length > 0)
                {
                    //读取文件内容
                    var textValue = UserCenterLogic.LoadFileContent(System.IO.Path.Combine(nowPath, arryHouse[0]));
                    if (textValue == null)
                    {
                        continue;
                    }
                    var myHouse = Newtonsoft.Json.JsonConvert.DeserializeObject<Common.House>(textValue);
                    listHome.Add(myHouse);
                }
            }
            return listHome;
        }
 
        #endregion
 
        #region ■ 删除住宅___________________________
 
        /// <summary>
        /// 删除住宅
        /// </summary>
        /// <param name="filePath">File path.</param>
        public void DeleteHouseMemmory(string houseId)
        {
            string filePath = this.GetHouseFilePathByHouseId(houseId);
            var delPath = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Config.Instance.Guid, GetHouseIdByFilePath(filePath));
            if (System.IO.Directory.Exists(delPath) == false)
            {
                return;
            }
            var fileList = GetHouseFileListByHomeId(houseId);
            foreach (var file in fileList)
            {
                //删除文件
                System.IO.File.Delete(System.IO.Path.Combine(delPath, file));
            }
            //删除文件夹
            System.IO.Directory.Delete(delPath, true);
            //HomeFilePathList中删除该列表
            Common.Config.Instance.HomeFilePathList.RemoveAll((obj) => obj == filePath);
            Config.Instance.Save();
        }
 
        #endregion
 
        #region ■ 获取该住宅的文件列表_______________
 
        /// <summary>
        /// 通过【houseId】获取该住宅的文件列表
        /// </summary>
        /// <returns>The house file list by home identifier.</returns>
        /// <param name="houseId">House identifier.</param>
        public List<string> GetHouseFileListByHomeId(string houseId)
        {
            var list = new List<string> { };
            var path = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Config.Instance.Guid, houseId);
            if (!System.IO.Directory.Exists(path))
            {
                return new List<string> { };
            }
            var files = System.IO.Directory.GetFiles(path);
            foreach (var file in files)
            {
                var f = file.Substring(path.Length + 1);
                System.Console.WriteLine(f);
                list.Add(f);
            }
            return list;
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 获取楼层名称
        /// </summary>
        /// <param name="i_FloorId">楼层ID</param>
        /// <returns></returns>
        public string GetFloorNameById(string i_FloorId)
        {
            if (i_FloorId == null) { return string.Empty; }
            if (Config.Instance.Home.FloorDics.ContainsKey(i_FloorId) == false)
            {
                return string.Empty;
            }
            return Config.Instance.Home.FloorDics[i_FloorId];
        }
 
        /// <summary>
        /// 通过【文件路径】获取住宅id
        /// </summary>
        /// <returns>The house identifier by file path.</returns>
        /// <param name="filePath">文件路径</param>
        public string GetHouseIdByFilePath(string filePath)
        {
            string[] sArray = filePath.Split(new string[] { "House_", ".json" }, StringSplitOptions.RemoveEmptyEntries);
            if (sArray.Length >= 1)
            {
                return sArray[0];
            }
            return null;
        }
 
        /// <summary>
        /// 通过【住宅id】获取住宅路径
        /// </summary>
        /// <returns>The house identifier by file path.</returns>
        /// <param name="houseId">住宅id</param>
        public string GetHouseFilePathByHouseId(string houseId)
        {
            return $"House_{houseId}.json";
        }
 
        #endregion
 
        #region ■ 结构体_____________________________
 
        /// <summary>
        ///  添加住宅的启动参数
        /// </summary>
        private class AddResidencePra : IfacePraCommon
        {
            /// <summary>
            /// RequestVersion
            /// </summary>
            public string RequestVersion = Common.CommonPage.RequestVersion;
            /// <summary>
            /// LoginAccessToken
            /// </summary>
            public string LoginAccessToken = Common.Config.Instance.Token;
            /// <summary>
            /// Name
            /// </summary>
            public string Name = string.Empty;
        }
 
        /// <summary>
        /// 新住宅的信息
        /// </summary>
        private class NewResidenceInfo
        {
            /// <summary>
            /// ZigbeeHomeGuid
            /// </summary>
            public string Id = string.Empty;
        }
        #endregion
    }
}