From c7df85937f73fb347ee0b19e9c052d2d00a6df6c Mon Sep 17 00:00:00 2001 From: 黄学彪 <hxb@hdlchina.com.cn> Date: 星期五, 18 九月 2020 13:58:19 +0800 Subject: [PATCH] 新版本 --- ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlGatewayLogic.cs | 1784 +++++++++++++++++++++++++---------------------------------- 1 files changed, 759 insertions(+), 1,025 deletions(-) diff --git a/ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlGatewayLogic.cs b/ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlGatewayLogic.cs index 7f97c6a..9631ac2 100755 --- a/ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlGatewayLogic.cs +++ b/ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlGatewayLogic.cs @@ -69,8 +69,7 @@ continue; } //娣诲姞缂撳瓨 - string gwID = HdlGatewayLogic.Current.GetGatewayId(gateway); - dicGateway[gwID] = gateway; + dicGateway[gateway.GwId] = gateway; } } } @@ -83,7 +82,7 @@ GatewayResourse.AppOldSelectGatewayId = string.Empty; //浠庢枃浠朵腑鑾峰彇涓婁竴娆¢�夋嫨鐨勭綉鍏砳d - byte[] data = Global.ReadFileByDirectory(DirNameResourse.LocalMemoryDirectory, DirNameResourse.AppOldSelectGatewayFile); + byte[] data = HdlFileLogic.Current.ReadFileByteContent(DirNameResourse.AppOldSelectGatewayFile); if (data != null) { string strvalue = System.Text.Encoding.UTF8.GetString(data); @@ -119,7 +118,7 @@ } List<string> listBackupGwId = new List<string>(); - var fileData = Global.ReadFileByDirectory(DirNameResourse.LocalMemoryDirectory, DirNameResourse.BackupGatewayIdFile); + var fileData = HdlFileLogic.Current.ReadFileByteContent(DirNameResourse.BackupGatewayIdFile); if (fileData != null) { //鏂板锛氳櫧鐒舵鐜囦綆锛屼絾鏄‘瀹炲彂鐢熶簡銆傚鏋滄湁缃戠粶鏃讹紝App閲嶆柊缁戝畾璁板綍鐨勭綉鍏冲け璐ョ殑璇� @@ -142,32 +141,18 @@ List<CommonDevice> list = Common.LocalDevice.Current.GetDeviceByGatewayID(gatewayId); foreach (var device in list) { - //鍒犻櫎璁惧锛屼笉鍒犻櫎鎴块棿淇℃伅 - Common.LocalDevice.Current.DeleteMemmoryDevice(device, false); + //鍒犻櫎涓�鑸澶� + Common.LocalDevice.Current.DeleteMemmoryDevice(device, true); + //鍒犻櫎Ota璁惧 + Common.LocalDevice.Current.DeleteMemmoryOtaDevice(device.DeviceAddr); } //鍒犻櫎缃戝叧鏂囦欢 this.DeleteGatewayFile(gatewayId); } - //LOG杈撳嚭 - if (listDelete.Count > 0) + //濡傛灉缃戝叧閮芥病浜嗙殑璇�,鎶婂満鏅叏閮ㄥ垹浜� + if (listDelete.Count > 0 && this.GetAllLocalGateway().Count == 0) { - string msg = "鏈湴鎷ユ湁鐨勭綉鍏筹細"; - foreach (var gatewayId in this.dicGateway.Keys) - { - msg += gatewayId + ","; - } - msg += "\r\n琚垹闄ょ殑缃戝叧锛�"; - foreach (var gatewayId in listDelete) - { - msg += gatewayId + ","; - } - msg += "\r\n姝ゆ椂浜戠杩斿洖褰撳墠璐﹀彿鎵�缁戝畾鏈夌殑缃戝叧锛�"; - foreach (var gatewayId in result.Keys) - { - msg += gatewayId + ","; - } - var bytes = System.Text.Encoding.UTF8.GetBytes(msg); - Common.Global.WriteFileByBytesByHomeId("GatewayDeleteLog.txt", bytes); + HdlSceneLogic.Current.DeleteAllLocalScene(); } } @@ -183,6 +168,20 @@ byte[] filebyte = Global.ReadFileByHomeId(file); string strvalue = System.Text.Encoding.UTF8.GetString(filebyte); var gateway = JsonConvert.DeserializeObject<ZbGateway>(strvalue); + if (gateway.GwId == string.Empty) + { + //杩欐槸鏃ф暟鎹�,闇�瑕佺壒娈婂鐞� + var jobject = Newtonsoft.Json.Linq.JObject.Parse(strvalue); + string gwInfo = jobject["getGwInfo"].ToString(); + + var result = JsonConvert.DeserializeObject<ZbGatewayData.GetGwData>(gwInfo); + //灏嗙綉鍏崇殑鏁版嵁璁剧疆鍒版湰鍦扮紦瀛樹腑 + this.SetGatewayDataToLocalMemmory(gateway, result); + } + if (gateway.HomeId == string.Empty) + { + gateway.HomeId = Config.Instance.Home.Id; + } return gateway; } catch (Exception ex) @@ -201,10 +200,16 @@ /// </summary> /// <param name="zbGateway">缃戝叧</param> /// <param name="mode">鏄惁鏄剧ず閿欒</param> - public async Task<bool> AddNewGateway(ZbGateway zbGateway, ShowErrorMode mode) - { + public bool AddNewGateway(ZbGateway zbGateway, ShowErrorMode mode) + { + //璁剧疆缃戝叧鐨勭粡绾害 + bool falge = this.SetGatewaySite(zbGateway, Common.Config.Instance.Home.Longitude, Common.Config.Instance.Home.Latitude, ShowErrorMode.NO); + if (falge == false) + { + return falge; + } //鎵ц娣诲姞缃戝叧鍒板唴瀛� - var result = await this.DoAddGatewayToMemory(zbGateway, mode); + var result = this.DoAddGatewayToMemory(zbGateway, mode); //鍓嶇殑缃戝叧缁戝畾鍦ㄤ簡褰撳墠璐﹀彿涓嬬殑涓嶅悓浣忓畢閲岄潰 if (result == 0) { @@ -224,9 +229,10 @@ { return false; } - //璁剧疆缃戝叧鐨勭粡绾害 - bool falge = this.SetGatewaySite(zbGateway, Common.Config.Instance.Home.Longitude, Common.Config.Instance.Home.Latitude); - return falge; + //娣诲姞缃戝叧鐨勮瘽,寮哄埗涓婚〉鍒锋柊 + UserView.UserPage.Instance.RefreshAllForm = true; + + return true; } /// <summary> @@ -236,8 +242,8 @@ public void AddVirtualGateway(string gatewayId) { var gateWay = new ZbGateway { IsVirtual = true }; - gateWay.getGatewayBaseInfo.gwID = gatewayId; - gateWay.getGatewayBaseInfo.HomeId = Shared.Common.Config.Instance.HomeId; + gateWay.GwId = gatewayId; + gateWay.HomeId = Shared.Common.Config.Instance.HomeId; gateWay.ReSave(); this.dicGateway[gatewayId] = gateWay; } @@ -248,7 +254,7 @@ /// <param name="zbGateway">缃戝叧瀵硅薄</param> /// <param name="mode">鏄惁鏄剧ず閿欒</param> /// <returns></returns> - private async Task<int> DoAddGatewayToMemory(ZbGateway zbGateway, ShowErrorMode mode) + private int DoAddGatewayToMemory(ZbGateway zbGateway, ShowErrorMode mode) { if (zbGateway == null) { @@ -261,18 +267,7 @@ return -1; } //鑾峰彇缃戝叧鐨勪俊鎭� - var result = await zbGateway.GetZbGwInfoAsync(); - //妫�娴嬬綉鍏宠繑鍥炵殑鍏遍�氶敊璇姸鎬佺爜 - string error = HdlCheckLogic.Current.CheckCommonErrorCode(result); - if (error != null) - { - if (mode == ShowErrorMode.YES) - { - this.ShowTipMsg(error); - } - return -1; - } - + var result = this.GetGatewayInfo(zbGateway, true, mode); if (result == null) { if (mode == ShowErrorMode.YES) @@ -284,32 +279,24 @@ return -1; } - if (result.getGwData == null) - { - if (mode == ShowErrorMode.YES) - { - //鑾峰彇缃戝叧淇℃伅澶辫触 - string msg = Language.StringByID(R.MyInternationalizationString.uGetGatewayInfoFail); - this.ShowTipMsg(msg); - } - return -1; - } - //璁剧疆浣忓畢ID鍒扮綉鍏� - bool flage2 = await this.SetHomeIdToGateway(zbGateway, Common.Config.Instance.HomeId); - if (flage2 == false) + if (result.HomeId != Common.Config.Instance.HomeId) { - if (mode == ShowErrorMode.YES) + bool flage2 = this.SetHomeIdToGateway(zbGateway, Common.Config.Instance.HomeId, mode); + if (flage2 == false) { - //鍚戠綉鍏宠缃綇瀹匢D澶辫触 - string msg = Language.StringByID(R.MyInternationalizationString.uSetHomeIdToGatewayFail); - this.ShowTipMsg(msg); + if (mode == ShowErrorMode.YES) + { + //鍚戠綉鍏宠缃綇瀹匢D澶辫触 + string msg = Language.StringByID(R.MyInternationalizationString.uSetHomeIdToGatewayFail); + this.ShowTipMsg(msg); + } + return -1; } - return -1; } //鏇存柊浜戠鏁版嵁搴� - int flage1 = await this.SetGatewayIdToDataBase(zbGateway); + int flage1 = this.SetGatewayIdToDataBase(zbGateway); //寮傚父涔熶笉楦熷畠,0鏄壒娈婂惈涔� if (flage1 == 0) { @@ -318,12 +305,12 @@ if (flage1 == -1) { //澶囦唤澶辫触鐨勭綉鍏矷D - HdlGatewayLogic.Current.BackupGatewayIdOnNotNetwork(zbGateway); + this.BackupGatewayIdOnNotNetwork(zbGateway); } //鏄惁宸茬粡瀛樺湪 - string gwID = HdlGatewayLogic.Current.GetGatewayId(zbGateway); - bool isEsist = HdlGatewayLogic.Current.IsGatewayExist(zbGateway); + string gwID = zbGateway.GwId; + bool isEsist = this.IsGatewayExist(zbGateway); if (isEsist == false) { //鏂板缓涓�涓櫄鎷熺殑缃戝叧鍑烘潵 @@ -334,11 +321,11 @@ //鍒锋柊鐨勬槸缂撳瓨,涓嶅埛鏂扮湡瀹炵墿鐞嗙綉鍏� this.dicGateway[gwID].GatewayOnlineFlage = zbGateway.GatewayOnlineFlage; - this.dicGateway[gwID].getGwInfo = result.getGwData; - this.dicGateway[gwID].getGatewayBaseInfo.GwName = result.getGwData.GwName; - this.dicGateway[gwID].getGatewayBaseInfo.IpAddress = result.getGwData.GwIP; - this.dicGateway[gwID].getGatewayBaseInfo.HomeId = Common.Config.Instance.HomeId; - this.dicGateway[gwID].ReSave(); + //灏嗙綉鍏崇殑鏁版嵁璁剧疆鍒版湰鍦扮紦瀛樹腑 + this.SetGatewayDataToLocalMemmory(this.dicGateway[gwID], result); + //椤轰究杩欎釜鍙橀噺涔熻缃竴涓� + this.SetGatewayDataToLocalMemmory(zbGateway, result, false); + if (isEsist == false) { //娣诲姞澶囦唤 @@ -354,14 +341,42 @@ /// <param name="zbGateway"></param> /// <param name="HomeId"></param> /// <returns></returns> - public async Task<bool> SetHomeIdToGateway(ZbGateway zbGateway, string HomeId) + public bool SetHomeIdToGateway(ZbGateway zbGateway, string HomeId, ShowErrorMode mode) { - var info = await zbGateway.GwSetHomeIdAsync(HomeId); - if (info != null && info.gwSetHomeIdData != null) + //璐﹀彿ID + string accountId = string.Empty; + if (HomeId != string.Empty) { - return true; + if (UserCenterResourse.UserInfo.AuthorityNo == 1) + { + //涓昏处鍙� + accountId = Config.Instance.Guid; + } + else + { + accountId = Config.Instance.Home.MainUserDistributedMark; + } } - return false; + var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 82 } }; + var data = new Newtonsoft.Json.Linq.JObject { { "HomeId", HomeId }, { "AccountId", accountId } }; + jObject.Add("Data", data); + //浣忓畢ID鐨勮缃�,鍥哄畾浣跨敤灞�鍩熺綉,涓嶅瓨鍦ㄨ繙绋嬬殑璇存硶 + var result = this.SendJobjectDataToGateway(zbGateway, "GwSetHomeId", jObject.ToString(), "GwSetHomeId_Respon", 5, true); + if (result.ErrorMsgDiv == -1) + { + if (mode == ShowErrorMode.YES) + { + //閿欒:缃戝叧瀵硅薄涓㈠け + string msg = Language.StringByID(R.MyInternationalizationString.uErrorGatewayLostMsg); + this.ShowTipMsg(msg); + } + return false; + } + if (result.ErrorMsgDiv == 0) + { + return false; + } + return true; } /// <summary> @@ -369,19 +384,16 @@ /// </summary> /// <param name="zbGateway">缃戝叧瀵硅薄</param> /// <returns></returns> - private async Task<int> SetGatewayIdToDataBase(ZbGateway zbGateway) + private int SetGatewayIdToDataBase(ZbGateway zbGateway) { //璋冪敤鎺ュ彛锛岀粦瀹氱綉鍏�(鍗充娇澶辫触锛屼篃杩斿洖true寰�涓嬭蛋) var bindGateway = new BindGatewayPra(); - string gwID = this.GetGatewayId(zbGateway); - bindGateway.BindGateways.Add(gwID); + bindGateway.BindGateways.Add(zbGateway.GwId); //鑾峰彇鎺у埗涓讳汉璐﹀彿鐨凾oken bindGateway.LoginAccessToken = UserCenterLogic.GetConnectMainToken(); - //涓嶆樉绀哄凡缁忚缁戝畾杩囩殑淇℃伅,NotSetAgain:鍋囧鏂綉鏃�,涓嶄簩娆″彂閫� - List<string> listNotShow = new List<string>() { "NotSetAgain" }; - var result = await UserCenterLogic.GetResultCodeByRequestHttps("App/BindGatewayToHome", true, bindGateway, listNotShow); - if (result == "Error" || result == "ErrorEx") + var result = UserCenterLogic.GetResultCodeByRequestHttps("App/BindGatewayToHome", true, bindGateway, new List<string> { "NotCheck" }, false); + if (result == "Error") { return -1; } @@ -400,7 +412,7 @@ /// <returns></returns> public bool HomeIdIsEmpty(ZbGateway zbGateway) { - return this.HomeIdIsEmpty(zbGateway.getGatewayBaseInfo.HomeId); + return this.HomeIdIsEmpty(zbGateway.HomeId); } /// <summary> @@ -425,7 +437,8 @@ /// 閲嶆柊缁戝畾缃戝叧(1:姝e父 -1:寮傚父 0:褰撳墠鐨勭綉鍏崇粦瀹氬湪浜嗗綋鍓嶈处鍙蜂笅鐨勪笉鍚屼綇瀹呴噷闈�) /// </summary> /// <param name="zbGateway">缃戝叧</param> - public async Task<int> ReBindNewGateway(ZbGateway zbGateway, NormalViewControl btnMsg = null) + /// <param name="btnMsg">娑堟伅鎺т欢</param> + public int ReBindNewGateway(ZbGateway zbGateway, NormalViewControl btnMsg = null) { if (zbGateway == null) { @@ -435,14 +448,14 @@ return -1; } //璁剧疆缃戝叧鐨勭粡绾害 - bool falge = this.SetGatewaySite(zbGateway, Common.Config.Instance.Home.Longitude, Common.Config.Instance.Home.Latitude); + bool falge = this.SetGatewaySite(zbGateway, Common.Config.Instance.Home.Longitude, Common.Config.Instance.Home.Latitude, ShowErrorMode.YES); if (falge == false) { return -1; } //璁剧疆浣忓畢ID鍒扮綉鍏� - bool flage2 = await this.SetHomeIdToGateway(zbGateway, Common.Config.Instance.HomeId); + bool flage2 = this.SetHomeIdToGateway(zbGateway, Common.Config.Instance.HomeId, ShowErrorMode.YES); if (flage2 == false) { //鍚戠綉鍏宠缃綇瀹匢D澶辫触 @@ -452,7 +465,7 @@ } //鏇存柊浜戠鏁版嵁搴� - int flage1 = await this.SetGatewayIdToDataBase(zbGateway); + int flage1 = this.SetGatewayIdToDataBase(zbGateway); //寮傚父涔熶笉楦熷畠,0鏄壒娈婂惈涔� if (flage1 == 0) { @@ -477,19 +490,23 @@ btnMsg.TextID = R.MyInternationalizationString.uGatewayDataIsChangingPleaseWhait; }); } - await Task.Delay(8000); + System.Threading.Thread.Sleep(8000); //鑾峰彇缃戝叧鐨勪俊鎭� - var result = await zbGateway.GetZbGwInfoAsync(); - //妫�娴嬬綉鍏宠繑鍥炵殑鍏遍�氶敊璇姸鎬佺爜 - string error = HdlCheckLogic.Current.CheckCommonErrorCode(result); - if (error != null) + ZbGatewayData.GetGwData result = null; + int count = 5; + while (count >= 0) { - this.ShowTipMsg(error); - return -1; + result = this.GetGatewayInfo(zbGateway, true, ShowErrorMode.NO); + if (result != null) + { + break; + } + count--; + //鏈�澶氬啀绛�20绉� + System.Threading.Thread.Sleep(4000); } - - if (result == null || result.getGwData == null) + if (result == null) { //鑾峰彇缃戝叧淇℃伅澶辫触 string msg = Language.StringByID(R.MyInternationalizationString.uGetGatewayInfoFail); @@ -498,7 +515,7 @@ } //鏄惁宸茬粡瀛樺湪 - string gwID = HdlGatewayLogic.Current.GetGatewayId(zbGateway); + string gwID = zbGateway.GwId; bool isEsist = HdlGatewayLogic.Current.IsGatewayExist(zbGateway); if (isEsist == false) { @@ -510,16 +527,20 @@ //鍒锋柊鐨勬槸缂撳瓨,涓嶅埛鏂扮湡瀹炵墿鐞嗙綉鍏� this.dicGateway[gwID].GatewayOnlineFlage = zbGateway.GatewayOnlineFlage; - this.dicGateway[gwID].getGwInfo = result.getGwData; - this.dicGateway[gwID].getGatewayBaseInfo.GwName = result.getGwData.GwName; - this.dicGateway[gwID].getGatewayBaseInfo.IpAddress = result.getGwData.GwIP; - this.dicGateway[gwID].getGatewayBaseInfo.HomeId = Common.Config.Instance.HomeId; - this.dicGateway[gwID].ReSave(); + //灏嗙綉鍏崇殑鏁版嵁璁剧疆鍒版湰鍦扮紦瀛樹腑 + this.SetGatewayDataToLocalMemmory(this.dicGateway[gwID], result); + //椤轰究杩欎釜鍙橀噺涔熻缃竴涓� + this.SetGatewayDataToLocalMemmory(zbGateway, result, false); + if (isEsist == false) { //娣诲姞澶囦唤 HdlAutoBackupLogic.AddOrEditorFile(this.dicGateway[gwID].FilePath); } + + //娣诲姞缃戝叧鐨勮瘽,寮哄埗涓婚〉鍒锋柊 + UserView.UserPage.Instance.RefreshAllForm = true; + return 1; } @@ -532,48 +553,36 @@ /// </summary> /// <param name="zbGateway">缃戝叧瀵硅薄</param> /// <param name="gatewayName">缃戝叧鍚�</param> - public async Task<bool> ReName(ZbGateway zbGateway, string gatewayName) + public bool ReName(ZbGateway zbGateway, string gatewayName) { - ZbGateway realWay = null; - if (this.GetRealGateway(ref realWay, zbGateway) == false) + //鍙彇32涓猙yte + var bytes = new byte[32]; + var reamarkGwBytes = System.Text.Encoding.UTF8.GetBytes(gatewayName); + System.Array.Copy(reamarkGwBytes, 0, bytes, 0, 32 < reamarkGwBytes.Length ? 32 : reamarkGwBytes.Length); + gatewayName = System.Text.Encoding.UTF8.GetString(bytes); + + var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 91 } }; + var data = new Newtonsoft.Json.Linq.JObject { { "GwName", gatewayName } }; + jObject.Add("Data", data); + var result = this.SendJobjectDataToGateway(zbGateway, "GwReName", jObject.ToString(), "GwReName_Respon"); + if (result.ErrorMsg != null) { - //鑾峰彇缃戝叧瀵硅薄澶辫触 - string msg = Language.StringByID(R.MyInternationalizationString.uGetGatewayTagartFail); - this.ShowErrorMsg(msg); + this.ShowTipMsg(result.ErrorMsg); return false; } - - var result = await realWay.GwReNameAsync(gatewayName); - //妫�娴嬬綉鍏宠繑鍥炵殑鍏遍�氶敊璇姸鎬佺爜 - string error = HdlCheckLogic.Current.CheckCommonErrorCode(result); - if (error != null) - { - this.ShowErrorMsg(error); - return false; - } - - if (result == null) + if (result.ErrorMsgDiv == 0) { //缃戝叧鍚嶇О淇敼澶辫触 string msg = Language.StringByID(R.MyInternationalizationString.uGatewayReNameFail); - - this.ShowErrorMsg(msg); - return false; - } - - //缃戝叧淇敼澶辫触 - if (result.gwReNameData == null) - { - //缃戝叧鍚嶇О淇敼澶辫触 - string msg = Language.StringByID(R.MyInternationalizationString.uGatewayReNameFail); - - this.ShowErrorMsg(msg); + //缃戝叧鍥炲瓒呮椂 + msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, null, "鍥炲瓒呮椂"); + this.ShowTipMsg(msg); return false; } //淇敼缂撳瓨 - string gwID = this.GetGatewayId(zbGateway); - this.dicGateway[gwID].getGatewayBaseInfo.GwName = gatewayName; + string gwID = zbGateway.GwId; + this.dicGateway[gwID].GwName = gatewayName; this.dicGateway[gwID].ReSave(); //娣诲姞鑷姩澶囦唤 @@ -590,20 +599,20 @@ /// 鎵ц鍒囨崲缃戝叧鎿嶄綔 /// </summary> /// <param name="gatewayId"></param> - public async Task<bool> DoSwitchGateway(string gatewayId) + public bool DoSwitchGateway(string gatewayId) { - var zbGateway = this.GetLocalGateway(gatewayId); - if (this.CheckGatewayOnlineByFlag(zbGateway) == true) + //閲嶆柊鑾峰彇鍦ㄧ嚎缃戝叧鐨勪俊鎭� + var result = this.GetOnlineGatewayInfo(gatewayId); + if (result == false) { - //閲嶆柊鑾峰彇鍦ㄧ嚎缃戝叧鐨勪俊鎭� - var result = await this.GetOnlineGatewayInfo(gatewayId); - if (result == false) - { - return false; - } + return false; } + //鍒囨崲缃戝叧,淇濆瓨缂撳瓨 this.SaveGatewayIdToLocation(gatewayId); + //鍒囨崲缃戝叧鐨勮瘽,涓婚〉闇�瑕侀噸鏂板埛鏂� + UserView.UserPage.Instance.RefreshAllForm = true; + return true; } @@ -612,7 +621,7 @@ /// </summary> /// <param name="gatewayId"></param> /// <returns></returns> - private async Task<bool> GetOnlineGatewayInfo(string gatewayId) + private bool GetOnlineGatewayInfo(string gatewayId) { //鏄剧ず杩涘害鏉� ProgressBar.Show(); @@ -623,9 +632,9 @@ if (this.GetRealGateway(ref realWay, gatewayId) == true) { //閲嶆柊璁剧疆浣忓畢ID(杩欎釜搴旇鏄笉缁忚繃APP,鐩存帴鎶婄綉鍏虫仮澶嶄簡鍑哄巶璁剧疆) - if (this.HomeIdIsEmpty(realWay.getGatewayBaseInfo.HomeId) == true) + if (this.HomeIdIsEmpty(realWay.HomeId) == true) { - int result2 = await this.ReBindNewGateway(realWay); + int result2 = this.ReBindNewGateway(realWay); if (result2 == 0) { //鍑虹幇鏈煡閿欒,璇风◢鍚庡啀璇� @@ -647,7 +656,7 @@ if (getGatewayInfo == true) { //鑾峰彇缃戝叧淇℃伅 - var info = await this.GetGatewayNewInfoAsync(realWay); + var info = this.GetGatewayInfo(realWay, false); if (info == null) { //鍏抽棴杩涘害鏉� @@ -655,12 +664,17 @@ return false; } } + //鍒锋柊鍏ㄩ儴鍦烘櫙 + if (this.IsMainGateway(realWay) == 1) + { + HdlSceneLogic.Current.RefreshSceneUIList(false); + } //鑾峰彇鍏ㄩ儴璁惧 - bool result = LocalDevice.Current.SetDeviceToMemmoryByGateway(realWay); + int result = LocalDevice.Current.SetDeviceToMemmoryByGateway(realWay, false); //鍏抽棴杩涘害鏉� ProgressBar.Close(); - if (result == false) + if (result == -1) { return false; } @@ -674,8 +688,7 @@ public void SaveGatewayIdToLocation(string gatewayId) { GatewayResourse.AppOldSelectGatewayId = gatewayId; - byte[] data = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(GatewayResourse.AppOldSelectGatewayId)); - Global.WriteFileToDirectoryByBytes(DirNameResourse.LocalMemoryDirectory, DirNameResourse.AppOldSelectGatewayFile, data); + HdlFileLogic.Current.SaveFileContent(DirNameResourse.AppOldSelectGatewayFile, GatewayResourse.AppOldSelectGatewayId); } #endregion @@ -685,47 +698,30 @@ /// <summary> /// 鍒犻櫎缃戝叧锛屽寘鎷簯绔拰鏈湴(澶辫触鏃朵笉浼氭樉绀轰俊鎭紝骞朵笖浼氳繑鍥瀟rue) /// </summary> - /// <param name="zbGateway"></param> - public async Task<bool> DeleteGateway(ZbGateway zbGateway) - { - string gwID = this.GetGatewayId(zbGateway); - //绉婚櫎鏈湴缃戝叧淇℃伅 - return await this.DeleteGateway(gwID); - } - - /// <summary> - /// 鍒犻櫎缃戝叧锛屽寘鎷簯绔拰鏈湴(澶辫触鏃朵笉浼氭樉绀轰俊鎭紝骞朵笖浼氳繑鍥瀟rue) - /// </summary> /// <param name="zbGatewayID"></param> - public async Task<bool> DeleteGateway(string zbGatewayID) + public bool DeleteGateway(string zbGatewayID) { ZbGateway realWay = null; - bool hadReal = this.GetRealGateway(ref realWay, zbGatewayID); - - //鏆備笉鏀寔鍒嗕韩 - if (hadReal == true && realWay.GatewayOnlineFlage == true) + this.GetRealGateway(ref realWay, zbGatewayID); + //娓呯┖缃戝叧鐨勪綇瀹匢D 缃戝叧瑙g粦澶辫触 涓嶇悊瀹�,鍥犱负缃戝叧鍙互鎸夋寜閿己鍒舵悳绱㈠緱鍒� + if (realWay != null) { - //娓呯┖缃戝叧鐨勪綇瀹匢D - bool result = await this.SetHomeIdToGateway(realWay, string.Empty); - if (result == false) - { - //缃戝叧瑙g粦澶辫触 涓嶇悊瀹�,鍥犱负缃戝叧鍙互鎸夋寜閿己鍒舵悳绱㈠緱鍒� - string msg = Language.StringByID(R.MyInternationalizationString.uGatewayUnBindFail); - //this.ShowErrorMsg(msg); - //return false; - } + this.SetHomeIdToGateway(realWay, string.Empty, ShowErrorMode.NO); } //鍒犻櫎浜戠鐨勭綉鍏� - await this.DeleteDataBaseGateway(zbGatewayID); + this.DeleteDataBaseGateway(zbGatewayID); //鍒犻櫎缃戝叧鏂囦欢 this.DeleteGatewayFile(zbGatewayID); //绉婚櫎 - ZbGateway.GateWayList.RemoveAll((obj) => this.GetGatewayId(obj) == zbGatewayID); + ZbGateway.GateWayList.RemoveAll((obj) => obj.GwId == zbGatewayID); //鏂紑mqtt杩炴帴 - realWay.DisConnectLocalMqttClient("GD"); + if (realWay != null) + { + realWay.DisConnectLocalMqttClient("GD"); + } return true; } @@ -758,18 +754,21 @@ { //鍒犻櫎璁惧鏂囦欢 Common.LocalDevice.Current.DeleteMemmoryDevice(device, true); + //鍒犻櫎Ota璁惧 + Common.LocalDevice.Current.DeleteMemmoryOtaDevice(device.DeviceAddr); } //濡傛灉鏄富缃戝叧 if (this.IsMainGateway(zbGatewayID) == 1) { - foreach (var room in Shared.Common.Room.Lists) + var listAllRoom = UserCenter.HdlRoomLogic.Current.GetAllListRooms(); + foreach (var room in listAllRoom) { //鍒犻櫎鍦烘櫙鏂囦欢 - foreach (var scene in room.SceneUIFilePathList) + foreach (var sceneId in room.ListSceneId) { - if (Global.IsExistsByHomeId(scene) == true) + if (Global.IsExistsByHomeId($"Scene_{sceneId}.json") == true) { - Global.DeleteFilebyHomeId(scene); + Global.DeleteFilebyHomeId($"Scene_{sceneId}.json"); } } } @@ -784,9 +783,8 @@ /// 鍒锋柊缃戝叧鐨勫湪绾跨姸鎬�(娉ㄦ剰,鍒锋柊鐨勬槸缂撳瓨,璇疯皟鐢–heckGatewayOnlineByFlag鏉ュ垽鏂槸鍚﹀湪绾�) /// </summary> /// <param name="listGateway"></param> - /// <param name="waitTime">灞�鍩熺綉鐨勬椂鍊�,鏃跺�欑瓑寰�3绉掑欢杩�</param> /// <returns></returns> - public void RefreshGatewayOnlineStatu(List<ZbGateway> listGateway, bool waitTime = true) + public void RefreshGatewayOnlineStatu(List<ZbGateway> listGateway) { var listRealWay = new List<ZbGateway>(); for (int i = 0; i < listGateway.Count; i++) @@ -801,41 +799,29 @@ { //铏氭嫙鐗╃悊缃戝叧 listRealWay.Add(listGateway[i]); - if (waitTime == false) - { - //涓嶇瓑寰呯殑璇�,鏍囪瘑鎸囧畾缃戝叧涓轰笉鍦ㄧ嚎 - this.SetGatewayNotOnLineFlag(listRealWay[i]); - } } - if (waitTime == true) - { - //鍙湁绛夊緟鐨勬椂鍊�,鎵嶆爣璇嗘寚瀹氱綉鍏充负涓嶅湪绾� - this.SetGatewayNotOnLineFlag(listRealWay[i]); - } + //鏍囪瘑鎸囧畾缃戝叧涓轰笉鍦ㄧ嚎 + listRealWay[i].GatewayOnlineFlage = false; } - if (waitTime == true) + //杩欐槸绗竴閬撳潕,寮哄埗妫�鏌IFI:绛夊緟2绉�(鍥犱负wifi鐨勬椂鍊欙紝瀹冧細鑷姩鍘诲埛鏂癴lage) + System.Threading.Thread.Sleep(2000); + //妫�鏌ユ槸鍚︽嫢鏈夌綉鍏冲瓨鍦ㄤ簬WIFi涓� + if (this.CheckHadGatewayInWifi(listRealWay) == false) { - //杩欐槸绗竴閬撳潕,寮哄埗妫�鏌IFI:绛夊緟2绉�(鍥犱负wifi鐨勬椂鍊欙紝瀹冧細鑷姩鍘诲埛鏂癴lage) - System.Threading.Thread.Sleep(2000); - //妫�鏌ユ槸鍚︽嫢鏈夌綉鍏冲瓨鍦ㄤ簬WIFi涓� - if (this.CheckHadGatewayInWifi(listRealWay) == false) - { - //绗簩閬撳潕锛氬湪杩滅▼鐨勬儏鍐典笅鍒锋柊缃戝叧鐨勫湪绾跨姸鎬� - this.RefreshGatewayOnlineOnRemode(listRealWay); - } + //绗簩閬撳潕锛氬湪杩滅▼鐨勬儏鍐典笅鍒锋柊缃戝叧鐨勫湪绾跨姸鎬� + this.RefreshGatewayOnlineOnRemode(listRealWay); } //鍒锋柊缂撳瓨鐨勫湪绾挎爣璇� foreach (var zbway in listRealWay) { - string gwID = this.GetGatewayId(zbway); + string gwID = zbway.GwId; if (this.dicGateway.ContainsKey(gwID) == false) { continue; } this.dicGateway[gwID].GatewayOnlineFlage = zbway.GatewayOnlineFlage; - this.dicGateway[gwID].ReSave(); } } @@ -872,31 +858,16 @@ } foreach (var way in listGateway) { - if (way == null || way.getGatewayBaseInfo == null) + if (way == null) { continue; } - string strId = this.GetGatewayId(way); - if (dicDbGateway.ContainsKey(strId) == true //濡傛灉浜戠涓婇潰鏈夎繖涓綉鍏� - && dicDbGateway[strId].MqttOnlineStatus == true //濡傛灉浜戠涓婇潰杩欎釜缃戝叧鏄湪绾跨殑 - ) + string strId = way.GwId; + if (dicDbGateway.ContainsKey(strId) == true) //濡傛灉浜戠涓婇潰鏈夎繖涓綉鍏� { - way.GatewayOnlineFlage = true; + way.GatewayOnlineFlage = dicDbGateway[strId].MqttOnlineStatus; } } - } - - /// <summary> - /// 鏍囪瘑鎸囧畾缃戝叧涓轰笉鍦ㄧ嚎 - /// </summary> - /// <param name="zbGateway"></param> - public void SetGatewayNotOnLineFlag(ZbGateway zbGateway) - { - if (zbGateway == null) - { - return; - } - zbGateway.GatewayOnlineFlage = false; } /// <summary> @@ -911,7 +882,7 @@ return false; } //浣跨敤缂撳瓨鐨勶紝鍥犱负鍒锋柊鍦ㄧ嚎鐘舵�佺殑鏃跺�欙紝鍒锋柊鐨勫氨鏄紦瀛橈紝鑰屼笉鏄湡瀹炵墿鐞嗙綉鍏� - string gwID = this.GetGatewayId(zbGateway); + string gwID = zbGateway.GwId; if (this.dicGateway.ContainsKey(gwID) == true) { return this.dicGateway[gwID].GatewayOnlineFlage; @@ -937,12 +908,12 @@ List<ZbGateway> newlist = new List<ZbGateway>(); foreach (var way in list) { - if (Config.Instance.HomeId != way.getGatewayBaseInfo.HomeId) + if (Config.Instance.HomeId != way.HomeId) { //濡傛灉涓嶆槸褰撳墠浣忓畢 continue; } - string gwID = this.GetGatewayId(way); + string gwID = way.GwId; if (this.dicGateway.ContainsKey(gwID) == false) { //濡傛灉鏈湴骞舵病鏈夎繖涓綉鍏� @@ -991,7 +962,7 @@ public List<string> GetAllGatewayFile() { List<string> list = new List<string>(); - List<string> listFile = Global.FileListByHomeId(); + List<string> listFile = HdlFileLogic.Current.GetRootPathListFile(); foreach (string file in listFile) { //鍙幏鍙栫綉鍏宠澶� @@ -1012,11 +983,11 @@ /// <returns></returns> public bool GetRealGateway(ref ZbGateway zbGateway, ZbGateway tagartWay) { - if (tagartWay == null || tagartWay.getGatewayBaseInfo == null) + if (tagartWay == null) { return false; } - return this.GetRealGateway(ref zbGateway, this.GetGatewayId(tagartWay)); + return this.GetRealGateway(ref zbGateway, tagartWay.GwId); } /// <summary> @@ -1029,14 +1000,14 @@ { var realWay = ZbGateway.GateWayList.Find((obj) => { - return this.GetGatewayId(obj) == gwId; + return obj.GwId == gwId; }); if (realWay == null) { //濡傛灉缃戝叧瀵硅薄涓㈠け浜�,鍒欏垱寤轰釜鏂扮殑 realWay = new ZbGateway { IsVirtual = true, }; - realWay.getGatewayBaseInfo.gwID = gwId; - realWay.getGatewayBaseInfo.HomeId = Shared.Common.Config.Instance.HomeId; + realWay.GwId = gwId; + realWay.HomeId = Shared.Common.Config.Instance.HomeId; ZbGateway.GateWayList.Add(realWay); } zbGateway = realWay; @@ -1307,77 +1278,21 @@ #endregion - #region 鈻� 鑾峰彇缃戝叧GwInfo閲岄潰鐨勫睘鎬__________ - - /// <summary> - /// 鑾峰彇缃戝叧GwInfo閲岄潰鐨勫睘鎬� - /// </summary> - /// <param name="zbGateway">缃戝叧瀵硅薄</param> - /// <param name="attributeName">getGwInfo閲岄潰灞炴�х殑鍚嶅瓧</param> - /// <param name="defult">濡傛灉鑾峰彇涓嶅埌鏃讹紝璁剧疆鐨勯粯璁ゅ��</param> - /// <returns></returns> - public object GetGwInfoAttribute(ZbGateway zbGateway, string attributeName, string defult = "") - { - string gwID = this.GetGatewayId(zbGateway); - var localWay = this.GetLocalGateway(gwID); - - object objValue = null; - if (localWay == null || localWay.getGwInfo == null) - { - //鏈湴娌℃湁璁板綍鏈夎繖涓笢瑗�,鍒欑洿鎺ヨ繑鍥炲弬鏁扮殑鏁版嵁 - if (zbGateway.getGwInfo != null) - { - objValue = zbGateway.getGwInfo.GetType().InvokeMember(attributeName, System.Reflection.BindingFlags.GetField, null, zbGateway.getGwInfo, null); - } - else - { - return defult; - } - } - else - { - //鑾峰彇鏈湴鐨勫睘鎬� - objValue = localWay.getGwInfo.GetType().InvokeMember(attributeName, System.Reflection.BindingFlags.GetField, null, localWay.getGwInfo, null); - } - - if (objValue == null) - { - return defult; - } - return objValue; - } - - #endregion - - #region 鈻� 鑾峰彇缃戝叧ID(鏈夌壒娈婃剰涔�)_____________ - - /// <summary> - /// 鑾峰彇缃戝叧瀵硅薄閲岄潰鐨勭綉鍏矷D(灏佽杩欎釜鏂规硶鏈夌壒娈婃剰涔�) - /// </summary> - /// <param name="zbGateway">缃戝叧瀵硅薄</param> - /// <returns></returns> - public string GetGatewayId(ZbGateway zbGateway) - { - //杩欎釜涓滆タ涓嶄細涓虹┖ - return zbGateway.getGatewayBaseInfo.gwID; - } - - #endregion - #region 鈻� 鑾峰彇鍚嶇О___________________________ /// <summary> /// 鑾峰彇缃戝叧鍔犵壒鏁堢殑鍚嶇О /// </summary> /// <param name="zbGateway"></param> - /// <param name="mode"></param> /// <returns></returns> - public string GetGatewayName(ZbGateway zbGateway, GetNameMode mode = GetNameMode.SpecialGateway) + public string GetGatewayName(ZbGateway zbGateway) { - string gwId = this.GetGatewayId(zbGateway); + if (zbGateway == null) { return string.Empty; } + + string gwId = zbGateway.GwId; if (this.dicGateway.ContainsKey(gwId) == false) { - return zbGateway.getGatewayBaseInfo.GwName == null ? string.Empty : zbGateway.getGatewayBaseInfo.GwName; + return zbGateway.GwName; } var localWay = this.dicGateway[gwId]; @@ -1387,14 +1302,10 @@ return name; } - if (mode == GetNameMode.SpecialGateway && localWay.getGwInfo != null) + var nameContent = LocalDevice.Current.GetDeviceModelIdNameInfo(localWay.LinuxImageType.ToString()); + if (nameContent != null) { - string keyName = Common.LocalDevice.deviceModelIdName + this.GetGwInfoAttribute(localWay, "LinuxImageType").ToString(); - if (Common.LocalDevice.Current.dicDeviceDefultNameID.ContainsKey(keyName) == true) - { - //娌℃湁鍚嶇О鏃讹紝鍒欎娇鐢≧鏂囦欢閲岄潰璁剧疆鐨勪笢瑗� - return Language.StringByID(Common.LocalDevice.Current.dicDeviceDefultNameID[keyName]); - } + return nameContent.A澶囨敞鍚嶅瓧; } return string.Empty; @@ -1411,7 +1322,7 @@ { return string.Empty; } - return zbGateway.getGatewayBaseInfo.GwName; + return zbGateway.GwName; } /// <summary> @@ -1425,35 +1336,35 @@ //鍒濆鍊�:鏃犳硶璇嗗埆鐨勭綉鍏宠澶� button.TextID = R.MyInternationalizationString.uUnDistinguishTheGatewayDevice; - string gwId = this.GetGatewayId(zbGateway); - if (this.dicGateway.ContainsKey(gwId) == false || this.dicGateway[gwId].getGwInfo == null) + string gwId = zbGateway.GwId; + if (this.dicGateway.ContainsKey(gwId) == false) { //濡傛灉杩欎釜缃戝叧娌℃湁淇℃伅锛屽垯浠庢柊鑾峰彇 - if (zbGateway.getGwInfo != null && zbGateway.getGwInfo.LinuxImageType != 0) + if (zbGateway.LinuxImageType != -1) { - string keyName = Common.LocalDevice.deviceModelIdName + zbGateway.getGwInfo.LinuxImageType; - if (Common.LocalDevice.Current.dicDeviceDefultNameID.ContainsKey(keyName) == true) + var nameContent = LocalDevice.Current.GetDeviceModelIdNameInfo(zbGateway.LinuxImageType.ToString()); + if (nameContent != null) { - //浣跨敤R鏂囦欢閲岄潰璁剧疆鐨勪笢瑗� - button.TextID = LocalDevice.Current.dicDeviceDefultNameID[keyName]; + button.Text = nameContent.A瀹樻柟鍚嶅瓧; } } else { //缁欎竴涓嚎绋嬪幓鑾峰彇瀹冪殑闀滃儚绫诲瀷 - HdlThreadLogic.Current.RunThread(async () => + HdlThreadLogic.Current.RunThread(() => { - var result = await this.GetGatewayNewInfoAsync(zbGateway, ShowErrorMode.NO); + var result = this.GetGatewayInfo(zbGateway, false, ShowErrorMode.NO); if (result != null) { - zbGateway.getGwInfo = result; + //灏嗙綉鍏崇殑鏁版嵁璁剧疆鍒版湰鍦扮紦瀛樹腑 + this.SetGatewayDataToLocalMemmory(zbGateway, result, false); + HdlThreadLogic.Current.RunMain(() => { - string keyName = Common.LocalDevice.deviceModelIdName + zbGateway.getGwInfo.LinuxImageType; - if (Common.LocalDevice.Current.dicDeviceDefultNameID.ContainsKey(keyName) == true) + var nameContent = LocalDevice.Current.GetDeviceModelIdNameInfo(zbGateway.LinuxImageType.ToString()); + if (nameContent != null) { - //浣跨敤R鏂囦欢閲岄潰璁剧疆鐨勪笢瑗� - button.TextID = LocalDevice.Current.dicDeviceDefultNameID[keyName]; + button.Text = nameContent.A瀹樻柟鍚嶅瓧; } }); } @@ -1462,11 +1373,10 @@ } else { - string keyName = Common.LocalDevice.deviceModelIdName + this.dicGateway[gwId].getGwInfo.LinuxImageType; - if (Common.LocalDevice.Current.dicDeviceDefultNameID.ContainsKey(keyName) == true) + var nameContent = LocalDevice.Current.GetDeviceModelIdNameInfo(zbGateway.LinuxImageType.ToString()); + if (nameContent != null) { - //浣跨敤R鏂囦欢閲岄潰璁剧疆鐨勪笢瑗� - button.TextID = Common.LocalDevice.Current.dicDeviceDefultNameID[keyName]; + button.Text = nameContent.A瀹樻柟鍚嶅瓧; } } } @@ -1479,34 +1389,16 @@ /// 鑾峰彇缃戝叧淇℃伅(鐗堟湰淇℃伅,闀滃儚绫诲瀷,鍩烘湰淇℃伅绛夈�傚彧鍒锋柊鏈湴缃戝叧鐨勭紦瀛�) /// </summary> /// <param name="zbGateway"></param> + /// <param name="useLocalConnect">鏄惁寮哄埗浣跨敤鏈湴杩炴帴鍙戦��</param> /// <param name="mode"></param> /// <returns></returns> - public async Task<ZbGatewayData.GetGwData> GetGatewayNewInfoAsync(ZbGateway zbGateway, ShowErrorMode mode = ShowErrorMode.YES) + public ZbGatewayData.GetGwData GetGatewayInfo(ZbGateway zbGateway, bool useLocalConnect, ShowErrorMode mode = ShowErrorMode.YES) { - ZbGateway realWay = null; - if (this.GetRealGateway(ref realWay, zbGateway) == false) - { - if (mode == ShowErrorMode.YES) - { - string msg = Language.StringByID(R.MyInternationalizationString.uErrorGatewayLostMsg); - this.ShowTipMsg(msg); - } - return null; - } //鑾峰彇缃戝叧鐗堟湰淇℃伅 - var imageTypeResult = await realWay.GetZbGwInfoAsync(); - //妫�娴嬬綉鍏宠繑鍥炵殑鍏遍�氶敊璇姸鎬佺爜 - string error = HdlCheckLogic.Current.CheckCommonErrorCode(imageTypeResult); - if (error != null) - { - if (mode == ShowErrorMode.YES) - { - this.ShowTipMsg(error); - } - return null; - } + var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 95 } }; + var result = this.SendJobjectDataToGateway(zbGateway, "GetZbGwInfo", jObject.ToString(), "GetZbGwInfo_Respon", 5, useLocalConnect); - if (imageTypeResult == null || imageTypeResult.getGwData == null) + if (result.ErrorMsgDiv != 1) { if (mode == ShowErrorMode.YES) { @@ -1518,65 +1410,129 @@ } return null; } - - string gwID = this.GetGatewayId(zbGateway); + var getGwInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<ZbGatewayData.GetGwData>(result.ReceiptData); + string gwID = zbGateway.GwId; if (this.dicGateway.ContainsKey(gwID) == true) { //鍒锋柊缂撳瓨 ZbGateway localWay = this.dicGateway[gwID]; - localWay.getGwInfo = imageTypeResult.getGwData; - localWay.getGatewayBaseInfo.GwName = imageTypeResult.getGwData.GwName; - localWay.getGatewayBaseInfo.IpAddress = imageTypeResult.getGwData.GwIP; - - localWay.ReSave(); + //灏嗙綉鍏崇殑鏁版嵁璁剧疆鍒版湰鍦扮紦瀛樹腑 + this.SetGatewayDataToLocalMemmory(localWay, getGwInfo); } + //椤轰究杩欎釜鍙橀噺涔熻缃竴涓� + this.SetGatewayDataToLocalMemmory(zbGateway, getGwInfo, false); - return imageTypeResult.getGwData; + return getGwInfo; } /// <summary> - /// 鑾峰彇缃戝叧淇℃伅,闈炲紓姝�,浼氱瓑寰�(鐗堟湰淇℃伅,闀滃儚绫诲瀷,鍩烘湰淇℃伅绛夈�傚彧鍒锋柊鏈湴缃戝叧鐨勭紦瀛�) + /// 灏嗙綉鍏崇殑鏁版嵁璁剧疆鍒版湰鍦扮紦瀛樹腑 /// </summary> - /// <param name="zbGateway"></param> - /// <param name="mode"></param> - /// <returns></returns> - public ZbGatewayData.GetGwData GetGatewayNewInfo(ZbGateway zbGateway, ShowErrorMode mode = ShowErrorMode.YES) + /// <param name="localWay">鏈湴缃戝叧</param> + /// <param name="data">缃戝叧鏁版嵁</param> + /// <param name="saveFile">鏄惁淇濆瓨鏂囦欢</param> + private void SetGatewayDataToLocalMemmory(ZbGateway localWay, ZbGatewayData.GetGwData data, bool saveFile = true) { - bool bolBreak = false; - ZbGatewayData.GetGwData result = null; - HdlThreadLogic.Current.RunThread(async () => + localWay.GwId = data.GwId; + localWay.GwName = data.GwName; + localWay.GwSerialNum = data.GWSN; + localWay.IsMainGateWay = data.IsDominant == 1 ? true : false; + localWay.GwIP = data.GwIP; + localWay.LinuxImageType = data.LinuxImageType; + localWay.LinuxHardVersion = data.LinuxHWVersion; + localWay.LinuxFirmwareVersion = data.LinuxFWVersion; + localWay.CoordinatorHardVersion = data.ZbHWVersion; + localWay.CoordinatorFirmwareVersion = data.ZbFWVersion; + localWay.CoordinatorImageId = data.ZbImageType; + localWay.DriveCodeList = data.DriveCodeList; + localWay.GwVersionDate = data.AppVersion; + if (saveFile == true) { - result = await this.GetGatewayNewInfoAsync(zbGateway, mode); - bolBreak = true; - }); - - while (bolBreak == false) - { - System.Threading.Thread.Sleep(500); + localWay.ReSave(); } - return result; } #endregion - #region 鈻� 娓呯┖鐪熷疄缃戝叧鍒楄〃___________________ + #region 鈻� 缃戝叧鎴块棿鐩稿叧_______________________ /// <summary> - /// 娓呯┖鍏ㄩ儴鐨勭湡瀹炵墿鐞嗙綉鍏冲璞� + /// 鑾峰彇缃戝叧鎵�鍦ㄧ殑鎴块棿 /// </summary> - public void ClearAllRealGateway() + /// <param name="zbGateway">缃戝叧瀵硅薄</param> + /// <returns></returns> + public Room GetRoomByGateway(ZbGateway zbGateway) { - //鍥犱负閭d竴鐬棿锛屾湁鍙兘mqtt浼氬姞鍥炴潵,鎵�浠ュ厛鍔犵紦瀛� - var list = new List<ZbGateway>(); - list.AddRange(ZbGateway.GateWayList); - //鐒跺悗娓呯┖鎺� - ZbGateway.GateWayList.Clear(); - //鏈�鍚庡啀鏂紑mqtt杩炴帴 - for (int i = 0; i < list.Count; i++) + return this.GetRoomByGateway(zbGateway.GwId); + } + + /// <summary> + /// 鑾峰彇缃戝叧鎵�鍦ㄧ殑鎴块棿 + /// </summary> + /// <param name="gatewayId">缃戝叧ID</param> + /// <returns></returns> + public Room GetRoomByGateway(string gatewayId) + { + var localGateway = this.GetLocalGateway(gatewayId); + if (localGateway == null) { - list[i].DisConnectLocalMqttClient("G"); + return null; } - list.Clear(); + return HdlRoomLogic.Current.GetRoomById(localGateway.RoomId); + } + + /// <summary> + /// 鍙樻洿缃戝叧鎴块棿 + /// </summary> + /// <param name="gwId">缃戝叧Id</param> + /// <param name="roomId">鎴块棿ID</param> + public void ChangedGatewayRoom(string gwId, string roomId) + { + var localGateway = this.GetLocalGateway(gwId); + if (localGateway != null) + { + localGateway.RoomId = roomId; + localGateway.ReSave(); + //娣诲姞澶囦唤 + HdlAutoBackupLogic.AddOrEditorFile(localGateway.FilePath); + } + } + + #endregion + + #region 鈻� 娓呯┖鐪熷疄缃戝叧閾炬帴___________________ + + /// <summary> + /// 娓呯┖鍏ㄩ儴鐨勭湡瀹炵墿鐞嗙綉鍏崇殑閾炬帴 + /// </summary> + /// <param name="roadGateway">鏄惁鍔犺浇鏈湴鐨勭綉鍏冲璞″埌鐪熷疄鍒楄〃涓�</param> + public void ClearAllRealGatewayConection(bool roadGateway) + { + //鏂紑mqtt杩炴帴(鍗充娇淇濆瓨鍦ㄥ唴瀛樺綋涓篃娌¢棶棰�,鍥犱负濡傛灉骞挎挱涓嶅埌,鍒欏畠涓嶄細寤虹珛閾炬帴) + for (int i = 0; i < ZbGateway.GateWayList.Count; i++) + { + ZbGateway.GateWayList[i].DisConnectLocalMqttClient("G"); + } + //鍔犺浇鏈湴缃戝叧瀵硅薄 + if (roadGateway == true) + { + List<string> listFile = this.GetAllGatewayFile(); + //鍙嶅簭鍒楀寲娣诲姞鍒扮紦瀛� + foreach (string file in listFile) + { + //浠庢枃浠朵腑鍙嶅簭鍒楀寲鍑虹綉鍏冲璞� + var gateway = this.GetGatewayFromFile(file); + if (gateway == null) + { + continue; + } + var tempWay = ZbGateway.GateWayList.Find(obj => (obj != null) && (obj.GwId == gateway.GwId)); + if (tempWay == null) + { + ZbGateway.GateWayList.Add(gateway); + } + } + } } #endregion @@ -1589,75 +1545,88 @@ /// <param name="zbGateway">缃戝叧瀵硅薄</param> /// <param name="mode">閿欒鏃讹紝鏄惁鏄剧ず閿欒</param> /// <returns></returns> - public async Task<List<FirmwareVersionInfo>> GetGatewayAllNewVersion(ZbGateway zbGateway, ShowErrorMode mode = ShowErrorMode.YES) + public List<FirmwareVersionInfo> GetGatewayAllNewVersion(ZbGateway zbGateway, ShowErrorMode mode = ShowErrorMode.YES) { + //濡傛灉鏄睍绀烘ā鏉�,鎴栬�呮槸铏氭嫙浣忓畢鐨勮瘽,娌℃湁浠�涔堟柊鐗堟湰鐨勮娉� + if (Common.Config.Instance.Home.IsVirtually == true || Common.Config.Instance.Home.IsShowTemplate == true) + { + return new List<FirmwareVersionInfo> { null, null, null }; + } //鑾峰彇缃戝叧鐗堟湰淇℃伅 - var result = await HdlGatewayLogic.Current.GetGatewayNewInfoAsync(zbGateway, mode); + var result = this.GetGatewayInfo(zbGateway, false, mode); if (result == null) { return null; } //浣跨敤鏈湴缂撳瓨瀵硅薄 - string gwID = this.GetGatewayId(zbGateway); - var localWay = this.GetLocalGateway(gwID); + var localWay = this.GetLocalGateway(zbGateway.GwId); if (localWay == null) { return null; } //娣诲姞缃戝叧鐨勫崌绾у浐浠�(鎴愪笉鎴愬姛閮芥棤鎵�璋�) - var flage = await HdlFirmwareUpdateLogic.AddFirmwareVersionInfo(FirmwareLevelType.Linux, - this.GetGwInfoAttribute(localWay, "LinuxHWVersion").ToString(), - this.GetGwInfoAttribute(localWay, "LinuxImageType").ToString()); + var flage = HdlFirmwareUpdateLogic.AddFirmwareVersionInfo(FirmwareLevelType.Linux, + localWay.LinuxHardVersion.ToString(), + localWay.LinuxImageType.ToString()); - //娣诲姞鍗忚皟鍣ㄧ殑鍗囩骇鍥轰欢(鎴愪笉鎴愬姛閮芥棤鎵�璋�) - flage = await HdlFirmwareUpdateLogic.AddFirmwareVersionInfo(FirmwareLevelType.Coordinator, - this.GetGwInfoAttribute(localWay, "ZbHWVersion").ToString(), - this.GetGwInfoAttribute(localWay, "ZbImageType").ToString()); + //娣诲姞鍗忚皟鍣ㄧ殑鍗囩骇鍥轰欢(鎴愪笉鎴愬姛閮芥棤鎵�璋�) 蹇呴』鑳藉鑱旂綉鎵嶈 + if (flage == 1) + { + //娌$綉鐨勬椂鍊欎笉鍐嶅鐞� + HdlFirmwareUpdateLogic.AddFirmwareVersionInfo(FirmwareLevelType.A鍗忚皟鍣�, + localWay.CoordinatorHardVersion.ToString(), + localWay.CoordinatorImageId.ToString()); + } //缃戝叧鐨勭増鏈� var gatewayFirmware = HdlFirmwareUpdateLogic.GetFirmwareMostVersionInfo(FirmwareLevelType.Linux, - this.GetGwInfoAttribute(localWay, "LinuxHWVersion").ToString(), - this.GetGwInfoAttribute(localWay, "LinuxImageType").ToString(), - Convert.ToInt32(this.GetGwInfoAttribute(localWay, "LinuxFWVersion"))); + localWay.LinuxHardVersion.ToString(), + localWay.LinuxImageType.ToString(), + localWay.LinuxFirmwareVersion); //鍗忚皟鍣ㄧ増鏈� - var coordinatorFirmware = HdlFirmwareUpdateLogic.GetFirmwareMostVersionInfo(FirmwareLevelType.Coordinator, - this.GetGwInfoAttribute(localWay, "ZbHWVersion").ToString(), - this.GetGwInfoAttribute(localWay, "ZbImageType").ToString(), - Convert.ToInt32(this.GetGwInfoAttribute(localWay, "ZbFWVersion"))); + var coordinatorFirmware = HdlFirmwareUpdateLogic.GetFirmwareMostVersionInfo(FirmwareLevelType.A鍗忚皟鍣�, + localWay.CoordinatorHardVersion.ToString(), + localWay.CoordinatorImageId.ToString(), + localWay.CoordinatorFirmwareVersion); - //杩欎釜缃戝叧闇�瑕佹湁铏氭嫙椹卞姩杩欎釜涓滆タ鎵嶈 - FirmwareVersionInfo virtualFirmware = null; - string linImage = this.GetGwInfoAttribute(localWay, "LinuxImageType").ToString(); - if (linImage != "6") - { - //铏氭嫙椹卞姩鍙� - var listvVode = await HdlGatewayLogic.Current.GetListVDDriveCode(localWay); - if (listvVode != null && listvVode.Count > 0) - { - //娣诲姞铏氭嫙椹卞姩鐨勫崌绾у浐浠�(鎴愪笉鎴愬姛閮芥棤鎵�璋�) - flage = await HdlFirmwareUpdateLogic.AddFirmwareVersionInfo(FirmwareLevelType.VirtualDevice, - listvVode[0].DriveHwVersion.ToString(), - listvVode[0].DriveImageType.ToString()); - - //铏氭嫙椹卞姩 - virtualFirmware = HdlFirmwareUpdateLogic.GetFirmwareMostVersionInfo(FirmwareLevelType.VirtualDevice, - listvVode[0].DriveHwVersion.ToString(), - listvVode[0].DriveImageType.ToString(), - listvVode[0].DriveFwVersion); - - if (virtualFirmware != null) - { - virtualFirmware.VirtualCode = listvVode[0].DriveCode; - } - } - } var list = new List<FirmwareVersionInfo>(); list.Add(gatewayFirmware); list.Add(coordinatorFirmware); - list.Add(virtualFirmware); + //杩欎釜缃戝叧闇�瑕佹湁铏氭嫙椹卞姩杩欎釜涓滆タ鎵嶈 + if (localWay.LinuxImageType != 6) + { + //铏氭嫙椹卞姩鍙� + foreach (var data in localWay.DriveCodeList) + { + //娣诲姞铏氭嫙椹卞姩鐨勫崌绾у浐浠�(鎴愪笉鎴愬姛閮芥棤鎵�璋�) 蹇呴』鑳藉鑱旂綉鎵嶈 + if (flage == 1) + { + HdlFirmwareUpdateLogic.AddFirmwareVersionInfo(FirmwareLevelType.A铏氭嫙椹卞姩, + data.DriveHwVersion.ToString(), + data.DriveImageType.ToString()); + } + + //铏氭嫙椹卞姩 + var virtualFirmware = HdlFirmwareUpdateLogic.GetFirmwareMostVersionInfo(FirmwareLevelType.A铏氭嫙椹卞姩, + data.DriveHwVersion.ToString(), + data.DriveImageType.ToString(), + data.DriveFwVersion); + + if (virtualFirmware != null) + { + virtualFirmware.VirtualCode = data.DriveCode; + list.Add(virtualFirmware); + } + } + } + if (list.Count == 2) + { + //铏氭嫙椹卞姩濡傛灉娌℃湁鏂扮増鏈殑璇濓紝鍥哄畾娣诲姞涓�涓┖鐨� + list.Add(null); + } return list; } @@ -1666,31 +1635,27 @@ /// </summary> /// <param name="zbGateway"></param> /// <returns></returns> - public async Task<List<ZbGatewayData.DriveCodeObj>> GetListVDDriveCode(ZbGateway zbGateway) + public List<ZbGatewayData.DriveCodeObj> GetListVDDriveCode(ZbGateway zbGateway) { - ZbGateway zbTemp = null; - if (this.GetRealGateway(ref zbTemp, zbGateway) == false) + //濡傛灉鏄睍绀烘ā鏉�,鎴栬�呮槸铏氭嫙浣忓畢鐨勮瘽,鐩存帴鍙栫紦瀛� + if (Common.Config.Instance.Home.IsVirtually == true || Common.Config.Instance.Home.IsShowTemplate == true) + { + //鍙栨湰鍦板璞� + var localgw = this.GetLocalGateway(zbGateway.GwId); + return localgw != null ? zbGateway.DriveCodeList : zbGateway.DriveCodeList; + } + var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 505 } }; + var result = this.SendJobjectDataToGateway(zbGateway, "VirtualDrive/CatDriveCode", jObject.ToString(), "VirtualDrive/CatDriveCode_Respon"); + if (result.ErrorMsg != null) + { + this.ShowTipMsg(result.ErrorMsg); + } + if (result.ErrorMsgDiv == 0) { return null; } - var result = await zbTemp.CheckVDDriveCodeAsync(); - //妫�娴嬬綉鍏宠繑鍥炵殑鍏遍�氶敊璇姸鎬佺爜 - string error = HdlCheckLogic.Current.CheckCommonErrorCode(result); - if (error != null) - { - this.ShowTipMsg(error); - return null; - } - - if (result == null || result.vDriveDriveCodeResponData == null || result.vDriveDriveCodeResponData.DriveCodeList == null) - { - return null; - } - if (result.vDriveDriveCodeResponData.DriveCodeList.Count > 0) - { - return result.vDriveDriveCodeResponData.DriveCodeList; - } - return null; + var dataInfo = JsonConvert.DeserializeObject<ZbGatewayData.VDriveDriveCodeResponData>(result.ReceiptData); + return dataInfo.DriveCodeList; } #endregion @@ -1704,8 +1669,7 @@ /// <returns></returns> public int IsMainGateway(ZbGateway zbGateway) { - string gwID = this.GetGatewayId(zbGateway); - return this.IsMainGateway(gwID); + return this.IsMainGateway(zbGateway.GwId); } /// <summary> @@ -1715,12 +1679,16 @@ /// <returns></returns> public int IsMainGateway(string waiID) { - ZbGateway zbGateway = null; - if (this.GetRealGateway(ref zbGateway, waiID) == false) + var realWay = ZbGateway.GateWayList.Find((obj) => + { + return obj.GwId == waiID; + }); + //铏氭嫙缃戝叧涔熻繑鍥� 0 + if (realWay == null || realWay.IsVirtual == true) { return 0; } - return zbGateway.IsMainGateWay == true ? 1 : 2; + return realWay.IsMainGateWay == true ? 1 : 2; } #endregion @@ -1734,23 +1702,23 @@ /// <param name="zbGateway"></param> public void SetRealGatewayPictrue(Button button, ZbGateway zbGateway) { - string gwID = this.GetGatewayId(zbGateway); - var localWay = this.GetLocalGateway(gwID); - if (localWay == null || localWay.getGwInfo == null) + var localWay = this.GetLocalGateway(zbGateway.GwId); + if (localWay == null) { - if (zbGateway.getGwInfo != null && zbGateway.getGwInfo.LinuxImageType != 0) + if (zbGateway.LinuxImageType != -1) { - button.UnSelectedImagePath = "Gateway/RealGateway" + zbGateway.getGwInfo.LinuxImageType + ".png"; + button.UnSelectedImagePath = "Gateway/RealGateway" + zbGateway.LinuxImageType + ".png"; } else { //缁欎竴涓嚎绋嬪幓鑾峰彇瀹冪殑闀滃儚绫诲瀷 - HdlThreadLogic.Current.RunThread(async () => + HdlThreadLogic.Current.RunThread(() => { - var result = await this.GetGatewayNewInfoAsync(zbGateway, ShowErrorMode.NO); + var result = this.GetGatewayInfo(zbGateway, true, ShowErrorMode.NO); if (result != null) { - zbGateway.getGwInfo = result; + //灏嗙綉鍏崇殑鏁版嵁璁剧疆鍒版湰鍦扮紦瀛樹腑 + this.SetGatewayDataToLocalMemmory(zbGateway, result, false); HdlThreadLogic.Current.RunMain(() => { button.UnSelectedImagePath = "Gateway/RealGateway" + result.LinuxImageType + ".png"; @@ -1761,7 +1729,7 @@ } else { - button.UnSelectedImagePath = "Gateway/RealGateway" + localWay.getGwInfo.LinuxImageType + ".png"; + button.UnSelectedImagePath = "Gateway/RealGateway" + localWay.LinuxImageType + ".png"; } } @@ -1772,23 +1740,23 @@ /// <param name="zbGateway"></param> public void SetGatewayIcon(Button button, ZbGateway zbGateway) { - string gwID = this.GetGatewayId(zbGateway); - var localWay = this.GetLocalGateway(gwID); - if (localWay == null || localWay.getGwInfo == null) + var localWay = this.GetLocalGateway(zbGateway.GwId); + if (localWay == null) { - if (zbGateway.getGwInfo != null && zbGateway.getGwInfo.LinuxImageType != 0) + if (zbGateway.LinuxImageType != -1) { - button.UnSelectedImagePath = "Gateway/GatewayIcon" + zbGateway.getGwInfo.LinuxImageType + ".png"; + button.UnSelectedImagePath = "Gateway/GatewayIcon" + zbGateway.LinuxImageType + ".png"; } else { //缁欎竴涓嚎绋嬪幓鑾峰彇瀹冪殑闀滃儚绫诲瀷 - HdlThreadLogic.Current.RunThread(async () => + HdlThreadLogic.Current.RunThread(() => { - var result = await this.GetGatewayNewInfoAsync(zbGateway, ShowErrorMode.NO); + var result = this.GetGatewayInfo(zbGateway, true, ShowErrorMode.NO); if (result != null) { - zbGateway.getGwInfo = result; + //灏嗙綉鍏崇殑鏁版嵁璁剧疆鍒版湰鍦扮紦瀛樹腑 + this.SetGatewayDataToLocalMemmory(zbGateway, result, false); HdlThreadLogic.Current.RunMain(() => { button.UnSelectedImagePath = "Gateway/GatewayIcon" + result.LinuxImageType + ".png"; @@ -1799,7 +1767,7 @@ } else { - button.UnSelectedImagePath = "Gateway/GatewayIcon" + localWay.getGwInfo.LinuxImageType + ".png"; + button.UnSelectedImagePath = "Gateway/GatewayIcon" + localWay.LinuxImageType + ".png"; } } @@ -1814,8 +1782,7 @@ /// <returns></returns> public bool IsGatewayExist(ZbGateway zbGateway) { - string gwID = this.GetGatewayId(zbGateway); - return this.IsGatewayExist(gwID); + return this.IsGatewayExist(zbGateway.GwId); } /// <summary> @@ -1859,272 +1826,6 @@ } } - #endregion - - #region 鈻� 缃戝叧鐩戣___________________________ - - #region 鈻� 鍙橀噺澹版槑___________________________ - - /// <summary> - /// 褰撳墠鐨勭綉缁滆繛鎺ユā寮� - /// </summary> - private GatewayConnectMode nowGwConnectMode = GatewayConnectMode.None; - /// <summary> - /// 鏄惁瀛樺湪缃戝叧姝e湪鍗囩骇 - /// </summary> - private bool hadGatewayUpdate = false; - - #endregion - - #region 鈻� 缃戝叧杩炴帴鏂瑰紡鍙樻洿___________________ - - /// <summary> - /// 褰撶綉鍏崇殑杩炴帴鏂瑰紡鏀瑰彉鏃�,妫�娴嬬綉鍏�,鐒跺悗鏄剧ず鐗规晥 - /// </summary> - /// <param name="connectMode">缃戝叧鍙樻洿鍚庣殑杩炴帴鏂瑰紡</param> - public void CheckGatewayByConnectChanged(GatewayConnectMode connectMode) - { - if (this.nowGwConnectMode == connectMode || this.hadGatewayUpdate == true) - { - //鐩稿悓鐨勮繛鎺ユā寮�,鎴栬�呮湁缃戝叧姝e湪鍗囩骇,鍒欎笉闇�瑕佹搷浣� - return; - } - - //娌℃湁缃戠粶 - if (connectMode == GatewayConnectMode.NoLine) - { - nowGwConnectMode = GatewayConnectMode.NoLine; - //鍦ㄨ繙绋嬬殑鏉′欢涓嬶紝妫�鏌ョ綉鍏崇殑鍦ㄧ嚎鐘舵��(缃戠粶鍙樻洿鏃惰Е鍙�) - } - //鍙互鍙�4G - else if (connectMode == GatewayConnectMode.Remote) - { - nowGwConnectMode = GatewayConnectMode.Remote; - //鍦ㄨ繙绋嬬殑鏉′欢涓嬶紝妫�鏌ョ綉鍏崇殑鍦ㄧ嚎鐘舵��(缃戠粶鍙樻洿鏃惰Е鍙�) - this.CheckGatewayStatuByRemote(); - } - //WIFI - else if (connectMode == GatewayConnectMode.WIFI) - { - nowGwConnectMode = GatewayConnectMode.WIFI; - //鍦╓IFI鐨勬潯浠朵笅锛屾鏌ョ綉鍏崇殑鍦ㄧ嚎鐘舵��(缃戠粶鍙樻洿鏃惰Е鍙�) - this.CheckGatewayStatuByWIFI(); - } - } - - #endregion - - #region 鈻� 妫�鏌ョ綉鍏�(WIFI)_____________________ - - /// <summary> - /// 鍦╓IFI鐨勬潯浠朵笅锛屾鏌ョ綉鍏崇殑鍦ㄧ嚎鐘舵��(缃戠粶鍙樻洿鏃惰Е鍙�) - /// </summary> - private void CheckGatewayStatuByWIFI() - { - if (this.dicGateway.Count == 0) - { - //濡傛灉鏈湴閮芥病鏈夌綉鍏崇殑璇濓紝涓嶉渶瑕佸鐞� - return; - } - - HdlThreadLogic.Current.RunThread(() => - { - var dicStatu = this.GetNowOnlineStatuBeforeCheck(); - //绛変釜3绉掞紝鍑嗗涓�涓� - System.Threading.Thread.Sleep(3000); - if (nowGwConnectMode != GatewayConnectMode.WIFI) - { - //缃戠粶涓存椂鍙樻洿锛燂紵 - return; - } - - //浠庣綉鍏宠幏鍙栧叏閮ㄧ殑缃戝叧 - List<ZbGateway> list = this.GetAllGatewayFromGateway(); - foreach (var way in list) - { - string gwId = this.GetGatewayId(way); - if (this.dicGateway.ContainsKey(gwId) == true) - { - //鍒锋柊涓�涓嬬紦瀛� - this.dicGateway[gwId].GatewayOnlineFlage = way.GatewayOnlineFlage; - } - if (this.CheckGatewayOnlineByFlag(way) == true) - { - //缃戝叧鍦ㄧ嚎 - dicStatu[gwId] = true; - } - } - //灏嗗彉鍖栫殑缃戝叧鎺ㄩ�佸埌鐣岄潰涓� - this.PushGatewayOnlineStatuToForm(dicStatu); - }); - } - - #endregion - - #region 鈻� 妫�鏌ョ綉鍏�(4G)_______________________ - - /// <summary> - /// 鍦ㄨ繙绋嬬殑鏉′欢涓嬶紝妫�鏌ョ綉鍏崇殑鍦ㄧ嚎鐘舵��(缃戠粶鍙樻洿鏃惰Е鍙�) - /// </summary> - private void CheckGatewayStatuByRemote() - { - if (this.dicGateway.Count == 0) - { - //濡傛灉鏈湴閮芥病鏈夌綉鍏崇殑璇濓紝涓嶉渶瑕佸鐞� - return; - } - - HdlThreadLogic.Current.RunThread(() => - { - //鍏堣幏鍙栫幇鍦ㄥ叏閮ㄧ殑缃戝叧锛屽垵鏈熷�よ缃负涓嶅湪绾� - var dicStatu = this.GetNowOnlineStatuBeforeCheck(); - - //鑾峰彇浜戠涓婇潰鐨勭綉鍏� - Dictionary<string, GatewayResult> dicDbGateway = HdlGatewayLogic.Current.GetGateWayListFromDataBase(); - if (nowGwConnectMode != GatewayConnectMode.Remote || dicDbGateway == null) - { - //缃戠粶涓存椂鍙樻洿锛燂紵 - return; - } - - bool hadOnline = false; - lock (dicGateway) - { - foreach (var way in this.dicGateway.Values) - { - //寰幆澶勭悊鏈湴鍏ㄩ儴鐨勭綉鍏冲垪琛� - string gwId = this.GetGatewayId(way); - if (dicDbGateway.ContainsKey(gwId) == true//濡傛灉浜戠涓婇潰鏈夎繖涓綉鍏� - && dicDbGateway[gwId].MqttOnlineStatus == true//濡傛灉浜戠涓婇潰杩欎釜缃戝叧鏄湪绾跨殑 - ) - { - dicGateway[gwId].GatewayOnlineFlage = true; - //瀹冩槸鍦ㄧ嚎鐨� - dicStatu[gwId] = true; - hadOnline = true; - } - } - } - - //灏嗗彉鍖栫殑缃戝叧鎺ㄩ�佸埌鐣岄潰涓� - this.PushGatewayOnlineStatuToForm(dicStatu); - - //濡傛灉娌℃湁缃戝叧鍦ㄧ嚎 - if (hadOnline == false) - { - //杩滅▼杩炴帴瓒呮椂,娌℃湁缃戝叧鍦ㄧ嚎 - string msg = Language.StringByID(R.MyInternationalizationString.uRemoteTimeOutAndNotGatewaiOnline); - //this.ShowTipMsg(msg); - } - }); - } - - #endregion - - #region 鈻� 妫�鏌ョ綉鍏�(鏃犵綉缁�)___________________ - - /// <summary> - /// 鍦ㄨ繙绋嬬殑鏉′欢涓嬶紝妫�鏌ョ綉鍏崇殑鍦ㄧ嚎鐘舵��(缃戠粶鍙樻洿鏃惰Е鍙�) - /// </summary> - private void CheckGatewayStatuByNotNet() - { - //褰撳墠鏃犵綉缁滆繛鎺�,璇风‘璁ょ綉缁� - string msg = Language.StringByID(R.MyInternationalizationString.uNowIsDonotNetworkAndCheckNetwork); - this.ShowTipMsg(msg); - } - - #endregion - - #region 鈻� 缃戝叧鎺ㄩ�乢__________________________ - - /// <summary> - /// 灏嗗彉鍖栫殑缃戝叧鎺ㄩ�佸埌鐣岄潰涓� - /// </summary> - /// <param name="dicStatu"></param> - private void PushGatewayOnlineStatuToForm(Dictionary<string, bool> dicStatu) - { - List<ZbGateway> listChange = new List<ZbGateway>(); - List<bool> listChangeStatu = new List<bool>(); - foreach (string gwid in dicStatu.Keys) - { - //缃戝叧涓嶅锛岀洿鎺ユ帹閫�,涓嶅垽鏂槸鍚︽敼鍙樹簡 - ZbGateway zbGateway = this.GetLocalGateway(gwid); - if (zbGateway != null) - { - listChange.Add(zbGateway); - listChangeStatu.Add(dicStatu[gwid]); - - zbGateway.GatewayOnlineFlage = dicStatu[gwid]; - } - } - - if (listChange.Count == 0) - { - return; - } - //淇濆瓨褰撳墠婵�娲荤殑鐣岄潰ID - var listId = new List<string>(); - listId.AddRange(UserCenterResourse.listActionFormId); - for (int i = 0; i < listId.Count; i++) - { - if (UserCenterResourse.DicActionForm.ContainsKey(listId[i]) == false) - { - continue; - } - //缃戝叧鍦ㄧ嚎鎺ㄩ�� - for (int j = 0; j < listChange.Count; j++) - { - UserCenterResourse.DicActionForm[listId[i]]?.GatewayOnlinePush(listChange[j], listChangeStatu[j]); - } - } - } - - #endregion - - #region 鈻� 涓�鑸柟娉昣__________________________ - - /// <summary> - /// 璁剧疆瀛樺湪缃戝叧姝e湪鍗囩骇鐨勬爣璇� - /// </summary> - /// <param name="update">鏄惁鏈夌綉鍏冲湪鍗囩骇</param> - public void SetHadGatewayUpdateFlage(bool update) - { - this.hadGatewayUpdate = update; - } - - /// <summary> - /// 鍦ㄧ綉缁滆繛鎺ュ彉鏇翠箣鍚庯紝缃戝叧杩炴帴妫�娴嬩箣鍓嶏紝璁剧疆鍏ㄩ儴缃戝叧涓虹绾跨姸鎬�(浠ユ湰鍦扮綉鍏充负鏍囧噯) - /// </summary> - /// <returns></returns> - private Dictionary<string, bool> GetNowOnlineStatuBeforeCheck() - { - //鍏堣幏鍙栫幇鍦ㄥ叏閮ㄧ殑缃戝叧 - List<ZbGateway> listOldGateway = this.GetAllGatewayFromGateway(); - var dicOldZb = new Dictionary<string, ZbGateway>(); - foreach (var zb in listOldGateway) - { - string gwID = this.GetGatewayId(zb); - dicOldZb[gwID] = zb; - } - - var dicStatu = new Dictionary<string, bool>(); - lock (dicGateway) - { - foreach (string gwId in this.dicGateway.Keys) - { - if (dicOldZb.ContainsKey(gwId) == true) - { - //鏍囪涓轰笉鍦ㄧ嚎 - this.SetGatewayNotOnLineFlag(dicOldZb[gwId]); - } - dicStatu[gwId] = false; - } - } - return dicStatu; - } - - #endregion - #endregion #region 鈻� 浠庝簯绔幏鍙栧叏閮ㄧ綉鍏冲垪琛↖D___________ @@ -2143,9 +1844,9 @@ } bool canBreak = false; - HdlThreadLogic.Current.RunThread(async () => + HdlThreadLogic.Current.RunThread(() => { - List<string> list = new List<string>() { "NotSetAgain", "NotCheck" }; + List<string> list = new List<string>() { "NotCheck" }; //璁剧疆璁块棶鎺ュ彛鐨勫弬鏁� var pra = new GetGatewayPra(); @@ -2154,7 +1855,7 @@ //鑾峰彇鎺у埗涓讳汉璐﹀彿鐨凾oken pra.ReqDto.LoginAccessToken = UserCenterLogic.GetConnectMainToken(); - var result = await UserCenterLogic.GetResponseDataByRequestHttps("App/GetSingleHomeGatewayPagger", true, pra, list); + var result = UserCenterLogic.GetResponseDataByRequestHttps("App/GetSingleHomeGatewayPagger", true, pra, list, false); if (string.IsNullOrEmpty(result) == true) { canBreak = true; @@ -2196,16 +1897,25 @@ /// <param name="gateway">缃戝叧瀵硅薄</param> /// <param name="Longitude">缁忓害</param> /// <param name="Latitude">缁村害</param> + /// <param name="mode">鏄剧ず閿欒</param> /// <returns></returns> - public bool SetGatewaySite(ZbGateway gateway, double Longitude, double Latitude) + public bool SetGatewaySite(ZbGateway gateway, double Longitude, double Latitude, ShowErrorMode mode) { ZbGateway realWay = null; if (this.GetRealGateway(ref realWay, gateway) == false) { - //閿欒:缃戝叧瀵硅薄涓㈠け - string msg = Language.StringByID(R.MyInternationalizationString.uErrorGatewayLostMsg); - this.ShowTipMsg(msg); + if (mode == ShowErrorMode.YES) + { + //閿欒:缃戝叧瀵硅薄涓㈠け + string msg = Language.StringByID(R.MyInternationalizationString.uErrorGatewayLostMsg); + this.ShowTipMsg(msg); + } return false; + } + if (((decimal)Longitude) == 0 && ((decimal)Latitude) == 0) + { + //娌℃湁閰嶇疆杩囩粡绾害 + return true; } int result = -1; @@ -2249,13 +1959,16 @@ realWay.Actions -= action; if (result != 0) { - //璁剧疆缃戝叧缁忕含搴﹀け璐� - string msg = Language.StringByID(R.MyInternationalizationString.uSetGatewaySiteFail); - if (result == -1) + if (mode == ShowErrorMode.YES) { - msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, null, "鍥炲瓒呮椂"); + //璁剧疆缃戝叧缁忕含搴﹀け璐� + string msg = Language.StringByID(R.MyInternationalizationString.uSetGatewaySiteFail); + if (result == -1) + { + msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, null, "鍥炲瓒呮椂"); + } + this.ShowTipMsg(msg); } - this.ShowTipMsg(msg); return false; } @@ -2270,16 +1983,16 @@ /// 瑙g粦浜戠缁戝畾鐨勭綉鍏� /// </summary> /// <param name="strWayId"></param> - public async Task<bool> DeleteDataBaseGateway(string strWayId) + public bool DeleteDataBaseGateway(string strWayId) { var Pra = new DeleteGatewayPra(); Pra.BindGateways.Add(strWayId); //鑾峰彇鎺у埗涓讳汉璐﹀彿鐨凾oken Pra.LoginAccessToken = UserCenterLogic.GetConnectMainToken(); - List<string> listNotShowError = new List<string>() { "NoExist", "NoBind", "NoRecord", "NotSetAgain" }; + List<string> listNotShowError = new List<string>() { "NoExist", "NoBind", "NoRecord", "NotCheck" }; - bool result = await UserCenterLogic.GetResultStatuByRequestHttps("App/ReleaseGatewayToHome", true, Pra, listNotShowError); + bool result = UserCenterLogic.GetResultStatuByRequestHttps("App/ReleaseGatewayToHome", true, Pra, listNotShowError, false); if (result == false) { return false; @@ -2297,15 +2010,13 @@ /// <param name="zbGateway"></param> public void BackupGatewayIdOnNotNetwork(ZbGateway zbGateway) { - var strId = HdlGatewayLogic.Current.GetGatewayId(zbGateway); + var strId = zbGateway.GwId; if (listBackupGwId.Contains(strId) == false) { listBackupGwId.Add(strId); //澶囦唤 - var strData = Newtonsoft.Json.JsonConvert.SerializeObject(listBackupGwId); - var byteData = System.Text.Encoding.UTF8.GetBytes(strData); - Global.WriteFileToDirectoryByBytes(DirNameResourse.LocalMemoryDirectory, DirNameResourse.BackupGatewayIdFile, byteData); + HdlFileLogic.Current.SaveFileContent(DirNameResourse.BackupGatewayIdFile, listBackupGwId); } } @@ -2314,9 +2025,9 @@ /// </summary> public void ResetComandToBindBackupGateway() { - HdlThreadLogic.Current.RunThread(async () => + HdlThreadLogic.Current.RunThread(() => { - var fileData = Global.ReadFileByDirectory(DirNameResourse.LocalMemoryDirectory, DirNameResourse.BackupGatewayIdFile); + var fileData = HdlFileLogic.Current.ReadFileByteContent(DirNameResourse.BackupGatewayIdFile); if (fileData == null) { return; @@ -2325,8 +2036,6 @@ var listTempId = new List<string>(); listTempId.AddRange(this.listBackupGwId); - //涓嶆樉绀哄凡缁忚缁戝畾杩囩殑淇℃伅,NotSetAgain:鍋囧鏂綉鏃�,涓嶄簩娆″彂閫� - List<string> listNotShow = new List<string>() { "NotSetAgain" }; //璋冪敤鎺ュ彛锛岀粦瀹氱綉鍏� var bindGateway = new BindGatewayPra(); //鑾峰彇鎺у埗涓讳汉璐﹀彿鐨凾oken @@ -2336,12 +2045,12 @@ { bindGateway.BindGateways.Clear(); bindGateway.BindGateways.Add(gwId); - var result = await UserCenterLogic.GetResultCodeByRequestHttps("App/BindGatewayToHome", true, bindGateway, listNotShow); + var result = UserCenterLogic.GetResultCodeByRequestHttps("App/BindGatewayToHome", true, bindGateway, new List<string> { "NotCheck" }, false); if (result == "Success") { this.listBackupGwId.Remove(gwId); } - if (result == "Error" || result == "ErrorEx") + if (result == "Error") { break; } @@ -2350,7 +2059,7 @@ if (this.listBackupGwId.Count == 0) { //濡傛灉娌℃湁浜嗗唴瀹�,鍒欏垹闄ゆ枃浠� - string file = UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.BackupGatewayIdFile); + string file = DirNameResourse.BackupGatewayIdFile; if (System.IO.File.Exists(file) == true) { System.IO.File.Delete(file); @@ -2359,299 +2068,324 @@ else { //澶囦唤 - var strData = Newtonsoft.Json.JsonConvert.SerializeObject(listBackupGwId); - var byteData = System.Text.Encoding.UTF8.GetBytes(strData); - Global.WriteFileToDirectoryByBytes(DirNameResourse.LocalMemoryDirectory, DirNameResourse.BackupGatewayIdFile, byteData); + HdlFileLogic.Current.SaveFileContent(DirNameResourse.BackupGatewayIdFile, listBackupGwId); } }); } #endregion - #region 鈻� 涓婁紶璁惧澶囦唤鍒扮綉鍏砡________________ + #region 鈻� 鍙戦�佺綉鍏冲懡浠ょ粰缃戝叧_________________ /// <summary> - /// 涓婁紶璁惧澶囦唤鍒扮綉鍏�(鏈�濂戒笉瑕佸幓鐞嗗畠鐨勮繑鍥炵粨鏋�) + /// 鍙戦�佹暟鎹埌缃戝叧锛屽苟鎺ュ彈缃戝叧杩斿洖鐨勬暟鎹�(ReceiptData涓鸿繑鍥炲��) /// </summary> - /// <param name="device">璁惧瀵硅薄</param> - /// <param name="backupEnum">澶囦唤绫诲瀷</param> - /// <param name="upLaodData">澶囦唤鐨勬暟鎹�</param> - /// <returns></returns> - public async Task<bool> UpLoadDeviceBackupDataToGateway(CommonDevice device, GatewayBackupEnum backupEnum, object upLaodData) + /// <param name="gateway">缃戝叧瀵硅薄</param> + /// <param name="sendTopic">鍙戦�佺殑涓婚</param> + /// <param name="sendData">闇�瑕佸彂閫佺殑鏁版嵁 JObject.ToString()鐨勪笢瑗�</param> + /// <param name="receiptTopic">鎸囧畾鎺ユ敹鍝釜涓婚</param> + /// <param name="waitTime">瓒呮椂鏃堕棿(绉�)</param> + /// <param name="useLocalConnect">鏄惁寮哄埗浣跨敤鏈湴杩炴帴鍙戦��</param> + /// <returns>缃戝叧杩斿洖鐨勬暟鎹�</returns> + public ReceiptGatewayResult SendJobjectDataToGateway(ZbGateway gateway, string sendTopic, string sendData, string receiptTopic, int waitTime = 5, bool useLocalConnect = false) { - //ZbGateway realWay = null; - //if (HdlGatewayLogic.Current.GetRealGateway(ref realWay, device.CurrentGateWayId) == false) - //{ - // //娌℃湁鎵惧埌鐪熷疄鐗╃悊缃戝叧 - // return false; - //} - //string fileName = device.FilePath + ((int)backupEnum).ToString().PadLeft(5, '0'); - ////鍒涘缓鏂囦欢瀵硅薄 - //var result = await realWay.CreateFileAsync(fileName); - //if (result == null || result.Result != 0) - //{ - // return false; - //} - ////鍙戦�佹暟鎹祦 - //var data = Newtonsoft.Json.JsonConvert.SerializeObject(upLaodData); - //var byteData = System.Text.Encoding.UTF8.GetBytes(data); - //var result2 = await realWay.SendFileAsync(byteData); - //if (result2 == null || result2.Result != 0) - //{ - // return false; - //} - return true; + var reResult = new ReceiptGatewayResult(); + + ZbGateway myGateway = null; + if (this.GetRealGateway(ref myGateway, gateway) == false) + { + //鑾峰彇缃戝叧瀵硅薄澶辫触 + reResult.ErrorMsg = Language.StringByID(R.MyInternationalizationString.uGetGatewayTagartFail); + reResult.ErrorMsgDiv = -1; + return reResult; + } + //缃戝叧ID + string gatewayID = gateway.GwId; + //閿欒涓婚 + string errorTopic = gatewayID + "/" + "Error_Respon"; + //妫�娴嬪璞$殑涓婚 + string checkTopic = gatewayID + "/" + receiptTopic; + + Action<string, string> receiptAction = (topic, message) => + { + var jobject = Newtonsoft.Json.Linq.JObject.Parse(message); + + //缃戝叧鍥炲閿欒 + if (topic == errorTopic) + { + var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonDevice.ErrorResponData>(jobject["Data"].ToString()); + reResult.ErrorMsg = HdlCheckLogic.Current.CheckCommonErrorCode(temp.Error); + } + //濡傛灉鏄寚瀹氱殑涓婚 + if (topic == checkTopic) + { + reResult.ReceiptData = jobject["Data"].ToString(); + } + }; + myGateway.Actions += receiptAction; + //鍙戦�佹暟鎹� + if (useLocalConnect == false) + { + myGateway.Send(sendTopic, sendData); + } + else + { + myGateway.SendLocation(sendTopic, System.Text.Encoding.UTF8.GetBytes(sendData)); + } + + //瓒呮椂鏃堕棿 + int TimeOut = 0; + waitTime = 20 * waitTime; + while (reResult.ReceiptData == null && TimeOut < waitTime) + { + //鍏ㄩ儴鎺ユ敹鎵嶉��鍑� + System.Threading.Thread.Sleep(50); + TimeOut++; + } + myGateway.Actions -= receiptAction; + receiptAction = null; + if (reResult.ReceiptData == null) + { + reResult.ErrorMsgDiv = 0; + } + + return reResult; } #endregion - #region 鈻� 鎭㈠缃戝叧___________________________ + #region 鈻� 缃戝叧鐩戣___________________________ /// <summary> - /// 鎭㈠缃戝叧閰嶇疆 + /// 褰撳墠鐨勭綉缁滆繛鎺ユā寮� /// </summary> - /// <param name="zbGateway">缃戝叧瀵硅薄</param> - /// <param name="listGatewayFile">缃戝叧閲岄潰鎷ユ湁鐨勬枃浠�</param> - /// <param name="backDirectory">浜戠涓嬭浇鐨勫浠芥暟鎹殑鏂囦欢澶瑰悕瀛�</param> - /// <returns></returns> - public async Task<bool> RecoverGatewaySettion(ZbGateway zbGateway, List<string> listGatewayFile, string backDirectory) + private GatewayConnectMode nowGwConnectMode = GatewayConnectMode.None; + /// <summary> + /// 鏄惁瀛樺湪缃戝叧姝e湪鍗囩骇 + /// </summary> + private bool hadGatewayUpdate = false; + + /// <summary> + /// 褰撶綉鍏崇殑杩炴帴鏂瑰紡鏀瑰彉鏃�,璁板綍褰撳墠鐨勮繛鎺ユ柟寮� + /// </summary> + /// <param name="connectMode">缃戝叧鍙樻洿鍚庣殑杩炴帴鏂瑰紡</param> + public void CheckGatewayByConnectChanged(GatewayConnectMode connectMode) { - //鑾峰彇浠庝簯绔偅閲屽緱鏉ョ殑鍏ㄩ儴鏂囦欢 - var listBackFile = HdlAutoBackupLogic.GetFileFromDirectory(UserCenterLogic.CombinePath(backDirectory)); - listBackFile.Sort(); - //娣诲姞闄勫姞鎯呮姤:杩樺師璁惧閰嶇疆 - ProgressBar.SetAppendText(Language.StringByID(R.MyInternationalizationString.uRecoverDeviceSettion)); - ProgressBar.SetMaxValue(listBackFile.Count); - ProgressBar.Show(); - - var listSaveFile = new HashSet<string>(); - foreach (string fileName in listBackFile) - { - if (fileName.Length < 10 || fileName.StartsWith("Device_") == false) - { - //涓嶆槸璁惧澶囦唤鏂囦欢 - continue; - } - //鎭㈠璁惧閰嶇疆 - var result = await this.RecoverDeviceSettion(backDirectory, fileName); - if (result == -1) - { - ProgressBar.Close(); - return false; - } - if (result == 1 || result == 2) - { - listSaveFile.Add(fileName); - } - - ProgressBar.SetValue(1); - listGatewayFile.Remove(fileName); - } - //娣诲姞闄勫姞鎯呮姤:瑕嗙洊缃戝叧澶囦唤涓� - ProgressBar.SetAppendText(Language.StringByID(R.MyInternationalizationString.uGatewayBackupDataIsCovering)); - foreach (var file in listSaveFile) - { - //鍒涘缓鏂囦欢瀵硅薄 - var result = await zbGateway.CreateFileAsync(file); - if (result == null || result.Result != 0) - { - Application.RunOnMainThread(() => - { - //瑕嗙洊缃戝叧澶囦唤澶辫触 - string msg = Language.StringByID(R.MyInternationalizationString.uCoverGatewayBackupDataFail); - var control = new ShowMsgControl(ShowMsgType.Tip, msg); - control.Show(); - }); - ProgressBar.Close(); - return false; - } - //鍙戦�佹暟鎹祦 - var byteData = Shared.Common.Global.ReadFileByDirectory(backDirectory, file); - var result2 = await zbGateway.SendFileAsync(byteData); - if (result2 == null || result2.Result != 0) - { - Application.RunOnMainThread(() => - { - //瑕嗙洊缃戝叧澶囦唤澶辫触 - string msg = Language.StringByID(R.MyInternationalizationString.uCoverGatewayBackupDataFail); - var control = new ShowMsgControl(ShowMsgType.Tip, msg); - control.Show(); - }); - ProgressBar.Close(); - return false; - } - } - //鍒犻櫎澶氫綑鐨勬枃浠� - foreach (var deleteFile in listGatewayFile) - { - if (deleteFile.Length > 12) - { - string deviceFileName = deleteFile.Substring(5); - if (deviceFileName.StartsWith("Device_") == false) - { - //涓嶆槸璁惧鏂囦欢 - continue; - } - string[] strArry = deviceFileName.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries); - if (strArry.Length < 4 || listSaveFile.Contains(deleteFile) == true) - { - //鎴戜篃涓嶇煡閬撳畠鏄粈涔堟枃浠�,鎴栬�呰繖涓枃浠舵槸瑕嗙洊鐨� - continue; - } - //杩欎釜鎴栬鍙互涓嶇悊浠� - var result = await zbGateway.DelFileOrDirAsync("/etc/hdlDat/" + deleteFile); - } - } - ProgressBar.Close(); - - return true; + this.nowGwConnectMode = connectMode; } /// <summary> - /// 鎭㈠璁惧閰嶇疆 + /// 寮�鍚娴嬬綉鍏冲湪绾跨姸鎬佺殑绾跨▼(姝ゆ柟娉曟槸缁欒澶囧垪琛ㄧ晫闈㈢敤鐨�) /// </summary> - /// <param name="backDirectory">浜戠涓嬭浇鐨勫浠芥暟鎹殑鏂囦欢澶瑰悕瀛�</param> - /// <param name="backFileName">缃戝叧鐨勮澶囧浠藉悕瀛�</param> - /// <returns></returns> - private async Task<int> RecoverDeviceSettion(string backDirectory, string backFileName) + /// <param name="frameLayout">鐣岄潰瀵硅薄</param> + public void StartCheckGatewayOnlineThread(EditorCommonForm frameLayout) { - string deviceFileName = backFileName.Substring(0, backFileName.Length - 5); - string[] strArry = deviceFileName.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries); - if (strArry.Length < 4) + HdlThreadLogic.Current.RunThread(() => { - //鎴戜篃涓嶇煡閬撳畠鏄粈涔堟枃浠� - return 0; - } - var device = Common.LocalDevice.Current.GetDevice(strArry[2], Convert.ToInt32(strArry[3])); - if (device == null) - { - //杩欎釜璁惧涓嶈浜� - return 2; - } - - var byteData = Shared.Common.Global.ReadFileByDirectory(backDirectory, backFileName); - var backType = (GatewayBackupEnum)Convert.ToInt32(backFileName.Substring(backFileName.Length - 5, 5)); - - bool result = true; - if (backType == GatewayBackupEnum.AMac鍚嶇О) - { - var recoverData = Newtonsoft.Json.JsonConvert.DeserializeObject<string>(System.Text.Encoding.UTF8.GetString(byteData)); - result = await Common.LocalDevice.Current.ReMacName(new List<CommonDevice>() { device }, recoverData); - } - else if (backType == GatewayBackupEnum.A绔偣鍚嶇О) - { - var recoverData = Newtonsoft.Json.JsonConvert.DeserializeObject<string>(System.Text.Encoding.UTF8.GetString(byteData)); - result = await Common.LocalDevice.Current.ReName(device, recoverData); - } - else if (backType == GatewayBackupEnum.APir鐏厜閰嶇疆) - { - var recoverData = Newtonsoft.Json.JsonConvert.DeserializeObject<IASZone.ConfigureParamates>(System.Text.Encoding.UTF8.GetString(byteData)); - result = await HdlDevicePirSensorLogic.Current.SetPirSensorLightSettion((IASZone)device, recoverData); - } - else if (backType == GatewayBackupEnum.A骞叉帴鐐归鑹茶皟鑺�) - { - var recoverData = Newtonsoft.Json.JsonConvert.DeserializeObject<Panel.KeyColorData>(System.Text.Encoding.UTF8.GetString(byteData)); - result = await HdlDevicePanelLogic.Current.SetPanelEpointColorInfo((Panel)device, recoverData); - } - else if (backType == GatewayBackupEnum.A骞叉帴鐐逛寒搴﹁皟鑺�) - { - var recoverData = Newtonsoft.Json.JsonConvert.DeserializeObject<Newtonsoft.Json.Linq.JObject>(System.Text.Encoding.UTF8.GetString(byteData)); - int directionsLevel = Convert.ToInt32(recoverData["directionsLevel"]); - int backlightLevel = Convert.ToInt32(recoverData["backlightLevel"]); - result = await HdlDevicePanelLogic.Current.SetDeviceLightSettion((Panel)device, directionsLevel, backlightLevel); - } - else if (backType == GatewayBackupEnum.A骞叉帴鐐硅妭鑳芥ā寮�) - { - var recoverData = Newtonsoft.Json.JsonConvert.DeserializeObject<Newtonsoft.Json.Linq.JObject>(System.Text.Encoding.UTF8.GetString(byteData)); - bool modeEnable = Convert.ToBoolean(recoverData["modeEnable"]); - int modeTime = Convert.ToInt32(recoverData["modeTime"]); - int level = Convert.ToInt32(recoverData["level"]); - result = await HdlDevicePanelLogic.Current.SetDeviceEnergyConservationMode((Panel)device, modeEnable, modeTime, level); - } - else if (backType == GatewayBackupEnum.A骞叉帴鐐圭鏈夊睘鎬�) - { - var recoverData = Newtonsoft.Json.JsonConvert.DeserializeObject<int>(System.Text.Encoding.UTF8.GetString(byteData)); - result = await HdlDevicePanelLogic.Current.EditorDryContactFunction((Panel)device, recoverData); - } - else if (backType == GatewayBackupEnum.A绐楀笜鏂瑰悜) - { - var statu = Newtonsoft.Json.JsonConvert.DeserializeObject<bool>(System.Text.Encoding.UTF8.GetString(byteData)); - result = await HdlDeviceCurtainLogic.Current.SetCurtainDirection((Rollershade)device, statu); - } - else if (backType == GatewayBackupEnum.A绐楀笜鎵嬫媺鎺у埗) - { - var statu = Newtonsoft.Json.JsonConvert.DeserializeObject<bool>(System.Text.Encoding.UTF8.GetString(byteData)); - result = await HdlDeviceCurtainLogic.Current.SetHandPullControl((Rollershade)device, statu); - } - else if (backType == GatewayBackupEnum.A绐楀笜涓婁笅闄愪綅) - { - var curtainDevice = (Rollershade)device; - var recoverData = Newtonsoft.Json.JsonConvert.DeserializeObject<Newtonsoft.Json.Linq.JObject>(System.Text.Encoding.UTF8.GetString(byteData)); - int upLimit = Convert.ToInt32(recoverData["upLimit"]); - int downLimit = Convert.ToInt32(recoverData["downLimit"]); - //鍏堥噸缃獥甯� - result = await HdlDeviceCurtainLogic.Current.RestoreCurtain(curtainDevice); - if (result == false) { return -1; } - await Task.Delay(5000); - //閲嶇疆涓婇檺浣� - result = await HdlDeviceCurtainLogic.Current.DeleteCurtainLimitPoint(curtainDevice, Rollershade.LimiType.UpLimit); - if (result == false) { return -1; } - //灏嗙獥甯樿皟鏁村埌鎸囧畾鐧惧垎姣� - curtainDevice.WcdGoToTiltValue(upLimit); - await Task.Delay(3000); - //鎵ц纭鍙婅鐩栫獥甯橀檺浣嶇偣 - result = await HdlDeviceCurtainLogic.Current.CommitCurtainLimitPoint(curtainDevice, Rollershade.CurtainPrivateInstalledLimi.UpLimit, -1, -1); - if (result == false) { return -1; } - - await Task.Delay(2000); - //閲嶇疆涓嬮檺浣� - result = await HdlDeviceCurtainLogic.Current.DeleteCurtainLimitPoint(curtainDevice, Rollershade.LimiType.DownLimit); - if (result == false) { return -1; } - //灏嗙獥甯樿皟鏁村埌鎸囧畾鐧惧垎姣� - curtainDevice.WcdGoToTiltValue(downLimit); - await Task.Delay(3000); - //鎵ц纭鍙婅鐩栫獥甯橀檺浣嶇偣 - result = await HdlDeviceCurtainLogic.Current.CommitCurtainLimitPoint(curtainDevice, Rollershade.CurtainPrivateInstalledLimi.DownLimit, -1, -1); - } - else if (backType == GatewayBackupEnum.A绌鸿皟鑷畾涔夋ā寮�) - { - var data = Newtonsoft.Json.JsonConvert.DeserializeObject<int>(System.Text.Encoding.UTF8.GetString(byteData)); - result = await HdlDeviceAirConditionerLogic.Current.SetAcModeSupport((AC)device, data); - if (result == true) + int waitCount = 0; + //濡傛灉浣忓畢ID鍙樻洿浜�,鍒欎笉鍐嶅鐞� + while (frameLayout.Parent != null && Config.Instance.HomeId != string.Empty) { - //杞崲涓轰簩杩涘埗 - var value = Convert.ToString(data, 2).PadLeft(16, '0'); - //杩欎簲涓缃槸鏀惧湪鍚庨潰鐨� - var fixValue = value.Substring(0, value.Length - 5); - var reportValue = value.Substring(fixValue.Length); - //鏇存敼缂撳瓨 - for (int i = 0; i < reportValue.Length; i++) + System.Threading.Thread.Sleep(1000); + if (this.hadGatewayUpdate == true) { - ((AC)device).listSupportMode[i] = Convert.ToInt32(reportValue[i]); + //缃戝叧姝e湪鍗囩骇,涓嶉渶瑕佹搷浣� + continue; } - device.ReSave(); + + waitCount++; + if (this.nowGwConnectMode == GatewayConnectMode.Remote) + { + //杩滅▼姣�20绉掓娴嬩竴娆� + if (waitCount < 20) { continue; } + } + else if (this.nowGwConnectMode == GatewayConnectMode.WIFI) + { + //灞�鍩熺綉姣�5绉掓娴嬩竴娆� + if (waitCount < 5) { continue; } + } + waitCount = 0; + + //鑾峰彇鍓嶅洖缃戝叧鐨勫湪绾跨姸鎬� + Dictionary<string, bool> dicOldOnline = this.GetOldGatewayOnlineStatu(); + if (dicOldOnline == null) + { + //鍒欎笉澶勭悊 + continue; + } + //鍙互鍙�4G + if (this.nowGwConnectMode == GatewayConnectMode.Remote) + { + //鍦ㄨ繙绋嬬殑鏉′欢涓嬶紝妫�鏌ョ綉鍏崇殑鍦ㄧ嚎鐘舵�� + this.CheckGatewayStatuByRemote(dicOldOnline); + } + //WIFI + else if (this.nowGwConnectMode == GatewayConnectMode.WIFI) + { + //鍦╓IFI鐨勬潯浠朵笅锛屾鏌ョ綉鍏崇殑鍦ㄧ嚎鐘舵�� + this.CheckGatewayStatuByWIFI(dicOldOnline); + } + } + }, ShowErrorMode.NO); + } + + /// <summary> + /// 鍦╓IFI鐨勬潯浠朵笅锛屾鏌ョ綉鍏崇殑鍦ㄧ嚎鐘舵�� + /// </summary> + private void CheckGatewayStatuByWIFI(Dictionary<string, bool> dicOldOnline) + { + //浠庣綉鍏宠幏鍙栧叏閮ㄧ殑缃戝叧 + List<ZbGateway> list = this.GetAllGatewayFromGateway(); + foreach (var way in list) + { + //灏嗘爣璇嗙疆涓篺alse + way.GatewayOnlineFlage = false; + } + //绛変釜2绉� + System.Threading.Thread.Sleep(2000); + + //2020.05.25杩藉姞:姝や綇瀹呮槸鍚︽嫢鏈夌綉鍏冲湪绾� + var hadGwOnline = false; + foreach (var way in list) + { + if (dicOldOnline.ContainsKey(way.GwId) == true) + { + if (way.GatewayOnlineFlage == true) + { + //鏈変竴涓綉鍏冲湪绾�,鍗冲湪绾� + hadGwOnline = true; + break; + } } } - return result == true ? 1 : -1; + + foreach (var way in list) + { + string gwId = way.GwId; + if (dicOldOnline.ContainsKey(gwId) == true) + { + //缃戝叧涔熶笉澶�,鐩存帴鎺ㄩ�佸惂 + this.PushGatewayOnlineStatuToForm(gwId, way.GatewayOnlineFlage, hadGwOnline); + } + else + { + //娌℃湁鍖呭惈,榛樿涓篺alse + this.PushGatewayOnlineStatuToForm(gwId, false, hadGwOnline); + } + } + } + + /// <summary> + /// 鍦ㄨ繙绋嬬殑鏉′欢涓嬶紝妫�鏌ョ綉鍏崇殑鍦ㄧ嚎鐘舵�� + /// </summary> + private void CheckGatewayStatuByRemote(Dictionary<string, bool> dicOldOnline) + { + //鑾峰彇浜戠涓婇潰鐨勭綉鍏� + Dictionary<string, GatewayResult> dicDbGateway = HdlGatewayLogic.Current.GetGateWayListFromDataBase(); + if (dicDbGateway == null) + { + //濡傛灉缃戠粶涓嶉��,鍒欎篃寰�涓嬭蛋 + dicDbGateway = new Dictionary<string, GatewayResult>(); + } + + //2020.05.25杩藉姞:姝や綇瀹呮槸鍚︽嫢鏈夌綉鍏冲湪绾� + var hadGwOnline = false; + foreach (var gwId in dicOldOnline.Keys) + { + //濡傛灉浜戠涓婇潰鏈夎繖涓綉鍏� + if (dicDbGateway.ContainsKey(gwId) == true + && dicDbGateway[gwId].MqttOnlineStatus == true) + { + //鏈変竴涓綉鍏冲湪绾�,鍗冲湪绾� + hadGwOnline = true; + break; + } + } + + foreach (var gwId in dicOldOnline.Keys) + { + //濡傛灉浜戠涓婇潰鏈夎繖涓綉鍏� + if (dicDbGateway.ContainsKey(gwId) == true) + { + //缃戝叧涔熶笉澶�,鐩存帴鎺ㄩ�� + this.PushGatewayOnlineStatuToForm(gwId, dicDbGateway[gwId].MqttOnlineStatus, hadGwOnline); + } + else + { + //浜戠涓嶅寘鍚殑,褰撲笉鍦ㄧ嚎澶勭悊 + this.PushGatewayOnlineStatuToForm(gwId, false, hadGwOnline); + } + } + } + + /// <summary> + /// 鑾峰彇鍓嶅洖缃戝叧鐨勫湪绾跨姸鎬� + /// </summary> + /// <returns></returns> + private Dictionary<string, bool> GetOldGatewayOnlineStatu() + { + if (this.dicGateway.Count == 0) + { + //娌℃湁缃戝叧,鍒欎笉澶勭悊 + return null; + } + + try + { + var dicOldOnline = new Dictionary<string, bool>(); + //濡傛灉鍦ㄥ惊鐜殑杩囩▼涓�,鍔ㄤ簡閲岄潰鐨勪笢瑗�,鎶ラ敊鍒欎笉鐞嗗畠,涓嬩竴鍥炲悎 + foreach (var zbway in this.dicGateway.Values) + { + //鑾峰彇鍓嶅洖鐨勫湪绾跨姸鎬� + dicOldOnline[zbway.GwId] = zbway.GatewayOnlineFlage; + } + return dicOldOnline; + } + catch { return null; } + } + + /// <summary> + /// 灏嗗彉鍖栫殑缃戝叧鎺ㄩ�佸埌鐣岄潰涓� + /// </summary> + /// <param name="gwId"></param> + /// <param name="online"></param> + /// <param name="hadGwOnline">2020.05.25杩藉姞:姝や綇瀹呮槸鍚︽嫢鏈夌綉鍏冲湪绾�</param> + private void PushGatewayOnlineStatuToForm(string gwId, bool online, bool hadGwOnline) + { + try + { + for (int i = 0; i < UserCenterResourse.listActionFormId.Count; i++) + { + string formId = UserCenterResourse.listActionFormId[i]; + if (UserCenterResourse.DicActionForm.ContainsKey(formId) == false) + { + continue; + } + //缃戝叧鍦ㄧ嚎鎺ㄩ�� + var zbway = this.GetLocalGateway(gwId); + if (zbway != null) + { + //鍒囨崲浣忓畢鏃�,杩欎釜涓滆タ鏈夊彲鑳芥槸null + zbway.GatewayOnlineFlage = online; + UserCenterResourse.DicActionForm[formId]?.GatewayOnlinePush(zbway, online, hadGwOnline); + } + } + } + catch { } + } + + /// <summary> + /// 璁剧疆瀛樺湪缃戝叧姝e湪鍗囩骇鐨勬爣璇� + /// </summary> + /// <param name="update">鏄惁鏈夌綉鍏冲湪鍗囩骇</param> + public void SetHadGatewayUpdateFlage(bool update) + { + this.hadGatewayUpdate = update; } #endregion - #region 鈻� 涓�鑸柟娉昣__________________________ - - /// <summary> - /// 寮瑰嚭缃戝叧涓嶅湪绾跨殑 - /// </summary> - private void ShowGatewayNotOnlineMsg() - { - Application.RunOnMainThread(() => - { - //缃戝叧杩炴帴澶辫触,璇风‘璁ょ綉缁� - string msg = Language.StringByID(R.MyInternationalizationString.uGatewayIsNotLinkAndCheckNetwork); - var control = new ShowMsgControl(ShowMsgType.Tip, msg); - control.Show(); - }); - } + #region 鈻� 涓�鑸柟娉昣__________________________ /// <summary> /// 鏄剧ず閿欒淇℃伅绐楀彛 -- Gitblit v1.8.0