黄学彪
2020-09-22 ade5917841b0fdcb1df7353ef7c56b1a1bdc9282
ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlBackupLogic.cs
@@ -5,7 +5,6 @@
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace Shared.Phone.UserCenter
{
@@ -44,8 +43,9 @@
        /// </summary>
        /// <param name="ZigbeeBackupType">0:App手动备份 1: App 自动备份 2:网关手动备份 3:网关自动备份</param>
        /// <param name="ZigbeeUniqueId">网关的唯一Id,获取网关备份需要</param>
        /// <param name="getOptionBackup">是否获取功能备份</param>
        /// <returns></returns>
        public async Task<List<BackupListNameInfo>> GetBackupListNameFromDB(int ZigbeeBackupType = 0, string ZigbeeUniqueId = null)
        public List<BackupListNameInfo> GetBackupListNameFromDB(int ZigbeeBackupType = 0, string ZigbeeUniqueId = null, bool getOptionBackup = false)
        {
            //备份名字很少的内存,可以直接一次性全部取完
            var pra = new GetBackupListPra();
@@ -57,57 +57,59 @@
            pra.LoginAccessToken = UserCenterLogic.GetConnectMainToken();
            //访问接口
            var listNotShow = new List<string>() { "NotSetAgain" };
            string result = await UserCenterLogic.GetResponseDataByRequestHttps("App/GetHomeDataBackupPagger", true, pra, listNotShow);
            string result = UserCenterLogic.GetResponseDataByRequestHttps("App/GetHomeDataBackupPagger", true, pra);
            if (string.IsNullOrEmpty(result) == true)
            {
                return null;
            }
            var data = Newtonsoft.Json.JsonConvert.DeserializeObject<GetBackupListResult>(result);
            if (ZigbeeUniqueId != null)
            var list = new List<BackupListNameInfo>();
            foreach (var data2 in data.PageData)
            {
                var list = new List<BackupListNameInfo>();
                foreach (var data2 in data.PageData)
                if (ZigbeeUniqueId != null && data2.GatewayUniqueId != ZigbeeUniqueId)
                {
                    if (data2.GatewayUniqueId == ZigbeeUniqueId)
                    {
                        list.Add(data2);
                    }
                    continue;
                }
                return list;
                if (getOptionBackup == false && data2.BackupName == DirNameResourse.OptionBackupName)
                {
                    //不获取功能备份
                    continue;
                }
                list.Add(data2);
            }
            return data.PageData;
            return list;
        }
        /// <summary>
        /// <para>从云端获取备份的文件,然后存入本地指定的临时文件夹(函数内部有进度条)</para>
        /// <para>从云端获取备份的文件,然后存入本地指定的临时文件夹</para>
        /// <para>返回文件夹名字(里面存放着全部的文件),返回null时,代表失败</para>
        /// </summary>
        /// <param name="BackupClassId">BackupClassId</param>
        /// <param name="IsGatewayAutoBackup">当前获取是否为网关自动备份</param>
        /// <param name="ZigbeeUniqueId">ZigbeeUniqueId 网关的唯一Id(当IsGatewayBackup=true时,此参数必须提供</param>
        /// <returns></returns>
        public async Task<string> GetBackFileFromDBAndSetToLocation(string BackupClassId, bool IsGatewayAutoBackup = false, string ZigbeeUniqueId = null)
        public string GetBackFileFromDBAndSetToLocation(string BackupClassId, bool IsGatewayAutoBackup = false, string ZigbeeUniqueId = null)
        {
            //不允许按系统的返回键
            Shared.Common.CommonPage.BackKeyCanClick = false;
            UserCenterResourse.AccountOption.AppCanSignout = false;
            //首先先创建一个临时文件夹,存在文件则清空
            string newDir = System.IO.Path.Combine(DirNameResourse.LocalMemoryDirectory, DirNameResourse.DownLoadBackupTempDirectory);
            Global.CreateEmptyDirectory(newDir, true);
            //开启进度条
            ProgressBar.Show();
            //获取这个备份下面有多少个文件
            List<string> listFile = await GetBackFileIDFromDB(BackupClassId, IsGatewayAutoBackup, ZigbeeUniqueId);
            List<string> listFile = GetBackFileIDFromDB(BackupClassId, IsGatewayAutoBackup, ZigbeeUniqueId);
            if (listFile == null)
            {
                //关闭进度条
                ProgressBar.Close();
                ProgressFormBar.Current.Close();
                //允许按系统的返回键
                Shared.Common.CommonPage.BackKeyCanClick = true;
                UserCenterResourse.AccountOption.AppCanSignout = true;
                return null;
            }
            //设置最大值
            ProgressBar.SetMaxValue(listFile.Count);
            var pra = new LoadBackInfoPra();
            pra.BackupClassId = BackupClassId;
@@ -117,32 +119,43 @@
            pra.LoginAccessToken = UserCenterLogic.GetConnectMainToken();
            //一个个的下载文件
            foreach (string fileName in listFile)
            int listFileCount = listFile.Count;
            for (int i = 0; i < listFile.Count; i++)
            {
                string fileName = listFile[i];
                //账号已经退出
                if (UserCenterLogic.IsAccountLoginOut() == true)
                if (HdlCheckLogic.Current.IsAccountLoginOut() == true)
                {
                    //关闭进度条
                    ProgressBar.Close(true);
                    ProgressFormBar.Current.Close();
                    //允许按系统的返回键
                    Shared.Common.CommonPage.BackKeyCanClick = true;
                    UserCenterResourse.AccountOption.AppCanSignout = true;
                    return null;
                }
                //★设置需要获取的文件名字★
                pra.BackupFileName = fileName;
                var result = await UserCenterLogic.GetByteResponseDataByRequestHttps("App/DownloadSomeDataBackup", true, pra);
                var result = UserCenterLogic.GetByteResponseDataByRequestHttps("App/DownloadSomeDataBackup", true, pra, null, true);
                if (result == null)
                {
                    //关闭进度条
                    ProgressBar.Close();
                    ProgressFormBar.Current.Close();
                    //允许按系统的返回键
                    Shared.Common.CommonPage.BackKeyCanClick = true;
                    UserCenterResourse.AccountOption.AppCanSignout = true;
                    return null;
                }
                //将输入写入本地的临时文件夹
                Global.WriteFileToDirectoryByBytes(newDir, fileName, result);
                //设置进度值
                ProgressBar.SetValue(1);
                ProgressFormBar.Current.SetValue(i + 1, listFileCount);
            }
            //关闭进度条
            ProgressBar.Close();
            ProgressFormBar.Current.Close();
            //允许按系统的返回键
            Shared.Common.CommonPage.BackKeyCanClick = true;
            UserCenterResourse.AccountOption.AppCanSignout = true;
            return newDir;
        }
@@ -154,7 +167,7 @@
        /// <param name="IsGatewayAutoBackup">当前获取是否为网关自动备份</param>
        /// <param name="ZigbeeUniqueId">ZigbeeUniqueId 网关的唯一Id(当IsGatewayBackup=true时,此参数必须提供</param>
        /// <returns></returns>
        private async Task<List<string>> GetBackFileIDFromDB(string BackupClassId, bool IsGatewayAutoBackup, string ZigbeeUniqueId)
        private List<string> GetBackFileIDFromDB(string BackupClassId, bool IsGatewayAutoBackup, string ZigbeeUniqueId)
        {
            var pra = new LoadBackInfoPra();
            pra.BackupClassId = BackupClassId;
@@ -166,8 +179,8 @@
            pra.LoginAccessToken = UserCenterLogic.GetConnectMainToken();
            //首先先获取云端有什么文件
            string result = await UserCenterLogic.GetResponseDataByRequestHttps("App/GetHomeDataBackupUploadListPagger", true, pra);
            if (result == null)
            string result = UserCenterLogic.GetResponseDataByRequestHttps("App/GetHomeDataBackupUploadListPagger", true, pra);
            if (string.IsNullOrEmpty(result) == true)
            {
                return null;
            }
@@ -193,7 +206,7 @@
        /// <param name="ManualBackupType">手动备份类型(0:App手动备份,2:网关手动备份)</param>
        /// <param name="ZigbeeUniqueId">网关的唯一Id,当ManualBackupType=2时,此值不能为空!</param>
        /// <returns></returns>
        public async Task<string> CreatNewBackupNameToDB(string backupName, int ManualBackupType = 0, string ZigbeeUniqueId = null)
        public string CreatNewBackupNameToDB(string backupName, int ManualBackupType = 0, string ZigbeeUniqueId = null)
        {
            var pra = new AddBackUpNamePra();
            pra.BackupName = backupName;
@@ -202,8 +215,8 @@
            //获取控制主人账号的Token
            pra.LoginAccessToken = UserCenterLogic.GetConnectMainToken();
            var result = await UserCenterLogic.GetResponseDataByRequestHttps("App/AddHomeAppGatewayName", true, pra);
            if (result == null)
            var result = UserCenterLogic.GetResponseDataByRequestHttps("App/AddHomeAppGatewayName", true, pra);
            if (string.IsNullOrEmpty(result) == true)
            {
                return null;
            }
@@ -222,8 +235,9 @@
        /// <param name="BackupClassId">备份主键ID</param>
        /// <param name="ManualBackupType">手动备份类型(0:App手动备份,2:网关手动备份)</param>
        /// <param name="upPath">指定上传的是哪个文件夹的文件(全路径),不指定时,上传的是本地路径下的文件</param>
        /// <param name="showBar">是否设置显示进度条</param>
        /// <returns></returns>
        public async Task<bool> UpLoadBackupFileToDB(string BackupClassId, int ManualBackupType = 0, string upPath = "")
        public bool UpLoadBackupFileToDB(string BackupClassId, int ManualBackupType = 0, string upPath = "", bool showBar = true)
        {
            List<string> listAllFile = null;
            //文件夹的全路径
@@ -261,18 +275,31 @@
            }
            //开启进度条
            ProgressBar.SetMaxValue(listAllFile.Count);
            ProgressBar.Show();
            int listFileCount = listAllFile.Count;
            if (showBar == true)
            {
                //开启进度条 正在上传备份文件
                ProgressFormBar.Current.Start();
                ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uBackupFileUploading));
            }
            //不允许按系统的返回键
            Shared.Common.CommonPage.BackKeyCanClick = false;
            UserCenterResourse.AccountOption.AppCanSignout = false;
            //你说为什么这里不分一个函数出来(理论上是分函数出来的)
            //是因为如果分函数的话,会出现双重异步,导致主线程的进度条的百分比永远定死0%
            foreach (string file in listNormalFile)
            for (int i = 0; i < listNormalFile.Count; i++)
            {
                string file = listNormalFile[i];
                //账号已经退出
                if (UserCenterLogic.IsAccountLoginOut() == true)
                if (HdlCheckLogic.Current.IsAccountLoginOut() == true)
                {
                    //关闭进度条
                    ProgressBar.Close(true);
                    ProgressFormBar.Current.Close();
                    //允许按系统的返回键
                    Shared.Common.CommonPage.BackKeyCanClick = true;
                    UserCenterResourse.AccountOption.AppCanSignout = true;
                    return false;
                }
                //判断是否是应该上传的文件
@@ -290,24 +317,31 @@
                list.Add(datainfo);
                //执行上传
                bool falge = await DoUpLoadInfoToDB(BackupClassId, list, ManualBackupType);
                bool falge = DoUpLoadInfoToDB(BackupClassId, list, ManualBackupType);
                if (falge == false)
                {
                    //关闭进度条
                    ProgressBar.Close();
                    ProgressFormBar.Current.Close();
                    //允许按系统的返回键
                    Shared.Common.CommonPage.BackKeyCanClick = true;
                    UserCenterResourse.AccountOption.AppCanSignout = true;
                    return false;
                }
                //设置进度值
                ProgressBar.SetValue(list.Count);
                ProgressFormBar.Current.SetValue(i + 1, listFileCount);
            }
            foreach (string file in listPicFile)
            for (int i = 0; i < listPicFile.Count; i++)
            {
                string file = listPicFile[i];
                //账号已经退出
                if (UserCenterLogic.IsAccountLoginOut() == true)
                if (HdlCheckLogic.Current.IsAccountLoginOut() == true)
                {
                    //关闭进度条
                    ProgressBar.Close(true);
                    ProgressFormBar.Current.Close();
                    //允许按系统的返回键
                    Shared.Common.CommonPage.BackKeyCanClick = true;
                    UserCenterResourse.AccountOption.AppCanSignout = true;
                    return false;
                }
@@ -319,14 +353,21 @@
                if (result == false)
                {
                    //关闭进度条
                    ProgressBar.Close();
                    ProgressFormBar.Current.Close();
                    //允许按系统的返回键
                    Shared.Common.CommonPage.BackKeyCanClick = true;
                    UserCenterResourse.AccountOption.AppCanSignout = true;
                    return false;
                }
                //设置进度值
                ProgressBar.SetValue(listPic.Count);
                ProgressFormBar.Current.SetValue(i + 1 + listNormalFile.Count, listFileCount);
            }
            //进度条关闭
            ProgressBar.Close();
            ProgressFormBar.Current.Close();
            //允许按系统的返回键
            Shared.Common.CommonPage.BackKeyCanClick = true;
            UserCenterResourse.AccountOption.AppCanSignout = true;
            return true;
        }
