using Shared.Common; using System; using System.Collections.Generic; using System.Text; namespace Shared.Phone.UserCenter { /// /// 住宅对象的逻辑 /// public class HdlResidenceLogic { #region ■ 变量声明___________________________ /// /// 住宅对象的逻辑 /// private static HdlResidenceLogic m_Current = null; /// /// 住宅对象的逻辑 /// public static HdlResidenceLogic Current { get { if (m_Current == null) { m_Current = new HdlResidenceLogic(); } return m_Current; } } #endregion #region ■ 创建新住宅_________________________ /// /// 创建新的住宅(返回住宅id,null代表失败) /// /// 住宅名称 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>(resultData); //添加住宅到缓存 this.AddHouseToMemmory(newInfo[0].Id, residenceName); return newInfo[0].Id; } /// /// 添加住宅到缓存 /// /// 住宅id /// 住宅名称. /// 是否为其他主用户分享过来的住宅 /// 仅子账号登陆的时候使用,当【IsOthreShare】为"true",并且【AccountType】为"1"时,该账号拥有管理员权限 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 ■ 切换住宅___________________________ /// /// 切换住宅(注:它只切换内存,界面并未处理) /// /// /// public bool SwitchResidence(string residenceId) { //打开进度条 ProgressBar.Show(); Config.Instance.HomeId = residenceId; //重新初始化住宅对象 Config.Instance.Home = this.GetHouseByHouseId(residenceId); Config.Instance.Save(); //刷新个人中心的内存及线程 bool result = UserCenterLogic.InitUserCenterMenmoryAndThread(false); //关闭进度条 ProgressBar.Close(); return result; } #endregion #region ■ 编辑住宅___________________________ /// /// 编辑住宅 /// /// 住宅id /// 住宅名称 /// 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; } /// /// 编辑缓存住宅 /// /// 住宅id /// 住宅名称 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.RefreshAllForm = true; } #endregion #region ■ 获取住宅___________________________ /// /// 通过【id】获取住宅 /// /// The house by house identifier. /// 住宅id public House GetHouseByHouseId(string houseId) { var path = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Config.Instance.Guid, houseId, $"House_{houseId}.json"); var file = HdlFileLogic.Current.ReadFileByteContent(path); if (file == null) { return null; } return Newtonsoft.Json.JsonConvert.DeserializeObject(System.Text.Encoding.UTF8.GetString(file)); } /// /// 通过【文件路径】获取住宅 /// /// The house by file path. /// 文件路径 public House GetHouseByFilePath(string filePath) { var path = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Config.Instance.Guid, GetHouseIdByFilePath(filePath), filePath); var file = HdlFileLogic.Current.ReadFileByteContent(path); if (file == null) { return null; } return Newtonsoft.Json.JsonConvert.DeserializeObject(System.Text.Encoding.UTF8.GetString(file)); } #endregion #region ■ 获取云端住宅列表___________________ /// /// 获取云端住宅列表 /// /// 是否检测网络,如果设置检测的话,当不能联网时,直接返回本地住宅 /// public List GetHomeListsFromDb(bool checkNetwork) { if (checkNetwork == true && 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), 5); if (revertObj == null) { return null; } if (revertObj.StateCode.ToUpper() == "SUCCESS") { var responseDataObj = Newtonsoft.Json.JsonConvert.DeserializeObject(revertObj.ResponseData.ToString()); if (responseDataObj.TotalCount > 0) { //清空当前住宅列表 Config.Instance.HomeFilePathList.Clear(); var listHouse = new List(); 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); } //检测本地的住宅文件是否合法 var listLocal = this.GetAllLocalResidenceListByDirectory(); foreach (var myHouse in listLocal) { //如果本地存在不属于他的住宅文件,则删除 if (myHouse.HouseDataDiv == 1 && Config.Instance.HomeFilePathList.Contains(myHouse.FileName) == false) { string housePath = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Config.Instance.Guid, myHouse.Id); HdlFileLogic.Current.DeleteDirectory(housePath); } } //如果切换了账号,或者原来的id不存在,则重置住宅ID if (Config.Instance.TheSameLoginAccount == false || Config.Instance.HomeFilePathList.Contains($"House_{Config.Instance.HomeId}.json") == false) { Config.Instance.HomeId = listHouse[0].Id; if (listHouse.Count > 0) { Config.Instance.HomeId = listHouse[0].Id; Config.Instance.Home = GetHouseByHouseId(listHouse[0].Id); } } Config.Instance.Save(); } } } catch (Exception ex) { HdlLogLogic.Current.WriteLog(ex); } return Config.Instance.HomeFilePathList; } #endregion #region ■ 获取本地住宅列表___________________ /// /// 获取本地住宅列表 /// /// public List GetLocalResidenceList() { //如果是虚拟住宅,则从根目录中获取 if (Common.Config.Instance.Home.IsVirtually == true) { //从文件夹中获取全部的住宅对象 return this.GetAllLocalResidenceListByDirectory(); } var listHome = new List(); foreach (var housePath in Common.Config.Instance.HomeFilePathList) { var home = this.GetHouseByFilePath(housePath); if (home == null) { continue; } listHome.Add(home); } return this.SortHouse(listHome); } /// /// 从文件夹中获取全部的住宅对象 /// /// public List GetAllLocalResidenceListByDirectory() { var strPath = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Common.Config.Instance.Guid); var listHome = new List(); //获取全部的文件夹 var listDirectory = new List(); var arryDirs = System.IO.Directory.GetDirectories(strPath); foreach (var file in arryDirs) { string[] arry = file.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries); listDirectory.Add(arry[arry.Length - 1]); } foreach (var myDir in listDirectory) { //获取各个文件夹里面的住宅文件 string nowPath = System.IO.Path.Combine(strPath, myDir); var arryHouse = System.IO.Directory.GetFiles(nowPath, "House_*"); if (arryHouse.Length > 0) { //读取文件内容 var textValue = HdlFileLogic.Current.ReadFileTextContent(System.IO.Path.Combine(nowPath, arryHouse[0])); if (textValue == null) { continue; } var myHouse = Newtonsoft.Json.JsonConvert.DeserializeObject(textValue); listHome.Add(myHouse); } } return this.SortHouse(listHome); } #endregion #region ■ 住宅排序___________________________ /// /// 住宅排序 /// /// /// public List SortHouse(List i_listHouse) { //从一堆文字中,获取这一堆文字里面数字字符串的最长长度 var listName = new List(); foreach (var house in i_listHouse) { listName.Add(house.Name); } int numberLength = this.GetNumberMaxLength(listName); var listSort = new List(); var dicHouse = new Dictionary(); foreach (var house in i_listHouse) { //临时缓存 dicHouse[house.Id] = house; var strArry = new string[2]; strArry[0] = house.Id; strArry[1] = string.Empty; string value = string.Empty; foreach (var c in house.Name) { if (char.IsNumber(c) == true) { //数字 value += c.ToString(); continue; } else if (value != string.Empty) { //如果房间名字带有数字的话,则左边加零,因为这里有个排序的问题 strArry[1] += value.PadLeft(numberLength, '0'); value = string.Empty; } strArry[1] += c.ToString(); } if (value != string.Empty) { //以数字结尾的话 strArry[1] += value.PadLeft(numberLength, '0'); } listSort.Add(strArry); } //排序 listSort.Sort((obj1, obj2) => { if (obj1[1].CompareTo(obj2[1]) > 0) { return 1; } return -1; }); var listSortHouse = new List(); foreach (var strArry in listSort) { listSortHouse.Add(dicHouse[strArry[0]]); } return listSortHouse; } /// /// 云端住宅排序 /// /// /// public List SortHouse(List i_listData) { //从一堆文字中,获取这一堆文字里面数字字符串的最长长度 var listName = new List(); foreach (var house in i_listData) { listName.Add(house.Name); } int numberLength = this.GetNumberMaxLength(listName); var listSort = new List(); var dicHouse = new Dictionary(); foreach (var house in i_listData) { //临时缓存 dicHouse[house.Id] = house; var strArry = new string[2]; strArry[0] = house.Id; strArry[1] = string.Empty; string value = string.Empty; foreach (var c in house.Name) { if (char.IsNumber(c) == true) { //数字 value += c.ToString(); continue; } else if (value != string.Empty) { //如果房间名字带有数字的话,则左边加零,因为这里有个排序的问题 strArry[1] += value.PadLeft(numberLength, '0'); value = string.Empty; } strArry[1] += c.ToString(); } if (value != string.Empty) { //以数字结尾的话 strArry[1] += value.PadLeft(numberLength, '0'); } listSort.Add(strArry); } //排序 listSort.Sort((obj1, obj2) => { if (obj1[1].CompareTo(obj2[1]) > 0) { return 1; } return -1; }); var listSortHouse = new List(); foreach (var strArry in listSort) { listSortHouse.Add(dicHouse[strArry[0]]); } return listSortHouse; } /// /// 从一堆文字中,获取这一堆文字里面数字字符串的最长长度 /// /// /// private int GetNumberMaxLength(List listText) { int maxLength = 0; foreach (var text in listText) { string value = string.Empty; foreach (var c in text) { if (char.IsNumber(c) == true) { //数字 value += c.ToString(); continue; } else if (value != string.Empty) { //判断数字长度 if (maxLength <= value.Length) { maxLength = value.Length; } value = string.Empty; } } //判断数字长度 if (maxLength <= value.Length) { maxLength = value.Length; } } return maxLength; } #endregion #region ■ 删除住宅___________________________ /// /// 删除住宅 /// /// File path. 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 ■ 获取该住宅的文件列表_______________ /// /// 通过【houseId】获取该住宅的文件列表 /// /// The house file list by home identifier. /// House identifier. public 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; } #endregion #region ■ 一般方法___________________________ /// /// 获取楼层名称 /// /// 楼层ID /// 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]; } /// /// 通过【文件路径】获取住宅id /// /// The house identifier by file path. /// 文件路径 public string GetHouseIdByFilePath(string filePath) { string[] sArray = filePath.Split(new string[] { "House_", ".json" }, StringSplitOptions.RemoveEmptyEntries); if (sArray.Length >= 1) { return sArray[0]; } return null; } /// /// 通过【住宅id】获取住宅路径 /// /// The house identifier by file path. /// 住宅id public string GetHouseFilePathByHouseId(string houseId) { return $"House_{houseId}.json"; } #endregion #region ■ 结构体_____________________________ /// /// 添加住宅的启动参数 /// private class AddResidencePra : IfacePraCommon { /// /// RequestVersion /// public string RequestVersion = Common.CommonPage.RequestVersion; /// /// LoginAccessToken /// public string LoginAccessToken = Common.Config.Instance.Token; /// /// Name /// public string Name = string.Empty; } /// /// 新住宅的信息 /// private class NewResidenceInfo { /// /// ZigbeeHomeGuid /// public string Id = string.Empty; } #endregion } }