using System; using System.Collections.Generic; using Shared.Phone.UserCenter; namespace Shared.Common { /// /// 住宅文件 /// [System.Serializable] public class House { #region ◆ 变量____________________________ /// /// 住宅文件 /// /// The file path. [Newtonsoft.Json.JsonIgnore] public string FileName { get { return $"House_{Id}.json"; } } /// /// 住宅id--使用云端提供的住宅唯一Id /// public string Id = string.Empty; /// /// 住宅名称 /// public string Name = string.Empty; /// /// 是否为其他主用户分享过来的住宅 /// public bool IsOthreShare; /// /// 当前住宅是其他主帐号分享过来的主帐号的分布式Id /// public string MainUserDistributedMark; /// /// 仅子账号登陆的时候使用,当【IsOthreShare】为"true",并且【AccountType】为"1"时,该账号拥有管理员权限 /// public int AccountType; /// /// 经度 /// public double Longitude = 0; /// /// 纬度 /// public double Latitude = 0; /// /// 房间路径列表 /// public List RoomFilePathList = new List { }; /// /// 楼层字典 /// key:FloorId /// value:FloorName /// public Dictionary FloorDics = new Dictionary { }; /// /// 当前楼层Id /// public string CurrentFloorId; /// /// 全局场景路径列表---备用 /// public List SceneFilePathList = new List { }; /// /// 设备路径列表---备用 /// public List DeviceFilePathList = new List { }; /// /// 功能路径列表---备用 /// public List FunctionFilePathList = new List { }; /// /// 通用标识--备用 /// public object Tag; /// /// 期、栋、层等区域---备用 /// public Dictionary LocationInfoList = new Dictionary { }; #endregion #region ◆ 住宅____________________________ #region ◆ 添加住宅_________________________ /// /// 添加住宅 /// /// 住宅id /// 住宅名称. /// 住宅数字型id public static void AddHouse(string houseId, string houseName, int numHomeId) { AddHouse(houseId, houseName, false, 0); } /// /// 添加住宅 /// /// 住宅id /// 住宅名称. /// 是否为其他主用户分享过来的住宅 /// 仅子账号登陆的时候使用,当【IsOthreShare】为"true",并且【AccountType】为"1"时,该账号拥有管理员权限 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 ◆ 删除住宅_________________________ /// /// 删除住宅 /// /// File path. 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(); } /// /// 删除住宅 /// /// House identifier. public static void DeleteHouseByHouseId(string houseId) { DeleteHouse(GetHouseFilePathByHouseId(houseId)); } #endregion #region ◆ 修改住宅_________________________ /// /// 修改住宅. /// /// House identifier. /// House name. 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 ◆ 获取住宅_________________________ /// /// 通过【id】获取住宅 /// /// The house by house identifier. /// 住宅id 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(System.Text.Encoding.UTF8.GetString(file)); } /// /// 通过【文件路径】获取住宅 /// /// The house by file path. /// 文件路径 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(System.Text.Encoding.UTF8.GetString(file)); } #endregion #region ◆ 获取住宅列表_____________________ /// /// Gets the home lists.获取住宅列表 /// public static async System.Threading.Tasks.Task> GetHomeLists() { 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(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.Confrim)); alert.Show(); alert.ResultEventHandler += (sender, e) => { if (e) { // } }; } else { //清空当前住宅列表 Config.Instance.HomeFilePathList.Clear(); 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(false); } //匹配当前住宅 if (Config.Instance.HomeFilePathList.Find((obj) => obj == $"House_{Config.Instance.HomeId}.json") == null) { Config.Instance.HomeId = GetHouseIdByFilePath(Config.Instance.HomeFilePathList[0]); } Config.Instance.Save(); } } } catch (Exception ex) { System.Console.WriteLine($"获取失败{ex.Message}"); } finally { } return Config.Instance.HomeFilePathList; } #endregion #region ◆ 获取住宅id_______________________ /// /// 通过【文件路径】获取住宅id /// /// The house identifier by file path. /// 文件路径 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 ◆ 获取住宅路径_____________________ /// /// 通过【住宅id】获取住宅路径 /// /// The house identifier by file path. /// 住宅id public static string GetHouseFilePathByHouseId(string houseId) { if (string.IsNullOrEmpty(houseId)) { return null; } return $"House_{houseId}.json"; } #endregion #region ◆ 获取该住宅的文件列表______________ /// /// 通过【houseId】获取该住宅的文件列表 /// /// The house file list by home identifier. /// House identifier. public static List GetHouseFileListByHomeId(string houseId) { var list = new List { }; var path = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Config.Instance.Guid, houseId); if (!System.IO.Directory.Exists(path)) { return new List { }; } 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; } /// /// 通过【文件路径】获取住宅下文件列表 /// /// The house file list by file path. /// File path. public static List GetHouseFileListByFilePath(string filePath) { return GetHouseFileListByHomeId(GetHouseIdByFilePath(filePath)); } #endregion #endregion #region ◆ 楼层____________________________ /// /// GetCurrentFloorName /// /// public string GetCurrentFloorName { get { return GetFloorNameById(CurrentFloorId); } } /// /// 获取楼层名称 /// /// /// 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; } /// /// InitFloor /// public void InitFloor() { if (Config.Instance.Home.FloorDics.Count > 0 && string.IsNullOrEmpty(CurrentFloorId)) { foreach (var floor in Config.Instance.Home.FloorDics) { CurrentFloorId = floor.Key; Save(false); return; } } } #endregion #region ◆ 房间____________________________ #region ◆ 添加房间路径_________________________ /// /// 添加【房间路径】到房间路径列表 /// /// true, if room list file path was added, false otherwise. /// Room file path. public bool AddRoomListFilePath(string roomFilePath) { if (RoomFilePathList.Contains(roomFilePath)) { return false; } RoomFilePathList.Add(roomFilePath); Save(); return true; } #endregion #region ◆ 删除房间路径_________________________ /// /// 移除房间路径 /// /// true, if room list file path was added, false otherwise. /// Room file path. public bool RemoveRoomListFilePath(string roomFilePath) { if (!RoomFilePathList.Contains(roomFilePath)) { return false; } RoomFilePathList.Remove(roomFilePath); Save(); return true; } #endregion #endregion #region ◆ 保存____________________________ /// /// 保存 /// /// 是否备份 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 } }