From f382eaff90d5126387b5616b602b4168b37ad21c Mon Sep 17 00:00:00 2001
From: 陈嘉乐 <cjl@hdlchina.com.cn>
Date: 星期二, 12 一月 2021 16:54:12 +0800
Subject: [PATCH] Merge branch 'master' into wjc

---
 Crabtree/SmartHome/HDL/Common/CommonUtlis.cs |  367 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 367 insertions(+), 0 deletions(-)

diff --git a/Crabtree/SmartHome/HDL/Common/CommonUtlis.cs b/Crabtree/SmartHome/HDL/Common/CommonUtlis.cs
index 1d6fc81..933002e 100644
--- a/Crabtree/SmartHome/HDL/Common/CommonUtlis.cs
+++ b/Crabtree/SmartHome/HDL/Common/CommonUtlis.cs
@@ -1,6 +1,8 @@
 锘縰sing System;
+using System.Collections.Generic;
 using System.Linq;
 using System.Text.RegularExpressions;
+using Shared.SimpleControl;
 
 namespace Shared
 {
@@ -185,6 +187,371 @@
             alert.Show ();
         }
 
+        #region 鈻� 閫氱敤Dialog_______________________
+        /// <summary>
+        /// 閫氱敤缂栬緫Dialog
+        /// </summary>
+        /// <param name="nameStr"></param>
+        /// <param name="titleStr"></param>
+        public void ShowEditTextDialog (string nameStr, Action<string> saveAction, string titleStr = "")
+        {
+            Dialog dialog = new Dialog ();
+
+            FrameLayout dialogBodyView = new FrameLayout () {
+                Gravity = Gravity.Center,
+                Width = Application.GetRealWidth (500),
+                Height = Application.GetRealHeight (330),
+                Radius = 5,
+                BorderColor = SkinStyle.Current.Transparent,
+                BorderWidth = 0,
+                BackgroundColor = SkinStyle.Current.DialogColor,
+            };
+            dialog.AddChidren (dialogBodyView);
+
+            Button btnTitle = new Button () {
+                Height = Application.GetRealHeight (80),
+                TextAlignment = TextAlignment.Center,
+                Text = titleStr,
+                TextColor = SkinStyle.Current.DialogTextColor,
+                BackgroundColor = SkinStyle.Current.DialogTitle,
+            };
+            dialogBodyView.AddChidren (btnTitle);
+
+            EditText etZoneName = new EditText () {
+                X = Application.GetRealWidth (50),
+                Y = Application.GetRealHeight (120),
+                Width = Application.GetRealWidth (400),
+                Height = Application.GetRealHeight (80),
+                TextColor = SkinStyle.Current.TextColor,
+                TextAlignment = TextAlignment.Center,
+                Radius = 5,
+                BorderColor = SkinStyle.Current.BorderColor,
+                BorderWidth = 2,
+                Text = nameStr,
+            };
+            dialogBodyView.AddChidren (etZoneName);
+
+            FrameLayout BottomView = new FrameLayout () {
+                Y = dialogBodyView.Height - Application.GetRealHeight (88),
+                Height = Application.GetRealHeight (90),
+                BackgroundColor = SkinStyle.Current.Black50Transparent,
+            };
+            dialogBodyView.AddChidren (BottomView);
+
+            Button btnClose = new Button () {
+                Width = Application.GetRealWidth (250),
+                TextID = SimpleControl.R.MyInternationalizationString.Close,
+                TextColor = SkinStyle.Current.DialogTextColor,
+                BackgroundColor = SkinStyle.Current.DialogTitle,
+            };
+            BottomView.AddChidren (btnClose);
+            btnClose.MouseUpEventHandler += (sdf, fds) => {
+                dialog.Close ();
+            };
+
+            Button btnOption = new Button () {
+                X = btnClose.Right + Application.GetRealWidth (2),
+                Width = Application.GetRealWidth (250),
+                TextID = SimpleControl.R.MyInternationalizationString.SAVE,
+                TextColor = SkinStyle.Current.DialogTextColor,
+                BackgroundColor = SkinStyle.Current.DialogTitle,
+            };
+            BottomView.AddChidren (btnOption);
+
+            btnOption.MouseUpEventHandler += (sdf, fds) => {
+                //淇敼鍚嶇О
+                var newStr = etZoneName.Text.Trim ();
+                if (nameStr != newStr) {
+                    saveAction?.Invoke (newStr);
+                }
+                dialog.Close ();
+            };
+            dialog.Show ();
+        }
+        #endregion
+
+
+        #region 鈻� 鍏辩敤涓婁紶澶囦唤鎿嶄綔_______________________
+        /// <summary>
+        /// 灏嗗綋鍓嶆湰鍦版暟鎹笂浼犲埌鑷姩澶囦唤
+        /// </summary>
+        /// <param name="finishAction">涓婁紶缁撴潫浜嬩欢</param>
+        public void ShowUploadToAutomaticBackupDialog (Action finishAction)
+        {
+            Dialog dialog = new Dialog ();
+
+            FrameLayout dialogBodyView = new FrameLayout () {
+                Gravity = Gravity.Center,
+                Width = Application.GetRealWidth (500),
+                Height = Application.GetRealHeight (500),
+                BackgroundColor = SkinStyle.Current.DialogColor,
+                Radius = 5,
+                BorderColor = SkinStyle.Current.Transparent,
+                BorderWidth = 0,
+            };
+            dialog.AddChidren (dialogBodyView);
+
+            Button btnTitle = new Button () {
+                Height = Application.GetRealHeight (80),
+                BackgroundColor = SkinStyle.Current.DialogTitle,
+                TextAlignment = TextAlignment.Center,
+                TextID = SimpleControl.R.MyInternationalizationString.Backup,
+                TextColor = SkinStyle.Current.DialogTextColor
+            };
+            dialogBodyView.AddChidren (btnTitle);
+            //Button btnGoDownTip = new Button () {
+            //    Gravity = Gravity.CenterHorizontal,
+            //    Y = Application.GetRealHeight (90 - 26),
+            //    Width = Application.GetRealWidth (53),
+            //    Height = Application.GetRealHeight (26),
+            //    UnSelectedImagePath = "Room/godown.png",
+            //};
+            //dialogBodyView.AddChidren (btnGoDownTip);
+
+            Button btnackupRemark = new Button () {
+                Gravity = Gravity.CenterHorizontal,
+                Y = Application.GetRealHeight (100),
+                Width = Application.GetRealWidth (400),
+                Height = Application.GetRealHeight (80),
+                TextID = SimpleControl.R.MyInternationalizationString.Remark,
+                TextAlignment = TextAlignment.CenterLeft,
+                TextColor = SkinStyle.Current.TextColor,
+            };
+            dialogBodyView.AddChidren (btnackupRemark);
+
+            EditText etBackupRemark = new EditText () {
+                Gravity = Gravity.CenterHorizontal,
+                Y = btnackupRemark.Bottom,
+                Width = Application.GetRealWidth (400),
+                Height = Application.GetRealHeight (80),
+                TextAlignment = TextAlignment.Center,
+                Radius = 5,
+                BorderColor = SkinStyle.Current.BorderColor,
+                BorderWidth = 1,
+                TextColor = SkinStyle.Current.TextColor,
+            };
+            dialogBodyView.AddChidren (etBackupRemark);
+            etBackupRemark.EditorEnterAction += (obj) => {
+                Application.HideSoftInput ();
+            };
+            FrameLayout bottomView = new FrameLayout () {
+                Y = Application.GetRealHeight (420),
+                Height = Application.GetRealHeight (85),
+                BackgroundColor = SkinStyle.Current.DialogTitle
+            };
+            dialogBodyView.AddChidren (bottomView);
+
+            Button btnClose = new Button () {
+                Width = Application.GetRealWidth (249),
+                TextID = SimpleControl.R.MyInternationalizationString.Close,
+                TextAlignment = TextAlignment.Center
+            };
+            bottomView.AddChidren (btnClose);
+            btnClose.MouseUpEventHandler += (send2er, e2) => {
+                dialog.Close ();
+            };
+
+            Button btnBottomLine = new Button () {
+                X = btnClose.Right,
+                Width = 1,
+                BackgroundColor = SkinStyle.Current.Black50Transparent,
+            };
+            bottomView.AddChidren (btnBottomLine);
+
+            Button btnSave = new Button () {
+                X = btnBottomLine.Right,
+                Width = Application.GetRealWidth (249),
+                TextID = SimpleControl.R.MyInternationalizationString.SAVE,
+                TextAlignment = TextAlignment.Center
+            };
+            bottomView.AddChidren (btnSave);
+
+            btnSave.MouseUpEventHandler += (sender2, e2) => {
+                if (etBackupRemark.Text.Trim () == "") {
+                    new Alert (Language.StringByID (SimpleControl.R.MyInternationalizationString.Tip), Language.StringByID (SimpleControl.R.MyInternationalizationString.InputNewBakeUpFilesName),
+                                   Language.StringByID (SimpleControl.R.MyInternationalizationString.Close)).Show ();
+                    return;
+                }
+                if (MainPage.LoginUser == null) {
+                    new Alert (Language.StringByID (SimpleControl.R.MyInternationalizationString.Tip), Language.StringByID (SimpleControl.R.MyInternationalizationString.PleaseLoginSystem),
+                                  Language.StringByID (SimpleControl.R.MyInternationalizationString.Close)).Show ();
+                    return;
+                }
+                userBakeupFile (etBackupRemark.Text.Trim (), finishAction);
+                dialog.Close ();
+            };
+            dialog.Show ();
+
+        }
+
+
+        ///// <summary>
+        ///// 浜や簰澶囦唤
+        ///// </summary>
+        //int InteractiveBackup = 0;
+        ///// <summary>
+        ///// 鑷畾涔夊伐绋嬪浠�
+        ///// </summary>
+        //int CustomProjectBackup = 1;
+        ///// <summary>
+        ///// 鐢ㄦ埛鑷姩澶囦唤
+        ///// </summary>
+        //int UserAutoBackup = 2;
+        ///// <summary>
+        ///// 鐢ㄦ埛鑷畾涔夊浠�
+        ///// </summary>
+        //int UserCustomizedBackup = 3;
+        /// <summary>
+        /// 鐢ㄦ埛澶囦唤鏂囦欢
+        /// </summary>
+        void userBakeupFile (string folderName, Action finishAction)
+        {
+            MainPage.Loading.Start ("Upload...");
+            System.Threading.Tasks.Task.Run (() => {
+                try {
+                    var revertObj = HttpServerRequest.Current.CreateBackupFolder(folderName);
+                    if (revertObj.Code == StateCode.SUCCESS) {
+                        var mBackupFolderCreateRes = Newtonsoft.Json.JsonConvert.DeserializeObject<BackupFolderCreateRes> (revertObj.Data.ToString ());
+                        //UpLoadBackupFileNEW (mBackupFolderCreateRes.id);
+                        UpLoadBackupFileOne (mBackupFolderCreateRes.id);
+                    } else {
+                        IMessageCommon.Current.ShowErrorInfoAlter (revertObj.Code);
+                    }
+                } catch (Exception ex) {
+                    Shared.Application.RunOnMainThread (() => {
+                        new Alert (Language.StringByID (SimpleControl.R.MyInternationalizationString.Tip), Language.StringByID (SimpleControl.R.MyInternationalizationString.CheckInternet),
+                                   Language.StringByID (SimpleControl.R.MyInternationalizationString.Close)).Show ();
+                        Shared.Utlis.WriteLine (ex.Message);
+                    });
+                } finally {
+                    Shared.Application.RunOnMainThread (() => {
+                        MainPage.Loading.Hide ();
+                        //缁撴潫浜嬩欢
+                        finishAction?.Invoke ();
+                    });
+                }
+            });
+        }
+
+        
+
+
+        //2020-01-11
+        /// <summary>
+        /// 鍒犻櫎鏌愪釜澶囦唤
+        /// </summary>
+        /// <param name="folderID"></param>
+        void DeleteFolderDataAfterUploadFailed (string folderID)
+        {
+            try {
+                var revertObj = HttpServerRequest.Current.DeleteBackupFolder (folderID);
+                if (revertObj.Code == StateCode.SUCCESS) {
+                    //GetHomeDataBackupList ();
+                } else {
+                    //IMessageCommon.Current.ShowErrorInfoAlter (API.API_DELETE_Folder_Delete, revertObj.StateCode);
+                }
+            } catch { } finally {
+                Application.RunOnMainThread (() => {
+                    MainPage.Loading.Hide ();
+                });
+            }
+        }
+
+        /// <summary>
+        /// 涓婁紶澶囦唤鏂囦欢
+        /// 澶氭涓婁紶澶氫釜鏂囦欢
+        /// </summary>
+        /// <param name="mBackupClassId"></param>
+        public void UpLoadBackupFileOne (string folderId)
+        {
+            var backuplist = IO.FileUtils.ReadFiles ();
+            var mFileList = backuplist.FindAll ((obj) => obj != "null" && obj != UserConfig.configFile && obj != UserInfo.GlobalRegisterFile && obj != "AccountListDB" && obj != CommonConfig.ConfigFile && (!obj.Contains (".json")));
+
+            if (mFileList.Count <= 0) return;
+
+            int resultCount = mFileList.Count;
+            int index = 0;
+            foreach (var fileName in mFileList) {
+                byte [] fileBytes = IO.FileUtils.ReadFile (fileName);
+                var result = UploadDataBackupByOne (folderId, fileName, fileBytes);
+                if (result) {
+                    index++;
+                    Application.RunOnMainThread (() => {
+                        int pro = (int)(index * 1.0 / resultCount * 100);
+                        MainPage.Loading.Text = pro.ToString () + "%";
+                    });
+                } else {
+                    Console.WriteLine ("涓婁紶澶辫触锛�" + fileName);
+                }
+            }
+
+
+            Utlis.WriteLine ($"涓婁紶瀹屾垚");
+
+            if (index != resultCount) {
+                //2020-01-11 澶囦唤澶辫触
+                DeleteFolderDataAfterUploadFailed (folderId);
+                Shared.Application.RunOnMainThread (() => {
+                    new Alert ("", Language.StringByID (SimpleControl.R.MyInternationalizationString.FailedToBackupFile), Language.StringByID (SimpleControl.R.MyInternationalizationString.Close)).Show ();
+                    MainPage.Loading.Hide ();
+                });
+                return;
+            }
+
+            Application.RunOnMainThread (() => {
+                //MainPage.Loading.Text = "100%";
+                new Alert ("", Language.StringByID (SimpleControl.R.MyInternationalizationString.BackupFileIsSuccessful), Language.StringByID (SimpleControl.R.MyInternationalizationString.Close)).Show ();
+            });
+
+        }
+
+        /// <summary>
+        /// 
+        /// </summary>
+        /// <param name="folderId"></param>
+        /// <param name="fileName"></param>
+        /// <param name="fileBytes"></param>
+        /// <returns></returns>
+        bool UploadDataBackupByOne (string folderId, string fileName, byte [] fileBytes)
+        {
+            try {
+                var queryDic = new Dictionary<string, object> ();
+                queryDic.Add ("folderId", folderId);
+                queryDic.Add ("fileName", fileName);
+                queryDic.Add ("homeId", UserConfig.Instance.CurrentRegion.Id);
+
+                var revertObj = HttpUtil.RequestHttpsUpload (RestSharp.Method.POST, NewAPI.API_POST_File_Create, fileBytes, queryDic, null, UserConfig.Instance.CurrentRegion.regionUrl);
+                if (revertObj.Code == StateCode.SUCCESS) {
+                    return true;
+                } else {
+                    //鎻愮ず閿欒
+                    return false;
+                }
+            } catch {
+                return false;
+            }
+        }
+
+    
+        #endregion
+
+        //#region 鈻� 鏂囦欢鎿嶄綔_______________________
+        ///// <summary>
+        ///// 鍒犻櫎鏈湴鏂囦欢
+        ///// </summary>
+        //public void ReadFilesAndDelete ()
+        //{
+        //    var fileNames = IO.FileUtils.ReadFiles ();
+        //    foreach (var fileName in fileNames) {
+        //        if (fileName == UserInfo.GlobalRegisterFile || fileName == UserConfig.configFile || fileName == MqttInfoConfig.ConfigFile || fileName == APIInfoConfig.ConfigFile) {
+        //            continue;
+        //        }
+        //        IO.FileUtils.DeleteFile (fileName);
+        //    }
+
+        //}
+        //#endregion
+
 
     }
 }

--
Gitblit v1.8.0