From 5a5a2b696866f947b6025d26c3302e8ffef46435 Mon Sep 17 00:00:00 2001
From: JLChen <551775569@qq.com>
Date: 星期一, 11 一月 2021 16:11:20 +0800
Subject: [PATCH] 2021-01-11 1.更新MQTT连接和本地搜索网关方法。2.子账号相关接口完善

---
 Crabtree/SmartHome/UI/SimpleControl/Phone/Register/Databackup.cs |  953 +++++++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 790 insertions(+), 163 deletions(-)

diff --git a/Crabtree/SmartHome/UI/SimpleControl/Phone/Register/Databackup.cs b/Crabtree/SmartHome/UI/SimpleControl/Phone/Register/Databackup.cs
index 22d7e54..29af1dc 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/Phone/Register/Databackup.cs
+++ b/Crabtree/SmartHome/UI/SimpleControl/Phone/Register/Databackup.cs
@@ -32,7 +32,7 @@
                 X = Application.GetRealWidth (90),
                 Width = Application.GetRealWidth (640 - 180),
                 TextAlignment = TextAlignment.Center,
-                Text = UserConfig.Instance.CurrentRegion.RegionName + " " + Language.StringByID (R.MyInternationalizationString.Backup),
+                Text = UserConfig.Instance.CurrentRegion.Name + " " + Language.StringByID (R.MyInternationalizationString.Backup),
                 TextSize = 19,
                 TextColor = SkinStyle.Current.TextColor1,
             };
@@ -73,25 +73,25 @@
 
                     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",
                         SelectedImagePath = "Item/Point.png",
-                        Visible = homeTemp.RegionID != UserConfig.Instance.CurrentRegion.RegionID
+                        Visible = homeTemp.Id != UserConfig.Instance.CurrentRegion.Id
                     };
                     rowView.AddChidren (btnPoint);
 
                     var btnName = new Button () {
                         Width = Application.GetRealWidth (550),
-                        Text = homeTemp.RegionName,
+                        Text = homeTemp.Name,
                         TextAlignment = TextAlignment.CenterLeft,
                         X = Application.GetRealWidth (90),
                         TextColor = SkinStyle.Current.TextColor1,
                     };
                     rowView.AddChidren (btnName);
 
