From d53e6af2c5f17838fa79659614b15a2a1f383399 Mon Sep 17 00:00:00 2001
From: wxr <464027401@qq.com>
Date: 星期五, 31 三月 2023 10:04:58 +0800
Subject: [PATCH] 1

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

diff --git a/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs b/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs
index 2f278d8..3d0bab8 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs
+++ b/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs
@@ -157,7 +157,7 @@
                 RemoteSettingWithHDLMQTT ();
             } else {
                 Utlis.WriteLine ("Assign OnePortWirelessFR");
-                RemoteSettingWithOldDevice ();
+                //RemoteSettingWithOldDevice ();
                 //RemoteSettingWithMQTT ();
             }
         }
@@ -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;
@@ -602,134 +609,134 @@
 
 
 
-        /// <summary>
-        /// 杩滅▼閰嶇疆
-        /// </summary>
-        void RemoteSettingWithOldDevice ()
-        {
-            byte [] gatewayBytes = null;
-            byte [] serverIPBytes = null;
+        ///// <summary>
+        ///// 杩滅▼閰嶇疆
+        ///// </summary>
+        //void RemoteSettingWithOldDevice ()
+        //{
+        //    byte [] gatewayBytes = null;
+        //    byte [] serverIPBytes = null;
 
-            string [] strServerIP = (MainPage.SeviceIP).Split ('.');
-            string [] strServerIP1 = (MainPage.SeviceIP).Split ('.');
-            MainPage.Loading.Start ("Configuring device...");
-            System.Threading.Tasks.Task.Run (() => {
-                try {
+        //    string [] strServerIP = (MainPage.SeviceIP).Split ('.');
+        //    string [] strServerIP1 = (MainPage.SeviceIP).Split ('.');
+        //    MainPage.Loading.Start ("Configuring device...");
+        //    System.Threading.Tasks.Task.Run (() => {
+        //        try {
 
-                    //var currentRegionIdBytes = new byte [] {
-                    //    (byte)(UserConfig.Instance.CurrentRegion.RegionID / 256 / 256 / 256),
-                    //    (byte)(UserConfig.Instance.CurrentRegion.RegionID / 256 / 256),
-                    //    (byte)(UserConfig.Instance.CurrentRegion.RegionID / 256),
-                    //    (byte)(UserConfig.Instance.CurrentRegion.RegionID % 256),
-                    //};
+        //            //var currentRegionIdBytes = new byte [] {
+        //            //    (byte)(UserConfig.Instance.CurrentRegion.RegionID / 256 / 256 / 256),
+        //            //    (byte)(UserConfig.Instance.CurrentRegion.RegionID / 256 / 256),
+        //            //    (byte)(UserConfig.Instance.CurrentRegion.RegionID / 256),
+        //            //    (byte)(UserConfig.Instance.CurrentRegion.RegionID % 256),
+        //            //};
 
 
 
 
-                    //var currentRegionIdBytes = Utlis.IntToByteArray (UserConfig.Instance.CurrentRegion.RegionID);
+        //            //var currentRegionIdBytes = Utlis.IntToByteArray (UserConfig.Instance.CurrentRegion.RegionID);
 
-                    byte [] RegionIDBytes = CommonPage.MyEncodingGB2312.GetBytes (UserConfig.Instance.CurrentRegion.Id.ToString ());
-                    var currentRegionIdBytes = new byte [4];
-                    Array.Copy (RegionIDBytes, 0, currentRegionIdBytes, 0, 4 < RegionIDBytes.Length ? 4 : RegionIDBytes.Length);
+        //            byte [] RegionIDBytes = CommonPage.MyEncodingGB2312.GetBytes (UserConfig.Instance.CurrentRegion.Id.ToString ());
+        //            var currentRegionIdBytes = new byte [4];
+        //            Array.Copy (RegionIDBytes, 0, currentRegionIdBytes, 0, 4 < RegionIDBytes.Length ? 4 : RegionIDBytes.Length);
 
-                    gatewayBytes = Control.ControlBytesSendHasReturn (Command.ReadGateWayModelInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, new byte [] { });
-                    serverIPBytes = Control.ControlBytesSendHasReturn (Command.ReadGatewayServerIP, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, new byte [] { });
+        //            gatewayBytes = Control.ControlBytesSendHasReturn (Command.ReadGateWayModelInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, new byte [] { });
+        //            serverIPBytes = Control.ControlBytesSendHasReturn (Command.ReadGatewayServerIP, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, new byte [] { });
 
-                    gatewayDeicve.Remote_GroupName = MainPage.LoginUser.AccountString;//UserConfig.Instance.CurrentRegion.RegionName;
-                    gatewayDeicve.Remote_ProjectName = gatewayDeicve.MAC.Replace (".", "");//UserConfig.Instance.CurrentRegion.RegionName;
-                    gatewayDeicve.Remote_UserName = "Admin";
-                    gatewayDeicve.Remote_Password = "c" + MainPage.LoginUser.ID;
-                    byte [] ggn = new byte [20];
-                    byte [] b1 = CommonPage.MyEncodingGB2312.GetBytes (gatewayDeicve.Remote_GroupName);
-                    gatewayDeicve.Remote_GroupName = CommonPage.MyEncodingGB2312.GetString (b1);
-                    Array.Copy (b1, 0, ggn, 0, 20 < b1.Length ? 20 : b1.Length);
+        //            gatewayDeicve.Remote_GroupName = MainPage.LoginUser.AccountString;//UserConfig.Instance.CurrentRegion.RegionName;
+        //            gatewayDeicve.Remote_ProjectName = gatewayDeicve.MAC.Replace (".", "");//UserConfig.Instance.CurrentRegion.RegionName;
+        //            gatewayDeicve.Remote_UserName = "Admin";
+        //            gatewayDeicve.Remote_Password = "c" + MainPage.LoginUser.ID;
+        //            byte [] ggn = new byte [20];
+        //            byte [] b1 = CommonPage.MyEncodingGB2312.GetBytes (gatewayDeicve.Remote_GroupName);
+        //            gatewayDeicve.Remote_GroupName = CommonPage.MyEncodingGB2312.GetString (b1);
+        //            Array.Copy (b1, 0, ggn, 0, 20 < b1.Length ? 20 : b1.Length);
 
-                    byte [] gpn = new byte [20];
-                    byte [] b2 = CommonPage.MyEncodingGB2312.GetBytes (gatewayDeicve.Remote_ProjectName);
-                    Array.Copy (b2, 0, gpn, 0, 20 < b2.Length ? 20 : b2.Length);
+        //            byte [] gpn = new byte [20];
+        //            byte [] b2 = CommonPage.MyEncodingGB2312.GetBytes (gatewayDeicve.Remote_ProjectName);
+        //            Array.Copy (b2, 0, gpn, 0, 20 < b2.Length ? 20 : b2.Length);
 
-                    byte [] gun = new byte [8];
-                    byte [] b3 = CommonPage.MyEncodingGB2312.GetBytes (gatewayDeicve.Remote_UserName);
-                    Array.Copy (b3, 0, gun, 0, 8 < b3.Length ? 8 : b3.Length);
+        //            byte [] gun = new byte [8];
+        //            byte [] b3 = CommonPage.MyEncodingGB2312.GetBytes (gatewayDeicve.Remote_UserName);
+        //            Array.Copy (b3, 0, gun, 0, 8 < b3.Length ? 8 : b3.Length);
 
-                    byte [] gpw = new byte [8];
-                    byte [] b4 = CommonPage.MyEncodingGB2312.GetBytes (gatewayDeicve.Remote_Password);
-                    Array.Copy (b4, 0, gpw, 0, 8 < b4.Length ? 8 : b4.Length);
+        //            byte [] gpw = new byte [8];
+        //            byte [] b4 = CommonPage.MyEncodingGB2312.GetBytes (gatewayDeicve.Remote_Password);
+        //            Array.Copy (b4, 0, gpw, 0, 8 < b4.Length ? 8 : b4.Length);
 
 
-                    byte [] macAddress = Control.ControlBytesSendHasReturn (Command.ReadDeviceMac, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, new byte [] { });
+        //            byte [] macAddress = Control.ControlBytesSendHasReturn (Command.ReadDeviceMac, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, new byte [] { });
 
-                    gatewayBytes [0] = 2;//杩滅▼鏍囩ず
-                    Array.Copy (ggn, 0, gatewayBytes, 1, 20 < ggn.Length ? 20 : ggn.Length);
-                    Array.Copy (gpn, 0, gatewayBytes, 21, 20 < gpn.Length ? 20 : gpn.Length);
-                    Array.Copy (gun, 0, gatewayBytes, 41, 8 < gun.Length ? 8 : gun.Length);
-                    Array.Copy (gpw, 0, gatewayBytes, 49, 8 < gpw.Length ? 8 : gpw.Length);
+        //            gatewayBytes [0] = 2;//杩滅▼鏍囩ず
+        //            Array.Copy (ggn, 0, gatewayBytes, 1, 20 < ggn.Length ? 20 : ggn.Length);
+        //            Array.Copy (gpn, 0, gatewayBytes, 21, 20 < gpn.Length ? 20 : gpn.Length);
+        //            Array.Copy (gun, 0, gatewayBytes, 41, 8 < gun.Length ? 8 : gun.Length);
+        //            Array.Copy (gpw, 0, gatewayBytes, 49, 8 < gpw.Length ? 8 : gpw.Length);
 
-                    Control.ControlBytesSend (Command.SetGateWayModelInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, gatewayBytes);
+        //            Control.ControlBytesSend (Command.SetGateWayModelInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, gatewayBytes);
 
-                    serverIPBytes [0] = Convert.ToByte (strServerIP [0]);
-                    serverIPBytes [1] = Convert.ToByte (strServerIP [1]);
-                    serverIPBytes [2] = Convert.ToByte (strServerIP [2]);
-                    serverIPBytes [3] = Convert.ToByte (strServerIP [3]);
-                    int point1 = 9999;
+        //            serverIPBytes [0] = Convert.ToByte (strServerIP [0]);
+        //            serverIPBytes [1] = Convert.ToByte (strServerIP [1]);
+        //            serverIPBytes [2] = Convert.ToByte (strServerIP [2]);
+        //            serverIPBytes [3] = Convert.ToByte (strServerIP [3]);
+        //            int point1 = 9999;
 
-                    serverIPBytes [4] = Convert.ToByte (point1 / 256);
-                    serverIPBytes [5] = Convert.ToByte (point1 % 256);
+        //            serverIPBytes [4] = Convert.ToByte (point1 / 256);
+        //            serverIPBytes [5] = Convert.ToByte (point1 % 256);
 
-                    serverIPBytes [6] = Convert.ToByte (strServerIP1 [0]);
-                    serverIPBytes [7] = Convert.ToByte (strServerIP1 [1]);
-                    serverIPBytes [8] = Convert.ToByte (strServerIP1 [2]);
-                    serverIPBytes [9] = Convert.ToByte (strServerIP1 [3]);
-                    int point2 = 9999;
+        //            serverIPBytes [6] = Convert.ToByte (strServerIP1 [0]);
+        //            serverIPBytes [7] = Convert.ToByte (strServerIP1 [1]);
+        //            serverIPBytes [8] = Convert.ToByte (strServerIP1 [2]);
+        //            serverIPBytes [9] = Convert.ToByte (strServerIP1 [3]);
+        //            int point2 = 9999;
 
-                    serverIPBytes [10] = Convert.ToByte (point2 / 256);
-                    serverIPBytes [11] = Convert.ToByte (point2 % 256);
+        //            serverIPBytes [10] = Convert.ToByte (point2 / 256);
+        //            serverIPBytes [11] = Convert.ToByte (point2 % 256);
 
-                    //杩滅▼ip鍦板潃鐨勮缃�
-                    var mobytes = Control.ControlBytesSendHasReturn (Command.SetGateWayModelInternetInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, serverIPBytes);
-                    if (mobytes == null) {
-                        Application.RunOnMainThread (() => {
-                            new Alert ("", ErrorCode.GatewayNoResponse,
-                                       Language.StringByID (R.MyInternationalizationString.Close)).Show ();
-                        });
-                    } else if (mobytes [0] == 0xF5) {
-                        Application.RunOnMainThread (() => {
-                            new Alert ("", Language.StringByID (R.MyInternationalizationString.OperationFailed),
-                                       Language.StringByID (R.MyInternationalizationString.Close)).Show ();
-                        });
-                    } else {
-                        if (gatewayDeicve.Type == DeviceType.OnePortWirelessFR) {
-                            var bindReginIdStatus = Control.ControlBytesSendHasReturn (Command.Write_APP_Data_STORE_1D5E_CMD, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, currentRegionIdBytes);
-                            if (bindReginIdStatus == null) {
-                                Application.RunOnMainThread (() => {
-                                    new Alert ("", ErrorCode.GatewayNoResponse, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
-                                });
-                                return;
-                            } else if (bindReginIdStatus [0] != 0xF8) {
-                                //new Alert ("", "缃戝叧缁戝畾浣忓畢澶辫触锛�", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
-                                Application.RunOnMainThread (() => {
-                                    new Alert ("", "Gateway failed to bind residence!", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
-                                });
-                                return;
-                            }
-                        }
-                        GotoHomePage ();
-                    }
-                } catch (Exception ex) {
-                    Utlis.WriteLine (ex.Message);
-                    Application.RunOnMainThread (() => {
-                        new Alert ("", Language.StringByID (R.MyInternationalizationString.OperationFailed), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
-                    });
-                    bool canRemove = false;
-                } finally {
-                    Application.RunOnMainThread (() => {
-                        MainPage.Loading.Hide ();
-                        //this.RemoveAll ();
-                    });
-                }
-            });
-            //Room.InitAllRoom ();
-        }
+        //            //杩滅▼ip鍦板潃鐨勮缃�
+        //            var mobytes = Control.ControlBytesSendHasReturn (Command.SetGateWayModelInternetInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, serverIPBytes);
+        //            if (mobytes == null) {
+        //                Application.RunOnMainThread (() => {
+        //                    new Alert ("", ErrorCode.GatewayNoResponse,
+        //                               Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+        //                });
+        //            } else if (mobytes [0] == 0xF5) {
+        //                Application.RunOnMainThread (() => {
+        //                    new Alert ("", Language.StringByID (R.MyInternationalizationString.OperationFailed),
+        //                               Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+        //                });
+        //            } else {
+        //                if (gatewayDeicve.Type == DeviceType.OnePortWirelessFR) {
+        //                    var bindReginIdStatus = Control.ControlBytesSendHasReturn (Command.Write_APP_Data_STORE_1D5E_CMD, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, currentRegionIdBytes);
+        //                    if (bindReginIdStatus == null) {
+        //                        Application.RunOnMainThread (() => {
+        //                            new Alert ("", ErrorCode.GatewayNoResponse, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+        //                        });
+        //                        return;
+        //                    } else if (bindReginIdStatus [0] != 0xF8) {
+        //                        //new Alert ("", "缃戝叧缁戝畾浣忓畢澶辫触锛�", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+        //                        Application.RunOnMainThread (() => {
+        //                            new Alert ("", "Gateway failed to bind residence!", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+        //                        });
+        //                        return;
+        //                    }
+        //                }
+        //                GotoHomePage ();
+        //            }
+        //        } catch (Exception ex) {
+        //            Utlis.WriteLine (ex.Message);
+        //            Application.RunOnMainThread (() => {
+        //                new Alert ("", Language.StringByID (R.MyInternationalizationString.OperationFailed), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+        //            });
+        //            bool canRemove = false;
+        //        } finally {
+        //            Application.RunOnMainThread (() => {
+        //                MainPage.Loading.Hide ();
+        //                //this.RemoveAll ();
+        //            });
+        //        }
+        //    });
+        //    //Room.InitAllRoom ();
+        //}
 
 
 
@@ -757,8 +764,13 @@
                     if (SetGateWayModelInfo()) {
                         //2.淇敼绠$悊淇℃伅
                         if (SetGateWayAdminInfo ()) {
-                            //3.鍐欓厤缃垚鍔熷悗涓嬩竴姝ユ搷浣�
-                            GatewaySettingSucceeded ();
+                            //3.鍐橫qtt杩滅▼鍩熷悕鍦板潃
+                            if (SetGateWayMqttUrlAddress ()) {
+                                //4.鍐欓厤缃垚鍔熷悗涓嬩竴姝ユ搷浣�
+                                GatewaySettingSucceeded ();
+                                //杩藉姞鍒ゆ柇缃戝叧涓婄綉绉橀挜鏄惁鍐欏叆鎴愬姛 2022骞�12鏈�28鏃�14:16:14
+                                ApplyServerKey ();
+                            }
                         }
                     }
 
@@ -846,10 +858,9 @@
         /// <returns></returns>
         bool SetGateWayAdminInfo ()
         {
-            byte [] name = CommonPage.MyEncodingGB2312.GetBytes (MainPage.LoginUser.AccountString);
-            //byte [] currentRegionIdBytes = CommonPage.MyEncodingGB2312.GetBytes (UserConfig.Instance.CurrentRegion.RegionID);
+            //byte [] name = CommonPage.MyEncodingGB2312.GetBytes (MainPage.LoginUser.AccountString);
+            byte [] name = CommonPage.MyEncodingGB2312.GetBytes (UserConfig.Instance.HomeGateway.gatewayId);//甯愬彿鍚嶆敼鎴恎atewayId
             byte [] currentRegionIdBytes = CommonPage.MyEncodingGB2312.GetBytes (UserConfig.Instance.CurrentRegion.Id.ToString ());
-            //var currentRegionIdBytes = Utlis.IntToByteArray (UserConfig.Instance.CurrentRegion.RegionID);
             byte [] adminBytes = new byte [73];
             adminBytes [36] = 1;//浣忓畢鏍囧織浣�
             Array.Copy (name, 0, adminBytes, 0, 36 < name.Length ? 36 : name.Length);
@@ -859,6 +870,47 @@
 
             return CheckIsSuccessfulWithBytes (backBytes, "Failed to modify gateway administrator information!");
         }
+
+        /// <summary>
+        /// 
+        /// </summary>
+        /// <param name="bytes"></param>
+        /// <returns></returns>
+        private byte GetCheckSum (byte [] bytes)
+        {
+            //byte checksum = 0x00;
+            //foreach (byte bt in bytes) {
+            //    checksum ^= bt;
+            //}
+
+            int iSum = 0;
+            for (int i = 0; i < bytes.Length; i++) {
+                iSum += bytes [i];
+            }
+            return (byte)(0xff & (0x100 - iSum));
+        }
+
+        /// <summary>
+        /// 淇敼Mqtt鍩熷悕鍦板潃
+        /// </summary>
+        /// <returns></returns>
+        bool SetGateWayMqttUrlAddress ()
+        {
+            byte [] utlBytes = new byte [65];
+            var url = new Uri (UserConfig.Instance.CurrentRegion.regionUrl);
+            var host = "";
+            if (url != null) {
+                host = url.Host;
+            }
+            byte [] hostBytes = CommonPage.MyEncodingGB2312.GetBytes (host);
+            Array.Copy (hostBytes, 0, utlBytes, 0, 64 < hostBytes.Length ? 64 : hostBytes.Length);
+            var sum = GetCheckSum (hostBytes);
+            utlBytes [64] = sum;//鏍¢獙浣�
+            byte [] backBytes = Control.ControlBytesSendHasReturn (Command.SetGateWayMqttURLAddress, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, utlBytes);
+
+            return CheckIsSuccessfulWithBytes (backBytes, "Failed to modify gateway remote address!");
+        }
+
 
         /// <summary>
         /// 鍒ゆ柇鏄惁淇鎴愬姛 F8 = 鎴愬姛锛� F5=澶辫触
@@ -914,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>
         /// 璺宠浆涓婚〉闈�
@@ -1398,4 +1472,10 @@
         //}
 
     }
+
+
+    public class DeviceSecret
+    {
+        public string deviceSecret;
+    }
 }
\ No newline at end of file

--
Gitblit v1.8.0