From 20f70e3446df19bf5d0faaae9f7bd58fd0fc4bcc Mon Sep 17 00:00:00 2001
From: wxr <464027401@qq.com>
Date: 星期五, 04 八月 2023 12:06:51 +0800
Subject: [PATCH] 轮询备份

---
 Crabtree/SmartHome/UI/SimpleControl/Phone/Register/MigrationServer.cs |  148 +++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 135 insertions(+), 13 deletions(-)

diff --git a/Crabtree/SmartHome/UI/SimpleControl/Phone/Register/MigrationServer.cs b/Crabtree/SmartHome/UI/SimpleControl/Phone/Register/MigrationServer.cs
index a5d14d8..10cf576 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/Phone/Register/MigrationServer.cs
+++ b/Crabtree/SmartHome/UI/SimpleControl/Phone/Register/MigrationServer.cs
@@ -19,7 +19,8 @@
         private bool finish = false;
 
 #if DEBUG
-        private string SeverAddr = "https://test-gz.hdlcontrol.com";
+        //private string SeverAddr = "https://test-gz.hdlcontrol.com";
+        private string SeverAddr = "https://bahrain-gateway.hdlcontrol.com";
 #else
         private string SeverAddr = "https://bahrain-gateway.hdlcontrol.com";
 #endif
@@ -186,12 +187,18 @@
 
 #if DEBUG
 
+ 
 
