gxc
2019-12-20 eb9cf55fc8568cb2d4b4bfac9deb617ce766a7a5
ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlBackupLogic.cs
@@ -35,6 +35,11 @@
            }
        }
        /// <summary>
        /// Log备份的名字
        /// </summary>
        private string LogBackupName = "##Log##";
        #endregion
        #region ■ 获取备份名字列表___________________
@@ -44,8 +49,9 @@
        /// </summary>
        /// <param name="ZigbeeBackupType">0:App手动备份 1: App 自动备份 2:网关手动备份 3:网关自动备份</param>
        /// <param name="ZigbeeUniqueId">网关的唯一Id,获取网关备份需要</param>
        /// <param name="getLogBackup">是否获取Log备份</param>
        /// <returns></returns>
        public async Task<List<BackupListNameInfo>> GetBackupListNameFromDB(int ZigbeeBackupType = 0, string ZigbeeUniqueId = null)
        public async Task<List<BackupListNameInfo>> GetBackupListNameFromDB(int ZigbeeBackupType = 0, string ZigbeeUniqueId = null, bool getLogBackup = false)
        {
            //备份名字很少的内存,可以直接一次性全部取完
            var pra = new GetBackupListPra();
@@ -65,24 +71,25 @@
            }
            var data = Newtonsoft.Json.JsonConvert.DeserializeObject<GetBackupListResult>(result);
            if (ZigbeeUniqueId != null)
            var list = new List<BackupListNameInfo>();
            foreach (var data2 in data.PageData)
            {
                var list = new List<BackupListNameInfo>();
                foreach (var data2 in data.PageData)
                if (ZigbeeUniqueId != null && data2.GatewayUniqueId != ZigbeeUniqueId)
                {
                    if (data2.GatewayUniqueId == ZigbeeUniqueId)
                    {
                        list.Add(data2);
                    }
                    continue;
                }
                return list;
                if (getLogBackup == false && data2.BackupName == LogBackupName)
                {
                    //不获取Log备份
                    continue;
                }
                list.Add(data2);
            }
            return data.PageData;
            return list;
        }
        /// <summary>
        /// <para>从云端获取备份的文件,然后存入本地指定的临时文件夹(函数内部有进度条)</para>
        /// <para>从云端获取备份的文件,然后存入本地指定的临时文件夹</para>
        /// <para>返回文件夹名字(里面存放着全部的文件),返回null时,代表失败</para>
        /// </summary>
        /// <param name="BackupClassId">BackupClassId</param>
@@ -91,23 +98,25 @@
        /// <returns></returns>
        public async Task<string> GetBackFileFromDBAndSetToLocation(string BackupClassId, bool IsGatewayAutoBackup = false, string ZigbeeUniqueId = null)
        {
            //不允许按系统的返回键
            Shared.Common.CommonPage.BackKeyCanClick = false;
            UserCenterResourse.Option.AppCanSignout = false;
            //首先先创建一个临时文件夹,存在文件则清空
            string newDir = System.IO.Path.Combine(DirNameResourse.LocalMemoryDirectory, DirNameResourse.DownLoadBackupTempDirectory);
            Global.CreateEmptyDirectory(newDir, true);
            //开启进度条
            ProgressBar.Show();
            //获取这个备份下面有多少个文件
            List<string> listFile = await GetBackFileIDFromDB(BackupClassId, IsGatewayAutoBackup, ZigbeeUniqueId);
            if (listFile == null)
            {
                //关闭进度条
                ProgressBar.Close();
                ProgressFormBar.Current.Close();
                //允许按系统的返回键
                Shared.Common.CommonPage.BackKeyCanClick = true;
                UserCenterResourse.Option.AppCanSignout = true;
                return null;
            }
            //设置最大值
            ProgressBar.SetMaxValue(listFile.Count);
            var pra = new LoadBackInfoPra();
            pra.BackupClassId = BackupClassId;
@@ -117,13 +126,18 @@
            pra.LoginAccessToken = UserCenterLogic.GetConnectMainToken();
            //一个个的下载文件
            foreach (string fileName in listFile)
            int listFileCount = listFile.Count;
            for (int i = 0; i < listFile.Count; i++)
            {
                string fileName = listFile[i];
                //账号已经退出
                if (HdlCheckLogic.Current.IsAccountLoginOut() == true)
                {
                    //关闭进度条
                    ProgressBar.Close(true);
                    ProgressFormBar.Current.Close();
                    //允许按系统的返回键
                    Shared.Common.CommonPage.BackKeyCanClick = true;
                    UserCenterResourse.Option.AppCanSignout = true;
                    return null;
                }
@@ -133,16 +147,22 @@
                if (result == null)
                {
                    //关闭进度条
                    ProgressBar.Close();
                    ProgressFormBar.Current.Close();
                    //允许按系统的返回键
                    Shared.Common.CommonPage.BackKeyCanClick = true;
                    UserCenterResourse.Option.AppCanSignout = true;
                    return null;
                }
                //将输入写入本地的临时文件夹
                Global.WriteFileToDirectoryByBytes(newDir, fileName, result);
                //设置进度值
                ProgressBar.SetValue(1);
                ProgressFormBar.Current.SetValue(i + 1, listFileCount);
            }
            //关闭进度条
            ProgressBar.Close();
            ProgressFormBar.Current.Close();
            //允许按系统的返回键
            Shared.Common.CommonPage.BackKeyCanClick = true;
            UserCenterResourse.Option.AppCanSignout = true;
            return newDir;
        }
