JLChen
2021-01-05 f500e14c0a994487070380c50c85e0929cbc8e63
Crabtree/SmartHome/UI/SimpleControl/Phone/Register/Databackup.cs
@@ -73,7 +73,7 @@
                    var btnPoint = new Button () {
                        Width = Application.GetRealWidth (10),
                        Height = Application.GetRealHeight (10),
                        Height = Application.GetRealWidth (10),
                        X = Application.GetRealWidth (50),
                        Gravity = Gravity.CenterVertical,
                        UnSelectedImagePath = "Item/Point.png",
@@ -390,13 +390,18 @@
                alert.Show ();
            };
            if (MainPage.LoginUser.AccountType != 1) {
            //if (MainPage.LoginUser.AccountType != 1) {
            //    rowView.AddRightView (btnDelFile);
            //}
            //2020-07-01 当前住宅属于自己的 允许删除备份
            if (!MainPage.IsAdministrator) {
                rowView.AddRightView (btnDelFile);
            }
            var btnPoint = new Button () {
                Width = Application.GetRealWidth (10),
                Height = Application.GetRealHeight (10),
                Height = Application.GetRealWidth (10),
                X = Application.GetRealWidth (25),
                Gravity = Gravity.CenterVertical,
                UnSelectedImagePath = "Item/Point.png",
@@ -434,8 +439,8 @@
            rowView.AddChidren (backupIcon);
            var Backup_restores = new Button () {
                Width = Application.GetRealWidth (56),
                Height = Application.GetRealHeight (57),
                Width = Application.GetMinRealAverage (56),
                Height = Application.GetMinRealAverage (57),
                X = backupIcon.Right + Application.GetRealWidth (20),
                UnSelectedImagePath = "Register/Backup_ restores.png",
                SelectedImagePath = "Register/Backup_ restores_on.png",
@@ -885,7 +890,7 @@
            mDownLoad.BackupFileName = file.BackupFileName;
            //var requestObj3 = new BackupDetailObj () { Id = file.Id };
            var requestJson3 = Newtonsoft.Json.JsonConvert.SerializeObject (mDownLoad);
            var revertObj3 = MainPage.RequestHttpsReturnByte (API.DownloadSomeDataBackup, requestJson3, false, mUrlHead);
            var revertObj3 = MainPage.RequestHttpsReturnByteNew (API.DownloadSomeDataBackup, requestJson3, mUrlHead);
            if (revertObj3 != null) {
                IO.FileUtils.WriteFileByBytes (file.BackupFileName, revertObj3);
@@ -911,6 +916,9 @@
        }
        private readonly object SendLocker = new object ();
        private readonly object DownloadLocker = new object ();
        /// <summary>
        /// 多线程下载
        /// </summary>
@@ -920,7 +928,7 @@
            int index = 0;
            int indexCount = 0;
            var mFileList = dataList.FindAll ((obj) => obj.BackupFileName != "null" && obj.BackupFileName != UserConfig.configFile && obj.BackupFileName != UserInfo.GlobalRegisterFile && obj.BackupFileName != "AccountListDB");
            var mFileList = dataList.FindAll ((obj) => obj.BackupFileName != "null" && obj.BackupFileName != UserConfig.configFile && obj.BackupFileName != UserInfo.GlobalRegisterFile && obj.BackupFileName != "AccountListDB" && obj.BackupFileName != CommonConfig.ConfigFile);
            if (maxThreads > mFileList.Count) {
                maxThreads = mFileList.Count;
            }
@@ -949,16 +957,44 @@
                new System.Threading.Thread (() => {
                    for (var j = startIndex1; j <= endIndex1; j++) {
                        var result = DownloadSomeDataBackup (mDownLoad, mUrlHead, mFileList [j]);
                        if (result) {
                            index++;
                            Application.RunOnMainThread (() => {
                                int pro = (int)(index * 1.0 / mFileList.Count * 100);
                                MainPage.Loading.Text = pro.ToString () + "%";
                            });
                        try {
                            //如果失败重新下载,重复3次
                            int errorCount = 0;
                            while (errorCount < 3) {
                                var result = DownloadSomeDataBackup (mDownLoad, mUrlHead, mFileList [j]);
                                if (result) {
                                    lock (DownloadLocker) {
                                        index++;
                                    }
                                    Application.RunOnMainThread (() => {
                                        int pro = (int)(index * 1.0 / mFileList.Count * 100);
                                        MainPage.Loading.Text = pro.ToString () + "%";
                                    });
                                    errorCount = 3;
                                } else {
                                    errorCount++;
                                    Utlis.WriteLine ($"下载失败: {mFileList [j].BackupFileName} 次数:{errorCount}");
                                }
                            }
                        } catch { } finally {
                            lock (SendLocker) {
                                indexCount++;
                            }
                        }
                        indexCount++;
                        //var t1 = DateTime.Now.Second;
                        //var result = DownloadSomeDataBackup (mDownLoad, mUrlHead, mFileList [j]);
                        //if (result) {
                        //    index++;
                        //    Application.RunOnMainThread (() => {
                        //        int pro = (int)(index * 1.0 / mFileList.Count * 100);
                        //        MainPage.Loading.Text = pro.ToString () + "%";
                        //    });
                        //} else {
                        //    Utlis.WriteLine ($"下载失败: {mFileList [j].BackupFileName} 时间:{DateTime.Now.Second - t1}S");
                        //}
                        //indexCount++;
                        //Utlis.WriteLine ($"下载线程:{id}  完成index:{j}  indexCount: {indexCount}");
                    }
@@ -1047,7 +1083,7 @@
        void UpLoadBackupFileToDBNew (string mBackupClassId, int maxThreads = 20)
        {
            var backuplist = IO.FileUtils.ReadFiles ();
            var mFileList = backuplist.FindAll ((obj) => obj != "null" && obj != UserConfig.configFile && obj != UserInfo.GlobalRegisterFile && obj != "AccountListDB");
            var mFileList = backuplist.FindAll ((obj) => obj != "null" && obj != UserConfig.configFile && obj != UserInfo.GlobalRegisterFile && obj != "AccountListDB" && obj != CommonConfig.ConfigFile);
            if (mFileList.Count <= 0) return;
            if (maxThreads > mFileList.Count) {
@@ -1155,14 +1191,30 @@
                UploadHomeAppGatewaySubFilesObj mUpData = upData;
                mUpData.UploadSubFileLists = list;
                var requestJson2 = Newtonsoft.Json.JsonConvert.SerializeObject (mUpData);
                var revertObj2 = MainPage.RequestHttps (API.UploadHomeAppGatewaySubFiles, requestJson2, mUrlHead);
                if (revertObj2.StateCode.ToUpper () == "SUCCESS") {
                    return true;
                } else {
                    //提示错误
                    return false;
                //var requestJson2 = Newtonsoft.Json.JsonConvert.SerializeObject (mUpData);
                //var revertObj2 = MainPage.RequestHttps (API.UploadHomeAppGatewaySubFiles, requestJson2, mUrlHead);
                //if (revertObj2.StateCode.ToUpper () == "SUCCESS") {
                //    return true;
                //} else {
                //    //提示错误
                //    return false;
                //}
                //2020-11-16 如果失败重新上传,重复3次
                int errorCount = 0;
                var result = false;
                while (errorCount < 3) {
                    var requestJson2 = Newtonsoft.Json.JsonConvert.SerializeObject (mUpData);
                    var revertObj2 = MainPage.RequestHttps (API.UploadHomeAppGatewaySubFiles, requestJson2, mUrlHead, MainPage.TIME_OUT_20);
                    if (revertObj2.StateCode.ToUpper () == "SUCCESS") {
                        result = true;
                        errorCount = 3;
                    } else {
                        errorCount++;
                        //Utlis.WriteLine ($"上传失败({startIndex1}-{endIndex1}),次数:{errorCount}");
                    }
                }
                return result;
            } catch {
                return false;
            }