JLChen
2020-12-09 e87985ec1dcb69beedaf9f95e8e7aba14b7c08d6
HDL_ON/DAL/Mqtt/MqttClient.cs
@@ -1,7 +1,6 @@
/*
更新了EMQ连接方式
*/
using System.Collections.Generic;
using System;
using MQTTnet.Client;
using System.Threading.Tasks;
@@ -9,8 +8,9 @@
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
{
@@ -21,7 +21,7 @@
        /// </summary>
        static string mqttEncryptKey = "";
        //static string checkGatewayTopicBase64 = "";
        static bool hadGateway = true;
        /// <summary>
        /// 挤下线主题
        /// </summary>
@@ -132,7 +132,7 @@
        static void InitMqtt()
        {
            new System.Threading.Thread(async () => {
                while (true)
                while (hadGateway)
                {
                    try
                    {
@@ -167,7 +167,7 @@
                    //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
                    };
@@ -179,8 +179,14 @@
                        //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;
@@ -204,7 +210,6 @@
        /// </summary>
        public static async Task StartCloudMqtt()
        {
            if (OnAppConfig.Instance.internetStatus == 0)
            {
                return;
@@ -227,7 +232,7 @@
                try
                {
                    #region 初始化远程Mqtt
                    remoteMqttIsConnecting = true;
                    RemoteMqttClient = new MqttFactory().CreateMqttClient();
@@ -245,17 +250,17 @@
                                    //新挤下线主题方案 收到挤下线主题
                                    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);
@@ -263,7 +268,9 @@
                                else
                                {
                                    SetGatewayOnlineResetCheck();
                                    if (Entity.DB_ResidenceData.residenceData.GatewayType == 0)
                                    Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, DB_ResidenceData.residenceData.HomeGateway.aesKey);
                                    //bus数据解析
                                    if (DB_ResidenceData.residenceData.GatewayType == 0)
                                    {
                                        var packet = new Packet();
@@ -280,6 +287,8 @@
                                    else
                                    {
                                        //A协议数据处理
                                        var revString = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
                                        Control.Ins.UpdataFunctionStatus(revString);
                                    }
                                }
                            }
@@ -303,11 +312,7 @@
                            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
@@ -425,8 +430,7 @@
                    //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;
@@ -582,7 +586,7 @@
                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);
@@ -590,7 +594,7 @@
                        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;
@@ -630,7 +634,8 @@
        {
            if (Control.Ins.IsRemote)
            {
                Control.Ins.GatewayOnline = true;
                if (!Control.Ins.GatewayOnline)
                    Control.Ins.GatewayOnline = true;
            }
        }
@@ -646,6 +651,19 @@
            {
                MqttInfoConfig.Current.mMqttInfo = mqttInfoRequestResult_Obj;
                await MQTTConnectAsync();
                //1.判断是否绑定了网关,获取网关远程连接的加密KEY
                if (DB_ResidenceData.residenceData.CheckWhetherGatewayIsBound())
                {
                    //2.找出是否存在匹配当前住宅的mac,存在再进行远程。
                    MqttInfoConfig.Current.HomeGatewayInfo = DB_ResidenceData.residenceData.HomeGateway;
                    //3.开始连接
                    await MQTTConnectAsync();
                }
                else
                {
                    Utlis.WriteLine("============>还没绑定网关");
                    hadGateway = false;
                }
            }
        }