@@ -222,8 +242,9 @@
        /// <param name="BackupClassId">备份主键ID</param>
        /// <param name="ManualBackupType">手动备份类型(0:App手动备份,2:网关手动备份)</param>
        /// <param name="upPath">指定上传的是哪个文件夹的文件(全路径),不指定时,上传的是本地路径下的文件</param>
        /// <param name="showBar">是否设置显示进度条</param>
        /// <returns></returns>
        public async Task<bool> UpLoadBackupFileToDB(string BackupClassId, int ManualBackupType = 0, string upPath = "")
        public async Task<bool> UpLoadBackupFileToDB(string BackupClassId, int ManualBackupType = 0, string upPath = "", bool showBar = true)
        {
            List<string> listAllFile = null;
            //文件夹的全路径
@@ -261,18 +282,31 @@
            }
            //开启进度条
            ProgressBar.SetMaxValue(listAllFile.Count);
            ProgressBar.Show();
            int listFileCount = listAllFile.Count;
            if (showBar == true)
            {
                //开启进度条 正在上传备份文件
                ProgressFormBar.Current.Start();
                ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uBackupFileUploading));
            }
            //不允许按系统的返回键
            Shared.Common.CommonPage.BackKeyCanClick = false;
            UserCenterResourse.Option.AppCanSignout = false;
            //你说为什么这里不分一个函数出来(理论上是分函数出来的)
            //是因为如果分函数的话,会出现双重异步,导致主线程的进度条的百分比永远定死0%
            foreach (string file in listNormalFile)
            for (int i = 0; i < listNormalFile.Count; i++)
            {
                string file = listNormalFile[i];
                //账号已经退出
                if (HdlCheckLogic.Current.IsAccountLoginOut() == true)
                {
                    //关闭进度条
                    ProgressBar.Close(true);
                    ProgressFormBar.Current.Close();
                    //允许按系统的返回键
                    Shared.Common.CommonPage.BackKeyCanClick = true;
                    UserCenterResourse.Option.AppCanSignout = true;
                    return false;
                }
                //判断是否是应该上传的文件
@@ -294,20 +328,27 @@
                if (falge == false)
                {
                    //关闭进度条
                    ProgressBar.Close();
                    ProgressFormBar.Current.Close();
                    //允许按系统的返回键
                    Shared.Common.CommonPage.BackKeyCanClick = true;
                    UserCenterResourse.Option.AppCanSignout = true;
                    return false;
                }
                //设置进度值
                ProgressBar.SetValue(list.Count);
                ProgressFormBar.Current.SetValue(i + 1, listFileCount);
            }
            foreach (string file in listPicFile)
            for (int i = 0; i < listPicFile.Count; i++)
            {
                string file = listPicFile[i];
                //账号已经退出
                if (HdlCheckLogic.Current.IsAccountLoginOut() == true)
                {
                    //关闭进度条
                    ProgressBar.Close(true);
                    ProgressFormBar.Current.Close();
                    //允许按系统的返回键
                    Shared.Common.CommonPage.BackKeyCanClick = true;
                    UserCenterResourse.Option.AppCanSignout = true;
                    return false;
                }
@@ -319,14 +360,28 @@
                if (result == false)
                {
                    //关闭进度条
                    ProgressBar.Close();
                    ProgressFormBar.Current.Close();
                    //允许按系统的返回键
                    Shared.Common.CommonPage.BackKeyCanClick = true;
                    UserCenterResourse.Option.AppCanSignout = true;
                    return false;
                }
                //设置进度值
                ProgressBar.SetValue(listPic.Count);
                ProgressFormBar.Current.SetValue(i + 1 + listNormalFile.Count, listFileCount);
            }
            //上传Log备份
            if (upPath != UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.LogDirectory))
            {
                //里面有个回调自身的方法,所以这里加个判断,不然会死循环
                await this.UpLoadLogBackup();
            }
            //进度条关闭
            ProgressBar.Close();
            ProgressFormBar.Current.Close();
            //允许按系统的返回键
            Shared.Common.CommonPage.BackKeyCanClick = true;
            UserCenterResourse.Option.AppCanSignout = true;
            return true;
        }
