| | |
| | | /* |
| | | 更新了EMQ连接方式 |
| | | */ |
| | | using System.Collections.Generic; |
| | | using System; |
| | | using MQTTnet.Client; |
| | | using System.Threading.Tasks; |
| | |
| | | using MQTTnet; |
| | | using System.Text; |
| | | using System.Security.Cryptography; |
| | | using System.IO; |
| | | using HDL_ON.DriverLayer; |
| | | using HDL_ON.Entity; |
| | | using HDL_ON.UI; |
| | | |
| | | namespace HDL_ON.DAL.Mqtt |
| | | { |
| | |
| | | /// </summary> |
| | | static string mqttEncryptKey = ""; |
| | | //static string checkGatewayTopicBase64 = ""; |
| | | |
| | | static bool hadGateway = true; |
| | | /// <summary> |
| | | /// 挤下线主题 |
| | | /// </summary> |
| | |
| | | static void InitMqtt() |
| | | { |
| | | new System.Threading.Thread(async () => { |
| | | while (true) |
| | | while (hadGateway) |
| | | { |
| | | try |
| | | { |
| | |
| | | //2020-05-14 订阅主题质量改为0 |
| | | var topicFilterBusGateWayToClient = new MqttTopicFilter() |
| | | { |
| | | Topic = $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.id}/#", |
| | | Topic = $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/#", |
| | | QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce |
| | | //QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce |
| | | }; |
| | |
| | | //QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce |
| | | }; |
| | | |
| | | var topicAlinkStatus = new MqttTopicFilter() |
| | | { |
| | | Topic = $"/user/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/app/thing/property/send", |
| | | QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce |
| | | }; |
| | | |
| | | Utlis.WriteLine("开始订阅!"); |
| | | var result = await RemoteMqttClient.SubscribeAsync(new MqttTopicFilter[] { topicFilterBusGateWayToClient, topicFilterPush2 }); |
| | | var result = await RemoteMqttClient.SubscribeAsync(new MqttTopicFilter[] { topicFilterBusGateWayToClient, topicFilterPush2 , topicAlinkStatus }); |
| | | if (result.Items[0].ResultCode == MQTTnet.Client.Subscribing.MqttClientSubscribeResultCode.GrantedQoS0) |
| | | { |
| | | isSubscribeSuccess = true; |
| | |
| | | /// </summary> |
| | | public static async Task StartCloudMqtt() |
| | | { |
| | | |
| | | if (OnAppConfig.Instance.internetStatus == 0) |
| | | if (MainPage.InternetStatus == 0) |
| | | { |
| | | return; |
| | | } |
| | |
| | | try |
| | | { |
| | | #region 初始化远程Mqtt |
| | | |
| | | remoteMqttIsConnecting = true; |
| | | RemoteMqttClient = new MqttFactory().CreateMqttClient(); |
| | | |
| | | |
| | |
| | | //新挤下线主题方案 收到挤下线主题 |
| | | ReceiveNotifySqueezeAsync(mMes); |
| | | } |
| | | else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.id}/NotifyBusGateWayInfoChange") |
| | | else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/NotifyBusGateWayInfoChange") |
| | | {//网关上线,需要更新aeskey |
| | | //收到网关上线消息主题 |
| | | ReceiveNotifyBusGateWayInfoChange(); |
| | | } |
| | | else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.id}/NotifyGateWayOffline") |
| | | else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/NotifyGateWayOffline") |
| | | {//网关掉线 |
| | | //----第二步:读取账号下面的网关列表 |
| | | ReceiveNotifyGateWayOffline(); |
| | | } |
| | | else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.id}/Common/CheckGateway") |
| | | else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/Common/CheckGateway") |
| | | { |
| | | var ss = Encoding.UTF8.GetString(e.ApplicationMessage.Payload); |
| | | ReceiveCheckGateway(ss); |
| | |
| | | else |
| | | { |
| | | SetGatewayOnlineResetCheck(); |
| | | if (Entity.DB_ResidenceData.residenceData.GatewayType == 0) |
| | | var bytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, DB_ResidenceData.residenceData.HomeGateway.aesKey); |
| | | |
| | | //bus数据解析 |
| | | if (DB_ResidenceData.residenceData.GatewayType == 0) |
| | | { |
| | | var packet = new Packet(); |
| | | |
| | |
| | | else |
| | | { |
| | | //A协议数据处理 |
| | | var revString = Encoding.UTF8.GetString(bytes); |
| | | Control.Ins.UpdataFunctionStatus(revString); |
| | | } |
| | | } |
| | | } |
| | |
| | | if (RemoteMqttClient.ConnectedHandler == null) |
| | | { |
| | | RemoteMqttClient.UseConnectedHandler(async (e) => { |
| | | Control.Ins.GatewayOnline = true; |
| | | IfNeedReadAllDeviceStatus = true; |
| | | Utlis.WriteLine($"============>Mqtt远程连接成功"); |
| | | SendPushSignOut(); |
| | | |
| | | //if (Control.Ins.IsRemote) { |
| | | // //Utlis.ShowAppLinkStatus (AppLinkStatus.CloudLink); |
| | | // MainPage.AddTip (Language.StringByID (SimpleControl.R.MyInternationalizationString.LinkSuccess)); |
| | | //} |
| | | FunctionList.List.ReadAllFunctionStatus(); |
| | | }); |
| | | } |
| | | #endregion |
| | |
| | | |
| | | //url = HttpUtil.GetProxyEMQUrl (url); |
| | | //#if DEBUG |
| | | // url = HttpUtil.GetProxyEMQUrl (url); |
| | | |
| | | //url = HttpUtil.GetProxyEMQUrl (url); |
| | | //#endif |
| | | var clientId = MqttInfoConfig.Current.mMqttInfo.clientId; |
| | | var username = MqttInfoConfig.Current.mMqttInfo.userName; |
| | |
| | | switch (optionType) |
| | | { |
| | | case 0: |
| | | topicName = $"/ClientToBusGateWay/{MqttInfoConfig.Current.HomeGatewayInfo.id}/Common/ON"; |
| | | topicName = $"/ClientToBusGateWay/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/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"; |
| | | 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; |
| | |
| | | { |
| | | if (Control.Ins.IsRemote) |
| | | { |
| | | Control.Ins.GatewayOnline = true; |
| | | if (!Control.Ins.GatewayOnline) |
| | | Control.Ins.GatewayOnline = true; |
| | | } |
| | | } |
| | | |
| | |
| | | else |
| | | { |
| | | Utlis.WriteLine("============>还没绑定网关"); |
| | | hadGateway = false; |
| | | } |
| | | } |
| | | } |