黄学彪
2020-09-18 c7df85937f73fb347ee0b19e9c052d2d00a6df6c
ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlBackupLogic.cs
@@ -96,8 +96,8 @@
            UserCenterResourse.AccountOption.AppCanSignout = false;
            //首先先创建一个临时文件夹,存在文件则清空
            string newDir = System.IO.Path.Combine(DirNameResourse.LocalMemoryDirectory, DirNameResourse.DownLoadBackupTempDirectory);
            Global.CreateEmptyDirectory(newDir, true);
            string newDir = DirNameResourse.DownLoadBackupTempDirectory;
            HdlFileLogic.Current.CreateDirectory(newDir, true);
            //获取这个备份下面有多少个文件
            List<string> listFile = GetBackFileIDFromDB(BackupClassId, IsGatewayAutoBackup, ZigbeeUniqueId);
@@ -147,12 +147,11 @@
                    return null;
                }
                //将输入写入本地的临时文件夹
                Global.WriteFileToDirectoryByBytes(newDir, fileName, result);
                HdlFileLogic.Current.SaveByteToFile(System.IO.Path.Combine(newDir, fileName), result);
                //设置进度值
                ProgressFormBar.Current.SetValue(i + 1, listFileCount);
            }
            //关闭进度条
            ProgressFormBar.Current.Close();
            //允许按系统的返回键
            Shared.Common.CommonPage.BackKeyCanClick = true;
            UserCenterResourse.AccountOption.AppCanSignout = true;
@@ -242,15 +241,25 @@
            List<string> listAllFile = null;
            //文件夹的全路径
            string fullDir = string.Empty;
            string localTemplateName = string.Empty;
            if (upPath == string.Empty)
            {
                //将模板数据保存到到指定的文件夹中
                var templateName = TemplateData.TemplateCommonLogic.Current.GetNewTemplateFileName();
                var templateFile = TemplateData.TemplateCommonLogic.Current.SaveTemplateDataToFile(templateName, "HomeTemplate");
                //将模板bin文件移动到备份文件夹中
                localTemplateName = System.IO.Path.Combine(Config.Instance.FullPath, templateName);
                try { System.IO.File.Move(templateFile, localTemplateName); }
                catch (Exception ex) { HdlLogLogic.Current.WriteLog(ex, "移动模板失败"); }
                //获取本地文件
                listAllFile = Global.FileListByHomeId();
                fullDir = UserCenterLogic.CombinePath();
                listAllFile = HdlFileLogic.Current.GetRootPathListFile();
                fullDir = Common.Config.Instance.FullPath;
            }
            else
            {
                listAllFile = HdlAutoBackupLogic.GetFileFromDirectory(upPath);
                listAllFile = HdlFileLogic.Current.GetFileFromDirectory(upPath);
                fullDir = upPath;
            }
            if (listAllFile.Count == 0)