@@ -338,7 +379,7 @@
        /// <param name="listData">上传的数据</param>
        /// <param name="ManualBackupType">手动备份类型(0:App手动备份,2:网关手动备份)</param>
        /// <returns></returns>
        private async Task<bool> DoUpLoadInfoToDB(string BackupClassId, List<FileInfoData> listData, int ManualBackupType = 0)
        private bool DoUpLoadInfoToDB(string BackupClassId, List<FileInfoData> listData, int ManualBackupType = 0)
        {
            var upData = new UpLoadDataPra();
            upData.ManualBackupType = ManualBackupType;
@@ -347,7 +388,7 @@
            //获取控制主人账号的Token
            upData.LoginAccessToken = UserCenterLogic.GetConnectMainToken();
            bool falge = await UserCenterLogic.GetResultStatuByRequestHttps("App/UploadHomeAppGatewaySubFiles", true, upData);
            bool falge = UserCenterLogic.GetResultStatuByRequestHttps("App/UploadHomeAppGatewaySubFiles", true, upData, null, true);
            if (falge == false)
            {
                return false;
@@ -375,14 +416,11 @@
                nvc.Add("HomeId", Shared.Common.Config.Instance.Home.Id);
                nvc.Add("BackupClassId", BackupClassId);
                nvc.Add("ManualBackupType", ManualBackupType.ToString());
                nvc.Add("IsOtherAccountCtrl", Common.Config.Instance.isAdministrator.ToString());
                nvc.Add("IsOtherAccountCtrl", Common.Config.Instance.Home.IsOthreShare.ToString());
                for (int i = 0; i < listPicFile.Count; i++)
                {
                    string base64 = GetBase64Text(fullName);
                    nvc.Add("UploadSubFileLists[" + i + "].BackupFileName", fileName);
                    nvc.Add("UploadSubFileLists[" + i + "].BackupFileContent", base64);
                }
                string base64 = GetBase64Text(fullName);
                nvc.Add("UploadSubFileLists[0].BackupFileName", fileName);
                nvc.Add("UploadSubFileLists[0].BackupFileContent", base64);
                bool result = UpLoadBigBackupFileToDB("App/UploadHomeAppGatewaySubFiles", fullName, nvc);
                if (result == false)
@@ -406,7 +444,7 @@
            string contentType = "application/octet-stream";
            string requestUrl = string.Empty;
            //启用管理员权限
            if (Config.Instance.isAdministrator == true)
            if (Config.Instance.Home.IsOthreShare == true)
            {
                requestUrl = $"{Config.Instance.AdminRequestBaseUrl}/{RequestName}";
            }
@@ -483,20 +521,188 @@
        #endregion
        #region ■ 上传Log备份________________________
        /// <summary>
        /// 上传Log备份(隐匿功能)
        /// </summary>
        /// <returns></returns>
        public bool UpLoadLogBackup()
        {
            string upPath = UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.LogDirectory);
            if (HdlAutoBackupLogic.GetFileFromDirectory(upPath).Count == 0)
            {
                //没有Log文件
                return true;
            }
            ProgressFormBar.Current.Start();
            ProgressFormBar.Current.SetMsg("正在上传Log文件");
            //从云端获取数据
            var pageData = HdlBackupLogic.Current.GetBackupListNameFromDB(0, null, true);
            if (pageData == null)
            {
                ProgressFormBar.Current.Close();
                return false;
            }
            string backId = string.Empty;
            for (int i = 0; i < pageData.Count; i++)
            {
                if (pageData[i].BackupName == DirNameResourse.OptionBackupName)
                {
                    //获取功能备份的ID
                    backId = pageData[i].Id;
                    break;
                }
            }
            if (backId == string.Empty)
            {
                //创建新的备份
                backId = HdlBackupLogic.Current.CreatNewBackupNameToDB(DirNameResourse.OptionBackupName);
                if (backId == null)
                {
                    ProgressFormBar.Current.Close();
                    return false;
                }
            }
            //上传Log文件
            bool result = this.UpLoadBackupFileToDB(backId, 0, upPath, false);
            if (result == true)
            {
                try
                {
                    var listAllFile = HdlAutoBackupLogic.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;
        }
        #endregion
        #region ■ 读取隐匿功能配置___________________
        /// <summary>
        /// 读取隐匿功能配置(不要在意返回值)
        /// </summary>
        /// <returns></returns>
        public bool LoadHideOption()
        {
            //先初始化
            UserCenterResourse.HideOption = new HideOptionInfo();
            if (UserCenterResourse.UserInfo.AuthorityNo != 1 && UserCenterResourse.UserInfo.AuthorityNo != 2)
            {
                return true;
            }
            //无视错误
            IMessageCommon.Current.IgnoreError = true;
            //从云端获取数据
            var pageData = this.GetBackupListNameFromDB(0, null, true);
            if (pageData == null)
            {
                //恢复
                IMessageCommon.Current.IgnoreError = false;
                return false;
            }
            string backId = string.Empty;
            for (int i = 0; i < pageData.Count; i++)
            {
                if (pageData[i].BackupName == DirNameResourse.OptionBackupName)
                {
                    //获取功能备份的ID
                    backId = pageData[i].Id;
                    break;
                }
            }
            if (backId == string.Empty)
            {
                //没有功能配置
                IMessageCommon.Current.IgnoreError = false;
                return true;
            }
            //获取这个备份下面有多少个文件
            List<string> listFile = GetBackFileIDFromDB(backId, false, null);
            if (listFile == null)
            {
                //恢复
                IMessageCommon.Current.IgnoreError = false;
                return false;
            }
            if (listFile.Count == 0)
            {
                //恢复
                IMessageCommon.Current.IgnoreError = false;
                return true;
            }
            //检测
            string checkKeys = UserCenterLogic.EncryptPassword(UserCenterResourse.FileEncryptKey, DirNameResourse.ShowOptionMenuFile + UserCenterResourse.UserInfo.Account);
            if (listFile.Contains(checkKeys) == true)
            {
                //显示主页隐藏菜单(Debug用)
                UserCenterResourse.HideOption.CenterHideMenu = 1;
            }
            checkKeys = UserCenterLogic.EncryptPassword(UserCenterResourse.FileEncryptKey, DirNameResourse.DetailedLogFile + UserCenterResourse.UserInfo.Account);
            if (listFile.Contains(checkKeys) == true)
            {
                //出力详细Log(Debug用)
                UserCenterResourse.HideOption.DetailedLog = 1;
            }
            checkKeys = UserCenterLogic.EncryptPassword(UserCenterResourse.FileEncryptKey, DirNameResourse.DeviceHistoryFile + UserCenterResourse.UserInfo.Account);
            if (listFile.Contains(checkKeys) == true)
            {
                //显示设备历史版本(Debug用)
                UserCenterResourse.HideOption.DeviceHistory = 1;
            }
            //恢复
            IMessageCommon.Current.IgnoreError = false;
            return true;
        }
        #endregion
        #region ■ 读取备份___________________________
        /// <summary>
        /// 读取APP备份文档(函数内部有进度条)
        /// </summary>
        /// <param name="BackupClassId"></param>
        public async Task<bool> LoadAppBackupInfo(string BackupClassId)
        public bool LoadAppBackupInfo(string BackupClassId)
        {
            //打开进度条 正在获取备份文件列表
            ProgressFormBar.Current.Start();
            ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uBackupFileListGetting));
            //从云端获取备份的文件,然后存入本地的临时文件夹
            string tempDirectory = await GetBackFileFromDBAndSetToLocation(BackupClassId);
            string tempDirectory = GetBackFileFromDBAndSetToLocation(BackupClassId);
            if (tempDirectory == null)
            {
                //关闭进度条
                ProgressFormBar.Current.Close();
                return false;
            }
            //关闭进度条
            ProgressFormBar.Current.Close();
            //如果读取到的文件完全没有问题,则清理本地的文件
            UserCenterLogic.DeleteAllLocationFile(false);
@@ -513,30 +719,6 @@
            return true;
        }
        /// <summary>
        /// 读取网关备份文档(函数内部有进度条)
        /// </summary>
        /// <param name="BackupClassId"></param>
        /// <param name="zbGateway">网关对象(请传入真实物理网关对象)</param>
        /// <param name="listGatewayFile">网关现有的文件名</param>
        public async Task<bool> LoadGatewayBackupInfo(string BackupClassId, ZigBee.Device.ZbGateway zbGateway, List<string> listGatewayFile)
        {
            //添加附加情报:获取备份数据
            ProgressBar.SetAppendText(Language.StringByID(R.MyInternationalizationString.uGetBackupData));
            //从云端获取备份的文件,然后存入本地的临时文件夹
            string tempDirectory = await GetBackFileFromDBAndSetToLocation(BackupClassId, false, HdlGatewayLogic.Current.GetGatewayId(zbGateway));
            //清空附加情报
            ProgressBar.SetAppendText(string.Empty);
            if (tempDirectory == null)
            {
                return false;
            }
            //恢复网关配置
            return await HdlGatewayLogic.Current.RecoverGatewaySettion(zbGateway, listGatewayFile, tempDirectory);
        }
        #endregion
        #region ■ 删除备份___________________________
@@ -546,14 +728,14 @@
        /// </summary>
        /// <param name="BackupClassId">备份的主键</param>
        /// <returns></returns>
        public async Task<bool> DeleteDbBackupData(string BackupClassId)
        public bool DeleteDbBackupData(string BackupClassId)
        {
            var Pra = new LoadBackInfoPra();
            Pra.BackupClassId = BackupClassId;
            //获取控制主人账号的Token
            Pra.LoginAccessToken = UserCenterLogic.GetConnectMainToken();
            bool success = await UserCenterLogic.GetResultStatuByRequestHttps("App/DelHomeAppGatewayName", true, Pra);
            bool success = UserCenterLogic.GetResultStatuByRequestHttps("App/DelHomeAppGatewayName", true, Pra);
            return success;
        }
@@ -584,7 +766,7 @@
        /// <returns></returns>
        private string GetBase64Text(string fileName)
        {
            string path = UserCenterResourse.LocalRootPath;
            string path = Config.Instance.FullPath;
            string fullName = System.IO.Path.Combine(path, fileName);
            FileStream fs = File.OpenRead(fullName);