From 352e2add907bb5d7332a7e598693084ef0e34a87 Mon Sep 17 00:00:00 2001
From: wxr <464027401@qq.com>
Date: 星期四, 16 六月 2022 21:12:19 +0800
Subject: [PATCH] 备份

---
 Crabtree/SmartHome/UI/SimpleControl/Phone/Register/MigrationServer.cs |  132 ++++++++++++++++++++++++++++++-------------
 1 files changed, 91 insertions(+), 41 deletions(-)

diff --git a/Crabtree/SmartHome/UI/SimpleControl/Phone/Register/MigrationServer.cs b/Crabtree/SmartHome/UI/SimpleControl/Phone/Register/MigrationServer.cs
index 16dd21d..ae319a3 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/Phone/Register/MigrationServer.cs
+++ b/Crabtree/SmartHome/UI/SimpleControl/Phone/Register/MigrationServer.cs
@@ -13,8 +13,8 @@
         /// </summary>
         private bool finish = false;
 
-        private string SeverAddr = "https://bahrain-gateway.hdlcontrol.com";
-        //private string SeverAddr = "https://test-gz.hdlcontrol.com";
+        //private string SeverAddr = "https://bahrain-gateway.hdlcontrol.com";
+        private string SeverAddr = "https://test-gz.hdlcontrol.com";
 
         FrameLayout contentView;
 
@@ -28,6 +28,8 @@
         string newUserId;
 
         Button btnSave;
+        Button btnClose;
+
 
 
         public MigrationServer ()
@@ -75,7 +77,7 @@
                 Y = Application.GetRealHeight (20),
                 Width = Application.GetRealWidth (400),
                 Height = Application.GetRealHeight (80),
-                Text = "璇疯緭鍏ュ瘑鐮侊紝纭杩佺Щ",
+                Text = "Please enter the password to confirm the migration",
                 TextAlignment = TextAlignment.CenterLeft,
                 TextColor = SkinStyle.Current.TextColor,
                 IsMoreLines = true,
@@ -103,7 +105,7 @@
                 Gravity = Gravity.CenterHorizontal,
                 Y = etPwd.Bottom,
                 Width = Application.GetRealWidth (400),
-                Height = Application.GetRealHeight (80),
+                Height = Application.GetRealHeight (120),
                 TextAlignment = TextAlignment.CenterLeft,
                 TextColor = SkinStyle.Current.DelColor,
                 IsMoreLines = true,
@@ -120,7 +122,7 @@
             };
             dialogBodyView.AddChidren (bottomView);
 
