wxr
2022-06-16 352e2add907bb5d7332a7e598693084ef0e34a87
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 = "网关固件已升级,正在初始化网关";
                                    btnTipMsg.Text = "Gateway firmware has been upgraded, initializing gateway.";
                                    btnTipMsg.TextColor = SkinStyle.Current.TextColor;
                                });
                                result = 100;
                            } else {
                                Application.RunOnMainThread (() => {
                                    btnTipMsg.Text = "正在升级网关";
                                    btnTipMsg.Text = "Upgrading gateway.";
                                    btnTipMsg.TextColor = SkinStyle.Current.TextColor;
                                });
                                //需要升级一端口
@@ -528,21 +531,21 @@
                                        {
                                            //result = result0;
                                            Application.RunOnMainThread (() => {
                                                btnTipMsg.Text = "正在升级网关 " + 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 = "正在升级网关 " + 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 = "网关升级成功.正在初始化网关";
                                            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 = "初始化网关成功,正在打开网关远程配置。";
                                            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 = "网关远程已开启,正在迁移账号信息。";
                                        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 = "住宅迁移成功,正在配置网关住宅信息。";
                                    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!");
        }
        
    }
}