From 7232642ff48a7fbde2018cde652f3e771fa58025 Mon Sep 17 00:00:00 2001
From: wei <kaede@kaededeMacBook-Air.local>
Date: 星期三, 27 一月 2021 14:32:55 +0800
Subject: [PATCH] 20200127-英文版处理

---
 HDL_ON/DAL/Mqtt/MqttClient.cs |   81 +++++++++++++++++++++++-----------------
 1 files changed, 46 insertions(+), 35 deletions(-)

diff --git a/HDL_ON/DAL/Mqtt/MqttClient.cs b/HDL_ON/DAL/Mqtt/MqttClient.cs
index 9246240..2a95958 100644
--- a/HDL_ON/DAL/Mqtt/MqttClient.cs
+++ b/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,11 +190,17 @@
                         Topic = $"/user/{MqttInfoConfig.Current.HomeGatewayInfo.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 });
                     if (result.Items[0].ResultCode == MQTTnet.Client.Subscribing.MqttClientSubscribeResultCode.GrantedQoS0)
                     {
+                        Control.Ins.GatewayOnline = true;
                         isSubscribeSuccess = true;
                         Utlis.WriteLine("璁㈤槄鎴愬姛锛�");
 
@@ -264,12 +274,12 @@
                                 }
                                 else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/Common/CheckGateway")
                                 {
+                                    
                                     var ss = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
                                     ReceiveCheckGateway(ss);
                                 }
                                 else
                                 {
-                                    MainPage.Log($"mqtt data");
                                     SetGatewayOnlineResetCheck();
                                     var bytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, DB_ResidenceData.Instance.HomeGateway.aesKey);
                                     
@@ -286,10 +296,19 @@
                                         {
                                             packet.Bytes = e.ApplicationMessage.Payload;
                                         }
+#if DEBUG
+                                        //string ddd = "";
+                                        //foreach (var bb in packet.Bytes)
+                                        //{
+                                        //    ddd += bb + ",";
+                                        //}
+                                        //MainPage.Log($"mqtt bus data:{ddd}");
+#endif
                                         packet.Manager();
                                     }
                                     else
                                     {
+                                        MainPage.Log($"mqtt A-Link data");
                                         //A鍗忚鏁版嵁澶勭悊
                                         var revString = Encoding.UTF8.GetString(bytes);
                                         Control.Ins.UpdataFunctionStatus(revString,null);
@@ -312,25 +331,8 @@
                     //(3)ConnectedHandler
                     if (RemoteMqttClient.ConnectedHandler == null)
                     {
-                        RemoteMqttClient.UseConnectedHandler(async (e) => {
-                            if (DB_ResidenceData.Instance.HomeGateway.gatewayStatus)
-                            {
-                                Control.Ins.GatewayOnline = true;
-                            }
-                            else
-                            {
-                                //new System.Threading.Thread(() => {
-                                //    while(true)
-                                //    {
-                                //        System.Threading.Thread.Sleep(1000);
-                                //        if (DB_ResidenceData.Instance.HomeGateway.GatewayOnline)
-                                //        {
-                                //            Control.Ins.GatewayOnline = true;
-                                //            break;
-                                //        }
-                                //    }
-                                //}) { IsBackground = true }.Start();
-                            }
+                        RemoteMqttClient.UseConnectedHandler(async (e) =>
+                        {
                             IfNeedReadAllDeviceStatus = true;
                             Utlis.WriteLine($"============>Mqtt杩滅▼杩炴帴鎴愬姛");
                             SendPushSignOut();
@@ -543,9 +545,6 @@
         /// </summary>
         static void ReceiveNotifySqueezeAsync(string mMes)
         {
-#if DEBUG
-            return;
-#endif
             if (mMes == PushSignStr) return;//鏄嚜宸辩殑鐧诲綍鎺ㄩ�佷笉澶勭悊
 
             //鏂紑杩滅▼杩炴帴
@@ -557,7 +556,12 @@
 
             DisConnectRemoteMqttClient("鎸や笅绾�");
 
-            HDLCommon.Current.CheckLogout();
+            Application.RunOnMainThread(() =>
+            {
+                //寮圭獥鎻愮ず琚尋涓嬬嚎
+                HDLCommon.Current.CheckLogout();
+            });
+           
 
             //UserInfo.Current.LastTime = DateTime.MinValue;
             //UserInfo.Current.SaveUserInfo();
@@ -603,8 +607,6 @@
         /// <returns></returns>
         public static async Task MqttRemoteSend(byte[] message, int optionType = 0)
         {
-            //return;
-
             try
             {
                 string topicName;
@@ -650,7 +652,6 @@
         /// 鏄惁闇�瑕佽鍙栦竴娆℃墍鏈夎澶囩姸鎬�
         /// </summary>
         static bool IfNeedReadAllDeviceStatus = true;
-        //public static bool IsGatewayOnline = true;
 
         /// <summary>
         /// 璁剧疆缃戝叧鍦ㄧ嚎鏍囧織锛屽苟閲嶇疆CheckGateway鍙傛暟
@@ -660,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 { }
+                }
             }
         }
 

--
Gitblit v1.8.0