黄学彪
2019-11-18 16604a593202f2f87adf71abd57d036fe7da3b52
ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlGatewayBackupLogic.cs
@@ -104,7 +104,7 @@
        /// <returns></returns>
        private bool SaveNVFile(ZbGateway realGateway)
        {
            //正在保存协调器nv分区组网信息
            //正在保存协调器nv分区信息
            ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uGatewayNVFileSaving));
            ProgressFormBar.Current.SetValue(0);
            System.Threading.Thread.Sleep(1000);
@@ -131,7 +131,7 @@
            {
                System.Threading.Thread.Sleep(1000);
                TimeOut++;
                ProgressFormBar.Current.SetValue2(TimeOut, 60);
                ProgressFormBar.Current.SetValue(TimeOut, 60);
            }
            realGateway.Actions -= action;
            if (result != 0)
@@ -183,7 +183,7 @@
            int TimeOut = 0;
            while (result == -1 && TimeOut < 30)
            {
                System.Threading.Thread.Sleep(100);
                System.Threading.Thread.Sleep(1000);
                TimeOut++;
            }
            realGateway.Actions -= action;
@@ -267,7 +267,7 @@
                    }
                    //设置进度百分比
                    var persent = Convert.ToInt32(jobject["Data"]["BackupPercent"].ToString());
                    ProgressFormBar.Current.SetValue2(persent, 100);
                    ProgressFormBar.Current.SetValue(persent, 100);
                }
            };
            realGateway.Actions += action;
