| | |
| | | /// 加密通讯KEY |
| | | /// </summary> |
| | | static string mqttEncryptKey = ""; |
| | | static string tuyaEncryptKey = ""; |
| | | //static string checkGatewayTopicBase64 = ""; |
| | | static bool hadGateway = true; |
| | | /// <summary> |
| | |
| | | /// </summary> |
| | | static string RandomKey = ""; |
| | | |
| | | /// <summary> |
| | | /// 随机生成字符 |
| | | /// </summary> |
| | | /// <returns></returns> |
| | | 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(); |
| | | RandomKey = Utlis.CreateRandomString(2); |
| | | } |
| | | |
| | | return RandomKey; |
| | | |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | /// </summary> |
| | | static string PushSignStr = DateTime.Now.Ticks.ToString(); |
| | | |
| | | /// <summary>
|
| | | /// 断开远程Mqtt的链接
|
| | | /// <summary> |
| | | /// 断开远程Mqtt的链接 |
| | | /// </summary> |
| | | static async Task DisConnectRemoteMqttClient(string s = "")
|
| | | {
|
| | | 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);
|
| | | 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) |
| | | { |
| | | Utlis.WriteLine($"Remote断开通讯连接出异常:{e.Message}");
|
| | | }
|
| | | Utlis.WriteLine($"Remote断开通讯连接出异常:{e.Message}"); |
| | | } |
| | | } |
| | | |
| | | /// <summary>
|
| | | /// 断开远程Mqtt的链接
|
| | | /// <summary> |
| | | /// 断开远程Mqtt的链接 |
| | | /// </summary> |
| | | static async Task DisConnectRemoteMqttClientWhenStart(string s = "")
|
| | | static async Task DisConnectRemoteMqttClientWhenStart(string s = "") |
| | | { |
| | | try |
| | | { |
| | |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | Utlis.WriteLine($"RemoteStart断开通讯连接出异常:{e.Message}");
|
| | | Utlis.WriteLine($"RemoteStart断开通讯连接出异常:{e.Message}"); |
| | | } |
| | | } |
| | | |
| | |
| | | /// <summary> |
| | | /// 外网的MQTT是否正在连接 |
| | | /// </summary> |
| | | public static bool remoteMqttIsConnecting; |
| | | public static bool RemoteMqttIsConnecting; |
| | | static bool remoteIsConnected; |
| | | |
| | | static MqttClient() |
| | |
| | | static void InitMqtt() |
| | | { |
| | | new System.Threading.Thread(async () => { |
| | | while (hadGateway) |
| | | while (true) |
| | | { |
| | | try |
| | | { |
| | | System.Threading.Thread.Sleep(500); |
| | | if (!Control.Ins.IsRemote) continue; |
| | | //进入后台不处理 |
| | | if (MainPage.IsEnterBackground) continue; |
| | | |
| | | await StartCloudMqtt(); |
| | | await SubscribeTopics(); |
| | |
| | | } |
| | | |
| | | static bool isSubscribeSuccess; |
| | | /// <summary> |
| | | /// 订阅主题 |
| | | /// </summary> |
| | | /// <returns></returns> |
| | | static async Task SubscribeTopics() |
| | | { |
| | | if (remoteIsConnected && !isSubscribeSuccess) |
| | |
| | | QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce, |
| | | //QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce |
| | | }; |
| | | |
| | | var topicAlinkStatus = new MqttTopicFilter() |
| | | { |
| | | Topic = $"/user/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/app/thing/property/send", |
| | | Topic = $"/user/{DB_ResidenceData.Instance.CurrentRegion.RegionID}/app/thing/property/send", |
| | | QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce |
| | | }; |
| | | var topicAlinkGatewayStatus = new MqttTopicFilter() |
| | | { |
| | | Topic = $"/user/{DB_ResidenceData.Instance.HomeGateway.gatewayId}/app/thing/property/send", |
| | | QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce |
| | | }; |
| | | //var topicBusStatus = new MqttTopicFilter() |
| | | //{ |
| | | // Topic = $"/BusGateWayToClient/{}/Common", |
| | | // QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce |
| | | //}; |
| | | |
| | | Utlis.WriteLine("开始订阅!"); |
| | | var result = await RemoteMqttClient.SubscribeAsync(new MqttTopicFilter[] { topicFilterBusGateWayToClient, topicFilterPush2 , topicAlinkStatus }); |
| | | var result = await RemoteMqttClient.SubscribeAsync(new MqttTopicFilter[] { topicFilterBusGateWayToClient, topicFilterPush2, topicAlinkStatus, topicAlinkGatewayStatus }); |
| | | if (result.Items[0].ResultCode == MQTTnet.Client.Subscribing.MqttClientSubscribeResultCode.GrantedQoS0) |
| | | { |
| | | Control.Ins.GatewayOnline = true; |
| | | isSubscribeSuccess = true; |
| | | Utlis.WriteLine("订阅成功!"); |
| | | |
| | |
| | | } |
| | | |
| | | //追加:没有远程连接的权限 |
| | | if (remoteMqttIsConnecting || remoteIsConnected) |
| | | if (RemoteMqttIsConnecting || remoteIsConnected) |
| | | { |
| | | return; |
| | | } |
| | |
| | | try |
| | | { |
| | | #region 初始化远程Mqtt |
| | | remoteMqttIsConnecting = true; |
| | | RemoteMqttIsConnecting = true; |
| | | RemoteMqttClient = new MqttFactory().CreateMqttClient(); |
| | | |
| | | |
| | |
| | | var ss = Encoding.UTF8.GetString(e.ApplicationMessage.Payload); |
| | | ReceiveCheckGateway(ss); |
| | | } |
| | | //涂鸦设备状态 |
| | | else if (topic == $"/user/{DB_ResidenceData.Instance.CurrentRegion.RegionID}/app/thing/property/send") |
| | | { |
| | | var bytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, tuyaEncryptKey); |
| | | MainPage.Log($"tuya mqtt A-Link data"); |
| | | //A协议数据处理 |
| | | var revString = Encoding.UTF8.GetString(bytes); |
| | | Control.Ins.UpdataFunctionStatus(revString, null); |
| | | } |
| | | else |
| | | { |
| | | SetGatewayOnlineResetCheck(); |
| | | var bytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, DB_ResidenceData.residenceData.HomeGateway.aesKey); |
| | | |
| | | var bytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, mqttEncryptKey); |
| | | //bus数据解析 |
| | | if (DB_ResidenceData.residenceData.GatewayType == 0) |
| | | if (DB_ResidenceData.Instance.GatewayType == 0) |
| | | { |
| | | var packet = new Packet(); |
| | | |
| | |
| | | } |
| | | else |
| | | { |
| | | MainPage.Log($"mqtt A-Link data"); |
| | | //A协议数据处理 |
| | | var revString = Encoding.UTF8.GetString(bytes); |
| | | Control.Ins.UpdataFunctionStatus(revString); |
| | | Control.Ins.UpdataFunctionStatus(revString, null); |
| | | } |
| | | } |
| | | } |
| | |
| | | //(3)ConnectedHandler |
| | | if (RemoteMqttClient.ConnectedHandler == null) |
| | | { |
| | | RemoteMqttClient.UseConnectedHandler(async (e) => { |
| | | Control.Ins.GatewayOnline = true; |
| | | RemoteMqttClient.UseConnectedHandler(async (e) => |
| | | { |
| | | IfNeedReadAllDeviceStatus = true; |
| | | Utlis.WriteLine($"============>Mqtt远程连接成功"); |
| | | SendPushSignOut(); |
| | | FunctionList.List.ReadAllFunctionStatus(); |
| | | }); |
| | | } |
| | | #endregion |
| | |
| | | finally |
| | | { |
| | | //最终要释放连接状态 |
| | | remoteMqttIsConnecting = false; |
| | | RemoteMqttIsConnecting = false; |
| | | |
| | | Utlis.WriteLine($"StartCloudMqtt: 结束"); |
| | | } |
| | |
| | | { |
| | | try |
| | | { |
| | | if (MqttInfoConfig.Current.HomeGatewayInfo != null && MqttInfoConfig.Current.HomeGatewayInfo.mac == Entity.DB_ResidenceData.residenceData.residenceGatewayMAC) |
| | | if (MqttInfoConfig.Current.HomeGatewayInfo != null && MqttInfoConfig.Current.HomeGatewayInfo.mac == Entity.DB_ResidenceData.Instance.residenceGatewayMAC) |
| | | { |
| | | await MQTTConnectAsync(); |
| | | } |
| | |
| | | return; |
| | | } |
| | | |
| | | if (MqttInfoConfig.Current.HomeGatewayInfo != null && MqttInfoConfig.Current.mMqttInfo != null) |
| | | //if (MqttInfoConfig.Current.HomeGatewayInfo != null && MqttInfoConfig.Current.mMqttInfo != null) |
| | | //没有网关情况下,也需要连接mqtt,涂鸦第三方设备不需要网关 |
| | | if (MqttInfoConfig.Current.mMqttInfo != null) |
| | | { |
| | | try |
| | | { |
| | |
| | | MqttInfoConfig.Current.Save(); |
| | | |
| | | mqttEncryptKey = MqttInfoConfig.Current.HomeGatewayInfo.aesKey; |
| | | |
| | | if (MqttInfoConfig.Current.HomeGatewayInfo == null) |
| | | { |
| | | MqttInfoConfig.Current.HomeGatewayInfo = new Server.HomeGatewayInfo(); |
| | | } |
| | | //解密密钥规则:已现有的住宅ID为基准,从右边一一获取值,最后如果不够16位,则往右补零 |
| | | string aesKey = string.Empty; |
| | | for (int i = DB_ResidenceData.Instance.CurrentRegion.RegionID.Length - 1; i >= 0; i--) |
| | | { |
| | | aesKey += DB_ResidenceData.Instance.CurrentRegion.RegionID[i].ToString(); |
| | | if (aesKey.Length == 16) { break; } |
| | | } |
| | | aesKey = aesKey.PadRight(16, '0'); |
| | | tuyaEncryptKey = aesKey; |
| | | |
| | | var options1 = new MQTTnet.Client.Options.MqttClientOptionsBuilder() |
| | | .WithClientId(clientId) |
| | |
| | | /// </summary> |
| | | static void ReceiveNotifySqueezeAsync(string mMes) |
| | | { |
| | | |
| | | if (mMes == PushSignStr) return;//是自己的登录推送不处理 |
| | | |
| | | //断开远程连接 |
| | |
| | | |
| | | DisConnectRemoteMqttClient("挤下线"); |
| | | |
| | | UserInfo.Current.LastTime = DateTime.MinValue; |
| | | UserInfo.Current.SaveUserInfo(); |
| | | |
| | | Application.RunOnMainThread(() => { |
| | | MainPage.GoLoginPage(UserInfo.Current); |
| | | Application.RunOnMainThread(() => |
| | | { |
| | | //弹窗提示被挤下线 |
| | | HDLCommon.Current.CheckLogout(); |
| | | }); |
| | | |
| | | |
| | | //UserInfo.Current.LastTime = DateTime.MinValue; |
| | | //UserInfo.Current.SaveUserInfo(); |
| | | |
| | | //Application.RunOnMainThread(() => { |
| | | // MainPage.GoLoginPage(UserInfo.Current); |
| | | // //弹窗提示被挤下线 |
| | | //}); |
| | | |
| | | //2020-08-11 删除推送数据 |
| | | //HDLRequest.Current.PushserivceSignOut (); |
| | |
| | | /// <returns></returns> |
| | | public static async Task MqttRemoteSend(byte[] message, int optionType = 0) |
| | | { |
| | | //return; |
| | | |
| | | try |
| | | { |
| | | string topicName; |
| | |
| | | /// 是否需要读取一次所有设备状态 |
| | | /// </summary> |
| | | static bool IfNeedReadAllDeviceStatus = true; |
| | | //public static bool IsGatewayOnline = true; |
| | | |
| | | /// <summary> |
| | | /// 设置网关在线标志,并重置CheckGateway参数 |
| | |
| | | if (Control.Ins.IsRemote) |
| | | { |
| | | if (!Control.Ins.GatewayOnline) |
| | | Control.Ins.GatewayOnline = true; |
| | | { |
| | | try |
| | | { |
| | | if (DB_ResidenceData.Instance.HomeGateway != null) |
| | | { |
| | | DB_ResidenceData.Instance.HomeGateway.gatewayStatus = true; |
| | | } |
| | | Control.Ins.GatewayOnline = true; |
| | | } |
| | | catch { } |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | MqttInfoConfig.Current.mMqttInfo = mqttInfoRequestResult_Obj; |
| | | await MQTTConnectAsync(); |
| | | //1.判断是否绑定了网关,获取网关远程连接的加密KEY |
| | | if (DB_ResidenceData.residenceData.CheckWhetherGatewayIsBound()) |
| | | if (DB_ResidenceData.Instance.CheckWhetherGatewayIsBound()) |
| | | { |
| | | //2.找出是否存在匹配当前住宅的mac,存在再进行远程。 |
| | | MqttInfoConfig.Current.HomeGatewayInfo = DB_ResidenceData.residenceData.HomeGateway; |
| | | MqttInfoConfig.Current.HomeGatewayInfo = DB_ResidenceData.Instance.HomeGateway; |
| | | //3.开始连接 |
| | | await MQTTConnectAsync(); |
| | | } |