From ed8b62c72831d92067369cc9abb35f1470b8e4d3 Mon Sep 17 00:00:00 2001
From: wxr <464027401@qq.com>
Date: 星期二, 03 一月 2023 17:30:14 +0800
Subject: [PATCH] 增加上网秘钥写入,本地通讯秘钥写入

---
 Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs |   49 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 49 insertions(+), 0 deletions(-)

diff --git a/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs b/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs
index 352e964..1fd7ee7 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs
+++ b/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs
@@ -768,6 +768,8 @@
                             if (SetGateWayMqttUrlAddress ()) {
                                 //4.鍐欓厤缃垚鍔熷悗涓嬩竴姝ユ搷浣�
                                 GatewaySettingSucceeded ();
+                                //杩藉姞鍒ゆ柇缃戝叧涓婄綉绉橀挜鏄惁鍐欏叆鎴愬姛 2022骞�12鏈�28鏃�14:16:14
+                                ApplyServerKey ();
                             }
                         }
                     }
@@ -968,6 +970,47 @@
             UploadOidAndSidList ();
 
         }
+
+        /// <summary>
+        /// 璇诲彇缃戝叧涓婄綉绉橀挜锛屽鏋滅閽ユ湁闂鍒欓噸鏂板啓鍏�
+        /// </summary>
+        void ApplyServerKey ()
+        {
+            if (gatewayDeicve.Type == DeviceType.OnePortMqttFR) {
+                var result = Control.ControlBytesSendHasReturn (Command.ApplyServerKey, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, new byte [] { 0});
+                if (result != null && result.Length > 1) {
+                    if (result [1] == 0xF5) {
+                        var secretKeyPack = HttpServerRequest.Current.ApplyDeviceSecret (gatewayDeicve.MAC);
+                        if (secretKeyPack != null) {
+                            if (secretKeyPack.Code == StateCode.SUCCESS) {
+                                var pack = Newtonsoft.Json.JsonConvert.DeserializeObject<DeviceSecret> (secretKeyPack.Data.ToString ());
+                                if (pack != null) {
+                                    byte [] usefullBytes = new byte [17];
+                                    usefullBytes [0] = 1;
+                                    byte [] ddd = CommonPage.MyEncodingGB2312.GetBytes (pack.deviceSecret);
+                                    Array.Copy (ddd, 0, usefullBytes, 1, 16 < ddd.Length ? 16 : ddd.Length);
+                                    var result2 = Control.ControlBytesSendHasReturn (Command.ApplyServerKey, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, usefullBytes);
+                                    var result3 = Control.ControlBytesSendHasReturn (Command.RestartTheGateway, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, new byte [] { });
+                                    Application.RunOnMainThread (() => {
+                                        new Alert ("", "The Internet access key was successfully written, and the gateway is being restarted!", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+                                    });
+                                }
+                            }
+                        } else {
+                            Application.RunOnMainThread (() => {
+                                new Alert ("", "Failed to generate Internet access key!", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+                            });
+                        }
+                    }
+                } else {
+                    //Application.RunOnMainThread (() => {
+                    //    new Alert ("", "Failed to read the Internet access key!", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+                    //});
+                }
+            }
+
+        }
+
 
         /// <summary>
         /// 涓婁紶oid鍜宻id鍒楄〃
@@ -1421,4 +1464,10 @@
         //}
 
     }
+
+
+    public class DeviceSecret
+    {
+        public string deviceSecret;
+    }
 }
\ No newline at end of file

--
Gitblit v1.8.0