| | |
| | | Utlis.WriteLine($"Remote主动断开_{s}"); |
| | | //await RemoteMqttClient.DisconnectAsync(new MQTTnet.Client.Disconnecting.MqttClientDisconnectOptions { }, CancellationToken.None); |
| | | await RemoteMqttClient.DisconnectAsync(); |
| | | |
| | | if (Control.Ins.IsRemote) |
| | | { |
| | | Control.Ins.GatewayOnline = false; |
| | | } |
| | | } |
| | | } |
| | | catch (Exception e) |
| | |
| | | try |
| | | { |
| | | System.Threading.Thread.Sleep(500); |
| | | //if (!Control.Ins.IsRemote) continue; |
| | | //进入后台不处理 |
| | | if (MainPage.IsEnterBackground) continue; |
| | | if (MqttInfoConfig.Current.HomeGatewayInfo == null) |
| | | { |
| | | continue; |
| | | } |
| | | |
| | | await StartCloudMqtt(); |
| | | await SubscribeTopics(); |
| | |
| | | var result = await RemoteMqttClient.SubscribeAsync(new MqttTopicFilter[] { topicFilterPush2, topicAlinkStatus }); |
| | | if (result.Items[0].ResultCode == MQTTnet.Client.Subscribing.MqttClientSubscribeResultCode.GrantedQoS0) |
| | | { |
| | | Control.Ins.GatewayOnline = true; |
| | | isSubscribeSuccess = true; |
| | | Utlis.WriteLine("订阅成功!"); |
| | | if (MqttInfoConfig.Current.HomeGatewayInfo != null && MqttInfoConfig.Current.HomeGatewayInfo.mac == Entity.DB_ResidenceData.Instance.residenceGatewayMAC) |
| | | { |
| | | Control.Ins.IsRemote = true; |
| | | } |
| | | MqttRemoteSend(new byte[] { 0 }, 3); |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | |
| | | else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/Common/CheckGateway") |
| | | { |
| | | var ss = Encoding.UTF8.GetString(e.ApplicationMessage.Payload); |
| | | ReceiveCheckGateway(ss); |
| | | //ReceiveCheckGateway(ss); |
| | | return; |
| | | } |
| | | } |
| | |
| | | //一端口数据解析 |
| | | else |
| | | { |
| | | SetGatewayOnlineResetCheck(); |
| | | //SetGatewayOnlineResetCheck(); |
| | | var bytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, mqttEncryptKey); |
| | | //bus数据解析 |
| | | var packet = new Packet(); |
| | |
| | | /// <returns></returns> |
| | | static async Task StartMQTTGetInfo() |
| | | { |
| | | //if (!Control.Ins.IsRemote) |
| | | //{ |
| | | // return; |
| | | //} |
| | | |
| | | await GetMqttInfoAndMQTTConnectAsync(); |
| | | |
| | | ////--判断是当前是否分享的住宅 |
| | |
| | | /// </summary> |
| | | static async Task MQTTConnectAsync() |
| | | { |
| | | |
| | | //if (!Control.Ins.IsRemote) |
| | | //{ |
| | | // return; |
| | | //} |
| | | |
| | | //if (MqttInfoConfig.Current.HomeGatewayInfo != null && MqttInfoConfig.Current.mMqttInfo != null) |
| | | //没有网关情况下,也需要连接mqtt,涂鸦第三方设备不需要网关 |
| | | if (MqttInfoConfig.Current.mMqttInfo != null) |
| | |
| | | MqttInfoConfig.Current.Save(); |
| | | |
| | | |
| | | if (MqttInfoConfig.Current.HomeGatewayInfo == null) |
| | | { |
| | | MqttInfoConfig.Current.HomeGatewayInfo = new Server.HomeGatewayInfo(); |
| | | } |
| | | |
| | | mqttEncryptKey = MqttInfoConfig.Current.HomeGatewayInfo.aesKey; |
| | | //解密密钥规则:已现有的住宅ID为基准,从右边一一获取值,最后如果不够16位,则往右补零 |
| | | string aesKey = string.Empty; |
| | |
| | | { |
| | | try |
| | | { |
| | | SetGatewayOnlineResetCheck(); |
| | | if (Control.Ins.IsRemote) |
| | | //SetGatewayOnlineResetCheck(); |
| | | if (Control.Ins.GatewayOnline_Cloud) |
| | | { |
| | | CheckIfNeedReadAllDeviceStatus(); |
| | | } |
| | | |
| | | } |
| | | catch { } |
| | | } |
| | |
| | | /// </summary> |
| | | static void ReceiveNotifyGateWayOffline() |
| | | { |
| | | if (Control.Ins.IsRemote) |
| | | { |
| | | Control.Ins.GatewayOnline = false; |
| | | } |
| | | Control.Ins.GatewayOnline_Cloud = false; |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// 收到挤下线推送 |
| | | /// </summary> |
| | | static void ReceiveNotifySqueezeAsync(string mMes) |
| | | { |
| | | if (mMes == PushSignStr) return;//是自己的登录推送不处理 |
| | | if (mMes == PushSignStr ) return;//是自己的登录推送不处理//或者当前不是远程链接状态 |
| | | |
| | | //断开远程连接 |
| | | Control.Ins.IsRemote = false; |
| | | if (!UserInfo.Current.IsLogin) |
| | | { |
| | | return; |
| | |
| | | //HDLRequest.Current.PushserivceSignOut (); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 收到CheckGateway主题 |
| | | /// </summary> |
| | | static void ReceiveCheckGateway(string mMes) |
| | | { |
| | | if (!Control.Ins.IsRemote) return; |
| | | ///// <summary> |
| | | ///// 收到CheckGateway主题 |
| | | ///// </summary> |
| | | //static void ReceiveCheckGateway(string mMes) |
| | | //{ |
| | | // if (!Control.Ins.IsRemote) return; |
| | | |
| | | Utlis.WriteLine("ReceiveCheckGateway!"); |
| | | // Utlis.WriteLine("ReceiveCheckGateway!"); |
| | | |
| | | //CheckIfNeedReadAllDeviceStatus (); |
| | | // //CheckIfNeedReadAllDeviceStatus (); |
| | | |
| | | //var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponsePack>(mMes); |
| | | Control.Ins.GatewayOnline = true; |
| | | } |
| | | // //var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponsePack>(mMes); |
| | | // Control.Ins.GatewayOnline = true; |
| | | //} |
| | | |
| | | /// <summary> |
| | | /// 推送挤下线主题 |
| | |
| | | } |
| | | await RemoteMqttClient.PublishAsync(new MqttApplicationMessage { Topic = topicName, Payload = message, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce }); |
| | | break; |
| | | case 3: |
| | | topicName = $"/ClientToBusGateWay/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/Common/CheckGateway"; |
| | | Utlis.WriteLine("CheckGateway"); |
| | | await RemoteMqttClient.PublishAsync(new MqttApplicationMessage { Topic = topicName, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce }); |
| | | break; |
| | | case 4://发布新方案的挤下线主题 |
| | | topicName = $"/BusGateWayToClient/{UserInfo.Current.ID}" + PushNotifySqueeze; |
| | | //message = Encoding.UTF8.GetBytes (PushSignStr); |
| | |
| | | /// </summary> |
| | | static bool IfNeedReadAllDeviceStatus = true; |
| | | |
| | | /// <summary> |
| | | /// 设置网关在线标志,并重置CheckGateway参数 |
| | | /// </summary> |
| | | static void SetGatewayOnlineResetCheck() |
| | | { |
| | | if (Control.Ins.IsRemote) |
| | | { |
| | | if (!Control.Ins.GatewayOnline) |
| | | { |
| | | try |
| | | { |
| | | if (DB_ResidenceData.Instance.HomeGateway != null) |
| | | { |
| | | DB_ResidenceData.Instance.HomeGateway.gatewayStatus = true; |
| | | } |
| | | Control.Ins.GatewayOnline = true; |
| | | } |
| | | catch { } |
| | | } |
| | | } |
| | | } |
| | | ///// <summary> |
| | | ///// 设置网关在线标志,并重置CheckGateway参数 |
| | | ///// </summary> |
| | | //static void SetGatewayOnlineResetCheck() |
| | | //{ |
| | | // if (Control.Ins.IsRemote) |
| | | // { |
| | | // if (!Control.Ins.GatewayOnline) |
| | | // { |
| | | // try |
| | | // { |
| | | // if (DB_ResidenceData.Instance.HomeGateway != null) |
| | | // { |
| | | // DB_ResidenceData.Instance.HomeGateway.gatewayStatus = true; |
| | | // } |
| | | // Control.Ins.GatewayOnline = true; |
| | | // } |
| | | // catch { } |
| | | // } |
| | | // } |
| | | //} |
| | | |
| | | |
| | | /// <summary> |