-                    if (homeTemp.RegionID == UserConfig.Instance.CurrentRegion.RegionID) {
+                    if (homeTemp.Id == UserConfig.Instance.CurrentRegion.Id) {
                         Button btnCheck = new Button () {
                             X = Application.GetRealWidth (20),
                             Y = Application.GetRealHeight (25),
@@ -112,13 +112,13 @@
                             try {
                                 var backuplist = IO.FileUtils.ReadFiles ();
                                 int index = 0;
-                                string oldRegionRootPath = IO.FileUtils.CreateRegionBackup (UserConfig.Instance.CurrentRegion.RegionName);
+                                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);
-                                        Console.WriteLine ("move file : " + fileName);
+                                        Utlis.WriteLine ("move file : " + fileName);
                                     }
                                     index++;
                                     Application.RunOnMainThread (() => {
@@ -127,7 +127,7 @@
                                     });
                                 }
                                 IO.FileUtils.DeleteAllFile ();
-                                string newRegionRootPath = IO.FileUtils.CreateRegionBackup (homeTemp.RegionName);
+                                string newRegionRootPath = IO.FileUtils.CreateRegionBackup (homeTemp.Id);
                                 IO.FileUtils.RestoreRegionFiles (newRegionRootPath);
                                 UserConfig.Instance.RefreshUserConfig ();
                                 UserConfig.Instance.CurrentRegion = homeTemp;
@@ -140,14 +140,14 @@
                                     new Alert ("", Language.StringByID (R.MyInternationalizationString.SwitchRegionSuccessfully),
                                               Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                                     this.DatabackupShow ();
-                                    EquipmentPublicClass.CheckLinkRemote (2);
+                                    EquipmentPublicClass.CheckLinkRemote (UserConfig.Instance.internetStatus);
                                 });
                             } catch (Exception ex) {
                                 Application.RunOnMainThread (() => {
                                     new Alert ("", Language.StringByID (R.MyInternationalizationString.CheckInternet),
                                                Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                                 });
-                                Console.WriteLine (ex.Message);
+                                Utlis.WriteLine (ex.Message);
                             } finally {
                                 Application.RunOnMainThread (() => {
                                     MainPage.Loading.Hide ();
@@ -184,8 +184,8 @@
                 };
                 dialog.AddChidren (dialogBodyView);
 
-                Button btnTitle = new Button () { 
-                    Height = Application.GetRealHeight(80),
+                Button btnTitle = new Button () {
+                    Height = Application.GetRealHeight (80),
                     BackgroundColor = SkinStyle.Current.DialogTitle,
                     TextAlignment = TextAlignment.Center,
                     TextID = R.MyInternationalizationString.Backup,
@@ -206,7 +206,7 @@
 
                 EditText etBackupRemark = new EditText () {
                     Gravity = Gravity.CenterHorizontal,
-                    Y = btnackupRemark.Bottom ,
+                    Y = btnackupRemark.Bottom,
                     Width = Application.GetRealWidth (400),
                     Height = Application.GetRealHeight (80),
                     TextAlignment = TextAlignment.Center,
@@ -219,10 +219,10 @@
                 etBackupRemark.EditorEnterAction += (obj) => {
                     Application.HideSoftInput ();
                 };
-                
-                FrameLayout bottomView = new FrameLayout () { 
-                    Y = Application.GetRealHeight(420),
-                    Height = Application.GetRealHeight(85),
+
+                FrameLayout bottomView = new FrameLayout () {
+                    Y = Application.GetRealHeight (420),
+                    Height = Application.GetRealHeight (85),
                     BackgroundColor = SkinStyle.Current.DialogTitle
                 };
                 dialogBodyView.AddChidren (bottomView);
@@ -237,16 +237,16 @@
                     dialog.Close ();
                 };
 
-                Button btnBottomLine = new Button () { 
+                Button btnBottomLine = new Button () {
                     X = btnClose.Right,
-                    Width =1,
+                    Width = 1,
                     BackgroundColor = SkinStyle.Current.Black50Transparent,
                 };
                 bottomView.AddChidren (btnBottomLine);
 
-                Button btnSave = new Button () { 
+                Button btnSave = new Button () {
                     X = btnBottomLine.Right,
-                    Width = Application.GetRealWidth(249),
+                    Width = Application.GetRealWidth (249),
                     TextID = R.MyInternationalizationString.SAVE,
                     TextAlignment = TextAlignment.Center
                 };
@@ -298,17 +298,51 @@
                     Application.RunOnMainThread (() => {
                         MainPage.Loading.Start ("Please wait...");
                     });
-                    GetUserFolderObj requestObj = new GetUserFolderObj () { LevelID = UserConfig.Instance.CurrentRegion.RegionID };
+                    var requestObj = new GetHomeDataBackupPaggerObj () {
+                        HomeId = UserConfig.Instance.CurrentRegion.Id,
+                        LoginAccessToken = MainPage.LoginUser.LoginTokenString
+                    };
+                    string urlHead = MainPage.RequestHttpsHost;
+                    if (requestObj.IsOtherAccountCtrl) {
+                        urlHead = UserConfig.Instance.MasterAccountRequestBaseUrl;
+                        requestObj.LoginAccessToken = UserConfig.Instance.MasterAccountToken;
+                    }
+
+                    //GetUserFolderObj requestObj = new GetUserFolderObj () { LevelID = UserConfig.Instance.CurrentRegion.Id };
                     var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj);
-                    var revertObj = MainPage.RequestHttps ("GetUserFolder", requestJson,true,false);
-                    if (revertObj.StateCode == "SUCCESS") {
-                        var responseDataObj = Newtonsoft.Json.JsonConvert.DeserializeObject<List<FolderRes>> (revertObj.ResponseData.ToString ());
+                    var revertObj = MainPage.RequestHttps (API.GetHomeDataBackupPagger, requestJson, urlHead);
+                    if (revertObj.StateCode.ToUpper () == StateCode.SUCCESS) {
+                        var data = Newtonsoft.Json.JsonConvert.DeserializeObject<GetBackupListResult> (revertObj.ResponseData.ToString ());
+                        //var list = new List<BackupListNameInfo> ();
+                        //foreach (var data2 in data.PageData) {
+                        //    if (ZigbeeUniqueId != null && data2.GatewayUniqueId != ZigbeeUniqueId) {
+                        //        continue;
+                        //    }
+                        //    if (getLogBackup == false && data2.BackupName == LogBackupName) {
+                        //        //涓嶈幏鍙朙og澶囦唤
+                        //        continue;
+                        //    }
+                        //    list.Add (data2);
+                        //}
+
+
+                        //var responseDataObj = Newtonsoft.Json.JsonConvert.DeserializeObject<List<FolderRes>> (revertObj.ResponseData.ToString ());
+                        if (data == null) return;
+
                         Application.RunOnMainThread (() => {
-                            foreach(var folder in responseDataObj)
-                            {
-                                addRow (folder.FolderName,folder.FolderID);
+                            foreach (var folder in data.PageData) {
+                                addRow (folder.BackupName, folder.Id);
                             }
                         });
+                    } else {
+                        //2020-01-11 鍒锋柊涓�娆asterToken
+                        if (requestObj.IsOtherAccountCtrl) {
+                            //if (revertObj.StateCode == "ParameterOrEmpty" || revertObj.StateCode == "NoLogin" || revertObj.StateCode == "HomeIdAndTokenNoConsistent") {
+                            if (revertObj.StateCode != ErrorCode.NetworkError) {
+                                UserConfig.Instance.CheckIfNeedGetMasterAccountToken ();
+                            }
+                        }
+                        MainPage.ShowAlertOnMainThread ("Failed to get backup data list!");
                     }
                     //var groupNames = service_backup.GetUserGroup (MainPage.LoginUser.MasterID, 0);
                     //Application.RunOnMainThread (() => {
@@ -318,7 +352,7 @@
                     //});
                 } catch {
                     Application.RunOnMainThread (() => {
-                        new Alert ("", Language.StringByID (R.MyInternationalizationString.CheckInternet),Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+                        new Alert ("", Language.StringByID (R.MyInternationalizationString.CheckInternet), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                     });
                 } finally {
                     Application.RunOnMainThread (() => {
@@ -328,7 +362,7 @@
             });
         }
 
-        void addRow (string groupName,int folderID)
+        void addRow (string groupName, string folderID)
         {
             var rowView = new RowLayout () {
                 Height = Application.GetRealHeight (110),
@@ -348,37 +382,26 @@
             };
             btnDelFile.MouseUpEventHandler += (sender, e) => {
                 Alert alert = new Alert ("", Language.StringByID (R.MyInternationalizationString.AreYouSureToDeleteFile), Language.StringByID (R.MyInternationalizationString.Cancel), Language.StringByID (R.MyInternationalizationString.Confrim));
-                alert.ResultEventHandler += ( sender2,  e2) => {
+                alert.ResultEventHandler += (sender2, e2) => {
                     if (e2) {
-                        MainPage.Loading.Start ("Please wait...");
-                        System.Threading.Tasks.Task.Run (() => {
-                            try {
-                                DeleteFolderDataObj requestObj = new DeleteFolderDataObj () { Id = folderID };
-                                var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj);
-                                var revertObj = MainPage.RequestHttps ("DeleteFolderData", requestJson);
-                                if (revertObj.StateCode == "SUCCESS") {
-                                    Application.RunOnMainThread (() => {
-                                        DatabackupShow ();
-                                    });
-                                }
-                            } catch { } finally {
-                                Application.RunOnMainThread (() => {
-                                    MainPage.Loading.Hide ();
-                                });
-                            }
-                        });
+                        DeleteFolderData (folderID);
                     }
                 };
                 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",
@@ -398,25 +421,9 @@
 
             btnName.MouseLongEventHandler += (sender, e) => {
                 Alert alert = new Alert ("", Language.StringByID (R.MyInternationalizationString.AreYouSureToDeleteFile), Language.StringByID (R.MyInternationalizationString.Cancel), Language.StringByID (R.MyInternationalizationString.Confrim));
-                alert.ResultEventHandler += ( sender2,  e2) => {
+                alert.ResultEventHandler += (sender2, e2) => {
                     if (e2) {
-                        MainPage.Loading.Start ("Please wait...");
-                        System.Threading.Tasks.Task.Run (() => {
-                            try {
-                                DeleteFolderDataObj requestObj = new DeleteFolderDataObj () { Id = folderID };
-                                var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj);
-                                var revertObj = MainPage.RequestHttps ("DeleteFolderData", requestJson);
-                                if (revertObj.StateCode == "SUCCESS") {
-                                    Application.RunOnMainThread (() => {
-                                        DatabackupShow ();
-                                    });
-                                }
-                            } catch { } finally {
-                                Application.RunOnMainThread (() => {
-                                    MainPage.Loading.Hide ();
-                                });
-                            }
-                        });
+                        DeleteFolderData (folderID);
                     }
                 };
                 alert.Show ();
@@ -432,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",
@@ -450,6 +457,122 @@
         }
 
         /// <summary>
+        /// 鍒犻櫎鏌愪釜澶囦唤
+        /// </summary>
+        /// <param name="folderID"></param>
+        void DeleteFolderData (string folderID)
+        {
+            MainPage.Loading.Start ("Please wait...");
+            System.Threading.Tasks.Task.Run (() => {
+                try {
+                    var requestObj = new DelHomeAppGatewayNameObj () {
+                        BackupClassId = folderID,
+                        HomeId = UserConfig.Instance.CurrentRegion.Id,
+                        LoginAccessToken = MainPage.LoginUser.LoginTokenString
+                    };
+                    string urlHead = MainPage.RequestHttpsHost;
+                    if (requestObj.IsOtherAccountCtrl) {
+                        urlHead = UserConfig.Instance.MasterAccountRequestBaseUrl;
+                        requestObj.LoginAccessToken = UserConfig.Instance.MasterAccountToken;
+                    }
+
+                    //DeleteFolderDataObj requestObj = new DeleteFolderDataObj () { Id = folderID };
+                    var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj);
+                    var revertObj = MainPage.RequestHttps (API.DelHomeAppGatewayName, requestJson, urlHead);
+                    if (revertObj.StateCode.ToUpper () == StateCode.SUCCESS) {
+                        Application.RunOnMainThread (() => {
+                            DatabackupShow ();
+                        });
+                    } else {
+                        ShowDelErrorInfo (revertObj.StateCode);
+                    }
+                } catch { } finally {
+                    Application.RunOnMainThread (() => {
+                        MainPage.Loading.Hide ();
+                    });
+                }
+            });
+
+        }
+
+        //2020-01-11
+        /// <summary>
+        /// 鍒犻櫎鏌愪釜澶囦唤
+        /// </summary>
+        /// <param name="folderID"></param>
+        void DeleteFolderDataAfterUploadFailed (string folderID)
+        {
+            Application.RunOnMainThread (() => {
+                MainPage.Loading.Start ("Please wait...");
+            });
+
+
+            try {
+                var requestObj = new DelHomeAppGatewayNameObj () {
+                    BackupClassId = folderID,
+                    HomeId = UserConfig.Instance.CurrentRegion.Id,
+                    LoginAccessToken = MainPage.LoginUser.LoginTokenString
+                };
+                string urlHead = MainPage.RequestHttpsHost;
+                if (requestObj.IsOtherAccountCtrl) {
+                    urlHead = UserConfig.Instance.MasterAccountRequestBaseUrl;
+                    requestObj.LoginAccessToken = UserConfig.Instance.MasterAccountToken;
+                }
+
+                //DeleteFolderDataObj requestObj = new DeleteFolderDataObj () { Id = folderID };
+                var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj);
+                var revertObj = MainPage.RequestHttps (API.DelHomeAppGatewayName, requestJson, urlHead);
+                if (revertObj.StateCode.ToUpper () == StateCode.SUCCESS) {
+                    //鍒犻櫎鎴愬姛
+                } else {
+                    //鍒犻櫎澶辫触
+                }
+            } catch { } finally {
+                Application.RunOnMainThread (() => {
+                    MainPage.Loading.Hide ();
+                });
+            }
+
+
+        }
+
+        /// <summary>
+        /// (2)ParameterOrEmpty,鍒欏搷搴斿瓧娈典腑[ErrorInfo]涓洪敊璇俊鎭�,ResponseData鍒欎负null
+        //3)NoLogin,鍒欏搷搴斿瓧娈典腑 [ErrorInfo] 涓洪敊璇俊鎭负 [鏃犳晥鐧诲綍Token!]
+        //(5)NoRecord,鍒欏搷搴斿瓧娈典腑 [ErrorInfo] 涓洪敊璇俊鎭负 [纭繚鎮ㄦ墍鎻愪氦 [椤圭洰 (浣忓畢)Id涓嶅瓨鍦�!]
+        //(6)BackupClassIdNoIsYou, 鍒欏搷搴斿瓧娈典腑 [ErrorInfo]涓洪敊璇俊鎭负 [鎮ㄦ墍澶囨敞鍚嶄富閿� = XXXX骞朵笉灞炰簬鎮ㄥ綋鍓嶄綇瀹�:XXXX]
+        //(7)NoPermission, 鍒欏搷搴斿瓧娈典腑 [ErrorInfo]涓洪敊璇俊鎭负 [鎮ㄥ苟涓嶅叿鏈夋鍒嗕韩鑰呮浣忓畢鐨勬潈闄�!]
+        //(8)InsufficientAuthority, 鍒欏搷搴斿瓧娈典腑 [ErrorInfo]涓洪敊璇俊鎭负 [鎮ㄥ苟涓嶅叿鏈夋鍒嗕韩鑰呮浣忓畢鐨勫畬鍏ㄦ帶鍒舵潈闄�!]
+        //(9)HomeIdAndTokenNoConsistent, 鍒欏搷搴斿瓧娈典腑 [ErrorInfo]涓洪敊璇俊鎭负 [鎮ㄥ綋鍓峊oken涓庝綘褰撳墠鎺у埗鐨凥omeId涓嶄竴鑷�!]
+        //(10)DeliveryBackNoUpdateDel, 鍒欏搷搴斿瓧娈典腑 [ErrorInfo]涓洪敊璇俊鎭负 [姝ゅ墠鎻愪氦鏂囦欢澶笽d鏄睘浜庝氦浠樺浠�, 涓嶈兘杩涜鏇存柊鍒犻櫎!]
+        /// </summary>
+        /// <param name="stateCodeStr"></param>
+        void ShowDelErrorInfo (string stateCodeStr)
+        {
+            string mes = "";
+            if (stateCodeStr == "HomeIdAndTokenNoConsistent") {
+                //Token 楠岃瘉澶辫触
+                mes = ErrorCode.HomeIdAndTokenNoConsistent;
+            } else if (stateCodeStr == "DeliveryBackNoUpdateDel") {
+                //浜や粯澶囦唤, 涓嶈兘鍒犻櫎!
+                mes = ErrorCode.DeliveryBackNoUpdateDel;
+            } else if (stateCodeStr == ErrorCode.NetworkError) {
+                mes = ErrorCode.NetworkError;
+            } else {
+                mes = ErrorCode.OperationFailed + ErrorCode.Reason + stateCodeStr;
+
+            }
+            if (!string.IsNullOrEmpty (mes)) {
+                Application.RunOnMainThread (() => {
+                    new Alert ("", mes, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+                });
+            }
+
+
+        }
+
+
+        /// <summary>
         /// 鐢ㄦ埛澶囦唤鏂囦欢
         /// </summary>
         void userBakeupFile (string groupName)
@@ -458,64 +581,182 @@
             //Alert alert = new Alert ("", Language.StringByID (R.MyInternationalizationString.SureToBackupYourthisFile), Language.StringByID (R.MyInternationalizationString.Cancel), Language.StringByID (R.MyInternationalizationString.Confrim));
 
             //alert.ResultEventHandler += (object sender2, bool e2) => {
-                //if (e2) {
-                    MainPage.Loading.Start ("Upload...");
+            //if (e2) {
+            MainPage.Loading.Start ("Upload...");
 
-                    System.Threading.Tasks.Task.Run (() => {
-                        try {
-                            AddFolderObj requestObj = new AddFolderObj () { LevelID = UserConfig.Instance.CurrentRegion.RegionID ,Name = groupName };
-                            var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj);
-                            var revertObj = MainPage.RequestHttps ("AddFolder", requestJson);
-                            if (revertObj.StateCode == "SUCCESS") {
-                                var addFolderID = Convert.ToInt32 (revertObj.ResponseData);
-                                var backuplist = IO.FileUtils.ReadFiles ();
-                                int index = 0;
-                                foreach (var fileName in backuplist) {
-                                    index++;
-                                    /// <summary>
-                                    /// 濡傛灉鏄壒娈婄殑娉ㄥ唽鐧婚檰鏂囦欢锛屽垯涓嶉渶瑕佸浠藉埌鏈嶅姟鍣�
-                                    /// </summary>
-                                    if (fileName == UserInfo.GlobalRegisterFile) {
-                                        continue;
-                                    }
-                                    AddUserBackupObj requestObj2 = new AddUserBackupObj () { LevelID = Convert.ToInt32(revertObj.ResponseData), Name = fileName, DetailByte = IO.FileUtils.ReadFile (fileName) };
-                                    var requestJson2 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj2);
-                                    var revertObj2 = MainPage.RequestHttps ("AddUserBackup", requestJson2);
-                                    if (revertObj2.StateCode == "SUCCESS") {
-                                        Application.RunOnMainThread (() => {
-                                            int pro = (int)(index * 1.0 / backuplist.Count * 100);
-                                            MainPage.Loading.Text = pro.ToString () + "%";
-                                        });
-                                    }
-                                }
-                            }
+            System.Threading.Tasks.Task.Run (() => {
+                try {
 
-                        } catch (Exception ex) {
-                            Shared.Application.RunOnMainThread (() => {
-                                new Alert ("", Language.StringByID (R.MyInternationalizationString.CheckInternet),
-                                           Language.StringByID (R.MyInternationalizationString.Close)).Show ();
-                            Console.WriteLine (ex.Message);
-                            });
-                        } finally {
-                            Shared.Application.RunOnMainThread (() => {
-                                MainPage.Loading.Hide ();
-                                DatabackupShow ();
-                            });
-                        }
+                    //if (string.IsNullOrEmpty (UserConfig.Instance.GatewayMAC)) return;
+                    //2020-01-11 娌$粦瀹歁AC涓嶅厑璁镐笂浼犲浠�
+                    if (!UserConfig.Instance.CheckHomeGatewaysNotEmpty()) {
+                        MainPage.ShowAlertOnMainThread (ErrorCode.BindGatewayFirst);
+                        return;
+                    }
+
+                    var requestObj = new AddHomeAppGatewayNameObj () {
+                        HomeId = UserConfig.Instance.CurrentRegion.Id,
+                        BackupName = groupName,
+                        LoginAccessToken = MainPage.LoginUser.LoginTokenString,
+                        GatewayUniqueId = UserConfig.Instance.GatewayMAC
+                    };
+                    string urlHead = MainPage.RequestHttpsHost;
+                    if (requestObj.IsOtherAccountCtrl) {
+                        urlHead = UserConfig.Instance.MasterAccountRequestBaseUrl;
+                        requestObj.LoginAccessToken = UserConfig.Instance.MasterAccountToken;
+                    }
+
+                    //AddFolderObj requestObj = new AddFolderObj () { LevelID = UserConfig.Instance.CurrentRegion.Id ,Name = groupName };
+                    var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj);
+                    var revertObj = MainPage.RequestHttps (API.AddHomeAppGatewayName, requestJson, urlHead);
+                    if (revertObj.StateCode.ToUpper () == StateCode.SUCCESS) {
+                        var DATA = Newtonsoft.Json.JsonConvert.DeserializeObject<AddBackupNameResult> (revertObj.ResponseData.ToString ());
+                        var addFolderID = DATA.BackupClassId;
+                        //UpLoadBackupFileToDB (addFolderID);
+                        UpLoadBackupFileToDBNew (addFolderID);
+
+
+                    } else {
+                        ShowAddHomeAppGatewayNameErrorInfo (revertObj.StateCode);
+
+                    }
+
+                } catch (Exception ex) {
+                    Shared.Application.RunOnMainThread (() => {
+                        new Alert ("", Language.StringByID (R.MyInternationalizationString.CheckInternet),
+                                   Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+                        Utlis.WriteLine (ex.Message);
                     });
+                } finally {
+                    Shared.Application.RunOnMainThread (() => {
+                        MainPage.Loading.Hide ();
+                        DatabackupShow ();
+                    });
+                }
+            });
             //    }
             //};
             //alert.Show ();
         }
 
         /// <summary>
+        ///(1)Success 鍒橻璋冪敤姝ゆ帴鍙f搷浣滄垚鍔焆,ResponseData鍒欎负null
+        //(2)ParameterOrEmpty,鍒欏搷搴斿瓧娈典腑 [ErrorInfo] 涓洪敊璇俊鎭�, ResponseData鍒欎负null
+        //(3)NoLogin,鍒欏搷搴斿瓧娈典腑 [ErrorInfo] 涓洪敊璇俊鎭负 [鏃犳晥鐧诲綍Token!]
+        //(4)NoRecord,鍒欏搷搴斿瓧娈典腑 [ErrorInfo] 涓洪敊璇俊鎭负 [纭繚鎮ㄦ墍鎻愪氦 [椤圭洰 (浣忓畢)Id涓嶅瓨鍦�!]
+        //(5)Exist, 鍒欏搷搴斿瓧娈典腑 [ErrorInfo]涓洪敊璇俊鎭负 [鎮ㄥ湪浣忓畢: XXX, 褰撳墠澶囦唤鍚�: XXXX宸插瓨鍦�, 璇锋崲涓浠藉啀鎻愪氦!]
+        //(6)NoBind, 鍒欏搷搴斿瓧娈典腑 [ErrorInfo]涓洪敊璇俊鎭负 [缃戝叧鐨勫敮涓�Id = XXX锛屼笌浣忓畢: XXXX骞朵笉瀛樺湪缁戝畾鍏崇郴!]
+        //(7)NoPermission, 鍒欏搷搴斿瓧娈典腑 [ErrorInfo]涓洪敊璇俊鎭负 [鎮ㄥ苟涓嶅叿鏈夋鍒嗕韩鑰呮浣忓畢鐨勬潈闄�!]
+
+        //(8)InsufficientAuthority, 鍒欏搷搴斿瓧娈典腑 [ErrorInfo]涓洪敊璇俊鎭负 [鎮ㄥ苟涓嶅叿鏈夋鍒嗕韩鑰呮浣忓畢鐨勫畬鍏ㄦ帶鍒舵潈闄�!]
+        //(9)HomeIdAndTokenNoConsistent, 鍒欏搷搴斿瓧娈典腑 [ErrorInfo]涓洪敊璇俊鎭负 [鎮ㄥ綋鍓峊oken涓庝綘褰撳墠鎺у埗鐨凥omeId涓嶄竴鑷�!]
+        /// </summary>
+        /// <param name="stateCodeStr"></param>
+        void ShowAddHomeAppGatewayNameErrorInfo (string stateCodeStr)
+        {
+            string mes = "";
+            if (stateCodeStr == "Exist") {
+                //澶囦唤鍚嶅凡瀛樺湪
+                mes = ErrorCode.NameExist;
+            } else if (stateCodeStr == "HomeIdAndTokenNoConsistent") {
+                //Token 楠岃瘉澶辫触
+                mes = ErrorCode.HomeIdAndTokenNoConsistent;
+            } else if (stateCodeStr == ErrorCode.NetworkError) {
+                mes = ErrorCode.NetworkError;
+            } else {
+                mes = ErrorCode.OperationFailed + ErrorCode.Reason + stateCodeStr;
+
+            }
+            if (!string.IsNullOrEmpty (mes)) {
+                Application.RunOnMainThread (() => {
+                    new Alert ("", mes, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+                });
+            }
+
+
+        }
+
+        ///// <summary>
+        ///// 涓婁紶澶囦唤鏂囦欢
+        ///// </summary>
+        ///// <param name="mBackupClassId"></param>
+        //void UpLoadBackupFileToDB (string mBackupClassId)
+        //{
+
+        //    var backuplist = IO.FileUtils.ReadFiles ();
+        //    int index = 0;
+        //    foreach (var fileName in backuplist) {
+        //        index++;
+        //        /// <summary>
+        //        /// 濡傛灉鏄壒娈婄殑娉ㄥ唽鐧婚檰鏂囦欢锛屽垯涓嶉渶瑕佸浠藉埌鏈嶅姟鍣�
+        //        /// </summary>
+        //        if (fileName == UserInfo.GlobalRegisterFile) {
+        //            continue;
+        //        }
+        //        //鏁寸悊鏁版嵁
+        //        var datainfo = new FileInfoData ();
+        //        datainfo.BackupFileName = fileName;
+        //        datainfo.BackupFileContent = Shared.IO.FileUtils.ReadFile (fileName);
+
+        //        var list = new List<FileInfoData> ();
+        //        list.Add (datainfo);
+
+        //        var upData = new UploadHomeAppGatewaySubFilesObj ();
+        //        upData.HomeId = UserConfig.Instance.CurrentRegion.Id;
+        //        upData.BackupClassId = mBackupClassId;
+        //        upData.UploadSubFileLists = list;
+        //        //鑾峰彇鎺у埗涓讳汉璐﹀彿鐨凾oken
+        //        upData.LoginAccessToken = MainPage.LoginUser.LoginTokenString;
+        //        string urlHead = MainPage.RequestHttpsHost;
+        //        if (upData.IsOtherAccountCtrl) {
+        //            urlHead = UserConfig.Instance.MasterAccountRequestBaseUrl;
+        //            upData.LoginAccessToken = UserConfig.Instance.MasterAccountToken;
+        //        }
+
+        //        //AddUserBackupObj requestObj2 = new AddUserBackupObj () { LevelID = Convert.ToInt32 (revertObj.ResponseData), Name = fileName, DetailByte = IO.FileUtils.ReadFile (fileName) };
+        //        var requestJson2 = Newtonsoft.Json.JsonConvert.SerializeObject (upData);
+        //        var revertObj2 = MainPage.RequestHttps (API.UploadHomeAppGatewaySubFiles, requestJson2, urlHead);
+        //        if (revertObj2.StateCode.ToUpper () == StateCode.SUCCESS) {
+        //            //index++;
+        //            Application.RunOnMainThread (() => {
+        //                int pro = (int)(index * 1.0 / backuplist.Count * 100);
+        //                MainPage.Loading.Text = pro.ToString () + "%";
+        //            });
+        //        } else {
+        //            //鎻愮ず閿欒
+        //            break;
+        //        }
+        //    }
+        //    if (index != backuplist.Count) {
+        //        //2020-01-11
+        //        DeleteFolderDataAfterUploadFailed (mBackupClassId);
+        //        Shared.Application.RunOnMainThread (() => {
+        //            new Alert ("", "Upload failed!", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+        //            MainPage.Loading.Hide ();
+        //        });
+
+        //    }
+
+        //    //Shared.Application.RunOnMainThread (() => {
+        //    //    if (index == 0) {
+        //    //        new Alert ("", "Upload failed!", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+        //    //    }
+
+        //    //    MainPage.Loading.Hide ();
+        //    //});
+        //}
+
+
+
+
+        /// <summary>
         /// 鐢ㄦ埛鎭㈠鏂囦欢
         /// </summary>
-        public void UserBakeupFileDetailedInformation (int folderID)
+        public void UserBakeupFileDetailedInformation (string BackupClassId)
         {
             //鏄惁纭畾鎭㈠璇ユ枃浠舵暟鎹俊鎭�
             Alert alert = new Alert ("", Language.StringByID (R.MyInternationalizationString.SureToRestoreFileInformation), Language.StringByID (R.MyInternationalizationString.Cancel), Language.StringByID (R.MyInternationalizationString.Confrim));
-            alert.ResultEventHandler += ( sender2,  e2) => {
+            alert.ResultEventHandler += (sender2, e2) => {
                 if (e2) {
                     MainPage.Loading.Start ("Download...");
                     System.Threading.Tasks.Task.Run (() => {
@@ -527,63 +768,110 @@
                                 }
                                 IO.FileUtils.DeleteFile (fileName);
                             }
-                            //鑾峰彇鎵�鏈夋枃浠剁粍鍚�
-                            var requestObj2 = new UserBackupListObj () { LevelID = folderID };
-                            var requestJson2 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj2);
-                            var revertObj2 = MainPage.RequestHttps ("UserBackupList", requestJson2);
-                            if (revertObj2.StateCode == "SUCCESS") {
-                                var responseDataObj = Newtonsoft.Json.JsonConvert.DeserializeObject<List<BackupInfoRes>> (revertObj2.ResponseData.ToString ());
-                                int index = 0;
-                                foreach (var file in responseDataObj) {
-                                    if (file.FileName == "null" || "UserConfig" == file.FileName) {
-                                        continue;
-                                    }
 
-                                    var requestObj3 = new BackupDetailObj () { Id = file.Id };
-                                    var requestJson3 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj3);
-                                    var revertObj3 = MainPage.RequestHttps ("BackupDetail", requestJson3);
-                                    if (revertObj3.StateCode == "SUCCESS") {
-                                        var jsonBytes = Newtonsoft.Json.JsonConvert.SerializeObject (revertObj3.ResponseData);
-                                        var byresss = Newtonsoft.Json.JsonConvert.DeserializeObject<byte[]> (jsonBytes);
-                                        index++;
-                                        //鏂板鍔犱簡浣忓畢鍖哄煙锛屾棫鐨勫浠芥暟鎹病鏈夊尯鍩熷睘鎬э紝褰撹幏鍙栧埌杩欎釜鏂囦欢鏃讹紝鎶婂綋鍓嶅埌浣忓畢淇℃伅鏇村埌褰撳墠澶囦唤鍒版暟鎹噷闈�
-                                        if ("UserConfig" == file.FileName){
-                                            //var regionTemp = UserConfig.Instance.CurrentRegion;
-                                            //UserConfig.Instance.RefreshUserConfig ();
-                                            //UserConfig.Instance.CurrentRegion = regionTemp;
-                                            //UserConfig.Instance.SaveUserConfig ();
-                                            ////UserConfig.Instance.GatewayList
-                                            continue;
-                                        }
-                                        IO.FileUtils.WriteFileByBytes (file.FileName, byresss);
-                                        Application.RunOnMainThread (() => {
-                                            int pro = (int)(index * 1.0 / responseDataObj.Count * 100);
-                                            MainPage.Loading.Text = pro.ToString () + "%";
-                                        });
-                                    }
+                            var requestObj2 = new GetHomeDataBackupUploadListPaggerObj ();
+                            requestObj2.HomeId = UserConfig.Instance.CurrentRegion.Id;
+                            requestObj2.BackupClassId = BackupClassId;
+                            requestObj2.PageSetting.PageSize = 999999;
+                            requestObj2.PageSetting.Page = 1;
+                            //鑾峰彇鎺у埗涓讳汉璐﹀彿鐨凾oken
+                            requestObj2.LoginAccessToken = MainPage.LoginUser.LoginTokenString;
+                            string urlHead = MainPage.RequestHttpsHost;
+                            if (requestObj2.IsOtherAccountCtrl) {
+                                urlHead = UserConfig.Instance.MasterAccountRequestBaseUrl;
+                                requestObj2.LoginAccessToken = UserConfig.Instance.MasterAccountToken;
+                            }
+
+                            //鑾峰彇鎵�鏈夋枃浠剁粍鍚�
+                            //var requestObj2 = new UserBackupListObj () { LevelID = folderID };
+                            var requestJson2 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj2);
+                            var revertObj2 = MainPage.RequestHttps (API.GetHomeDataBackupUploadListPagger, requestJson2, urlHead);
+                            if (revertObj2.StateCode.ToUpper () == StateCode.SUCCESS) {
+                                var fileListData = Newtonsoft.Json.JsonConvert.DeserializeObject<LoadBackInfoResult> (revertObj2.ResponseData.ToString ());
+                                var responseDataObj = fileListData.PageData;
+                                ////鑾峰彇鏂囦欢鍚嶅瓧
+                                //List<string> listFileName = new List<string> ();
+                                //foreach (LoadBackupFileNameInfo file in fileListData.PageData) {
+                                //    listFileName.Add (file.BackupFileName);
+                                //}
+                                //var responseDataObj = Newtonsoft.Json.JsonConvert.DeserializeObject<List<BackupInfoRes>> (revertObj2.ResponseData.ToString ());
+
+                                var mDownLoad = new DownloadSomeDataBackupObj ();
+                                mDownLoad.HomeId = UserConfig.Instance.CurrentRegion.Id;
+                                mDownLoad.BackupClassId = BackupClassId;
+                                //鑾峰彇鎺у埗涓讳汉璐﹀彿鐨凾oken
+                                mDownLoad.LoginAccessToken = MainPage.LoginUser.LoginTokenString;
+                                string mUrlHead = MainPage.RequestHttpsHost;
+                                if (mDownLoad.IsOtherAccountCtrl) {
+                                    urlHead = UserConfig.Instance.MasterAccountRequestBaseUrl;
+                                    mDownLoad.LoginAccessToken = UserConfig.Instance.MasterAccountToken;
                                 }
-                                MainPage.LoginUser.SaveUserInfo ();
-                                Room.InitAllRoom ();
-                                Application.RunOnMainThread (() => {
-                                    this.RemoveFromParent ();
-                                    CommonPage.IsRemote = false;
-                                    UserMiddle.Init ();
-                                    EquipmentPublicClass.CheckLinkRemote (2);
-                                    new Alert ("", Language.StringByID (R.MyInternationalizationString.RestoreFileIsSuccessfull),
-                                   Language.StringByID (R.MyInternationalizationString.Close)).Show ();
-                                });
-                                MainPage.LoginUser = Newtonsoft.Json.JsonConvert.DeserializeObject<UserInfo> (System.Text.Encoding.UTF8.GetString (Shared.IO.FileUtils.ReadFile (UserInfo.GlobalRegisterFile)));
-                                if (MainPage.LoginUser != null) {
-                                    MainPage.LoginUser.LastTime = System.DateTime.Now;
-                                    MainPage.LoginUser.SaveUserInfo ();
-                                }
+
+                                MultiThreadDownload (responseDataObj, mDownLoad, mUrlHead);
+
+
+                                //int index = 0;
+                               
+                                //foreach (var file in responseDataObj) {
+                                //    index++;
+                                //    if (file.BackupFileName == "null" || "UserConfig" == file.BackupFileName) {
+                                //        continue;
+                                //    }
+                                //    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);
+                                //    if (revertObj3 != null) {
+
+                                //        IO.FileUtils.WriteFileByBytes (file.BackupFileName, revertObj3);
+                                //        Application.RunOnMainThread (() => {
+                                //            int pro = (int)(index * 1.0 / responseDataObj.Count * 100);
+                                //            MainPage.Loading.Text = pro.ToString () + "%";
+                                //        });
+                                //    } else {
+                                //        break;
+                                //    }
+
+                                //}
+
+
+                                //if (index != responseDataObj.Count) {
+                                //    //鍒犻櫎宸茬粡涓嬭浇鐨勬枃浠朵釜鏁伴敊璇�
+                                //    var fileNames2 = IO.FileUtils.ReadFiles ();
+                                //    foreach (var fileName in fileNames2) {
+                                //        if (fileName == UserInfo.GlobalRegisterFile) {
+                                //            continue;
+                                //        }
+                                //        IO.FileUtils.DeleteFile (fileName);
+                                //    }
+
+                                //    MainPage.ShowAlertOnMainThread ("Download failed!");
+
+                                //    return;
+                                //}
+
+                                //MainPage.LoginUser.SaveUserInfo ();
+                                //Room.InitAllRoom ();
+                                //Application.RunOnMainThread (() => {
+                                //    this.RemoveFromParent ();
+                                //    CommonPage.IsRemote = false;
+                                //    UserMiddle.Init ();
+                                //    EquipmentPublicClass.CheckLinkRemote (UserConfig.Instance.internetStatus);
+                                //    new Alert ("", Language.StringByID (R.MyInternationalizationString.RestoreFileIsSuccessfull),
+                                //   Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+                                //});
+                                //MainPage.LoginUser = Newtonsoft.Json.JsonConvert.DeserializeObject<UserInfo> (System.Text.Encoding.UTF8.GetString (Shared.IO.FileUtils.ReadFile (UserInfo.GlobalRegisterFile)));
+                                //if (MainPage.LoginUser != null) {
+                                //    MainPage.LoginUser.LastTime = System.DateTime.Now;
+                                //    MainPage.LoginUser.SaveUserInfo ();
+                                //}
                             }
                         } catch (Exception ex) {
                             Shared.Application.RunOnMainThread (() => {
                                 new Alert ("", Language.StringByID (R.MyInternationalizationString.CheckInternet),
                                            Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                             });
-                            Console.WriteLine (ex.ToString());
+                            Utlis.WriteLine (ex.ToString ());
                         } finally {
                             Shared.Application.RunOnMainThread (() => {
                                 MainPage.Loading.Hide ();
@@ -594,5 +882,344 @@
             };
             alert.Show ();
         }
+
+
+        bool DownloadSomeDataBackup (DownloadSomeDataBackupObj mmDownLoad, string mUrlHead, LoadBackupFileNameInfo file)
+        {
+            DownloadSomeDataBackupObj mDownLoad = mmDownLoad;
+            mDownLoad.BackupFileName = file.BackupFileName;
+            //var requestObj3 = new BackupDetailObj () { Id = file.Id };
+            var requestJson3 = Newtonsoft.Json.JsonConvert.SerializeObject (mDownLoad);
+            var revertObj3 = MainPage.RequestHttpsReturnByteNew (API.DownloadSomeDataBackup, requestJson3, mUrlHead);
+            if (revertObj3 != null) {
+
+                IO.FileUtils.WriteFileByBytes (file.BackupFileName, revertObj3);
+                return true;
+            } else {
+                return false;
+            }
+        }
+
+        /// <summary>
+        /// 鍒犻櫎鏈湴鏂囦欢
+        /// </summary>
+        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 ();
+
+        /// <summary>
+        /// 澶氱嚎绋嬩笅杞�
+        /// </summary>
+        void MultiThreadDownload (List<LoadBackupFileNameInfo> dataList, DownloadSomeDataBackupObj mDownLoad, string mUrlHead, int maxThreads = 5)
+        {
+
+            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" && obj.BackupFileName != 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 {
+                            //濡傛灉澶辫触閲嶆柊涓嬭浇锛岄噸澶�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++;
+                            }
+                        }
+
+                        //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}");
+                    }
+
+                }) { 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 (() => {
+                this.RemoveFromParent ();
+                //CommonPage.IsRemote = false;
+                UserMiddle.Init ();
+                EquipmentPublicClass.CheckLinkRemote (UserConfig.Instance.internetStatus);
+                new Alert ("", Language.StringByID (R.MyInternationalizationString.RestoreFileIsSuccessfull),
+               Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+            });
+            MainPage.LoginUser = Newtonsoft.Json.JsonConvert.DeserializeObject<UserInfo> (System.Text.Encoding.UTF8.GetString (Shared.IO.FileUtils.ReadFile (UserInfo.GlobalRegisterFile)));
+            if (MainPage.LoginUser != null) {
+                MainPage.LoginUser.LastTime = System.DateTime.Now;
+                MainPage.LoginUser.SaveUserInfo ();
+            }
+
+
+
+        }
+
+        bool UploadDataBackup (UploadHomeAppGatewaySubFilesObj upData, string mUrlHead, List<string> fileList, int startIndex1, int endIndex1)
+        {
+            try {
+
+                var list = new List<FileInfoData> ();
+                for (var j = startIndex1; j <= endIndex1; j++) {
+                    var datainfo = new FileInfoData ();
+                    datainfo.BackupFileName = fileList [j];
+                    datainfo.BackupFileContent = Shared.IO.FileUtils.ReadFile (fileList [j]);
+                    list.Add (datainfo);
+                }
+
+                //鏁寸悊鏁版嵁
+                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 () == StateCode.SUCCESS) {
+                    return true;
+                } else {
+                    //鎻愮ず閿欒
+                    return false;
+                }
+            } catch {
+                return false;
+            }
+        }
+
+
+
+
+        /// <summary>
+        /// 涓婁紶澶囦唤鏂囦欢
+        /// 澶氭涓婁紶澶氫釜鏂囦欢
+        /// </summary>
+        /// <param name="mBackupClassId"></param>
+        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" && obj != CommonConfig.ConfigFile);
+            if (mFileList.Count <= 0) return;
+
+            if (maxThreads > mFileList.Count) {
+                maxThreads = mFileList.Count;
+            }
+
+            int index = 0;
+            int indexCount = 0;
+            int resultCount = mFileList.Count / maxThreads;
+
+            var upData = new UploadHomeAppGatewaySubFilesObj ();
+            upData.HomeId = UserConfig.Instance.CurrentRegion.Id;
+            upData.BackupClassId = mBackupClassId;
+            //鑾峰彇鎺у埗涓讳汉璐﹀彿鐨凾oken
+            upData.LoginAccessToken = MainPage.LoginUser.LoginTokenString;
+            string urlHead = MainPage.RequestHttpsHost;
+            if (upData.IsOtherAccountCtrl) {
+                urlHead = UserConfig.Instance.MasterAccountRequestBaseUrl;
+                upData.LoginAccessToken = UserConfig.Instance.MasterAccountToken;
+            }
+
+
+            for (int i = 1; i <= resultCount; i++) {
+                int startIndex = ((i - 1) * maxThreads + 0);
+                int endIndex = maxThreads * i - 1;
+                if (i == resultCount) {
+                    /*鏈�鍚庝竴涓嚎绋�* */
+                    endIndex = mFileList.Count - 1;
+                }
+                var id = i;
+                var startIndex1 = startIndex;
+                var endIndex1 = endIndex;
+                //2020-06-10 淇敼澶у浘鐗囧ぇ鏂囦欢澶勭悊
+                var result = UploadDataBackupNew (upData, urlHead, mFileList, startIndex1, endIndex1);
+                if (result) {
+                    index++;
+                    Application.RunOnMainThread (() => {
+                        int pro = (int)(index * 1.0 / resultCount * 100);
+                        MainPage.Loading.Text = pro.ToString () + "%";
+                    });
+
+                }
+                indexCount++;
+
+            }
+
+
+            Utlis.WriteLine ($"涓婁紶瀹屾垚");
+
+            if (index != resultCount) {
+                //2020-01-11 澶囦唤澶辫触
+                DeleteFolderDataAfterUploadFailed (mBackupClassId);
+                Shared.Application.RunOnMainThread (() => {
+                    new Alert ("", Language.StringByID (R.MyInternationalizationString.FailedToBackupFile), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+                    MainPage.Loading.Hide ();
+                });
+                return;
+            }
+
+            Application.RunOnMainThread (() => {
+                //MainPage.Loading.Text = "100%";
+                new Alert ("", Language.StringByID (R.MyInternationalizationString.BackupFileIsSuccessful), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+            });
+
+        }
+
+
+        /// <summary>
+        /// 2020-06-05 闇�瑕佺瓑寰呬簯绔鍔犳帴鍙�
+        /// 澶勭悊澶ф枃浠跺拰鍥剧墖涓婁紶
+        /// 
+        /// </summary>
+        /// <param name="upData"></param>
+        /// <param name="mUrlHead"></param>
+        /// <param name="fileList"></param>
+        /// <param name="startIndex1"></param>
+        /// <param name="endIndex1"></param>
+        /// <returns></returns>
+        bool UploadDataBackupNew (UploadHomeAppGatewaySubFilesObj upData, string mUrlHead, List<string> fileList, int startIndex1, int endIndex1)
+        {
+            try {
+
+                var list = new List<FileInfoData> ();
+                for (var j = startIndex1; j <= endIndex1; j++) {
+                    var datainfo = new FileInfoData ();
+                    datainfo.BackupFileName = fileList [j];
+                    datainfo.BackupFileContent = Shared.IO.FileUtils.ReadFile (fileList [j]);
+
+                    if (datainfo.BackupFileContent.Length > 1024 * 10) {
+                        var upDataBig = new UploadHomeAppGatewaySubFilesBigObj ();
+                        upDataBig.RequestVersion = upData.RequestVersion;
+                        upDataBig.HomeId = upData.HomeId;
+                        upDataBig.BackupClassId = upData.BackupClassId;
+                        upDataBig.FileName = fileList [j];
+                        upDataBig.IsOtherAccountCtrl = upData.IsOtherAccountCtrl;
+
+                        var revertObj5 = MainPage.RequestHttpsNewBig (API.UploadHomeAppGatewaySingleSubFile, datainfo.BackupFileContent, upDataBig, mUrlHead);
+
+                    } else {
+                        list.Add (datainfo);
+                    }
+                }
+
+                //鏁寸悊鏁版嵁
+                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 () == StateCode.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 () == StateCode.SUCCESS) {
+                        result = true;
+                        errorCount = 3;
+                    } else {
+                        errorCount++;
+                        //Utlis.WriteLine ($"涓婁紶澶辫触({startIndex1}-{endIndex1})锛屾鏁�:{errorCount}");
+                    }
+                }
+                return result;
+            } catch {
+                return false;
+            }
+        }
+
+
     }
 }
\ No newline at end of file

--
Gitblit v1.8.0