wxr
2023-08-04 20f70e3446df19bf5d0faaae9f7bd58fd0fc4bcc
Crabtree/SmartHome/UI/SimpleControl/Phone/Register/MigrationServer.cs
@@ -6,6 +6,11 @@
namespace Shared.SimpleControl.Phone
{
    public class DeviceSecret
    {
        public string deviceSecret;
    }
    public class MigrationServer
    {
        /// <summary>
@@ -13,8 +18,12 @@
        /// </summary>
        private bool finish = false;
        private string SeverAddr = "https://bahrain-gateway.hdlcontrol.com";
#if DEBUG
        //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
        FrameLayout contentView;
@@ -26,13 +35,32 @@
        EditText etPwd;
        string pwd;
        string newUserId;
        string newHomeId;
        Button btnSave;
        Button btnClose;
        Alert sssAlert = new Alert ("", "Please press the PROG button on Gateway for 3 sec.", "Confirm");
        Action tipLockedAction;
        bool showedLockedTip = false;
        public MigrationServer ()
        {
            loading = new Loading ();
            loading = new Loading ();
            tipLockedAction = () => {
                Application.RunOnMainThread (() => {
                    if (!showedLockedTip) {
                        sssAlert.Show ();
                        showedLockedTip = true;
                        sssAlert.ResultEventHandler += (sd, dd) => {
                            showedLockedTip = false;
                        };
                    }
                });
            };
            Packet.lockedAction = tipLockedAction;
        }
@@ -40,7 +68,7 @@
        public void ShowDialog()
        {
            #region 弹窗
#region 弹窗
            Dialog dialog = new Dialog ();
            FrameLayout dialogBodyView = new FrameLayout () {
@@ -75,7 +103,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 +131,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 +148,7 @@
            };
            dialogBodyView.AddChidren (bottomView);
            Button btnClose = new Button () {
            btnClose = new Button () {
                Width = Application.GetRealWidth (249),
                TextID = R.MyInternationalizationString.Close,
                TextAlignment = TextAlignment.Center
@@ -128,6 +156,8 @@
            bottomView.AddChidren (btnClose);
            btnClose.MouseUpEventHandler += (send2er, e2) => {
                dialog.Close ();
                Packet.lockedAction = null;
            };
            Button btnBottomLine = new Button () {
@@ -151,22 +181,28 @@
            btnSave.MouseUpEventHandler += (sender2, e2) => {
                if (finish) {
                    dialog.Close ();
                    Packet.lockedAction = null;
                    return;
                }
#if DEBUG
                //WriteSecretKey (1, 0, new byte [] {1,1,2,3,2,3,4,5,2,3,5,6 });
                //CheckGateway ();
                //var newHomeId = Home2New ();
                //if (newHomeId == "") { } else { }
                //Account2New (etPwd.Text.Trim());
                //return;
                //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() == "") {
                    btnTipMsg.Text = "请输入密码";
                    btnTipMsg.Text = "Please input a password";
                    return;
                }
@@ -179,13 +215,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,10 +242,11 @@
            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 ();
                    Packet.lockedAction = null;
                };
            } else {
@@ -223,13 +260,13 @@
            //3.接收升级文件获取请求
            //迁移账号
            #endregion
#endregion
        }
        #region 云端
#region 云端
        /// <summary>
        /// 账号登录验证
        /// </summary>
@@ -287,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;
                }
            }
@@ -298,18 +336,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;
            } 
@@ -328,7 +368,8 @@
            var requestJson = HttpUtil.GetSignRequestJson (dic);
            //var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (dic);
            var revertObj = MainPage.RequestHttps ("/smart-open/third/device/authByMac", requestJson, false, false, SeverAddr);
            var revertObj = MainPage.RequestHttps ("/home-wisdom/program/device/secret/applyDeviceSecret", requestJson, false, false, SeverAddr);
            //var revertObj = MainPage.RequestHttps ("/smart-open/third/device/authByMac", requestJson, false, false, SeverAddr);
            if (revertObj.code == "0") {
                return revertObj.data.ToString ();
            } else { }
@@ -339,38 +380,37 @@
        /// 迁移备份
        /// </summary>
        /// <param name="newHomeId"></param>
        /// <param name="newUserId"></param>
        /// <returns></returns>
        private string moveFolder2New (string newHomeId,string newUserId)
        private string moveFolder2New (string newHomeId,string folderName = "")
        {
            Dictionary<string, object> dic = new Dictionary<string, object> ();
            dic.Add ("backupDataType", "HDL_ON");
            dic.Add ("homeId",newHomeId);
            dic.Add ("userId",newUserId);
            dic.Add ("backupClassify", "USER_DEFINED_BACKUP");
            dic.Add ("folderName", "迁移备份" + DateTime.Now.ToString ());
            dic.Add ("tenantId", "202106");
            var requestJson = HttpUtil.GetSignRequestJson (dic);
            var revertObj = MainPage.RequestHttps ("/home-wisdom/data/move/folder/save", requestJson, false, false, SeverAddr);
            if(revertObj!= null) {
                if(revertObj.code == "0") {
                    var resultObj = Newtonsoft.Json.JsonConvert.DeserializeObject<FolderObj> (revertObj.data.ToString ());
                    return resultObj.id;
            try {
                Dictionary<string, object> dic = new Dictionary<string, object> ();
                dic.Add ("backupDataType", "HDL_ON");
                dic.Add ("homeId", newHomeId);
                dic.Add ("userId", newUserId);
                dic.Add ("backupClassify", "USER_DEFINED_BACKUP");
                dic.Add ("folderName", folderName == "" ? "MigrateBackup" + DateTime.Now.ToString () : folderName);
                dic.Add ("tenantId", "202106");
                var requestJson = HttpUtil.GetSignRequestJson (dic);
                var revertObj = MainPage.RequestHttps ("/home-wisdom/data/move/folder/save", requestJson, false, false, SeverAddr);
                if (revertObj != null) {
                    if (revertObj.code == "0") {
                        var resultObj = Newtonsoft.Json.JsonConvert.DeserializeObject<FolderObj> (revertObj.data.ToString ());
                        return resultObj.id;
                    }
                }
                return "";
            } catch {
                return "";
            }
            Application.RunOnMainThread (() => {
                btnTipTitle.Text = "备份迁移失败.";
                btnTipTitle.TextColor = SkinStyle.Current.DelColor;
            });
            return "";
        }
        private string MoveFile2New(string newHomeId, string newUserId,string backupId)
        private string MoveFile2New(string backupId)
        {
            Dictionary<string, object> dic = new Dictionary<string, object> ();
            dic.Add ("backupId", backupId);//1534728347497418754//1534728860322385922
            dic.Add ("backupId", backupId);
            dic.Add ("homeId", newHomeId);
            dic.Add ("userId", newUserId);
            dic.Add ("backupClassify", "USER_DEFINED_BACKUP");
@@ -394,8 +434,6 @@
                if(fileObjs.Count > 9) {
                    if (!dic.ContainsKey ("list")) {
                        dic.Add ("list", fileObjs);
                    //} else {
                    //    dic ["list"] = fileObjs;
                    }
                    var json = HttpUtil.GetSignRequestJson (dic);
                    var revertObj_foreach = MainPage.RequestHttps ("/home-wisdom/data/move/file/save", json, false, false, SeverAddr);
@@ -405,7 +443,7 @@
                            continue;
                        }
                    }
                }
                }
            }
            if (!dic.ContainsKey ("list")) {
                dic.Add ("list", fileObjs);
@@ -421,28 +459,212 @@
            }
            return "";
        }
        /// <summary>
        /// 标记2.0平台数据
        /// 迁移旧云端备份列表到新云端
        /// </summary>
        /// <returns></returns>
        private bool Mark(string newHomeId)
        private void MoveOldBackupList ()
        {
            try {
                GetUserFolderObj requestObj = new GetUserFolderObj () { LevelID = UserConfig.Instance.CurrentRegion.RegionID };
                var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj);
                var revertObj = MainPage.RequestHttps ("GetUserFolder", requestJson, true, false);//获取旧服务器备份列表
                if (revertObj.StateCode == "SUCCESS") {
                    var responseDataObj = Newtonsoft.Json.JsonConvert.DeserializeObject<List<FolderRes>> (revertObj.ResponseData.ToString ());
                    int index = 1;
                    foreach (var folder in responseDataObj) {
                        Application.RunOnMainThread (() => {
                            try {
                                btnTipMsg.Text = $"Migrating server backup {index} / {responseDataObj.Count + 1}.";
                                btnTipMsg.TextColor = SkinStyle.Current.TextColor;
                            } catch { }
                        });
                        try {
                            var requestObj2 = new UserBackupListObj () { LevelID = folder.FolderID };
                            var requestJson2 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj2);
                            var revertObj2 = MainPage.RequestHttps ("UserBackupList", requestJson2);//下载旧服务器的备份文件列表
                            if (revertObj2.StateCode == "SUCCESS") {
                                var newFolderId = moveFolder2New (newHomeId, folder.FolderName);//在新服务器创建新的备份,获取备份ID
                                if (string.IsNullOrEmpty (newFolderId)) {//如果新服务器备份ID为空,可能报错了,新服务器可能已经存在这个备份
                                    var backupInfoRes = Newtonsoft.Json.JsonConvert.DeserializeObject<List<BackupInfoRes>> (revertObj2.ResponseData.ToString ());
                                    MoveOldBackupFile2New (newFolderId, backupInfoRes);
                                }
                            }
                        } catch { }
                    }
                }
            } catch {
            }
        }
        /// <summary>
        /// 迁移旧服务器备份文件到新服务器
        /// </summary>
        /// <param name="backupId"></param>
        /// <returns></returns>
        private string MoveOldBackupFile2New ( string backupId, List<BackupInfoRes> responseDataObj)
        {
            Dictionary<string, object> dic = new Dictionary<string, object> ();
            dic.Add ("backupId", backupId);
            dic.Add ("homeId", newHomeId);
            dic.Add ("mark", false);
            var requestJson = HttpUtil.GetSignRequestJson (dic);
            var revertObj = MainPage.RequestHttps ("/home-wisdom/app/home/markVoice", requestJson, false, false, SeverAddr);
            if (revertObj != null) {
                if(revertObj.code == "0") {
                    if(revertObj.data.ToString() == "true") {
                        return true;
            dic.Add ("userId", newUserId);
            dic.Add ("backupClassify", "USER_DEFINED_BACKUP");
            dic.Add ("tenantId", "202106");
            List<BackupFileObj> fileObjs = new List<BackupFileObj> ();
            int index = 0;
            foreach (var file in responseDataObj) {
                var fileName = file.FileName;
                if (fileName == "null" || "UserConfig" == fileName || fileName == UserInfo.GlobalRegisterFile) {
                    continue;
                }
                index++;
                var requestObj3 = new BackupDetailObj () { Id = file.Id };
                var requestJson3 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj3);
                var revertObj3 = MainPage.RequestHttps ("BackupDetail", requestJson3);//下载旧服务器的备份文件
                if (revertObj3.StateCode == "SUCCESS") {
                    var jsonBytes = Newtonsoft.Json.JsonConvert.SerializeObject (revertObj3.ResponseData);
                    var byresss = Newtonsoft.Json.JsonConvert.DeserializeObject<byte []> (jsonBytes);
                    BackupFileObj backupFileObj = new BackupFileObj () { fileName = fileName, content = byresss };
                    fileObjs.Add (backupFileObj);
                    if (fileObjs.Count > 9) {
                        if (!dic.ContainsKey ("list")) {
                            dic.Add ("list", fileObjs);
                        }
                        var json = HttpUtil.GetSignRequestJson (dic);
                        var revertObj_foreach = MainPage.RequestHttps ("/home-wisdom/data/move/file/save", json, false, false, SeverAddr);
                        if (revertObj_foreach != null) {
                            if (revertObj_foreach.code == "0") {
                                fileObjs.Clear ();
                                continue;
                            }
                        }
                    }
                }
            }
            return false;
            if (!dic.ContainsKey ("list")) {
                dic.Add ("list", fileObjs);
            }
            var requestJson = HttpUtil.GetSignRequestJson (dic);
            var revertObj = MainPage.RequestHttps ("/home-wisdom/data/move/file/save", requestJson, false, false, SeverAddr);
            if (revertObj != null) {
                if (revertObj.code == "0") {
                    return "true";
                }
            }
            return "";
        }
        #endregion
        /// 迁移服务器全部备份
        /// </summary>
        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", 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>
        /// 获取定时器列表
        /// </summary>
        /// <param name="newHomeId"></param>
        private List<BackupFileObj> GetTimer (string newHomeId)
        {
            var requestObj = new Timer () { RegionID = UserConfig.Instance.CurrentRegion.RegionID };
            var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj);
            var revertObj = MainPage.RequestHttps ("GetTimerList", requestJson);
            if (revertObj.StateCode == "SUCCESS") {
                var timers = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Timer>> (revertObj.ResponseData.ToString ());
                if(timers.Count > 0) {
                    List<BackupFileObj> backups = new List<BackupFileObj> ();
                    foreach (var timer in timers) {
                        var timerContent = System.Text.Encoding.UTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (timer));
                        BackupFileObj backupFileObj = new BackupFileObj () {
                            fileName = timer.TimerName,
                            content = timerContent
                        };
                        backups.Add (backupFileObj);
                    }
                    return backups;
                }
            } else {
                return null;
            }
            return new List<BackupFileObj> ();
        }
        /// <summary>
        /// 定时器备份
        /// </summary>
        /// <returns></returns>
        private string BackupSchedule (string newHomeId, List<BackupFileObj> backups)
        {
            Dictionary<string, object> dic = new Dictionary<string, object> ();
            dic.Add ("backupDataType", "HDL_ON");
            dic.Add ("homeId", newHomeId);
            dic.Add ("userId", newUserId);
            dic.Add ("backupClassify", "CUSTOM_PROJECT_BACKUP");
            dic.Add ("folderName", "ScheduleBackup" + DateTime.Now.ToString ());
            dic.Add ("tenantId", "202106");
            var requestJson = HttpUtil.GetSignRequestJson (dic);
            var revertObj = MainPage.RequestHttps ("/home-wisdom/data/move/folder/save", requestJson, false, false, SeverAddr);
            if (revertObj != null) {
                if (revertObj.code == "0") {
                    var resultObj = Newtonsoft.Json.JsonConvert.DeserializeObject<FolderObj> (revertObj.data.ToString ());
                    var ddd =  MoveTimerFileData (newHomeId,resultObj.id, backups);
                    return ddd;
                }
            }
            return "";
        }
        /// <summary>
        /// 保存定时器数据
        /// </summary>
        /// <param name="newHomeId"></param>
        /// <param name="backupId"></param>
        /// <param name="fileObjs"></param>
        /// <returns></returns>
        private string MoveTimerFileData (string newHomeId, string backupId, List<BackupFileObj> fileObjs)
        {
            Dictionary<string, object> dic = new Dictionary<string, object> ();
            dic.Add ("backupId", backupId);
            dic.Add ("homeId", newHomeId);
            dic.Add ("userId", newUserId);
            dic.Add ("backupClassify", "CUSTOM_PROJECT_BACKUP");
            dic.Add ("tenantId", "202106");
            dic.Add ("list", fileObjs);
            var requestJson = HttpUtil.GetSignRequestJson (dic);
            var revertObj = MainPage.RequestHttps ("/home-wisdom/data/move/file/save", requestJson, false, false, SeverAddr);
            if (revertObj != null) {
                if (revertObj.code == "0") {
                    return "true";
                }
            }
            return "";
        }
