| | |
| | | try { |
| | | if (remoteIsConnected) { |
| | | remoteIsConnected = false; |
| | | System.Console.WriteLine ($"============>MqttRemote主动断开_{s}"); |
| | | Utlis.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"); |
| | | Utlis.WriteLine ($"============>MqttRemoteDisConnectRemoteMqttClient"); |
| | | |
| | | } |
| | | } catch (Exception e) { |
| | | System.Console.WriteLine ($"============>MqttRemote断开通讯连接出异常:{e.Message}"); |
| | | Utlis.WriteLine ($"============>MqttRemote断开通讯连接出异常:{e.Message}"); |
| | | } |
| | | } |
| | | static DateTime dateTime = DateTime.MinValue; |
| | |
| | | } |
| | | var aesDecryptTopic = e.ApplicationMessage.Topic; |
| | | var aesDecryptPayload = e.ApplicationMessage.Payload; |
| | | //Console.WriteLine ("Topic={0}", aesDecryptTopic); |
| | | //Utlis.WriteLine ("Topic={0}", aesDecryptTopic); |
| | | |
| | | //if (aesDecryptTopic == $"NotifyBusGateWayInfoChagne/{CurRemoteMACInfo.md5_mac_string}") {//网关上线,需要更新aeskey |
| | | //2020-01-11 修改订阅主题地址 |
| | |
| | | |
| | | if (RemoteMqttClient.DisconnectedHandler == null) { |
| | | RemoteMqttClient.UseDisconnectedHandler (async (e) => { |
| | | System.Console.WriteLine ($"============>Mqtt远程连接断开"); |
| | | Utlis.WriteLine ($"============>Mqtt远程连接断开"); |
| | | isSubscribeTopicSuccess = false; |
| | | await DisConnectRemoteMqttClient ("StartRemoteMqtt.DisconnectedHandler"); |
| | | //await StartRemoteMqtt(); |
| | |
| | | RemoteMqttClient.UseConnectedHandler (async (e) => { |
| | | IfNeedReadAllDeviceStatus = true; |
| | | |
| | | System.Console.WriteLine ($"============>Mqtt远程连接成功"); |
| | | Utlis.WriteLine ($"============>Mqtt远程连接成功"); |
| | | if (CommonPage.IsRemote) { |
| | | Utlis.ShowAppLinkStatus (AppLinkStatus.CloudLink); |
| | | } |
| | |
| | | await MQTTConnectAsync (); |
| | | |
| | | } else { |
| | | System.Console.WriteLine ("============>Mqtt 取消连接,当前住宅没绑定网关"); |
| | | Utlis.WriteLine ("============>Mqtt 取消连接,当前住宅没绑定网关"); |
| | | } |
| | | |
| | | } |
| | |
| | | } |
| | | |
| | | } catch (Exception ex) { |
| | | System.Console.WriteLine ($"============>Mqtt 远程连接通讯连接出异常:{ex.Message}"); |
| | | Utlis.WriteLine ($"============>Mqtt 远程连接通讯连接出异常:{ex.Message}"); |
| | | } finally { |
| | | //最终要释放连接状态 |
| | | remoteMqttIsConnecting = false; |
| | |
| | | //{ |
| | | // try { |
| | | // if (!remoteIsConnected) { |
| | | // System.Console.WriteLine ($"============>Mqtt 未连接 取消发送"); |
| | | // Utlis.WriteLine ($"============>Mqtt 未连接 取消发送"); |
| | | // return; |
| | | // } |
| | | |
| | |
| | | // var m1 = new MqttApplicationMessage { Topic = topicName, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce }; |
| | | |
| | | // try { |
| | | // Console.WriteLine ("CheckGateway"); |
| | | // Utlis.WriteLine ("CheckGateway"); |
| | | // RemoteMqttClient.PublishAsync (m1); |
| | | // } catch (Exception e) { |
| | | // Console.WriteLine ($"CheckGateway Fail:{e.Message}"); |
| | | // Utlis.WriteLine ($"CheckGateway Fail:{e.Message}"); |
| | | // //await DisConnectRemoteMqttClient (e.Message); |
| | | // //await StartCloudMqtt (); |
| | | // } |
| | |
| | | { |
| | | try { |
| | | //if (!remoteIsConnected) { |
| | | // System.Console.WriteLine ($"============>Mqtt 未连接 取消发送"); |
| | | // Utlis.WriteLine ($"============>Mqtt 未连接 取消发送"); |
| | | // return; |
| | | //} |
| | | |
| | |
| | | break; |
| | | case 3: |
| | | topicName = $"/ClientToBusGateWay/{CurRemoteMACInfo.macMark}/Common/CheckGateway"; |
| | | //Console.WriteLine ("CheckGateway"); |
| | | //Utlis.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"); |
| | | // Utlis.WriteLine ($"============>Mqtt MqttRemoteSend catch"); |
| | | if (!IsDisConnectingWithSendCatch) { |
| | | IsDisConnectingWithSendCatch = true; |
| | | await DisConnectRemoteMqttClient ("MqttRemoteSendCatch"); |
| | |
| | | |
| | | |
| | | } else { |
| | | System.Console.WriteLine ("============>Mqtt 取消连接,当前住宅没绑定网关"); |
| | | Utlis.WriteLine ("============>Mqtt 取消连接,当前住宅没绑定网关"); |
| | | } |
| | | } else { |
| | | var mGatewayRes = infoResult.PageData.Find ((obj) => obj.GatewayUniqueId == UserConfig.Instance.CurrentRegion.HomeGateways [0].GatewayUniqueId); |
| | |
| | | /// </summary> |
| | | static async Task ReceiveNotifyBusGateWayInfoChange () |
| | | { |
| | | System.Console.WriteLine ("============>Mqtt 网关上线"); |
| | | Utlis.WriteLine ("============>Mqtt 网关上线"); |
| | | SetGatewayOnlineResetCheck (); |
| | | if (CommonPage.IsRemote) { |
| | | |
| | |
| | | /// </summary> |
| | | static void ReceiveNotifyGateWayOffline () |
| | | { |
| | | System.Console.WriteLine ("============>Mqtt GateWayOffline"); |
| | | Utlis.WriteLine ("============>Mqtt GateWayOffline"); |
| | | IsGatewayOnline = false; |
| | | if (CommonPage.IsRemote) { |
| | | Utlis.ShowAppLinkStatus (AppLinkStatus.CloudOffline); |
| | |
| | | MqttRemoteSend (new byte [] { }, 3); |
| | | CheckGatewayCount++; |
| | | #if DEBUG |
| | | Console.WriteLine ("CheckGateway"); |
| | | Utlis.WriteLine ("CheckGateway"); |
| | | #endif |
| | | |
| | | |
| | |
| | | /// 收到CheckGateway主题 |
| | | /// </summary> |
| | | static void ReceiveCheckGatewayTopic () { |
| | | //System.Console.WriteLine ("============>Mqtt CheckGateway网关回复"); |
| | | //Utlis.WriteLine ("============>Mqtt CheckGateway网关回复"); |
| | | SetGatewayOnlineResetCheck (); |
| | | |
| | | |
| | |
| | | /// </summary> |
| | | static async Task ReceiveNotifySqueezeAsync (byte [] mes) |
| | | { |
| | | Console.WriteLine ($"============>MqttNotifySqueeze: {ErrorCode.LoginInAnotherDevice}"); |
| | | Utlis.WriteLine ($"============>MqttNotifySqueeze: {ErrorCode.LoginInAnotherDevice}"); |
| | | var mMes = CommonPage.MyEncodingUTF8.GetString (mes); |
| | | if (mMes == PushSignStr) return;//是自己的登录推送不处理 |
| | | |
| | |
| | | RemoteMqttClient = null; |
| | | } |
| | | CommonPage.IsRemote = false; |
| | | Console.WriteLine ("Close Mqtt!!!"); |
| | | Utlis.WriteLine ("Close Mqtt!!!"); |
| | | } catch { } |
| | | } |
| | | |
| | |
| | | webPush.DeleteToken_Push (UserConfig.Instance.tokenID); |
| | | }); |
| | | } catch (Exception ex) { |
| | | Console.WriteLine (ex.Message); |
| | | Utlis.WriteLine (ex.Message); |
| | | } finally { |
| | | Application.RunOnMainThread (() => { |
| | | Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.LinkStatusTipColor; |
| | |
| | | }); |
| | | |
| | | RemoteMqttClient.UseDisconnectedHandler (e => { |
| | | //Console.WriteLine ("RemoteMqttClient UseDisconnectedHandler"); |
| | | //Utlis.WriteLine ("RemoteMqttClient UseDisconnectedHandler"); |
| | | if (thisShowTip) { |
| | | if (CommonPage.IsRemote) { |
| | | //MainPage.AddTip (Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.RemoteConnectionDisconnected)); |
| | |
| | | } |
| | | }); |
| | | RemoteMqttClient.UseConnectedHandler (async e => { |
| | | //Console.WriteLine ("RemoteMqttClient IsRemote"); |
| | | //Utlis.WriteLine ("RemoteMqttClient IsRemote"); |
| | | await MqttCheckGateway (); |
| | | }); |
| | | } |
| | |
| | | MainPage.Loading.Hide (); |
| | | Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.DelColor; |
| | | }); |
| | | System.Console.WriteLine ($"重新连接远程通讯失败,因为获取新的KEY失败"); |
| | | Utlis.WriteLine ($"重新连接远程通讯失败,因为获取新的KEY失败"); |
| | | return; |
| | | } |
| | | //var messgae = System.Text.Encoding.UTF8.GetString (tempResult); |
| | | |
| | | var responsePack = tempResult.ResponseData;// Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>> (messgae); |
| | | //if (responsePack == null || responsePack ["ResponseData"] == null) { |
| | | // Console.WriteLine ("断开后重新链接需要重新登录获取连接的密码失败"); |
| | | // Utlis.WriteLine ("断开后重新链接需要重新登录获取连接的密码失败"); |
| | | // return; |
| | | //} |
| | | var dictrionaryResult = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>> (tempResult.ResponseData.ToString ()); |
| | |
| | | await Close (); |
| | | await RemoteMqttClient.ConnectAsync (options); |
| | | } catch (Exception ex) { |
| | | System.Console.WriteLine ("============>" + ex.Message); |
| | | Utlis.WriteLine ("============>" + ex.Message); |
| | | Application.RunOnMainThread (() => { |
| | | MainPage.Loading.Hide (); |
| | | isConnecting = false.ToString (); |
| | |
| | | return; |
| | | } |
| | | var topicName = @"/" + MainPage.LoginUser.AccountString.ToLower () + @"/CheckGateway/" + UserConfig.Instance.GatewayMAC.Replace (".", ""); |
| | | Console.WriteLine ("MqttCheckGateway : " + topicName); |
| | | Utlis.WriteLine ("MqttCheckGateway : " + topicName); |
| | | //base64加密主题 |
| | | var topicNameBase64 = Shared.Securitys.EncryptionService.AesEncryptTopic (topicName, mqttEncryptKey); |
| | | |