From 9f326f4000847e6167d8166fa2f6a66f53cb3734 Mon Sep 17 00:00:00 2001 From: 黄学彪 <hxb@hdlchina.com.cn> Date: 星期四, 17 十二月 2020 09:07:13 +0800 Subject: [PATCH] 新云端Ver1.3 --- ZigbeeApp/Shared/Phone/Common/Logic/HdlBackupLogic.cs | 1516 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 1,516 insertions(+), 0 deletions(-) diff --git a/ZigbeeApp/Shared/Phone/Common/Logic/HdlBackupLogic.cs b/ZigbeeApp/Shared/Phone/Common/Logic/HdlBackupLogic.cs new file mode 100644 index 0000000..fce1d85 --- /dev/null +++ b/ZigbeeApp/Shared/Phone/Common/Logic/HdlBackupLogic.cs @@ -0,0 +1,1516 @@ +锘縰sing 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 +{ + /// <summary> + /// 澶囦唤涓氬姟鐨勯�昏緫 + /// </summary> + public class HdlBackupLogic + { + #region 鈻� 鍙橀噺澹版槑___________________________ + + /// <summary> + /// 澶囦唤涓氬姟鐨勯�昏緫 + /// </summary> + private static HdlBackupLogic m_Current = null; + /// <summary> + /// 澶囦唤涓氬姟鐨勯�昏緫 + /// </summary> + public static HdlBackupLogic Current + { + get + { + if (m_Current == null) + { + m_Current = new HdlBackupLogic(); + } + return m_Current; + } + } + + #endregion + + #region 鈻� 鑾峰彇澶囦唤鍚嶅瓧鍒楄〃___________________ + + /// <summary> + /// 浠庝簯绔幏鍙栧浠芥暟鎹殑鍚嶅瓧鍒楄〃 + /// </summary> + /// <param name="backupDiv">澶囦唤绫诲瀷(1:鎵嬪姩澶囦唤,2锛氳嚜鍔ㄥ浠�)</param> + /// <param name="mode">澶辫触鏃舵槸鍚︽樉绀簍ip娑堟伅</param> + /// <param name="getOptionBackup">鏄惁鑾峰彇鍔熻兘澶囦唤</param> + /// <returns></returns> + public List<BackupListNameInfo> 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/app/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<List<BackupListNameInfo>>(result.Data.ToString()); + var list = new List<BackupListNameInfo>(); + foreach (var data2 in listData) + { + if (getOptionBackup == false && data2.FolderName == HdlFileNameResourse.OptionBackupName) + { + //涓嶈幏鍙栧姛鑳藉浠� + continue; + } + if (data2.BackupClassify != strdiv || data2.BackupDataType != "ZIGBEE_HOME") + { + //涓嶆槸鎸囧畾鐨勫浠界被鍨�,鎴栬�厇igbee鐨勫浠�,鍒欎笉鑾峰彇 + continue; + } + list.Add(data2); + } + return list; + } + + /// <summary> + /// <para>浠庝簯绔幏鍙栧浠界殑鏂囦欢锛岀劧鍚庡瓨鍏ユ湰鍦版寚瀹氱殑涓存椂鏂囦欢澶�</para> + /// <para>杩斿洖鏂囦欢澶瑰悕瀛�(閲岄潰瀛樻斁鐫�鍏ㄩ儴鐨勬枃浠�),杩斿洖null鏃讹紝浠h〃澶辫触</para> + /// </summary> + /// <param name="i_folderId">澶囦唤涓婚敭</param> + /// <param name="mode">澶辫触鏃舵槸鍚︽樉绀簍ip娑堟伅</param> + /// <returns></returns> + 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/app/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<House>(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; + } + + /// <summary> + /// 浠庝簯绔幏鍙栧叏閮ㄧ殑澶囦唤鏂囦欢鐨勫悕瀛�(key:鏂囦欢鍚嶅瓧 value:涓婚敭) + /// </summary> + /// <param name="i_folderId">澶囦唤涓婚敭</param> + /// <param name="mode">澶辫触鏃舵槸鍚︽樉绀簍ip娑堟伅</param> + /// <returns></returns> + private Dictionary<string, string> 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/app/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<List<LoadBackupFileNameInfo>>(result.Data.ToString()); + + //鑾峰彇鏂囦欢鍚嶅瓧 + var dicFileName = new Dictionary<string, string>(); + foreach (var fileData in listData) + { + dicFileName[fileData.FileName] = fileData.Id; + } + return dicFileName; + } + + #endregion + + #region 鈻� 鍒涘缓澶囦唤___________________________ + + /// <summary> + /// 鍒涘缓涓�涓浠藉悕瀛�(鎴愬姛鏃惰繑鍥炲浠界殑涓婚敭ID锛屽け璐ユ椂杩斿洖null) + /// </summary> + /// <param name="backupName">澶囦唤鍚嶅瓧</param> + /// <param name="backupDiv">澶囦唤绫诲瀷(1:鎵嬪姩澶囦唤,2锛氳嚜鍔ㄥ浠�)</param> + /// <param name="mode">澶辫触鏃舵槸鍚︽樉绀簍ip娑堟伅</param> + /// <returns></returns> + 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/app/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<AddBackupNameResult>(result.Data.ToString()); + return data.Id; + } + + /// <summary> + /// 淇濆瓨鏈湴澶囦唤鍒癆PP(璋冭瘯瀹濅笓鐢�) + /// </summary> + /// <param name="backupName">澶囦唤鍚嶅瓧</param> + /// <param name="finishEvent">瀹屾垚鍚庣殑鍥炶皟浜嬩欢(鍙傛暟涓烘枃浠跺す鍚嶅瓧)</param> + public void SaveBackupDataToApp(string backupName, Action<string> 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); + } + + /// <summary> + /// 灏嗘湰鍦版枃浠跺鍒跺埌鎸囧畾鐨勬枃浠跺す涓�(璋冭瘯瀹濅笓鐢�) + /// </summary> + /// <param name="directoryName">淇濆瓨鐨勬枃浠跺す鍚嶅瓧</param> + /// <param name="backUpName">澶囦唤鍚嶅瓧</param> + /// <param name="finishEvent">瀹屾垚鍚庣殑鍥炶皟浜嬩欢(鍙傛暟涓烘枃浠跺す鍚嶅瓧)</param> + private void CopyLocalFileToDirectory(string directoryName, string backUpName, Action<string> 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); + + //灏嗘ā鏉縝in鏂囦欢绉诲姩鍒板浠芥枃浠跺す涓� + 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 鈻� 涓婁紶澶囦唤___________________________ + + /// <summary> + /// 涓婁紶鏈湴鎵�鏈夋枃浠跺埌浜戠(鍑芥暟鍐呴儴鏈夎繘搴︽潯) + /// </summary> + /// <param name="i_folderId">澶囦唤涓婚敭ID</param> + /// <param name="upPath">鎸囧畾涓婁紶鐨勬槸鍝釜鏂囦欢澶圭殑鏂囦欢(鍏ㄨ矾寰�),涓嶆寚瀹氭椂锛屼笂浼犵殑鏄湰鍦拌矾寰勪笅鐨勬枃浠�</param> + /// <param name="showBar">鏄惁璁剧疆鏄剧ず杩涘害鏉�</param> + /// <param name="mode">澶辫触鏃舵槸鍚︽樉绀簍ip娑堟伅</param> + /// <returns></returns> + public bool UpLoadBackupFileToDB(string i_folderId, string upPath = "", bool showBar = true, ShowNetCodeMode mode = ShowNetCodeMode.YES) + { + List<string> 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"); + + //灏嗘ā鏉縝in鏂囦欢绉诲姩鍒板浠芥枃浠跺す涓� + 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<string>(); + foreach (string fileName in listAllFile) + { + //鍒ゆ柇鎸囧畾鏂囦欢鏄惁闇�瑕佷笂浼�(鏍圭洰褰曠殑鎵嶅垽鏂�) + if (upPath == string.Empty && this.IsNotUpLoadFile(fileName) == true) + { + continue; + } + listNormalFile.Add(fileName); + } + + //寮�鍚繘搴︽潯 + int listFileCount = listNormalFile.Count; + if (showBar == true) + { + //寮�鍚繘搴︽潯 姝e湪涓婁紶澶囦唤鏂囦欢 + 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; + } + + /// <summary> + /// 鎵ц涓婁紶鍒颁簯绔� + /// </summary> + /// <param name="i_folderId">澶囦唤涓婚敭ID</param> + /// <param name="i_fileName">鏂囦欢鍚嶅瓧</param> + /// <param name="i_content">涓婁紶鐨勬暟鎹�</param> + /// <param name="mode">澶辫触鏃舵槸鍚︽樉绀簍ip娑堟伅</param> + /// <returns></returns> + public bool DoUpLoadInfoToDB(string i_folderId, string i_fileName, byte[] i_content, ShowNetCodeMode mode = ShowNetCodeMode.YES) + { + var dicQuery = new Dictionary<string, object>(); + dicQuery["folderId"] = i_folderId; + dicQuery["fileName"] = i_fileName; + dicQuery["homeId"] = Config.Instance.Home.Id; + + var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/app/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 鈻� 缂栬緫澶囦唤___________________________ + + /// <summary> + /// 缂栬緫澶囦唤鍚嶅瓧 + /// </summary> + /// <param name="i_folderId">澶囦唤涓婚敭id</param> + /// <param name="i_backName">澶囨敞鍚嶅瓧</param> + /// <param name="mode">澶辫触鏃舵槸鍚︽樉绀簍ip娑堟伅</param> + /// <returns></returns> + 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/app/backup/folder/update", RestSharp.Method.POST, pra, null, null, CheckMode.A璐﹀彿鏉冮檺); + //妫�娴嬬姸鎬佺爜 + if (HdlCheckLogic.Current.CheckNetCode(result, mode) == false) + { + return false; + } + return true; + } + + #endregion + + #region 鈻� 涓嬭浇澶囦唤___________________________ + + /// <summary> + /// 涓嬭浇APP澶囦唤鏂囨。(鍑芥暟鍐呴儴鏈夎繘搴︽潯,璋冭瘯瀹濅笓鐢�) + /// </summary> + /// <param name="BackupClassId"></param> + /// <param name="BackupName">浜戠鏄剧ず鐨勫悕瀛�</param> + /// <param name="finishEvent">鎴愬姛涔嬪悗鐨勪簨浠�,鍙傛暟涓轰繚瀛樺浠界殑璺緞鏂囦欢澶�(鍏ㄨ矾寰�)</param> + public void DownLoadAppBackupInfo(string BackupClassId, string BackupName, Action<string> finishEvent = null) + { + //鑾峰彇鏈満淇濆瓨鐨勫叏閮ㄤ綇瀹呭浠芥暟鎹� + var listLocalHouse = HdlResidenceLogic.Current.GetAllLocalResidenceListByDirectory(false); + var directoryName = "DownLoadResidenceData_" + DateTime.Now.ToString("yyyyMMdd_HHmmss"); + foreach (var house in listLocalHouse) + { + //濡傛灉鍚嶅瓧鍜屼綇瀹卋ase鏁版嵁鐨勫悕瀛椾竴鏍蜂篃鍙互淇濆瓨 + 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); + } + + /// <summary> + /// 涓嬭浇APP澶囦唤鏂囨。,鐒跺悗瀛樺叆鍥哄畾鐨勬枃浠跺す涓�(璋冭瘯瀹濅笓鐢�) + /// </summary> + /// <param name="BackupClassId"></param> + /// <param name="targetDir">鐩爣鏂囦欢澶�</param> + /// <param name="finishEvent">鎴愬姛涔嬪悗鐨勪簨浠�,鍙傛暟涓轰繚瀛樺浠界殑璺緞鏂囦欢澶�(鍏ㄨ矾寰�)</param> + private void DownLoadAppBackupInfoAndSetToDirectory(string BackupClassId, string targetDir, Action<string> finishEvent = null) + { + //鍏ㄨ矾寰� + targetDir = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Common.Config.Instance.Guid, targetDir); + //鎵撳紑杩涘害鏉� 姝e湪鑾峰彇澶囦唤鏂囦欢鍒楄〃 + 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 鈻� 璇诲彇澶囦唤___________________________ + + /// <summary> + /// 璇诲彇APP澶囦唤鏂囨。(鍑芥暟鍐呴儴鏈夎繘搴︽潯,Home涓撶敤) + /// </summary> + /// <param name="i_folderId"></param> + /// <param name="mode">澶辫触鏃舵槸鍚︽樉绀簍ip娑堟伅</param> + public bool LoadAppBackupInfo(string i_folderId, ShowNetCodeMode mode = ShowNetCodeMode.YES) + { + //鎵撳紑杩涘害鏉� 姝e湪鑾峰彇澶囦唤鏂囦欢鍒楄〃 + 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 鈻� 鍔犺浇澶囦唤___________________________ + + /// <summary> + /// 鍔犺浇鏈湴鐨勫浠芥暟鎹�(璋冭瘯瀹濅笓鐢�) + /// </summary> + /// <param name="strDirectory">澶囦唤鏁版嵁鎵�鍦ㄧ殑鏂囦欢澶�(涓嶆槸鍏ㄨ矾寰�)</param> + 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 鈻� 鎭㈠涓存椂澶囦唤_______________________ + + /// <summary> + /// 鎭㈠涓存椂澶囦唤 + /// </summary> + public void RestoreTemporaryBackupData() + { + //濡傛灉鏈湴瀛樺湪涓存椂澶囦唤鐨勬枃浠跺す鐨勮瘽,鍒欒鏄庣敤鎴峰湪鏌ョ湅妯℃澘鐨勬椂鍊�,寮哄埗鍏抽棴浜咥pp + //杩欎釜鏃跺��,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 鈻� 鍒犻櫎澶囦唤___________________________ + + /// <summary> + /// 鍒犻櫎浜戠澶囦唤 + /// </summary> + /// <param name="i_folderId">澶囦唤鐨勪富閿�</param> + /// <param name="mode">澶辫触鏃舵槸鍚︽樉绀簍ip娑堟伅</param> + /// <returns></returns> + 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/app/backup/folder/delete", RestSharp.Method.POST, pra, null, null, CheckMode.A璐﹀彿鏉冮檺); + //妫�娴嬬姸鎬佺爜 + if (HdlCheckLogic.Current.CheckNetCode(result, mode) == false) + { + return false; + } + return true; + } + + /// <summary> + /// 鍒犻櫎鏈湴澶囦唤 + /// </summary> + /// <param name="dirctoryName">鏂囦欢澶瑰悕瀛�</param> + 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 鈻� 涓婁紶鑷姩澶囦唤_______________________ + + /// <summary> + /// 鎵嬪姩鎵ц涓婁紶鑷姩澶囦唤鏁版嵁(0:娌℃湁鍙笂浼犵殑鑷姩澶囦唤鏁版嵁 1:鎴愬姛 -1锛氬け璐�) + /// </summary> + /// <returns></returns> + public int DoUpLoadAutoBackupDataByHand() + { + //缂栬緫鏂囦欢 + List<string> listEditor = this.GetLocalAutoBackupEditorFile(); + //鍒犻櫎鏂囦欢 + List<string> listDelete = this.GetLocalAutoBackupDeleteFile(); + //娌℃湁鏁版嵁 + if (listEditor.Count == 0 && listDelete.Count == 0) + { + return 0; + } + + //寮�鍚繘搴︽潯 姝e湪涓婁紶澶囦唤鏂囦欢 + 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; + } + + /// <summary> + /// 涓婁紶鏂囦欢鍒颁簯绔� + /// </summary> + /// <param name="listFile">缂栬緫鎴栬�呮坊鍔犵殑鏂囦欢(鐩墠宸茬粡涓嶆槸涓婁紶瀹冧簡)</param> + /// <returns></returns> + private bool UpLoadAutoBackupFileToDB(List<string> 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"); + + //灏嗘ā鏉縝in鏂囦欢绉诲姩鍒板浠芥枃浠跺す涓� + 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 (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; + } + + /// <summary> + /// 浜戠鎵ц鍒犻櫎鎸囧畾鏂囦欢 + /// </summary> + /// <param name="listData">鍒犻櫎鐨勬枃浠�</param> + /// <returns></returns> + private bool DoDeleteAutoBackFileFromDB(List<string> 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 鈻� 鑾峰彇鏈湴鑷姩澶囦唤鏂囦欢_______________ + + /// <summary> + /// 鑾峰彇鏈湴鑷姩澶囦唤鐩綍涓嬬殑娣诲姞鎴栬�呯紪杈戠殑鏂囦欢 + /// </summary> + /// <returns></returns> + public List<string> GetLocalAutoBackupEditorFile() + { + return HdlFileLogic.Current.GetFileFromDirectory(HdlFileNameResourse.AutoBackupDirectory); + } + + /// <summary> + /// 鑾峰彇鏈湴鑷姩澶囦唤鐩綍涓嬬殑鍒犻櫎鐨勬枃浠� + /// </summary> + /// <returns></returns> + public List<string> GetLocalAutoBackupDeleteFile() + { + return HdlFileLogic.Current.GetFileFromDirectory(HdlFileNameResourse.AutoBackupdeleteDirectory); + } + + #endregion + + #region 鈻� 璁剧疆鑷姩澶囦唤鏂囦欢鐘舵�乢______________ + + /// <summary> + /// 鍙樻洿鑷姩澶囦唤娣诲姞鎴栬�呬慨鏀规枃浠剁殑鐘舵�� + /// </summary> + /// <param name="fileName">鏂囦欢鐨勫悕瀛�,涓嶅惈璺緞</param> + 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); + } + + /// <summary> + /// 鍙樻洿鑷姩澶囦唤鍒犻櫎鏂囦欢鐨勭姸鎬� + /// </summary> + /// <param name="fileName">鏂囦欢鐨勫悕瀛�,涓嶅惈璺緞</param> + 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 鈻� 鍚屾鏁版嵁___________________________ + + /// <summary> + /// 鍚屾浜戠鏁版嵁(浠呴檺APP鍚姩涔嬪悗) -1锛氬紓甯� 0:宸茬粡鍚屾杩囷紝涓嶉渶瑕佸悓姝� 1锛氭甯稿悓姝� 2:娌℃湁鑷姩澶囦唤鏁版嵁 + /// </summary> + /// <returns></returns> + 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澶囦唤________________________ + + /// <summary> + /// 涓婁紶Log澶囦唤(闅愬尶鍔熻兘) + /// </summary> + /// <returns></returns> + public bool UpLoadLogBackup() + { + string upPath = HdlFileNameResourse.LogDirectory; + if (HdlFileLogic.Current.GetFileFromDirectory(upPath).Count == 0) + { + //娌℃湁Log鏂囦欢 + return true; + } + + ProgressFormBar.Current.Start(); + ProgressFormBar.Current.SetMsg("姝e湪涓婁紶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) + { + //鑾峰彇鍔熻兘澶囦唤鐨処D + 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; + } + + /// <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("姝e湪涓婁紶鏂囦欢"); + + //浠庝簯绔幏鍙栨暟鎹� + 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) + { + //鑾峰彇鍔熻兘澶囦唤鐨処D + 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 鈻� 璇诲彇闅愬尶鍔熻兘閰嶇疆___________________ + + /// <summary> + /// 璇诲彇闅愬尶鍔熻兘閰嶇疆(涓嶈鍦ㄦ剰杩斿洖鍊�) + /// </summary> + /// <returns></returns> + 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) + { + //鑾峰彇鍔熻兘澶囦唤鐨処D + 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) + { + //寮�鍚悗鍙拌皟璇旳pp鍔熻兘(Debug鐢�) + HdlUserCenterResourse.HideOption.StartDebugApp = 1; + } + checkKeys = HdlCommonLogic.Current.EncryptPassword(HdlUserCenterResourse.FileEncryptKey, HdlFileNameResourse.CheckDeviceTypeFile + HdlUserCenterResourse.UserInfo.Account); + if (dicFile.ContainsKey(checkKeys) == true) + { + //寮�鍚娴嬭澶嘥ype鐨�(Debug鐢�) + HdlUserCenterResourse.HideOption.CheckDeviceType = 1; + } + return true; + } + + #endregion + + #region 鈻� 澶囦唤鎻愰啋___________________________ + + /// <summary> + /// 淇濆瓨澶囦唤鎻愰啋璁剧疆鍒版湰鍦� + /// </summary> + /// <param name="notPrompted">涓嶅啀鎻愮ず</param> + /// <param name="day"></param> + 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<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 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(); + //}); + } + } + + /// <summary> + /// 妫�娴嬭嚜鍔ㄥ浠芥枃浠舵槸鍚﹀彉鏇磋繃 + /// </summary> + /// <returns></returns> + private bool CheckAutoBackupFileIsChanged() + { + List<string> listFile1 = HdlFileLogic.Current.GetFileFromDirectory(HdlFileNameResourse.AutoBackupDirectory); + List<string> 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; + } + + /// <summary> + /// 妫�娴嬭兘鍚︽樉绀鸿嚜鍔ㄥ浠界殑鐣岄潰 + /// </summary> + /// <returns></returns> + 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<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> + /// <param name="file"></param> + /// <returns></returns> + 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; + } + + /// <summary> + /// 鑾峰彇澶囦唤绫诲瀷鐨勫瓧绗︽爣璇� + /// </summary> + /// <param name="i_mode"></param> + /// <returns></returns> + public string GetBackUpModeText(BackUpMode i_mode) + { + if (i_mode == BackUpMode.A鑷姩澶囦唤) + { + return "AUTOMATIC_USER_BACKUP"; + } + return "USER_DEFINED_BACKUP"; + } + + /// <summary> + /// 鍒犻櫎鍏ㄩ儴鐨勮嚜鍔ㄥ浠界殑鏈湴鏂囦欢(姝ゅ嚱鏁扮敤浜庤鍙栬嚜鍔ㄥ浠界殑鏃跺�欎娇鐢�) + /// </summary> + 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 鈻� 鏁版嵁缁撴瀯___________________________ + + /// <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 + } +} -- Gitblit v1.8.0