From a6e88ed16c1c2e28b209450d5c9abc21005ec5ee Mon Sep 17 00:00:00 2001
From: JLChen <551775569@qq.com>
Date: 星期四, 24 六月 2021 16:57:36 +0800
Subject: [PATCH] 2021-06-24 1.增加网关动态密钥处理

---
 Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs |   91 +++++++++++++++++++++++++++++++++++++--------
 1 files changed, 74 insertions(+), 17 deletions(-)

diff --git a/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs b/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs
index 1d20d0c..c8f0475 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs
+++ b/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs
@@ -9,6 +9,7 @@
 using MQTTnet;
 using System.Text;
 using System.Security.Cryptography;
+using System.Collections.Generic;
 
 namespace SmartHome
 {
@@ -35,6 +36,10 @@
         ///// </summary>
         //static readonly string TopicToApp = "/Push/NotifySqueeze";
 
+        /// <summary>
+        /// 鏄惁鑾峰彇杩囧瘑閽�
+        /// </summary>
+        static bool isGetGatewaySecretKey = false;
 
         /// <summary>
         /// 闅忔満Key
@@ -66,7 +71,7 @@
         /// <summary>
         /// 鏂紑杩滅▼Mqtt鐨勯摼鎺�
         /// </summary>
-     public   static async System.Threading.Tasks.Task DisConnectRemoteMqttClient (string s = "")
+        public static async System.Threading.Tasks.Task DisConnectRemoteMqttClient (string s = "")
         {
             try {
                 if (remoteIsConnected) {
@@ -120,6 +125,7 @@
                 bNeedStartTip = true;
                 bNeedConnectTip = true;
                 CommonConfig.Current.IfGetMqttInfoSuccess = false;
+                isGetGatewaySecretKey = false;//鑾峰彇瀵嗛挜鏍囪涓篺alse
             }
             await DisConnectRemoteMqttClient (s);
         }
@@ -246,8 +252,16 @@
                         //QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce
                     };
 
+                    //褰撹澶嘙QTT鎶ユ枃鍔犲瘑瀵嗛挜鍙戠敓鍙樻洿鏃�,浜戠閫氳繃璇ヤ富棰橀�氱煡鍏朵粬瀹㈡埛绔埛鏂板瘑閽�,鍏跺畠鍜岃缃戝叧浜や簰鐨勫鎴风鑷璁㈤槄璇ヤ富棰�,璇ヤ富棰樺寘涓嶅姞瀵�
+                    var topicFilterPush3 = new MqttTopicFilter {
+                        Topic = $"/user/{CommonConfig.Current.HomeGatewayInfo.gatewayId}/custom/mqtt/secret/change",
+                        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, topicFilterPush3 });
                     if (result.Items [0].ResultCode == MQTTnet.Client.Subscribing.MqttClientSubscribeResultCode.GrantedQoS0) {
                         isSubscribeSuccess = true;
                         Utlis.WriteLine ("璁㈤槄鎴愬姛锛�");
@@ -321,6 +335,11 @@
                                 } else if (topic == $"/BusGateWayToClient/{CommonConfig.Current.HomeGatewayInfo.gatewayId}/NotifyBusGateWayInfoChange") {//缃戝叧涓婄嚎锛岄渶瑕佹洿鏂癮eskey
                                     //鏀跺埌缃戝叧涓婄嚎娑堟伅涓婚
                                     ReceiveNotifyBusGateWayInfoChange ();
+                                } else if (topic == $"/user/{CommonConfig.Current.HomeGatewayInfo.gatewayId}/custom/mqtt/secret/change") {//璁惧瀵嗛挜鏇存柊閫氱煡锛岄渶瑕佹洿鏂癮eskey
+                                    //璁惧瀵嗛挜鏇存柊閫氱煡
+                                    HDLUtils.WriteLine ("璁惧瀵嗛挜鏇存柊閫氱煡");
+                                    isGetGatewaySecretKey = false;//鑾峰彇瀵嗛挜鏍囪涓篺alse
+                                    GetGatewaySecretKey ();
                                 } else if (topic == $"/BusGateWayToClient/{CommonConfig.Current.HomeGatewayInfo.gatewayId}/NotifyGateWayOffline") {//缃戝叧鎺夌嚎                                                                                                                    //----绗簩姝ワ細璇诲彇璐﹀彿涓嬮潰鐨勭綉鍏冲垪琛�
                                     ReceiveNotifyGateWayOffline ();
                                 } else if (topic == $"/BusGateWayToClient/{CommonConfig.Current.HomeGatewayInfo.gatewayId}/Common/CheckGateway") {
@@ -372,13 +391,16 @@
                     //}
 
                     //(4)===========寮�濮嬭繛鎺ヨ繃绋�==========
-                    //涔嬪墠宸茬粡鑾峰彇鍙傛暟鎴愬姛杩�
-                    if (CommonConfig.Current.IfGetMqttInfoSuccess) {
-                        //鍒ゆ柇鏄惁闇�瑕侀噸鏂拌幏鍙�
-                        await CheckMQTTConnectAsync ();
-                    } else {
-                        //寮�濮嬭幏鍙栬繙绋嬭繛鎺ュ弬鏁�
-                        await StartMQTTGetInfo ();
+                    //4.1 濡傛灉娌¤幏鍙栬繃瀵嗛挜鍒欓噸鏂拌幏鍙栦竴娆�, 鑾峰彇涓�娆℃渶鏂扮殑瀵嗛挜
+                    if (isGetGatewaySecretKey || GetGatewaySecretKey ()) {
+                        //涔嬪墠宸茬粡鑾峰彇鍙傛暟鎴愬姛杩�
+                        if (CommonConfig.Current.IfGetMqttInfoSuccess) {
+                            //鍒ゆ柇鏄惁闇�瑕侀噸鏂拌幏鍙�
+                            await CheckMQTTConnectAsync ();
+                        } else {
+                            //寮�濮嬭幏鍙栬繙绋嬭繛鎺ュ弬鏁�
+                            await StartMQTTGetInfo ();
+                        }
                     }
 
                 } catch (Exception ex) {
@@ -665,9 +687,6 @@
                     //#endif
                     CheckIfNeedReadAllDeviceStatus ();
                 }
-
-                GetSingleHomeGatewayPaggerAndMQTTConnectAsync (false);
-
             } catch { }
         }
 
