wei
2021-03-03 d4811b7d34b45ff6b21b97f11da128b5572ec526
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,19 +8,21 @@
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
{
    public static class MqttCommon
    public static class MqttClient
    {
        /// <summary>
        /// 加密通讯KEY
        /// </summary>
        static string mqttEncryptKey = "";
        static string tuyaEncryptKey = "";
        //static string checkGatewayTopicBase64 = "";
        static bool hadGateway = true;
        /// <summary>
        /// 挤下线主题
        /// </summary>
@@ -32,18 +33,18 @@
        /// </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>
@@ -51,65 +52,50 @@
        /// </summary>
        public static IMqttClient RemoteMqttClient = new MqttFactory().CreateMqttClient();
        /// <summary>
        /// 推送标识
        /// </summary>
        static string PushSignStr = System.DateTime.Now.Ticks.ToString();
        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)
                    {
                        //不是无网络
                        if (OnAppConfig.Instance.internetStatus != 0)
                        {
                            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
            {
                //if (remoteIsConnected) {
                remoteIsConnected = false;
                isSubscribeSuccess = false;
                Utlis.WriteLine($"RemoteStart主动断开_{s}");
                await RemoteMqttClient.DisconnectAsync();
                //}
            }
            catch (Exception e)
            {
                Utlis.WriteLine($"RemoteStart断开通讯连接出异常:{e.Message}");
                Utlis.WriteLine($"RemoteStart断开通讯连接出异常:{e.Message}");
            }
        }
        /// <summary>
        /// 断开mqtt连接
@@ -121,21 +107,18 @@
        {
            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;
        public static bool RemoteMqttIsConnecting;
        static bool remoteIsConnected;
        static MqttCommon()
        static MqttClient()
        {
            InitMqtt();
        }
@@ -150,13 +133,12 @@
                    try
                    {
                        System.Threading.Thread.Sleep(500);
                        //if (!UserInfo.Current.IsLogin) {
                        //    continue;
                        //}
                        if (!Control.Ins.IsRemote) continue;
                        //if (BusSocket.IsEnterBackground) continue;
                        //进入后台不处理
                        if (MainPage.IsEnterBackground) continue;
                        if (MqttInfoConfig.Current.HomeGatewayInfo == null)
                        {
                            continue;
                        }
                        await StartCloudMqtt();
                        await SubscribeTopics();
@@ -177,45 +159,46 @@
        }
        static bool isSubscribeSuccess;
        /// <summary>
        /// 订阅主题
        /// </summary>
        /// <returns></returns>
        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()
                    if (DB_ResidenceData.Instance.GatewayType == 0 && !DB_ResidenceData.Instance.CheckWhetherGatewayIdIsNull())
                    {
                        Topic = $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.id}/#",
                        QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce
                        //QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce
                    };
                        Utlis.WriteLine("开始订阅一端口通用主题!");
                        //2020-05-14 订阅主题质量改为0
                        var topicFilterBusGateWayToClient = new MqttTopicFilter()
                        {
                            Topic = $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/#",
                            QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce
                        };
                        await RemoteMqttClient.SubscribeAsync(topicFilterBusGateWayToClient);
                    }
                    //挤下线主题
                    var topicFilterPush2 = new MqttTopicFilter
                    {
                        Topic = $"/BusGateWayToClient/{UserInfo.Current.ID}/#",
                        QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce,
                        //QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce
                    };
                    var topicAlinkStatus = new MqttTopicFilter()
                    {
                        Topic = $"/user/{DB_ResidenceData.Instance.CurrentRegion.RegionID}/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[] {  topicFilterPush2, topicAlinkStatus });
                    if (result.Items[0].ResultCode == MQTTnet.Client.Subscribing.MqttClientSubscribeResultCode.GrantedQoS0)
                    {
                        isSubscribeSuccess = true;
                        Utlis.WriteLine("订阅成功!");
                        MqttRemoteSend(new byte[] { 0 }, 3);
                        //连接成功后检测是否需要通过远程获取Key
                        CheckIfNeedGetLocalPasswordFromRemote();
                    }
                }
                catch (Exception ex)
                {
@@ -224,16 +207,12 @@
            }
        }
        static DateTime mFlagDateTime;
        //static readonly object SendLocker = new object ();
        /// <summary>
        /// 启动远程Mqtt
        /// </summary>
        public static async Task StartCloudMqtt()
        {
            if (OnAppConfig.Instance.internetStatus == 0)
            if (MainPage.InternetStatus == 0)
            {
                return;
            }
@@ -244,7 +223,7 @@
            }
            //追加:没有远程连接的权限
            if (remoteMqttIsConnecting || remoteIsConnected)
            if (RemoteMqttIsConnecting || remoteIsConnected)
            {
                return;
            }
