New file |
| | |
| | | using Newtonsoft.Json; |
| | | using Shared.Common; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | |
| | | namespace Shared.Phone.UserCenter |
| | | { |
| | | /// <summary> |
| | | /// 自动备份的逻辑 |
| | | /// </summary> |
| | | public class HdlAutoBackupLogic |
| | | { |
| | | #region ■ 变量声明___________________________ |
| | | |
| | | #endregion |
| | | |
| | | #region ■ 上传备份___________________________ |
| | | |
| | | /// <summary> |
| | | /// 手动执行上传自动备份数据(0:没有可上传的自动备份数据 1:成功 -1:失败) |
| | | /// </summary> |
| | | /// <returns></returns> |
| | | public static int DoUpLoadAutoBackupDataByHand() |
| | | { |
| | | //编辑文件 |
| | | List<string> listEditor = GetAutoBackupEditorFile(); |
| | | //删除文件 |
| | | List<string> listDelete = GetAutoBackupDeleteFile(); |
| | | //没有数据 |
| | | if (listEditor.Count == 0 && listDelete.Count == 0) |
| | | { |
| | | return 0; |
| | | } |
| | | |
| | | //开启进度条 正在上传备份文件 |
| | | ProgressFormBar.Current.Start(); |
| | | ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uBackupFileUploading)); |
| | | |
| | | //上传文件到云端 |
| | | bool result = UpLoadBackupFileToDB(listEditor); |
| | | if (result == false) |
| | | { |
| | | ProgressFormBar.Current.Close(); |
| | | return -1; |
| | | } |
| | | |
| | | //删除文件 |
| | | result = DoDeleteFileFromDB(listDelete); |
| | | if (result == false) |
| | | { |
| | | ProgressFormBar.Current.Close(); |
| | | return -1; |
| | | } |
| | | |
| | | ProgressFormBar.Current.Close(); |
| | | |
| | | return 1; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 上传文件到云端 |
| | | /// </summary> |
| | | /// <param name="listFile">编辑或者添加的文件(目前已经不是上传它了)</param> |
| | | /// <returns></returns> |
| | | private static bool UpLoadBackupFileToDB(List<string> listFile) |
| | | { |
| | | string localPath = Config.Instance.FullPath; |
| | | |
| | | //将模板数据保存到到指定的文件夹中 |
| | | var templateName = TemplateData.TemplateCommonLogic.Current.GetNewTemplateFileName(new DateTime(2000, 12, 31, 12, 59, 57)); |
| | | var templateFile = TemplateData.TemplateCommonLogic.Current.SaveTemplateDataToFile(templateName, "HomeTemplate"); |
| | | |
| | | //将模板bin文件移动到备份文件夹中 |
| | | var localTemplateName = System.IO.Path.Combine(localPath, templateName); |
| | | try { System.IO.File.Move(templateFile, localTemplateName); } |
| | | catch (Exception ex) { HdlLogLogic.Current.WriteLog(ex, "移动模板失败"); } |
| | | |
| | | //获取本地文件 |
| | | var listAllFile = HdlFileLogic.Current.GetRootPathListFile(); |
| | | var listUpFile = new List<string>(); |
| | | foreach (string fileName in listAllFile) |
| | | { |
| | | //判断指定文件是否需要上传(根目录的才判断) |
| | | if (HdlBackupLogic.Current.IsNotUpLoadFile(fileName) == true) |
| | | { |
| | | continue; |
| | | } |
| | | //其他图片的资源文件,只有在变更了的时候,才上传 |
| | | if (fileName.EndsWith(".png") == true && listFile.Contains(fileName) == false) |
| | | { |
| | | continue; |
| | | } |
| | | listUpFile.Add(fileName); |
| | | } |
| | | |
| | | int listFileCount = listUpFile.Count; |
| | | for (int i = 0; i < listUpFile.Count; i++) |
| | | { |
| | | string file = listUpFile[i]; |
| | | var datainfo = new FileInfoData(); |
| | | datainfo.BackupFileName = file; |
| | | datainfo.BackupFileContent = HdlFileLogic.Current.ReadFileByteContent(System.IO.Path.Combine(localPath, file)); |
| | | if (datainfo.BackupFileContent == null) |
| | | { |
| | | continue; |
| | | } |
| | | |
| | | var upData = new UpLoadDataPra(); |
| | | upData.HomeId = Config.Instance.Home.Id; |
| | | upData.UploadSubFileLists = new List<FileInfoData> { datainfo }; |
| | | //获取控制主人账号的Token |
| | | upData.LoginAccessToken = UserCenterLogic.GetConnectMainToken(); |
| | | |
| | | //执行是上传 |
| | | bool falge = UserCenterLogic.GetResultStatuByRequestHttps("App/HomeAppAutoDataBackup", true, upData, null, true); |
| | | if (falge == false) |
| | | { |
| | | return false; |
| | | } |
| | | //设置进度值 |
| | | ProgressFormBar.Current.SetValue(i + 1, listFileCount); |
| | | } |
| | | |
| | | //删除掉这个模板文件 |
| | | HdlFileLogic.Current.DeleteFile(localTemplateName); |
| | | |
| | | //删除文件 |
| | | var backPath = DirNameResourse.AutoBackupDirectory; |
| | | foreach (var file in listFile) |
| | | { |
| | | string fullName = System.IO.Path.Combine(backPath, file); |
| | | HdlFileLogic.Current.DeleteFile(fullName); |
| | | } |
| | | |
| | | return true; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 云端执行删除指定文件 |
| | | /// </summary> |
| | | /// <param name="listData">删除的文件</param> |
| | | /// <returns></returns> |
| | | private static bool DoDeleteFileFromDB(List<string> listData) |
| | | { |
| | | if (listData.Count == 0) |
| | | { |
| | | return true; |
| | | } |
| | | |
| | | //获取app的自动备份 |
| | | var data = HdlBackupLogic.Current.GetBackupListNameFromDB(1); |
| | | if (data == null || data.Count == 0) |
| | | { |
| | | return true; |
| | | } |
| | | //自动备份只有一个 |
| | | var autoBackupId = data[0].Id; |
| | | |
| | | var upData = new DeleteFilePra(); |
| | | upData.BackupClassId = autoBackupId; |
| | | upData.HomeId = Config.Instance.Home.Id; |
| | | upData.DeleteFileNameLists = listData; |
| | | //获取控制主人账号的Token |
| | | upData.LoginAccessToken = UserCenterLogic.GetConnectMainToken(); |
| | | |
| | | bool falge = UserCenterLogic.GetResultStatuByRequestHttps("App/DeleteAppBackupFile", true, upData); |
| | | if (falge == false) |
| | | { |
| | | return false; |
| | | } |
| | | |
| | | //删除文件 |
| | | var backPath = DirNameResourse.AutoBackupdeleteDirectory; |
| | | foreach (var file in listData) |
| | | { |
| | | string fullName = System.IO.Path.Combine(backPath, file); |
| | | HdlFileLogic.Current.DeleteFile(fullName); |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | #region ■ 获取文件___________________________ |
| | | |
| | | /// <summary> |
| | | /// 获取自动备份目录下的添加或者编辑的文件 |
| | | /// </summary> |
| | | /// <returns></returns> |
| | | public static List<string> GetAutoBackupEditorFile() |
| | | { |
| | | return HdlFileLogic.Current.GetFileFromDirectory(DirNameResourse.AutoBackupDirectory); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取自动备份目录下的删除的文件 |
| | | /// </summary> |
| | | /// <returns></returns> |
| | | public static List<string> GetAutoBackupDeleteFile() |
| | | { |
| | | return HdlFileLogic.Current.GetFileFromDirectory(DirNameResourse.AutoBackupdeleteDirectory); |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | #region ■ 设置文件状态_______________________ |
| | | |
| | | /// <summary> |
| | | /// 添加或者修改文件 |
| | | /// </summary> |
| | | /// <param name="fileName">文件的名字,不含路径</param> |
| | | public static void AddOrEditorFile(string fileName) |
| | | { |
| | | fileName = System.IO.Path.GetFileName(fileName); |
| | | //自动备份目录 |
| | | string strBackPath = DirNameResourse.AutoBackupDirectory; |
| | | if (System.IO.Directory.Exists(strBackPath) == false) |
| | | { |
| | | //预创建个人中心全部的文件夹 |
| | | HdlFileLogic.Current.CreatAllUserCenterDirectory(); |
| | | } |
| | | |
| | | //自动删除备份目录 |
| | | string strdelBackPath = DirNameResourse.AutoBackupdeleteDirectory; |
| | | //如果删除列表里面有这个东西的话,移除掉 |
| | | string delFile = System.IO.Path.Combine(strdelBackPath, fileName); |
| | | HdlFileLogic.Current.DeleteFile(delFile); |
| | | |
| | | string soureFile = System.IO.Path.Combine(Common.Config.Instance.FullPath, fileName); |
| | | string newFile = System.IO.Path.Combine(strBackPath, fileName); |
| | | |
| | | //原原本本的复制文件到指定文件夹 |
| | | HdlFileLogic.Current.CopyFile(soureFile, newFile); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 删除文件 |
| | | /// </summary> |
| | | /// <param name="fileName">文件的名字,不含路径</param> |
| | | public static void DeleteFile(string fileName) |
| | | { |
| | | fileName = System.IO.Path.GetFileName(fileName); |
| | | //自动删除备份目录 |
| | | string strBackPath = DirNameResourse.AutoBackupdeleteDirectory; |
| | | string newFile = System.IO.Path.Combine(strBackPath, fileName); |
| | | |
| | | //创建一个空文件 |
| | | var file = System.IO.File.Create(newFile); |
| | | file.Close(); |
| | | |
| | | //自动备份目录 |
| | | strBackPath = DirNameResourse.AutoBackupDirectory; |
| | | //如果备份列表里面有这个东西的话,移除掉 |
| | | string delFile = System.IO.Path.Combine(strBackPath, fileName); |
| | | |
| | | HdlFileLogic.Current.DeleteFile(delFile); |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | #region ■ 同步数据___________________________ |
| | | |
| | | /// <summary> |
| | | /// 同步云端数据(仅限APP启动之后) -1:异常 0:已经同步过,不需要同步 1:正常同步 2:没有自动备份数据 |
| | | /// </summary> |
| | | /// <returns></returns> |
| | | public static int SynchronizeDbAutoBackupData() |
| | | { |
| | | //暂时不支持成员 |
| | | if (UserCenterResourse.UserInfo.AuthorityNo == 3) |
| | | { |
| | | //同步服务器的分享内容 |
| | | HdlShardLogic.Current.SynchronizeDbSharedContent(); |
| | | return 1; |
| | | } |
| | | //判断是否能够同步数据 |
| | | string checkFile = DirNameResourse.AutoDownLoadBackupCheckFile; |
| | | //如果本地已经拥有了这个文件,则说明不是新手机,不再自动还原 |
| | | if (System.IO.File.Exists(checkFile) == true) |
| | | { |
| | | return 0; |
| | | } |
| | | |
| | | //获取app的自动备份 |
| | | var data = HdlBackupLogic.Current.GetBackupListNameFromDB(1); |
| | | if (data == null) |
| | | { |
| | | return -1; |
| | | } |
| | | if (data.Count == 0) |
| | | { |
| | | //创建一个空文件(标识已经完成同步) |
| | | var file = System.IO.File.Create(checkFile); |
| | | file.Close(); |
| | | return 2; |
| | | } |
| | | //自动备份只有一个 |
| | | string backId = data[0].Id; |
| | | |
| | | //账号数据同步中 |
| | | ProgressFormBar.Current.Start(); |
| | | ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uAccountDataIsSynchronizing)); |
| | | |
| | | //从云端获取备份的文件,然后存入本地指定的临时文件夹 |
| | | string tempDir = HdlBackupLogic.Current.GetBackFileFromDBAndSetToLocation(backId); |
| | | if (tempDir == null) |
| | | { |
| | | //删除检测文件 |
| | | System.IO.File.Delete(checkFile); |
| | | //关闭进度条 |
| | | ProgressFormBar.Current.Close(); |
| | | //同步失败 |
| | | return -1; |
| | | } |
| | | //如果读取到的文件完全没有问题,则清理本地的文件 |
| | | HdlFileLogic.Current.DeleteAllLocationFile(false); |
| | | |
| | | //没有错误的话,则移动到当前住宅文件夹下面 |
| | | HdlFileLogic.Current.MoveDirectoryFileToHomeDirectory(tempDir, true); |
| | | |
| | | //创建一个空文件(标识已经完成同步) |
| | | var file2 = System.IO.File.Create(checkFile); |
| | | file2.Close(); |
| | | |
| | | //重新刷新住宅对象 |
| | | UserCenterLogic.RefreshHomeObject(); |
| | | //根据模板文件,恢复数据 |
| | | TemplateData.TemplateCommonLogic.Current.RecoverDataByTemplateBinFile(); |
| | | //强制生成设备和网关文件 |
| | | TemplateData.TemplateCommonLogic.Current.CreatDeviceAndGatewayFileFromMemoryByForce(); |
| | | |
| | | //关闭进度条 |
| | | ProgressFormBar.Current.Close(); |
| | | |
| | | return 1; |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | #region ■ 一般方法___________________________ |
| | | |
| | | /// <summary> |
| | | /// 删除全部的自动备份的本地文件(此函数用于读取自动备份的时候使用) |
| | | /// </summary> |
| | | public static void DeleteAllAutoBackupFile() |
| | | { |
| | | //清空自动备份【文件夹】(编辑,追加) |
| | | string dirPath = DirNameResourse.AutoBackupDirectory; |
| | | HdlFileLogic.Current.DeleteDirectory(dirPath); |
| | | HdlFileLogic.Current.CreateDirectory(dirPath, true); |
| | | |
| | | //清空自动备份【文件夹】(删除) |
| | | dirPath = DirNameResourse.AutoBackupdeleteDirectory; |
| | | HdlFileLogic.Current.DeleteDirectory(dirPath); |
| | | HdlFileLogic.Current.CreateDirectory(dirPath, true); |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | #region ■ 备份提醒___________________________ |
| | | |
| | | /// <summary> |
| | | /// 保存备份提醒设置到本地 |
| | | /// </summary> |
| | | /// <param name="notPrompted">不再提示</param> |
| | | /// <param name="day"></param> |
| | | public static void SaveBackupNotPrompted(bool notPrompted, int day = -1) |
| | | { |
| | | //文件全路径 |
| | | string fullName = DirNameResourse.AutoBackupNotPromptedFile; |
| | | BackupNotPrompted info = null; |
| | | if (System.IO.File.Exists(fullName) == true) |
| | | { |
| | | var data = HdlFileLogic.Current.ReadFileByteContent(fullName); |
| | | info = JsonConvert.DeserializeObject<BackupNotPrompted>(System.Text.Encoding.UTF8.GetString(data)); |
| | | } |
| | | if (info == null) |
| | | { |
| | | info = new BackupNotPrompted(); |
| | | } |
| | | |
| | | info.NotPrompted = notPrompted; |
| | | if (day != -1) |
| | | { |
| | | info.OldDay = DateTime.Now.ToString("yyyy-MM-dd"); |
| | | info.Day = day; |
| | | } |
| | | //保存 |
| | | HdlFileLogic.Current.SaveFileContent(fullName, info); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 显示自动备份的界面 |
| | | /// </summary> |
| | | public static void ShowAutoBackupPromptedForm() |
| | | { |
| | | if (UserCenterResourse.UserInfo.AuthorityNo == 3) |
| | | { |
| | | //暂不支持成员 |
| | | return; |
| | | } |
| | | |
| | | //判断是否有文件变更了 |
| | | if (CheckAutoBackupFileIsChanged() == false) |
| | | { |
| | | return; |
| | | } |
| | | //判断能否显示自动备份的界面 |
| | | if (CheckCanShowAutoBackupForm() == true) |
| | | { |
| | | HdlThreadLogic.Current.RunMain(() => |
| | | { |
| | | var form = new HdlBackup.HdlAutoBackupForm(); |
| | | form.AddForm(); |
| | | }); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 检测自动备份文件是否变更过 |
| | | /// </summary> |
| | | /// <returns></returns> |
| | | private static bool CheckAutoBackupFileIsChanged() |
| | | { |
| | | List<string> listFile1 = HdlFileLogic.Current.GetFileFromDirectory(DirNameResourse.AutoBackupDirectory); |
| | | List<string> listFile2 = GetAutoBackupDeleteFile(); |
| | | |
| | | if (listFile1.Count == 0 && listFile2.Count == 0) |
| | | { |
| | | //没有文件变更 |
| | | return false; |
| | | } |
| | | if (listFile2.Count > 0) |
| | | { |
| | | //有文件被删除 |
| | | return true; |
| | | } |
| | | foreach (var fileName in listFile1) |
| | | { |
| | | //住宅和收藏文件,不作为判断标准 |
| | | if (fileName.StartsWith("House_") == true |
| | | || fileName == "Room_Favorite.json") |
| | | { |
| | | continue; |
| | | } |
| | | return true; |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 检测能否显示自动备份的界面 |
| | | /// </summary> |
| | | /// <returns></returns> |
| | | private static bool CheckCanShowAutoBackupForm() |
| | | { |
| | | //文件全路径 |
| | | string fullName = DirNameResourse.AutoBackupNotPromptedFile; |
| | | if (System.IO.File.Exists(fullName) == false) |
| | | { |
| | | //本地没有存在这个文件 |
| | | return true; |
| | | } |
| | | BackupNotPrompted info = null; |
| | | var data = HdlFileLogic.Current.ReadFileByteContent(fullName); |
| | | info = JsonConvert.DeserializeObject<BackupNotPrompted>(System.Text.Encoding.UTF8.GetString(data)); |
| | | if (info.NotPrompted == true) |
| | | { |
| | | //不再提示 |
| | | return false; |
| | | } |
| | | if (info.Day == 0) |
| | | { |
| | | return true; |
| | | } |
| | | |
| | | DateTime oldTime = Convert.ToDateTime(info.OldDay); |
| | | int intDay = (DateTime.Now - oldTime).Days; |
| | | //时间已经超过 |
| | | if (intDay >= info.Day) |
| | | { |
| | | return true; |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | #region ■ 数据结构___________________________ |
| | | |
| | | /// <summary> |
| | | /// 自动备份不需要再次提醒的结构体 |
| | | /// </summary> |
| | | private class BackupNotPrompted |
| | | { |
| | | /// <summary> |
| | | /// 不再提示 |
| | | /// </summary> |
| | | public bool NotPrompted = false; |
| | | /// <summary> |
| | | /// 前一次的日期:2019-01-01(格式) |
| | | /// </summary> |
| | | public string OldDay = string.Empty; |
| | | /// <summary> |
| | | /// 相隔日期天数 |
| | | /// </summary> |
| | | public int Day = 0; |
| | | } |
| | | |
| | | #endregion |
| | | } |
| | | } |