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/ON.Ios/Info.plist                        |    4 
 Crabtree/ON/Properties/AndroidManifest.xml        |    4 
 Crabtree/.vs/SmartHome/xs/UserPrefs.xml           |   39 ++++++-------
 Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs |   91 ++++++++++++++++++++++++-----
 Crabtree/SmartHome/UI/SimpleControl/MainPage.cs   |    2 
 5 files changed, 97 insertions(+), 43 deletions(-)

diff --git a/Crabtree/.vs/SmartHome/xs/UserPrefs.xml b/Crabtree/.vs/SmartHome/xs/UserPrefs.xml
index 240550b..76f470b 100644
--- a/Crabtree/.vs/SmartHome/xs/UserPrefs.xml
+++ b/Crabtree/.vs/SmartHome/xs/UserPrefs.xml
@@ -1,40 +1,36 @@
 锘�<Properties StartupConfiguration="{1D83BF28-BA88-4152-BA41-D7EFE90A5437}|Default">
   <MonoDevelop.Ide.ItemProperties.ON.Droid PreferredExecutionTarget="Android.Android_Accelerated_Oreo" />
-  <MonoDevelop.Ide.Workbench ActiveDocument="ON.Ios/Resources/Language.ini">
+  <MonoDevelop.Ide.Workbench ActiveDocument="SmartHome/UI/SimpleControl/MqttCommon.cs">
     <Files>
-      <File FileName="SmartHome/UI/SimpleControl/Phone/Alexa/SmartSpeakertListPage.cs" Line="153" Column="48" />
-      <File FileName="SmartHome/HDL/Common/HttpUtil/HttpUtil.cs" Line="236" Column="1" />
-      <File FileName="SmartHome/HDL/Common/CommonUtlis.cs" Line="264" Column="27" />
-      <File FileName="ON/Assets/Language.ini" Line="1114" Column="1" />
-      <File FileName="SmartHome/UI/SimpleControl/R.cs" Line="1222" Column="44" />
-      <File FileName="ON.Ios/Resources/Language.ini" Line="2230" Column="1" />
+      <File FileName="SmartHome/HDL/Common/HttpUtil/HttpServerRequest.cs" Line="815" Column="1" />
+      <File FileName="SmartHome/UI/SimpleControl/Phone/Guide/GuideAddGateway.cs" />
+      <File FileName="SmartHome/UI/SimpleControl/Phone/Guide/GuideAddResidence.cs" />
+      <File FileName="ON/Properties/AndroidManifest.xml" />
+      <File FileName="ON.Ios/Info.plist" />
+      <File FileName="SmartHome/UI/SimpleControl/MqttCommon.cs" Line="349" Column="1" />
+      <File FileName="SmartHome/HDL/Common/HDLLinkUtlis.cs" Line="132" Column="1" />
+      <File FileName="SmartHome/UI/SimpleControl/MainPage.cs" Line="1" Column="1" />
     </Files>
     <Pads>
       <Pad Id="ProjectPad">
         <State name="__root__">
           <Node name="SmartHome" expanded="True">
-            <Node name="ON.Droid" expanded="True">
-              <Node name="Assets" expanded="True" />
-            </Node>
-            <Node name="ON.Ios" expanded="True">
-              <Node name="References" expanded="True" />
-              <Node name="Resources" expanded="True">
-                <Node name="Language.ini" selected="True" />
-              </Node>
-            </Node>
             <Node name="Shared" expanded="True">
+              <Node name="DLL" expanded="True" />
               <Node name="HDL" expanded="True">
                 <Node name="Common" expanded="True">
                   <Node name="HttpUtil" expanded="True" />
                 </Node>
+                <Node name="Operation" expanded="True">
+                  <Node name="Device" expanded="True" />
+                </Node>
               </Node>
+              <Node name="IO" expanded="True" />
+              <Node name="Net" expanded="True" />
               <Node name="UI" expanded="True">
                 <Node name="SimpleControl" expanded="True">
