using System; using System.Collections.Generic; namespace Shared.SimpleControl.Phone { public class Databackup : FrameLayout { //WebServiceBackup service_backup = new WebServiceBackup (); VerticalScrolViewLayout VerticalScrolViewMiddle; /// /// 构造函数 /// public Databackup () { BackgroundColor = SkinStyle.Current.MainColor; } /// /// 加载备份文件列表页面 /// public void DatabackupShow () { this.RemoveAll (); #region 标题 var topView = new FrameLayout () { Y = Application.GetRealHeight (36), Height = Application.GetRealHeight (90), BackgroundColor = SkinStyle.Current.MainColor }; AddChidren (topView); var title = new Button () { X = Application.GetRealWidth (90), Width = Application.GetRealWidth (640 - 180), TextAlignment = TextAlignment.Center, Text = UserConfig.Instance.CurrentRegion.Name + " " + Language.StringByID (R.MyInternationalizationString.Backup), TextSize = 19, TextColor = SkinStyle.Current.TextColor1, }; topView.AddChidren (title); title.MouseUpEventHandler += (sdf, asf) => { Dialog dialog = new Dialog (); FrameLayout bodyView = new FrameLayout (); dialog.AddChidren (bodyView); bodyView.MouseUpEventHandler += (lkj, oiu) => { dialog.Close (); }; VerticalScrolViewLayout dialogBodyView = new VerticalScrolViewLayout () { Y = Application.GetRealHeight (126), Gravity = Gravity.CenterHorizontal, Width = Application.GetRealWidth (500), Height = Application.GetRealHeight (360), BackgroundColor = SkinStyle.Current.MainColor, Radius = 5, BorderColor = SkinStyle.Current.Transparent, BorderWidth = 0 }; bodyView.AddChidren (dialogBodyView); foreach (var homeTemp in UserConfig.Instance.HomeLists) { var rowView = new RowLayout () { Height = Application.GetRealHeight (110), }; dialogBodyView.AddChidren (rowView); Button btnRowLine = new Button () { Y = Application.GetRealHeight (107), Height = Application.GetRealHeight (3), BackgroundColor = SkinStyle.Current.Black50Transparent }; rowView.AddChidren (btnRowLine); var btnPoint = new Button () { Width = Application.GetRealWidth (10), Height = Application.GetRealWidth (10), X = Application.GetRealWidth (50), Gravity = Gravity.CenterVertical, UnSelectedImagePath = "Item/Point.png", SelectedImagePath = "Item/Point.png", Visible = homeTemp.Id != UserConfig.Instance.CurrentRegion.Id }; rowView.AddChidren (btnPoint); var btnName = new Button () { Width = Application.GetRealWidth (550), Text = homeTemp.Name, TextAlignment = TextAlignment.CenterLeft, X = Application.GetRealWidth (90), TextColor = SkinStyle.Current.TextColor1, }; rowView.AddChidren (btnName); if (homeTemp.Id == UserConfig.Instance.CurrentRegion.Id) { Button btnCheck = new Button () { X = Application.GetRealWidth (20), Y = Application.GetRealHeight (25), Width = Application.GetRealWidth (72), Height = Application.GetRealHeight (61), UnSelectedImagePath = "Skin/Check.png", }; rowView.AddChidren (btnCheck); } btnName.MouseUpEventHandler += (ss, ee) => { if (UserConfig.Instance.CurrentRegion == homeTemp) { dialog.Close (); return; } MainPage.Loading.Start ("Please wait..."); System.Threading.Tasks.Task.Run (() => { try { var backuplist = IO.FileUtils.ReadFiles (); int index = 0; string oldRegionRootPath = IO.FileUtils.CreateRegionBackup (UserConfig.Instance.CurrentRegion.Id); IO.FileUtils.DeleteRegionFiles (oldRegionRootPath); foreach (var fileName in backuplist) { System.IO.FileInfo fileInfo = new System.IO.FileInfo (IO.FileUtils.RootPath + fileName); if (fileInfo.Exists) { fileInfo.MoveTo (oldRegionRootPath + fileName); Utlis.WriteLine ("move file : " + fileName); } index++; Application.RunOnMainThread (() => { int pro = (int)(index * 1.0 / backuplist.Count * 50); MainPage.Loading.Text = pro.ToString () + "%"; }); } IO.FileUtils.DeleteAllFile (); string newRegionRootPath = IO.FileUtils.CreateRegionBackup (homeTemp.Id); IO.FileUtils.RestoreRegionFiles (newRegionRootPath); UserConfig.Instance.RefreshUserConfig (); UserConfig.Instance.CurrentRegion = homeTemp; UserConfig.Instance.SaveUserConfig (); Room.InitAllRoom (); //UserConfig.Instance.RefreshUserConfig (); //MainPage.LoginUser.SaveUserInfo (); Application.RunOnMainThread (() => { new Alert ("", Language.StringByID (R.MyInternationalizationString.SwitchRegionSuccessfully), Language.StringByID (R.MyInternationalizationString.Close)).Show (); this.DatabackupShow (); EquipmentPublicClass.CheckLinkRemote (UserConfig.Instance.internetStatus); }); } catch (Exception ex) { Application.RunOnMainThread (() => { new Alert ("", Language.StringByID (R.MyInternationalizationString.CheckInternet), Language.StringByID (R.MyInternationalizationString.Close)).Show (); }); Utlis.WriteLine (ex.Message); } finally { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); dialog.Close (); }); } }); }; } dialog.Show (); }; Button ItemButton = new Button () { Width = Application.GetRealWidth (55), Height = Application.GetRealHeight (55), UnSelectedImagePath = "Item/+.png", SelectedImagePath = "Item/+.png", Y = Application.GetRealHeight (15), X = Application.GetRealWidth (640 - 80), }; topView.AddChidren (ItemButton); ItemButton.MouseUpEventHandler += (sender, e) => { Action finishAction = () => { GetHomeDataBackupList (); }; //弹窗提示上传备份Dialog CommonUtlis.Current.ShowUploadToAutomaticBackupDialog (finishAction); }; var back = new Button () { Height = Application.GetRealHeight (90), Width = Application.GetRealWidth (85), UnSelectedImagePath = "Item/Back.png", Gravity = Gravity.CenterVertical, }; topView.AddChidren (back); back.MouseUpEventHandler += (sender, e) => { (Parent as PageLayout).PageIndex -= 1; }; #endregion var FrameLayoutView = new FrameLayout () { Width = LayoutParams.MatchParent, Height = Application.GetRealHeight (1136 - 126), Y = topView.Bottom, BackgroundColor = SkinStyle.Current.ViewColor, }; AddChidren (FrameLayoutView); VerticalScrolViewMiddle = new VerticalScrolViewLayout (); VerticalScrolViewMiddle.RemoveAll (); FrameLayoutView.AddChidren (VerticalScrolViewMiddle); GetHomeDataBackupList (); } /// /// 获取住宅备份数据列表 /// void GetHomeDataBackupList () { System.Threading.Tasks.Task.Run (() => { try { Application.RunOnMainThread (() => { VerticalScrolViewMiddle.RemoveAll (); MainPage.Loading.Start (Language.StringByID (R.MyInternationalizationString.load)); }); var revertObj = HttpServerRequest.Current.GetHomeDataBackupList (); if (revertObj.Code == StateCode.SUCCESS) { var data = Newtonsoft.Json.JsonConvert.DeserializeObject> (revertObj.Data.ToString ()); if (data == null) return; Application.RunOnMainThread (() => { foreach (var folder in data) { if (folder.backupClassify == "CUSTOM_PROJECT_BACKUP") { } else { AddRowView (folder); } } }); } else { //提示错误 IMessageCommon.Current.ShowErrorInfoAlter (revertObj.Code); } } catch { Application.RunOnMainThread (() => { new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.CheckInternet), Language.StringByID (R.MyInternationalizationString.Close)).Show (); }); } finally { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); }); } }); } /// /// 加载view /// /// /// void AddRowView (BackupListNameInfoRes mBackupFile) { var rowView = new RowLayout () { Height = Application.GetRealHeight (110), }; VerticalScrolViewMiddle.AddChidren (rowView); Button btnRowLine = new Button () { Y = Application.GetRealHeight (107), Height = Application.GetRealHeight (3), BackgroundColor = SkinStyle.Current.Black50Transparent }; rowView.AddChidren (btnRowLine); var btnDelFile = new Button () { TextID = R.MyInternationalizationString.Del, BackgroundColor = SkinStyle.Current.DelColor }; btnDelFile.MouseUpEventHandler += (sender, e) => { Alert alert = new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.AreYouSureToDeleteFile), Language.StringByID (R.MyInternationalizationString.Cancel), Language.StringByID (R.MyInternationalizationString.Confrim)); alert.ResultEventHandler += (sender2, e2) => { if (e2) { DeleteFolderData (mBackupFile.id); } }; alert.Show (); }; //分享住宅隐藏删除备份按钮 if (UserConfig.Instance.CurrentRegion.IsOthreShare == false) { rowView.AddRightView (btnDelFile); } var btnPoint = new Button () { Width = Application.GetRealWidth (10), Height = Application.GetRealHeight (10), X = Application.GetRealWidth (25), Gravity = Gravity.CenterVertical, UnSelectedImagePath = "Item/Point.png", SelectedImagePath = "Item/Point.png", }; rowView.AddChidren (btnPoint); var btnName = new Button () { Width = Application.GetRealWidth (342), Height = LayoutParams.MatchParent, Text = mBackupFile.folderName.Trim (), TextAlignment = TextAlignment.CenterLeft, X = Application.GetRealWidth (50), TextColor = SkinStyle.Current.TextColor1, }; rowView.AddChidren (btnName); //保存事件 Action renameAction = (newName) => { RenameFileName (newName, mBackupFile, btnName); }; btnName.MouseUpEventHandler += (sender, e) => { CommonUtlis.Current.ShowEditTextDialog (mBackupFile.folderName, renameAction, Language.StringByID (R.MyInternationalizationString.rename)); }; var backupIcon = new Button () { Width = Application.GetRealWidth (72), Height = Application.GetRealHeight (57), X = Application.GetRealWidth (452), }; rowView.AddChidren (backupIcon); var Backup_restores = new Button () { Width = Application.GetMinRealAverage (56), Height = Application.GetMinRealAverage (57), X = backupIcon.Right + Application.GetRealWidth (20), UnSelectedImagePath = "Register/Backup_ restores.png", SelectedImagePath = "Register/Backup_ restores.png", Gravity = Gravity.CenterVertical, }; rowView.AddChidren (Backup_restores); Backup_restores.MouseDownEventHandler += (sender, e) => { Backup_restores.IsSelected = true; }; Backup_restores.MouseUpEventHandler += (sender, e) => { Backup_restores.IsSelected = false; UserBakeupFileDetailedInformation (mBackupFile.id); }; } /// /// 请求修改备注 /// /// /// /// void RenameFileName (string newName, BackupListNameInfoRes mBackupFile, Button btnName) { MainPage.Loading.Start (Language.StringByID (R.MyInternationalizationString.load)); System.Threading.Tasks.Task.Run (() => { try { var revertObj = HttpServerRequest.Current.UpdateBackupFolderName (newName, mBackupFile); if (revertObj.Code == StateCode.SUCCESS) { //AmendTheSuccess 修改成功 Application.RunOnMainThread (() => { mBackupFile.folderName = newName; btnName.Text = newName; Utlis.ShowAlertOnMainThread (Language.StringByID (R.MyInternationalizationString.AmendTheSuccess)); }); } else { //提示错误 IMessageCommon.Current.ShowErrorInfoAlter (revertObj.Code); } } catch { } finally { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); }); } }); } /// /// 删除某个备份 /// /// void DeleteFolderData (string folderId) { MainPage.Loading.Start (Language.StringByID (R.MyInternationalizationString.load)); System.Threading.Tasks.Task.Run (() => { try { var revertObj = HttpServerRequest.Current.DeleteBackupFolder (folderId); if (revertObj.Code == StateCode.SUCCESS) { GetHomeDataBackupList (); } else { IMessageCommon.Current.ShowErrorInfoAlter (revertObj.Code); } } catch { } finally { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); }); } }); } /// /// 用户恢复文件 /// public void UserBakeupFileDetailedInformation (string folderId) { //是否确定恢复该文件数据信息 Alert alert = new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.SureToRestoreFileInformation), Language.StringByID (R.MyInternationalizationString.Cancel), Language.StringByID (R.MyInternationalizationString.Confrim)); alert.ResultEventHandler += (sender2, e2) => { if (e2) { MainPage.Loading.Start ("Download..."); System.Threading.Tasks.Task.Run (() => { try { ReadFilesAndDelete (); var revertObj = HttpServerRequest.Current.GetBackupFolderAllFileList (folderId); if (revertObj.Code == StateCode.SUCCESS) { var fileListData = Newtonsoft.Json.JsonConvert.DeserializeObject> (revertObj.Data.ToString ()); MultiThreadDownload (fileListData, folderId); } else { //提示错误 IMessageCommon.Current.ShowErrorInfoAlter (revertObj.Code); } } catch (Exception ex) { Shared.Application.RunOnMainThread (() => { new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.CheckInternet), Language.StringByID (R.MyInternationalizationString.Close)).Show (); }); Shared.Utlis.WriteLine (ex.ToString ()); } finally { Shared.Application.RunOnMainThread (() => { MainPage.Loading.Hide (); MainPage.Loading.Start (Language.StringByID (R.MyInternationalizationString.load)); System.Threading.Tasks.Task.Run (() => { try { var revertObj = HttpServerRequest.Current.GetSpeakerList (); if (revertObj.Code == StateCode.SUCCESS) { var speakerListRes = Newtonsoft.Json.JsonConvert.DeserializeObject (revertObj.Data.ToString ()); if (speakerListRes != null && speakerListRes.list != null && speakerListRes.list.Count > 0) { } } else { //提示错误 IMessageCommon.Current.ShowErrorInfoAlter (revertObj.Code); } } catch { } finally { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); }); } }); }); } }); } }; alert.Show (); } /// /// 删除本地文件 /// void ReadFilesAndDelete () { var fileNames = IO.FileUtils.ReadFiles (); foreach (var fileName in fileNames) { if (fileName == UserInfo.GlobalRegisterFile || fileName == UserConfig.configFile) { continue; } IO.FileUtils.DeleteFile (fileName); } } /// /// /// private readonly object SendLocker = new object (); /// /// /// private readonly object DownloadLocker = new object (); /// /// 多线程下载 /// void MultiThreadDownload (List dataList, string folderId, int maxThreads = 5) { int index = 0; int indexCount = 0; var mFileList = dataList.FindAll ((obj) => obj.fileName != "null" && obj.fileName != UserConfig.configFile && obj.fileName != UserInfo.GlobalRegisterFile && obj.fileName != "AccountListDB" && obj.fileName != CommonConfig.ConfigFile); if (maxThreads > mFileList.Count) { maxThreads = mFileList.Count; } if (mFileList.Count <= 0) return; int resultCount = mFileList.Count / maxThreads; //int residue = mFileList.Count % maxThreads; /*开启若干线程,分别下载对应的资源*/ for (int i = 1; i <= maxThreads; i++) { int startIndex = ((i - 1) * resultCount + 0); int endIndex = resultCount * i - 1; if (i == maxThreads) { /* * 最后一个线程 * */ endIndex = mFileList.Count - 1; } var id = i; var startIndex1 = startIndex; var endIndex1 = endIndex; //Utlis.WriteLine ($"下载线程:{id} startIndex1:{startIndex1} endIndex1:{endIndex1}"); new System.Threading.Thread (() => { for (var j = startIndex1; j <= endIndex1; j++) { try { var result = DownloadSomeDataBackup (folderId, mFileList [j]); if (result) { lock (DownloadLocker) { index++; } //index++; Application.RunOnMainThread (() => { int pro = (int)(index * 1.0 / mFileList.Count * 100); MainPage.Loading.Text = pro.ToString () + "%"; }); } } catch { } finally { lock (SendLocker) { indexCount++; } } //Utlis.WriteLine ($"下载线程:{id} 完成index:{j} indexCount: {indexCount}"); } }) { IsBackground = true }.Start (); System.Threading.Thread.Sleep (100); } while (indexCount < mFileList.Count) { System.Threading.Thread.Sleep (200); } Utlis.WriteLine ($"下载完成"); if (index != mFileList.Count) { //删除已经下载的文件个数错误 ReadFilesAndDelete (); Application.RunOnMainThread (() => { new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.DownloadFailed), Language.StringByID (R.MyInternationalizationString.Close)).Show (); }); return; } MainPage.LoginUser.SaveUserInfo (); Room.InitAllRoom (); Application.RunOnMainThread (() => { //if (CommonPage.IsRemote) { // SmartHome.MqttCommon.DisConnectRemote("UserBakeupFileDetailedInformation"); //} this.RemoveFromParent (); UserMiddle.Init (true, true,false); EquipmentPublicClass.CheckLinkRemote (UserConfig.Instance.internetStatus); new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.RestoreFileIsSuccessfull), Language.StringByID (R.MyInternationalizationString.Close)).Show (); }); } /// /// 下载单个备份文件 /// /// /// /// /// bool DownloadSomeDataBackup (string folderId, BackupFileInfoRes file) { var requestJson = HttpUtil.GetSignRequestJson (new BackupFileDownObj () { folderId = folderId, fileId = file.id, homeId = UserConfig.Instance.CurrentRegion.Id }); var revertObj = HttpUtil.RequestHttpsDownload (NewAPI.API_POST_File_Down, requestJson, null, UserConfig.Instance.CurrentRegion.regionUrl); if (revertObj != null && revertObj.Length > 0) { Utlis.WriteLine ("DownloadSomeDataBackup: " + file.fileName); IO.FileUtils.WriteFileByBytes (file.fileName, revertObj); return true; } else { return false; } } #region 备上传和下载相关接口封装 #endregion } }