From 5a5a2b696866f947b6025d26c3302e8ffef46435 Mon Sep 17 00:00:00 2001 From: JLChen <551775569@qq.com> Date: 星期一, 11 一月 2021 16:11:20 +0800 Subject: [PATCH] 2021-01-11 1.更新MQTT连接和本地搜索网关方法。2.子账号相关接口完善 --- Crabtree/SmartHome/UI/SimpleControl/EquipmentPublicClass.cs | 260 +++++++++++++++++++++++++++------------------------ 1 files changed, 139 insertions(+), 121 deletions(-) diff --git a/Crabtree/SmartHome/UI/SimpleControl/EquipmentPublicClass.cs b/Crabtree/SmartHome/UI/SimpleControl/EquipmentPublicClass.cs index 0013c38..22c41e1 100644 --- a/Crabtree/SmartHome/UI/SimpleControl/EquipmentPublicClass.cs +++ b/Crabtree/SmartHome/UI/SimpleControl/EquipmentPublicClass.cs @@ -1,207 +1,225 @@ using System; using Shared.SimpleControl.Phone; -using System.Collections.Generic; -using System.Text; -using System.Net.NetworkInformation; namespace Shared.SimpleControl { public class EquipmentPublicClass { - /* - - */ + + static bool BSaveGateWay = true; + public void AlertNotOnline () { new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.TipEquipmentNotOnline), Language.StringByID (R.MyInternationalizationString.Close)).Show (); } static bool isConnectiong = false; - + static DateTime mFlagDateTime; /// <summary> /// 妫�娴�/杩炴帴杩滅▼ /// </summary> public static void CheckLinkRemote (int status, bool IsReconnectRemote = true) { - //if (isConnectiong) { - // return; - //} - //isConnectiong = true; - UserConfig.Instance.internetStatus = status; -#if wallon + + //20S鍚庡己鍒堕噸缃甶sConnectiong鐘舵�侊紝闃叉isConnectiong涓�鐩翠负true鐘舵�� + if (mFlagDateTime.AddSeconds (20).Ticks <= System.DateTime.Now.Ticks) { + mFlagDateTime = DateTime.Now; + if (isConnectiong) { + Shared.Utlis.WriteLine ("20s銆媔sConnectiong true "); + isConnectiong = false; + } + } + + if (isConnectiong) { return; -#endif - if (MainPage.LoginUser == null || !MainPage.LoginUser.IsLogin || isConnectiong) { + } + isConnectiong = true; + UserConfig.Instance.internetStatus = status; + + if (MainPage.LoginUser == null || !MainPage.LoginUser.IsLogin) { + isConnectiong = false; return; } - isConnectiong = true; +#if DEBUG + Shared.Utlis.WriteLine ($"CheckLinkRemote: 寮�濮�"); +#endif + new System.Threading.Thread (async () => { + bool IsRemoteLoadingShow = false; + //Control.IsCanLocal = false; try { if (status == 0) { + Utlis.ShowAppLinkStatus (AppLinkStatus.CloudOffline); Application.RunOnMainThread (() => { MainPage.AddTip (Language.StringByID (R.MyInternationalizationString.InternetStatusTip)); - MainPage.WiFiStatus = "CrabtreeAdd/WiFiUnlink.png"; - UserMiddle.btnLinkStatus.UnSelectedImagePath = MainPage.WiFiStatus; - }); - - await DisConnectRemoteAsync (); - + if (CommonPage.IsRemote) { + SmartHome.MqttCommon.DisConnectRemote ("NO net", false); + } } else { - //Application.RunOnMainThread (() => { - // if (UserMiddle.LinkStatusTip.BackgroundColor == SkinStyle.Current.DelColor) { - // UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.MainColor; - // } - //}); - var localFileList = IO.FileUtils.ReadFiles (); - var gateWayList = localFileList.FindAll ((obj) => { - return (obj.StartsWith ("Equipment_")) && ( - obj.Split ('_') [1].ToString () == DeviceType.OnePortBus.ToString () || - obj.Split ('_') [1].ToString () == DeviceType.RCU.ToString () || - obj.Split ('_') [1].ToString () == DeviceType.SuperWireless.ToString () || - obj.Split ('_') [1].ToString () == DeviceType.OnePortWirelessFR.ToString () || - obj.Split ('_') [1].ToString () == DeviceType.OnePortMqttFR.ToString ()); - }); - bool canRemote = false; - - if (gateWayList == null || gateWayList.Count == 0) { + #region 鏂版柟妗� + var mHomeGateway = UserConfig.Instance.HomeGateway; + //1.褰撳墠浣忓畢浜戠杩樻病缁戝畾缃戝叧 + if (mHomeGateway == null) { Utlis.ShowAppLinkStatus (AppLinkStatus.WiFiOffline); - await DisConnectRemoteAsync (); + DisConnectRemoteAsync (); } - + //2.杩滅▼杩炴帴鎯呭喌涓嬶紝鏄惁闇�瑕佹柇寮�杩炴帴閲嶈繛 if (IsReconnectRemote) { if (CommonPage.IsRemote) { CommonPage.IsRemote = false; Utlis.ShowAppLinkStatus (AppLinkStatus.WiFiOffline); - await SmartHome.MqttCommon.DisConnectRemoteMqttClient (); + SmartHome.MqttCommon.DisConnectRemote ("IsReconnectRemote"); } } + bool canRemote = false; if (CommonPage.IsRemote) { - //await SmartHome.MqttCommon.StartCloudMqtt (); SmartHome.MqttCommon.MqttRemoteSend (new byte [] { }, 3); } + //褰撳墠浣忓畢浜戠鏄惁缁戝畾缃戝叧锛岄粯璁ゆ病鏈� + bool HomeGatewaysExists = false; + SmartHome.MqttCommon.GatewayCommon = null; + if (mHomeGateway != null) { + //(1).褰撳墠浣忓畢浜戠宸茬粡缁戝畾缃戝叧 + HomeGatewaysExists = true; + //(2).妯℃嫙鐢熸垚涓�涓綉鍏冲璞� + SmartHome.MqttCommon.GatewayCommon = new OnePortBus () { + SubnetID = (byte)mHomeGateway.subnetId, + DeviceID = (byte)mHomeGateway.deviceId, + MAC = mHomeGateway.mac, + Remote_IP1 = "115.29.251.24", + Remote_IP1_Prot = "9999", + Name = "OnePortBus", + obj1 = 1, + }; + canRemote = true; + //(3).褰撳墠Wi-Fi杩炴帴锛屽弬璇曟湰鍦版悳绱㈢綉鍏� + if (status == 2) { + //var result = Control.ControlBytesSendHasReturn (Command.ReadGateway, common.SubnetID, common.DeviceID, new byte [] { (byte)new Random ().Next (255), (byte)new Random ().Next (255) }); + #region + //寮�濮嬫湰鍦版悳绱㈢綉鍏�... + //MainPage.AddTip (Language.StringByID (R.MyInternationalizationString.SearchingGatewayLocally)); + MainPage.LoadingStart (Language.StringByID (R.MyInternationalizationString.SearchingGatewayLocally)); - //鏈湴鏄惁瀛樺湪涓庡綋鍓嶄綇瀹呯粦瀹氱綉鍏崇殑鏁版嵁 - bool HomeGatewaysExistsLocally = false; - foreach (var gatewayFileName in gateWayList) { - var tempStrings = gatewayFileName.Split ('_'); - var gateWayString = CommonPage.MyEncodingUTF8.GetString (IO.FileUtils.ReadFile (gatewayFileName)); - var common = Newtonsoft.Json.JsonConvert.DeserializeObject<GatewayBase> (gateWayString); - if (common != null) { - if (common.MAC.Replace (".", "") == UserConfig.Instance.GatewayMAC.Replace (".", "")) { - HomeGatewaysExistsLocally = true; - canRemote = true; - if (status == 2) { - //var result = Control.ControlBytesSendHasReturn (Command.ReadGateway, common.SubnetID, common.DeviceID, new byte [] { (byte)new Random ().Next (255), (byte)new Random ().Next (255) }); - #region - var control = new Control (); - control.Send (new Target () { - IPEndPoint = new System.Net.IPEndPoint (System.Net.IPAddress.Parse (new Net.NetWiFi ().BroadcastIpAddress.ToString ()), 6000), - Command = Command.ReadGateway, - SubnetID = common.SubnetID, - DeviceID = common.DeviceID, - AddData = new byte [] { (byte)new Random ().Next (255), (byte)new Random ().Next (255) }, - }, SendCount.Three, true, true); - var result = control.UsefulBytes; + var control = new Control (); + control.Send (new Target () { + IPEndPoint = new System.Net.IPEndPoint (System.Net.IPAddress.Parse (new Net.NetWiFi ().BroadcastIpAddress.ToString ()), 6000), + Command = Command.ReadGateway, + SubnetID = SmartHome.MqttCommon.GatewayCommon.SubnetID, + DeviceID = SmartHome.MqttCommon.GatewayCommon.DeviceID, + AddData = new byte [] { (byte)new Random ().Next (255), (byte)new Random ().Next (255) }, + }, SendCount.Three, true, true); + var result = control.UsefulBytes; - #endregion - if (result != null) { - canRemote = false; - var mac0 = CommonPage.byteToHex16 (result [5]) + "." + CommonPage.byteToHex16 (result [6]) + "." + CommonPage.byteToHex16 (result [7]) + "." + CommonPage.byteToHex16 (result [8]) + "." + CommonPage.byteToHex16 (result [9]) + "." + CommonPage.byteToHex16 (result [10]) + "." + CommonPage.byteToHex16 (result [11]) + "." + CommonPage.byteToHex16 (result [12]); - if (common.MAC == mac0) { - CommonPage.IsRemote = false; - Application.RunOnMainThread (() => { - MainPage.WiFiStatus = "CrabtreeAdd/WiFi.png"; - UserMiddle.btnLinkStatus.UnSelectedImagePath = MainPage.WiFiStatus; - MainPage.Loading.Hide (); - }); - Shared.SimpleControl.Phone.UserMiddle.ReadAllDeviceStatus (); - await SmartHome.MqttCommon.DisConnectRemoteMqttClient (); - return; - } - } - } else { - break; + #endregion + if (result != null) { + var mac0 = CommonPage.byteToHex16 (result [5]) + "." + CommonPage.byteToHex16 (result [6]) + "." + CommonPage.byteToHex16 (result [7]) + "." + CommonPage.byteToHex16 (result [8]) + "." + CommonPage.byteToHex16 (result [9]) + "." + CommonPage.byteToHex16 (result [10]) + "." + CommonPage.byteToHex16 (result [11]) + "." + CommonPage.byteToHex16 (result [12]); + if (SmartHome.MqttCommon.GatewayCommon.MAC.Replace (".", "").Replace (":", "").ToUpper () == mac0.Replace (".", "").Replace (":", "").ToUpper ()) { + //鎼滅储鎴愬姛锛屽惎鐢ㄦ湰鍦拌繛鎺� + //Control.IsCanLocal = true;//鏈湴鎼滅储鎴愬姛锛屽厑璁告湰鍦伴�氫俊 + canRemote = false; + Shared.SimpleControl.CommonPage.IsRemote = false; + Application.RunOnMainThread (() => { + Utlis.ShowAppLinkStatus (AppLinkStatus.WiFi); + MainPage.Loading.Hide (); + }); + UserMiddle.ReadAllDeviceStatus (); + await SmartHome.MqttCommon.DisConnectRemote ("WIFI", false); + MainPage.AddTip (Language.StringByID (R.MyInternationalizationString.SearchGatewaySuccess)); } + } else { + //鎼滅储澶辫触锛屽惎鐢ㄨ繙绋嬭繛鎺� + MainPage.AddTip (Language.StringByID (R.MyInternationalizationString.SearchGatewayFail)); + } + } + //(3).鏈湴鎼滅储涓嶅埌缃戝叧鎴栬�呮病杩炴帴Wi-Fi鎯呭喌涓嬶紝妫�娴嬫槸鍚︽嫢鏈夊綋鍓嶄綇瀹呯殑杩滅▼鏉冮檺 + if (canRemote) { + if (!UserConfig.Instance.AllowRemoteControl) { + //娌℃湁褰撳墠浣忓畢鐨勮繙绋嬫潈闄� + canRemote = false; + MainPage.AddTip (Language.StringByID (R.MyInternationalizationString.NoRemoteControlPermission)); } } } - - - //鑳借繙绋� + //5.鑳借繙绋� if (canRemote) { if (!CommonPage.IsRemote) { - if (UserConfig.Instance.CheckHomeGateways ()) { - if (!string.IsNullOrEmpty (UserConfig.Instance.CurrentRegion.HomeGateways [0].GatewayUniqueId)) { + if (UserConfig.Instance.CheckHomeGatewaysNotEmpty ()) { + if (!string.IsNullOrEmpty (UserConfig.Instance.GatewayMAC)) { + // 5.1鑳借繙绋�,鏄剧ずCloudUnlink鐘舵�侊紝IsRemote璁句负true寮�濮嬭繙绋� + //MainPage.AddTip (Language.StringByID (R.MyInternationalizationString.StartEnablingRmote)); + MainPage.LoadingTipShow (Language.StringByID (R.MyInternationalizationString.StartEnablingRmote)); Utlis.ShowAppLinkStatus (AppLinkStatus.CloudUnlink); + IsRemoteLoadingShow = true; CommonPage.IsRemote = true; - SmartHome.MqttCommon.IsGatewayOnline = false; - //await SmartHome.MqttCommon.StartCloudMqtt (); + SmartHome.MqttCommon.InitState (); } } } } else { - - //涓嶈兘杩滅▼锛屽鏋滀箣鍓嶈繙绋嬫ā寮忓垯鏂紑MQTT杩炴帴,鏄剧ず涓烘湭杩炴帴 + //6.涓嶈兘杩滅▼锛屽鏋滀箣鍓嶈繙绋嬫ā寮忓垯鏂紑MQTT杩炴帴,鏄剧ず涓烘湭杩炴帴 if (CommonPage.IsRemote) { Utlis.ShowAppLinkStatus (AppLinkStatus.WiFiOffline); CommonPage.IsRemote = false; - await SmartHome.MqttCommon.DisConnectRemoteMqttClient (); - + await SmartHome.MqttCommon.DisConnectRemote ("", false); } - - if (!HomeGatewaysExistsLocally) { + //鏈湴涓嶅瓨鍦ㄤ笌褰撳墠浣忓畢缁戝畾缃戝叧鐨勬暟鎹� + if (!HomeGatewaysExists) { Utlis.ShowAppLinkStatus (AppLinkStatus.WiFiOffline); } - } - - - //if (canRemote && !CommonPage.IsRemote) { - // if (UserConfig.Instance.CheckHomeGateways ()) { - // if (!string.IsNullOrEmpty (UserConfig.Instance.CurrentRegion.HomeGateways [0].GatewayUniqueId)) { - // Utlis.ShowAppLinkStatus (AppLinkStatus.CloudUnlink); - // Shared.SimpleControl.CommonPage.IsRemote = true; - // //await SmartHome.MqttCommon.StartCloudMqtt (); - // } - // } - //} - - - } + #endregion } catch (Exception ex) { - Console.WriteLine ("CheckLinkRemote : " + ex.ToString ()); + Shared.Utlis.WriteLine ("CheckLinkRemote : " + ex.ToString ()); } finally { isConnectiong = false; + //if (!IsRemoteLoadingShow) { + // Application.RunOnMainThread (() => { + // MainPage.Loading.Hide (); + // }); + //} + Application.RunOnMainThread (() => { MainPage.Loading.Hide (); }); + + ////涓嶆槸杩滅▼ 骞朵笖鏈湴鎼滅储涓嶅埌鍖归厤鐨勭綉鍏冲氨鎻愮ず绂佹鏈湴鍙戦�� + //if (!CommonPage.IsRemote && !Control.IsCanLocal && status != 0) { + // Application.RunOnMainThread (() => { + // Utlis.ShowAppLinkStatus (AppLinkStatus.WiFiUnlink); + // //鏃犳硶鏈湴閫氫俊銆傚綋鍓嶄綇瀹呬簯绔病鏈夌粦瀹氱綉鍏炽�佹垨鑰呭綋鍓嶄綇瀹呮湰鍦版病鏈夌綉鍏虫暟鎹紒 + // //MainPage.AddTip (Language.StringByID (R.MyInternationalizationString.DisableLocalCommunication)); + // //new Alert (Language.StringByID (R.MyInternationalizationString.Tip), + // // Language.StringByID (R.MyInternationalizationString.DisableLocalCommunication), + // // Language.StringByID (R.MyInternationalizationString.Close)).Show (); + // }); + //} + +#if DEBUG + Shared.Utlis.WriteLine ($"CheckLinkRemote: 缁撴潫"); +#endif } }) { IsBackground = true }.Start (); } - - /// <summary> /// 鍏抽棴杩滅▼杩炴帴妯″紡锛屽苟鏂紑MQTT /// </summary> - /// <returns></returns> - static async System.Threading.Tasks.Task DisConnectRemoteAsync (){ + /// <returns></returns> + static async System.Threading.Tasks.Task DisConnectRemoteAsync () + { if (CommonPage.IsRemote) { CommonPage.IsRemote = false; - await SmartHome.MqttCommon.DisConnectRemoteMqttClient (); + await SmartHome.MqttCommon.DisConnectRemote (); } } } -} - - - +} \ No newline at end of file -- Gitblit v1.8.0