@@ -255,42 +234,63 @@
                try
                {
                    #region 初始化远程Mqtt
                    RemoteMqttIsConnecting = true;
                    RemoteMqttClient = new MqttFactory().CreateMqttClient();
                    //(1)当[连接云端的Mqtt成功后]或者[以及后面App通过云端Mqtt转发数据给网关成功后],处理接收到云端数据包响应时在mqttServerClient_ApplicationMessageReceived这个方法处理
                    if (RemoteMqttClient.ApplicationMessageReceivedHandler == null)
                    {
                        //处理接收到的数据
                        RemoteMqttClient.UseApplicationMessageReceivedHandler((e) => {
                            try
                            {
                                var topic = e.ApplicationMessage.Topic;
                                MainPage.Log($"收到mqtt主题:{topic}");
                                //一端口主题处理
                                if (DB_ResidenceData.Instance.GatewayType == 0 && !DB_ResidenceData.Instance.CheckWhetherGatewayIdIsNull())
                                {
                                    if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/NotifyBusGateWayInfoChange")
                                    {
                                        //网关上线,需要更新aeskey
                                        //收到网关上线消息主题
                                        ReceiveNotifyBusGateWayInfoChange();
                                        return;
                                    }
                                    else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/NotifyGateWayOffline")
                                    {
                                        //网关掉线
                                        //----第二步:读取账号下面的网关列表
                                        ReceiveNotifyGateWayOffline();
                                        return;
                                    }
                                    else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/Common/CheckGateway")
                                    {
                                        var ss = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
                                        //ReceiveCheckGateway(ss);
                                        return;
                                    }
                                }
                                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")
                                //A网关设备状态-包含涂鸦设备
                                else if (topic == $"/user/{DB_ResidenceData.Instance.CurrentRegion.RegionID}/app/thing/property/send")
                                {
                                    var ss = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
                                    ReceiveCheckGateway(ss);
                                    var bytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, tuyaEncryptKey);
                                    var revString = Encoding.UTF8.GetString(bytes);
                                    Control.Ins.UpdataFunctionStatus(revString, null,true);
                                }
                                //一端口数据解析
                                else
                                {
                                    SetGatewayOnlineResetCheck();
                                    //SetGatewayOnlineResetCheck();
                                    var bytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, mqttEncryptKey);
                                    //bus数据解析
                                    var packet = new Packet();
                                    if (!string.IsNullOrEmpty(mqttEncryptKey))
@@ -301,7 +301,6 @@
                                    {
                                        packet.Bytes = e.ApplicationMessage.Payload;
                                    }
                                    packet.IsLocal = false;
                                    packet.Manager();
                                }
                            }
@@ -316,25 +315,20 @@
                            Utlis.WriteLine($"远程连接断开");
                            isSubscribeSuccess = false;
                            await DisConnectRemoteMqttClient("UseDisconnectedHandler");
                        });
                    }
                    //(3)ConnectedHandler
                    if (RemoteMqttClient.ConnectedHandler == null)
                    {
                        RemoteMqttClient.UseConnectedHandler(async (e) => {
                        RemoteMqttClient.UseConnectedHandler(async (e) =>
                        {
                            IfNeedReadAllDeviceStatus = true;
                            Control.Ins.GatewayOnline_Cloud = true;
                            Utlis.WriteLine($"============>Mqtt远程连接成功");
                            SendPushSignOut();
                            //if (Control.Ins.IsRemote) {
                            //    //Utlis.ShowAppLinkStatus (AppLinkStatus.CloudLink);
                            //    MainPage.AddTip (Language.StringByID (SimpleControl.R.MyInternationalizationString.LinkSuccess));
                            //}
                        });
                    }
                    #endregion
                    //}
                    //(4)===========开始连接过程==========
                    //之前已经获取参数成功过
@@ -357,7 +351,7 @@
                finally
                {
                    //最终要释放连接状态
                    remoteMqttIsConnecting = false;
                    RemoteMqttIsConnecting = false;
                    Utlis.WriteLine($"StartCloudMqtt: 结束");
                }
@@ -387,7 +381,7 @@
        {
            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();
                }
@@ -410,11 +404,6 @@
        /// <returns></returns>
        static async Task StartMQTTGetInfo()
        {
            if (!Control.Ins.IsRemote)
            {
                return;
            }
            await GetMqttInfoAndMQTTConnectAsync();
            ////--判断是当前是否分享的住宅
@@ -433,20 +422,10 @@
        /// </summary>
        static async Task MQTTConnectAsync()
        {
            if (!Control.Ins.IsRemote)
            //if (MqttInfoConfig.Current.HomeGatewayInfo != null && MqttInfoConfig.Current.mMqttInfo != null)
            //没有网关情况下,也需要连接mqtt,涂鸦第三方设备不需要网关
            if (MqttInfoConfig.Current.mMqttInfo != null)
            {
                return;
            }
            if (MqttInfoConfig.Current.HomeGatewayInfo != null && MqttInfoConfig.Current.mMqttInfo != null)
            {
                if (bNeedConnectTip)
                {
                    bNeedConnectTip = false;
                    MainPage.LoadingTipShow(Language.StringByID(SimpleControl.R.MyInternationalizationString.GetSuccessfullyStartConnecting));
                }
                try
                {
@@ -454,8 +433,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;
@@ -464,7 +442,18 @@
                    MqttInfoConfig.Current.IfGetMqttInfoSuccess = true;
                    MqttInfoConfig.Current.Save();
                    mqttEncryptKey = MqttInfoConfig.Current.HomeGatewayInfo.aesKey;
                    //解密密钥规则:已现有的住宅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)
@@ -485,7 +474,6 @@
                    {
                        remoteIsConnected = true;
                        IsDisConnectingWithSendCatch = false;
                        UnsupportedProtocolVersionCount = 0;
                    }
                    else
                    {
@@ -497,22 +485,9 @@
                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);
                    }
                    //重新中心服务器获取参数标记
                    MqttInfoConfig.Current.IfGetMqttInfoSuccess = false;
                    Console.WriteLine("Connect error: " + ex.Message);
                }
                finally
                {
@@ -534,12 +509,11 @@
        {
            try
            {
                SetGatewayOnlineResetCheck();
                if (Control.Ins.IsRemote)
                //SetGatewayOnlineResetCheck();
                if (Control.Ins.GatewayOnline_Cloud)
                {
                    CheckIfNeedReadAllDeviceStatus();
                }
            }
            catch { }
        }
@@ -549,23 +523,33 @@
        /// </summary>
        static void ReceiveNotifyGateWayOffline()
        {
            if (Control.Ins.IsRemote)
            {
                Control.Ins.GatewayOnline = false;
            }
            Control.Ins.GatewayOnline_Cloud = false;
        }
        /// <summary>
        /// 收到挤下线推送
        /// </summary>
        static void ReceiveNotifySqueezeAsync(string mMes)
        {
            if (mMes == PushSignStr ) return;//是自己的登录推送不处理//或者当前不是远程链接状态
            //测试账号,不挤下线
            switch (UserInfo.Current.AccountString)
            {
                case "13415629083":
                case "18316120654":
                case "15622703419":
                case "18824864143":
                case "464027401@qq.com":
                case "2791308028@qq.com":
                case "13697499568":
                case "18666455392":
                case "13375012446":
                case "13602944661":
                case "18778381374":
                case "18316672920":
                    return;
            }
            if (mMes == PushSignStr) return;//是自己的登录推送不处理
            //断开远程连接
            Control.Ins.IsRemote = false;
            if (!UserInfo.Current.IsLogin)
            {
                return;
@@ -573,32 +557,39 @@
            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 ();
        }
        /// <summary>
        /// 收到CheckGateway主题
        /// </summary>
        static void ReceiveCheckGateway(string mMes)
        {
            if (!Control.Ins.IsRemote) return;
        ///// <summary>
        ///// 收到CheckGateway主题
        ///// </summary>
        //static void ReceiveCheckGateway(string mMes)
        //{
        //    if (!Control.Ins.IsRemote) return;
            Utlis.WriteLine("ReceiveCheckGateway!");
        //    Utlis.WriteLine("ReceiveCheckGateway!");
            //CheckIfNeedReadAllDeviceStatus ();
        //    //CheckIfNeedReadAllDeviceStatus ();
            //var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponsePack>(mMes);
            Control.Ins.GatewayOnline = true;
        }
        //    //var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponsePack>(mMes);
        //    Control.Ins.GatewayOnline = true;
        //}
        /// <summary>
        /// 推送挤下线主题
@@ -617,25 +608,18 @@
        /// <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";
                        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";
                        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;
@@ -664,18 +648,28 @@
        /// 是否需要读取一次所有设备状态
        /// </summary>
        static bool IfNeedReadAllDeviceStatus = true;
        //public static bool IsGatewayOnline = true;
        /// <summary>
        /// 设置网关在线标志,并重置CheckGateway参数
        /// </summary>
        static void SetGatewayOnlineResetCheck()
        {
            if (Control.Ins.IsRemote)
            {
                Control.Ins.GatewayOnline = true;
            }
        }
        ///// <summary>
        ///// 设置网关在线标志,并重置CheckGateway参数
        ///// </summary>
        //static void SetGatewayOnlineResetCheck()
        //{
        //    if (Control.Ins.IsRemote)
        //    {
        //        if (!Control.Ins.GatewayOnline)
        //        {
        //            try
        //            {
        //                if (DB_ResidenceData.Instance.HomeGateway != null)
        //                {
        //                    DB_ResidenceData.Instance.HomeGateway.gatewayStatus = true;
        //                }
        //                Control.Ins.GatewayOnline = true;
        //            }
        //            catch { }
        //        }
        //    }
        //}
        /// <summary>
@@ -684,35 +678,26 @@
        /// <returns></returns>
        static async Task GetMqttInfoAndMQTTConnectAsync()
        {
            var mqttInfoRequestResult_Obj =new Server.HttpServerRequest().GetMqttRemoteInfo(GetRandomKey());
            var mqttInfoRequestResult_Obj = new Server.HttpServerRequest().GetMqttRemoteInfo(GetRandomKey());
            if (mqttInfoRequestResult_Obj != null)
            {
                MqttInfoConfig.Current.mMqttInfo = mqttInfoRequestResult_Obj;
                if (UserConfig.Instance.GatewayList != null && UserConfig.Instance.GatewayList.Count > 0)
                await MQTTConnectAsync();
                //1.判断是否绑定了网关,获取网关远程连接的加密KEY
                //if (DB_ResidenceData.Instance.CheckWhetherGatewayIsBound())
                {
                    //----第二步找出是否存在匹配当前住宅的mac,存在再进行远程。
                    MqttInfoConfig.Current.HomeGatewayInfo = UserConfig.Instance.GatewayList[0];
                    if (MqttInfoConfig.Current.HomeGatewayInfo != null)
                    {
                        //----第三步 开始连接
                        await MQTTConnectAsync();
                    }
                    //2.找出是否存在匹配当前住宅的mac,存在再进行远程。
                    MqttInfoConfig.Current.HomeGatewayInfo = DB_ResidenceData.Instance.HomeGateway;
                    //3.开始连接
                    await MQTTConnectAsync();
                }
                else
                {
                    Utlis.WriteLine("============>还没绑定网关");
                }
                //else
                //{
                //    Utlis.WriteLine("============>还没绑定网关");
                //    hadGateway = false;
                //}
            }
            else
            {
                Utlis.WriteLine("============>MqttInfo null");
            }
        }
    }
}