From 01c46e7bfe9aa8fb20b29f70c83c03a307af548a Mon Sep 17 00:00:00 2001
From: JLChen <551775569@qq.com>
Date: 星期二, 22 十二月 2020 16:35:16 +0800
Subject: [PATCH] 2020-12-22 1.更新。

---
 HDL_ON/DAL/Mqtt/MqttClient.cs |   88 ++++++++++++++++++++++++++++++--------------
 1 files changed, 60 insertions(+), 28 deletions(-)

diff --git a/HDL_ON/DAL/Mqtt/MqttClient.cs b/HDL_ON/DAL/Mqtt/MqttClient.cs
index ea06685..9246240 100644
--- a/HDL_ON/DAL/Mqtt/MqttClient.cs
+++ b/HDL_ON/DAL/Mqtt/MqttClient.cs
@@ -10,6 +10,7 @@
 using System.Security.Cryptography;
 using HDL_ON.DriverLayer;
 using HDL_ON.Entity;
+using HDL_ON.UI;
 
 namespace HDL_ON.DAL.Mqtt
 {
@@ -20,7 +21,7 @@
         /// </summary>
         static string mqttEncryptKey = "";
         //static string checkGatewayTopicBase64 = "";
-
+        static bool hadGateway = true;
         /// <summary>
         /// 鎸や笅绾夸富棰�
         /// </summary>
@@ -118,7 +119,7 @@
         /// <summary>
         /// 澶栫綉鐨凪QTT鏄惁姝e湪杩炴帴
         /// </summary>
-        public static bool remoteMqttIsConnecting;
+        public static bool RemoteMqttIsConnecting;
         static bool remoteIsConnected;
 
         static MqttClient()
@@ -131,12 +132,14 @@
         static void InitMqtt()
         {
             new System.Threading.Thread(async () => {
-                while (true)
+                while (hadGateway)
                 {
                     try
                     {
                         System.Threading.Thread.Sleep(500);
                         if (!Control.Ins.IsRemote) continue;
+                        //杩涘叆鍚庡彴涓嶅鐞�
+                        if (MainPage.IsEnterBackground) continue;
 
                         await StartCloudMqtt();
                         await SubscribeTopics();
@@ -178,8 +181,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;
@@ -203,8 +212,7 @@
         /// </summary>
         public static async Task StartCloudMqtt()
         {
-
-            if (OnAppConfig.Instance.internetStatus == 0)
+            if (MainPage.InternetStatus == 0)
             {
                 return;
             }
@@ -215,7 +223,7 @@
             }
 
             //杩藉姞锛氭病鏈夎繙绋嬭繛鎺ョ殑鏉冮檺
-            if (remoteMqttIsConnecting || remoteIsConnected)
+            if (RemoteMqttIsConnecting || remoteIsConnected)
             {
                 return;
             }
@@ -226,7 +234,7 @@
                 try
                 {
                     #region 鍒濆鍖栬繙绋婱qtt
-
+                    RemoteMqttIsConnecting = true;
                     RemoteMqttClient = new MqttFactory().CreateMqttClient();
 
 
@@ -261,8 +269,12 @@
                                 }
                                 else
                                 {
+                                    MainPage.Log($"mqtt data");
                                     SetGatewayOnlineResetCheck();
-                                    if (Entity.DB_ResidenceData.residenceData.GatewayType == 0)
+                                    var bytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, DB_ResidenceData.Instance.HomeGateway.aesKey);
+                                    
+                                    //bus鏁版嵁瑙f瀽
+                                    if (DB_ResidenceData.Instance.GatewayType == 0)
                                     {
                                         var packet = new Packet();
 
@@ -279,6 +291,8 @@
                                     else
                                     {
                                         //A鍗忚鏁版嵁澶勭悊
+                                        var revString = Encoding.UTF8.GetString(bytes);
+                                        Control.Ins.UpdataFunctionStatus(revString,null);
                                     }
                                 }
                             }
@@ -299,14 +313,27 @@
                     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();
+                            }
                             IfNeedReadAllDeviceStatus = true;
                             Utlis.WriteLine($"============>Mqtt杩滅▼杩炴帴鎴愬姛");
                             SendPushSignOut();
-
-                            //if (Control.Ins.IsRemote) {
-                            //    //Utlis.ShowAppLinkStatus (AppLinkStatus.CloudLink);
-                            //    MainPage.AddTip (Language.StringByID (SimpleControl.R.MyInternationalizationString.LinkSuccess));
-                            //}
                         });
                     }
                     #endregion
@@ -333,7 +360,7 @@
                 finally
                 {
                     //鏈�缁堣閲婃斁杩炴帴鐘舵��
-                    remoteMqttIsConnecting = false;
+                    RemoteMqttIsConnecting = false;
 
                     Utlis.WriteLine($"StartCloudMqtt: 缁撴潫");
                 }
@@ -363,7 +390,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();
                 }
