| | |
| | | using System; |
| | | using System.Text; |
| | | using System.Security.Cryptography; |
| | | using MQTTnet.Client; |
| | | using MQTTnet; |
| | | using System.Threading.Tasks; |
| | | using Shared; |
| | | using HDL_ON.DAL.Server; |
| | | |
| | | namespace HDL_ON.DAL.Net |
| | | { |
| | | |
| | | // public static class MqttCommon |
| | | // { |
| | | // /// <summary> |
| | | // /// 是否提示 远程连接的错误信息 |
| | | // /// </summary> |
| | | // public static bool IfDEBUG = false; |
| | | |
| | | // /// <summary> |
| | | // /// 加密通讯KEY |
| | | // /// </summary> |
| | | // static string mqttEncryptKey = ""; |
| | | // //static string checkGatewayTopicBase64 = ""; |
| | | |
| | | // /// <summary> |
| | | // /// 挤下线主题 |
| | | // /// </summary> |
| | | // static readonly string PushNotifySqueeze = "/Push/NotifySqueeze"; |
| | | |
| | | // ///// <summary> |
| | | // ///// 挤下线主题 |
| | | // ///// </summary> |
| | | // //static readonly string TopicToApp = "/Push/NotifySqueeze"; |
| | | |
| | | |
| | | // /// <summary> |
| | | // /// 随机Key |
| | | // /// </summary> |
| | | // static string RandomKey = ""; |
| | | |
| | | // static string GetRandomKey() |
| | | // { |
| | | // if (string.IsNullOrEmpty(RandomKey)) |
| | | // { |
| | | // //随机2位字符串 |
| | | // RandomKey = Utlis.CreateRandomString(2); |
| | | // } |
| | | |
| | | // return RandomKey; |
| | | |
| | | // } |
| | | |
| | | // /// <summary> |
| | | // /// 远程MqttClient |
| | | // /// </summary> |
| | | // public static IMqttClient RemoteMqttClient = new MqttFactory().CreateMqttClient(); |
| | | |
| | | |
| | | // /// <summary> |
| | | // /// 推送标识 |
| | | // /// </summary> |
| | | // static string PushSignStr = System.DateTime.Now.Ticks.ToString(); |
| | | |
| | | // /// <summary> |
| | | // /// 断开远程Mqtt的链接 |
| | | // /// </summary> |
| | | // static async System.Threading.Tasks.Task DisConnectRemoteMqttClient(string s = "") |
| | | // { |
| | | // try |
| | | // { |
| | | // if (remoteIsConnected) |
| | | // { |
| | | // remoteIsConnected = false; |
| | | // isSubscribeSuccess = false; |
| | | // Utlis.WriteLine($"Remote主动断开_{s}"); |
| | | // //await RemoteMqttClient.DisconnectAsync(new MQTTnet.Client.Disconnecting.MqttClientDisconnectOptions { }, CancellationToken.None); |
| | | // await RemoteMqttClient.DisconnectAsync(); |
| | | |
| | | // if (MainPage.IsRemote) |
| | | // { |
| | | // ////不是无网络 |
| | | // //if (UserConfig.Instance.internetStatus != 0) |
| | | // //{ |
| | | // // Utlis.ShowAppLinkStatus(AppLinkStatus.CloudUnlink); |
| | | // //} |
| | | // } |
| | | // } |
| | | // } |
| | | // catch (Exception e) |
| | | // { |
| | | // Utlis.WriteLine($"Remote断开通讯连接出异常:{e.Message}"); |
| | | // } |
| | | // } |
| | | |
| | | // /// <summary> |
| | | // /// 断开远程Mqtt的链接 |
| | | // /// </summary> |
| | | // static async Task DisConnectRemoteMqttClientWhenStart(string s = "") |
| | | // { |
| | | // try |
| | | // { |
| | | // //if (remoteIsConnected) { |
| | | // remoteIsConnected = false; |
| | | // isSubscribeSuccess = false; |
| | | // Utlis.WriteLine($"RemoteStart主动断开_{s}"); |
| | | // await RemoteMqttClient.DisconnectAsync(); |
| | | |
| | | // //} |
| | | // } |
| | | // catch (Exception e) |
| | | // { |
| | | // Utlis.WriteLine($"RemoteStart断开通讯连接出异常:{e.Message}"); |
| | | // } |
| | | |
| | | // } |
| | | |
| | | |
| | | // /// <summary> |
| | | // /// 断开mqtt连接 |
| | | // /// </summary> |
| | | // /// <param name="s">断开原因</param> |
| | | // /// <param name="reset">是否需要去中心服务器 重新获取参数</param> |
| | | // /// <returns></returns> |
| | | // public static async Task DisConnectRemote(string s = "", bool reset = true) |
| | | // { |
| | | // if (reset) |
| | | // { |
| | | // bNeedStartTip = true; |
| | | // bNeedConnectTip = true; |
| | | // MqttInfoConfig.Current.IfGetMqttInfoSuccess = false; |
| | | // } |
| | | // await DisConnectRemoteMqttClient(s); |
| | | // } |
| | | |
| | | // //static DateTime dateTime = DateTime.MinValue; |
| | | // /// <summary> |
| | | // /// 外网的MQTT是否正在连接 |
| | | // /// </summary> |
| | | // public static bool remoteMqttIsConnecting; |
| | | // static bool remoteIsConnected; |
| | | |
| | | // static MqttCommon() |
| | | // { |
| | | // InitMqtt(); |
| | | // } |
| | | |
| | | // public static bool IsInitMqtt = false; |
| | | |
| | | |
| | | |
| | | // static void InitMqtt() |
| | | // { |
| | | // new System.Threading.Thread(async () => |
| | | // { |
| | | // while (true) |
| | | // { |
| | | // try |
| | | // { |
| | | // System.Threading.Thread.Sleep(500); |
| | | |
| | | // if (!MainPage.IsRemote) continue; |
| | | |
| | | // //if (BusSocket.IsEnterBackground) continue; |
| | | |
| | | // await StartCloudMqtt(); |
| | | // await SubscribeTopics(); |
| | | // } |
| | | // catch { } |
| | | // } |
| | | // }) |
| | | // { IsBackground = true }.Start(); |
| | | // } |
| | | |
| | | // /// <summary> |
| | | // /// 初始化状态 |
| | | // /// </summary> |
| | | // public static void InitState() |
| | | // { |
| | | // IfNeedReadAllDeviceStatus = true; |
| | | // bNeedStartTip = true; |
| | | // bNeedConnectTip = true; |
| | | // IsGatewayOnline = false; |
| | | // StartCloudMqtt(); |
| | | |
| | | // } |
| | | |
| | | // static bool isSubscribeSuccess; |
| | | // static async Task SubscribeTopics() |
| | | // { |
| | | // if (remoteIsConnected && !isSubscribeSuccess) |
| | | // { |
| | | // try |
| | | // { |
| | | |
| | | // //var topicFilterPush = new TopicFilter { QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce, |
| | | // // Topic = $"/BusGateWayToClient/{MqttInfoConfig.Instance.CurRemoteMACInfo.clientId}/Push/NotifySqueeze" }; |
| | | |
| | | // //2020-05-14 订阅主题质量改为0 |
| | | // var topicFilterBusGateWayToClient = new MqttTopicFilter() |
| | | // { |
| | | // Topic = $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.id}/#", |
| | | // QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce |
| | | // //QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce |
| | | // }; |
| | | |
| | | // var topicFilterPush2 = new MqttTopicFilter |
| | | // { |
| | | // Topic = $"/BusGateWayToClient/{MainPage.LoginUser.ID}/#", |
| | | // QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce, |
| | | // //QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce |
| | | // }; |
| | | |
| | | // Utlis.WriteLine("开始订阅!"); |
| | | // var result = await RemoteMqttClient.SubscribeAsync(new MqttTopicFilter[] { topicFilterBusGateWayToClient, topicFilterPush2 }); |
| | | // if (result.Items[0].ResultCode == MQTTnet.Client.Subscribing.MqttClientSubscribeResultCode.GrantedQoS0) |
| | | // { |
| | | // isSubscribeSuccess = true; |
| | | // Utlis.WriteLine("订阅成功!"); |
| | | |
| | | // MqttRemoteSend(new byte[] { 0 }, 3); |
| | | |
| | | // //读取搜索网关,判断网关是否在线 |
| | | // CheckGatewaysIfOnline(); |
| | | |
| | | // //连接成功后检测是否需要通过远程获取Key |
| | | // CheckIfNeedGetLocalPasswordFromRemote(); |
| | | // } |
| | | |
| | | |
| | | // } |
| | | // catch (Exception ex) |
| | | // { |
| | | // Console.WriteLine("订阅catch:" + ex.Message.ToString()); |
| | | // } |
| | | // } |
| | | // } |
| | | |
| | | // static DateTime mFlagDateTime; |
| | | |
| | | // //static readonly object SendLocker = new object (); |
| | | // /// <summary> |
| | | // /// 启动远程Mqtt |
| | | // /// </summary> |
| | | // public static async Task StartCloudMqtt() |
| | | // { |
| | | |
| | | // if (UserConfig.Instance.internetStatus == 0) |
| | | // { |
| | | // return; |
| | | // } |
| | | |
| | | // if (!UserInfo.Current.IsLogin) |
| | | // { |
| | | // return; |
| | | // } |
| | | |
| | | // //追加:没有远程连接的权限 |
| | | // if (remoteMqttIsConnecting || remoteIsConnected) |
| | | // { |
| | | // return; |
| | | // } |
| | | |
| | | // //lock (SendLocker) { |
| | | // // remoteMqttIsConnecting = true; |
| | | // //} |
| | | // remoteMqttIsConnecting = true; |
| | | //#if DEBUG |
| | | // Utlis.WriteLine($"StartCloudMqtt: 开始"); |
| | | //#endif |
| | | |
| | | // await Task.Factory.StartNew(async () => |
| | | // { |
| | | // try |
| | | // { |
| | | // //lock (RemoteMqttClient) { |
| | | // // //表示后面将进行连接 |
| | | // // remoteMqttIsConnecting = true; |
| | | |
| | | // #region 初始化远程Mqtt |
| | | |
| | | // RemoteMqttClient = new MqttFactory().CreateMqttClient(); |
| | | |
| | | |
| | | // //(1)当[连接云端的Mqtt成功后]或者[以及后面App通过云端Mqtt转发数据给网关成功后],处理接收到云端数据包响应时在mqttServerClient_ApplicationMessageReceived这个方法处理 |
| | | // if (RemoteMqttClient.ApplicationMessageReceivedHandler == null) |
| | | // { |
| | | // RemoteMqttClient.UseApplicationMessageReceivedHandler((e) => |
| | | // { |
| | | // try |
| | | // { |
| | | // var topic = e.ApplicationMessage.Topic; |
| | | // if (topic == $"/BusGateWayToClient/{UserInfo.Current.ID}" + PushNotifySqueeze) |
| | | // { |
| | | // var mMes = CommonPage.MyEncodingUTF8.GetString(e.ApplicationMessage.Payload); |
| | | // //新挤下线主题方案 收到挤下线主题 |
| | | // ReceiveNotifySqueezeAsync(mMes); |
| | | // } |
| | | // else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.id}/NotifyBusGateWayInfoChange") |
| | | // {//网关上线,需要更新aeskey |
| | | // //收到网关上线消息主题 |
| | | // ReceiveNotifyBusGateWayInfoChange(); |
| | | // } |
| | | // else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.id}/NotifyGateWayOffline") |
| | | // {//网关掉线 //----第二步:读取账号下面的网关列表 |
| | | // ReceiveNotifyGateWayOffline(); |
| | | // } |
| | | // else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.id}/Common/CheckGateway") |
| | | // { |
| | | // var ss = CommonPage.MyEncodingUTF8.GetString(e.ApplicationMessage.Payload); |
| | | // ReceiveCheckGateway(ss); |
| | | // } |
| | | // else |
| | | // { |
| | | |
| | | // SetGatewayOnlineResetCheck(); |
| | | |
| | | // var packet = new Packet(); |
| | | |
| | | // if (!string.IsNullOrEmpty(mqttEncryptKey)) |
| | | // { |
| | | // packet.Bytes = Shared.Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, mqttEncryptKey); |
| | | // } |
| | | // else |
| | | // { |
| | | // packet.Bytes = e.ApplicationMessage.Payload; |
| | | // } |
| | | // packet.IsLocal = false; |
| | | // packet.Manager(); |
| | | // } |
| | | // } |
| | | // catch { } |
| | | // }); |
| | | // } |
| | | |
| | | // //(2)DisconnectedHandler |
| | | // if (RemoteMqttClient.DisconnectedHandler == null) |
| | | // { |
| | | // RemoteMqttClient.UseDisconnectedHandler(async (e) => |
| | | // { |
| | | // Utlis.WriteLine($"远程连接断开"); |
| | | // isSubscribeSuccess = false; |
| | | // await DisConnectRemoteMqttClient("UseDisconnectedHandler"); |
| | | |
| | | // }); |
| | | // } |
| | | // //(3)ConnectedHandler |
| | | // if (RemoteMqttClient.ConnectedHandler == null) |
| | | // { |
| | | // RemoteMqttClient.UseConnectedHandler(async (e) => |
| | | // { |
| | | // IfNeedReadAllDeviceStatus = true; |
| | | // bNeedStartTip = true;// |
| | | // bNeedConnectTip = true; |
| | | // Utlis.WriteLine($"============>Mqtt远程连接成功"); |
| | | // SendPushSignOut(); |
| | | |
| | | // //if (CommonPage.IsRemote) { |
| | | // // //Utlis.ShowAppLinkStatus (AppLinkStatus.CloudLink); |
| | | // // MainPage.AddTip (Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.LinkSuccess)); |
| | | // //} |
| | | // }); |
| | | // } |
| | | // #endregion |
| | | // //} |
| | | |
| | | // //(4)===========开始连接过程========== |
| | | // //之前已经获取参数成功过 |
| | | // if (MqttInfoConfig.Current.IfGetMqttInfoSuccess) |
| | | // { |
| | | // //判断是否需要重新获取 |
| | | // await CheckMQTTConnectAsync(); |
| | | // } |
| | | // else |
| | | // { |
| | | // //开始获取远程连接参数 |
| | | // await StartMQTTGetInfo(); |
| | | // } |
| | | |
| | | // } |
| | | // catch (Exception ex) |
| | | // { |
| | | // Utlis.WriteLine($"error:" + ex.Message); |
| | | // if (IfDEBUG) |
| | | // { |
| | | // Utlis.ShowAlertOnMainThread("error: " + ex.Message); |
| | | // } |
| | | // } |
| | | // finally |
| | | // { |
| | | // //最终要释放连接状态 |
| | | // remoteMqttIsConnecting = false; |
| | | |
| | | // //lock (SendLocker) { |
| | | // // remoteMqttIsConnecting = false; |
| | | // //} |
| | | |
| | | // //连接成功才关闭Loading |
| | | // if (remoteIsConnected) |
| | | // { |
| | | // //MainPage.LoadingTipHide(); |
| | | // ERRORCount = 0; |
| | | // } |
| | | // else |
| | | // { |
| | | // ERRORCount++; |
| | | // //每5次重新提示一次 |
| | | // if (ERRORCount > 5) |
| | | // { |
| | | // ERRORCount = 0; |
| | | // //MainPage.AddTip(Language.StringByID(Shared.SimpleControl.R.MyInternationalizationString.RemoteFailure)); |
| | | // //MainPage.LoadingTipHide(); |
| | | |
| | | // } |
| | | // } |
| | | |
| | | //#if DEBUG |
| | | // Utlis.WriteLine($"StartCloudMqtt: 结束"); |
| | | //#endif |
| | | // } |
| | | |
| | | // }); |
| | | // } |
| | | |
| | | // //static bool |
| | | |
| | | |
| | | // /// <summary> |
| | | // /// 检查网关是否在线线程 |
| | | // /// </summary> |
| | | // static System.Threading.Thread CheckGatewaysThead; |
| | | |
| | | // /// <summary> |
| | | // /// 检查网关是否在线 |
| | | // /// </summary> |
| | | // static void CheckGatewaysIfOnline() |
| | | // { |
| | | // if (GatewayCommon == null) return; |
| | | |
| | | // try |
| | | // { |
| | | // //提示搜索网关中 |
| | | // //MainPage.LoadingTipShow(Language.StringByID(Shared.SimpleControl.R.MyInternationalizationString.SubscriptionSuccessful)); |
| | | |
| | | // if (CheckGatewaysThead != null) |
| | | // CheckGatewaysThead.Abort(); |
| | | |
| | | // //CheckGatewaysThead = new System.Threading.Thread(() => { |
| | | // // var returnBytes = Control.ControlBytesSendHasReturn(Command.ReadGateway, GatewayCommon.SubnetID, GatewayCommon.DeviceID, new byte[] { (byte)new Random().Next(255), (byte)new Random().Next(255) }); |
| | | // // //隐藏提示 |
| | | // // MainPage.LoadingTipHide(); |
| | | // // if (CommonPage.IsRemote) |
| | | // // { |
| | | // // if (returnBytes == null) |
| | | // // { |
| | | // // Application.RunOnMainThread(() => { |
| | | // // IsGatewayOnline = false; |
| | | // // Utlis.ShowAppLinkStatus(AppLinkStatus.CloudOffline); |
| | | // // MainPage.AddTip(Language.StringByID(Shared.SimpleControl.R.MyInternationalizationString.RemoteFailedGatewayOffline)); |
| | | // // //发送一次CheckGateway主题 |
| | | // // MqttRemoteSend(new byte[] { 0 }, 3); |
| | | // // }); |
| | | // // } |
| | | // // else |
| | | // // { |
| | | // // MainPage.AddTip(Language.StringByID(Shared.SimpleControl.R.MyInternationalizationString.LinkSuccess)); |
| | | |
| | | // // IsGatewayOnline = true; |
| | | // // Utlis.ShowAppLinkStatus(AppLinkStatus.CloudLink); |
| | | // // Console.WriteLine("ReadGateway success"); |
| | | // // CheckIfNeedReadAllDeviceStatus(); |
| | | // // } |
| | | // // } |
| | | // //}) |
| | | // //{ IsBackground = true }; |
| | | |
| | | // //CheckGatewaysThead.Start(); |
| | | // } |
| | | // catch |
| | | // { |
| | | |
| | | // } |
| | | // } |
| | | |
| | | |
| | | // /// <summary> |
| | | // /// 检测是否需要发送刷新获取所有设备的命令 |
| | | // /// </summary> |
| | | // static void CheckIfNeedReadAllDeviceStatus() |
| | | // { |
| | | // if (IfNeedReadAllDeviceStatus) |
| | | // { |
| | | // Utlis.WriteLine("ReadAllDeviceStatus"); |
| | | // IfNeedReadAllDeviceStatus = false; |
| | | // //Shared.SimpleControl.Phone.UserMiddle.ReadAllDeviceStatus(); |
| | | // } |
| | | |
| | | // } |
| | | |
| | | |
| | | |
| | | // //连接错误次数统计 |
| | | // static int ERRORCount = 0; |
| | | |
| | | // /// <summary> |
| | | // /// 是否需要提示 |
| | | // /// </summary> |
| | | // static bool bNeedConnectTip = true; |
| | | |
| | | // /// <summary> |
| | | // /// 从开始到连接成功,只提示1次 |
| | | // /// </summary> |
| | | // static bool bNeedStartTip = true; |
| | | // /// <summary> |
| | | // /// 正在获取连接参数... |
| | | // /// </summary> |
| | | // static void ShowStartTip() |
| | | // { |
| | | // if (bNeedStartTip) |
| | | // { |
| | | // bNeedStartTip = false; |
| | | // if (MainPage.IsRemote) |
| | | // { |
| | | // //MainPage.LoadingTipShow(Language.StringByID(Shared.SimpleControl.R.MyInternationalizationString.GettingRemoteConnectionInfo)); |
| | | // } |
| | | // } |
| | | |
| | | // } |
| | | |
| | | // /// <summary> |
| | | // /// 检测之前获取的Mac与当前住宅MAC是否一致 不一致从新获取 |
| | | // /// </summary> |
| | | // /// <returns></returns> |
| | | // static async Task CheckMQTTConnectAsync() |
| | | // { |
| | | // //try |
| | | // //{ |
| | | // // if (MqttInfoConfig.Current.HomeGatewayInfo != null && MqttInfoConfig.Current.HomeGatewayInfo.mac == UserConfig.Instance.GatewayMAC) |
| | | // // { |
| | | // // await MQTTConnectAsync(); |
| | | // // } |
| | | // // else |
| | | // // { |
| | | // // //Mac 变化了重新获取参数 |
| | | // // await StartMQTTGetInfo(); |
| | | // // } |
| | | // //} |
| | | // //catch |
| | | // //{ |
| | | // // MqttInfoConfig.Current.IfGetMqttInfoSuccess = false; |
| | | // //} |
| | | |
| | | // } |
| | | |
| | | // /// <summary> |
| | | // /// 开始获取Mqtt 远程参数 |
| | | // /// </summary> |
| | | // /// <returns></returns> |
| | | // static async Task StartMQTTGetInfo() |
| | | // { |
| | | // ShowStartTip(); |
| | | |
| | | // if (!MainPage.IsRemote) |
| | | // { |
| | | // return; |
| | | // } |
| | | |
| | | // await GetMqttInfoAndMQTTConnectAsync(); |
| | | |
| | | // ////--判断是当前是否分享的住宅 |
| | | // //if (!UserConfig.Instance.CurrentRegion.IsOthreShare) { |
| | | // // //主账号获取MQTT 远程链接信息,并连接 |
| | | // // await GetMqttInfoAndMQTTConnectAsync (); |
| | | // //} else { |
| | | // // //如果是分享过来的住宅 走下面流程 |
| | | // // //--第一步:获取当前住分享宅网关信息并连接MQTT |
| | | // // await GetSingleHomeGatewayPaggerAndMQTTConnectAsync (); |
| | | // //} |
| | | // } |
| | | |
| | | |
| | | // /// <summary> |
| | | // /// 连接MQTT |
| | | // /// </summary> |
| | | // static async Task MQTTConnectAsync() |
| | | // { |
| | | |
| | | // if (!MainPage.IsRemote) |
| | | // { |
| | | // return; |
| | | // } |
| | | |
| | | // //if (MqttInfoConfig.Current.HomeGatewayInfo != null && MqttInfoConfig.Current.mMqttInfo != null) |
| | | // //{ |
| | | // // if (bNeedConnectTip) |
| | | // // { |
| | | // // bNeedConnectTip = false; |
| | | // // MainPage.LoadingTipShow(Language.StringByID(Shared.SimpleControl.R.MyInternationalizationString.GetSuccessfullyStartConnecting)); |
| | | // // } |
| | | |
| | | // // try |
| | | // // { |
| | | |
| | | // // var url = MqttInfoConfig.Current.mMqttInfo.url; |
| | | |
| | | // // //url = HttpUtil.GetProxyEMQUrl (url); |
| | | // // //#if DEBUG |
| | | // // // url = HttpUtil.GetProxyEMQUrl (url); |
| | | |
| | | // // //#endif |
| | | // // var clientId = MqttInfoConfig.Current.mMqttInfo.clientId; |
| | | // // var username = MqttInfoConfig.Current.mMqttInfo.userName; |
| | | // // var passwordRemote = MqttInfoConfig.Current.mMqttInfo.passWord; |
| | | // // //获取参数成功,保存到本地并标记为true |
| | | // // MqttInfoConfig.Current.IfGetMqttInfoSuccess = true; |
| | | // // MqttInfoConfig.Current.Save(); |
| | | |
| | | // // mqttEncryptKey = MqttInfoConfig.Current.HomeGatewayInfo.aesKey; |
| | | |
| | | // // var options1 = new MQTTnet.Client.Options.MqttClientOptionsBuilder() |
| | | // // .WithClientId(clientId) |
| | | // // .WithTcpServer(url.Split(':')[1].Substring("//".Length), int.Parse(url.Split(':')[2])) |
| | | // // .WithCredentials(username, passwordRemote) |
| | | // // .WithCleanSession() |
| | | // // .WithProtocolVersion(MQTTnet.Formatter.MqttProtocolVersion.V311) |
| | | // // .WithCommunicationTimeout(new TimeSpan(0, 0, 10)) |
| | | // // //.WithCommunicationTimeout (new TimeSpan (0, 0, 5)) |
| | | // // //.WithCommunicationTimeout (new TimeSpan (0, 1, 0)) |
| | | // // .Build(); |
| | | |
| | | // // await DisConnectRemoteMqttClient("StartRemoteMqtt"); |
| | | |
| | | // // var mResult = await RemoteMqttClient.ConnectAsync(options1); |
| | | |
| | | // // if (mResult.ResultCode == MQTTnet.Client.Connecting.MqttClientConnectResultCode.Success) |
| | | // // { |
| | | // // remoteIsConnected = true; |
| | | // // IsDisConnectingWithSendCatch = false; |
| | | // // UnsupportedProtocolVersionCount = 0; |
| | | // // } |
| | | // // else |
| | | // // { |
| | | // // //重新中心服务器获取参数标记 |
| | | // // MqttInfoConfig.Current.IfGetMqttInfoSuccess = false; |
| | | // // } |
| | | |
| | | // // } |
| | | // // catch (Exception ex) |
| | | // // { |
| | | |
| | | // // if (ex.Message == MqttCommunicationTimedOutException) |
| | | // // { |
| | | // // Console.WriteLine("Connect error TimedOut: " + ex.Message); |
| | | // // } |
| | | // // else |
| | | // // { |
| | | // // //重新中心服务器获取参数标记 |
| | | // // MqttInfoConfig.Current.IfGetMqttInfoSuccess = false; |
| | | // // Console.WriteLine("Connect error: " + ex.Message); |
| | | // // } |
| | | |
| | | // // //Console.WriteLine ("Connect error: " + ex.Message); |
| | | // // if (IfDEBUG) |
| | | // // { |
| | | // // MainPage.ShowAlertOnMainThread("Connect error: " + ex.Message); |
| | | // // } |
| | | // // } |
| | | // // finally |
| | | // // { |
| | | |
| | | // // } |
| | | |
| | | // //} |
| | | // //else |
| | | // //{ |
| | | // // MqttInfoConfig.Current.IfGetMqttInfoSuccess = false; |
| | | // //} |
| | | |
| | | // } |
| | | |
| | | // static int UnsupportedProtocolVersionCount = 0; |
| | | // static readonly string MqttCommunicationTimedOutException = "Exception of type 'MQTTnet.Exceptions.MqttCommunicationTimedOutException' was thrown."; |
| | | // // |
| | | // static readonly string UnsupportedProtocolVersion = "Connecting with MQTT server failed (UnsupportedProtocolVersion)."; |
| | | |
| | | |
| | | |
| | | // /// <summary> |
| | | // /// 收到网关上线消息 |
| | | // /// </summary> |
| | | // static void ReceiveNotifyBusGateWayInfoChange() |
| | | // { |
| | | // try |
| | | // { |
| | | // SetGatewayOnlineResetCheck(); |
| | | // if (MainPage.IsRemote) |
| | | // { |
| | | // //#if DEBUG |
| | | // //MainPage.AddTip(Language.StringByID(Shared.SimpleControl.R.MyInternationalizationString.GatewayLoginOnline)); |
| | | // //#endif |
| | | // CheckIfNeedReadAllDeviceStatus(); |
| | | // } |
| | | |
| | | // GetSingleHomeGatewayPaggerAndMQTTConnectAsync(false); |
| | | |
| | | // } |
| | | // catch { } |
| | | // } |
| | | |
| | | // /// <summary> |
| | | // /// 收到网关掉线信息 |
| | | // /// </summary> |
| | | // static void ReceiveNotifyGateWayOffline() |
| | | // { |
| | | // Utlis.WriteLine("============>Mqtt GateWayOffline"); |
| | | // IsGatewayOnline = false; |
| | | // if (MainPage.IsRemote) |
| | | // { |
| | | // //Utlis.ShowAppLinkStatus(AppLinkStatus.CloudOffline); |
| | | // //MainPage.AddTip(Language.StringByID(Shared.SimpleControl.R.MyInternationalizationString.GatewayOffline)); |
| | | |
| | | // } |
| | | // } |
| | | |
| | | |
| | | // /// <summary> |
| | | // /// 收到挤下线推送 |
| | | // /// </summary> |
| | | // static void ReceiveNotifySqueezeAsync(string mMes) |
| | | // { |
| | | |
| | | // if (mMes == PushSignStr) return;//是自己的登录推送不处理 |
| | | |
| | | // //断开远程连接 |
| | | // MainPage.IsRemote = false; |
| | | // if (!UserInfo.Current.IsLogin) |
| | | // { |
| | | // return; |
| | | // } |
| | | |
| | | // DisConnectRemoteMqttClient("挤下线"); |
| | | |
| | | // UserInfo.Current.lastTime = DateTime.MinValue; |
| | | // UserInfo.Current.SaveUserInfo(); |
| | | // //Room.Lists.Clear(); |
| | | |
| | | // Application.RunOnMainThread(() => |
| | | // { |
| | | // //Utlis.ShowAppLinkStatus(AppLinkStatus.WiFi); |
| | | // //new Shared.SimpleControl.Phone.AccountLogin(MainPage.LoginUser.AccountString, "").Show(); |
| | | // //SharedMethod.SharedMethod.CurPageLayout = null; |
| | | // ////CommonPage.IsRemote = false; |
| | | |
| | | // //MainPage.LoadingTipHide(); |
| | | // //MainPage.Loading.Hide(); |
| | | |
| | | // //new Alert(Language.StringByID(Shared.SimpleControl.R.MyInternationalizationString.Tip), Language.StringByID(Shared.SimpleControl.R.MyInternationalizationString.LoggedOnOtherDevices), |
| | | // // Language.StringByID(Shared.SimpleControl.R.MyInternationalizationString.Close)).Show(); |
| | | // }); |
| | | |
| | | // //2020-08-11 删除推送数据 |
| | | // //HDLRequest.Current.PushserivceSignOut (); |
| | | |
| | | // //#if HDL |
| | | // // if (!String.IsNullOrEmpty (MainPage.LoginUser.AllVisionRegisterDevUserNameGuid)) { |
| | | // // com.freeview.global.Video.Logout (); |
| | | // // }///BusGateWayToClient/320c1fea-1866-4708-8277-e2321a4dd236/NotifyGateWayInfoChange |
| | | // //#endif |
| | | |
| | | |
| | | // } |
| | | |
| | | // /// <summary> |
| | | // /// 收到CheckGateway主题 |
| | | // /// </summary> |
| | | // static void ReceiveCheckGateway(string mMes) |
| | | // { |
| | | // if (!MainPage.IsRemote) return; |
| | | |
| | | // Utlis.WriteLine("ReceiveCheckGateway!"); |
| | | |
| | | // //CheckIfNeedReadAllDeviceStatus (); |
| | | |
| | | // var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponsePack>(mMes); |
| | | // if (obj == null) |
| | | // { |
| | | // //SetGatewayOnlineResetCheck (); |
| | | // return; |
| | | // } |
| | | |
| | | // //switch (obj.StateCode) |
| | | // //{ |
| | | // // case "HDLUdpDataForwardServerMqttClientNoOnLine": |
| | | // // case "NoOnline": |
| | | // // case "NetworkAnomaly"://不在线 |
| | | // // IsGatewayOnline = false; |
| | | // // MainPage.AddTip(Language.StringByID(Shared.SimpleControl.R.MyInternationalizationString.RemoteFailedGatewayOffline)); |
| | | // // Utlis.ShowAppLinkStatus(AppLinkStatus.CloudOffline); |
| | | |
| | | // // break; |
| | | // // case "NoRecord"://MAC不正确 |
| | | // // IsGatewayOnline = false; |
| | | // // MainPage.AddTip(Language.StringByID(Shared.SimpleControl.R.MyInternationalizationString.MACError)); |
| | | // // Utlis.ShowAppLinkStatus(AppLinkStatus.CloudOffline); |
| | | // // break; |
| | | // // case "Success": |
| | | // // SetGatewayOnlineResetCheck(); |
| | | // // IsGatewayOnline = true; |
| | | // // //MainPage.AddTip (UserConfig.Instance.CurrentRegion.Name + ":" + Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.LinkSuccess)); |
| | | // // break; |
| | | // // default: |
| | | // // IsGatewayOnline = false; |
| | | // // MainPage.AddTip(Language.StringByID(Shared.SimpleControl.R.MyInternationalizationString.LinkLoser)); |
| | | // // Utlis.ShowAppLinkStatus(AppLinkStatus.CloudOffline); |
| | | // // break; |
| | | // //} |
| | | |
| | | // } |
| | | |
| | | // /// <summary> |
| | | // /// 推送挤下线主题 |
| | | // /// </summary> |
| | | // static void SendPushSignOut() |
| | | // { |
| | | // byte[] message = CommonPage.MyEncodingUTF8.GetBytes(PushSignStr); |
| | | // MqttRemoteSend(message, 4); |
| | | // } |
| | | |
| | | // /// <summary> |
| | | // /// |
| | | // /// </summary> |
| | | // /// <param name="message">附加数据包</param> |
| | | // /// <param name="optionType">操作类型:0=网关控制;1=订阅网关数据;2=订阅网关上线数据</param> |
| | | // /// <returns></returns> |
| | | // public static async Task MqttRemoteSend(byte[] message, int optionType = 0) |
| | | // { |
| | | // //return; |
| | | |
| | | // try |
| | | // { |
| | | // string topicName; |
| | | // switch (optionType) |
| | | // { |
| | | // case 0: |
| | | // topicName = $"/ClientToBusGateWay/{MqttInfoConfig.Current.HomeGatewayInfo.id}/Common/ON"; |
| | | // if (!string.IsNullOrEmpty(mqttEncryptKey)) |
| | | // { |
| | | // message = Shared.Securitys.EncryptionService.AesEncryptPayload(message, mqttEncryptKey); |
| | | // } |
| | | // await RemoteMqttClient.PublishAsync(new MqttApplicationMessage { Topic = topicName, Payload = message, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce }); |
| | | // break; |
| | | // case 3: |
| | | // topicName = $"/ClientToBusGateWay/{MqttInfoConfig.Current.HomeGatewayInfo.id}/Common/CheckGateway"; |
| | | // Shared.Utlis.WriteLine("CheckGateway"); |
| | | // await RemoteMqttClient.PublishAsync(new MqttApplicationMessage { Topic = topicName, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce }); |
| | | // break; |
| | | // case 4://发布新方案的挤下线主题 |
| | | // topicName = $"/BusGateWayToClient/{MainPage.LoginUser.ID}" + PushNotifySqueeze; |
| | | // //message = CommonPage.MyEncodingUTF8.GetBytes (PushSignStr); |
| | | // await RemoteMqttClient.PublishAsync(new MqttApplicationMessage { Topic = topicName, Payload = message, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce }); |
| | | // break; |
| | | // } |
| | | // } |
| | | // catch (Exception e) |
| | | // { |
| | | // //Utlis.WriteLine ($"============>Mqtt MqttRemoteSend catch"); |
| | | // if (!IsDisConnectingWithSendCatch) |
| | | // { |
| | | // IsDisConnectingWithSendCatch = true; |
| | | // await DisConnectRemoteMqttClient("SendCatch"); |
| | | // } |
| | | // } |
| | | // } |
| | | // /// <summary> |
| | | // /// SendCatch 后执行一次断开操作 |
| | | // /// </summary> |
| | | // static bool IsDisConnectingWithSendCatch = false; |
| | | |
| | | |
| | | // /// <summary> |
| | | // /// 是否需要读取一次所有设备状态 |
| | | // /// </summary> |
| | | // static bool IfNeedReadAllDeviceStatus = true; |
| | | // public static bool IsGatewayOnline = true; |
| | | // //static int CheckGatewayCount = 0; |
| | | // //static DateTime mCheckGatewayTime; |
| | | |
| | | |
| | | // /// <summary> |
| | | // /// 设置网关在线标志,并重置CheckGateway参数 |
| | | // /// </summary> |
| | | // static void SetGatewayOnlineResetCheck() |
| | | // { |
| | | |
| | | // //mCheckGatewayTime = DateTime.Now; |
| | | // //CheckGatewayCount = 0; |
| | | // if (MainPage.IsRemote) |
| | | // { |
| | | // if (!IsGatewayOnline) |
| | | // { |
| | | // IsGatewayOnline = true; |
| | | // //Utlis.ShowAppLinkStatus(AppLinkStatus.CloudLink); |
| | | // } |
| | | // } |
| | | // } |
| | | |
| | | |
| | | // /// <summary> |
| | | // /// 主账号获取MQTT 远程链接信息,并连接 |
| | | // /// </summary> |
| | | // /// <returns></returns> |
| | | // static async Task GetMqttInfoAndMQTTConnectAsync() |
| | | // { |
| | | // var mqttInfoRequestResult_Obj = new HttpServerRequest().GetMqttRemoteInfo(GetRandomKey()); |
| | | // if (mqttInfoRequestResult_Obj != null) |
| | | // { |
| | | // //MqttInfoConfig.Current.mMqttInfo = mqttInfoRequestResult_Obj; |
| | | // //if (UserConfig.Instance.GatewayList != null && UserConfig.Instance.GatewayList.Count > 0) |
| | | // //{ |
| | | // // //----第二步找出是否存在匹配当前住宅的mac,存在再进行远程。 |
| | | // // MqttInfoConfig.Current.HomeGatewayInfo = UserConfig.Instance.GatewayList[0]; |
| | | // // if (MqttInfoConfig.Current.HomeGatewayInfo != null) |
| | | // // { |
| | | // // //----第三步 开始连接 |
| | | // // await MQTTConnectAsync(); |
| | | // // } |
| | | // //} |
| | | // //else |
| | | // //{ |
| | | // // Utlis.WriteLine("============>还没绑定网关"); |
| | | // //} |
| | | |
| | | // } |
| | | // else |
| | | // { |
| | | // Utlis.WriteLine("============>MqttInfo null"); |
| | | // } |
| | | |
| | | // } |
| | | |
| | | |
| | | // /// <summary> |
| | | // /// 分享住宅 获取当前住宅网关信息并且连接MQTT 或者刷新 |
| | | // /// 2020-03-17 |
| | | // /// </summary> |
| | | // static async Task GetSingleHomeGatewayPaggerAndMQTTConnectAsync(bool bNeedConnect = true) |
| | | // { |
| | | // ////--第一步:请求当前住宅到网关信息 |
| | | // //var requestObj3 = new GetSingleHomeGatewayPaggerObj (); |
| | | // //requestObj3.ReqDto.LoginAccessToken = MainPage.LoginUser.LoginTokenString; |
| | | // //requestObj3.ReqDto.HomeId = UserConfig.Instance.CurrentRegion.Id; |
| | | // //requestObj3.ReqDto.PageSetting.Page = 1; |
| | | // //requestObj3.ReqDto.PageSetting.PageSize = 10; |
| | | // //string urlHead = MainPage.RequestHttpsHostZigbee; |
| | | // //if (requestObj3.IsOtherAccountCtrl) { |
| | | // // urlHead = UserConfig.Instance.MasterAccountRequestBaseUrl; |
| | | // // requestObj3.ReqDto.LoginAccessToken = UserConfig.Instance.MasterAccountToken; |
| | | // //} |
| | | |
| | | // ////urlHead = MainPage.RequestHttpsHostZigbee; |
| | | |
| | | // //var requestJson3 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj3); |
| | | // //var revertObj3 = MainPage.RequestHttpsNew (API.GetSingleHomeGatewayPagger, requestJson3, urlHead); |
| | | // //if (revertObj3.StateCode.ToUpper () == HttpUtil.SUCCESS_CODE) { |
| | | // // var infoResult = Newtonsoft.Json.JsonConvert.DeserializeObject<GetGatewayResult> (revertObj3.ResponseData.ToString ()); |
| | | // // if (bNeedConnect) { |
| | | // // UserConfig.Instance.SetNowHomeGateways (infoResult.PageData); |
| | | |
| | | // // //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); |
| | | // // if (mGatewayRes != null) { |
| | | // // MqttInfoConfig.Instance.CurRemoteMACInfo = new RemoteMACInfo (); |
| | | // // MqttInfoConfig.Instance.CurRemoteMACInfo.aesKey = mGatewayRes.AesKey; |
| | | // // MqttInfoConfig.Instance.CurRemoteMACInfo.mac = mGatewayRes.GatewayUniqueId; |
| | | // // MqttInfoConfig.Instance.CurRemoteMACInfo.macMark = mGatewayRes.MacMark; |
| | | // // MqttInfoConfig.Instance.CurRemoteMACInfo.isValid = mGatewayRes.MqttOnlineStatus ? "Valid" : "InValid"; |
| | | // // //await MQTTConnectAsync (); |
| | | |
| | | // // var mqttInfoRequestPar = new ShareMemberConnMqttInfoObj () { |
| | | // // LoginAccessToken = MainPage.LoginUser.LoginTokenString, |
| | | // // PlatformStr = GetRandomKey(), |
| | | // // PublishPayloadJsonStr = PushSignStr, |
| | | // // MainUserDistributedMark = UserConfig.Instance.CurrentRegion.MainUserDistributedMark, |
| | | // // HomeId = UserConfig.Instance.CurrentRegion.Id, |
| | | // // IsRedirectSelectEmqServer = mGatewayRes.ExtensionPara//是否是新网关,是否自动选择服务器 |
| | | // // }; |
| | | // // var mqttInfoRequestResult = MainPage.RequestHttpsNew (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) { |
| | | // // MqttInfoConfig.Instance.mMqttInfo = mqttInfoRequestResult_info; |
| | | |
| | | // // //--第三步:连接mqtt |
| | | // // await MQTTConnectAsync (); |
| | | // // } |
| | | |
| | | // // } else { |
| | | // // //Utlis.WriteLine ("============>Mqtt 取消连接,mqttInfoRequestResult"); |
| | | // // } |
| | | |
| | | |
| | | // // } |
| | | |
| | | |
| | | // // } else { |
| | | // // Shared.Utlis.WriteLine ("============>Mqtt 取消连接,当前住宅没绑定网关"); |
| | | // // } |
| | | // // } else { |
| | | // // var mGatewayRes = infoResult.PageData.Find ((obj) => obj.GatewayUniqueId == UserConfig.Instance.CurrentRegion.HomeGateways [0].GatewayUniqueId); |
| | | // // if (mGatewayRes != null) { |
| | | // // MqttInfoConfig.Instance.CurRemoteMACInfo.aesKey = mGatewayRes.AesKey; |
| | | // // mqttEncryptKey = MqttInfoConfig.Instance.CurRemoteMACInfo.isNewBusproGateway ? MqttInfoConfig.Instance.CurRemoteMACInfo.aesKey : ""; |
| | | // // //获取AesKey成功后更新并保存到本地 |
| | | // // MqttInfoConfig.Instance.Save (); |
| | | // // } |
| | | |
| | | // // } |
| | | |
| | | |
| | | // //} else { |
| | | // // //获取主人token |
| | | // // if (revertObj3.StateCode.ToUpper () == "HomeIdAndTokenNoConsistent") { |
| | | // // UserConfig.Instance.CheckIfNeedGetMasterAccountToken (); |
| | | // // } |
| | | // //} |
| | | |
| | | // } |
| | | // //} |
| | | |
| | | |
| | | |
| | | // } |
| | | |
| | | |
| | | public class RemoteRequestParameters |
| | | { |
| | | public string RequestVersion; |