From c05a93ddb0714a310a31574aaf030e677d9a07b6 Mon Sep 17 00:00:00 2001
From: wxr <464027401@qq.com>
Date: 星期五, 06 一月 2023 17:43:31 +0800
Subject: [PATCH] 增加多网关绑定支持

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

diff --git a/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs b/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs
index 156c47f..3d0bab8 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs
+++ b/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs
@@ -316,7 +316,7 @@
                     //}
 
                     EventHandler<MouseEventArgs> eHandler = (sender, e) => {
-                        Dialog dialog = new Dialog ();
+                        Dialog dialog = new Dialog ();//淇敼璁惧澶囨敞
                         FrameLayout dialogBodyView = new FrameLayout () {
                             BackgroundColor = SkinStyle.Current.DialogColor,
                             Width = Application.GetRealWidth (494),
@@ -415,9 +415,16 @@
                             //CommonPage.UpdateRemark (common.SubnetID, common.DeviceID, btnChangeName.Text.Trim ());
 
                             byte [] updateBytes = new byte [20];
-                            byte [] remakeBytes = CommonPage.MyEncodingGB2312.GetBytes (btnChangeName.Text.Trim ());
+                            string changeName = btnChangeName.Text.Trim ();
+                            byte [] remakeBytes = CommonPage.MyEncodingGB2312.GetBytes (changeName);
                             Array.Copy (remakeBytes, 0, updateBytes, 0, remakeBytes.Length < 20 ? remakeBytes.Length : 20);
                             Control.ControlBytesSend (Command.Write_DeviceRamarkCMD, common.SubnetID, common.DeviceID, updateBytes);
+
+                            //淇敼浜戠澶囨敞
+                            new Thread (() => {
+                                var http = new HttpServerRequest ();
+                                http.EditDeviceName (common.DeviceID.ToString(), UserConfig.Instance.CurrentRegion.Id,changeName);
+                            }) { IsBackground = true }.Start ();
 
 
                             btnDeviceName.Text = btnChangeName.Text;
@@ -761,6 +768,8 @@
                             if (SetGateWayMqttUrlAddress ()) {
                                 //4.鍐欓厤缃垚鍔熷悗涓嬩竴姝ユ搷浣�
                                 GatewaySettingSucceeded ();
+                                //杩藉姞鍒ゆ柇缃戝叧涓婄綉绉橀挜鏄惁鍐欏叆鎴愬姛 2022骞�12鏈�28鏃�14:16:14
+                                ApplyServerKey ();
                             }
                         }
                     }
@@ -957,58 +966,80 @@
                 }
             }
 
-            GotoHomePage ();
+            //GotoHomePage ();
+            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.Replace(".",""));
+                        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 = System.Text.Encoding.ASCII.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);
+
+                                    //byte [] restartArray = new byte [12];
+                                    //string [] macArray = gatewayDeicve.MAC.Split (".");
+                                    //for(int i = 0; i < macArray.Length; i++) {
+                                    //    restartArray [i + 2] = Convert.ToByte (macArray [i], 16);
+                                    //}
+                                    //restartArray [10] = gatewayDeicve.SubnetID;
+                                    //restartArray [11] = gatewayDeicve.DeviceID;
+                                    //var result3 = Control.ControlBytesSendHasReturn (Command.RestartTheGateway, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, restartArray);
+                                    //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>
-        ///// Gateway setting succeeded锛宒ata up.Please wait...
-        ///// 缁戝畾Mac(鍗板害鐗�)
-        ///// </summary>
-        //void BindGatewaysNew ()
-        //{
-        //    //Application.RunOnMainThread (() => {
-        //    //    MainPage.Loading.Start ("Gateway setting succeeded锛宒ata up. Please wait...");
-        //    //});
-        //    //IO.FileUtils.SaveEquipmentMessage (gatewayDeicve);
-        //    //var gatewayMAC = gatewayDeicve.MAC.Replace (".", "");
+        /// <summary>
+        /// 涓婁紶oid鍜宻id鍒楄〃
+        /// </summary>
+        void UploadOidAndSidList ()
+        {
 
-        //    ////璧版柊鏇存柊缁戝畾鎺ュ彛
-        //    //var mBindMacObj = new BindMacObj () {
-        //    //    LoginAccessToken = MainPage.LoginUser.LoginTokenString,
-        //    //    HomeId = UserConfig.Instance.CurrentRegion.Id,
-        //    //    MAC = gatewayMAC
+            Application.RunOnMainThread (() => {
+                MainPage.Loading.Start ("Uploading device list...");
+            });
+            var RES = HDLLinkUtlis.Current.UploadOidAndSidList ();
+            Application.RunOnMainThread (() => {
+                if (RES) {
+                    //Utlis.ShowTip ("Device list upload successfully锛�");
+                } else {
+                    Utlis.ShowTip ("Device list upload failed锛�");
+                }
+                GotoHomePage ();
+            });
 
-        //    //};
-
-        //    //string urlHead = MainPage.RequestHttpsHost;
-        //    //if (mBindMacObj.IsOtherAccountCtrl) {
-        //    //    urlHead = UserConfig.Instance.MasterAccountRequestBaseUrl;
-        //    //    mBindMacObj.LoginAccessToken = UserConfig.Instance.MasterAccountToken;
-        //    //}
-
-
-        //    //var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (mBindMacObj);
-
-        //    ////var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (new EditMACByHomeId { RegionID = UserConfig.Instance.CurrentRegion.Id, MAC = gatewayMAC, IsReBind = true });
-        //    //var respone = MainPage.RequestHttps (API.BindMac, requestJson, urlHead);
-        //    //if (respone.StateCode.ToUpper () != StateCode.SUCCESS) {
-        //    //    GetNowHomeGatewayAfterBindMacFailed ();//2020-01-11
-        //    //    Application.RunOnMainThread (() => {
-        //    //        new Alert ("", $"Failed to bind Mac address! {ErrorCode.Reason }{respone.StateCode}", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
-        //    //        if (MainPage.LoginUser.AccountString == "464027401@qq.com") {
-        //    //            new Alert (respone.StateCode, respone.ErrorInfo, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
-        //    //        }
-        //    //    });
-        //    //    return;
-        //    //}
-        //    //GetNowHomeGateway ();
-
-        //    var gatewayMAC = gatewayDeicve.MAC.Replace (".", "");
-        //    AddMacToUserConfig (gatewayMAC);
-
-        //}
+        }
 
         /// <summary>
         /// 璺宠浆涓婚〉闈�
@@ -1441,4 +1472,10 @@
         //}
 
     }
+
+
+    public class DeviceSecret
+    {
+        public string deviceSecret;
+    }
 }
\ No newline at end of file

--
Gitblit v1.8.0