@@ -424,8 +451,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;
@@ -517,7 +543,9 @@
         /// </summary>
         static void ReceiveNotifySqueezeAsync(string mMes)
         {
-
+#if DEBUG
+            return;
+#endif
             if (mMes == PushSignStr) return;//鏄嚜宸辩殑鐧诲綍鎺ㄩ�佷笉澶勭悊
 
             //鏂紑杩滅▼杩炴帴
@@ -529,13 +557,15 @@
 
             DisConnectRemoteMqttClient("鎸や笅绾�");
 
-            UserInfo.Current.LastTime = DateTime.MinValue;
-            UserInfo.Current.SaveUserInfo();
+            HDLCommon.Current.CheckLogout();
 
-            Application.RunOnMainThread(() => {
-                MainPage.GoLoginPage(UserInfo.Current);
-                //寮圭獥鎻愮ず琚尋涓嬬嚎
-            });
+            //UserInfo.Current.LastTime = DateTime.MinValue;
+            //UserInfo.Current.SaveUserInfo();
+
+            //Application.RunOnMainThread(() => {
+            //    MainPage.GoLoginPage(UserInfo.Current);
+            //    //寮圭獥鎻愮ず琚尋涓嬬嚎
+            //});
 
             //2020-08-11 鍒犻櫎鎺ㄩ�佹暟鎹�
             //HDLRequest.Current.PushserivceSignOut ();
@@ -629,7 +659,8 @@
         {
             if (Control.Ins.IsRemote)
             {
-                Control.Ins.GatewayOnline = true;
+                if (!Control.Ins.GatewayOnline)
+                    Control.Ins.GatewayOnline = true;
             }
         }
 
@@ -646,16 +677,17 @@
                 MqttInfoConfig.Current.mMqttInfo = mqttInfoRequestResult_Obj;
                 await MQTTConnectAsync();
                 //1.鍒ゆ柇鏄惁缁戝畾浜嗙綉鍏筹紝鑾峰彇缃戝叧杩滅▼杩炴帴鐨勫姞瀵咾EY
-                if (DB_ResidenceData.residenceData.CheckWhetherGatewayIsBound())
+                if (DB_ResidenceData.Instance.CheckWhetherGatewayIsBound())
                 {
                     //2.鎵惧嚭鏄惁瀛樺湪鍖归厤褰撳墠浣忓畢鐨刴ac锛屽瓨鍦ㄥ啀杩涜杩滅▼銆�
-                    MqttInfoConfig.Current.HomeGatewayInfo = DB_ResidenceData.residenceData.HomeGateway;
+                    MqttInfoConfig.Current.HomeGatewayInfo = DB_ResidenceData.Instance.HomeGateway;
                     //3.寮�濮嬭繛鎺�
                     await MQTTConnectAsync();
                 }
                 else
                 {
                     Utlis.WriteLine("============>杩樻病缁戝畾缃戝叧");
+                    hadGateway = false;
                 }
             }
         }

--
Gitblit v1.8.0