| | |
| | | static MqttCommon () |
| | | { |
| | | InitMqtt (); |
| | | InitCheckGateway (); |
| | | //InitCheckGateway (); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | 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; |
| | |
| | | await System.Threading.Tasks.Task.Factory.StartNew (async () => { |
| | | try { |
| | | //lock (RemoteMqttClient) { |
| | | //表示后面将进行连接 |
| | | //表示后面将进行连接 |
| | | |
| | | #region 初始化远程Mqtt |
| | | //(3)当[连接云端的Mqtt成功后]或者[以及后面App通过云端Mqtt转发数据给网关成功后],处理接收到云端数据包响应时在mqttServerClient_ApplicationMessageReceived这个方法处理 |
| | | if (RemoteMqttClient.ApplicationMessageReceivedHandler == null) { |
| | | RemoteMqttClient.UseApplicationMessageReceivedHandler (async (e) => { |
| | | try { |
| | | if (!RemoteMqttClient.IsConnected || !CommonPage.IsRemote) { |
| | | return; |
| | | #region 初始化远程Mqtt |
| | | //(3)当[连接云端的Mqtt成功后]或者[以及后面App通过云端Mqtt转发数据给网关成功后],处理接收到云端数据包响应时在mqttServerClient_ApplicationMessageReceived这个方法处理 |
| | | if (RemoteMqttClient.ApplicationMessageReceivedHandler == null) { |
| | | RemoteMqttClient.UseApplicationMessageReceivedHandler (async (e) => { |
| | | try { |
| | | if (!RemoteMqttClient.IsConnected || !CommonPage.IsRemote) { |
| | | return; |
| | | } |
| | | var aesDecryptTopic = e.ApplicationMessage.Topic; |
| | | var aesDecryptPayload = e.ApplicationMessage.Payload; |
| | | //Utlis.WriteLine ("Topic={0}", aesDecryptTopic); |
| | | |
| | | //if (aesDecryptTopic == $"NotifyBusGateWayInfoChagne/{CurRemoteMACInfo.md5_mac_string}") {//网关上线,需要更新aeskey |
| | | //2020-01-11 修改订阅主题地址 |
| | | if (aesDecryptTopic == $"/BusGateWayToClient/{CurRemoteMACInfo.macMark}/NotifyBusGateWayInfoChange") {//网关上线,需要更新aeskey //----第二步:读取账号下面的网关列表 |
| | | 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") { |
| | | ReceiveCheckGatewayTopic (); |
| | | } else { |
| | | SetGatewayOnlineResetCheck (); |
| | | |
| | | if (!string.IsNullOrEmpty (mqttEncryptKey)) { |
| | | aesDecryptPayload = Shared.Securitys.EncryptionService.AesDecryptPayload (e.ApplicationMessage.Payload, mqttEncryptKey); |
| | | } |
| | | var aesDecryptTopic = e.ApplicationMessage.Topic; |
| | | var aesDecryptPayload = e.ApplicationMessage.Payload; |
| | | //Console.WriteLine ("Topic={0}", aesDecryptTopic); |
| | | |
| | | //if (aesDecryptTopic == $"NotifyBusGateWayInfoChagne/{CurRemoteMACInfo.md5_mac_string}") {//网关上线,需要更新aeskey |
| | | //2020-01-11 修改订阅主题地址 |
| | | if (aesDecryptTopic == $"/BusGateWayToClient/{CurRemoteMACInfo.macMark}/NotifyBusGateWayInfoChange") {//网关上线,需要更新aeskey //----第二步:读取账号下面的网关列表 |
| | | 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") { |
| | | ReceiveCheckGatewayTopic (); |
| | | } else { |
| | | SetGatewayOnlineResetCheck (); |
| | | var packet = new Packet (); |
| | | packet.IsLocal = false; |
| | | packet.Bytes = aesDecryptPayload; |
| | | packet.Manager (); |
| | | |
| | | 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 (); |
| | | } catch { } |
| | | }); |
| | | } |
| | | |
| | | if (RemoteMqttClient.DisconnectedHandler == null) { |
| | | RemoteMqttClient.UseDisconnectedHandler (async (e) => { |
| | | Utlis.WriteLine ($"============>Mqtt远程连接断开"); |
| | | isSubscribeTopicSuccess = false; |
| | | await DisConnectRemoteMqttClient ("StartRemoteMqtt.DisconnectedHandler"); |
| | | //await StartRemoteMqtt(); |
| | | //if (thisShowTip) { |
| | | // if (CommonPage.IsRemote) { |
| | | // Application.RunOnMainThread (() => { |
| | | // MainPage.Loading.Hide (); |
| | | // }); |
| | | // } |
| | | //} else { |
| | | // thisShowTip = true; |
| | | //} |
| | | }); |
| | | } |
| | | if (RemoteMqttClient.ConnectedHandler == null) { |
| | | RemoteMqttClient.UseConnectedHandler (async (e) => { |
| | | IfNeedReadAllDeviceStatus = true; |
| | | mCheckGatewayTime = DateTime.Now; |
| | | CheckGatewayCount = 0; |
| | | bNeedStartTip = true; |
| | | |
| | | Utlis.WriteLine ($"============>Mqtt远程连接成功"); |
| | | if (CommonPage.IsRemote) { |
| | | Utlis.ShowAppLinkStatus (AppLinkStatus.CloudLink); |
| | | } |
| | | |
| | | if (CurRemoteMACInfo != null) { |
| | | IsGatewayOnline = CurRemoteMACInfo.isValid != "InValid"; |
| | | if (!IsGatewayOnline) { |
| | | //网关不在线 |
| | | if (CommonPage.IsRemote) { |
| | | Utlis.ShowAppLinkStatus (AppLinkStatus.CloudOffline); |
| | | } |
| | | |
| | | } catch { } |
| | | }); |
| | | } |
| | | MainPage.AddTip ("Remote failed,gateway offline"); |
| | | } else { |
| | | //Remote Connection Succeeded |
| | | |
| | | if (RemoteMqttClient.DisconnectedHandler == null) { |
| | | RemoteMqttClient.UseDisconnectedHandler (async (e) => { |
| | | System.Console.WriteLine ($"============>Mqtt远程连接断开"); |
| | | isSubscribeTopicSuccess = false; |
| | | await DisConnectRemoteMqttClient ("StartRemoteMqtt.DisconnectedHandler"); |
| | | //await StartRemoteMqtt(); |
| | | //if (thisShowTip) { |
| | | // if (CommonPage.IsRemote) { |
| | | // Application.RunOnMainThread (() => { |
| | | // MainPage.Loading.Hide (); |
| | | // }); |
| | | // } |
| | | //网关在线 |
| | | |
| | | //重新一次所有设备状态 |
| | | IfNeedReadAllDeviceStatus = false; |
| | | Shared.SimpleControl.Phone.UserMiddle.ReadAllDeviceStatus (); |
| | | // |
| | | MqttRemoteSend (new byte [] { 0 }, 3); |
| | | |
| | | if (CommonPage.IsRemote) { |
| | | MainPage.AddTip (Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.LinkSuccess)); |
| | | } |
| | | } |
| | | |
| | | //if (CurRemoteMACInfo.isValid == "InValid") { |
| | | // MainPage.AddTip ("Remote failed,gateway offline"); |
| | | //} else { |
| | | // thisShowTip = true; |
| | | // MqttRemoteSend (new byte [] { 0 }, 3); |
| | | //} |
| | | }); |
| | | } |
| | | if (RemoteMqttClient.ConnectedHandler == null) { |
| | | RemoteMqttClient.UseConnectedHandler (async (e) => { |
| | | IfNeedReadAllDeviceStatus = true; |
| | | |
| | | System.Console.WriteLine ($"============>Mqtt远程连接成功"); |
| | | if (CommonPage.IsRemote) { |
| | | Utlis.ShowAppLinkStatus (AppLinkStatus.CloudLink); |
| | | } |
| | | |
| | | if (CurRemoteMACInfo != null) { |
| | | IsGatewayOnline = CurRemoteMACInfo.isValid != "InValid"; |
| | | if (!IsGatewayOnline) { |
| | | //网关不在线 |
| | | if (CommonPage.IsRemote) { |
| | | Utlis.ShowAppLinkStatus (AppLinkStatus.CloudOffline); |
| | | } |
| | | MainPage.AddTip ("Remote failed,gateway offline"); |
| | | } 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); |
| | | //} |
| | | } |
| | | } |
| | | |
| | | |
| | | }); |
| | | } |
| | | #endregion |
| | | }); |
| | | } |
| | | #endregion |
| | | //} |
| | | //正在获取连接参数..." |
| | | ShowStartTip (); |
| | | |
| | | mqttRequestParToken = MainPage.LoginUser.LoginTokenString; |
| | | //--第一步:获取mqtt链接参数 |
| | | var mqttInfoRequestResult = new ResponsePack () { StateCode = "" }; |
| | | //--判断是当前是否分享的住宅 |
| | | if (!UserConfig.Instance.CurrentRegion.IsOthreShare) { |
| | | var mqttInfoUrl = $"{MainPage.RequestHttpsHost}/EmqMqtt/GetConnMqttInfo";//获取连接远程云端Emq Mqtt 服务器连接信息 |
| | | var mqttInfoRequestPar = new GetConnMqttInfoObj () { |
| | | LoginAccessToken = mqttRequestParToken, |
| | | PlatformStr = "L1", |
| | | PublishPayloadJsonStr = PushSignStr, |
| | | }; |
| | | mqttInfoRequestResult = MainPage.RequestHttps ("", Newtonsoft.Json.JsonConvert.SerializeObject (mqttInfoRequestPar), mqttInfoUrl); |
| | | if (mqttInfoRequestResult != null && mqttInfoRequestResult.ResponseData != null) { |
| | | var mqttInfoRequestResult_info = Newtonsoft.Json.JsonConvert.DeserializeObject<MqttInfo> (mqttInfoRequestResult.ResponseData.ToString ()); |
| | | if (mqttInfoRequestResult_info != null) { |
| | | mMqttInfo = mqttInfoRequestResult_info; |
| | | //----第二步:读取账号下面的网关列表 |
| | | var gatewayListUrl = $"{MainPage.RequestHttpsHostMqtt}/Center/Center/GetGatewayPagger"; //App、Buspro软件登录后获取网关列表 http 请求 |
| | | var gatewayListRequestPar = new RemoteRequestParameters () { LoginAccessToken = mqttRequestParToken, RequestVersion = "RequestVersion1", RequestProtocolType = 0, RequestSource = 1 }; |
| | | var gatewayListRequestResult = MainPage.RequestHttps ("", Newtonsoft.Json.JsonConvert.SerializeObject (gatewayListRequestPar), gatewayListUrl); |
| | | var gatewayListRequestResult_Obj = Newtonsoft.Json.JsonConvert.DeserializeObject<MqttRemoteInfo> (gatewayListRequestResult.ResponseData.ToString ()); |
| | | //--找出是否存在匹配当前住宅的mac,存在再进行远程。 |
| | | if (UserConfig.Instance.CheckHomeGatewaysNotEmpty ()) { |
| | | CurRemoteMACInfo = gatewayListRequestResult_Obj.pageData.Find ((obj) => obj.mac == UserConfig.Instance.CurrentRegion.HomeGateways [0].GatewayUniqueId); |
| | | //CurRemoteMACInfo = gatewayListRequestResult_Obj.pageData [0]; |
| | | await MQTTConnectAsync (); |
| | | mqttRequestParToken = MainPage.LoginUser.LoginTokenString; |
| | | //--第一步:获取mqtt链接参数 |
| | | var mqttInfoRequestResult = new ResponsePack () { StateCode = "" }; |
| | | //--判断是当前是否分享的住宅 |
| | | if (!UserConfig.Instance.CurrentRegion.IsOthreShare) { |
| | | var mqttInfoUrl = $"{MainPage.RequestHttpsHost}/EmqMqtt/GetConnMqttInfo";//获取连接远程云端Emq Mqtt 服务器连接信息 |
| | | var mqttInfoRequestPar = new GetConnMqttInfoObj () { |
| | | LoginAccessToken = mqttRequestParToken, |
| | | PlatformStr = "L1", |
| | | PublishPayloadJsonStr = PushSignStr, |
| | | }; |
| | | mqttInfoRequestResult = MainPage.RequestHttps ("", Newtonsoft.Json.JsonConvert.SerializeObject (mqttInfoRequestPar), mqttInfoUrl); |
| | | if (mqttInfoRequestResult != null && mqttInfoRequestResult.ResponseData != null) { |
| | | var mqttInfoRequestResult_info = Newtonsoft.Json.JsonConvert.DeserializeObject<MqttInfo> (mqttInfoRequestResult.ResponseData.ToString ()); |
| | | if (mqttInfoRequestResult_info != null) { |
| | | mMqttInfo = mqttInfoRequestResult_info; |
| | | //----第二步:读取账号下面的网关列表 |
| | | var gatewayListUrl = $"{MainPage.RequestHttpsHostMqtt}/Center/Center/GetGatewayPagger"; //App、Buspro软件登录后获取网关列表 http 请求 |
| | | var gatewayListRequestPar = new RemoteRequestParameters () { LoginAccessToken = mqttRequestParToken, RequestVersion = "RequestVersion1", RequestProtocolType = 0, RequestSource = 1 }; |
| | | var gatewayListRequestResult = MainPage.RequestHttps ("", Newtonsoft.Json.JsonConvert.SerializeObject (gatewayListRequestPar), gatewayListUrl); |
| | | var gatewayListRequestResult_Obj = Newtonsoft.Json.JsonConvert.DeserializeObject<MqttRemoteInfo> (gatewayListRequestResult.ResponseData.ToString ()); |
| | | //--找出是否存在匹配当前住宅的mac,存在再进行远程。 |
| | | if (UserConfig.Instance.CheckHomeGatewaysNotEmpty ()) { |
| | | CurRemoteMACInfo = gatewayListRequestResult_Obj.pageData.Find ((obj) => obj.mac == UserConfig.Instance.CurrentRegion.HomeGateways [0].GatewayUniqueId); |
| | | //CurRemoteMACInfo = gatewayListRequestResult_Obj.pageData [0]; |
| | | await MQTTConnectAsync (); |
| | | |
| | | } else { |
| | | System.Console.WriteLine ("============>Mqtt 取消连接,当前住宅没绑定网关"); |
| | | } |
| | | |
| | | } else { |
| | | Utlis.WriteLine ("============>Mqtt 取消连接,当前住宅没绑定网关"); |
| | | } |
| | | |
| | | } else { |
| | | Utlis.WriteLine ("============>Mqtt GetGatewayPagger 失败"); |
| | | } |
| | | |
| | | } else { |
| | | //如果是分享过来的住宅 走下面流程 |
| | | var mqttInfoRequestPar = new ShareMemberConnMqttInfoObj () { |
| | | LoginAccessToken = mqttRequestParToken, |
| | | PlatformStr = "L1", |
| | | PublishPayloadJsonStr = PushSignStr, |
| | | MainUserDistributedMark = UserConfig.Instance.CurrentRegion.MainUserDistributedMark, |
| | | HomeId = UserConfig.Instance.CurrentRegion.Id |
| | | }; |
| | | mqttInfoRequestResult = MainPage.RequestHttps (API.ShareMemberConnMqttInfo, Newtonsoft.Json.JsonConvert.SerializeObject (mqttInfoRequestPar)); |
| | | //--第一步:获取mqtt链接参数 |
| | | if (mqttInfoRequestResult != null && mqttInfoRequestResult.ResponseData != null) { |
| | | var mqttInfoRequestResult_info = Newtonsoft.Json.JsonConvert.DeserializeObject<MqttInfo> (mqttInfoRequestResult.ResponseData.ToString ()); |
| | | if (mqttInfoRequestResult_info != null) { |
| | | mMqttInfo = mqttInfoRequestResult_info; |
| | | //--第二步:获取当前住分享宅网关信息并连接MQTT |
| | | await GetSingleHomeGatewayPaggerAndMQTTConnectAsync (); |
| | | |
| | | } |
| | | Utlis.WriteLine ("============>Mqtt GetInfo 失败"); |
| | | } |
| | | } else { |
| | | //如果是分享过来的住宅 走下面流程 |
| | | var mqttInfoRequestPar = new ShareMemberConnMqttInfoObj () { |
| | | LoginAccessToken = mqttRequestParToken, |
| | | PlatformStr = "L1", |
| | | PublishPayloadJsonStr = PushSignStr, |
| | | MainUserDistributedMark = UserConfig.Instance.CurrentRegion.MainUserDistributedMark, |
| | | HomeId = UserConfig.Instance.CurrentRegion.Id |
| | | }; |
| | | mqttInfoRequestResult = MainPage.RequestHttps (API.ShareMemberConnMqttInfo, Newtonsoft.Json.JsonConvert.SerializeObject (mqttInfoRequestPar)); |
| | | //--第一步:获取mqtt链接参数 |
| | | if (mqttInfoRequestResult != null && mqttInfoRequestResult.ResponseData != null) { |
| | | var mqttInfoRequestResult_info = Newtonsoft.Json.JsonConvert.DeserializeObject<MqttInfo> (mqttInfoRequestResult.ResponseData.ToString ()); |
| | | if (mqttInfoRequestResult_info != null) { |
| | | mMqttInfo = mqttInfoRequestResult_info; |
| | | //--第二步:获取当前住分享宅网关信息并连接MQTT |
| | | await GetSingleHomeGatewayPaggerAndMQTTConnectAsync (); |
| | | |
| | | } |
| | | |
| | | } |
| | | } |
| | | |
| | | } catch (Exception ex) { |
| | | System.Console.WriteLine ($"============>Mqtt 远程连接通讯连接出异常:{ex.Message}"); |
| | | Utlis.WriteLine ($"============>Mqtt 远程连接通讯连接出异常:{ex.Message}"); |
| | | } finally { |
| | | //最终要释放连接状态 |
| | | remoteMqttIsConnecting = false; |
| | |
| | | { |
| | | |
| | | if (CurRemoteMACInfo != null && mMqttInfo != null) { |
| | | MainPage.AddTip ("Successfully obtained parameters, starting to connect..."); |
| | | |
| | | |
| | | var url = mMqttInfo.connEmqDomainPort; |
| | | var clientId = mMqttInfo.connEmqClientId; |
| | | var username = mMqttInfo.connEmqUserName; |
| | |
| | | } |
| | | |
| | | |
| | | // 571=Starting remote connection mode... |
| | | //572=Getting remote connection parameters... |
| | | //573=Successfully obtained parameters, starting to connect... |
| | | /// <summary> |
| | | /// 从开始到连接成功,只提示1次 |
| | | /// </summary> |
| | | static bool bNeedStartTip = true; |
| | | /// <summary> |
| | | /// 正在获取连接参数... |
| | | /// </summary> |
| | | static void ShowStartTip () |
| | | { |
| | | if (bNeedStartTip) { |
| | | bNeedStartTip = false; |
| | | if (CommonPage.IsRemote) { |
| | | MainPage.AddTip ("Getting remote connection parameters..."); |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | ///// <summary> |
| | | ///// |
| | | ///// </summary> |
| | |
| | | //{ |
| | | // 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); |
| | |
| | | if (CheckGatewayCount >= 3) { |
| | | //连续3次没回复,判定网关超时 |
| | | if (MainPage.WiFiStatus == "CrabtreeAdd/CloudLink.png") { |
| | | IsGatewayOnline = false; |
| | | //IsGatewayOnline = false; |
| | | Utlis.ShowAppLinkStatus (AppLinkStatus.CloudOffline); |
| | | MainPage.AddTip ("Gateway offline!"); |
| | | } |
| | |
| | | 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 (); |
| | | |
| | | |
| | | |
| | | //旧网关方法 |
| | | //MainPage.WiFiStatus = "CrabtreeAdd/CloudUnlink.png"; |
| | |
| | | /// </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); |
| | | |