From 9f326f4000847e6167d8166fa2f6a66f53cb3734 Mon Sep 17 00:00:00 2001 From: 黄学彪 <hxb@hdlchina.com.cn> Date: 星期四, 17 十二月 2020 09:07:13 +0800 Subject: [PATCH] 新云端Ver1.3 --- ZigbeeApp/Shared/Phone/Common/Logic/HdlResidenceLogic.cs | 908 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 908 insertions(+), 0 deletions(-) diff --git a/ZigbeeApp/Shared/Phone/Common/Logic/HdlResidenceLogic.cs b/ZigbeeApp/Shared/Phone/Common/Logic/HdlResidenceLogic.cs new file mode 100644 index 0000000..df92624 --- /dev/null +++ b/ZigbeeApp/Shared/Phone/Common/Logic/HdlResidenceLogic.cs @@ -0,0 +1,908 @@ +锘縰sing Shared.Common; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Shared.Phone +{ + /// <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浠h〃澶辫触) + /// </summary> + /// <param name="residenceName">浣忓畢鍚嶇О</param> + /// <param name="listUintName">鑷畾涔夊崟鍏冪殑鏍囬鍚嶇О(homeApp璁剧疆涓簄ull)</param> + /// <param name="listUintContent">鑷畾涔夊崟鍏冪殑鍐呭(homeApp璁剧疆涓簄ull)</param> + /// <param name="TemplateMode">1:鏃犳ā鏉挎湁璁惧 2:鏈夋ā鏉挎湁璁惧 3:鏃犳ā鏉挎棤璁惧</param> + public string CreatNewResidence(string residenceName, List<string> listUintName, + List<string> listUintContent, int TemplateMode = 1) + { + var pra = new { homeId = 0, homeName = residenceName, homeType = "ZIGBEE" }; + //娣诲姞浣忓畢 + var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/app/home/add", RestSharp.Method.POST, pra); + //妫�娴嬬姸鎬佺爜 + if (HdlCheckLogic.Current.CheckNetCode(result, ShowNetCodeMode.YES) == false) + { + return null; + } + var newInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<IdInfoClass>(result.Data.ToString()); + + if (listUintName != null) + { + //缂栬緫浣忓畢鍖哄煙 + var success = this.EditorResidenceArea(newInfo.Id, listUintName, listUintContent); + if (success == false) + { + //缂栬緫浣忓畢鍖哄煙澶辫触 + HdlMessageLogic.Current.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uEditorResidenceAreaFail)); + } + } + + //娣诲姞浣忓畢鍒扮紦瀛� + this.AddHouseToMemmory(newInfo.Id, residenceName, listUintName, + listUintContent, TemplateMode); + + return newInfo.Id; + } + + /// <summary> + /// 娣诲姞浣忓畢鍒扮紦瀛� + /// </summary> + /// <param name="houseId">浣忓畢id</param> + /// <param name="houseName">浣忓畢鍚嶇О.</param> + /// <param name="listUintName">鑷畾涔夊崟鍏冪殑鏍囬鍚嶇О(homeApp璁剧疆涓簄ull)</param> + /// <param name="listUintContent">鑷畾涔夊崟鍏冪殑鍐呭(homeApp璁剧疆涓簄ull)</param> + /// <param name="TemplateMode">1:鏃犳ā鏉挎湁璁惧 2:鏈夋ā鏉挎湁璁惧 3:鏃犳ā鏉挎棤璁惧</param> + /// <param name="isOthreShare">鏄惁涓哄叾浠栦富鐢ㄦ埛鍒嗕韩杩囨潵鐨勪綇瀹�</param> + /// <param name="accountType">浠呭瓙璐﹀彿鐧婚檰鐨勬椂鍊欎娇鐢�,褰撱�怚sOthreShare銆戜负"true"锛屽苟涓斻�怉ccountType銆戜负"1"鏃讹紝璇ヨ处鍙锋嫢鏈夌鐞嗗憳鏉冮檺</param> + public void AddHouseToMemmory(string houseId, string houseName, + List<string> listUintName, List<string> listUintContent, int TemplateMode = 1, + bool isOthreShare = false, int accountType = 0) + { + var home = new Common.House(); + home.Id = houseId; + home.Name = houseName; + home.IsOtherShare = isOthreShare; + home.AccountType = accountType; + + if (listUintName != null) + { + home.ListUintName.AddRange(listUintName); + home.ListUintContent.AddRange(listUintContent); + } + home.TemplateMode = TemplateMode; + if (TemplateMode == 3) + { + //鏍囪涓鸿櫄鎷熶綇瀹� + home.IsVirtually = true; + } + + //鍒涘缓鏂囦欢澶� + 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 bool SwitchResidence(string residenceId) + { + //鎵撳紑杩涘害鏉� + ProgressBar.Show(); + + Config.Instance.HomeId = residenceId; + //閲嶆柊鍒濆鍖栦綇瀹呭璞� + Config.Instance.Home = this.GetHouseByHouseId(residenceId); + Config.Instance.Save(); + //鍒锋柊涓汉涓績鐨勫唴瀛樺強绾跨▼ + bool result = HdlUserCenterLogic.Current.InitUserCenterMenmoryAndThread(false); + + //鍏抽棴杩涘害鏉� + ProgressBar.Close(); + + return result; + } + + #endregion + + #region 鈻� 缂栬緫浣忓畢___________________________ + + /// <summary> + /// 缂栬緫浣忓畢 + /// </summary> + /// <param name="residenceId">浣忓畢id</param> + /// <param name="residenceName">浣忓畢鍚嶇О</param> + /// <param name="listUintName">鑷畾涔夊崟鍏冪殑鏍囬鍚嶇О(homeApp璁剧疆涓簄ull)</param> + /// <param name="listUintContent">鑷畾涔夊崟鍏冪殑鍐呭(homeApp璁剧疆涓簄ull)</param> + /// <param name="TemplateMode">1:鏃犳ā鏉挎湁璁惧 2:鏈夋ā鏉挎湁璁惧 3:鏃犳ā鏉挎棤璁惧</param> + /// <returns></returns> + public bool EditorResidenceName(string residenceId, string residenceName, + List<string> listUintName, List<string> listUintContent, int TemplateMode = 1) + { + var pra = new { homeId = Config.Instance.Home.Id, homeType = "ZIGBEE", homeName = residenceName }; + var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/app/home/update", RestSharp.Method.POST, pra, null, null, CheckMode.A璐﹀彿鏉冮檺); + //妫�娴嬬姸鎬佺爜 + if (HdlCheckLogic.Current.CheckNetCode(result, ShowNetCodeMode.YES) == false) + { + return false; + } + if (listUintName != null) + { + //缂栬緫浣忓畢鍖哄煙 + var success = this.EditorResidenceArea(residenceId, listUintName, listUintContent); + if (success == false) + { + //缂栬緫浣忓畢鍖哄煙澶辫触 + HdlMessageLogic.Current.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uEditorResidenceAreaFail)); + } + } + //鍒锋柊鍐呭瓨鐨勪綇瀹呭悕 + this.EditorHouseByHouseId(residenceId, residenceName, listUintName, listUintContent, TemplateMode); + + return true; + } + + /// <summary> + /// 缂栬緫缂撳瓨浣忓畢 + /// </summary> + /// <param name="residenceId">浣忓畢id</param> + /// <param name="residenceName">浣忓畢鍚嶇О</param> + /// <param name="listUintName">鑷畾涔夊崟鍏冪殑鏍囬鍚嶇О(homeApp璁剧疆涓簄ull)</param> + /// <param name="listUintContent">鑷畾涔夊崟鍏冪殑鍐呭(homeApp璁剧疆涓簄ull)</param> + /// <param name="TemplateMode">1:鏃犳ā鏉挎湁璁惧 2:鏈夋ā鏉挎湁璁惧 3:鏃犳ā鏉挎棤璁惧</param> + public void EditorHouseByHouseId(string residenceId, string residenceName, + List<string> listUintName, List<string> listUintContent, int TemplateMode = 1) + { + House home = Config.Instance.Home; + if (Config.Instance.Home.Id != residenceId) + { + //浠庣紦瀛樹腑鑾峰彇 + home = GetHouseByHouseId(residenceId); + } + if (home == null) + { + return; + } + home.Name = residenceName; + if (listUintName != null) + { + home.ListUintName.Clear(); + home.ListUintName.AddRange(listUintName); + + home.ListUintContent.Clear(); + home.ListUintContent.AddRange(listUintContent); + } + home.TemplateMode = TemplateMode; + if (TemplateMode == 3) + { + //鏍囪涓鸿櫄鎷熶綇瀹� + home.IsVirtually = true; + } + home.Save(); + //浣忓畢淇敼鍚嶇О鐨勮瘽,涓婚〉闇�瑕侀噸鏂板埛鏂� + UserView.UserPage.Instance.RefreshAllForm = true; + } + + /// <summary> + /// 缂栬緫浣忓畢鍖哄煙 + /// </summary> + /// <param name="residenceId">浣忓畢id</param> + /// <param name="listUintName">鑷畾涔夊崟鍏冪殑鏍囬鍚嶇О</param> + /// <param name="listUintContent">鑷畾涔夊崟鍏冪殑鍐呭</param> + /// <returns></returns> + private bool EditorResidenceArea(string residenceId, List<string> listUintName, List<string> listUintContent) + { + //鈽嗐優銉笺偗鈽� 浣忓畢鍖哄煙 + return true; + } + + #endregion + + #region 鈻� 缂栬緫浣忓畢缁忕含搴____________________ + + /// <summary> + /// 缂栬緫浣忓畢缁忕含搴� + /// </summary> + /// <param name="i_latitude">绾害</param> + /// <param name="i_longitude">缁忓害</param> + /// <param name="i_addresName">浣嶇疆鍚嶇О</param> + /// <param name="mode">澶辫触鏃舵槸鍚︽樉绀簍ip娑堟伅</param> + /// <returns></returns> + public bool EditorResidenceLongitudeAndLatitude(double i_latitude, double i_longitude, string i_addresName, ShowNetCodeMode mode = ShowNetCodeMode.YES) + { + //濡傛灉涓嶆槸铏氭嫙浣忓畢,鎵嶆洿鏂颁簯绔� + if (Config.Instance.Home.IsVirtually == true) + { + //淇濆瓨缂撳瓨 + Config.Instance.Home.Longitude = i_longitude; + Config.Instance.Home.Latitude = i_latitude; + Config.Instance.Home.ResidenceAddressName = i_addresName; + Config.Instance.Home.Save(); + return true; + } + + var pra = new { homeId = Config.Instance.Home.Id, homeType = "ZIGBEE", latitude = i_latitude, longitude = i_longitude }; + var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/app/home/update", RestSharp.Method.POST, pra, null, null, CheckMode.A璐﹀彿鏉冮檺); + //妫�娴嬬姸鎬佺爜 + if (HdlCheckLogic.Current.CheckNetCode(result, mode) == false) + { + return false; + } + //淇濆瓨缂撳瓨 + Config.Instance.Home.Longitude = i_longitude; + Config.Instance.Home.Latitude = i_latitude; + Config.Instance.Home.ResidenceAddressName = i_addresName; + Config.Instance.Home.Save(); + + //璁剧疆鍏ㄩ儴缃戝叧鐨勪綇瀹呭湴鍧� + var listGateway = HdlGatewayLogic.Current.GetAllLocalGateway(); + foreach (var gateway in listGateway) + { + //涓嶅垽鏂槸鍚﹀け璐� + HdlGatewayLogic.Current.SetGatewaySite(gateway.GwId, i_longitude, i_latitude, ShowErrorMode.NO); + } + + return true; + } + + #endregion + + #region 鈻� 鑾峰彇浣忓畢___________________________ + + /// <summary> + /// 閫氳繃銆恑d銆戣幏鍙栦綇瀹� + /// </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 = HdlFileLogic.Current.ReadFileByteContent(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 = HdlFileLogic.Current.ReadFileByteContent(path); + if (file == null) + { + return null; + } + return Newtonsoft.Json.JsonConvert.DeserializeObject<House>(System.Text.Encoding.UTF8.GetString(file)); + } + + #endregion + + #region 鈻� 鑾峰彇浜戠浣忓畢鍒楄〃___________________ + + /// <summary> + /// 鐧婚檰鐨勬椂鍊�,鑾峰彇浜戠浣忓畢鍒楄〃(0:褰撳墠娌℃湁缃戠粶鐨勬儏鍐典笅鍔犺浇鏈湴浣忓畢 1:閫氳繃鑱旂綉鍔犺浇鍙婃牎楠屾湰鍦颁綇瀹�) + /// </summary> + /// <param name="mode">澶辫触鏃舵槸鍚︽樉绀簍ip娑堟伅</param> + /// <returns></returns> + public int GetHomeListsFromDbOnLogin(ShowNetCodeMode mode = ShowNetCodeMode.YES) + { + //鑾峰彇浜戠浣忓畢鏁版嵁 + var listHouseData = this.GetHomeListsFromDb(mode); + if (listHouseData == null) + { + return 0; + } + + //娓呯┖褰撳墠浣忓畢鍒楄〃 + Config.Instance.HomeFilePathList.Clear(); + var listHouse = new List<House>(); + foreach (var residence in listHouseData) + { + //鍒锋柊浣忓畢瀵硅薄鐨勭紦瀛� + var house = this.RefreshResidenceMemory(residence); + + 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涓嶅瓨鍦�,鍒欓噸缃綇瀹匢D + if (Config.Instance.TheSameLoginAccount == false || + Config.Instance.HomeFilePathList.Find((obj) => obj == $"House_{Config.Instance.HomeId}.json") == null) + { + Config.Instance.HomeId = listHouse.Count > 0 ? listHouse[0].Id : string.Empty; + foreach (var house in listHouse) + { + //鍒濆閫夋嫨瀹冭嚜宸辩殑浣忓畢 + if (house.IsOtherShare == false) + { + Config.Instance.HomeId = house.Id; + Config.Instance.Home = GetHouseByHouseId(house.Id); + break; + } + } + } + Config.Instance.Save(); + return 1; + } + + /// <summary> + /// 鑾峰彇浜戠浣忓畢鍒楄〃 + /// </summary> + /// <param name="mode">澶辫触鏃舵槸鍚︽樉绀簍ip娑堟伅</param> + /// <returns></returns> + public List<ResidenceInformation> GetHomeListsFromDb(ShowNetCodeMode mode = ShowNetCodeMode.YES) + { + var pra = new { homeType = "ZIGBEE" }; + + var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/app/home/list", RestSharp.Method.POST, pra); + //妫�娴嬬姸鎬佺爜 + if (HdlCheckLogic.Current.CheckNetCode(result, mode) == false) + { + return null; + } + var listTemp = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ResidenceInformation>>(result.Data.ToString()); + var listData = new List<ResidenceInformation>(); + foreach (var data in listTemp) + { + if (data.HomeType == "ZIGBEE") + { + listData.Add(data); + } + } + return this.SortHouse(listData); + } + + /// <summary> + /// 鍒锋柊浣忓畢瀵硅薄鐨勭紦瀛� + /// </summary> + /// <param name="i_residence">浜戠杩斿洖鐨勬暟鎹浆鎹㈡垚鐨勬暟鎹�</param> + public House RefreshResidenceMemory(ResidenceInformation i_residence) + { + string fileName = $"House_{i_residence.Id}.json"; + if (Config.Instance.HomeFilePathList.Contains(fileName) == false) + { + Config.Instance.HomeFilePathList.Add(fileName); + } + var house = this.GetHouseByHouseId(i_residence.Id); + if (house == null) + { + house = new House(); + } + house.Id = i_residence.Id; + house.Name = i_residence.HomeName; + house.IsOtherShare = i_residence.IsOtherShare; + house.AccountType = i_residence.AccountType == "ADMIN" ? 1 : 0; + house.Longitude = i_residence.Longitude; + house.Latitude = i_residence.Latitude; + house.IsRemoteControl = i_residence.IsRemoteControl; + house.LastEditorTime = HdlCommonLogic.Current.ConvertUtcTimeToLocalTime2(i_residence.ModifyTime).ToString("yyyy/MM/dd HH:mm"); + + Global.CreateHomeDirectory(i_residence.Id); + + house.Save(false); + + return house; + } + + /// <summary> + /// 浠庝簯绔�,鍒锋柊浣忓畢鐨勫尯鍩�(true:浼氬埛鏂癷_house鐨凩ist鍙橀噺, false:浠�涔堥兘涓嶅鐞�) + /// </summary> + /// <param name="i_homeId">浣忓畢ID</param> + public bool RefreshHomeAreaFromDb(string i_homeId) + { + var house = this.GetHouseByHouseId(i_homeId); + if (house == null) + { + return false; + } + //var pra2 = new + //{ + // RequestVersion = CommonPage.RequestVersion, + // RequestSource = 5, + // LoginAccessToken = Common.Config.Instance.Token, + // HomeId = i_house.Id + //}; + //var result = UserCenterLogic.GetResponseDataByRequestHttps("App/GetHomeDirectory", false, pra2, new List<string> { "NotCheck" }); + //if (result != null) + //{ + // var listData = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ResidenceAreaInfo>>(result); + // //鍒濆鍖� + // i_house.ListUintContent = new List<string>(); + // i_house.ListUintName = new List<string>(); + // foreach (var data in listData) + // { + // i_house.ListUintName.Add(data.Tag); + // i_house.ListUintContent.Add(data.Remark); + // } + // return true; + //} + return false; + } + + #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 this.SortHouse(listHome); + } + + /// <summary> + /// 浠庢枃浠跺す涓幏鍙栧叏閮ㄧ殑浣忓畢瀵硅薄 + /// </summary> + /// <param name="getGatewayId">鏄惁鑾峰彇缃戝叧ID(瀛樺湪listGatewayId閲岄潰)</param> + /// <returns></returns> + public List<House> GetAllLocalResidenceListByDirectory(bool getGatewayId = false) + { + 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 = HdlFileLogic.Current.ReadFileTextContent(System.IO.Path.Combine(nowPath, arryHouse[0])); + if (textValue == null) + { + continue; + } + var myHouse = Newtonsoft.Json.JsonConvert.DeserializeObject<Common.House>(textValue); + listHome.Add(myHouse); + //濡傛灉璇诲彇缃戝叧id鐨勮瘽 + if (getGatewayId == true) + { + //鏍囪瘑鍒濆鍖栧畬鎴� + myHouse.listGatewayId = new List<string>(); + //鑾峰彇缃戝叧鏂囦欢 + var arryGateway = System.IO.Directory.GetFiles(nowPath, "Gateway_*"); + foreach (var myFile in arryGateway) + { + string[] Arry = myFile.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries); + if (Arry.Length >= 3) + { + //鑾峰彇鍏剁綉鍏砳d + myHouse.listGatewayId.Add(Arry[2]); + } + } + } + } + } + return this.SortHouse(listHome); + } + + /// <summary> + /// 浠庢枃浠跺す涓幏鍙栧叏閮ㄥ浠戒綇瀹呭璞� + /// </summary> + /// <returns></returns> + public List<House> GetAllLocalBackupResidenceListByDirectory() + { + var strPath = System.IO.Path.Combine(IO.FileUtils.RootPath, Config.Instance.Guid); + var listHome = new List<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) + { + if (myDir.StartsWith("BackupResidenceData") == false + && myDir.StartsWith("DownLoadResidenceData") == false) + { + //鍙鎵嬪姩澶囦唤鍜屼簯绔笅杞界殑 + continue; + } + //鑾峰彇鍚勪釜鏂囦欢澶归噷闈㈢殑浣忓畢鏂囦欢 + 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<House>(textValue); + if (myHouse.Id != Config.Instance.Home.Id) + { + //涓嶆槸杩欎釜浣忓畢鐨勫浠� + continue; + } + if (myDir.StartsWith("BackupResidenceData") == true) + { + //姝や綇瀹呮暟鎹槸涓存椂澶囦唤鏁版嵁 + myHouse.HouseDataDiv = 2; + myHouse.SaveDirctoryName = myDir; + } + else if (myDir.StartsWith("DownLoadResidenceData") == true) + { + //姝や綇瀹呮暟鎹槸浠庝簯绔笅杞界殑澶囦唤鏁版嵁 + myHouse.HouseDataDiv = 3; + myHouse.SaveDirctoryName = myDir; + } + listHome.Add(myHouse); + } + } + return listHome; + } + + #endregion + + #region 鈻� 浣忓畢鎺掑簭___________________________ + + /// <summary> + /// 浣忓畢鎺掑簭 + /// </summary> + /// <param name="i_listHouse"></param> + /// <returns></returns> + public List<House> SortHouse(List<House> i_listHouse) + { + //浠庝竴鍫嗘枃瀛椾腑,鑾峰彇杩欎竴鍫嗘枃瀛楅噷闈㈡暟瀛楀瓧绗︿覆鐨勬渶闀块暱搴� + var listName = new List<string>(); + foreach (var house in i_listHouse) + { + listName.Add(house.Name); + } + int numberLength = HdlCommonLogic.Current.GetNumberMaxLength(listName); + + var listSort = new List<string[]>(); + var dicHouse = new Dictionary<string, House>(); + 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<House>(); + foreach (var strArry in listSort) + { + listSortHouse.Add(dicHouse[strArry[0]]); + } + return listSortHouse; + } + + /// <summary> + /// 浜戠浣忓畢鎺掑簭 + /// </summary> + /// <param name="i_listData"></param> + /// <returns></returns> + public List<ResidenceInformation> SortHouse(List<ResidenceInformation> i_listData) + { + //浠庝竴鍫嗘枃瀛椾腑,鑾峰彇杩欎竴鍫嗘枃瀛楅噷闈㈡暟瀛楀瓧绗︿覆鐨勬渶闀块暱搴� + var listName = new List<string>(); + foreach (var house in i_listData) + { + listName.Add(house.HomeName); + } + int numberLength = HdlCommonLogic.Current.GetNumberMaxLength(listName); + + var listSort = new List<string[]>(); + var dicHouse = new Dictionary<string, ResidenceInformation>(); + 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.HomeName) + { + 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<ResidenceInformation>(); + foreach (var strArry in listSort) + { + listSortHouse.Add(dicHouse[strArry[0]]); + } + return listSortHouse; + } + + #endregion + + #region 鈻� 鍒犻櫎浣忓畢___________________________ + + /// <summary> + /// 鍒犻櫎浜戠浣忓畢 + /// </summary> + /// <param name="houseId">鍒犻櫎鎸囧畾浣忓畢</param> + /// <returns></returns> + public bool DeleteCloundHouse(string houseId) + { + var pra = new { homeId = houseId }; + var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/app/home/delete", RestSharp.Method.POST, pra); + //妫�娴嬬姸鎬佺爜 + if (HdlCheckLogic.Current.CheckNetCode(result, ShowNetCodeMode.YES) == false) + { + return false; + } + return true; + } + + /// <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> + /// 閫氳繃銆恏ouseId銆戣幏鍙栬浣忓畢鐨勬枃浠跺垪琛� + /// </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> + /// 閫氳繃銆愭枃浠惰矾寰勩�戣幏鍙栦綇瀹卛d + /// </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> + /// 閫氳繃銆愪綇瀹卛d銆戣幏鍙栦綇瀹呰矾寰� + /// </summary> + /// <returns>The house identifier by file path.</returns> + /// <param name="houseId">浣忓畢id</param> + public string GetHouseFilePathByHouseId(string houseId) + { + return $"House_{houseId}.json"; + } + + /// <summary> + /// 鑾峰彇浣忓畢鐨勫尯鍩熸枃鏈� + /// </summary> + /// <param name="i_house"></param> + /// <returns></returns> + public string GetResidenceAreaText(House i_house) + { + return this.GetResidenceAreaText(i_house.ListUintName, i_house.ListUintContent); + } + + /// <summary> + /// 鑾峰彇浣忓畢鐨勫尯鍩熸枃鏈� + /// </summary> + /// <param name="i_ListUintName">鍗曞厓鏍囬</param> + /// <param name="i_ListUintContent">鍗曞厓鍐呭</param> + /// <returns></returns> + public string GetResidenceAreaText(List<string> i_ListUintName, List<string> i_ListUintContent) + { + string textArea = string.Empty; + for (int i = 0; i < i_ListUintName.Count; i++) + { + textArea += i_ListUintContent[i] + i_ListUintName[i] + "_"; + } + return textArea.TrimEnd('_'); + } + + #endregion + } +} -- Gitblit v1.8.0