From ac2ee45fadc64fe840bbba6264df3ca4622adb15 Mon Sep 17 00:00:00 2001
From: JLChen <551775569@qq.com>
Date: 星期日, 12 一月 2020 16:06:13 +0800
Subject: [PATCH] 2020-01-12 1.优化修改。

---
 Crabtree/SmartHome/UI/SimpleControl/Phone/Register/Databackup.cs |  193 ++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 147 insertions(+), 46 deletions(-)

diff --git a/Crabtree/SmartHome/UI/SimpleControl/Phone/Register/Databackup.cs b/Crabtree/SmartHome/UI/SimpleControl/Phone/Register/Databackup.cs
index 097b679..ed10e5a 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/Phone/Register/Databackup.cs
+++ b/Crabtree/SmartHome/UI/SimpleControl/Phone/Register/Databackup.cs
@@ -311,7 +311,7 @@
                     //GetUserFolderObj requestObj = new GetUserFolderObj () { LevelID = UserConfig.Instance.CurrentRegion.Id };
                     var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj);
                     var revertObj = MainPage.RequestHttps (API.GetHomeDataBackupPagger, requestJson, urlHead);
-                    if (revertObj.StateCode.ToUpper() == "SUCCESS") {
+                    if (revertObj.StateCode.ToUpper () == "SUCCESS") {
                         var data = Newtonsoft.Json.JsonConvert.DeserializeObject<GetBackupListResult> (revertObj.ResponseData.ToString ());
                         //var list = new List<BackupListNameInfo> ();
                         //foreach (var data2 in data.PageData) {
@@ -330,11 +330,19 @@
                         if (data == null) return;
 
                         Application.RunOnMainThread (() => {
-                            foreach(var folder in data.PageData)
-                            {
+                            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 (() => {
@@ -482,6 +490,47 @@
 
         }
 
+        //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 () == "SUCCESS") {
+                    //鍒犻櫎鎴愬姛
+                } else {
+                    //鍒犻櫎澶辫触
+                }
+            } catch { } finally {
+                Application.RunOnMainThread (() => {
+                    MainPage.Loading.Hide ();
+                });
+            }
+
+
+        }
+
         /// <summary>
         /// (2)ParameterOrEmpty,鍒欏搷搴斿瓧娈典腑[ErrorInfo]涓洪敊璇俊鎭�,ResponseData鍒欎负null
         //3)NoLogin,鍒欏搷搴斿瓧娈典腑 [ErrorInfo] 涓洪敊璇俊鎭负 [鏃犳晥鐧诲綍Token!]
@@ -534,7 +583,11 @@
                 try {
 
                     //if (string.IsNullOrEmpty (UserConfig.Instance.GatewayMAC)) return;
-                    if (!UserConfig.Instance.CheckHomeGateways () || string.IsNullOrEmpty (UserConfig.Instance.CurrentRegion.HomeGateways [0].GatewayUniqueId)) return;
+                    //2020-01-11 娌$粦瀹歁AC涓嶅厑璁镐笂浼犲浠�
+                    if (!UserConfig.Instance.CheckHomeGateways () || string.IsNullOrEmpty (UserConfig.Instance.CurrentRegion.HomeGateways [0].GatewayUniqueId)) {
+                        MainPage.ShowAlertOnMainThread (ErrorCode.BindGatewayFirst);
+                        return;
+                    }
 
                     var requestObj = new AddHomeAppGatewayNameObj () {
                         HomeId = UserConfig.Instance.CurrentRegion.Id,
@@ -581,13 +634,11 @@
         }
 
         /// <summary>
-        /// (1)Success 鍒橻璋冪敤姝ゆ帴鍙f搷浣滄垚鍔焆,ResponseData鍒欎负null
-        //        (2)ParameterOrEmpty,鍒欏搷搴斿瓧娈典腑 [ErrorInfo] 涓洪敊璇俊鎭�, ResponseData鍒欎负null
-        //        (3)NoLogin,鍒欏搷搴斿瓧娈典腑 [ErrorInfo] 涓洪敊璇俊鎭负 [鏃犳晥鐧诲綍Token!]
-
+        ///(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]涓洪敊璇俊鎭负 [鎮ㄥ苟涓嶅叿鏈夋鍒嗕韩鑰呮浣忓畢鐨勬潈闄�!]
 
@@ -619,17 +670,17 @@
 
         }
 
-      /// <summary>
-      /// 涓婁紶澶囦唤鏂囦欢
-      /// </summary>
-      /// <param name="mBackupClassId"></param>
+        /// <summary>
+        /// 涓婁紶澶囦唤鏂囦欢
+        /// </summary>
+        /// <param name="mBackupClassId"></param>
         void UpLoadBackupFileToDB (string mBackupClassId)
         {
 
             var backuplist = IO.FileUtils.ReadFiles ();
             int index = 0;
             foreach (var fileName in backuplist) {
-                //index++;
+                index++;
                 /// <summary>
                 /// 濡傛灉鏄壒娈婄殑娉ㄥ唽鐧婚檰鏂囦欢锛屽垯涓嶉渶瑕佸浠藉埌鏈嶅姟鍣�
                 /// </summary>
@@ -660,24 +711,33 @@
                 var requestJson2 = Newtonsoft.Json.JsonConvert.SerializeObject (upData);
                 var revertObj2 = MainPage.RequestHttps (API.UploadHomeAppGatewaySubFiles, requestJson2, urlHead);
                 if (revertObj2.StateCode.ToUpper () == "SUCCESS") {
-                    index++;
+                    //index++;
                     Application.RunOnMainThread (() => {
                         int pro = (int)(index * 1.0 / backuplist.Count * 100);
                         MainPage.Loading.Text = pro.ToString () + "%";
                     });
                 } else {
                     //鎻愮ず閿欒
+                    break;
                 }
             }
-            
-
-            Shared.Application.RunOnMainThread (() => {
-                if (index == 0) {
+            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 ();
-            });
+                    MainPage.Loading.Hide ();
+                });
+
+            }
+
+            //Shared.Application.RunOnMainThread (() => {
+            //    if (index == 0) {
+            //        new Alert ("", "Upload failed!", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+            //    }
+
+            //    MainPage.Loading.Hide ();
+            //});
         }
 
 
@@ -742,7 +802,51 @@
                                 }
 
                                 int index = 0;