-            Button btnClose = new Button () {
+            btnClose = new Button () {
                 Width = Application.GetRealWidth (249),
                 TextID = R.MyInternationalizationString.Close,
                 TextAlignment = TextAlignment.Center
@@ -161,12 +163,11 @@
                 //var newHomeId = Home2New ();
                 //if (newHomeId == "") { } else { }
                 //Account2New (etPwd.Text.Trim());
-
                 //return;
 #endif
 
                 if (etPwd.Text.Trim() == "") {
-                    btnTipMsg.Text = "璇疯緭鍏ュ瘑鐮�";
+                    btnTipMsg.Text = "Please input a password";
                     return;
                 }
 
@@ -179,13 +180,13 @@
                         var verResult = AcountVer (pwd);
                         if (verResult) {
                             Application.RunOnMainThread (() => {
-                                btnTipMsg.Text = "楠岃瘉閫氳繃锛屾鍦ㄦ娴嬬綉鍏抽厤缃�";
+                                btnTipMsg.Text = "Verification passed, detecting gateway configuration";
                                 btnTipMsg.TextColor = SkinStyle.Current.TextColor;
                                 CheckGateway (); 
                             });
                         } else {
                             Application.RunOnMainThread (() => {
-                                btnTipMsg.Text = "瀵嗙爜閿欒锛岄獙璇佸け璐ワ紝璇烽噸璇曘��";
+                                btnTipMsg.Text = "Password error, verification failed, please try again.";
                                 loading.Hide ();
                             });
                         }
@@ -206,7 +207,7 @@
 
             if (MainPage.WiFiStatus != "CrabtreeAdd/WiFi.png") {
                 //status = btnSave.Text = "ReCheck";
-                btnTipMsg.Text = "璇峰湪灞�鍩熺綉鍐呰繛鎺ョ綉鍏宠澶�";
+                btnTipMsg.Text = "Please connect the gateway device in the LAN.";
                 etPwd.Visible = false;
                 btnSave.MouseUpEventHandler = (sender2, e2) => {
                     dialog.Close ();
@@ -298,18 +299,20 @@
         /// <param name="mac"></param>
         /// <param name="newHomeId">鏂板钩鍙扮殑浣忓畢id</param>
         /// <returns></returns>
-        private bool Gateway2New (string mac,string newHomeId)
+        private bool Gateway2New (string mac,string newHomeId,int subnetId)
         {
             Dictionary<string, object> dic = new Dictionary<string, object> ();
             dic.Add ("mac", mac);
             dic.Add ("homeId", newHomeId);
             dic.Add ("userId", newUserId);
+            dic.Add ("subnetId",subnetId);
             dic.Add ("gatewayType", 0);
 
             dic.Add ("tenantId", "202106");
             var requestJson = HttpUtil.GetSignRequestJson (dic);
             
             var revertObj = MainPage.RequestHttps ("/home-wisdom/data/move/gateway/save", requestJson, false, false, SeverAddr);
+
             if (revertObj!= null && revertObj.code == "0") {
                 return true;
             } 
@@ -478,14 +481,14 @@
                             var bytes = Control.ControlBytesSendHasReturn (Command.readGatewayVision, common.SubnetID, common.DeviceID, new byte [] { });
                             if(bytes == null) {
                                 Application.RunOnMainThread (() => {
-                                    btnTipMsg.Text = "鏃犳硶鎼滅储鍒扮綉鍏筹紝璇风‘瀹氭槸鍚︿笌缃戝叧鍦ㄥ悓涓�缃戠粶銆�";
+                                    btnTipMsg.Text = "The gateway cannot be searched. Please make sure it is on the same network as the gateway.";
                                     btnTipMsg.TextColor = SkinStyle.Current.DelColor;
                                     loading.Hide ();
                                 });
                                 return;
                             } else {
                                 Application.RunOnMainThread (() => {
-                                    btnTipMsg.Text = "缃戝叧杩炴帴鎴愬姛锛屾鍦ㄦ鏌ョ綉鍏冲浐浠躲��";
+                                    btnTipMsg.Text = "Gateway connection succeeded. Checking gateway firmware.";
                                     btnTipMsg.TextColor = SkinStyle.Current.TextColor;
                                 });
                             }
@@ -497,13 +500,13 @@
 
                             if (visionString.Contains( "Ind_V03.01U_2021/07/12")) {//FW_MCIP-L1_RF.01_V03.01_210712_STM32F107VCT6_U2_india_beta") {
                                 Application.RunOnMainThread (() => {
-                                    btnTipMsg.Text = "缃戝叧鍥轰欢宸插崌绾э紝姝e湪鍒濆鍖栫綉鍏�";
+                                    btnTipMsg.Text = "Gateway firmware has been upgraded, initializing gateway.";
                                     btnTipMsg.TextColor = SkinStyle.Current.TextColor;
                                 });
                                 result = 100;
                             } else {
                                 Application.RunOnMainThread (() => {
-                                    btnTipMsg.Text = "姝e湪鍗囩骇缃戝叧";
+                                    btnTipMsg.Text = "Upgrading gateway.";
                                     btnTipMsg.TextColor = SkinStyle.Current.TextColor;
                                 });
                                 //闇�瑕佸崌绾т竴绔彛
@@ -528,21 +531,21 @@
                                         {
                                             //result = result0;
                                             Application.RunOnMainThread (() => {
-                                                btnTipMsg.Text = "姝e湪鍗囩骇缃戝叧 " + result + "/" + upgradeData.Count;
+                                                btnTipMsg.Text = "Upgrading gateway " + result + "/" + upgradeData.Count;
                                             });
                                         }
                                     }
                                     if (MainPage.GatewayStatus.Contains ("upgrading") && result == 0) {
                                         SendUpgradeData (common.SubnetID, common.DeviceID, arrayTemp);
                                         Application.RunOnMainThread (() => {
-                                            btnTipMsg.Text = "姝e湪鍗囩骇缃戝叧 " + result + "/" + upgradeData.Count;
+                                            btnTipMsg.Text = "Upgrading gateway " + result + "/" + upgradeData.Count;
                                         });
                                     } else if (result == -99) {
                                         System.Threading.Thread.Sleep (100);
                                         continue;
                                     } else if (result == 100) {
                                         Application.RunOnMainThread (() => {
-                                            btnTipMsg.Text = "缃戝叧鍗囩骇鎴愬姛.姝e湪鍒濆鍖栫綉鍏�";
+                                            btnTipMsg.Text = "Gateway upgrade succeeded. Initializing gateway.";
                                             btnTipMsg.TextColor = SkinStyle.Current.TextColor;
                                         });
                                         break;
@@ -610,7 +613,7 @@
                                         System.Threading.Thread.Sleep (100);
                                     } else {
                                         Application.RunOnMainThread (() => {
-                                            btnTipMsg.Text = "鍒濆鍖栫綉鍏虫垚鍔�,姝e湪鎵撳紑缃戝叧杩滅▼閰嶇疆銆�";
+                                            btnTipMsg.Text = "Successfully initialized the gateway. Opening the gateway remote configuration.";
                                             btnTipMsg.TextColor = SkinStyle.Current.TextColor;
                                         });
                                         break;
@@ -620,12 +623,12 @@
                                 var setRemoteResult = SetGatewayRemote (common.SubnetID, common.DeviceID);
                                 if (setRemoteResult) {
                                     Application.RunOnMainThread (() => {
-                                        btnTipMsg.Text = "缃戝叧杩滅▼宸插紑鍚�,姝e湪杩佺Щ璐﹀彿淇℃伅銆�";
+                                        btnTipMsg.Text = "The gateway remote has been enabled, and the account information is being migrated.";
                                         btnTipMsg.TextColor = SkinStyle.Current.TextColor;
                                     });
                                 } else {
                                     Application.RunOnMainThread (() => {
-                                        btnTipMsg.Text = "缃戝叧杩滅▼寮�鍚け璐ャ��";
+                                        btnTipMsg.Text = "Gateway remote opening failed. Please try again.";
                                         btnTipMsg.TextColor = SkinStyle.Current.DelColor;
                                     });
                                     return;
@@ -635,13 +638,14 @@
                                 var moveAccontResult = Account2New (pwd);
                                 if (moveAccontResult) {
                                     Application.RunOnMainThread (() => {
-                                        btnTipMsg.Text = "璐﹀彿淇℃伅杩佺Щ鎴愬姛锛屾鍦ㄨ縼绉讳綇瀹呫��";
+                                        btnTipMsg.Text = "The account information is migrated successfully. The account is being migrated.";
                                         btnTipMsg.TextColor = SkinStyle.Current.TextColor;
                                     });
                                 } else {
                                     Application.RunOnMainThread (() => {
-                                        btnTipMsg.Text = "璐﹀彿杩佺Щ澶辫触銆�";
+                                        btnTipMsg.Text = "Account migration failed. Please try again.";
                                         btnTipMsg.TextColor = SkinStyle.Current.DelColor;
+                                        loading.Hide ();
                                     });
                                     return;
                                 }
@@ -649,13 +653,13 @@
                                 var newHomeId = Home2New ();
                                 if (newHomeId == "") {
                                     Application.RunOnMainThread (() => {
-                                        btnTipMsg.Text = "浣忓畢杩佺Щ澶辫触銆�";
+                                        btnTipMsg.Text = "Home migration failed. Please try again.";
                                         btnTipMsg.TextColor = SkinStyle.Current.DelColor;
                                         loading.Hide ();
                                     });
                                     return;
                                 } else {
-                                    btnTipMsg.Text = "浣忓畢杩佺Щ鎴愬姛,姝e湪閰嶇疆缃戝叧浣忓畢淇℃伅銆�";
+                                    btnTipMsg.Text = "Residence migration succeeded. Gateway residence information is being configured.";
                                     btnTipMsg.TextColor = SkinStyle.Current.TextColor;
                                 }
 
@@ -670,12 +674,12 @@
                                 var writeHomeIdResult = WriteHomeId (common.SubnetID, common.DeviceID, homeIdSendBytes);
                                 if (writeHomeIdResult) {
                                     Application.RunOnMainThread (() => {
-                                        btnTipMsg.Text = "缃戝叧浣忓畢淇℃伅閰嶇疆鎴愬姛锛屾鍦ㄨ幏鍙栦笂缃戠閽ャ��";
+                                        btnTipMsg.Text = "The gateway home information is configured successfully, and the Internet access key is being obtained.";
                                         btnTipMsg.TextColor = SkinStyle.Current.TextColor;
                                     });
                                 } else {
                                     Application.RunOnMainThread (() => {
-                                        btnTipMsg.Text = "缃戝叧浣忓畢淇℃伅閰嶇疆澶辫触.";
+                                        btnTipMsg.Text = "Gateway home information configuration failed. Please try again.";
                                         btnTipMsg.TextColor = SkinStyle.Current.DelColor;
                                     });
                                 }
@@ -684,7 +688,7 @@
 
                                 if (!string.IsNullOrEmpty (netKet)) {
                                     Application.RunOnMainThread (() => {
-                                        btnTipMsg.Text = "鑾峰彇涓婄綉绉橀挜鎴愬姛銆傛鍦ㄥ啓鍏�";
+                                        btnTipMsg.Text = "The Internet access key was obtained successfully. It is being written.";
                                         btnTipMsg.TextColor = SkinStyle.Current.TextColor;
                                     });
                                     var secretkeySendBytes = new byte [netKet.Length + 1];
@@ -698,31 +702,35 @@
                                     var writeSecretKeyResult = WriteSecretKey (common.SubnetID, common.DeviceID, secretkeySendBytes);
                                     if (writeSecretKeyResult) {
                                         Application.RunOnMainThread (() => {
-                                            btnTipMsg.Text = "涓婄綉绉橀挜鍐欏叆鎴愬姛锛屾鍦ㄨ縼绉荤綉鍏炽��";
+                                            btnTipMsg.Text = "The Internet access key was written successfully. The gateway is being migrated.";
                                         });
                                     } else {
                                         Application.RunOnMainThread (() => {
-                                            btnTipMsg.Text = "涓婄綉绉橀挜鍐欏叆澶辫触銆�";
+                                            btnTipMsg.Text = "Failed to write Internet secret key. Please try again.";
                                             btnTipMsg.TextColor = SkinStyle.Current.DelColor;
                                             return;
                                         });
                                     }
                                 } else {
                                     Application.RunOnMainThread (() => {
-                                        btnTipMsg.Text = "涓婄綉绉橀挜鑾峰彇澶辫触銆�";
+                                        btnTipMsg.Text = "Failed to obtain the Internet secret key. Please try again.";
                                         btnTipMsg.TextColor = SkinStyle.Current.DelColor;
                                     });
                                 }
+                                //鍐欏叆mqtt鍩熷悕淇℃伅
+                                SetGateWayMqttUrlAddress (common.SubnetID, common.DeviceID);
+
+
                                 //杩佺Щ缃戝叧
-                                var moveGatewayResult = Gateway2New (common.MAC.Replace (".", ""), newHomeId);
+                                var moveGatewayResult = Gateway2New (common.MAC.Replace (".", ""), newHomeId, common.SubnetID);
                                 if (moveGatewayResult) {
                                     Application.RunOnMainThread (() => {
-                                        btnTipMsg.Text = "缃戝叧杩佺Щ鎴愬姛锛屾鍦ㄨ縼绉诲浠芥暟鎹��";
+                                        btnTipMsg.Text = "Gateway migration succeeded. The backup data is being migrated.";
                                         btnTipMsg.TextColor = SkinStyle.Current.TextColor;
                                     });
                                 } else {
                                     Application.RunOnMainThread (() => {
-                                        btnTipMsg.Text = "缃戝叧杩佺Щ澶辫触銆�";
+                                        btnTipMsg.Text = "Gateway migration failed. Please try again.";
                                         btnTipMsg.TextColor = SkinStyle.Current.DelColor;
                                         loading.Hide ();
                                     });
@@ -737,15 +745,18 @@
                                     //var markResult = Mark (newHomeId);
                                     //if (markResult) {
                                     Application.RunOnMainThread (() => {
-                                        btnTipTitle.Text = "澶囦唤鏁版嵁杩佺Щ鎴愬姛锛屽钩鍙拌縼绉诲畬鎴愩��";
+                                        btnTipTitle.Text = "The backup data migration is successful, and the platform migration is completed.";
+                                        btnTipTitle.Height = Application.GetRealHeight (150);
                                         //btnTipMsg.TextColor = SkinStyle.Current.TextColor;
                                         btnTipMsg.Text = "";
                                         etPwd.Visible = false;
                                         loading.Hide ();
+                                        btnSave.Visible = false;
+                                        btnClose.Width = Application.GetRealWidth (500);
                                     });
                                 } else {
                                     Application.RunOnMainThread (() => {
-                                        btnTipMsg.Text = "澶囦唤鏁版嵁杩佺Щ澶辫触銆�";
+                                        btnTipMsg.Text = "Backup data migration failed. Please try again.";
                                         btnTipMsg.TextColor = SkinStyle.Current.DelColor;
                                         loading.Hide ();
                                     });
@@ -755,16 +766,15 @@
                             }
 
 
-                            Application.RunOnMainThread (() => {
-                            });
-
                             break;//鍙崌绾т竴涓�
                         }
                     }
                 } else {
                     //娌℃湁缃戝叧
                     Application.RunOnMainThread (() => {
-                        btnTipTitle.Text = "鏈娴嬪埌缃戝叧锛岃妫�鏌ユ湰鍦版暟鎹�.";
+                        btnTipMsg.Text = "No gateway detected, please check local data";
+                        btnTipMsg.TextColor = SkinStyle.Current.DelColor;
+                        loading.Hide ();
                     });
                     return;
                 }
@@ -905,6 +915,46 @@
             return false;
         }
 
-        
+        /// <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>
+        void SetGateWayMqttUrlAddress (byte subnetId,byte deviceId)
+        {
+            byte [] utlBytes = new byte [65];
+            var url = new Uri (SeverAddr);
+            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, subnetId, deviceId, utlBytes);
+
+            //return CheckIsSuccessfulWithBytes (backBytes, "Failed to modify gateway remote address!");
+        }
+
     }
 }

--
Gitblit v1.8.0