From c7df85937f73fb347ee0b19e9c052d2d00a6df6c Mon Sep 17 00:00:00 2001
From: 黄学彪 <hxb@hdlchina.com.cn>
Date: 星期五, 18 九月 2020 13:58:19 +0800
Subject: [PATCH] 新版本

---
 ZigbeeApp/Shared/Phone/TemplateData/TemplateCommonLogic.cs |  342 ++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 289 insertions(+), 53 deletions(-)

diff --git a/ZigbeeApp/Shared/Phone/TemplateData/TemplateCommonLogic.cs b/ZigbeeApp/Shared/Phone/TemplateData/TemplateCommonLogic.cs
index f435350..2407f44 100755
--- a/ZigbeeApp/Shared/Phone/TemplateData/TemplateCommonLogic.cs
+++ b/ZigbeeApp/Shared/Phone/TemplateData/TemplateCommonLogic.cs
@@ -171,7 +171,7 @@
         /// </summary>
         /// <param name="strData">妯℃澘鏂囦欢涓殑琛屾暟鎹�</param>
         /// <param name="hadRoom">鏄惁宸茬粡鏈変簡鎴块棿</param>
-        private bool AdjustTemplateBinFileContentOnLoadMemory(string strData,bool hadRoom)
+        private bool AdjustTemplateBinFileContentOnLoadMemory(string strData, bool hadRoom)
         {
             //鍦烘櫙瀵硅薄
             if (strData == "#SceneTemplate END#")
@@ -510,9 +510,17 @@
             {
                 modelData = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelDeviceBindData>(fileData);
             }
-            else if (saveDiv == ModelDeviceSaveEnum.A闈㈡澘鑳屽厜鐏�)
+            else if (saveDiv == ModelDeviceSaveEnum.A闈㈡澘浜害璋冭妭)
             {
-                modelData = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelPanelBackLightInfo>(fileData);
+                modelData = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelPanelBrightnessAdjustInfo>(fileData);
+            }
+            else if (saveDiv == ModelDeviceSaveEnum.A闈㈡澘鑺傝兘妯″紡)
+            {
+                modelData = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelPanelEnergyModeInfo>(fileData);
+            }
+            else if (saveDiv == ModelDeviceSaveEnum.A闈㈡澘鎸囩ず鐏�)
+            {
+                modelData = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelPanelIndicatorLightInfo>(fileData);
             }
             else if (saveDiv == ModelDeviceSaveEnum.A闈㈡澘闇囧姩鍔熻兘)
             {
@@ -581,7 +589,7 @@
                 {
                     fileName = model.FileName;
                     //澶囦唤鏁版嵁宸茬粡瀛樺湪,鏄惁瑕嗙洊?
-                    this.ShowMassage(ShowMsgType.Confirm, "澶囦唤鏁版嵁宸茬粡瀛樺湪,鏄惁瑕嗙洊?", () =>
+                    this.ShowMassage(ShowMsgType.Confirm, Language.StringByID(R.MyInternationalizationString.BackUpDataIsEsixtAndPickUp), () =>
                     {
                         //灏嗘ā鏉挎暟鎹繚瀛樺埌鍒版寚瀹氱殑鏂囦欢澶逛腑
                         this.SaveTemplateDataToLocation2(fileName, backupName);
@@ -617,7 +625,7 @@
                 else
                 {
                     //鏈湴澶囦唤淇濆瓨鎴愬姛
-                    this.ShowMassage(ShowMsgType.Tip, "鏈湴澶囦唤淇濆瓨鎴愬姛");
+                    this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.SaveLocalBackDataSuccess));
                 }
             });
         }
@@ -661,6 +669,9 @@
                 TemplateDeviceDataLogic.Current.ReDeviceEpointName(device, Common.LocalDevice.Current.GetDeviceEpointName(device));
             }
         }
+        #endregion
+
+        #region 鈻� 鑾峰彇妯℃澘瀵硅薄鐩稿叧___________________
 
         /// <summary>
         /// 鑾峰彇鏈湴鍏ㄩ儴鐨勬ā鏉垮垪琛ㄧ殑鍩烘湰淇℃伅
@@ -716,12 +727,265 @@
         }
 
         /// <summary>