@@ -831,7 +850,7 @@
             var mqttInfoRequestResult_Obj = HttpServerRequest.Current.GetMqttRemoteInfo (GetRandomKey ());
             if (mqttInfoRequestResult_Obj != null) {
                 CommonConfig.Current.mMqttInfo = mqttInfoRequestResult_Obj;
-                if (UserConfig.Instance.CheckHomeGatewaysNotEmpty()) {
+                if (UserConfig.Instance.CheckHomeGatewaysNotEmpty ()) {
                     //----绗簩姝ユ壘鍑烘槸鍚﹀瓨鍦ㄥ尮閰嶅綋鍓嶄綇瀹呯殑mac锛屽瓨鍦ㄥ啀杩涜杩滅▼銆�
                     CommonConfig.Current.HomeGatewayInfo = UserConfig.Instance.HomeGateway;
                     if (CommonConfig.Current.HomeGatewayInfo != null) {
@@ -848,13 +867,51 @@
         }
 
 
+
         /// <summary>
-        /// 鍒嗕韩浣忓畢 鑾峰彇褰撳墠浣忓畢缃戝叧淇℃伅骞朵笖杩炴帴MQTT 鎴栬�呭埛鏂�  
-        /// 2020-03-17
+        /// 璁惧瀵嗛挜鏇存柊閫氱煡锛岄噸鏂版嬁瀵嗛挜
         /// </summary>
-        static async Task GetSingleHomeGatewayPaggerAndMQTTConnectAsync (bool bNeedConnect = true)
+        /// <returns></returns>
+        static bool GetGatewaySecretKey ()
         {
-          
+            bool isSuccess = false;
+            try {
+                if (string.IsNullOrEmpty (UserConfig.Instance.CurrentRegion.Id)) return false;
+                var nowhomeId = UserConfig.Instance.CurrentRegion.Id;
+                var requestJson = HttpUtil.GetSignRequestJson (new HomeIdObj () { homeId = nowhomeId });
+                var revertObj = HttpUtil.RequestHttpsPostFroHome (NewAPI.API_POST_GetGatewayList, requestJson);
+                if (revertObj.Code == StateCode.SUCCESS) {
+                    var mHomeGatewayRes = Newtonsoft.Json.JsonConvert.DeserializeObject<List<HomeGatewayInfo>> (revertObj.Data.ToString ());
+                    if (nowhomeId == UserConfig.Instance.CurrentRegion.Id) {
+                        if (mHomeGatewayRes != null) {
+                            if (mHomeGatewayRes.Count > 0) {
+                                //鍙彇绗竴涓綇瀹�
+                                var info = mHomeGatewayRes [0];
+                                UserConfig.Instance.HomeGateway = info;
+                                UserConfig.Instance.SaveUserConfig ();
+                                CommonConfig.Current.HomeGatewayInfo = info;
+                                //鏇存柊缃戝叧淇℃伅鍜屽姞瀵咾ey
+                                mqttEncryptKey = info.aesKey;
+                                CommonConfig.Current.Save ();
+                                isSuccess = true;
+                                isGetGatewaySecretKey = true;//鑾峰彇瀵嗛挜鏍囪涓簍rue
+
+                            } else {
+                                //鎵撳嵃閿欒
+                                HDLUtils.WriteLine ("gateway is not bound");
+                            }
+                        }
+                    }
+                } else {
+                    //鎵撳嵃閿欒
+                    HDLUtils.WriteLine ("Failed to get gateway info");
+
+                }
+                return isSuccess;
+            } catch {
+                //return "";
+                return isSuccess;
+            }
         }
 
 

--
Gitblit v1.8.0