/* 更新了EMQ连接方式 */ using System.Collections.Generic; using System; using MQTTnet.Client; using System.Threading.Tasks; using Shared; using MQTTnet; using System.Text; using System.Security.Cryptography; using System.IO; using HDL_ON.DriverLayer; namespace HDL_ON.DAL.Mqtt { public static class MqttClient { /// /// 加密通讯KEY /// static string mqttEncryptKey = ""; //static string checkGatewayTopicBase64 = ""; /// /// 挤下线主题 /// static readonly string PushNotifySqueeze = "/Push/NotifySqueeze"; /// /// 随机Key /// static string RandomKey = ""; static string GetRandomKey() { if (string.IsNullOrEmpty(RandomKey)) { //随机2位字符串 Random random = new Random(Guid.NewGuid().GetHashCode()); int num = random.Next(65, 91); RandomKey = Convert.ToChar(num).ToString(); } return RandomKey; } /// /// 远程MqttClient /// public static IMqttClient RemoteMqttClient = new MqttFactory().CreateMqttClient(); /// /// 推送标识 /// static string PushSignStr = System.DateTime.Now.Ticks.ToString(); /// /// 断开远程Mqtt的链接 /// static async 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 (Control.Ins.IsRemote) { //不是无网络 if (OnAppConfig.Instance.internetStatus != 0) { Control.Ins.GatewayOnline = false; } } } } catch (Exception e) { Utlis.WriteLine($"Remote断开通讯连接出异常:{e.Message}"); } } /// /// 断开远程Mqtt的链接 /// 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}"); } } /// /// 断开mqtt连接 /// /// 断开原因 /// 是否需要去中心服务器 重新获取参数 /// public static async Task DisConnectRemote(string s = "", bool reset = true) { if (reset) { MqttInfoConfig.Current.IfGetMqttInfoSuccess = false; } await DisConnectRemoteMqttClient(s); } //static DateTime dateTime = DateTime.MinValue; /// /// 外网的MQTT是否正在连接 /// public static bool remoteMqttIsConnecting; static bool remoteIsConnected; static MqttClient() { InitMqtt(); } public static bool IsInitMqtt = false; static void InitMqtt() { new System.Threading.Thread(async () => { while (true) { try { System.Threading.Thread.Sleep(500); //if (!UserInfo.Current.IsLogin) { // continue; //} if (!Control.Ins.IsRemote) continue; //if (BusSocket.IsEnterBackground) continue; await StartCloudMqtt(); await SubscribeTopics(); } catch { } } }) { IsBackground = true }.Start(); } /// /// 初始化状态 /// public static void InitState() { IfNeedReadAllDeviceStatus = true; 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/{UserInfo.Current.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); //连接成功后检测是否需要通过远程获取Key CheckIfNeedGetLocalPasswordFromRemote(); } } catch (Exception ex) { Console.WriteLine("订阅catch:" + ex.Message.ToString()); } } } static DateTime mFlagDateTime; //static readonly object SendLocker = new object (); /// /// 启动远程Mqtt /// public static async Task StartCloudMqtt() { if (OnAppConfig.Instance.internetStatus == 0) { return; } if (!UserInfo.Current.IsLogin) { return; } //追加:没有远程连接的权限 if (remoteMqttIsConnecting || remoteIsConnected) { return; } Utlis.WriteLine($"StartCloudMqtt: 开始"); await Task.Factory.StartNew(async () => { try { #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 = Encoding.UTF8.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 = Encoding.UTF8.GetString(e.ApplicationMessage.Payload); ReceiveCheckGateway(ss); } else { SetGatewayOnlineResetCheck(); var packet = new Packet(); if (!string.IsNullOrEmpty(mqttEncryptKey)) { packet.Bytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, mqttEncryptKey); } else { packet.Bytes = e.ApplicationMessage.Payload; } 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; Utlis.WriteLine($"============>Mqtt远程连接成功"); SendPushSignOut(); //if (Control.Ins.IsRemote) { // //Utlis.ShowAppLinkStatus (AppLinkStatus.CloudLink); // MainPage.AddTip (Language.StringByID (SimpleControl.R.MyInternationalizationString.LinkSuccess)); //} }); } #endregion //} //(4)===========开始连接过程========== //之前已经获取参数成功过 if (MqttInfoConfig.Current.IfGetMqttInfoSuccess) { //判断是否需要重新获取 await CheckMQTTConnectAsync(); } else { //开始获取远程连接参数 await StartMQTTGetInfo(); } } catch (Exception ex) { Utlis.WriteLine($"error:" + ex.Message); } finally { //最终要释放连接状态 remoteMqttIsConnecting = false; Utlis.WriteLine($"StartCloudMqtt: 结束"); } }); } /// /// 检测是否需要发送刷新获取所有设备的命令 /// static void CheckIfNeedReadAllDeviceStatus() { if (IfNeedReadAllDeviceStatus) { Utlis.WriteLine("ReadAllDeviceStatus"); IfNeedReadAllDeviceStatus = false; } } /// /// 检测之前获取的Mac与当前住宅MAC是否一致 不一致从新获取 /// /// static async Task CheckMQTTConnectAsync() { try { if (MqttInfoConfig.Current.HomeGatewayInfo != null && MqttInfoConfig.Current.HomeGatewayInfo.mac == Entity.DB_ResidenceData.residenceData.residenceGatewayMAC) { await MQTTConnectAsync(); } else { //Mac 变化了重新获取参数 await StartMQTTGetInfo(); } } catch { MqttInfoConfig.Current.IfGetMqttInfoSuccess = false; } } /// /// 开始获取Mqtt 远程参数 /// /// static async Task StartMQTTGetInfo() { if (!Control.Ins.IsRemote) { return; } await GetMqttInfoAndMQTTConnectAsync(); ////--判断是当前是否分享的住宅 //if (!UserConfig.Instance.CurrentRegion.IsOthreShare) { // //主账号获取MQTT 远程链接信息,并连接 // await GetMqttInfoAndMQTTConnectAsync (); //} else { // //如果是分享过来的住宅 走下面流程 // //--第一步:获取当前住分享宅网关信息并连接MQTT // await GetSingleHomeGatewayPaggerAndMQTTConnectAsync (); //} } /// /// 连接MQTT /// static async Task MQTTConnectAsync() { if (!Control.Ins.IsRemote) { return; } if (MqttInfoConfig.Current.HomeGatewayInfo != null && MqttInfoConfig.Current.mMqttInfo != null) { 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 void ReceiveNotifyBusGateWayInfoChange() { try { SetGatewayOnlineResetCheck(); if (Control.Ins.IsRemote) { CheckIfNeedReadAllDeviceStatus(); } } catch { } } /// /// 收到网关掉线信息 /// static void ReceiveNotifyGateWayOffline() { if (Control.Ins.IsRemote) { Control.Ins.GatewayOnline = false; } } /// /// 收到挤下线推送 /// static void ReceiveNotifySqueezeAsync(string mMes) { if (mMes == PushSignStr) return;//是自己的登录推送不处理 //断开远程连接 Control.Ins.IsRemote = false; if (!UserInfo.Current.IsLogin) { return; } DisConnectRemoteMqttClient("挤下线"); UserInfo.Current.LastTime = DateTime.MinValue; UserInfo.Current.SaveUserInfo(); Application.RunOnMainThread(() => { MainPage.GoLoginPage(UserInfo.Current); //弹窗提示被挤下线 }); //2020-08-11 删除推送数据 //HDLRequest.Current.PushserivceSignOut (); } /// /// 收到CheckGateway主题 /// static void ReceiveCheckGateway(string mMes) { if (!Control.Ins.IsRemote) return; Utlis.WriteLine("ReceiveCheckGateway!"); //CheckIfNeedReadAllDeviceStatus (); //var obj = Newtonsoft.Json.JsonConvert.DeserializeObject(mMes); Control.Ins.GatewayOnline = true; } /// /// 推送挤下线主题 /// static void SendPushSignOut() { byte[] message = Encoding.UTF8.GetBytes(PushSignStr); MqttRemoteSend(message, 4); } /// /// /// /// 附加数据包 /// 操作类型:0=网关控制;1=订阅网关数据;2=订阅网关上线数据 /// 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 = 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"; 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); 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"); } } } /// /// SendCatch 后执行一次断开操作 /// static bool IsDisConnectingWithSendCatch = false; /// /// 是否需要读取一次所有设备状态 /// static bool IfNeedReadAllDeviceStatus = true; //public static bool IsGatewayOnline = true; /// /// 设置网关在线标志,并重置CheckGateway参数 /// static void SetGatewayOnlineResetCheck() { if (Control.Ins.IsRemote) { Control.Ins.GatewayOnline = true; } } /// /// 主账号获取MQTT 远程链接信息,并连接 /// /// static async Task GetMqttInfoAndMQTTConnectAsync() { var mqttInfoRequestResult_Obj =new Server.HttpServerRequest().GetMqttRemoteInfo(GetRandomKey()); if (mqttInfoRequestResult_Obj != null) { MqttInfoConfig.Cur.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"); } } } } public class MqttInfo { /// /// /// public string url; /// /// /// public string clientId; /// /// /// public string userName; /// /// /// public string passWord; } namespace Securitys { public partial class EncryptionService { #region 加密 /// /// 加密主题为Base64 /// /// /// /// public static string AesEncryptTopic(string pToEncrypt, string key) { if (string.IsNullOrEmpty(pToEncrypt)) return null; if (string.IsNullOrEmpty(key)) return pToEncrypt; //需要加密内容的明文流 Byte[] toEncryptArray = Encoding.UTF8.GetBytes(pToEncrypt); //配置AES加密Key(密钥、向量、模式、填充) RijndaelManaged rm = new RijndaelManaged { Key = Encoding.UTF8.GetBytes(key), IV = Encoding.UTF8.GetBytes(key), Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7 }; //创建AES加密器对象 ICryptoTransform cTransform = rm.CreateEncryptor(); //使用AES将明文流转成密文字节数组 Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); //将AES生成的密文字节数组转成Base64字符串 return Convert.ToBase64String(resultArray, 0, resultArray.Length); } /// /// 加密负载为二进制流 /// /// /// /// public static byte[] AesEncryptPayload(byte[] toEncryptArray, string key) { if (string.IsNullOrEmpty(key)) return toEncryptArray; //配置AES加密Key(密钥、向量、模式、填充) var rm = new RijndaelManaged { Key = Encoding.UTF8.GetBytes(key), IV = Encoding.UTF8.GetBytes(key), Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7 }; //创建AES加密器对象 var cTransform = rm.CreateEncryptor(); //使用AES将明文流转成密文字节数组 return cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); } #endregion #region 解密 /// /// 解密主题数据 /// /// /// /// public static string AesDecryptTopic(string pToDecrypt, string key) { //AES密文Base64转成字符串 Byte[] toEncryptArray = Convert.FromBase64String(pToDecrypt); //配置AES加密Key(密钥、向量、模式、填充) RijndaelManaged rm = new RijndaelManaged { Key = Encoding.UTF8.GetBytes(key), IV = Encoding.UTF8.GetBytes(key), Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7 }; //创建AES解密器对象 ICryptoTransform cTransform = rm.CreateDecryptor(); //使用AES将密文流转成明文的字节数组 Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); //转成字符串 return Encoding.UTF8.GetString(resultArray); } /// /// 采用Aes解密负载数据 /// /// /// /// public static byte[] AesDecryptPayload(byte[] toEncryptArray, string key) { //配置AES加密Key(密钥、向量、模式、填充) var rm = new RijndaelManaged { Key = Encoding.UTF8.GetBytes(key), IV = Encoding.UTF8.GetBytes(key), Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7 }; //创建AES解密器对象 var cTransform = rm.CreateDecryptor(); //使用AES将密文流转成明文的字节数组 return cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); } #endregion } }