@@ -313,6 +313,394 @@
        #endregion
        #region ■ 网关还原___________________________
        /// <summary>
        /// 执行网关还原操作
        /// </summary>
        /// <param name="realGateway">真实物理网关对象</param>
        /// <param name="backupId">备份主键</param>
        public bool RecoverGateway(ZbGateway realGateway, string backupId)
        {
            //展开进度条
            ProgressFormBar.Current.Start();
            //从云端获取备份文件列表
            var result = this.GetGatewayFileList(realGateway, backupId);
            if (result == false)
            {
                //关闭进度条
                ProgressFormBar.Current.Close();
                return false;
            }
            //从云端下载备份文件
            result = this.DownloadGatewayFile(realGateway, backupId);
            if (result == false)
            {
                //关闭进度条
                ProgressFormBar.Current.Close();
                return false;
            }
            //恢复协调器nv分区的组网信息
            result = this.RestoreGatewayNV(realGateway);
            if (result == false)
            {
                //关闭进度条
                ProgressFormBar.Current.Close();
                return false;
            }
            //打开协调器串口发送功能
            result = this.OpenZbMsComSend(realGateway);
            if (result == false)
            {
                //关闭进度条
                ProgressFormBar.Current.Close();
                return false;
            }
            //重启网关系统
            result = this.GatewayReboot(realGateway);
            //关闭进度条
            ProgressFormBar.Current.Close();
            return result;
        }
        #endregion
        #region ■ 从云端获取备份文件列表_____________
        /// <summary>
        /// 从云端获取备份文件列表
        /// </summary>
        /// <param name="realGateway"></param>
        /// <param name="backupClassId"></param>
        /// <returns></returns>
        private bool GetGatewayFileList(ZbGateway realGateway, string backupClassId)
        {
            //正在获取备份文件列表
            ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uBackupFileListGetting));
            ProgressFormBar.Current.SetValue(0);
            System.Threading.Thread.Sleep(1000);
            int result = -1;
            Action<string, string> action = (topic, message) =>
            {
                var gatewayID = topic.Split('/')[0];
                if (topic == gatewayID + "/GetDataFileList_Respon")
                {
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
                    result = Convert.ToInt32(jobject["Data"]["Status"].ToString());
                    if (result != 0)
                    {
                        //获取备份文件列表失败
                        this.ShowErrorMsg(Language.StringByID(R.MyInternationalizationString.uGetBackupFileListFail));
                        return;
                    }
                }
            };
            realGateway.Actions += action;
            //获取接口的连接模式
            string fullUrl = string.Empty;
            var connectMode = UserCenterLogic.GetHttpConnectMode(true);
            if (connectMode == HttpConnectMode.Normal)
            {
                //普通访问
                fullUrl = $"{Common.CommonPage.RequestHttpsHost}/{"App/GetHomeDataBackupUploadListPagger"}";
            }
            else if (connectMode == HttpConnectMode.Admin)
            {
                //以管理员的身份访问,自身是成员
                fullUrl = $"{Common.Config.Instance.AdminRequestBaseUrl}/{"App/GetHomeDataBackupUploadListPagger"}";
            }
            var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 6202 } };
            var data = new Newtonsoft.Json.Linq.JObject {
                { "url", fullUrl }, { "RequestVersion", Common.CommonPage.RequestVersion },
                { "LoginAccessToken", UserCenterLogic.GetConnectMainToken() }, { "IsGatewayAutoBackup", 0 },
                { "BackupClassId", backupClassId },{ "GatewayUniqueId",HdlGatewayLogic.Current.GetGatewayId(realGateway)},
                { "IsOtherAccountCtrl",connectMode == HttpConnectMode.Admin?1:0}
            };
            jObject.Add("Data", data);
            realGateway.Send("GetDataFileList", jObject.ToString());
            int timeOut = 0;
            while (result == -1 && timeOut < 30)
            {
                System.Threading.Thread.Sleep(1000);
                timeOut++;
            }
            realGateway.Actions -= action;
            if (result != 0)
            {
                string msg = Language.StringByID(R.MyInternationalizationString.uGetBackupFileListFail);
                if (result == -1)
                {
                    //响应超时,获取备份文件列表失败
                    msg = Language.StringByID(R.MyInternationalizationString.uResponseTimeoutsAndGetBackupFileListFail);
                }
                this.ShowErrorMsg(msg);
                return false;
            }
            return true;
        }
        #endregion
        #region ■ 从云端下载备份文件_________________
        /// <summary>
        /// 从云端下载备份文件
        /// </summary>
        /// <param name="realGateway"></param>
        /// <param name="backupClassId"></param>
        /// <returns></returns>
        private bool DownloadGatewayFile(ZbGateway realGateway, string backupClassId)
        {
            //正在下载备份文件
            ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uBackupFileDownloading));
            ProgressFormBar.Current.SetValue(0);
            System.Threading.Thread.Sleep(1000);
            bool threadAction = true;
            bool success = false;
            int timeOut = 0;
            Action<string, string> action = (topic, message) =>
            {
                var gatewayID = topic.Split('/')[0];
                if (topic == gatewayID + "/RestoreDataFileProgress_Respon")
                {
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
                    var result = Convert.ToInt32(jobject["Data"]["Status"].ToString());
                    if (result == 1 || result == 3)
                    {
                        //下载备份文件失败
                        this.ShowErrorMsg(Language.StringByID(R.MyInternationalizationString.uDownloadBackupFileFail));
                        threadAction = false;
                        return;
                    }
                    else if (result == 2)
                    {
                        //下载成功
                        success = true;
                        threadAction = false;
                        return;
                    }
                    else if (result == 0)
                    {
                        //刷新等待时间
                        timeOut = 0;
                        //设置进度百分比
                        var persent = Convert.ToInt32(jobject["Data"]["RestoreDataPercent"].ToString());
                        ProgressFormBar.Current.SetValue(persent, 100);
                    }
                }
            };
            realGateway.Actions += action;
            //获取接口的连接模式
            string fullUrl = string.Empty;
            var connectMode = UserCenterLogic.GetHttpConnectMode(true);
            if (connectMode == HttpConnectMode.Normal)
            {
                //普通访问
                fullUrl = $"{Common.CommonPage.RequestHttpsHost}/{"App/DownloadSomeDataBackup"}";
            }
            else if (connectMode == HttpConnectMode.Admin)
            {
                //以管理员的身份访问,自身是成员
                fullUrl = $"{Common.Config.Instance.AdminRequestBaseUrl}/{"App/DownloadSomeDataBackup"}";
            }
            var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 6203 } };
            var data = new Newtonsoft.Json.Linq.JObject {
                { "url", fullUrl }, { "RequestVersion", Common.CommonPage.RequestVersion },
                { "LoginAccessToken", UserCenterLogic.GetConnectMainToken() }, { "IsGatewayAutoBackup", 0 },
                { "BackupClassId", backupClassId },{ "GatewayUniqueId",HdlGatewayLogic.Current.GetGatewayId(realGateway)},
                { "IsOtherAccountCtrl",connectMode == HttpConnectMode.Admin?1:0}
            };
            jObject.Add("Data", data);
            realGateway.Send("GetDataFileList", jObject.ToString());
            while (threadAction == true && timeOut < 30)
            {
                System.Threading.Thread.Sleep(1000);
                timeOut++;
            }
            realGateway.Actions -= action;
            if (timeOut >= 30)
            {
                //响应超时,下载备份文件失败
                string msg = Language.StringByID(R.MyInternationalizationString.uResponseTimeoutsAndDownloadBackupFileFail);
                this.ShowErrorMsg(msg);
                return false;
            }
            return success;
        }
        #endregion
        #region ■ 恢复协调器nv分区的组网信息_________
        /// <summary>
        /// 恢复协调器nv分区的组网信息
        /// </summary>
        /// <param name="realGateway"></param>
        /// <returns></returns>
        private bool RestoreGatewayNV(ZbGateway realGateway)
        {
            //正在恢复协调器nv分区
            ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uGatewayNVRestoring));
            ProgressFormBar.Current.SetValue(0);
            System.Threading.Thread.Sleep(1000);
            int result = -1;
            Action<string, string> action = (topic, message) =>
            {
                var gatewayID = topic.Split('/')[0];
                if (topic == gatewayID + "/" + "ZbGwOperation/RestoreNV_Respon")
                {
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
                    result = Convert.ToInt32(jobject["Data"]["Flag"].ToString());
                }
            };
            realGateway.Actions += action;
            var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 501 } };
            var data = new Newtonsoft.Json.Linq.JObject { { "ImageName", "NVImage.bin" }, { "ImagePath", "/etc/hdlDat/" } };
            jObject.Add("Data", data);
            realGateway.Send("ZbGwOperation/RestoreNV", jObject.ToString());
            int TimeOut = 0;
            while (result == -1 && TimeOut < 60)
            {
                System.Threading.Thread.Sleep(1000);
                TimeOut++;
                ProgressFormBar.Current.SetValue(TimeOut, 60);
            }
            realGateway.Actions -= action;
            if (result != 0)
            {
                //恢复协调器nv分区失败
                string msg = Language.StringByID(R.MyInternationalizationString.uRestoreGatewayNVFail);
                if (result == -1)
                {
                    //响应超时,恢复协调器nv分区失败
                    msg = Language.StringByID(R.MyInternationalizationString.uResponseTimeoutsAndRestoreGatewayNVFail);
                }
                this.ShowErrorMsg(msg);
                return false;
            }
            return true;
        }
        #endregion
        #region ■ 重启网关系统_______________________
        /// <summary>
        /// 重启网关系统
        /// </summary>
        /// <param name="realGateway"></param>
        /// <returns></returns>
        private bool GatewayReboot(ZbGateway realGateway)
        {
            //正在重启网关系统
            ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uGatewayRerooting));
            ProgressFormBar.Current.SetValue(0);
            System.Threading.Thread.Sleep(1000);
            int result = -1;
            Action<string, string> action = (topic, message) =>
            {
                var gatewayID = topic.Split('/')[0];
                if (topic == gatewayID + "/" + "GwReboot_Respon")
                {
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
                    result = Convert.ToInt32(jobject["Data"]["Result"].ToString());
                }
            };
            realGateway.Actions += action;
            var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 83 } };
            realGateway.Send("GwReboot", jObject.ToString());
            int timeOut = 0;
            while (result == -1 && timeOut < 30)
            {
                System.Threading.Thread.Sleep(1000);
                timeOut++;
            }
            realGateway.Actions -= action;
            if (result != 0)
            {
                //重启网关系统失败
                string msg = Language.StringByID(R.MyInternationalizationString.uGatewayRerootFail);
                if (result == -1)
                {
                    msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, null, "回复超时");
                }
                this.ShowErrorMsg(msg);
                return false;
            }
            //网关已经接收到重启命令,接下来去判断网关是否重启完成
            bool gatewatAction = false;
            bool threadAction = true;
            HdlThreadLogic.Current.RunThread(async () =>
            {
                timeOut = 0;
                while (gatewatAction == false)
                {
                    //大概网关重启要60秒
                    if (timeOut >= 65 && timeOut % 5 == 0)
                    {
                        //5秒一次,去获取版本
                        var data = await HdlGatewayLogic.Current.GetGatewayNewInfoAsync(realGateway, ShowErrorMode.NO);
                        if (data != null)
                        {
                            gatewatAction = true;
                            threadAction = false;
                            break;
                        }
                    }
                    await System.Threading.Tasks.Task.Delay(1000);
                    timeOut++;
                    //设置进度值
                    ProgressFormBar.Current.SetValue(timeOut, 180);
                    if (timeOut >= 180)
                    {
                        //180秒还等不到的话,网关应该出问题了
                        threadAction = false;
                        break;
                    }
                }
            });
            while (threadAction == true)
            {
                System.Threading.Thread.Sleep(4000);
            }
            if (gatewatAction == true)
            {
                //网关数据恢复成功!
                string msg = Language.StringByID(R.MyInternationalizationString.uRestoreGatewayDataSuccess);
                this.ShowTipMsg(msg);
                return true;
            }
            else
            {
                //响应超时,重启网关系统失败
                string msg = Language.StringByID(R.MyInternationalizationString.uResponseTimeoutsAndGatewayRerootFail);
                this.ShowErrorMsg(msg);
                return false;
            }
        }
        #endregion
        #region ■ 一般方法___________________________
        /// <summary>