#endregion
        /// <summary>
        /// 检测一端口信息
@@ -474,36 +696,49 @@
                            gateWayString = CommonPage.MyEncodingUTF8.GetString (IO.FileUtils.ReadFile (gatewayFileName));
                            common = Newtonsoft.Json.JsonConvert.DeserializeObject<GatewayBase> (gateWayString);
                            //bool
                            CommonPage.IsRemote = false;
                            Control.ControlBytesSendHasReturn (Command.ReadGateway, common.SubnetID, common.DeviceID, new byte [] { CommonPage.RandomHigh, CommonPage.RandomLow });
                            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;
                                });
                            }
                            var visionString = Encoding.GetEncoding ("gb2312").GetString (bytes);
                            int result = -99;
                            if (visionString.Contains( "Ind_V03.01U_2021/07/12")) {//FW_MCIP-L1_RF.01_V03.01_210712_STM32F107VCT6_U2_india_beta") {
                            if (visionString.Contains ("Ind_C03.02U_2022/06/22")) {
                                Application.RunOnMainThread (() => {
                                    btnTipMsg.Text = "网关固件已升级,正在初始化网关";
                                    btnTipMsg.Text = "Gateway firmware has been upgraded, initializing gateway.";
                                    btnTipMsg.TextColor = SkinStyle.Current.TextColor;
                                });
                                result = 100;
                            //} else if (!visionString.Contains ("Ind_V02.35U_2019/06/25")) {//目前只允许升级这个固件的网关
                            //    Application.RunOnMainThread (() => {
                            //        btnTipTitle.Text = "The gateway does not support automatic migration. Please contact technical support.";
                            //        btnTipTitle.Height = Application.GetRealHeight (150);
                            //        btnTipTitle.TextColor = SkinStyle.Current.DelColor;
                            //        btnTipMsg.Text = "";
                            //        etPwd.Visible = false;
                            //        loading.Hide ();
                            //        btnSave.Visible = false;
                            //        btnClose.Width = Application.GetRealWidth (500);
                            //    });
                            //    return;
                            } else {
                                Application.RunOnMainThread (() => {
                                    btnTipMsg.Text = "正在升级网关";
                                    btnTipMsg.Text = "Upgrading gateway.";
                                    btnTipMsg.TextColor = SkinStyle.Current.TextColor;
                                });
                                //需要升级一端口
@@ -517,77 +752,52 @@
                                arrayTemp [1] = Convert.ToByte ((upgradeData.Count & 0xFF0000) >> 16);
                                arrayTemp [2] = Convert.ToByte ((upgradeData.Count & 0xFF00) >> 8);
                                arrayTemp [3] = Convert.ToByte (upgradeData.Count & 0xFF);
                                //丢失次数
                                int lostCount = 0;
                                while (true) {
                                    var ub = MainPage.GatewayStatus.Split ("_");
                                    if (ub.Length > 1) {
                                         result = Convert.ToInt32 (ub [1]);
                                        result = Convert.ToInt32 (ub [1]);
                                        //if (result < result0 && result0 < 100)
                                        {
                                            //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;
                                        });
                                        //初始化标记
                                        MainPage.GatewayStatus = "";
                                        break;
                                    }
                                     //if (result == -1) {
                                     //    if (lostCount > 3) {
                                     //        Application.RunOnMainThread (() => {
                                     //            btnTipMsg.Text = "网关升级失败,请重试.";
                                     //            loading.Hide ();
                                     //        });
                                     //        return;
                                     //    }
                                     //} else if (result == -2) {
                                     //    if (lostCount > 3) {
                                     //        Application.RunOnMainThread (() => {
                                     //            btnTipMsg.Text = "网关无响应。";
                                     //            loading.Hide ();
                                     //        });
                                     //        return;
                                     //    }
                                     //} else if (result == 999999) {
                                     //    Application.RunOnMainThread (() => {
                                     //        btnTipMsg.Text = "网关升级成功.正在初始化网关";
                                     //        btnTipMsg.TextColor = SkinStyle.Current.TextColor;
                                     //    });
                                     //    break;
                                     //}
                                     else {
                                    } else {
                                        if (upgradeData.Count >= result) {
                                            var listPack = upgradeData [result-1];//
                                            var listPack = upgradeData [result - 1];//
                                            byte [] packData = new byte [2 + listPack.Count];
                                            packData [0] = Convert.ToByte (result / 256);
                                            packData [1] = Convert.ToByte (result % 256 );
                                            packData [1] = Convert.ToByte (result % 256);
                                            Array.Copy (listPack.ToArray (), 0, packData, 2, listPack.Count);
                                            Console.WriteLine ("packId"+result);
                                            Console.WriteLine ("packId" + result);
                                            SendUpgradeData (common.SubnetID, common.DeviceID, packData);
                                        }
                                    }
                                }
                            }
                            //重新设置一下子网号
                            SetSubnetId (common);
                            System.Threading.Thread.Sleep (1000);
                            if (result == 100) {
@@ -605,14 +815,21 @@
                                initialBytes [11] = 0x00;
                                //初始化网关命令发送
                                InitializationGateway (common.SubnetID, common.DeviceID, initialBytes);
                                int initiaIndex = 0;
                                while (true) {
                                    if (MainPage.GatewayStatus != "Initialization_complete") {
                                        System.Threading.Thread.Sleep (100);
                                    } else {
                                    } else if (initiaIndex == 0) {
                                        //初始化标记
                                        MainPage.GatewayStatus = "";
                                        Application.RunOnMainThread (() => {
                                            btnTipMsg.Text = "初始化网关成功,正在打开网关远程配置。";
                                            btnTipMsg.Text = "Successfully initialized the gateway. Opening the gateway remote configuration.";
                                            btnTipMsg.TextColor = SkinStyle.Current.TextColor;
                                        });
                                        break;
                                    }
                                    initiaIndex++;
                                    if(initiaIndex > 200) {
                                        break;
                                    }
                                }
@@ -620,13 +837,14 @@
                                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;
                                        loading.Hide ();
                                    });
                                    return;
                                }
