陈嘉乐
2020-07-10 48ba446936b51fffafa7c3600c0dadc6ac0e8c20
ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlBackupLogic.cs
@@ -35,11 +35,6 @@
            }
        }
        /// <summary>
        /// Log备份的名字
        /// </summary>
        private string LogBackupName = "##Log##";
        #endregion
        #region ■ 获取备份名字列表___________________
@@ -49,9 +44,9 @@
        /// </summary>
        /// <param name="ZigbeeBackupType">0:App手动备份 1: App 自动备份 2:网关手动备份 3:网关自动备份</param>
        /// <param name="ZigbeeUniqueId">网关的唯一Id,获取网关备份需要</param>
        /// <param name="getLogBackup">是否获取Log备份</param>
        /// <param name="getOptionBackup">是否获取功能备份</param>
        /// <returns></returns>
        public async Task<List<BackupListNameInfo>> GetBackupListNameFromDB(int ZigbeeBackupType = 0, string ZigbeeUniqueId = null, bool getLogBackup = false)
        public async Task<List<BackupListNameInfo>> GetBackupListNameFromDB(int ZigbeeBackupType = 0, string ZigbeeUniqueId = null, bool getOptionBackup = false)
        {
            //备份名字很少的内存,可以直接一次性全部取完
            var pra = new GetBackupListPra();
@@ -78,9 +73,9 @@
                {
                    continue;
                }
                if (getLogBackup == false && data2.BackupName == LogBackupName)
                if (getOptionBackup == false && data2.BackupName == DirNameResourse.OptionBackupName)
                {
                    //不获取Log备份
                    //不获取功能备份
                    continue;
                }
                list.Add(data2);
@@ -89,7 +84,7 @@
        }
        /// <summary>
        /// <para>从云端获取备份的文件,然后存入本地指定的临时文件夹(函数内部有进度条)</para>
        /// <para>从云端获取备份的文件,然后存入本地指定的临时文件夹</para>
        /// <para>返回文件夹名字(里面存放着全部的文件),返回null时,代表失败</para>
        /// </summary>
        /// <param name="BackupClassId">BackupClassId</param>
@@ -98,23 +93,25 @@
        /// <returns></returns>
        public async Task<string> GetBackFileFromDBAndSetToLocation(string BackupClassId, bool IsGatewayAutoBackup = false, string ZigbeeUniqueId = null)
        {
            //不允许按系统的返回键
            Shared.Common.CommonPage.BackKeyCanClick = false;
            UserCenterResourse.AccountOption.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.AccountOption.AppCanSignout = true;
                return null;
            }
            //设置最大值
            ProgressBar.SetMaxValue(listFile.Count);
            var pra = new LoadBackInfoPra();
            pra.BackupClassId = BackupClassId;
@@ -124,13 +121,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.AccountOption.AppCanSignout = true;
                    return null;
                }
@@ -140,16 +142,22 @@
                if (result == null)
                {
                    //关闭进度条
                    ProgressBar.Close();
                    ProgressFormBar.Current.Close();
                    //允许按系统的返回键
                    Shared.Common.CommonPage.BackKeyCanClick = true;
                    UserCenterResourse.AccountOption.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.AccountOption.AppCanSignout = true;
            return newDir;
        }
@@ -229,9 +237,9 @@
        /// <param name="BackupClassId">备份主键ID</param>
        /// <param name="ManualBackupType">手动备份类型(0:App手动备份,2:网关手动备份)</param>
        /// <param name="upPath">指定上传的是哪个文件夹的文件(全路径),不指定时,上传的是本地路径下的文件</param>
        /// <param name="setBarMaxValue">是否设置进度条的最大值(这个东西一般不用动它)</param>
        /// <param name="showBar">是否设置显示进度条</param>
        /// <returns></returns>
        public async Task<bool> UpLoadBackupFileToDB(string BackupClassId, int ManualBackupType = 0, string upPath = "", bool setBarMaxValue = true)
        public async Task<bool> UpLoadBackupFileToDB(string BackupClassId, int ManualBackupType = 0, string upPath = "", bool showBar = true)
        {
            List<string> listAllFile = null;
            //文件夹的全路径
@@ -269,21 +277,31 @@
            }
            //开启进度条
            if (setBarMaxValue == true)
            int listFileCount = listAllFile.Count;
            if (showBar == true)
            {
                ProgressBar.SetMaxValue(listAllFile.Count);
                ProgressBar.Show();
                //开启进度条 正在上传备份文件
                ProgressFormBar.Current.Start();
                ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uBackupFileUploading));
            }
            //不允许按系统的返回键
            Shared.Common.CommonPage.BackKeyCanClick = false;
            UserCenterResourse.AccountOption.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.AccountOption.AppCanSignout = true;
                    return false;
                }
                //判断是否是应该上传的文件