@@ -265,7 +274,13 @@
            foreach (string fileName in listAllFile)
            {
                if (fileName.EndsWith(".png") == true)
                //判断指定文件是否需要上传(根目录的才判断)
                if (upPath == string.Empty && this.IsNotUpLoadFile(fileName) == true)
                {
                    continue;
                }
                if (fileName.EndsWith(".png") == true
                    || fileName.EndsWith(".bin") == true)
                {
                    //图片文件
                    listPicFile.Add(fileName);
@@ -275,7 +290,7 @@
            }
            //开启进度条
            int listFileCount = listAllFile.Count;
            int listFileCount = listPicFile.Count + listNormalFile.Count;
            if (showBar == true)
            {
                //开启进度条 正在上传备份文件
@@ -300,18 +315,18 @@
                    //允许按系统的返回键
                    Shared.Common.CommonPage.BackKeyCanClick = true;
                    UserCenterResourse.AccountOption.AppCanSignout = true;
                    if (localTemplateName != string.Empty)
                    {
                        //删除掉这个模板文件
                        HdlFileLogic.Current.DeleteFile(localTemplateName);
                    }
                    return false;
                }
                //判断是否是应该上传的文件
                if (IsNotUpLoadFile(file) == true && upPath == string.Empty)
                {
                    continue;
                }
                //整理数据
                var datainfo = new FileInfoData();
                datainfo.BackupFileName = file;
                string fullFileName = System.IO.Path.Combine(fullDir, file);
                datainfo.BackupFileContent = Shared.IO.FileUtils.ReadFile(fullFileName);
                datainfo.BackupFileContent = HdlFileLogic.Current.ReadFileByteContent(fullFileName);
                var list = new List<FileInfoData>();
                list.Add(datainfo);
@@ -325,6 +340,11 @@
                    //允许按系统的返回键
                    Shared.Common.CommonPage.BackKeyCanClick = true;
                    UserCenterResourse.AccountOption.AppCanSignout = true;
                    if (localTemplateName != string.Empty)
                    {
                        //删除掉这个模板文件
                        HdlFileLogic.Current.DeleteFile(localTemplateName);
                    }
                    return false;
                }
                //设置进度值
@@ -342,6 +362,11 @@
                    //允许按系统的返回键
                    Shared.Common.CommonPage.BackKeyCanClick = true;
                    UserCenterResourse.AccountOption.AppCanSignout = true;
                    if (localTemplateName != string.Empty)
                    {
                        //删除掉这个模板文件
                        HdlFileLogic.Current.DeleteFile(localTemplateName);
                    }
                    return false;
                }
@@ -357,10 +382,21 @@
                    //允许按系统的返回键
                    Shared.Common.CommonPage.BackKeyCanClick = true;
                    UserCenterResourse.AccountOption.AppCanSignout = true;
                    if (localTemplateName != string.Empty)
                    {
                        //删除掉这个模板文件
                        HdlFileLogic.Current.DeleteFile(localTemplateName);
                    }
                    return false;
                }
                //设置进度值
                ProgressFormBar.Current.SetValue(i + 1 + listNormalFile.Count, listFileCount);
            }
            if (localTemplateName != string.Empty)
            {
                //删除掉这个模板文件
                HdlFileLogic.Current.DeleteFile(localTemplateName);
            }
            //进度条关闭