+        /// 鑾峰彇浜戠鍏ㄩ儴鐨勬ā鏉垮垪琛ㄧ殑鍩烘湰淇℃伅
+        /// </summary>
+        /// <returns></returns>
+        public List<CloundModelBaseInfo> GetCloundAllModelList()
+        {
+            var pra = new
+            {
+                RequestVersion = Common.CommonPage.RequestVersion,
+                RequestSource = 5,
+                LoginAccessToken = Common.Config.Instance.Token
+            };
+            var result = UserCenterLogic.GetResponseDataByRequestHttps("ProjectTemplate/GetProTemplateList", false, pra);
+            if (result == null) { return new List<CloundModelBaseInfo>(); }
+
+            var dicData = new Dictionary<string, List<CloundModelBaseInfo>>();
+            var listTime = new List<string>();
+
+            var listCloundData = Newtonsoft.Json.JsonConvert.DeserializeObject<List<CloundModelBaseInfo>>(result);
+            foreach (var data in listCloundData)
+            {
+                //鍙樻洿鏃堕棿鏍煎紡
+                data.CreatedOnUtc = UserCenterLogic.ConvertUtcTimeToLocalTime(data.CreatedOnUtc).ToString("yyyy.MM.dd HH:mm");
+                if (dicData.ContainsKey(data.CreatedOnUtc) == false)
+                {
+                    dicData[data.CreatedOnUtc] = new List<CloundModelBaseInfo>();
+                    listTime.Add(data.CreatedOnUtc);
+                }
+                dicData[data.CreatedOnUtc].Add(data);
+            }
+
+            //鎸夋椂闂存帓搴�
+            listTime.Sort();
+
+            var listData = new List<CloundModelBaseInfo>();
+            for (int i = listTime.Count - 1; i >= 0; i--)
+            {
+                listData.AddRange(dicData[listTime[i]]);
+            }
+            return listData;
+        }
+
+        /// <summary>
         /// 鑾峰彇涓�涓柊鐨勬ā鏉夸繚瀛樻枃浠跺悕
         /// </summary>
         /// <returns></returns>
         public string GetNewTemplateFileName()
         {
-            return "ModelData_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".bin";
+            return "ModelData_Local_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".bin";
+        }
+
+        #endregion
+
+        #region 鈻� 涓婁紶妯℃澘澶囦唤_______________________
+
+        /// <summary>
+        /// 涓婁紶妯℃澘澶囦唤(鍐呴儴浣跨敤绾跨▼鏉ユ墽琛�,鏈夎浆鍦堢殑鐣岄潰)
+        /// </summary>
+        /// <param name="i_localTemplate">鏈湴妯℃澘淇℃伅</param>
+        /// <param name="i_saveName">澶囦唤鍚嶅瓧</param>
+        public void UpLoadTemplateData(LocalModelBaseInfo i_localTemplate, string i_saveName)
+        {
+            HdlThreadLogic.Current.RunThread(() =>
+            {
+                ProgressBar.Show();
+
+                //鑾峰彇浜戠鐨勬ā鏉垮垪琛�
+                var listTemplate = this.GetCloundAllModelList();
+                foreach (var data in listTemplate)
+                {
+                    if (data.TemplateName == i_saveName)
+                    {
+                        //妯℃澘鍚嶅瓧宸茬粡瀛樺湪
+                        this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.TheTemplateNameIsEsixt));
+                        ProgressBar.Close();
+                        return;
+                    }
+                }
+                //杩欓噷淇敼鎺夋ā鏉挎枃浠堕噷闈㈣杞界殑妯℃澘鍚嶇О
+                string templateFile = System.IO.Path.Combine(DirNameResourse.AllResidenceTemplateDirectory, i_localTemplate.FileName);
+                string binFileData = HdlFileLogic.Current.ReadFileTextContent(templateFile);
+                var arryBinFile = binFileData.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
+                //鏇挎崲鐩爣 杩欓噷鏄ā鏉垮熀鏈俊鎭殑json鏁版嵁
+                string strFileData = arryBinFile[1];
+                var templateBaseInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<LocalModelBaseInfo>(strFileData);
+                templateBaseInfo.ModelName = i_saveName;//鏇存敼鎺夊悕瀛�
+                templateBaseInfo.EditorTime = DateTime.Now.ToString("yyyy.MM.dd HH:mm");//鏇存敼鎺夋椂闂�
+                //鏇挎崲瀵硅薄
+                string replaceDta = Newtonsoft.Json.JsonConvert.SerializeObject(templateBaseInfo);
+                binFileData = binFileData.Replace(strFileData, replaceDta);
+
+                var pra = new
+                {
+                    RequestVersion = Common.CommonPage.RequestVersion,
+                    RequestSource = 5,
+                    LoginAccessToken = Common.Config.Instance.Token,
+                    ProTemplateName = i_saveName,
+                    ProTemplateDetails = new List<TemplateDetailsInfo>()
+                };
+                var detailsInfo = new TemplateDetailsInfo();
+                detailsInfo.DetailName = "ModelData_Cloud_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".bin";
+                detailsInfo.DetailContent = Encoding.UTF8.GetBytes(binFileData);
+                pra.ProTemplateDetails.Add(detailsInfo);
+
+                //娓呮帀杩欎釜瀛楃涓茬紦瀛�
+                binFileData = null;
+
+                var result = UserCenterLogic.GetResultStatuByRequestHttps("ProjectTemplate/AddProTemplate", false, pra);
+                if (result == true)
+                {
+                    //涓婁紶妯℃澘鎴愬姛
+                    this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.UploadTemplateSuccess));
+                }
+                else
+                {
+                    //涓婁紶妯℃澘澶辫触
+                    this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.UploadTemplateFail));
+                }
+                ProgressBar.Close();
+            });
+        }
+
+        /// <summary>
+        /// 涓婁紶妯℃澘鏂囦欢鐨勪俊鎭�
+        /// </summary>
+        private class TemplateDetailsInfo
+        {
+            /// <summary>
+            /// 鏂囦欢鍚嶅瓧
+            /// </summary>
+            public string DetailName = string.Empty;
+            /// <summary>
+            /// 妯℃澘鍐呭
+            /// </summary>
+            public byte[] DetailContent = null;
+        }
+
+        #endregion
+
+        #region 鈻� 涓嬭浇妯℃澘澶囦唤_______________________
+
+        /// <summary>
+        /// 涓嬭浇妯℃澘澶囦唤(鍐呴儴鏄娇鐢ㄧ嚎绋嬫墽琛�,鏈夌晫闈㈠瀷杩涘害鏉�)
+        /// </summary>
+        /// <param name="i_templateId">鏁版嵁搴撲富閿�</param>
+        /// <param name="i_SuccessAction">涓嬭浇瀹屽叏鎴愬姛涔嬪悗鐨勫洖璋冧簨浠�,鍙傛暟涓轰繚瀛樻ā鏉跨殑鍏ㄨ矾寰�(鍙傛暟null浠h〃澶辫触)</param>
+        public void DownLoadTemplate(string i_templateId, Action<string> i_SuccessAction = null)
+        {
+            HdlThreadLogic.Current.RunThread(() =>
+            {
+                ProgressFormBar.Current.Start();
+                ProgressFormBar.Current.SetMsg("姝e湪涓嬭浇妯℃澘鏁版嵁");
+                System.Threading.Thread.Sleep(1500);
+
+                var pra = new
+                {
+                    RequestVersion = Common.CommonPage.RequestVersion,
+                    RequestSource = 5,
+                    LoginAccessToken = Common.Config.Instance.Token,
+                    ProTemplateId = i_templateId
+                };
+                var result = UserCenterLogic.GetResponseDataByRequestHttps("ProjectTemplate/GetProTemplateDetailList", false, pra);
+                if (result == null)
+                {
+                    //涓嬭浇妯℃澘澶辫触
+                    this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.DownloadTemplateFail));
+                    ProgressFormBar.Current.Close();
+                    i_SuccessAction?.Invoke(null);
+                    return;
+                }
+                var fileListData = Newtonsoft.Json.JsonConvert.DeserializeObject<List<DownloadTemplateData>>(result);
+                if (fileListData.Count == 0)
+                {
+                    //涓嬭浇妯℃澘澶辫触
+                    this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.DownloadTemplateFail));
+                    ProgressFormBar.Current.Close();
+                    i_SuccessAction?.Invoke(null);
+                    return;
+                }
+                //瑙f瀽杩欎釜妯℃澘鐨勫悕瀛�
+                var strFileData = this.GetDataFromFileContent(Encoding.UTF8.GetString(fileListData[0].DetailContent), "#START#", "#TemplateData END#");
+                var templateBaseInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<LocalModelBaseInfo>(strFileData);
+
+                //妫�娴嬫湰鍦扮殑妯℃澘鏄惁鏈夊悓鍚嶇殑
+                var listLocal = this.GetLocalAllModelList();
+                string fileName = fileListData[0].DetailName;
+                foreach (var localData in listLocal)
+                {
+                    if (localData.ModelName == templateBaseInfo.ModelName)
+                    {
+                        //鏇挎崲,鐩存帴浣跨敤鏈湴鐨勬ā鏉挎枃浠跺悕瀛�
+                        fileName = localData.FileName;
+                        break;
+                    }
+                }
+
+                //瀛樻垚鏂囦欢
+                string fileFullName = System.IO.Path.Combine(DirNameResourse.AllResidenceTemplateDirectory, fileName);
+                HdlFileLogic.Current.SaveByteToFile(fileFullName, fileListData[0].DetailContent);
+
+                //鑾峰彇鍗囩骇鍥轰欢鏂囦欢
+                var result2 = HdlFirmwareUpdateLogic.DownLoadTemplateDeviceFirmware(fileFullName, "姝e湪鑾峰彇鍗囩骇鍥轰欢鏁版嵁");
+                if (result2 == -1)
+                {
+                    this.ShowMassage(ShowMsgType.Tip, "鑾峰彇鍗囩骇鍥轰欢鏁版嵁澶辫触");
+                    ProgressFormBar.Current.Close();
+                    i_SuccessAction?.Invoke(null);
+                    return;
+                }
+                ProgressFormBar.Current.Close();
+                i_SuccessAction?.Invoke(fileFullName);
+            });
+        }
+
+        /// <summary>
+        /// 涓嬭浇妯℃澘
+        /// </summary>
+        private class DownloadTemplateData
+        {
+            /// <summary>
+            /// 妯℃澘鏂囦欢鍚嶅瓧
+            /// </summary>
+            public string DetailName = string.Empty;
+            /// <summary>
+            /// 妯℃澘鏁版嵁
+            /// </summary>
+            public byte[] DetailContent = null;
+        }
+
+        #endregion
+
+        #region 鈻� 鍒犻櫎妯℃澘澶囦唤_______________________
+
+        /// <summary>
+        /// 鍒犻櫎浜戠妯℃澘澶囦唤
+        /// </summary>
+        /// <param name="i_templateId">妯℃澘涓婚敭</param>
+        /// <returns></returns>
+        public bool DeleteTemplateFromDb(string i_templateId)
+        {
+            var pra = new
+            {
+                RequestVersion = Common.CommonPage.RequestVersion,
+                RequestSource = 5,
+                LoginAccessToken = Common.Config.Instance.Token,
+                ProTemplateId = i_templateId
+            };
+            var result = UserCenterLogic.GetResultStatuByRequestHttps("ProjectTemplate/DeleteProTemplate", false, pra);
+            return result;
+        }
+
+        /// <summary>
+        /// 鍒犻櫎鏈湴妯℃澘澶囦唤
+        /// </summary>
+        /// <param name="i_baseInfo">鏈湴妯℃澘鐨勫熀鏈俊鎭�</param>
+        /// <returns></returns>
+        public void DeleteLocalTemplate(LocalModelBaseInfo i_baseInfo)
+        {
+            var fullFile = System.IO.Path.Combine(DirNameResourse.AllResidenceTemplateDirectory, i_baseInfo.FileName);
+            HdlFileLogic.Current.DeleteFile(fullFile);
         }
 
         #endregion