+                                //foreach (var file in responseDataObj) {
+                                //    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) {
+                                //        index++;
+                                //        IO.FileUtils.WriteFileByBytes (file.BackupFileName, revertObj3);
+                                //        Application.RunOnMainThread (() => {
+                                //            int pro = (int)(index * 1.0 / responseDataObj.Count * 100);
+                                //            MainPage.Loading.Text = pro.ToString () + "%";
+                                //        });
+                                //    } else {
+
+
+                                //    }
+
+                                //    //if (revertObj3.StateCode.ToUpper() == "SUCCESS") {
+                                //    //    var jsonBytes = Newtonsoft.Json.JsonConvert.SerializeObject (revertObj3.ResponseData);
+                                //    //    var byresss = Newtonsoft.Json.JsonConvert.DeserializeObject<byte[]> (jsonBytes);
+                                //    //    index++;
+                                //    //    //鏂板鍔犱簡浣忓畢鍖哄煙锛屾棫鐨勫浠芥暟鎹病鏈夊尯鍩熷睘鎬э紝褰撹幏鍙栧埌杩欎釜鏂囦欢鏃讹紝鎶婂綋鍓嶅埌浣忓畢淇℃伅鏇村埌褰撳墠澶囦唤鍒版暟鎹噷闈�
+                                //    //    if ("UserConfig" == file.BackupFileName){
+                                //    //        //var regionTemp = UserConfig.Instance.CurrentRegion;
+                                //    //        //UserConfig.Instance.RefreshUserConfig ();
+                                //    //        //UserConfig.Instance.CurrentRegion = regionTemp;
+                                //    //        //UserConfig.Instance.SaveUserConfig ();
+                                //    //        ////UserConfig.Instance.GatewayList
+                                //    //        continue;
+                                //    //    }
+                                //    //    IO.FileUtils.WriteFileByBytes (file.BackupFileName, byresss);
+                                //    //    Application.RunOnMainThread (() => {
+                                //    //        int pro = (int)(index * 1.0 / responseDataObj.Count * 100);
+                                //    //        MainPage.Loading.Text = pro.ToString () + "%";
+                                //    //    });
+                                //    //} else {
+                                //    //    //閿欒鎻愮ず
+
+                                //    //}
+                                //}
                                 foreach (var file in responseDataObj) {
+                                    index++;
                                     if (file.BackupFileName == "null" || "UserConfig" == file.BackupFileName) {
                                         continue;
                                     }
@@ -751,37 +855,34 @@
                                     var requestJson3 = Newtonsoft.Json.JsonConvert.SerializeObject (mDownLoad);
                                     var revertObj3 = MainPage.RequestHttpsReturnByte (API.DownloadSomeDataBackup, requestJson3, false, mUrlHead);
                                     if (revertObj3 != null) {
-                                        index++;
+                                     
                                         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 (revertObj3.StateCode.ToUpper() == "SUCCESS") {
-                                    //    var jsonBytes = Newtonsoft.Json.JsonConvert.SerializeObject (revertObj3.ResponseData);
-                                    //    var byresss = Newtonsoft.Json.JsonConvert.DeserializeObject<byte[]> (jsonBytes);
-                                    //    index++;
-                                    //    //鏂板鍔犱簡浣忓畢鍖哄煙锛屾棫鐨勫浠芥暟鎹病鏈夊尯鍩熷睘鎬э紝褰撹幏鍙栧埌杩欎釜鏂囦欢鏃讹紝鎶婂綋鍓嶅埌浣忓畢淇℃伅鏇村埌褰撳墠澶囦唤鍒版暟鎹噷闈�
-                                    //    if ("UserConfig" == file.BackupFileName){
-                                    //        //var regionTemp = UserConfig.Instance.CurrentRegion;
-                                    //        //UserConfig.Instance.RefreshUserConfig ();
-                                    //        //UserConfig.Instance.CurrentRegion = regionTemp;
-                                    //        //UserConfig.Instance.SaveUserConfig ();
-                                    //        ////UserConfig.Instance.GatewayList
-                                    //        continue;
-                                    //    }
-                                    //    IO.FileUtils.WriteFileByBytes (file.BackupFileName, byresss);
-                                    //    Application.RunOnMainThread (() => {
-                                    //        int pro = (int)(index * 1.0 / responseDataObj.Count * 100);
-                                    //        MainPage.Loading.Text = pro.ToString () + "%";
-                                    //    });
-                                    //} else {
-                                    //    //閿欒鎻愮ず
-
-                                    //}
                                 }
+
+
+                                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 (() => {

--
Gitblit v1.8.0