wxr
2023-08-04 20f70e3446df19bf5d0faaae9f7bd58fd0fc4bcc
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);//旧平台token
            dic.Add ("oldPlatformHomeId", UserConfig.Instance.CurrentRegion.RegionID);//旧平台住宅id
            dic.Add ("homeId", newUserId);//新平台住宅id
            dic.Add ("homeId", newHomeId);//新平台住宅id
            dic.Add ("userId", newUserId);//新平台用户id
            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) {