@@ -305,20 +323,27 @@
                if (falge == false)
                {
                    //关闭进度条
                    ProgressBar.Close();
                    ProgressFormBar.Current.Close();
                    //允许按系统的返回键
                    Shared.Common.CommonPage.BackKeyCanClick = true;
                    UserCenterResourse.AccountOption.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.AccountOption.AppCanSignout = true;
                    return false;
                }
@@ -330,22 +355,21 @@
                if (result == false)
                {
                    //关闭进度条
                    ProgressBar.Close();
                    ProgressFormBar.Current.Close();
                    //允许按系统的返回键
                    Shared.Common.CommonPage.BackKeyCanClick = true;
                    UserCenterResourse.AccountOption.AppCanSignout = true;
                    return false;
                }
                //设置进度值
                ProgressBar.SetValue(listPic.Count);
            }
            //上传Log备份
            if (upPath != UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.LogDirectory))
            {
                //里面有个回调自身的方法,所以这里加个判断,不然会死循环
                await this.UpLoadLogBackup();
                ProgressFormBar.Current.SetValue(i + 1 + listNormalFile.Count, listFileCount);
            }
            //进度条关闭
            ProgressBar.Close();
            ProgressFormBar.Current.Close();
            //允许按系统的返回键
            Shared.Common.CommonPage.BackKeyCanClick = true;
            UserCenterResourse.AccountOption.AppCanSignout = true;
            return true;
        }
@@ -394,14 +418,11 @@
                nvc.Add("HomeId", Shared.Common.Config.Instance.Home.Id);
                nvc.Add("BackupClassId", BackupClassId);
                nvc.Add("ManualBackupType", ManualBackupType.ToString());
                nvc.Add("IsOtherAccountCtrl", Common.Config.Instance.isAdministrator.ToString());
                nvc.Add("IsOtherAccountCtrl", Common.Config.Instance.Home.IsOthreShare.ToString());
                for (int i = 0; i < listPicFile.Count; i++)
                {
                    string base64 = GetBase64Text(fullName);
                    nvc.Add("UploadSubFileLists[" + i + "].BackupFileName", fileName);
                    nvc.Add("UploadSubFileLists[" + i + "].BackupFileContent", base64);
                }
                string base64 = GetBase64Text(fullName);
                nvc.Add("UploadSubFileLists[0].BackupFileName", fileName);
                nvc.Add("UploadSubFileLists[0].BackupFileContent", base64);
                bool result = UpLoadBigBackupFileToDB("App/UploadHomeAppGatewaySubFiles", fullName, nvc);
                if (result == false)
@@ -425,7 +446,7 @@
            string contentType = "application/octet-stream";
            string requestUrl = string.Empty;
            //启用管理员权限
            if (Config.Instance.isAdministrator == true)
            if (Config.Instance.Home.IsOthreShare == true)
            {
                requestUrl = $"{Config.Instance.AdminRequestBaseUrl}/{RequestName}";
            }
@@ -505,10 +526,10 @@
        #region ■ 上传Log备份________________________
        /// <summary>
        /// 上传Log备份
        /// 上传Log备份(隐匿功能)
        /// </summary>
        /// <returns></returns>
        private async Task<bool> UpLoadLogBackup()
        public async Task<bool> UpLoadLogBackup()
        {
            string upPath = UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.LogDirectory);
            if (HdlAutoBackupLogic.GetFileFromDirectory(upPath).Count == 0)