@@ -635,117 +853,273 @@
                                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;
                                }
                                //迁移住宅,获取新的homeid
                                var newHomeId = Home2New ();
                                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;
                                }
                                //写入homeId
                                var homeIdSendBytes = new byte [73];
                                var homeIdByte = new byte [36];
                                var homeStrs = newHomeId;// UserConfig.Instance.CurrentRegion.RegionID.ToString ();//需要使用新平台生成的homeId
                                for (int i = 0; i < UserConfig.Instance.CurrentRegion.RegionID.ToString ().Length; i++) {
                                    homeIdByte [i] = Convert.ToByte (homeStrs [i].ToString (), 16);
                                }
                                Array.Copy (homeIdByte, homeIdSendBytes, homeIdByte.Length);
                                var writeHomeIdResult = WriteHomeId (common.SubnetID, common.DeviceID, homeIdSendBytes);
                                var writeHomeIdResult = SetGateWayAdminInfo (common.SubnetID, newHomeId);
                                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;
                                        loading.Hide ();
                                        return;
                                    });
                                }
                                //获取上网秘钥
                                var netKet = GetInternetAccessKey (common.MAC.Replace (".", ""));
                                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];
                                    var secretkeyByte = new byte [netKet.Length];
                                    for (int i = 0; i < netKet.Length; i++) {
                                        secretkeyByte [i] = Convert.ToByte (netKet [i].ToString (), 16);
                                    }
                                    Array.Copy (secretkeyByte, 0, secretkeySendBytes, 1, secretkeyByte.Length);
                                    var deviceSecret = Newtonsoft.Json.JsonConvert.DeserializeObject<DeviceSecret> (netKet);
                                    var secretkeyByte = Encoding.UTF8.GetBytes (deviceSecret.deviceSecret);
                                    var secretkeySendBytes = new byte [secretkeyByte.Length + 1];
                                    secretkeySendBytes [0] = 1;
                                    Array.Copy (secretkeyByte, 0, secretkeySendBytes, 1, secretkeyByte.Length);
                                    //for (int i = 0; i < netKet.Length; i++) {
                                    //    secretkeyByte [i] = Convert.ToByte (netKet [i].ToString (), 16);
                                    //}
                                    //Array.Copy (secretkeyByte, 0, secretkeySendBytes, 1, secretkeyByte.Length);
                                    //secretkeySendBytes [0] = 1;
                                    System.Threading.Thread.Sleep (10000);
                                    //写入上网秘钥
                                    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;
                                            loading.Hide ();
                                            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 = "上网秘钥获取失败。";
                                        btnTipMsg.Text = "Failed to obtain the Internet secret key. Please try again.";
                                        btnTipMsg.TextColor = SkinStyle.Current.DelColor;
                                        return;
                                    });
                                }
                                //写入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 ();
                                    });
                                    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, newUserId);
                                var backId = moveFolder2New (newHomeId);
                                //迁移备份文件
                                var moveFileResult = MoveFile2New (newHomeId, newUserId, backId);
                                var moveFileResult = MoveFile2New (backId);
                                // Application.RunOnMainThread (() => {
                                //     btnTipMsg.Text = "Migrating server backup.";
                                //     btnTipMsg.TextColor = SkinStyle.Current.TextColor;
                                // });
                                // MoveOldBackupList ();
                                if (moveFileResult == "true") {
                                    Application.RunOnMainThread (() => {
                                        btnTipMsg.Text = "Migration backup succeeded, migrating Schedule data.";
                                        btnTipMsg.TextColor = SkinStyle.Current.TextColor;
                                    });
                                    //迁移定时器
                                    var timerList = GetTimer (newHomeId);
                                    if (timerList == null) {
                                        Application.RunOnMainThread (() => {
                                            btnTipMsg.Text = "Schedule migration failed. Please try again.";
                                            btnTipMsg.TextColor = SkinStyle.Current.DelColor;
                                            loading.Hide ();
                                            return;
                                        });
                                    } else {
                                        if (timerList.Count > 0) {
                                            var backupTimerResult = BackupSchedule (newHomeId, timerList);
                                            if (string.IsNullOrEmpty (backupTimerResult)) {
                                                btnTipMsg.Text = "Schedule migration failed.. Please try again.";
                                                btnTipMsg.TextColor = SkinStyle.Current.DelColor;
                                                loading.Hide ();
                                                return;
                                            }
                                        }
                                    }
                                    //标记流程完成
                                    //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 +1129,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;
                }