-
-                //var moveAccontResult = Account2New ("12345678");
-                //var newHomeId = Home2New ();
-                ////杩佺Щ缃戝叧
-                //var moveGatewayResult = Gateway2New ("4D59383553502243", newHomeId, 0);
+                //Account2New ("12345678");
+                //Home2New ();
+                ////杩佺Щ浜戠鍏朵粬澶囦唤
+                //var moveCloudDataResult = MoveCloudBackup ();
+                //Application.RunOnMainThread (() => {
+                //    btnTipMsg.Text = $"Failed to migrate cloud backup list.{moveCloudDataResult}";
+                //    btnTipMsg.TextColor = SkinStyle.Current.DelColor;
+                //    loading.Hide ();
+                //    return;
+                //});
 #endif
 
                 if (etPwd.Text.Trim() == "") {
@@ -317,6 +324,7 @@
             if (revertObj != null) {
                 if (revertObj.data != null) {
                     var newHomeInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<Move_HomeInfo> (revertObj.data.ToString ());
+                    newHomeId = newHomeInfo.homeId;
                     return newHomeInfo.homeId;
                 }
             }
@@ -554,20 +562,23 @@
 
         /// 杩佺Щ鏈嶅姟鍣ㄥ叏閮ㄥ浠�
         /// </summary>
-        private void MoveCloudBackup ()
+        private string MoveCloudBackup ()
         {
             var dic = new Dictionary<string, object> ();
             dic.Add ("oldPlatformToken", MainPage.LoginUser.LoginTokenString);//鏃у钩鍙皌oken
             dic.Add ("oldPlatformHomeId", UserConfig.Instance.CurrentRegion.RegionID);//鏃у钩鍙颁綇瀹卛d
-            dic.Add ("homeId", newUserId);//鏂板钩鍙颁綇瀹卛d
+            dic.Add ("homeId", newHomeId);//鏂板钩鍙颁綇瀹卛d
             dic.Add ("userId", newUserId);//鏂板钩鍙扮敤鎴穒d
             dic.Add ("tenantId", "202106");//绉熸埛id
             var requestJson = HttpUtil.GetSignRequestJson (dic);
             var revertObj = MainPage.RequestHttps ("/home-wisdom/data/move/backup/all", requestJson, false, false, SeverAddr);
             if (revertObj != null) {
                 if (revertObj.code == "0") {
+                    
                 }
+                return revertObj.code;
             }
+            return "-99";
         }
 
         /// <summary>
@@ -678,8 +689,6 @@
                 GatewayBase common = null;
                 string gateWayString = "";
                 if (gateWayList.Count > 0) {
-
-
                     foreach (var gatewayFileName in gateWayList) {
                         var tempStrings = gatewayFileName.Split ('_');
                         if (tempStrings [1].ToString () == DeviceType.OnePortBus.ToString () || tempStrings [1].ToString () == DeviceType.RCU.ToString () ||
@@ -888,6 +897,8 @@
                                 //鑾峰彇涓婄綉绉橀挜
                                 var netKet = GetInternetAccessKey (common.MAC.Replace (".", ""));
 
+                                
+
                                 if (!string.IsNullOrEmpty (netKet)) {
                                     Application.RunOnMainThread (() => {
                                         btnTipMsg.Text = "The Internet access key was obtained successfully. It is being written.";
@@ -918,6 +929,110 @@
                                             return;
                                         });
                                     }
+
+#if DEBUG
+                                    System.Threading.Thread.Sleep (5000);
+#endif
+
+                                    CommonPage.FindGateway = true;
+                                    try {
+                                        #region 鎼滅储璁惧
+                                        CommonPage.LocalPhoneFindDevice = true;
+                                        CommonPage.RandomHigh = (byte)new Random ().Next (255);
+                                        CommonPage.RandomLow = (byte)new Random ().Next (255);
+                                        CommonPage.GateWayList.Clear ();
+                                        CommonPage.FindGateway = true;
+                                        //濡傛灉涓ゆ閮芥病鏈夋暟鎹弽棣堬紝灏变笉璇诲彇
+                                        int readCount = 2;
+                                        while (0 < readCount) {
+                                            readCount--;
+                                            System.IO.MemoryStream ms = new System.IO.MemoryStream ();
+                                            ms.WriteByte (CommonPage.RandomHigh);
+                                            ms.WriteByte (CommonPage.RandomLow);
+
+                                            List<Common> list = CommonPage.GateWayList;
+
+                                            int tempCount = list.Count;
+                                            string s = ";";
+                                            for (int i = 0; i < list.Count; i++) {
+                                                Common common2 = list [i];
+                                                if (s.Contains (";" + common2.SubnetID.ToString () + ":" + common2.DeviceID.ToString ()))
+                                                    continue;
+                                                s += common2.SubnetID.ToString () + ":" + common2.DeviceID.ToString () + ";";
+                                                ms.WriteByte (common2.SubnetID);
+                                                ms.WriteByte (common2.DeviceID);
+                                            }
+                                            Control control = new Control ();
+
+                                            Console.WriteLine ("缁勬挱鎼滅储缃戝叧锛屾悳绱㈠湴鍧�鏄�:" + CommonPage.EndPoint.ToString () + "    " + CommonPage.GateWayList.Count);
+                                            control.Send (new Target () {
+                                                IPEndPoint = CommonPage.EndPoint,
+                                                Command = Command.ReadGateway,
+                                                SubnetID = 0xFF,
+                                                DeviceID = 0xFF,
+                                                AddData = ms.ToArray ()
+                                            }, SendCount.Zero, false);
+
+                                            Console.WriteLine ("骞挎挱鎼滅储缃戝叧锛屾悳绱㈠湴鍧�鏄�:" + new Net.NetWiFi ().BroadcastIpAddress.ToString () + "    " + CommonPage.GateWayList.Count);
+                                            control.Send (new Target () {
+                                                IPEndPoint = new System.Net.IPEndPoint (new Net.NetWiFi ().BroadcastIpAddress, 6000),
+                                                Command = Command.ReadGateway,
+                                                SubnetID = 0xFF,
+                                                DeviceID = 0xFF,
+                                                AddData = ms.ToArray ()
+                                            }, SendCount.Zero, false);
+
+                                            System.Threading.Thread.Sleep (800);
+                                            //濡傛灉鏁伴噺涓嶇浉绛夛紝灏遍噸缃鏁�
+                                            if (tempCount != CommonPage.GateWayList.Count) {
+                                                readCount = 2;
+                                            }
+                                        }
+                                        CommonPage.FindGateway = false;
+
+                                        CommonPage.LocalPhoneFindDevice = false;
+                                        #endregion
+                                    } catch (Exception ex) {
+                                        Console.WriteLine (ex.ToString ());
+                                    } finally {
+                                        if (CommonPage.GateWayList.Count > 0) {
+                                            foreach (var gateway1 in CommonPage.GateWayList) {
+                                                var bingResidenceIdBytes = Control.ControlBytesSendHasReturn (Command.Read_APP_Data_STORE_1D5C_CMD, common.SubnetID, common.DeviceID, new byte [] { });
+                                                if (bingResidenceIdBytes == null) {
+                                                    continue;
+                                                }
+                                                int bingResideceId = 0;
+                                                for (int i = 0; i < bingResidenceIdBytes.Length; i++) {
+                                                    bingResideceId += (int)(bingResidenceIdBytes [i] * Math.Pow (256, 3 - i));
+                                                }
+                                                if (bingResideceId != UserConfig.Instance.CurrentRegion.RegionID && bingResideceId != 0) {
+                                                } else {
+                                                    //鍐欏叆涓婄綉绉橀挜
+                                                    writeSecretKeyResult = WriteSecretKey (gateway1.SubnetID, gateway1.DeviceID, secretkeySendBytes);
+                                                    if (writeSecretKeyResult) {
+                                                        Application.RunOnMainThread (() => {
+                                                            btnTipMsg.Text = "The Internet access key was written successfully.";
+                                                        });
+                                                    } else {
+                                                        Application.RunOnMainThread (() => {
+                                                            btnTipMsg.Text = "Failed to write Internet secret key.";
+                                                            btnTipMsg.TextColor = SkinStyle.Current.DelColor;
+                                                        });
+                                                    }
+#if DEBUG
+                                                    System.Threading.Thread.Sleep (5000);
+#endif
+
+                                                }
+                                            }
+                                        }
+                                    }
+
+
+
+
+
+
                                 } else {
                                     Application.RunOnMainThread (() => {
                                         btnTipMsg.Text = "Failed to obtain the Internet secret key. Please try again.";
@@ -927,7 +1042,6 @@
                                 }
                                 //鍐欏叆mqtt鍩熷悕淇℃伅
                                 SetGateWayMqttUrlAddress (common.SubnetID, common.DeviceID);
-
 
                                 //杩佺Щ缃戝叧
                                 var moveGatewayResult = Gateway2New (common.MAC.Replace (".", ""), newHomeId, common.SubnetID);
@@ -944,6 +1058,16 @@
                                     });
                                     return;
                                 }
+                                //杩佺Щ浜戠鍏朵粬澶囦唤
+                                var moveCloudDataResult = MoveCloudBackup ();
+                                if (moveCloudDataResult != "0") {
+                                    Application.RunOnMainThread (() => {
+                                        btnTipMsg.Text = $"Failed to migrate cloud backup list.{moveCloudDataResult}";
+                                        btnTipMsg.TextColor = SkinStyle.Current.DelColor;
+                                        loading.Hide ();
+                                        return;
+                                    });
+                                }
                                 //鍒涘缓杩佺Щ澶囦唤鏂囦欢澶�
                                 var backId = moveFolder2New (newHomeId);
                                 //杩佺Щ澶囦唤鏂囦欢
@@ -958,8 +1082,6 @@
                                         btnTipMsg.Text = "Migration backup succeeded, migrating Schedule data.";
                                         btnTipMsg.TextColor = SkinStyle.Current.TextColor;
                                     });
-                                    //杩佺Щ浜戠鍏朵粬澶囦唤
-                                    MoveCloudBackup ();
                                     //杩佺Щ瀹氭椂鍣�
                                     var timerList = GetTimer (newHomeId);
                                     if (timerList == null) {

--
Gitblit v1.8.0