wei
2021-01-27 7232642ff48a7fbde2018cde652f3e771fa58025
HDL_ON/DAL/Mqtt/MqttClient.cs
@@ -32,18 +32,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>
@@ -132,7 +132,7 @@
        static void InitMqtt()
        {
            new System.Threading.Thread(async () => {
                while (hadGateway)
                while (true)
                {
                    try
                    {
@@ -160,6 +160,10 @@
        }
        static bool isSubscribeSuccess;
        /// <summary>
        /// 订阅主题
        /// </summary>
        /// <returns></returns>
        static async Task SubscribeTopics()
        {
            if (remoteIsConnected && !isSubscribeSuccess)
@@ -186,16 +190,17 @@
                        Topic = $"/user/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/app/thing/property/send",
                        QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce
                    };
                    var topicBusStatus = new MqttTopicFilter()
                    {
                        Topic = $"/BusGateWayToClient/MacMask/Common",
                        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, topicBusStatus });
                    var result = await RemoteMqttClient.SubscribeAsync(new MqttTopicFilter[] { topicFilterBusGateWayToClient, topicFilterPush2, topicAlinkStatus });
                    if (result.Items[0].ResultCode == MQTTnet.Client.Subscribing.MqttClientSubscribeResultCode.GrantedQoS0)
                    {
                        Control.Ins.GatewayOnline = true;
                        isSubscribeSuccess = true;
                        Utlis.WriteLine("订阅成功!");
@@ -269,6 +274,7 @@
                                }
                                else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/Common/CheckGateway")
                                {
                                    var ss = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
                                    ReceiveCheckGateway(ss);
                                }
@@ -327,7 +333,6 @@
                    {
                        RemoteMqttClient.UseConnectedHandler(async (e) =>
                        {
                            Control.Ins.GatewayOnline = true;
                            IfNeedReadAllDeviceStatus = true;
                            Utlis.WriteLine($"============>Mqtt远程连接成功");
                            SendPushSignOut();
@@ -540,9 +545,6 @@
        /// </summary>
        static void ReceiveNotifySqueezeAsync(string mMes)
        {
#if DEBUG
            return;
#endif
            if (mMes == PushSignStr) return;//是自己的登录推送不处理
            //断开远程连接
@@ -605,8 +607,6 @@
        /// <returns></returns>
        public static async Task MqttRemoteSend(byte[] message, int optionType = 0)
        {
            //return;
            try
            {
                string topicName;
@@ -661,7 +661,17 @@
            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 { }
                }
            }
        }