@@ -780,7 +1153,8 @@
        {
            byte [] buffer = new byte [1024];
            List<List<byte>> upgradeData = new List<List<byte>> ();
            System.IO.Stream stream = Application.Activity.Assets.Open ("FW_MCIP-L1_RF.01_V03.01_210712_STM32F107VCT6_U2_india_beta.bin");
            System.IO.Stream stream = Application.Activity.Assets.Open ("india_beta.bin");
            //System.IO.Stream stream = Application.Activity.Assets.Open ("india_test.bin");
            int length = 0;
            try {
                while ((length = stream.Read (buffer, 0, buffer.Length)) != 0) {
@@ -837,6 +1211,28 @@
        }
        /// <summary>
        /// 设置子网号
        /// </summary>
        /// <param name="gatewayDevice"></param>
        private void SetSubnetId (GatewayBase gatewayDevice)
        {
            string [] macAddress = gatewayDevice.MAC.Split ('.');
                byte [] Musics = new byte [10];
                for (int i = 0; i < macAddress.Length; i++) {
                    Musics [i] = Convert.ToByte (macAddress [i], 16);
                }
                try {
                    Musics [8] = Convert.ToByte (Convert.ToInt32 (gatewayDevice.SubnetID));
                    if (Musics [8] < 0 || Musics [8] > 255) {
                        throw new Exception ();
                    }
                } catch {
                    return;
                }
                Control.ControlBytesSend (Command.SetDeviceSubnetID, gatewayDevice.SubnetID, gatewayDevice.DeviceID, Musics);
        }
        /// <summary>
        /// 设备网关开启远程
        /// </summary>
        private bool SetGatewayRemote(byte subnetId, byte deviceId)
@@ -844,38 +1240,37 @@
            var sendByte = new byte [67];
            sendByte [0] = 4;
            var result = Control.ControlBytesSendHasReturn (Command.SetGateWayModelInfo, subnetId, deviceId, sendByte);
            if (result == null) {
            } else {
                if(result.Length>0 && result[0] == 248) {
                    return true;
                }
            if (result != null) {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 写入homeId
        /// </summary>
        /// <param name="subnetId"></param>
        /// <param name="deviceId"></param>
        /// <param name="sendByets"></param>
        private bool WriteHomeId (byte subnetId, byte deviceId, byte [] sendByets)
        {
            var result = Control.ControlBytesSendHasReturn (Command.WriteHomeId, subnetId, deviceId, sendByets);
            if (result == null)
                return false;
            if (result.Length == 3) {
                if (result [2] == 0xF8) {
                    return true;
                }
            } else if (result.Length == 1) {
                if (result [0] == 0xF8) {
                    return true;
                }
            }
            return false;
        }
        ///// <summary>
        ///// 写入homeId
        ///// </summary>
        ///// <param name="subnetId"></param>
        ///// <param name="deviceId"></param>
        ///// <param name="sendByets"></param>
        //private bool WriteHomeId (byte subnetId, byte deviceId, byte [] sendByets)
        //{
        //    //var result = Control.ControlBytesSendHasReturn (Command.WriteHomeId, subnetId, deviceId, sendByets);
        //    //if (result == null)
        //    //    return false;
        //    //if (result.Length == 3) {
        //    //    if (result [2] == 0xF8) {
        //    //        return true;
        //    //    }
        //    //} else if (result.Length == 1) {
        //    //    if (result [0] == 0xF8) {
        //    //        return true;
        //    //    }
        //    //}
        //    //return false;
        //    return SetGateWayAdminInfo (subnetId, homeId);
        //}
        
        /// <summary>
@@ -905,6 +1300,75 @@
            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!");
        }
        /// <summary>
        /// 修改管理员信息
        /// </summary>
        /// <param name="adminBytes"></param>
        /// <returns></returns>
        private bool SetGateWayAdminInfo (byte subnetId,string homeId)
        {
            //byte [] name = CommonPage.MyEncodingGB2312.GetBytes (MainPage.LoginUser.AccountString);
            byte [] currentRegionIdBytes = CommonPage.MyEncodingGB2312.GetBytes (homeId);
            byte [] adminBytes = new byte [73];
            adminBytes [36] = 1;//住宅标志位
            Array.Copy (currentRegionIdBytes, 0, adminBytes, 37, 36 < currentRegionIdBytes.Length ? 36 : currentRegionIdBytes.Length);
            byte [] result = Control.ControlBytesSendHasReturn (Command.WriteHomeId, subnetId, 0, adminBytes);
            if (result == null)
                return false;
            if (result.Length == 3) {
                if (result [2] == 0xF8) {
                    return true;
                }
            } else if (result.Length == 1) {
                if (result [0] == 0xF8) {
                    return true;
                }
            }
            return false;
        }
    }
}