| | |
| | | static MqttCommon () |
| | | { |
| | | InitMqtt (); |
| | | InitCheckGateway (); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | { |
| | | new System.Threading.Thread (async () => { |
| | | while (true) { |
| | | System.Threading.Thread.Sleep (100); |
| | | if (!CommonPage.IsRemote) continue; |
| | | try { |
| | | System.Threading.Thread.Sleep (100); |
| | | if (!CommonPage.IsRemote) continue; |
| | | |
| | | await StartCloudMqtt (); |
| | | await CheckingSubscribeTopics (); |
| | | await StartCloudMqtt (); |
| | | await CheckingSubscribeTopics (); |
| | | } catch { } |
| | | |
| | | } |
| | | }) { IsBackground = true }.Start (); |
| | |
| | | } else if (aesDecryptTopic == $"/BusGateWayToClient/{mMqttInfo.connEmqClientId}/Push/NotifySqueeze") {//订阅挤下线问题 |
| | | await ReceiveNotifySqueezeAsync (aesDecryptPayload); |
| | | }else if (aesDecryptTopic == $"/BusGateWayToClient/{CurRemoteMACInfo.macMark}/Common/CheckGateway") { |
| | | MainPage.WiFiStatus = "CrabtreeAdd/CloudUnlink.png"; |
| | | // = $"/ClientToBusGateWay/{CurRemoteMACInfo.macMark}/Common/OldON"; |
| | | var ss = CommonPage.MyEncodingUTF8.GetString (aesDecryptPayload); |
| | | var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponsePack> (ss); |
| | | if (obj == null) { |
| | | return; |
| | | } |
| | | switch (obj.StateCode) { |
| | | case "HDLUdpDataForwardServerMqttClientNoOnLine": |
| | | case "NoOnline": |
| | | case "NetworkAnomaly"://不在线 |
| | | MainPage.AddTip ("Gateway offline"); |
| | | //Application.RunOnMainThread (() => { |
| | | // Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.DelColor; |
| | | //}); |
| | | break; |
| | | case "NoRecord"://MAC不正确 |
| | | MainPage.AddTip (Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.MACError)); |
| | | //Application.RunOnMainThread (() => { |
| | | // Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.DelColor; |
| | | //}); |
| | | break; |
| | | case "Success": |
| | | MainPage.AddTip (UserConfig.Instance.CurrentRegion.Name + ":" + Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.LinkSuccess)); |
| | | MainPage.WiFiStatus = "CrabtreeAdd/CloudLink.png"; |
| | | break; |
| | | default: |
| | | MainPage.AddTip (Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.LinkLoser)); |
| | | //Application.RunOnMainThread (() => { |
| | | // Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.DelColor; |
| | | //}); |
| | | break; |
| | | } |
| | | Application.RunOnMainThread (() => { |
| | | UserMiddle.btnLinkStatus.UnSelectedImagePath = MainPage.WiFiStatus; |
| | | }); |
| | | ReceiveCheckGatewayTopic (); |
| | | } else { |
| | | SetGatewayOnlineResetCheck (); |
| | | |
| | | if (!string.IsNullOrEmpty (mqttEncryptKey)) { |
| | | aesDecryptPayload = Shared.Securitys.EncryptionService.AesDecryptPayload (e.ApplicationMessage.Payload, mqttEncryptKey); |
| | | } |
| | | |
| | | var packet = new Packet (); |
| | | packet.IsLocal = false; |
| | | packet.Bytes = aesDecryptPayload; |
| | | packet.Manager (); |
| | | |
| | | } |
| | | var packet = new Packet (); |
| | | packet.IsLocal = false; |
| | | packet.Bytes = aesDecryptPayload; |
| | | packet.Manager (); |
| | | |
| | | } catch { } |
| | | }); |
| | | } |
| | |
| | | break; |
| | | case 3: |
| | | topicName = $"/ClientToBusGateWay/{CurRemoteMACInfo.macMark}/Common/CheckGateway"; |
| | | Console.WriteLine ("CheckGateway"); |
| | | //Console.WriteLine ("CheckGateway"); |
| | | await RemoteMqttClient.PublishAsync (new MqttApplicationMessage { Topic = topicName, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce }); |
| | | break; |
| | | } |
| | |
| | | static async Task ReceiveNotifyBusGateWayInfoChange () |
| | | { |
| | | System.Console.WriteLine ("============>Mqtt 网关上线"); |
| | | IsGatewayOnline = true; |
| | | SetGatewayOnlineResetCheck (); |
| | | if (CommonPage.IsRemote) { |
| | | |
| | | if (MainPage.WiFiStatus != "CrabtreeAdd/CloudLink.png") { |
| | | Utlis.ShowAppLinkStatus (AppLinkStatus.CloudLink); |
| | | } |
| | | //#if DEBUG |
| | | MainPage.AddTip ("Gateway login online"); |
| | | //#endif |
| | |
| | | IfNeedReadAllDeviceStatus = false; |
| | | Shared.SimpleControl.Phone.UserMiddle.ReadAllDeviceStatus (); |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | //当前住宅不是分享来 |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | static int CheckGatewayCount = 0; |
| | | static DateTime mCheckGatewayTime; |
| | | |
| | | /// <summary> |
| | | /// 设置网关在线标志,并重置CheckGateway参数 |
| | | /// </summary> |
| | | static void SetGatewayOnlineResetCheck () |
| | | { |
| | | IsGatewayOnline = true; |
| | | mCheckGatewayTime = DateTime.Now; |
| | | CheckGatewayCount = 0; |
| | | if (CommonPage.IsRemote) { |
| | | if (MainPage.WiFiStatus != "CrabtreeAdd/CloudLink.png") { |
| | | Utlis.ShowAppLinkStatus (AppLinkStatus.CloudLink); |
| | | } |
| | | } |
| | | } |
| | | |
| | | //static void TipGatewayOffline () { |
| | | |
| | | //} |
| | | |
| | | /// <summary> |
| | | /// 定时检测网关是否在线 |
| | | /// 1.线程休眠间隔5s |
| | | /// 2.发送CheckGateway间隔为9S,收到主题刷新该时间,重置发送次数 |
| | | /// 3.发送次数到达3次以上,则判定网关为离线状态 |
| | | /// </summary> |
| | | /// <returns></returns> |
| | | static void InitCheckGateway () |
| | | { |
| | | new System.Threading.Thread (async () => { |
| | | while (true) { |
| | | try { |
| | | |
| | | if (CommonPage.IsRemote && remoteIsConnected) { |
| | | if (CheckGatewayCount >= 3) { |
| | | //连续3次没回复,判定网关超时 |
| | | if (MainPage.WiFiStatus == "CrabtreeAdd/CloudLink.png") { |
| | | IsGatewayOnline = false; |
| | | Utlis.ShowAppLinkStatus (AppLinkStatus.CloudOffline); |
| | | MainPage.AddTip ("Gateway offline!"); |
| | | } |
| | | } |
| | | |
| | | if (mCheckGatewayTime.AddSeconds (10).Ticks <= System.DateTime.Now.Ticks) { |
| | | mCheckGatewayTime = DateTime.Now; |
| | | //CheckGateway |
| | | MqttRemoteSend (new byte [] { }, 3); |
| | | CheckGatewayCount++; |
| | | #if DEBUG |
| | | Console.WriteLine ("CheckGateway"); |
| | | #endif |
| | | |
| | | |
| | | } |
| | | } |
| | | |
| | | System.Threading.Thread.Sleep (5500); |
| | | } catch { } |
| | | |
| | | } |
| | | }) { IsBackground = true }.Start (); |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// 收到CheckGateway主题 |
| | | /// </summary> |
| | | static void ReceiveCheckGatewayTopic () { |
| | | //System.Console.WriteLine ("============>Mqtt CheckGateway网关回复"); |
| | | SetGatewayOnlineResetCheck (); |
| | | |
| | | |
| | | |
| | | //旧网关方法 |
| | | //MainPage.WiFiStatus = "CrabtreeAdd/CloudUnlink.png"; |
| | | //// = $"/ClientToBusGateWay/{CurRemoteMACInfo.macMark}/Common/OldON"; |
| | | //var ss = CommonPage.MyEncodingUTF8.GetString (aesDecryptPayload); |
| | | //var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponsePack> (ss); |
| | | //if (obj == null) { |
| | | // return; |
| | | //} |
| | | //switch (obj.StateCode) { |
| | | //case "HDLUdpDataForwardServerMqttClientNoOnLine": |
| | | //case "NoOnline": |
| | | //case "NetworkAnomaly"://不在线 |
| | | // MainPage.AddTip ("Gateway offline"); |
| | | // //Application.RunOnMainThread (() => { |
| | | // // Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.DelColor; |
| | | // //}); |
| | | // break; |
| | | //case "NoRecord"://MAC不正确 |
| | | // MainPage.AddTip (Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.MACError)); |
| | | // //Application.RunOnMainThread (() => { |
| | | // // Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.DelColor; |
| | | // //}); |
| | | // break; |
| | | //case "Success": |
| | | // MainPage.AddTip (UserConfig.Instance.CurrentRegion.Name + ":" + Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.LinkSuccess)); |
| | | // MainPage.WiFiStatus = "CrabtreeAdd/CloudLink.png"; |
| | | // break; |
| | | //default: |
| | | // MainPage.AddTip (Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.LinkLoser)); |
| | | // //Application.RunOnMainThread (() => { |
| | | // // Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.DelColor; |
| | | // //}); |
| | | // break; |
| | | //} |
| | | //Application.RunOnMainThread (() => { |
| | | // UserMiddle.btnLinkStatus.UnSelectedImagePath = MainPage.WiFiStatus; |
| | | //}); |
| | | } |
| | | |
| | | |
| | | |
| | | /// <summary> |
| | | /// 收到挤下线推送 |
| | | /// </summary> |