using Newtonsoft.Json;
using Shared.Common;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Text;
namespace Shared.Phone
{
///
/// 备份业务的逻辑
///
public class HdlBackupLogic
{
#region ■ 变量声明___________________________
///
/// 备份业务的逻辑
///
private static HdlBackupLogic m_Current = null;
///
/// 备份业务的逻辑
///
public static HdlBackupLogic Current
{
get
{
if (m_Current == null)
{
m_Current = new HdlBackupLogic();
}
return m_Current;
}
}
#endregion
#region ■ 获取备份名字列表___________________
///
/// 从云端获取备份数据的名字列表
///
/// 备份类型(1:手动备份,2:自动备份)
/// 失败时是否显示tip消息
/// 是否获取功能备份
///
public List GetBackupListNameFromDB(BackUpMode backupDiv, ShowNetCodeMode mode = ShowNetCodeMode.YES, bool getOptionBackup = false)
{
var pra = new { homeId = Config.Instance.Home.Id };
var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/backup/folder/findAll", RestSharp.Method.POST, pra, null, null, CheckMode.A账号权限);
//检测状态码
if (HdlCheckLogic.Current.CheckNetCode(result, mode) == false)
{
return null;
}
//获取备份类型的字符标识
string strdiv = this.GetBackUpModeText(backupDiv);
var listData = Newtonsoft.Json.JsonConvert.DeserializeObject>(result.Data.ToString());
var list = new List();
foreach (var data2 in listData)
{
if (getOptionBackup == false && data2.FolderName == HdlFileNameResourse.OptionBackupName)
{
//不获取功能备份
continue;
}
if (data2.BackupClassify != strdiv || data2.BackupDataType != "ZIGBEE_HOME")
{
//不是指定的备份类型,或者zigbee的备份,则不获取
continue;
}
list.Add(data2);
}
return list;
}
///
/// 从云端获取备份的文件,然后存入本地指定的临时文件夹
/// 返回文件夹名字(里面存放着全部的文件),返回null时,代表失败
///
/// 备份主键
/// 失败时是否显示tip消息
///
public string GetBackFileFromDBAndSetToLocation(string i_folderId, ShowNetCodeMode mode = ShowNetCodeMode.YES)
{
//不允许按系统的返回键
Config.Instance.BackKeyCanClick = false;
HdlUserCenterResourse.AccountOption.AppCanSignout = false;
//首先先创建一个临时文件夹,存在文件则清空
string newDir = HdlFileNameResourse.DownLoadBackupTempDirectory;
HdlFileLogic.Current.CreateDirectory(newDir, true);
//获取这个备份下面有多少个文件
var dicFile = GetBackFileIDFromDB(i_folderId, mode);
if (dicFile == null)
{
//关闭进度条
ProgressFormBar.Current.Close();
//允许按系统的返回键
Config.Instance.BackKeyCanClick = true;
HdlUserCenterResourse.AccountOption.AppCanSignout = true;
return null;
}
//一个个的下载文件
int listFileCount = dicFile.Count;
int fileCount = 0;
foreach (string fileName in dicFile.Keys)
{
fileCount++;
//账号已经退出
if (HdlCheckLogic.Current.IsAccountLoginOut() == true)
{
//关闭进度条
ProgressFormBar.Current.Close();
//允许按系统的返回键
Config.Instance.BackKeyCanClick = true;
HdlUserCenterResourse.AccountOption.AppCanSignout = true;
return null;
}
//★设置需要获取的文件名字★
var pra = new { fileId = dicFile[fileName], folderId = i_folderId, homeId = Config.Instance.Home.Id };
var result = HdlHttpLogic.Current.RequestByteFromZigbeeHttps("home-wisdom/backup/file/downOne", RestSharp.Method.POST, pra, null, null, CheckMode.A账号权限, true, 10);
//检测状态码
if (result == null || result.Length == 0)
{
//关闭进度条
ProgressFormBar.Current.Close();
//允许按系统的返回键
Config.Instance.BackKeyCanClick = true;
HdlUserCenterResourse.AccountOption.AppCanSignout = true;
return null;
}
//将输入写入本地的临时文件夹
if (fileName.StartsWith("House_") == false)
{
HdlFileLogic.Current.SaveByteToFile(System.IO.Path.Combine(newDir, fileName), result);
}
else
{
//兼容旧云端的数据迁移到新云端
var myHouse = Newtonsoft.Json.JsonConvert.DeserializeObject(Encoding.UTF8.GetString(result));
myHouse.Id = Config.Instance.Home.Id;
HdlFileLogic.Current.SaveFileContent(System.IO.Path.Combine(newDir, myHouse.FileName), myHouse);
}
//设置进度值
ProgressFormBar.Current.SetValue(fileCount, listFileCount);
}
//允许按系统的返回键
Config.Instance.BackKeyCanClick = true;
HdlUserCenterResourse.AccountOption.AppCanSignout = true;
return newDir;
}
///
/// 从云端获取全部的备份文件的名字(key:文件名字 value:主键)
///
/// 备份主键
/// 失败时是否显示tip消息
///
private Dictionary GetBackFileIDFromDB(string i_folderId, ShowNetCodeMode mode = ShowNetCodeMode.YES)
{
//获取这个备份下的文件列表
var pra = new { folderId = i_folderId, homeId = Config.Instance.Home.Id };
var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/backup/file/findAll", RestSharp.Method.POST, pra, null, null, CheckMode.A账号权限);
//检测状态码
if (HdlCheckLogic.Current.CheckNetCode(result, mode) == false)
{
return null;
}
var listData = Newtonsoft.Json.JsonConvert.DeserializeObject>(result.Data.ToString());
//获取文件名字
var dicFileName = new Dictionary();
foreach (var fileData in listData)
{
dicFileName[fileData.FileName] = fileData.Id;
}
return dicFileName;
}
#endregion
#region ■ 创建备份___________________________
///
/// 创建一个备份名字(成功时返回备份的主键ID,失败时返回null)
///
/// 备份名字
/// 备份类型(1:手动备份,2:自动备份)
/// 失败时是否显示tip消息
///
public string CreatNewBackupNameToDB(string backupName, BackUpMode backupDiv, ShowNetCodeMode mode = ShowNetCodeMode.YES)
{
//获取备份类型的字符标识
string strdiv = this.GetBackUpModeText(backupDiv);
var pra = new
{
backupClassify = strdiv,
backupDataType = "ZIGBEE_HOME",
folderName = backupName,
homeId = Config.Instance.Home.Id
};
var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/backup/folder/create", RestSharp.Method.POST, pra, null, null, CheckMode.A账号权限);
//检测状态码
if (HdlCheckLogic.Current.CheckNetCode(result, mode) == false)
{
return null;
}
var data = Newtonsoft.Json.JsonConvert.DeserializeObject(result.Data.ToString());
return data.Id;
}
///
/// 保存本地备份到APP(调试宝专用)
///
/// 备份名字
/// 完成后的回调事件(参数为文件夹名字)
public void SaveBackupDataToApp(string backupName, Action finishEvent = null)
{
//获取本机保存的全部住宅备份数据
var listLocalHouse = HdlResidenceLogic.Current.GetAllLocalResidenceListByDirectory(false);
var directoryName = "BackupResidenceData_" + DateTime.Now.ToString("yyyyMMdd_HHmmss");
foreach (var house in listLocalHouse)
{
if (house.Id == Config.Instance.Home.Id && house.Name == backupName
&& house.HouseDataDiv != 1)
{
//如果本地拥有相同名字的备份的话,则直接覆盖
directoryName = house.SaveDirctoryName;
//备份数据已经存在,是否覆盖?
HdlMessageLogic.Current.ShowMassage(ShowMsgType.Confirm, Language.StringByID(R.MyInternationalizationString.BackUpDataIsEsixtAndPickUp), () =>
{
//将本地文件复制到指定的文件夹中
this.CopyLocalFileToDirectory(directoryName, backupName, finishEvent);
});
return;
}
}
//将本地文件复制到指定的文件夹中
this.CopyLocalFileToDirectory(directoryName, backupName, finishEvent);
}
///
/// 将本地文件复制到指定的文件夹中(调试宝专用)
///
/// 保存的文件夹名字
/// 备份名字
/// 完成后的回调事件(参数为文件夹名字)
private void CopyLocalFileToDirectory(string directoryName, string backUpName, Action finishEvent = null)
{
//当前路径
var localPath = Common.Config.Instance.FullPath;
//需要保存的路径
var savePath = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Common.Config.Instance.Guid, directoryName);
if (System.IO.Directory.Exists(savePath) == false)
{
//创建文件夹
System.IO.Directory.CreateDirectory(savePath);
}
else
{
//如果存在,则清空全部文件
var files = System.IO.Directory.GetFiles(savePath);
foreach (var file in files)
{
HdlFileLogic.Current.DeleteFile(file);
}
}
//本地全部的文件
var listLocalFile = HdlFileLogic.Current.GetRootPathListFile();
foreach (var file in listLocalFile)
{
//判断指定文件是否需要复制过去
if (file.StartsWith("House_") == true
|| this.IsNotUpLoadFile(file) == true)
{
continue;
}
string localFile = System.IO.Path.Combine(localPath, file);
string newFile = System.IO.Path.Combine(savePath, file);
try
{
//复制
System.IO.File.Copy(localFile, newFile);
}
catch { }
}
//序列化住宅对象
string oldHomeName = Common.Config.Instance.Home.Name;
Common.Config.Instance.Home.Name = backUpName;
HdlFileLogic.Current.SaveFileContent(System.IO.Path.Combine(savePath, Common.Config.Instance.Home.FileName), Common.Config.Instance.Home);
Common.Config.Instance.Home.Name = oldHomeName;
//将模板数据保存到到指定的文件夹中
var templateName = HdlTemplateCommonLogic.Current.GetNewTemplateFileName();
var templateFile = HdlTemplateCommonLogic.Current.SaveTemplateDataToFile(templateName, backUpName);
//将模板bin文件移动到备份文件夹中
try { System.IO.File.Move(templateFile, System.IO.Path.Combine(savePath, templateName)); }
catch (Exception ex) { HdlLogLogic.Current.WriteLog(ex, "移动模板失败"); }
//本地备份保存成功
if (finishEvent == null)
{
HdlMessageLogic.Current.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.SaveLocalBackDataSuccess));
}
else
{
//调用回调函数
finishEvent.Invoke(directoryName);
}
}
#endregion
#region ■ 上传备份___________________________
///
/// 上传本地所有文件到云端(函数内部有进度条)
///
/// 备份主键ID
/// 指定上传的是哪个文件夹的文件(全路径),不指定时,上传的是本地路径下的文件
/// 是否设置显示进度条
/// 失败时是否显示tip消息
///
public bool UpLoadBackupFileToDB(string i_folderId, string upPath = "", bool showBar = true, ShowNetCodeMode mode = ShowNetCodeMode.YES)
{
List listAllFile = null;
//文件夹的全路径
string fullDir = string.Empty;
string localTemplateName = string.Empty;
if (upPath == string.Empty)
{
//将模板数据保存到到指定的文件夹中
var templateName = HdlTemplateCommonLogic.Current.GetNewTemplateFileName();
var templateFile = HdlTemplateCommonLogic.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 = HdlFileLogic.Current.GetRootPathListFile();
fullDir = Common.Config.Instance.FullPath;
}
else
{
listAllFile = HdlFileLogic.Current.GetFileFromDirectory(upPath);
fullDir = upPath;
}
if (listAllFile.Count == 0)
{
return true;
}
//普通文件,可以一次性上传多个的
var listNormalFile = new List();
foreach (string fileName in listAllFile)
{
//判断指定文件是否需要上传(根目录的才判断)
if (upPath == string.Empty && this.IsNotUpLoadFile(fileName) == true)
{
continue;
}
listNormalFile.Add(fileName);
}
//开启进度条
int listFileCount = listNormalFile.Count;
if (showBar == true)
{
//开启进度条 正在上传备份文件
ProgressFormBar.Current.Start();
ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uBackupFileUploading));
}
//不允许按系统的返回键
Config.Instance.BackKeyCanClick = false;
HdlUserCenterResourse.AccountOption.AppCanSignout = false;
for (int i = 0; i < listNormalFile.Count; i++)
{
string file = listNormalFile[i];
//账号已经退出
if (HdlCheckLogic.Current.IsAccountLoginOut() == true)
{
//关闭进度条
ProgressFormBar.Current.Close();
//允许按系统的返回键
Config.Instance.BackKeyCanClick = true;
HdlUserCenterResourse.AccountOption.AppCanSignout = true;
if (localTemplateName != string.Empty)
{
//删除掉这个模板文件
HdlFileLogic.Current.DeleteFile(localTemplateName);
}
return false;
};
var fileByte = HdlFileLogic.Current.ReadFileByteContent(System.IO.Path.Combine(fullDir, file));
//执行上传
bool falge = this.DoUpLoadInfoToDB(i_folderId, file, fileByte, mode);
if (falge == false)
{
//关闭进度条
ProgressFormBar.Current.Close();
//允许按系统的返回键
Config.Instance.BackKeyCanClick = true;
HdlUserCenterResourse.AccountOption.AppCanSignout = true;
if (localTemplateName != string.Empty)
{
//删除掉这个模板文件
HdlFileLogic.Current.DeleteFile(localTemplateName);
}
return false;
}
//设置进度值
ProgressFormBar.Current.SetValue(i + 1, listFileCount);
}
if (localTemplateName != string.Empty)
{
//删除掉这个模板文件
HdlFileLogic.Current.DeleteFile(localTemplateName);
}
//进度条关闭
ProgressFormBar.Current.Close();
//允许按系统的返回键
Config.Instance.BackKeyCanClick = true;
HdlUserCenterResourse.AccountOption.AppCanSignout = true;
return true;
}
///
/// 执行上传到云端
///
/// 备份主键ID
/// 文件名字
/// 上传的数据
/// 失败时是否显示tip消息
///
public bool DoUpLoadInfoToDB(string i_folderId, string i_fileName, byte[] i_content, ShowNetCodeMode mode = ShowNetCodeMode.YES)
{
var dicQuery = new Dictionary();
dicQuery["folderId"] = i_folderId;
dicQuery["fileName"] = i_fileName;
dicQuery["homeId"] = Config.Instance.Home.Id;
var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/backup/file/create", RestSharp.Method.POST, i_content, dicQuery, null, CheckMode.A账号权限, true, 10);
//检测状态码
if (HdlCheckLogic.Current.CheckNetCode(result, mode) == false)
{
return false;
}
return true;
}
#endregion
#region ■ 编辑备份___________________________
///
/// 编辑备份名字
///
/// 备份主键id
/// 备注名字
/// 失败时是否显示tip消息
///
public bool EditorBackupName(string i_folderId, string i_backName, ShowNetCodeMode mode = ShowNetCodeMode.YES)
{
var pra = new { folderId = i_folderId, folderName = i_backName, homeId = Config.Instance.Home.Id };
var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/backup/folder/update", RestSharp.Method.POST, pra, null, null, CheckMode.A账号权限);
//检测状态码
if (HdlCheckLogic.Current.CheckNetCode(result, mode) == false)
{
return false;
}
return true;
}
#endregion
#region ■ 下载备份___________________________
///
/// 下载APP备份文档(函数内部有进度条,调试宝专用)
///
///
/// 云端显示的名字
/// 成功之后的事件,参数为保存备份的路径文件夹(全路径)
public void DownLoadAppBackupInfo(string BackupClassId, string BackupName, Action finishEvent = null)
{
//获取本机保存的全部住宅备份数据
var listLocalHouse = HdlResidenceLogic.Current.GetAllLocalResidenceListByDirectory(false);
var directoryName = "DownLoadResidenceData_" + DateTime.Now.ToString("yyyyMMdd_HHmmss");
foreach (var house in listLocalHouse)
{
//如果名字和住宅base数据的名字一样也可以保存
if (house.Name == BackupName && house.HouseDataDiv != 1)
{
//如果本地拥有相同名字的备份的话,则直接覆盖
directoryName = house.SaveDirctoryName;
//备份数据已经存在,是否覆盖?
HdlMessageLogic.Current.ShowMassage(ShowMsgType.Confirm, Language.StringByID(R.MyInternationalizationString.BackUpDataIsEsixtAndPickUp), () =>
{
//下载APP备份文档,然后存入固定的文件夹中
HdlThreadLogic.Current.RunThread(() =>
{
this.DownLoadAppBackupInfoAndSetToDirectory(BackupClassId, directoryName, finishEvent);
});
});
return;
}
}
//下载APP备份文档,然后存入固定的文件夹中
this.DownLoadAppBackupInfoAndSetToDirectory(BackupClassId, directoryName, finishEvent);
}
///
/// 下载APP备份文档,然后存入固定的文件夹中(调试宝专用)
///
///
/// 目标文件夹
/// 成功之后的事件,参数为保存备份的路径文件夹(全路径)
private void DownLoadAppBackupInfoAndSetToDirectory(string BackupClassId, string targetDir, Action finishEvent = null)
{
//全路径
targetDir = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Common.Config.Instance.Guid, targetDir);
//打开进度条 正在获取备份文件列表
ProgressFormBar.Current.Start();
ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uBackupFileListGetting));
//从云端获取备份的文件,然后存入本地的临时文件夹
string tempDirectory = GetBackFileFromDBAndSetToLocation(BackupClassId);
if (tempDirectory == null)
{
//关闭进度条
ProgressFormBar.Current.Close();
return;
}
tempDirectory = System.IO.Path.Combine(Config.Instance.FullPath, tempDirectory);
if (System.IO.Directory.Exists(targetDir) == false)
{
//创建文件夹
System.IO.Directory.CreateDirectory(targetDir);
}
else
{
//如果存在,则清空全部文件
var files = System.IO.Directory.GetFiles(targetDir);
foreach (var file in files)
{
HdlFileLogic.Current.DeleteFile(file);
}
}
//获取下载到的全部的文件
var listFile = HdlFileLogic.Current.GetFileFromDirectory(tempDirectory);
foreach (var file in listFile)
{
var sourseFile = System.IO.Path.Combine(tempDirectory, file);
var targetFile = System.IO.Path.Combine(targetDir, file);
//移动文件
try { System.IO.File.Move(sourseFile, targetFile); }
catch { }
}
//关闭进度条
ProgressFormBar.Current.Close();
//调用回调函数
finishEvent?.Invoke(targetDir);
finishEvent = null;
}
#endregion
#region ■ 读取备份___________________________
///
/// 读取APP备份文档(函数内部有进度条,Home专用)
///
///
/// 失败时是否显示tip消息
public bool LoadAppBackupInfo(string i_folderId, ShowNetCodeMode mode = ShowNetCodeMode.YES)
{
//打开进度条 正在获取备份文件列表
ProgressFormBar.Current.Start();
ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uBackupFileListGetting));
//从云端获取备份的文件,然后存入本地的临时文件夹
string tempDirectory = GetBackFileFromDBAndSetToLocation(i_folderId, mode);
if (tempDirectory == null)
{
//关闭进度条
ProgressFormBar.Current.Close();
return false;
}
//清空全部房间
HdlRoomLogic.Current.DeleteAllRoom();
//清空本地全部的场景数据
HdlSceneLogic.Current.DeleteAllLocalScene();
//如果读取到的文件完全没有问题,则清理本地的文件
HdlFileLogic.Current.DeleteAllLocationFile(false);
//清理本地的模板文件
HdlTemplateCommonLogic.Current.DeleteAllLocalFile();
//没有错误的话,则移动到当前住宅文件夹下面
HdlFileLogic.Current.MoveDirectoryFileToHomeDirectory(tempDirectory, true);
//删除全部的自动备份的本地文件(此函数用于读取自动备份的时候使用)
this.DeleteAllAutoBackupFile();
//刷新本地缓存
HdlUserCenterLogic.Current.RefreshAllMemory();
//关闭进度条
ProgressFormBar.Current.Close();
return true;
}
#endregion
#region ■ 加载备份___________________________
///
/// 加载本地的备份数据(调试宝专用)
///
/// 备份数据所在的文件夹(不是全路径)
public void LoadLocalBackupData(string strDirectory)
{
//全路径
strDirectory = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Common.Config.Instance.Guid, strDirectory);
//清空全部房间
HdlRoomLogic.Current.DeleteAllRoom();
//清空本地全部的场景数据
HdlSceneLogic.Current.DeleteAllLocalScene();
//清理本地的文件
HdlFileLogic.Current.DeleteAllLocationFile(false);
//清理本地的模板文件
HdlTemplateCommonLogic.Current.DeleteAllLocalFile();
string localDir = Common.Config.Instance.FullPath;
//获取全部的文件
var listFile = HdlFileLogic.Current.GetFileFromDirectory(strDirectory);
foreach (var file in listFile)
{
var sourseFile = System.IO.Path.Combine(strDirectory, file);
if (file.StartsWith("ModelData_") == true)
{
//复制模板数据文件到指定文件夹
HdlTemplateCommonLogic.Current.CopyTemplateFileToLocalDirectory2(sourseFile);
continue;
}
//其他文件都移到本地
var targetFile = System.IO.Path.Combine(localDir, file);
//复制文件
try { System.IO.File.Copy(sourseFile, targetFile, true); }
catch { }
}
//刷新本地缓存
HdlUserCenterLogic.Current.RefreshAllMemory();
}
#endregion
#region ■ 恢复临时备份_______________________
///
/// 恢复临时备份
///
public void RestoreTemporaryBackupData()
{
//如果本地存在临时备份的文件夹的话,则说明用户在查看模板的时候,强制关闭了App
//这个时候,App再次启动的时候,则还原数据
if (System.IO.Directory.Exists(HdlFileNameResourse.TemporaryBackupLocalFileDirectory) == false)
{
return;
}
//清空全部房间
HdlRoomLogic.Current.DeleteAllRoom();
//清空本地全部的场景数据
HdlSceneLogic.Current.DeleteAllLocalScene();
//清理本地的文件
HdlFileLogic.Current.DeleteAllLocationFile(false);
//清理本地的模板文件
HdlTemplateCommonLogic.Current.DeleteAllLocalFile();
//将备份中的模板文件复制到模板的文件夹
HdlFileLogic.Current.CopyDirectoryFileToDirectory(HdlFileNameResourse.TemporaryBackupTemplateFileDirectory, HdlFileNameResourse.LocalTemplateDirectory);
//将备份中的根目录文件复制到根目录
HdlFileLogic.Current.CopyDirectoryFileToDirectory(HdlFileNameResourse.TemporaryBackupLocalFileDirectory, Common.Config.Instance.FullPath);
//删除掉这个临时备份的文件夹
HdlFileLogic.Current.DeleteDirectory(HdlFileNameResourse.TemporaryBackupLocalFileDirectory);
}
#endregion
#region ■ 删除备份___________________________
///
/// 删除云端备份
///
/// 备份的主键
/// 失败时是否显示tip消息
///
public bool DeleteDbBackupData(string i_folderId, ShowNetCodeMode mode = ShowNetCodeMode.YES)
{
var pra = new { folderId = i_folderId, homeId = Config.Instance.Home.Id };
var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/backup/folder/delete", RestSharp.Method.POST, pra, null, null, CheckMode.A账号权限);
//检测状态码
if (HdlCheckLogic.Current.CheckNetCode(result, mode) == false)
{
return false;
}
return true;
}
///
/// 删除本地备份
///
/// 文件夹名字
public void DeleteLocalBackupData(string dirctoryName)
{
string strPath = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Common.Config.Instance.Guid, dirctoryName);
if (System.IO.Directory.Exists(strPath) == true)
{
try { System.IO.Directory.Delete(strPath, true); }
catch (Exception ex)
{ HdlLogLogic.Current.WriteLog(ex, "删除本地备份文件失败"); }
}
}
#endregion
#region ■ 上传自动备份_______________________
///
/// 手动执行上传自动备份数据(0:没有可上传的自动备份数据 1:成功 -1:失败)
///
///
public int DoUpLoadAutoBackupDataByHand()
{
//编辑文件
List listEditor = this.GetLocalAutoBackupEditorFile();
//删除文件
List listDelete = this.GetLocalAutoBackupDeleteFile();
//没有数据
if (listEditor.Count == 0 && listDelete.Count == 0)
{
return 0;
}
//开启进度条 正在上传备份文件
ProgressFormBar.Current.Start();
ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uBackupFileUploading));
//上传文件到云端
bool result = UpLoadAutoBackupFileToDB(listEditor);
if (result == false)
{
ProgressFormBar.Current.Close();
return -1;
}
//删除文件
result = DoDeleteAutoBackFileFromDB(listDelete);
if (result == false)
{
ProgressFormBar.Current.Close();
return -1;
}
ProgressFormBar.Current.Close();
return 1;
}
///
/// 上传文件到云端
///
/// 编辑或者添加的文件(目前已经不是上传它了)
///
private bool UpLoadAutoBackupFileToDB(List listFile)
{
//获取app的自动备份
var listBackData = this.GetBackupListNameFromDB(BackUpMode.A自动备份);
if (listBackData == null)
{
return false;
}
string strBackId = string.Empty;
if (listBackData.Count == 0)
{
//创建一个新的备份
strBackId = this.CreatNewBackupNameToDB("AutoBackup", BackUpMode.A自动备份);
if (strBackId == null)
{
return false;
}
}
else
{
//自动备份只有一个
strBackId = listBackData[0].Id;
}
string localPath = Config.Instance.FullPath;
//将模板数据保存到到指定的文件夹中
var templateName = HdlTemplateCommonLogic.Current.GetNewTemplateFileName(new DateTime(2000, 12, 31, 12, 59, 57));
var templateFile = HdlTemplateCommonLogic.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();
foreach (string fileName in listAllFile)
{
//判断指定文件是否需要上传(根目录的才判断)
if (this.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 fileContent = HdlFileLogic.Current.ReadFileByteContent(System.IO.Path.Combine(localPath, file));
if (fileContent == null)
{
continue;
}
//执行上传
var falge = this.DoUpLoadInfoToDB(strBackId, file, fileContent);
if (falge == false)
{
return false;
}
//设置进度值
ProgressFormBar.Current.SetValue(i + 1, listFileCount);
}
//删除掉这个模板文件
HdlFileLogic.Current.DeleteFile(localTemplateName);
//删除文件
var backPath = HdlFileNameResourse.AutoBackupDirectory;
foreach (var file in listFile)
{
string fullName = System.IO.Path.Combine(backPath, file);
HdlFileLogic.Current.DeleteFile(fullName);
}
return true;
}
///
/// 云端执行删除指定文件
///
/// 删除的文件
///
private bool DoDeleteAutoBackFileFromDB(List listData)
{
if (listData.Count == 0)
{
return true;
}
//获取app的自动备份
var data = this.GetBackupListNameFromDB(BackUpMode.A自动备份);
if (data == null || data.Count == 0)
{
return true;
}
//自动备份只有一个
var result = this.DeleteDbBackupData(data[0].Id);
if (result == false)
{
return false;
}
//删除文件
var backPath = HdlFileNameResourse.AutoBackupdeleteDirectory;
foreach (var file in listData)
{
string fullName = System.IO.Path.Combine(backPath, file);
HdlFileLogic.Current.DeleteFile(fullName);
}
return true;
}
#endregion
#region ■ 获取本地自动备份文件_______________
///
/// 获取本地自动备份目录下的添加或者编辑的文件
///
///
public List GetLocalAutoBackupEditorFile()
{
return HdlFileLogic.Current.GetFileFromDirectory(HdlFileNameResourse.AutoBackupDirectory);
}
///
/// 获取本地自动备份目录下的删除的文件
///
///
public List GetLocalAutoBackupDeleteFile()
{
return HdlFileLogic.Current.GetFileFromDirectory(HdlFileNameResourse.AutoBackupdeleteDirectory);
}
#endregion
#region ■ 设置自动备份文件状态_______________
///
/// 变更自动备份添加或者修改文件的状态
///
/// 文件的名字,不含路径
public void AddOrEditorAutoBackFileStatu(string fileName)
{
fileName = System.IO.Path.GetFileName(fileName);
//自动备份目录
string strBackPath = HdlFileNameResourse.AutoBackupDirectory;
if (System.IO.Directory.Exists(strBackPath) == false)
{
//预创建个人中心全部的文件夹
HdlFileLogic.Current.CreatAllUserCenterDirectory();
}
//自动删除备份目录
string strdelBackPath = HdlFileNameResourse.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);
}
///
/// 变更自动备份删除文件的状态
///
/// 文件的名字,不含路径
public void DeleteAutoBackFileStatu(string fileName)
{
fileName = System.IO.Path.GetFileName(fileName);
//自动删除备份目录
string strBackPath = HdlFileNameResourse.AutoBackupdeleteDirectory;
string newFile = System.IO.Path.Combine(strBackPath, fileName);
//创建一个空文件
var file = System.IO.File.Create(newFile);
file.Close();
//自动备份目录
strBackPath = HdlFileNameResourse.AutoBackupDirectory;
//如果备份列表里面有这个东西的话,移除掉
string delFile = System.IO.Path.Combine(strBackPath, fileName);
HdlFileLogic.Current.DeleteFile(delFile);
}
#endregion
#region ■ 同步数据___________________________
///
/// 同步云端数据(仅限APP启动之后) -1:异常 0:已经同步过,不需要同步 1:正常同步 2:没有自动备份数据
///
///
public int SynchronizeDbAutoBackupData()
{
//暂时不支持成员
if (HdlUserCenterResourse.ResidenceOption.AuthorityNo == 3)
{
//同步服务器的分享内容
HdlShardLogic.Current.SynchronizeDbSharedContent();
return 1;
}
//判断是否能够同步数据
string checkFile = HdlFileNameResourse.AutoDownLoadBackupCheckFile;
//如果本地已经拥有了这个文件,则说明不是新手机,不再自动还原
if (System.IO.File.Exists(checkFile) == true)
{
return 0;
}
//获取app的自动备份
var listData = HdlBackupLogic.Current.GetBackupListNameFromDB(BackUpMode.A自动备份);
if (listData == null)
{
return -1;
}
if (listData.Count == 0)
{
//创建一个空文件(标识已经完成同步)
var file = System.IO.File.Create(checkFile);
file.Close();
return 2;
}
//自动备份只有一个
string backId = listData[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();
//重新刷新住宅对象
HdlUserCenterLogic.Current.RefreshHomeObject();
//根据模板文件,恢复数据
HdlTemplateCommonLogic.Current.RecoverDataByTemplateBinFile();
//强制生成设备和网关文件
HdlTemplateCommonLogic.Current.CreatDeviceAndGatewayFileFromMemoryByForce();
//关闭进度条
ProgressFormBar.Current.Close();
return 1;
}
#endregion
#region ■ 上传Log备份________________________
///
/// 上传Log备份(隐匿功能)
///
///
public bool UpLoadLogBackup()
{
string upPath = HdlFileNameResourse.LogDirectory;
if (HdlFileLogic.Current.GetFileFromDirectory(upPath).Count == 0)
{
//没有Log文件
return true;
}
ProgressFormBar.Current.Start();
ProgressFormBar.Current.SetMsg("正在上传Log文件");
//从云端获取数据
var pageData = this.GetBackupListNameFromDB(BackUpMode.A手动备份, ShowNetCodeMode.YES, true);
if (pageData == null)
{
ProgressFormBar.Current.Close();
return false;
}
string backId = string.Empty;
for (int i = 0; i < pageData.Count; i++)
{
if (pageData[i].FolderName == HdlFileNameResourse.OptionBackupName)
{
//获取功能备份的ID
backId = pageData[i].Id;
break;
}
}
if (backId == string.Empty)
{
//创建新的备份
backId = this.CreatNewBackupNameToDB(HdlFileNameResourse.OptionBackupName, BackUpMode.A手动备份);
if (backId == null)
{
ProgressFormBar.Current.Close();
return false;
}
}
//上传Log文件
bool result = this.UpLoadBackupFileToDB(backId, upPath, false);
if (result == true)
{
try
{
var listAllFile = HdlFileLogic.Current.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;
}
///
/// 上传东西到隐匿功能备份
///
///
///
///
public bool UpLoadByteDataToOptionBackup(string fileName, byte[] byteData)
{
ProgressFormBar.Current.Start();
ProgressFormBar.Current.SetMsg("正在上传文件");
//从云端获取数据
var pageData = this.GetBackupListNameFromDB(BackUpMode.A手动备份, ShowNetCodeMode.YES, 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].FolderName == HdlFileNameResourse.OptionBackupName)
{
//获取功能备份的ID
backId = pageData[i].Id;
break;
}
}
if (backId == string.Empty)
{
//创建新的备份
backId = this.CreatNewBackupNameToDB(HdlFileNameResourse.OptionBackupName, BackUpMode.A手动备份);
if (backId == null)
{
ProgressFormBar.Current.Close();
HdlThreadLogic.Current.RunMain(() =>
{
var contr = new ShowMsgControl(ShowMsgType.Tip, "创建功能备份失败");
contr.Show();
});
return false;
}
}
//执行上传
bool falge = DoUpLoadInfoToDB(backId, fileName, byteData);
//关闭进度条
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
#region ■ 读取隐匿功能配置___________________
///
/// 读取隐匿功能配置(不要在意返回值)
///
///
public bool LoadHideOption()
{
//先初始化
HdlUserCenterResourse.HideOption = new HideOptionInfo();
if (HdlUserCenterResourse.ResidenceOption.AuthorityNo != 1 && HdlUserCenterResourse.ResidenceOption.AuthorityNo != 2)
{
return true;
}
//从云端获取数据
var pageData = this.GetBackupListNameFromDB(BackUpMode.A手动备份, ShowNetCodeMode.No, true);
if (pageData == null)
{
return false;
}
string backId = string.Empty;
for (int i = 0; i < pageData.Count; i++)
{
if (pageData[i].FolderName == HdlFileNameResourse.OptionBackupName)
{
//获取功能备份的ID
backId = pageData[i].Id;
break;
}
}
if (backId == string.Empty)
{
//没有功能配置
return true;
}
//获取这个备份下面有多少个文件
var dicFile = this.GetBackFileIDFromDB(backId, ShowNetCodeMode.No);
if (dicFile == null)
{
return false;
}
if (dicFile.Count == 0)
{
return true;
}
//检测
string checkKeys = HdlCommonLogic.Current.EncryptPassword(HdlUserCenterResourse.FileEncryptKey, HdlFileNameResourse.ShowOptionMenuFile + HdlUserCenterResourse.UserInfo.Account);
if (dicFile.ContainsKey(checkKeys) == true)
{
//显示主页隐藏菜单(Debug用)
HdlUserCenterResourse.HideOption.CenterHideMenu = 1;
}
checkKeys = HdlCommonLogic.Current.EncryptPassword(HdlUserCenterResourse.FileEncryptKey, HdlFileNameResourse.DetailedLogFile + HdlUserCenterResourse.UserInfo.Account);
if (dicFile.ContainsKey(checkKeys) == true)
{
//出力详细Log(Debug用)
HdlUserCenterResourse.HideOption.DetailedLog = 1;
}
checkKeys = HdlCommonLogic.Current.EncryptPassword(HdlUserCenterResourse.FileEncryptKey, HdlFileNameResourse.DeviceHistoryFile + HdlUserCenterResourse.UserInfo.Account);
if (dicFile.ContainsKey(checkKeys) == true)
{
//显示设备历史版本(Debug用)
HdlUserCenterResourse.HideOption.DeviceHistory = 1;
}
checkKeys = HdlCommonLogic.Current.EncryptPassword(HdlUserCenterResourse.FileEncryptKey, HdlFileNameResourse.StartDebugAppFile + HdlUserCenterResourse.UserInfo.Account);
if (dicFile.ContainsKey(checkKeys) == true)
{
//开启后台调试App功能(Debug用)
HdlUserCenterResourse.HideOption.StartDebugApp = 1;
}
checkKeys = HdlCommonLogic.Current.EncryptPassword(HdlUserCenterResourse.FileEncryptKey, HdlFileNameResourse.CheckDeviceTypeFile + HdlUserCenterResourse.UserInfo.Account);
if (dicFile.ContainsKey(checkKeys) == true)
{
//开启检测设备Type的(Debug用)
HdlUserCenterResourse.HideOption.CheckDeviceType = 1;
}
return true;
}
#endregion
#region ■ 备份提醒___________________________
///
/// 保存备份提醒设置到本地
///
/// 不再提示
///
public void SaveBackupNotPrompted(bool notPrompted, int day = -1)
{
//文件全路径
string fullName = HdlFileNameResourse.AutoBackupNotPromptedFile;
BackupNotPrompted info = null;
if (System.IO.File.Exists(fullName) == true)
{
var data = HdlFileLogic.Current.ReadFileByteContent(fullName);
info = JsonConvert.DeserializeObject(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);
}
///
/// 显示自动备份的界面
///
public void ShowAutoBackupPromptedForm()
{
if (HdlUserCenterResourse.ResidenceOption.AuthorityNo == 3)
{
//暂不支持成员
return;
}
//判断是否有文件变更了
if (CheckAutoBackupFileIsChanged() == false)
{
return;
}
//判断能否显示自动备份的界面
if (this.CheckCanShowAutoBackupForm() == true)
{
//HdlThreadLogic.Current.RunMain(() =>
//{
// var form = new UserCenter.HdlBackup.HdlAutoBackupForm();
// form.AddForm();
//});
}
}
///
/// 检测自动备份文件是否变更过
///
///
private bool CheckAutoBackupFileIsChanged()
{
List listFile1 = HdlFileLogic.Current.GetFileFromDirectory(HdlFileNameResourse.AutoBackupDirectory);
List listFile2 = this.GetLocalAutoBackupDeleteFile();
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;
}
///
/// 检测能否显示自动备份的界面
///
///
private bool CheckCanShowAutoBackupForm()
{
//文件全路径
string fullName = HdlFileNameResourse.AutoBackupNotPromptedFile;
if (System.IO.File.Exists(fullName) == false)
{
//本地没有存在这个文件
return true;
}
BackupNotPrompted info = null;
var data = HdlFileLogic.Current.ReadFileByteContent(fullName);
info = JsonConvert.DeserializeObject(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 ■ 一般方法___________________________
///
/// 判断是否是应该上传的文件
///
///
///
public bool IsNotUpLoadFile(string file)
{
if (file.StartsWith("Device_") == true
|| file.StartsWith("Gateway_") == true
|| file.StartsWith("Room_") == true
|| file.StartsWith("Scene_") == true)
{
//设备,网关,房间,场景文件不需要上传,它已经保存在bin模板文件中
return true;
}
return false;
}
///
/// 获取备份类型的字符标识
///
///
///
public string GetBackUpModeText(BackUpMode i_mode)
{
if (i_mode == BackUpMode.A自动备份)
{
return "AUTOMATIC_USER_BACKUP";
}
return "USER_DEFINED_BACKUP";
}
///
/// 删除全部的自动备份的本地文件(此函数用于读取自动备份的时候使用)
///
public void DeleteAllAutoBackupFile()
{
//清空自动备份【文件夹】(编辑,追加)
string dirPath = HdlFileNameResourse.AutoBackupDirectory;
HdlFileLogic.Current.DeleteDirectory(dirPath);
HdlFileLogic.Current.CreateDirectory(dirPath, true);
//清空自动备份【文件夹】(删除)
dirPath = HdlFileNameResourse.AutoBackupdeleteDirectory;
HdlFileLogic.Current.DeleteDirectory(dirPath);
HdlFileLogic.Current.CreateDirectory(dirPath, true);
}
#endregion
#region ■ 数据结构___________________________
///
/// 自动备份不需要再次提醒的结构体
///
private class BackupNotPrompted
{
///
/// 不再提示
///
public bool NotPrompted = false;
///
/// 前一次的日期:2019-01-01(格式)
///
public string OldDay = string.Empty;
///
/// 相隔日期天数
///
public int Day = 0;
}
#endregion
}
}