From ac39680a5ace5c02c63369014993fbfc959dbe5f Mon Sep 17 00:00:00 2001 From: JLChen <551775569@qq.com> Date: 星期一, 24 二月 2020 09:09:10 +0800 Subject: [PATCH] 2020-02-24 1.优化更新。 --- Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs | 106 +++++++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 87 insertions(+), 19 deletions(-) diff --git a/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs b/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs index 2536317..b949444 100644 --- a/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs +++ b/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs @@ -21,6 +21,7 @@ static string checkGatewayTopicBase64 = ""; static RemoteMACInfo CurRemoteMACInfo = null; static MqttInfo mMqttInfo = null; + public static bool IsGatewayOnline = true; /// <summary> /// 鎵嬫満鏍囪瘑 @@ -46,6 +47,10 @@ InitMqtt (); } + /// <summary> + /// 淇濇椿閲嶈繛鍜岄噸璁㈤槄 绾跨▼ + /// </summary> + /// <returns></returns> public static async System.Threading.Tasks.Task InitMqtt () { new System.Threading.Thread (async () => { @@ -93,9 +98,15 @@ QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce }; + //缃戝叧鎺夌嚎涓婚 + var topicFilterNotifyGateWayOffline = new TopicFilter () { + Topic = $"/BusGateWayToClient/{CurRemoteMACInfo.macMark}/NotifyGateWayOffline", + QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce + }; + - var topicFilters = new TopicFilter [] { topicFilterCommon, topicFilterGateWayInfoChange, topicFilterNotifySqueeze }; + var topicFilters = new TopicFilter [] { topicFilterCommon, topicFilterGateWayInfoChange, topicFilterNotifySqueeze, topicFilterNotifyGateWayOffline }; var result = await RemoteMqttClient.SubscribeAsync (topicFilters); if (result.Items [0].ResultCode == MQTTnet.Client.Subscribing.MqttClientSubscribeResultCode.GrantedQoS2) { isSubscribeTopicSuccess = true; @@ -119,6 +130,10 @@ System.Console.WriteLine ($"============>MqttRemote涓诲姩鏂紑_{s}"); //await RemoteMqttClient.DisconnectAsync(new MQTTnet.Client.Disconnecting.MqttClientDisconnectOptions { }, CancellationToken.None); await RemoteMqttClient.DisconnectAsync (); + if (CommonPage.IsRemote) { + Utlis.ShowAppLinkStatus (AppLinkStatus.CloudUnlink); + } + } } catch (Exception e) { System.Console.WriteLine ($"============>MqttRemote鏂紑閫氳杩炴帴鍑哄紓甯�:{e.Message}"); @@ -146,21 +161,11 @@ public static async System.Threading.Tasks.Task StartCloudMqtt () { - //Application.RunOnMainThread (() => { - // if (5 < (DateTime.Now - dateTime).TotalSeconds) { - // return; - // } - // //MainPage.Loading.Start (Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.Connecting)); - // dateTime = DateTime.Now; - //}); if (!MainPage.LoginUser.IsLogin) { return; } //杩藉姞锛氭病鏈夎繙绋嬭繛鎺ョ殑鏉冮檺 - if (remoteMqttIsConnecting - || remoteIsConnected) { - //System.Console.WriteLine ($"============>MqttremoteMqttIsConnecting:{remoteMqttIsConnecting} remoteIsConnected:{remoteIsConnected} "); - + if (remoteMqttIsConnecting || remoteIsConnected) { return; } @@ -186,6 +191,8 @@ //2020-01-11 淇敼璁㈤槄涓婚鍦板潃 if (aesDecryptTopic == $"/BusGateWayToClient/{CurRemoteMACInfo.macMark}/NotifyBusGateWayInfoChange") {//缃戝叧涓婄嚎锛岄渶瑕佹洿鏂癮eskey //----绗簩姝ワ細璇诲彇璐﹀彿涓嬮潰鐨勭綉鍏冲垪琛� await ReceiveNotifyBusGateWayInfoChange (); + } else if (aesDecryptTopic == $"/BusGateWayToClient/{CurRemoteMACInfo.macMark}/NotifyGateWayOffline") {//缃戝叧鎺夌嚎 //----绗簩姝ワ細璇诲彇璐﹀彿涓嬮潰鐨勭綉鍏冲垪琛� + ReceiveNotifyGateWayOffline (); } else if (aesDecryptTopic == $"/BusGateWayToClient/{mMqttInfo.connEmqClientId}/Push/NotifySqueeze") {//璁㈤槄鎸や笅绾块棶棰� await ReceiveNotifySqueezeAsync (aesDecryptPayload); }else if (aesDecryptTopic == $"/BusGateWayToClient/{CurRemoteMACInfo.macMark}/Common/CheckGateway") { @@ -258,23 +265,39 @@ } if (RemoteMqttClient.ConnectedHandler == null) { RemoteMqttClient.UseConnectedHandler (async (e) => { + IfNeedReadAllDeviceStatus = true; - Shared.SimpleControl.Phone.UserMiddle.ReadAllDeviceStatus (); + System.Console.WriteLine ($"============>Mqtt杩滅▼杩炴帴鎴愬姛"); if (CommonPage.IsRemote) { Utlis.ShowAppLinkStatus (AppLinkStatus.CloudLink); } if (CurRemoteMACInfo != null) { - if (CurRemoteMACInfo.isValid == "InValid") { + IsGatewayOnline = CurRemoteMACInfo.isValid != "InValid"; + if (!IsGatewayOnline) { + //缃戝叧涓嶅湪绾� + if (CommonPage.IsRemote) { + Utlis.ShowAppLinkStatus (AppLinkStatus.CloudOffline); + } MainPage.AddTip ("Remote failed,gateway offline"); - //Application.RunOnMainThread (() => { - // MainPage.Loading.Hide (); - //}); } else { + //缃戝叧鍦ㄧ嚎 + + //閲嶆柊涓�娆℃墍鏈夎澶囩姸鎬� + IfNeedReadAllDeviceStatus = false; + Shared.SimpleControl.Phone.UserMiddle.ReadAllDeviceStatus (); + // MqttRemoteSend (new byte [] { 0 }, 3); } + + //if (CurRemoteMACInfo.isValid == "InValid") { + // MainPage.AddTip ("Remote failed,gateway offline"); + //} else { + // MqttRemoteSend (new byte [] { 0 }, 3); + //} } + }); } @@ -389,6 +412,11 @@ public static async Task MqttRemoteSend (byte [] message, int optionType = 0) { try { + if (!remoteIsConnected) { + System.Console.WriteLine ($"============>Mqtt 鏈繛鎺� 鍙栨秷鍙戦��"); + return; + } + var topicName = @"/" + MainPage.LoginUser.AccountString.ToLower () + @"/" + UserConfig.Instance.GatewayMAC.Replace (".", "") + @"/" + currentGuid; switch (optionType) { case 0: @@ -494,12 +522,33 @@ } - + /// <summary> + /// 鏄惁闇�瑕佽鍙栦竴娆℃墍鏈夎澶囩姸鎬� + /// </summary> + static bool IfNeedReadAllDeviceStatus = true; /// <summary> /// 鏀跺埌缃戝叧涓婄嚎娑堟伅 /// </summary> static async Task ReceiveNotifyBusGateWayInfoChange () { + System.Console.WriteLine ("============>Mqtt 缃戝叧涓婄嚎"); + IsGatewayOnline = true; + if (CommonPage.IsRemote) { + + if (MainPage.WiFiStatus != "CrabtreeAdd/CloudLink.png") { + Utlis.ShowAppLinkStatus (AppLinkStatus.CloudLink); + } +//#if DEBUG + MainPage.AddTip ("Gateway login online"); +//#endif + if (IfNeedReadAllDeviceStatus) { + IfNeedReadAllDeviceStatus = false; + Shared.SimpleControl.Phone.UserMiddle.ReadAllDeviceStatus (); + } + + + } + //褰撳墠浣忓畢涓嶆槸鍒嗕韩鏉� if (!UserConfig.Instance.CurrentRegion.IsOthreShare) { @@ -519,6 +568,24 @@ } } + /// <summary> + /// 鏀跺埌缃戝叧鎺夌嚎淇℃伅 + /// </summary> + static void ReceiveNotifyGateWayOffline () + { + System.Console.WriteLine ("============>Mqtt GateWayOffline"); + IsGatewayOnline = false; + if (CommonPage.IsRemote) { + Utlis.ShowAppLinkStatus (AppLinkStatus.CloudOffline); + MainPage.AddTip ("Remote failed,gateway offline"); + //if (MainPage.WiFiStatus != "CrabtreeAdd/CloudUnlink.png") { + // Utlis.ShowAppLinkStatus (AppLinkStatus.CloudUnlink); + //} + + } + } + + /// <summary> /// 鏀跺埌鎸や笅绾挎帹閫� @@ -541,11 +608,12 @@ MainPage.LoginUser.LastTime = DateTime.Now.AddDays (-30); MainPage.LoginUser.SaveUserInfo (); - DisConnectRemoteMqttClient ("LoginOut"); + DisConnectRemoteMqttClient ("鎸や笅绾�"); MainPage.ShowAlertOnMainThread (ErrorCode.LoginInAnotherDevice); Application.RunOnMainThread (() => { + MainPage.WiFiStatus = "CrabtreeAdd/WiFi.png"; UserMiddle.btnLinkStatus.UnSelectedImagePath = MainPage.WiFiStatus; new AccountLogin ().Show (); -- Gitblit v1.8.0