@@ -529,8 +565,8 @@
        /// <returns></returns>
        public bool UpLoadLogBackup()
        {
            string upPath = UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.LogDirectory);
            if (HdlAutoBackupLogic.GetFileFromDirectory(upPath).Count == 0)
            string upPath = DirNameResourse.LogDirectory;
            if (HdlFileLogic.Current.GetFileFromDirectory(upPath).Count == 0)
            {
                //没有Log文件
                return true;
@@ -572,7 +608,7 @@
            {
                try
                {
                    var listAllFile = HdlAutoBackupLogic.GetFileFromDirectory(upPath);
                    var listAllFile = HdlFileLogic.Current.GetFileFromDirectory(upPath);
                    if (listAllFile.Count > 10)
                    {
                        listAllFile.Sort();
@@ -594,6 +630,85 @@
            });
            return result;
        }
        /// <summary>
        /// 上传东西到隐匿功能备份
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="byteData"></param>
        /// <returns></returns>
        public bool UpLoadByteDataToOptionBackup(string fileName, byte[] byteData)
        {
            ProgressFormBar.Current.Start();
            ProgressFormBar.Current.SetMsg("正在上传文件");
            //从云端获取数据
            var pageData = HdlBackupLogic.Current.GetBackupListNameFromDB(0, null, true);
            if (pageData == null)
            {
                ProgressFormBar.Current.Close();
                HdlThreadLogic.Current.RunMain(() =>
                {
                    var contr = new ShowMsgControl(ShowMsgType.Tip, "获取功能备份失败");
                    contr.Show();
                });
                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 = this.CreatNewBackupNameToDB(DirNameResourse.OptionBackupName);
                if (backId == null)
                {
                    ProgressFormBar.Current.Close();
                    HdlThreadLogic.Current.RunMain(() =>
                    {
                        var contr = new ShowMsgControl(ShowMsgType.Tip, "创建功能备份失败");
                        contr.Show();
                    });
                    return false;
                }
            }
            //上传Log文件
            var datainfo = new FileInfoData();
            datainfo.BackupFileName = fileName;
            datainfo.BackupFileContent = byteData;
            var list = new List<FileInfoData>();
            list.Add(datainfo);
            //执行上传
            bool falge = DoUpLoadInfoToDB(backId, list, 0);
            //关闭进度条
            ProgressFormBar.Current.Close();
            if (falge == false)
            {
                HdlThreadLogic.Current.RunMain(() =>
                {
                    var contr = new ShowMsgControl(ShowMsgType.Tip, "文件上传成功");
                    contr.Show();
                });
                return false;
            }
            HdlThreadLogic.Current.RunMain(() =>
            {
                var contr = new ShowMsgControl(ShowMsgType.Tip, "文件上传成功");
                contr.Show();
            });
            return true;
        }
        #endregion
@@ -673,6 +788,18 @@
                //显示设备历史版本(Debug用)
                UserCenterResourse.HideOption.DeviceHistory = 1;
            }
            checkKeys = UserCenterLogic.EncryptPassword(UserCenterResourse.FileEncryptKey, DirNameResourse.StartDebugAppFile + UserCenterResourse.UserInfo.Account);
            if (listFile.Contains(checkKeys) == true)
            {
                //开启后台调试App功能(Debug用)
                UserCenterResourse.HideOption.StartDebugApp = 1;
            }
            checkKeys = UserCenterLogic.EncryptPassword(UserCenterResourse.FileEncryptKey, DirNameResourse.CheckDeviceTypeFile + UserCenterResourse.UserInfo.Account);
            if (listFile.Contains(checkKeys) == true)
            {
                //开启检测设备Type的(Debug用)
                UserCenterResourse.HideOption.CheckDeviceType = 1;
            }
            //恢复
            IMessageCommon.Current.IgnoreError = false;
@@ -701,20 +828,28 @@
                ProgressFormBar.Current.Close();
                return false;
            }
            //关闭进度条
            ProgressFormBar.Current.Close();
            //清空全部房间
            HdlRoomLogic.Current.DeleteAllRoom();
            //清空本地全部的场景数据
            HdlSceneLogic.Current.DeleteAllLocalScene();
            //如果读取到的文件完全没有问题,则清理本地的文件
            UserCenterLogic.DeleteAllLocationFile(false);
            HdlFileLogic.Current.DeleteAllLocationFile(false);
            //清理本地的模板文件
            TemplateData.TemplateCommonLogic.Current.DeleteAllLocalFile();
            //没有错误的话,则移动到当前住宅文件夹下面
            Global.MoveDirectoryFileToHomeDirectory(tempDirectory, true);
            HdlFileLogic.Current.MoveDirectoryFileToHomeDirectory(tempDirectory, true);
            //删除全部的自动备份的本地文件(此函数用于读取自动备份的时候使用)
            HdlAutoBackupLogic.DeleteAllAutoBackupFile();
            //刷新本地缓存
            UserCenterLogic.RefreshAllMemory();
            //关闭进度条
            ProgressFormBar.Current.Close();
            return true;
        }
@@ -749,11 +884,14 @@
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public bool IsNotUpLoadFile(string file)
        private bool IsNotUpLoadFile(string file)
        {
            if (file == "Config.json")
            if (file.StartsWith("Device_") == true
               || file.StartsWith("Gateway_") == true
               || file.StartsWith("Room_") == true
               || file.StartsWith("Scene_") == true)
            {
                //不能覆盖Config文件
                //设备,网关,房间,场景文件不需要上传,它已经保存在bin模板文件中
                return true;
            }
            return false;