@@ -517,54 +538,147 @@
                return true;
            }
            ProgressFormBar.Current.Start();
            ProgressFormBar.Current.SetMsg("正在上传Log文件");
            //从云端获取数据
            var pageData = await HdlBackupLogic.Current.GetBackupListNameFromDB(0, null, true);
            if (pageData == null)
            {
                ProgressFormBar.Current.Close();
                return false;
            }
            string backId = string.Empty;
            for (int i = 0; i < pageData.Count; i++)
            {
                if (pageData[i].BackupName == DirNameResourse.OptionBackupName)
                {
                    //获取功能备份的ID
                    backId = pageData[i].Id;
                    break;
                }
            }
            if (backId == string.Empty)
            {
                //创建新的备份
                backId = await HdlBackupLogic.Current.CreatNewBackupNameToDB(DirNameResourse.OptionBackupName);
                if (backId == null)
                {
                    ProgressFormBar.Current.Close();
                    return false;
                }
            }
            //上传Log文件
            bool result = await this.UpLoadBackupFileToDB(backId, 0, upPath, false);
            if (result == true)
            {
                try
                {
                    var listAllFile = HdlAutoBackupLogic.GetFileFromDirectory(upPath);
                    if (listAllFile.Count > 10)
                    {
                        listAllFile.Sort();
                        while (listAllFile.Count >= 10)
                        {
                            System.IO.File.Delete(listAllFile[0]);
                            listAllFile.RemoveAt(0);
                        }
                    }
                }
                catch { }
            }
            ProgressFormBar.Current.Close();
            HdlThreadLogic.Current.RunMain(() =>
            {
                var contr = new ShowMsgControl(ShowMsgType.Tip, "Log上传成功");
                contr.Show();
            });
            return result;
        }
        #endregion
        #region ■ 读取隐匿功能配置___________________
        /// <summary>
        /// 读取隐匿功能配置(不要在意返回值)
        /// </summary>
        /// <returns></returns>
        public async Task<bool> LoadHideOption()
        {
            //先初始化
            UserCenterResourse.HideOption = new HideOptionInfo();
            if (UserCenterResourse.UserInfo.AuthorityNo != 1 && UserCenterResourse.UserInfo.AuthorityNo != 2)
            {
                return true;
            }
            //无视错误
            IMessageCommon.Current.IgnoreError = true;
            //从云端获取数据
            var pageData = await HdlBackupLogic.Current.GetBackupListNameFromDB(0, null, true);
            var pageData = await this.GetBackupListNameFromDB(0, null, true);
            if (pageData == null)
            {
                //恢复
                IMessageCommon.Current.IgnoreError = false;
                return false;
            }
            string backId = string.Empty;
            for (int i = 0; i < pageData.Count; i++)
            {
                if (pageData[i].BackupName == LogBackupName)
                if (pageData[i].BackupName == DirNameResourse.OptionBackupName)
                {
                    //删除掉这个备份
                    bool flage = await this.DeleteDbBackupData(pageData[i].Id);
                    if (flage == false)
                    {
                        //恢复
                        IMessageCommon.Current.IgnoreError = false;
                        return false;
                    }
                    //获取功能备份的ID
                    backId = pageData[i].Id;
                    break;
                }
            }
            //创建新的备份
            string backupClassId = await HdlBackupLogic.Current.CreatNewBackupNameToDB(LogBackupName);
            if (backupClassId == null)
            if (backId == string.Empty)
            {
                //没有功能配置
                IMessageCommon.Current.IgnoreError = false;
                return true;
            }
            //获取这个备份下面有多少个文件
            List<string> listFile = await GetBackFileIDFromDB(backId, false, null);
            if (listFile == null)
            {
                //恢复
                IMessageCommon.Current.IgnoreError = false;
                return false;
            }
            //上传Log文件
            bool result = await this.UpLoadBackupFileToDB(backupClassId, 0, upPath, false);
            if (result == true)
            if (listFile.Count == 0)
            {
                try
                {
                    //删除本地全部Log
                    Global.CreateEmptyDirectory(Path.Combine(DirNameResourse.LocalMemoryDirectory, DirNameResourse.LogDirectory), true);
                }
                catch { }
                //恢复
                IMessageCommon.Current.IgnoreError = false;
                return true;
            }
            //检测
            string checkKeys = UserCenterLogic.EncryptPassword(UserCenterResourse.FileEncryptKey, DirNameResourse.ShowOptionMenuFile + UserCenterResourse.UserInfo.Account);
            if (listFile.Contains(checkKeys) == true)
            {
                //显示主页隐藏菜单(Debug用)
                UserCenterResourse.HideOption.CenterHideMenu = 1;
            }
            checkKeys = UserCenterLogic.EncryptPassword(UserCenterResourse.FileEncryptKey, DirNameResourse.DetailedLogFile + UserCenterResourse.UserInfo.Account);
            if (listFile.Contains(checkKeys) == true)
            {
                //出力详细Log(Debug用)
                UserCenterResourse.HideOption.DetailedLog = 1;
            }
            checkKeys = UserCenterLogic.EncryptPassword(UserCenterResourse.FileEncryptKey, DirNameResourse.DeviceHistoryFile + UserCenterResourse.UserInfo.Account);
            if (listFile.Contains(checkKeys) == true)
            {
                //显示设备历史版本(Debug用)
                UserCenterResourse.HideOption.DeviceHistory = 1;
            }
            //恢复
            IMessageCommon.Current.IgnoreError = false;
            return result;
            return true;
        }
        #endregion
@@ -577,12 +691,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);
@@ -597,30 +719,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
@@ -670,7 +768,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);