@@ -483,6 +538,73 @@
        #endregion
        #region ■ 上传Log备份________________________
        /// <summary>
        /// 上传Log备份
        /// </summary>
        /// <returns></returns>
        private async Task<bool> UpLoadLogBackup()
        {
            string upPath = UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.LogDirectory);
            if (HdlAutoBackupLogic.GetFileFromDirectory(upPath).Count == 0)
            {
                //没有Log文件
                return true;
            }
            //无视错误
            IMessageCommon.Current.IgnoreError = true;
            //从云端获取数据
            var pageData = await HdlBackupLogic.Current.GetBackupListNameFromDB(0, null, true);
            if (pageData == null)
            {
                //恢复
                IMessageCommon.Current.IgnoreError = false;
                return false;
            }
            for (int i = 0; i < pageData.Count; i++)
            {
                if (pageData[i].BackupName == LogBackupName)
                {
                    //删除掉这个备份
                    bool flage = await this.DeleteDbBackupData(pageData[i].Id);
                    if (flage == false)
                    {
                        //恢复
                        IMessageCommon.Current.IgnoreError = false;
                        return false;
                    }
                    break;
                }
            }
            //创建新的备份
            string backupClassId = await HdlBackupLogic.Current.CreatNewBackupNameToDB(LogBackupName);
            if (backupClassId == null)
            {
                //恢复
                IMessageCommon.Current.IgnoreError = false;
                return false;
            }
            //上传Log文件
            bool result = await this.UpLoadBackupFileToDB(backupClassId, 0, upPath, false);
            if (result == true)
            {
                try
                {
                    //删除本地全部Log
                    Global.CreateEmptyDirectory(Path.Combine(DirNameResourse.LocalMemoryDirectory, DirNameResourse.LogDirectory), true);
                }
                catch { }
            }
            //恢复
            IMessageCommon.Current.IgnoreError = false;
            return result;
        }
        #endregion
        #region ■ 读取备份___________________________
        /// <summary>
@@ -491,12 +613,20 @@
        /// <param name="BackupClassId"></param>
        public async Task<bool> LoadAppBackupInfo(string BackupClassId)
        {
            //打开进度条 正在获取备份文件列表
            ProgressFormBar.Current.Start();
            ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uBackupFileListGetting));
            //从云端获取备份的文件,然后存入本地的临时文件夹
            string tempDirectory = await GetBackFileFromDBAndSetToLocation(BackupClassId);
            if (tempDirectory == null)
            {
                //关闭进度条
                ProgressFormBar.Current.Close();
                return false;
            }
            //关闭进度条
            ProgressFormBar.Current.Close();
            //如果读取到的文件完全没有问题,则清理本地的文件
            UserCenterLogic.DeleteAllLocationFile(false);
@@ -511,30 +641,6 @@
            UserCenterLogic.RefreshAllMemory();
            return true;
        }
        /// <summary>
        /// 读取网关备份文档(函数内部有进度条)
        /// </summary>
        /// <param name="BackupClassId"></param>
        /// <param name="zbGateway">网关对象(请传入真实物理网关对象)</param>
        /// <param name="listGatewayFile">网关现有的文件名</param>
        public async Task<bool> LoadGatewayBackupInfo(string BackupClassId, ZigBee.Device.ZbGateway zbGateway, List<string> listGatewayFile)
        {
            //添加附加情报:获取备份数据
            ProgressBar.SetAppendText(Language.StringByID(R.MyInternationalizationString.uGetBackupData));
            //从云端获取备份的文件,然后存入本地的临时文件夹
            string tempDirectory = await GetBackFileFromDBAndSetToLocation(BackupClassId, false, HdlGatewayLogic.Current.GetGatewayId(zbGateway));
            //清空附加情报
            ProgressBar.SetAppendText(string.Empty);
            if (tempDirectory == null)
            {
                return false;
            }
            //恢复网关配置
            return await HdlGatewayLogic.Current.RecoverGatewaySettion(zbGateway, listGatewayFile, tempDirectory);
        }
        #endregion
@@ -584,7 +690,7 @@
        /// <returns></returns>
        private string GetBase64Text(string fileName)
        {
            string path = UserCenterResourse.LocalRootPath;
            string path = Config.Instance.FullPath;
            string fullName = System.IO.Path.Combine(path, fileName);
            FileStream fs = File.OpenRead(fullName);