-                  <Node name="Phone" expanded="True">
-                    <Node name="Alexa" expanded="True" />
-                    <Node name="Schedule" expanded="True" />
-                    <Node name="User" expanded="True" />
-                  </Node>
+                  <Node name="Phone" expanded="True" />
+                  <Node name="MqttCommon.cs" selected="True" />
                 </Node>
               </Node>
             </Node>
@@ -56,6 +52,7 @@
   <MonoDevelop.Ide.DebuggingService.Breakpoints>
     <BreakpointStore>
       <Breakpoint file="/Users/jlchen/JLChen/ProjectsCode/HDLGit/CrabtreeOnNew/Crabtree/SmartHome/HDL/Common/HttpUtil/HttpServerRequest.cs" relfile="SmartHome/HDL/Common/HttpUtil/HttpServerRequest.cs" line="153" column="1" />
+      <Breakpoint file="/Users/jlchen/JLChen/ProjectsCode/HDLGit/CrabtreeOnNew/Crabtree/SmartHome/HDL/Common/HDLLinkUtlis.cs" relfile="SmartHome/HDL/Common/HDLLinkUtlis.cs" line="661" column="1" />
     </BreakpointStore>
   </MonoDevelop.Ide.DebuggingService.Breakpoints>
   <MultiItemStartupConfigurations />
diff --git a/Crabtree/ON.Ios/Info.plist b/Crabtree/ON.Ios/Info.plist
index bb675f0..de909cb 100755
--- a/Crabtree/ON.Ios/Info.plist
+++ b/Crabtree/ON.Ios/Info.plist
@@ -17,9 +17,9 @@
 	<key>CFBundleIdentifier</key>
 	<string>com.hdl.on</string>
 	<key>CFBundleShortVersionString</key>
-	<string>2.603291</string>
+	<string>2.606221</string>
 	<key>CFBundleVersion</key>
-	<string>2.603291</string>
+	<string>2.606221</string>
 	<key>LSRequiresIPhoneOS</key>
 	<true/>
 	<key>MinimumOSVersion</key>
diff --git a/Crabtree/ON/Properties/AndroidManifest.xml b/Crabtree/ON/Properties/AndroidManifest.xml
index c76bc9c..b9a0df7 100644
--- a/Crabtree/ON/Properties/AndroidManifest.xml
+++ b/Crabtree/ON/Properties/AndroidManifest.xml
@@ -1,6 +1,6 @@
 锘�<?xml version="1.0" encoding="utf-8"?>
-<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.hdl.crabtreenew" android:versionName="2.604151" android:versionCode="202104151">
-	<uses-sdk android:minSdkVersion="23" android:targetSdkVersion="28" />
+<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.hdl.crabtreenew" android:versionName="2.606221" android:versionCode="202106221">
+	<uses-sdk android:minSdkVersion="23" android:targetSdkVersion="29" />
 	<!-- 鍦ㄥ畨鍗揚鐗堟湰涔嬪悗锛屽繀椤昏鎺堜簣FOREGROUND_SERVICE鏉冮檺锛屾墠鑳藉浣跨敤鍓嶅彴鏈嶅姟 -->
 	<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
 	<!--  瀹氫綅鏉冮檺锛岀敤浜嶢PP閫氳繃鑾峰彇瀹氫綅鍧愭爣鏌ヨ褰撳墠鍦板尯澶╂皵-->
diff --git a/Crabtree/SmartHome/UI/SimpleControl/MainPage.cs b/Crabtree/SmartHome/UI/SimpleControl/MainPage.cs
index 8bd2e0c..5db8240 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/MainPage.cs
+++ b/Crabtree/SmartHome/UI/SimpleControl/MainPage.cs
@@ -38,7 +38,7 @@
         //public static Button LogoButton = new Button ();
   
 
-        public static string RequestVersion = "2.604151";
+        public static string RequestVersion = "2.606221";
         public static UserInfo LoginUser;
         /// <summary>
         /// 鏄惁鏄鐞嗗憳鏉冮檺(鍙樻洿浜�,鎴愬憳鐨勬椂鍊�,杩欎釜涔熶负ture銆備负浠�涔堜細澹版槑杩欐牱鍙橀噺,鍥犱负鏈変簺鎺ュ彛蹇呴』浣跨敤鍘熸潵鐨凾oken)
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