| | |
| | | System.Threading.Thread.Sleep (200); |
| | | if (!CommonPage.IsRemote) continue; |
| | | |
| | | if (Control.IsEnterBackground) continue; |
| | | |
| | | await StartCloudMqtt (); |
| | | await CheckingSubscribeTopics (); |
| | | } catch { } |
| | |
| | | IfNeedReadAllDeviceStatus = true; |
| | | bNeedStartTip = true; |
| | | IsGatewayOnline = false; |
| | | |
| | | StartCloudMqtt (); |
| | | } |
| | | |
| | | |
| | |
| | | } |
| | | try { |
| | | if (!isSubscribeTopicSuccess) { |
| | | |
| | | |
| | | var topicFilterCommon = new TopicFilter () { |
| | | Topic = $"/BusGateWayToClient/{CurRemoteMACInfo.macMark}/Common/#", |
| | |
| | | QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce |
| | | }; |
| | | |
| | | |
| | | |
| | | 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; |
| | | } |
| | | |
| | | ////2020-06-22 通用主题 |
| | | //var topicFilterMacMark = new TopicFilter () { |
| | | // Topic = $"/BusGateWayToClient/{CurRemoteMACInfo.macMark}/#", |
| | | // QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce |
| | | //}; |
| | | |
| | | ////挤下线主题 |
| | | //var topicFilterNotifySqueeze = new TopicFilter () { |
| | | // Topic = $"/BusGateWayToClient/{mMqttInfo.connEmqClientId}/Push/NotifySqueeze", |
| | | // QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce |
| | | //}; |
| | | |
| | | //var topicFilters = new TopicFilter [] { topicFilterMacMark, topicFilterNotifySqueeze }; |
| | | //var result = await RemoteMqttClient.SubscribeAsync (topicFilters); |
| | | //if (result.Items [0].ResultCode == MQTTnet.Client.Subscribing.MqttClientSubscribeResultCode.GrantedQoS2) { |
| | | // isSubscribeTopicSuccess = true; |
| | | //} |
| | | } |
| | | } catch (Exception ex) { |
| | | |
| | |
| | | //表示后面将进行连接 |
| | | |
| | | #region 初始化远程Mqtt |
| | | //2020-06-22 增加 |
| | | RemoteMqttClient = new MqttFactory ().CreateMqttClient (); |
| | | |
| | | //(3)当[连接云端的Mqtt成功后]或者[以及后面App通过云端Mqtt转发数据给网关成功后],处理接收到云端数据包响应时在mqttServerClient_ApplicationMessageReceived这个方法处理 |
| | | if (RemoteMqttClient.ApplicationMessageReceivedHandler == null) { |
| | | RemoteMqttClient.UseApplicationMessageReceivedHandler (async (e) => { |
| | |
| | | } catch (Exception ex) { |
| | | //Utlis.WriteLine ($"============>Mqtt 远程连接通讯连接出异常:{ex.Message}"); |
| | | |
| | | MainPage.LoadingSetText ("Connect error, trying to reconnect."); |
| | | MainPage.LoadingTipShow ("Connect error, trying to reconnect."); |
| | | |
| | | if (IfDEBUG) { |
| | | MainPage.ShowAlertOnMainThread ("error: " + ex.Message); |
| | |
| | | //最终要释放连接状态 |
| | | remoteMqttIsConnecting = false; |
| | | |
| | | MainPage.LoadingHide (); |
| | | MainPage.LoadingTipHide (); |
| | | |
| | | //MainPage.LoadingHide (); |
| | | } |
| | | }); |
| | | } |
| | |
| | | } else { |
| | | //Utlis.WriteLine ("============>Mqtt CurRemoteMACInfo null"); |
| | | |
| | | MainPage.LoadingSetText ("Please wait, failed to get gateway parameters."); |
| | | MainPage.LoadingTipShow ("Please wait, failed to get gateway parameters."); |
| | | } |
| | | } |
| | | |
| | | } else { |
| | | //Utlis.WriteLine ("============>Mqtt mqttInfoRequestResult_Obj null"); |
| | | |
| | | MainPage.LoadingSetText ("Please wait, failed to get user parameters."); |
| | | //MainPage.LoadingSetText ("Please wait, failed to get parameters."); |
| | | MainPage.LoadingTipShow ("Please wait, failed to get user parameters."); |
| | | //MainPage.LoadingTipShow ("Please wait, failed to get parameters."); |
| | | } |
| | | } catch { |
| | | |
| | |
| | | } else { |
| | | //Utlis.WriteLine ("============>Mqtt GetInfo 失败"); |
| | | MainPage.AddTip ("Please wait, failed to get parameters."); |
| | | //MainPage.LoadingSetText ("Please wait, failed to get parameters."); |
| | | //MainPage.LoadingTipShow ("Please wait, failed to get parameters."); |
| | | } |
| | | |
| | | } |
| | |
| | | LoginAccessToken = mqttRequestParToken, |
| | | PlatformStr = "L1", |
| | | PublishPayloadJsonStr = PushSignStr, |
| | | MainUserDistributedMark = UserConfig.Instance.CurrentRegion.MainUserDistributedMark, |
| | | //MainUserDistributedMark = UserConfig.Instance.CurrentRegion.MainUserDistributedMark, |
| | | HomeId = UserConfig.Instance.CurrentRegion.Id |
| | | }; |
| | | var mqttInfoRequestResult = MainPage.RequestHttps (API.ShareMemberConnMqttInfo, Newtonsoft.Json.JsonConvert.SerializeObject (mqttInfoRequestPar)); |
| | |
| | | //--第二步:获取当前住分享宅网关信息并连接MQTT |
| | | await GetSingleHomeGatewayPaggerAndMQTTConnectAsync (); |
| | | } else { |
| | | MainPage.LoadingSetText ("Please wait, failed to get user parameters."); |
| | | MainPage.LoadingTipShow ("Please wait, failed to get user parameters."); |
| | | } |
| | | } |
| | | } |
| | |
| | | //获取参数成功,开始连接... |
| | | //MainPage.AddTip ("Successfully obtained parameters, starting to connect..."); |
| | | |
| | | MainPage.LoadingStart ("Please wait, connecting remotely..."); |
| | | MainPage.LoadingTipShow ("Please wait, connecting remotely..."); |
| | | |
| | | //try { |
| | | // var url = mMqttInfo.connEmqDomainPort; |
| | |
| | | remoteIsConnected = true; |
| | | IsDisConnectingWithSendCatch = false; |
| | | |
| | | MainPage.LoadingSetText ("Remote connection succeeded."); |
| | | MainPage.LoadingTipShow ("Remote connection succeeded."); |
| | | |
| | | //MainPage.AddTip ("Connection succeeded!"); |
| | | //await MqttRemoteSend (new byte [] { 0 }, 1); |
| | |
| | | //options1 异常提示 |
| | | //Console.WriteLine ("Connect error: " + ex.Message); |
| | | |
| | | MainPage.LoadingSetText ("Connect error."); |
| | | MainPage.LoadingTipShow ("Connect error."); |
| | | |
| | | if (IfDEBUG) { |
| | | MainPage.ShowAlertOnMainThread ("Connect error: " + ex.Message); |
| | |
| | | bNeedStartTip = false; |
| | | if (CommonPage.IsRemote) { |
| | | //MainPage.AddTip ("Getting remote connection parameters..."); |
| | | MainPage.LoadingStart ("Please wait, getting connection parameters..."); |
| | | MainPage.LoadingTipShow ("Please wait, getting connection parameters..."); |
| | | } |
| | | } |
| | | } |
| | |
| | | |
| | | var requestJson3 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj3); |
| | | var revertObj3 = MainPage.RequestHttps (API.GetSingleHomeGatewayPagger, requestJson3, urlHead); |
| | | if (revertObj3.StateCode.ToUpper () == "SUCCESS") { |
| | | if (revertObj3.StateCode.ToUpper () == StateCode.SUCCESS) { |
| | | var infoResult = Newtonsoft.Json.JsonConvert.DeserializeObject<GetGatewayResult> (revertObj3.ResponseData.ToString ()); |
| | | if (bNeedConnect) { |
| | | //2020-01-11 |
| | |
| | | //var gatewayListRequestResult_Obj = Newtonsoft.Json.JsonConvert.DeserializeObject<MqttRemoteInfo> (revertObj3.ResponseData.ToString ()); |
| | | //--找出是否存在匹配当前住宅的mac,存在再进行远程。 |
| | | if (UserConfig.Instance.CheckHomeGatewaysNotEmpty ()) { |
| | | var mGatewayRes = infoResult.PageData.Find ((obj) => obj.GatewayUniqueId == UserConfig.Instance.CurrentRegion.HomeGateways [0].GatewayUniqueId); |
| | | var mGatewayRes = infoResult.PageData.Find ((obj) => obj.GatewayUniqueId == UserConfig.Instance.HomeGateway.mac); |
| | | if (mGatewayRes != null) { |
| | | CurRemoteMACInfo = new RemoteMACInfo (); |
| | | CurRemoteMACInfo.aesKey = mGatewayRes.AesKey; |
| | |
| | | Utlis.WriteLine ("============>Mqtt 取消连接,当前住宅没绑定网关"); |
| | | } |
| | | } else { |
| | | var mGatewayRes = infoResult.PageData.Find ((obj) => obj.GatewayUniqueId == UserConfig.Instance.CurrentRegion.HomeGateways [0].GatewayUniqueId); |
| | | var mGatewayRes = infoResult.PageData.Find ((obj) => obj.GatewayUniqueId == UserConfig.Instance.HomeGateway.mac); |
| | | if (mGatewayRes != null) { |
| | | CurRemoteMACInfo.aesKey = mGatewayRes.AesKey; |
| | | mqttEncryptKey = CurRemoteMACInfo.aesKey; |
| | | } else { |
| | | MainPage.LoadingSetText ("Please wait, failed to get gateway parameters."); |
| | | MainPage.LoadingTipShow ("Please wait, failed to get gateway parameters."); |
| | | } |
| | | |
| | | } |
| | |
| | | var requestObj3 = new GatewayByRegionListObj () { RegionID = UserConfig.Instance.CurrentRegion.RegionID }; |
| | | var requestJson3 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj3); |
| | | var revertObj3 = MainPage.RequestHttps ("GatewayByRegionList", requestJson3, true, false); |
| | | if (revertObj3.StateCode.ToUpper() == "SUCCESS") { |
| | | if (revertObj3.StateCode.ToUpper() == StateCode.SUCCESS) { |
| | | var responseDataObj = Newtonsoft.Json.JsonConvert.DeserializeObject<List<GatewayRes>> (revertObj3.ResponseData.ToString ()); |
| | | var gatewayList = responseDataObj; |
| | | if (gatewayList != null && gatewayList.Count > 0) { |
| | |
| | | Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.DelColor; |
| | | }); |
| | | break; |
| | | case "Success": |
| | | case StateCode.SUCCESS: |
| | | CommonPage.IsRemote = true; |
| | | Application.RunOnMainThread (() => { |
| | | MainPage.Loading.Hide (); |