@@ -1053,7 +1317,7 @@
                     }
                 }
                 //涓嶇濡備綍,閮介渶瑕佸垯閲嶆柊淇濆瓨鎴愭枃浠�
-                this.SaveDeviceMemmoryData(device);
+                this.SaveDeviceMemmoryData(device.DeviceAddr, device.DeviceEpoint);
                 //杩樺師鍙婂彉鏇村満鏅殑鎵ц鐩爣
                 //this.RecoverAndChangedSceneAdjustTarget(device, targetMac);
             }
@@ -1206,12 +1470,12 @@
                 HdlGatewayLogic.Current.ChangedGatewayRoom(sourceGwid, this.modelData.dicDeviceTemplateRealRoom[targetGwid]);
             }
             //鍙樻洿缃戝叧鍚嶅瓧
-            if (this.modelData.dicGatewayInfo.ContainsKey(targetGwid) == true)
-            {
-                var localGateway = HdlGatewayLogic.Current.GetLocalGateway(sourceGwid);
-                string gwName = HdlGatewayLogic.Current.GetGatewayName(this.modelData.dicGatewayInfo[targetGwid]);
-                HdlGatewayLogic.Current.ReName(localGateway, gwName);
-            }
+            //if (this.modelData.dicGatewayInfo.ContainsKey(targetGwid) == true)
+            //{
+            //    var localGateway = HdlGatewayLogic.Current.GetLocalGateway(sourceGwid);
+            //    string gwName = HdlGatewayLogic.Current.GetGatewayName(this.modelData.dicGatewayInfo[targetGwid]);
+            //    HdlGatewayLogic.Current.ReName(localGateway, gwName);
+            //}
         }
 
         /// <summary>
