JLChen
2020-01-12 ac2ee45fadc64fe840bbba6264df3ca4622adb15
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 刷新一次MasterToken
                        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 没绑定MAC不允许上传备份
                    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 则[调用此接口操作成功],ResponseData则为null
        //        (2)ParameterOrEmpty,则响应字段中 [ErrorInfo] 为错误信息, ResponseData则为null
        //        (3)NoLogin,则响应字段中 [ErrorInfo] 为错误信息为 [无效登录Token!]
        ///(1)Success 则[调用此接口操作成功],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 (() => {