| | |
| | | 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 (); |
| | |
| | | |
| | | |
| | | |
| | | /// <summary>
|
| | | /// 断开远程Mqtt的链接
|
| | | /// <summary> |
| | | /// 断开远程Mqtt的链接 |
| | | /// </summary> |
| | | public static async System.Threading.Tasks.Task DisConnectRemoteMqttClient (string s = "")
|
| | | {
|
| | | try {
|
| | | if (remoteIsConnected) {
|
| | | remoteIsConnected = false;
|
| | | System.Console.WriteLine ($"============>MqttRemote主动断开_{s}");
|
| | | //await RemoteMqttClient.DisconnectAsync(new MQTTnet.Client.Disconnecting.MqttClientDisconnectOptions { }, CancellationToken.None);
|
| | | public static async System.Threading.Tasks.Task DisConnectRemoteMqttClient (string s = "") |
| | | { |
| | | try { |
| | | if (remoteIsConnected) { |
| | | remoteIsConnected = false; |
| | | 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); |
| | | } |
| | | |
| | | }
|
| | | System.Console.WriteLine ($"============>MqttRemoteDisConnectRemoteMqttClient"); |
| | | |
| | | } |
| | | } catch (Exception e) { |
| | | System.Console.WriteLine ($"============>MqttRemote断开通讯连接出异常:{e.Message}");
|
| | | }
|
| | | System.Console.WriteLine ($"============>MqttRemote断开通讯连接出异常:{e.Message}"); |
| | | } |
| | | } |
| | | static DateTime dateTime = DateTime.MinValue; |
| | | |
| | |
| | | } 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 { } |
| | | }); |
| | | } |
| | |
| | | if (RemoteMqttClient.ConnectedHandler == null) { |
| | | RemoteMqttClient.UseConnectedHandler (async (e) => { |
| | | IfNeedReadAllDeviceStatus = true; |
| | | |
| | | |
| | | System.Console.WriteLine ($"============>Mqtt远程连接成功"); |
| | | if (CommonPage.IsRemote) { |
| | |
| | | await DisConnectRemoteMqttClient ("StartRemoteMqtt"); |
| | | await RemoteMqttClient.ConnectAsync (options1); |
| | | remoteIsConnected = true; |
| | | IsDisConnectingWithSendCatch = false; |
| | | //await MqttRemoteSend (new byte [] { 0 }, 1); |
| | | //await MqttRemoteSend (new byte [] { 0 }, 2); |
| | | //await MqttRemoteSend (new byte [] { 0 }, 4); |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | ///// <summary> |
| | | ///// |
| | | ///// </summary> |
| | | ///// <param name="message">附加数据包</param> |
| | | ///// <param name="optionType">操作类型:0=网关控制;1=订阅网关数据;2=订阅网关上线数据;3=订阅网关是否在线主题 4=订阅挤下线主题 </param> |
| | | ///// <returns></returns> |
| | | //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: |
| | | // var messageSend = message; |
| | | // if (string.IsNullOrEmpty (mqttEncryptKey)) { |
| | | // topicName = $"/ClientToBusGateWay/{CurRemoteMACInfo.macMark}/Common/OldON"; |
| | | // } else { |
| | | // topicName = $"/ClientToBusGateWay/{CurRemoteMACInfo.macMark}/Common/NewON"; |
| | | // messageSend = Shared.Securitys.EncryptionService.AesEncryptPayload (message, mqttEncryptKey); |
| | | // } |
| | | // //base64加密 |
| | | // var m = new MqttApplicationMessage { Topic = topicName, Payload = messageSend, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce }; |
| | | // //if (remoteIsConnected) { |
| | | // try { |
| | | // RemoteMqttClient.PublishAsync (m); |
| | | // } catch (Exception e) { |
| | | // //await DisConnectRemoteMqttClient (e.Message); |
| | | // //await StartCloudMqtt (); |
| | | // //if (remoteIsConnected) { |
| | | // // RemoteMqttClient.PublishAsync (m); |
| | | // //} |
| | | // } |
| | | // //} |
| | | // break; |
| | | // case 3: |
| | | // topicName = $"/ClientToBusGateWay/{CurRemoteMACInfo.macMark}/Common/CheckGateway"; |
| | | |
| | | // var m1 = new MqttApplicationMessage { Topic = topicName, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce }; |
| | | |
| | | // try { |
| | | // Console.WriteLine ("CheckGateway"); |
| | | // RemoteMqttClient.PublishAsync (m1); |
| | | // } catch (Exception e) { |
| | | // Console.WriteLine ($"CheckGateway Fail:{e.Message}"); |
| | | // //await DisConnectRemoteMqttClient (e.Message); |
| | | // //await StartCloudMqtt (); |
| | | // } |
| | | // break; |
| | | // } |
| | | |
| | | |
| | | // } catch (Exception e) { |
| | | |
| | | // } |
| | | //} |
| | | |
| | | /// <summary> |
| | | /// |
| | | /// </summary> |
| | | /// <param name="message">附加数据包</param> |
| | | /// <param name="optionType">操作类型:0=网关控制;1=订阅网关数据;2=订阅网关上线数据;3=订阅网关是否在线主题 4=订阅挤下线主题 </param> |
| | | /// <param name="optionType">操作类型:0=网关控制;1=订阅网关数据;2=订阅网关上线数据</param> |
| | | /// <returns></returns> |
| | | public static async Task MqttRemoteSend (byte [] message, int optionType = 0) |
| | | { |
| | | try { |
| | | if (!remoteIsConnected) { |
| | | System.Console.WriteLine ($"============>Mqtt 未连接 取消发送"); |
| | | return; |
| | | } |
| | | //if (!remoteIsConnected) { |
| | | // System.Console.WriteLine ($"============>Mqtt 未连接 取消发送"); |
| | | // return; |
| | | //} |
| | | |
| | | var topicName = @"/" + MainPage.LoginUser.AccountString.ToLower () + @"/" + UserConfig.Instance.GatewayMAC.Replace (".", "") + @"/" + currentGuid; |
| | | string topicName; |
| | | switch (optionType) { |
| | | case 0: |
| | | var messageSend = message; |
| | | if (string.IsNullOrEmpty (mqttEncryptKey)) { |
| | | topicName = $"/ClientToBusGateWay/{CurRemoteMACInfo.macMark}/Common/OldON"; |
| | | } else { |
| | | topicName = $"/ClientToBusGateWay/{CurRemoteMACInfo.macMark}/Common/NewON"; |
| | | messageSend = Shared.Securitys.EncryptionService.AesEncryptPayload (message, mqttEncryptKey); |
| | | topicName = $"/ClientToBusGateWay/{CurRemoteMACInfo.macMark}/Common/ON"; |
| | | if (!string.IsNullOrEmpty (mqttEncryptKey)) { |
| | | message = Shared.Securitys.EncryptionService.AesEncryptPayload (message, mqttEncryptKey); |
| | | } |
| | | //base64加密 |
| | | var m = new MqttApplicationMessage { Topic = topicName, Payload = messageSend, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce }; |
| | | //if (remoteIsConnected) { |
| | | try { |
| | | RemoteMqttClient.PublishAsync (m); |
| | | } catch (Exception e) { |
| | | //await DisConnectRemoteMqttClient (e.Message); |
| | | //await StartCloudMqtt (); |
| | | //if (remoteIsConnected) { |
| | | // RemoteMqttClient.PublishAsync (m); |
| | | //} |
| | | } |
| | | //} |
| | | await RemoteMqttClient.PublishAsync (new MqttApplicationMessage { Topic = topicName, Payload = message, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce }); |
| | | break; |
| | | case 3: |
| | | topicName = $"/ClientToBusGateWay/{CurRemoteMACInfo.macMark}/Common/CheckGateway"; |
| | | |
| | | var m1 = new MqttApplicationMessage { Topic = topicName, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce }; |
| | | |
| | | try { |
| | | Console.WriteLine ("CheckGateway"); |
| | | RemoteMqttClient.PublishAsync (m1); |
| | | } catch (Exception e) { |
| | | Console.WriteLine ($"CheckGateway Fail:{e.Message}"); |
| | | //await DisConnectRemoteMqttClient (e.Message); |
| | | //await StartCloudMqtt (); |
| | | } |
| | | //Console.WriteLine ("CheckGateway"); |
| | | await RemoteMqttClient.PublishAsync (new MqttApplicationMessage { Topic = topicName, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce }); |
| | | break; |
| | | } |
| | | |
| | | |
| | | } catch (Exception e) { |
| | | // System.Console.WriteLine ($"============>Mqtt MqttRemoteSend catch"); |
| | | if (!IsDisConnectingWithSendCatch) { |
| | | IsDisConnectingWithSendCatch = true; |
| | | await DisConnectRemoteMqttClient ("MqttRemoteSendCatch"); |
| | | } |
| | | |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// SendCatch 后执行一次断开操作 |
| | | /// </summary> |
| | | static bool IsDisConnectingWithSendCatch = false; |
| | | |
| | | |
| | | |
| | | /// <summary> |
| | | /// 分享住宅 获取当前住宅网关信息并且连接MQTT 或者刷新 |
| | |
| | | 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> |