@@ -1412,12 +1676,13 @@
         #region 鈻� 淇濆瓨璁惧缂撳瓨_______________________
 
         /// <summary>
-        /// 淇濆瓨璁惧缂撳瓨
+        /// 淇濆瓨璁惧缂撳瓨(鑰冭檻鏈夌殑璁惧鐢ㄧ殑鏄�200绔偣,鎵�浠ヨ繖閲屾渶濂戒笉鐢ㄨ澶囧璞′綔涓哄弬鏁�)
         /// </summary>
-        /// <param name="device">璁惧瀵硅薄</param>
-        public void SaveDeviceMemmoryData(CommonDevice device)
+        /// <param name="deviceMac">璁惧mac</param>
+        /// <param name="deviceEpoint">璁惧Epoint</param>
+        public void SaveDeviceMemmoryData(string deviceMac, int deviceEpoint)
         {
-            string mainkey = Common.LocalDevice.Current.GetDeviceMainKeys(device);
+            string mainkey = Common.LocalDevice.Current.GetDeviceMainKeys(deviceMac, deviceEpoint);
             if (this.modelData.dicDeviceTemplateData.ContainsKey(mainkey) == false)
             {
                 return;
@@ -1425,7 +1690,7 @@
 
             //淇濆瓨璺緞
             string saveFile = DirNameResourse.LocalTemplateDirectory;
-            saveFile = System.IO.Path.Combine(saveFile, device.FilePath);
+            saveFile = System.IO.Path.Combine(saveFile, mainkey);
 
             var listData = this.modelData.dicDeviceTemplateData[mainkey];
             if (listData.Count == 0)
@@ -1791,14 +2056,14 @@
         }
 
         /// <summary>
-        /// 鑾峰彇璁惧淇濆瓨鐨勬ā鏉垮璞�
+        /// 鑾峰彇璁惧淇濆瓨鐨勬ā鏉垮璞�(鑰冭檻鏈夌殑璁惧鐢ㄧ殑鏄�200绔偣,鎵�浠ヨ繖閲屾渶濂戒笉鐢ㄨ澶囧璞′綔涓哄弬鏁�)
         /// </summary>
         /// <param name="device"></param>
         /// <param name="saveEnum"></param>
         /// <returns></returns>
-        public TemplateDeviceDataCommon GetDeviceModelDataClass(CommonDevice device, ModelDeviceSaveEnum saveEnum, TemplateDeviceDataCommon newClass)
+        public TemplateDeviceDataCommon GetDeviceModelDataClass(string deviceMac, int deviceEpoint, ModelDeviceSaveEnum saveEnum, TemplateDeviceDataCommon newClass)
         {
-            string mainkey = Common.LocalDevice.Current.GetDeviceMainKeys(device);
+            string mainkey = Common.LocalDevice.Current.GetDeviceMainKeys(deviceMac, deviceEpoint);
 
             //鍒涘缓瀛樺偍绌洪棿
             if (this.modelData.dicDeviceTemplateData.ContainsKey(mainkey) == false)
@@ -1815,8 +2080,8 @@
             }
             //鏂板缓涓�涓柊鐨勫璞�
             newClass.DataSaveDiv = saveEnum;
-            newClass.DeviceEpoint = device.DeviceEpoint;
-            newClass.DeviceMac = device.DeviceAddr;
+            newClass.DeviceEpoint = deviceEpoint;
+            newClass.DeviceMac = deviceMac;
 
             //榛樿鍒涘缓涓�涓储寮曚綅
             newClass.ListReceiveResult.Add(string.Empty);
@@ -1837,36 +2102,7 @@
         /// <param name="buttonText">鎸夐挳鐨勬枃鏈�</param>
         private void ShowMassage(ShowMsgType msgType, string msg, Action action = null, string buttonText = null)
         {
-            //绌哄璞℃椂锛屼笉鏄剧ず
-            if (string.IsNullOrEmpty(msg))
-            {
-                return;
-            }
-            Application.RunOnMainThread(() =>
-            {
-                var alert = new ShowMsgControl(msgType, msg, buttonText);
-                if (action != null)
-                {
-                    alert.ConfirmClickEvent += () =>
-                    {
-                        try
-                        {
-                            //鍥炶皟鍑芥暟
-                            action?.Invoke();
-                        }
-                        catch (Exception ex)
-                        {
-                            //鍑虹幇鏈煡閿欒,鏁版嵁涓㈠け
-                            this.ShowMassage(ShowMsgType.Error, Language.StringByID(R.MyInternationalizationString.uUnKnownError));
-
-                            //Log鍑哄姏
-                            HdlLogLogic.Current.WriteLog(ex);
-                        }
-                        action = null;
-                    };
-                }
-                alert.Show();
-            });
+            HdlMessageLogic.Current.ShowMassage(msgType, msg, action, buttonText);
         }
 
         #endregion

--
Gitblit v1.8.0