| | |
| | | static MqttCommon () |
| | | { |
| | | InitMqtt (); |
| | | InitCheckGateway (); |
| | | //InitCheckGateway (); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | 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; |
| | | //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 (); |
| | | 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) => { |
| | | Utlis.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; |
| | | |
| | | 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); |
| | | } |
| | | 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 { |
| | | Utlis.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) { |
| | | Utlis.WriteLine ($"============>Mqtt 远程连接通讯连接出异常:{ex.Message}"); |
| | |
| | | { |
| | | |
| | | 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> |
| | | ///// |
| | |
| | | if (CheckGatewayCount >= 3) { |
| | | //连续3次没回复,判定网关超时 |
| | | if (MainPage.WiFiStatus == "CrabtreeAdd/CloudLink.png") { |
| | | IsGatewayOnline = false; |
| | | //IsGatewayOnline = false; |
| | | Utlis.ShowAppLinkStatus (AppLinkStatus.CloudOffline); |
| | | MainPage.AddTip ("Gateway offline!"); |
| | | } |
| | |
| | | //Utlis.WriteLine ("============>Mqtt CheckGateway网关回复"); |
| | | SetGatewayOnlineResetCheck (); |
| | | |
| | | |
| | | |
| | | //旧网关方法 |
| | | //MainPage.WiFiStatus = "CrabtreeAdd/CloudUnlink.png"; |