From 25429f085093d89d543a0b90e30d0d62d1b7dac9 Mon Sep 17 00:00:00 2001 From: hxb <hxb@hdlchina.com.cn> Date: 星期二, 30 八月 2022 09:37:38 +0800 Subject: [PATCH] 合并了IOS的代码 --- ZigbeeApp/Shared/Phone/TemplateData/TemplateCommonLogic.cs | 2139 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 2,139 insertions(+), 0 deletions(-) diff --git a/ZigbeeApp/Shared/Phone/TemplateData/TemplateCommonLogic.cs b/ZigbeeApp/Shared/Phone/TemplateData/TemplateCommonLogic.cs new file mode 100644 index 0000000..ada602b --- /dev/null +++ b/ZigbeeApp/Shared/Phone/TemplateData/TemplateCommonLogic.cs @@ -0,0 +1,2139 @@ +锘縰sing Shared.Phone.UserCenter; +using System; +using System.Collections.Generic; +using System.Text; +using ZigBee.Device; + +namespace Shared.Phone.TemplateData +{ + /// <summary> + /// 妯℃澘鐨勫叡閫氶�昏緫绫� + /// </summary> + public class TemplateCommonLogic + { + #region 鈻� 鍙橀噺澹版槑___________________________ + + /// <summary> + /// 妯℃澘鐨勫叡閫氶�昏緫绫� + /// </summary> + private static TemplateCommonLogic m_Current = null; + /// <summary> + /// 妯℃澘鐨勫叡閫氶�昏緫绫� + /// </summary> + public static TemplateCommonLogic Current + { + get + { + if (m_Current == null) + { + m_Current = new TemplateCommonLogic(); + } + return m_Current; + } + } + /// <summary> + /// 妯℃澘鏁版嵁 + /// </summary> + public TemplateMemoryData modelData = new TemplateMemoryData(); + /// <summary> + /// 妯℃澘鏂囦欢涓� #start# 鍒� #end# 鐨勫唴瀹�(涓存椂鍙橀噺) + /// </summary> + private string strTempContentData = string.Empty; + /// <summary> + /// 鏈湴妯℃澘鏂囦欢鐨勫悕瀛� + /// </summary> + private const string TemplateFileName = "ModelData_Release.bin"; + + #endregion + + #region 鈻� 鍔犺浇鏈湴妯℃澘缂撳瓨___________________ + + /// <summary> + /// 鍔犺浇鏈湴妯℃澘鏂囦欢缂撳瓨(姝ゆ柟娉曚互鏈湴缂撳瓨涓哄噯,鍒囨崲浣忓畢鏃朵娇鐢�) + /// </summary> + public void LoadLocalTemplateMemoryData() + { + this.modelData = new TemplateMemoryData(); + //淇濆瓨鐨勮矾寰� + string saveFile = DirNameResourse.LocalTemplateDirectory; + + //濡傛灉褰撳墠浣忓畢鎷ユ湁閫夋嫨鐨勬ā鏉� + if (Common.Config.Instance.Home.SelectTemplate != string.Empty) + { + string checkFile = System.IO.Path.Combine(saveFile, TemplateFileName); + //濡傛灉鏈湴娌℃湁杩欎釜bin鏂囦欢 + if (System.IO.File.Exists(checkFile) == false) + { + //澶嶅埗妯℃澘bin鏂囦欢鍒版湰鍦扮殑妯℃澘鏂囦欢澶归噷 + this.CopyTemplateFileToLocalDirectory(Common.Config.Instance.Home.SelectTemplate); + } + } + + //鑾峰彇杩欎釜璺緞涓嬮潰鍏ㄩ儴鐨勬枃浠� + var listFile = HdlFileLogic.Current.GetFileFromDirectory(saveFile); + + //妯℃澘Bin鏂囦欢 + string templateBinFile = string.Empty; + //杩欓噷鏄鍙栦粬涓婁竴娆$紪杈戝畬鎴愪箣鍚庣殑妯℃澘鏁版嵁(涔熷氨鏄紪杈戝埌涓�鍗婁箣鍚�,閫�鍑篈pp,涓嬩竴娆″啀缂栬緫) + foreach (var fileName in listFile) + { + if (fileName == TemplateFileName) + { + //妯℃澘Bin鏂囦欢 + templateBinFile = fileName; + continue; + } + if (fileName.StartsWith("Device_") == false) + { + //鍙璁惧 + continue; + } + string fileData = HdlFileLogic.Current.ReadFileTextContent(System.IO.Path.Combine(saveFile, fileName)); + if (fileData == null) + { + continue; + } + string deviceData = string.Empty; + ModelDeviceSaveEnum saveDiv = ModelDeviceSaveEnum.A鏈畾涔�; + //鏍规嵁鎹㈣绗﹀垏鍒嗘暟鎹枃鏈� + string[] arryData = fileData.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); + foreach (string strData in arryData) + { + //璁惧鏁版嵁鏍囧織 + if (strData.StartsWith("===>") == true) + { + if (deviceData != string.Empty) + { + //鍙嶅簭鍒楀寲璁惧鐨勪繚瀛樻枃浠跺唴瀹� + var tempData = this.DeserializeDeviceDataByDiv(saveDiv, deviceData); + //灏嗚澶囨ā鏉挎暟鎹坊鍔犲叆缂撳瓨 + string mainKey = Common.LocalDevice.Current.GetDeviceMainKeys(tempData.DeviceMac, tempData.DeviceEpoint); + this.SetTemplateDeviceDataToMemmory(tempData, deviceData, mainKey, true); + } + //娓呯┖ + deviceData = string.Empty; + saveDiv = (ModelDeviceSaveEnum)Convert.ToInt32(strData.Substring(4)); + continue; + } + deviceData += strData; + } + if (deviceData != string.Empty) + { + //鍙嶅簭鍒楀寲璁惧鐨勪繚瀛樻枃浠跺唴瀹� + var tempData = this.DeserializeDeviceDataByDiv(saveDiv, deviceData); + //灏嗚澶囨ā鏉挎暟鎹坊鍔犲叆缂撳瓨 + string mainKey = Common.LocalDevice.Current.GetDeviceMainKeys(tempData.DeviceMac, tempData.DeviceEpoint); + this.SetTemplateDeviceDataToMemmory(tempData, deviceData, mainKey, true); + } + } + + //璇诲彇妯℃澘Bin鏂囦欢 + if (templateBinFile != string.Empty) + { + bool hadRoom = HdlRoomLogic.Current.GetAllListRooms().Count > 1 && Common.Config.Instance.Home.FloorDics.Count == 0; + //寮�濮嬭鍙栨枃浠跺唴瀹� + this.ReadTemplateFileMethord((strData, saveDiv, deviceType) => + { + //鍦ㄥ姞杞芥湰鍦扮紦瀛樼殑鍓嶆彁涓�,澶勭悊妯℃澘鏂囦欢閲岄潰鐨勭壒娈婂唴瀹� + var result = this.AdjustTemplateBinFileContentOnLoadMemory(strData, hadRoom); + //false浠h〃瀹冧笉鏄壒娈婂唴瀹� + if (result == false) + { + //澶勭悊妯℃澘鏂囦欢閲岄潰鐨勫叡閫氬唴瀹� + //鍥犱负涓婇潰宸茬粡鍔犺浇浜嗘寚瀹氭湰鍦扮殑缂撳瓨,鎵�浠ヨ繖閲屼笉闇�瑕佹坊鍔犲叆dicDeviceTemplateData涓� + this.AdjustTemplateBinFileCommonContent(strData, saveDiv, deviceType, false); + } + }); + } + //娓呯┖瀵硅薄缂撳瓨 + this.strTempContentData = string.Empty; + + //鍔犺浇璁惧鍜岀綉鍏虫ā鏉块�夋嫨鐨勬暟鎹� + this.modelData.dicDeviceTemplateSelect = new Dictionary<string, string>(); + this.modelData.dicGatewayTemplateSelect = new Dictionary<string, string>(); + + string fileData2 = HdlFileLogic.Current.ReadFileTextContent(DirNameResourse.DeviceTemplateSelectFile); + if (fileData2 != null) + { + //璁惧閫夋嫨鐨勬ā鏉垮璞�(keys:鏈湴璁惧鐨凪ac value:妯℃澘涓殑Mac) + this.modelData.dicDeviceTemplateSelect = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(fileData2); + } + fileData2 = HdlFileLogic.Current.ReadFileTextContent(DirNameResourse.GatewayTemplateSelectFile); + if (fileData2 != null) + { + //缃戝叧瀵硅薄閫夋嫨鐨勬ā鏉垮璞�(keys:鏈湴缃戝叧ID, value:妯℃澘涓殑缃戝叧ID) + this.modelData.dicGatewayTemplateSelect = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(fileData2); + } + } + + /// <summary> + /// 鍦ㄥ姞杞芥湰鍦扮紦瀛樼殑鍓嶆彁涓�,澶勭悊妯℃澘鏂囦欢閲岄潰鐨勭壒娈婂唴瀹� + /// </summary> + /// <param name="strData">妯℃澘鏂囦欢涓殑琛屾暟鎹�</param> + /// <param name="hadRoom">鏄惁宸茬粡鏈変簡鎴块棿</param> + private bool AdjustTemplateBinFileContentOnLoadMemory(string strData, bool hadRoom) + { + //鍦烘櫙瀵硅薄 + if (strData == "#SceneTemplate END#") + { + if (hadRoom == false) + { + //鍙垵濮嬪寲涓�娆�,鏈夋埧闂存椂浠h〃宸茬粡涓嶆槸绗竴娆″姞杞戒簡 + var scene = Newtonsoft.Json.JsonConvert.DeserializeObject<Common.SceneUI>(this.strTempContentData); + scene.Save(); + } + this.strTempContentData = string.Empty; + return true; + } + //妤煎眰瀵硅薄 + else if (strData == "#FloorInfo END#") + { + if (hadRoom == false) + { + //鍙垵濮嬪寲涓�娆�,鏈夋埧闂存椂浠h〃宸茬粡涓嶆槸绗竴娆″姞杞戒簡 + Common.Config.Instance.Home.FloorDics = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(this.strTempContentData); + Common.Config.Instance.Home.Save(false); + } + this.strTempContentData = string.Empty; + return true; + } + //鎴块棿瀵硅薄 + else if (strData == "#RoomInfo END#") + { + var room = Newtonsoft.Json.JsonConvert.DeserializeObject<Common.Room>(this.strTempContentData); + //鍚勮嚜绔偣鎵�澶勭殑鎴块棿ID,淇濆瓨璧锋潵 + foreach (var deviceKey in room.ListDevice) + { + this.modelData.dicDeviceTemplateRoom[deviceKey] = room.Id; + } + if (hadRoom == false) + { + //绗竴娆″姞杞�,璁惧鍒楄〃闇�瑕佹竻绌�,閫夋嫨璁惧妯℃澘鏃�,鎵嶆坊鍔� + room.ListDevice.Clear(); + room.Save(); + } + this.strTempContentData = string.Empty; + return true; + } + //璁惧閫夋嫨鐨勬ā鏉� + else if (strData == "#DeviceSelectTemplate END#") + { + //杩欎釜涓滆タ鍦ㄨ繖涓垎鏀笅涓嶄粠鏂囦欢涓鍙�,浠庢湰鍦扮紦瀛樻枃浠跺綋涓幏鍙� + this.strTempContentData = string.Empty; + return true; + } + //缃戝叧閫夋嫨鐨勬ā鏉� + else if (strData == "#GatewaySelectTemplate END#") + { + //杩欎釜涓滆タ鍦ㄨ繖涓垎鏀笅涓嶄粠鏂囦欢涓鍙�,浠庢湰鍦扮紦瀛樻枃浠跺綋涓幏鍙� + this.strTempContentData = string.Empty; + return true; + } + return false; + } + + #endregion + + #region 鈻� 鏍规嵁妯℃澘Bin鏂囦欢鎭㈠鏁版嵁____________ + + /// <summary> + /// 鏍规嵁妯℃澘Bin鏂囦欢,鎭㈠鏁版嵁(鍒嗕袱涓嚱鏁板惂,澶毦鎺у埗浜�) + /// </summary> + public void RecoverDataByTemplateBinFile() + { + //閲嶆柊鍒濆鍖� + this.modelData = new TemplateMemoryData(); + + //寮�濮嬭鍙栨枃浠跺唴瀹� + this.ReadTemplateFileMethord((strData, saveDiv, deviceType) => + { + //鍦ㄦ仮澶嶆暟鎹殑鍓嶆彁涓�,澶勭悊妯℃澘鏂囦欢閲岄潰鐨勭壒娈婂唴瀹� + var result = this.AdjustTemplateBinFileContentOnRecover(strData); + //false浠h〃瀹冧笉鏄壒娈婂唴瀹� + if (result == false) + { + //澶勭悊妯℃澘鏂囦欢閲岄潰鐨勫叡閫氬唴瀹� + //鍥犱负鏄互妯℃澘鏉ユ仮澶嶄綇瀹呮暟鎹�,鎵�浠ヨ繖閲岄渶瑕佹坊鍔犲叆dicDeviceTemplateData涓� + this.AdjustTemplateBinFileCommonContent(strData, saveDiv, deviceType, true); + } + }); + //娓呯┖瀵硅薄缂撳瓨 + this.strTempContentData = string.Empty; + + //鍒犳帀杩欎袱涓繚瀛橀�夋嫨妯℃澘鐨勬枃浠�(杩欎袱涓笢瑗垮彲鑳借繕瀛樺湪) + HdlFileLogic.Current.DeleteFile(DirNameResourse.DeviceTemplateSelectFile); + HdlFileLogic.Current.DeleteFile(DirNameResourse.GatewayTemplateSelectFile); + + //鍐嶆鍒濆鍖栨埧闂� + HdlRoomLogic.Current.InitAllRoom(); + + //鏃犳ā鏉挎ā寮忔椂,鎭㈠澶囦唤鐨勬椂鍊�,鎶婂浠芥枃浠跺垹闄� + if (Common.Config.Instance.Home.TemplateMode != 2) + { + //瀛樻斁鐨勮矾寰� + string fullFile = System.IO.Path.Combine(DirNameResourse.LocalTemplateDirectory, TemplateFileName); + HdlFileLogic.Current.DeleteFile(fullFile); + } + //瑕嗙洊鐗╃悊璁惧鎵�鍦ㄧ殑鎴块棿鏁版嵁 + HdlFileLogic.Current.SaveFileContent(DirNameResourse.DeviceRoomIdFile, this.modelData.dicDeviceTemplateRealRoom); + } + + /// <summary> + /// 鍦ㄦ仮澶嶆暟鎹殑鍓嶆彁涓�,澶勭悊妯℃澘鏂囦欢閲岄潰鐨勭壒娈婂唴瀹� + /// </summary> + /// <param name="strData">妯℃澘鏂囦欢涓殑琛屾暟鎹�</param> + private bool AdjustTemplateBinFileContentOnRecover(string strData) + { + //鍦烘櫙瀵硅薄 + if (strData == "#SceneTemplate END#") + { + var scene = Newtonsoft.Json.JsonConvert.DeserializeObject<Common.SceneUI>(this.strTempContentData); + scene.Save(); + this.strTempContentData = string.Empty; + return true; + } + //妤煎眰瀵硅薄 + else if (strData == "#FloorInfo END#") + { + Common.Config.Instance.Home.FloorDics = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(this.strTempContentData); + Common.Config.Instance.Home.Save(false); + this.strTempContentData = string.Empty; + return true; + } + //鎴块棿瀵硅薄 + else if (strData == "#RoomInfo END#") + { + var room = Newtonsoft.Json.JsonConvert.DeserializeObject<Common.Room>(this.strTempContentData); + room.Save(); + //鍚勮嚜绔偣鎵�澶勭殑鎴块棿ID,淇濆瓨璧锋潵 + foreach (var deviceKey in room.ListDevice) + { + this.modelData.dicDeviceTemplateRoom[deviceKey] = room.Id; + } + this.strTempContentData = string.Empty; + return true; + } + //璁惧閫夋嫨鐨勬ā鏉� + else if (strData == "#DeviceSelectTemplate END#") + { + this.modelData.dicDeviceTemplateSelect = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(this.strTempContentData); + this.strTempContentData = string.Empty; + return true; + } + //缃戝叧閫夋嫨鐨勬ā鏉� + else if (strData == "#GatewaySelectTemplate END#") + { + this.modelData.dicGatewayTemplateSelect = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(this.strTempContentData); + this.strTempContentData = string.Empty; + return true; + } + return false; + } + + #endregion + + #region 鈻� 澶勭悊妯℃澘鏂囦欢閲岄潰鐨勫叡閫氬唴瀹筥________ + + /// <summary> + /// 澶勭悊妯℃澘鏂囦欢閲岄潰鐨勫叡閫氬唴瀹� + /// </summary> + /// <param name="strData">妯℃澘鏂囦欢涓殑琛屾暟鎹�</param> + /// <param name="saveDiv">妯℃澘璁惧淇濆瓨鐨勫尯鍒�(璁惧鏁版嵁鏃舵湁鏁�)</param> + /// <param name="deviceType">妯℃澘涓澶囩殑deviceType(璁惧鏁版嵁鏃舵湁鏁�,鍙嶅皠鐢�)</param> + /// <param name="addToTemplate">鏄惁娣诲姞鍒拌澶囨ā鏉跨紦瀛樹腑</param> + private void AdjustTemplateBinFileCommonContent(string strData, ModelDeviceSaveEnum saveDiv, string deviceType, bool addToTemplate) + { + //妯℃澘鍩烘湰鏁版嵁 + if (strData == "#TemplateData END#") + { + var templateData = Newtonsoft.Json.JsonConvert.DeserializeObject<LocalModelBaseInfo>(this.strTempContentData); + this.modelData.TemplateName = templateData.ModelName; + this.strTempContentData = string.Empty; + return; + } + //璁惧妯℃澘 + else if (strData == "#DeviceTemplate END#") + { + //鍙嶅簭鍒楀寲璁惧鐨勪繚瀛樻枃浠跺唴瀹� + var tempData = this.DeserializeDeviceDataByDiv(saveDiv, this.strTempContentData); + //灏嗚澶囨ā鏉挎暟鎹坊鍔犲叆缂撳瓨(姝ゅ鐗规畩,涓嶉渶瑕佸姞鍏ヨ澶囨ā鏉跨紦瀛樹腑) + string mainKey = Common.LocalDevice.Current.GetDeviceMainKeys(tempData.DeviceMac, tempData.DeviceEpoint); + this.SetTemplateDeviceDataToMemmory(tempData, this.strTempContentData, mainKey, addToTemplate); + this.strTempContentData = string.Empty; + return; + } + //璁惧瀵硅薄 + else if (strData == "#DeviceInfo END#") + { + //鍙嶅簭鍒楀寲璁惧 + CommonDevice device = null; + if (HdlCheckLogic.Current.CheckIsNumber(deviceType) == true) + { + //鏁板�煎瀷涓烘柊鏁版嵁,鐩存帴杞崲 + device = CommonDevice.CommonDeviceByByteString(Convert.ToInt32(deviceType), this.strTempContentData); + } + else + { + //瀛楃涓插瀷涓烘棫鏁版嵁,闇�瑕佺壒娈婂鐞� + var myType = (DeviceType)Enum.Parse(typeof(DeviceType), deviceType); + device = CommonDevice.CommonDeviceByByteString((int)myType, this.strTempContentData); + } + if (device != null) + { + if (this.modelData.dicDeviceInfo.ContainsKey(device.DeviceAddr) == false) + { + this.modelData.dicDeviceInfo[device.DeviceAddr] = new List<CommonDevice>(); + } + this.modelData.dicDeviceInfo[device.DeviceAddr].Add(device); + } + this.strTempContentData = string.Empty; + return; + } + //缃戝叧瀵硅薄鏁版嵁 + else if (strData == "#GatewayInfo END#") + { + //鍙嶅簭鍒楀寲璁惧 + var gateway = Newtonsoft.Json.JsonConvert.DeserializeObject<ZbGateway>(this.strTempContentData); + this.modelData.dicGatewayInfo[gateway.GwId] = gateway; + this.strTempContentData = string.Empty; + return; + } + //鐗╃悊璁惧鐨勬埧闂� + else if (strData == "#DeviceTemplateRealRoom END#") + { + this.modelData.dicDeviceTemplateRealRoom = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(this.strTempContentData); + this.strTempContentData = string.Empty; + return; + } + //妯℃澘浣忓畢鐨勪俊鎭�(鐩墠鍦ㄨ繖閲屽熀鏈病鐢�) + else if (strData == "#TemplateHomeInfo END#") + { + this.strTempContentData = string.Empty; + return; + } + this.strTempContentData += strData; + } + + #endregion + + #region 鈻� 璇诲彇妯℃澘鏂囦欢鍐呭___________________ + + /// <summary> + /// 璇诲彇妯℃澘鏂囦欢鍐呭 + /// </summary> + /// <param name="AdjustAction"> + /// <para>鍙傛暟1:妯℃澘鏂囦欢涓殑琛屾暟鎹�</para> + /// <para>鍙傛暟2:妯℃澘璁惧淇濆瓨鐨勫尯鍒�(璁惧鏁版嵁鏃舵湁鏁�)</para> + /// <para>鍙傛暟3:鏉夸腑璁惧鐨刣eviceType(璁惧鏁版嵁鏃舵湁鏁�,鍙嶅皠鐢�)</para> + /// </param> + private void ReadTemplateFileMethord(Action<string, ModelDeviceSaveEnum, string> AdjustAction) + { + //淇濆瓨鐨勮矾寰� + string saveFile = DirNameResourse.LocalTemplateDirectory; + saveFile = System.IO.Path.Combine(saveFile, TemplateFileName); + + string fileData = HdlFileLogic.Current.ReadFileTextContent(saveFile); + if (fileData == null) + { + AdjustAction = null; + return; + } + + var saveDiv = ModelDeviceSaveEnum.A鏈畾涔�; + var deviceType = string.Empty; + + //鏍规嵁鎹㈣绗﹀垏鍒嗘暟鎹枃鏈� + string[] arryData = fileData.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); + foreach (string strData in arryData) + { + if (strData == "#START#") + { + //鏃犻檮鍔犳暟鎹殑銆愭暟鎹爣棰樸�� + continue; + } + if (strData.StartsWith("#DeviceTemplate START#") == true) + { + //闄勫姞鏁版嵁:璁惧淇濆瓨鍖哄垎 + saveDiv = (ModelDeviceSaveEnum)Convert.ToInt32(strData.Substring(22)); + continue; + } + if (strData.StartsWith("#DeviceInfo START#") == true) + { + //闄勫姞鏁版嵁:璁惧瀵硅薄绫诲瀷 + deviceType = strData.Substring(18); + continue; + } + try + { + //鎵ц鏁版嵁澶勭悊 + AdjustAction(strData, saveDiv, deviceType); + } + catch (Exception ex) + { + HdlLogLogic.Current.WriteLog(ex, "妯℃澘bin鏂囦欢鍑洪棶棰榎r\n" + this.strTempContentData); + this.strTempContentData = string.Empty; + } + } + } + + #endregion + + #region 鈻� 鍙嶅簭鍒楀寲璁惧鐨勪繚瀛樻枃浠跺唴瀹筥________ + + /// <summary> + /// 鍙嶅簭鍒楀寲璁惧鐨勪繚瀛樻枃浠跺唴瀹� + /// </summary> + /// <param name="saveDiv">淇濆瓨鍖哄垎</param> + /// <param name="fileData"></param> + /// <returns></returns> + private TemplateDeviceDataCommon DeserializeDeviceDataByDiv(ModelDeviceSaveEnum saveDiv, string fileData) + { + TemplateDeviceDataCommon modelData = null; + if (saveDiv == ModelDeviceSaveEnum.APir閰嶇疆) + { + modelData = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelPirSensorSettion>(fileData); + } + else if (saveDiv == ModelDeviceSaveEnum.A绌鸿皟鎽嗛鍔熻兘) + { + modelData = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelAcSwingModeSupport>(fileData); + } + else if (saveDiv == ModelDeviceSaveEnum.A绌鸿皟鑷畾涔夋ā寮�) + { + modelData = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelAcModeSupport>(fileData); + } + else if (saveDiv == ModelDeviceSaveEnum.A绐楀笜鎵嬫媺鎺у埗) + { + modelData = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelCurtainHandPullControl>(fileData); + } + else if (saveDiv == ModelDeviceSaveEnum.A绐楀笜鏂瑰悜鍙婇檺浣�) + { + modelData = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelCurtainDirectionAndLimite>(fileData); + } + else if (saveDiv == ModelDeviceSaveEnum.A绔偣鍚嶇О) + { + modelData = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelDeviceEpointNameInfo>(fileData); + } + else if (saveDiv == ModelDeviceSaveEnum.A璁惧鍚嶇О) + { + modelData = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelDeviceMacNameInfo>(fileData); + } + else if (saveDiv == ModelDeviceSaveEnum.A璁惧缁戝畾鍒楄〃) + { + modelData = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelDeviceBindData>(fileData); + } + else if (saveDiv == ModelDeviceSaveEnum.A闈㈡澘浜害璋冭妭) + { + 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闈㈡澘闇囧姩鍔熻兘) + { + modelData = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelPanelVibrationInfo>(fileData); + } + return modelData; + } + + /// <summary> + /// 灏嗚澶囨ā鏉挎暟鎹坊鍔犲叆缂撳瓨 + /// </summary> + /// <param name="modelData">妯℃澘鏁版嵁</param> + /// <param name="fileData">璁惧淇濆瓨鍦ㄦ枃浠朵腑鐨勫唴瀹�(鍙负null)</param> + /// <param name="mainKey">娣诲姞鐨勪富閿�</param> + /// <param name="addToTemplate">鏄惁娣诲姞鍒拌澶囨ā鏉跨紦瀛樹腑</param> + private void SetTemplateDeviceDataToMemmory(TemplateDeviceDataCommon modelData, string fileData, string mainKey, bool addToTemplate) + { + if (modelData == null) + { + return; + } + + //浠庢ā鏉夸富鏂囦欢涓幏鍙栫殑璁惧妯℃澘淇℃伅,鏄笉闇�瑕佹坊鍔犲埌杩欎釜鍙橀噺涓殑 + //鍥犱负瀹冨彧鑳芥參鎱竴涓釜鍖归厤 + if (addToTemplate == true) + { + if (this.modelData.dicDeviceTemplateData.ContainsKey(mainKey) == false) + { + this.modelData.dicDeviceTemplateData[mainKey] = new List<TemplateDeviceDataCommon>(); + } + this.modelData.dicDeviceTemplateData[mainKey].Add(modelData); + } + + if (fileData != null) + { + //涓存椂缂撳瓨:妯℃澘涓悇鑷鐐规墍淇濆瓨鐨勫唴瀹�(keys:璁惧涓婚敭),璁惧閫夋嫨妯℃澘鏃�,妯℃澘鏁版嵁杩佺Щ浣跨敤,鍥犱负鏄紩鐢ㄧ被鍨�,鎵�浠ラ渶瑕侀噸鏂癗ew + if (this.modelData.dicDeviceFileContent.ContainsKey(mainKey) == false) + { + this.modelData.dicDeviceFileContent[mainKey] = new List<TemplateDeviceContent>(); + } + var fileCentent = new TemplateDeviceContent(); + fileCentent.saveDiv = modelData.DataSaveDiv; + fileCentent.FileContent = fileData; + fileCentent.DeviceMac = modelData.DeviceMac; + this.modelData.dicDeviceFileContent[mainKey].Add(fileCentent); + } + } + + #endregion + + #region 鈻� 淇濆瓨妯℃澘鏁版嵁鍒版湰鍦扮浉鍏砡____________ + + /// <summary> + /// 淇濆瓨妯℃澘鏁版嵁鍒版湰鍦� + /// </summary> + /// <param name="backupName">澶囦唤鍚嶇О</param> + public void SaveTemplateDataToLocation(string backupName) + { + //鑾峰彇鏈湴鍏ㄩ儴鐨勬ā鏉垮垪琛ㄧ殑鍩烘湰淇℃伅 + var localModel = this.GetLocalAllModelList(); + var fileName = this.GetNewTemplateFileName(); + foreach (var model in localModel) + { + //鍚嶅瓧涓�鏍锋椂 + if (model.ModelName == backupName) + { + fileName = model.FileName; + //澶囦唤鏁版嵁宸茬粡瀛樺湪,鏄惁瑕嗙洊? + this.ShowMassage(ShowMsgType.Confirm, Language.StringByID(R.MyInternationalizationString.BackUpDataIsEsixtAndPickUp), () => + { + //灏嗘ā鏉挎暟鎹繚瀛樺埌鍒版寚瀹氱殑鏂囦欢澶逛腑 + this.SaveTemplateDataToLocation2(fileName, backupName); + }); + return; + } + } + //灏嗘ā鏉挎暟鎹繚瀛樺埌鍒版寚瀹氱殑鏂囦欢澶逛腑 + this.SaveTemplateDataToLocation2(fileName, backupName); + } + + /// <summary> + /// 淇濆瓨妯℃澘鏁版嵁鍒版湰鍦� + /// </summary> + /// <param name="fileName">淇濆瓨鏂囦欢鐨勫悕瀛�</param> + /// <param name="backName">妯℃澘澶囦唤鐨勫悕瀛�</param> + private void SaveTemplateDataToLocation2(string fileName, string backupName) + { + HdlThreadLogic.Current.RunThread(() => + { + ProgressFormBar.Current.Start(); + ProgressFormBar.Current.SetMsg("姝e湪淇濆瓨妯℃澘鏁版嵁"); + System.Threading.Thread.Sleep(1500); + + //灏嗘ā鏉挎暟鎹繚瀛樺埌鍒版寚瀹氱殑鏂囦欢澶逛腑 + var fileFullName = this.SaveTemplateDataToFile(fileName, backupName); + //鑾峰彇鍗囩骇鍥轰欢鏂囦欢 + var result = HdlFirmwareUpdateLogic.DownLoadTemplateDeviceFirmware(fileFullName, "姝e湪淇濆瓨鍗囩骇鍥轰欢鏁版嵁"); + if (result == -1) + { + this.ShowMassage(ShowMsgType.Tip, "淇濆瓨鍗囩骇鍥轰欢鏁版嵁澶辫触"); + } + else + { + //鏈湴澶囦唤淇濆瓨鎴愬姛 + this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.SaveLocalBackDataSuccess)); + } + ProgressFormBar.Current.Close(); + }); + } + + /// <summary> + /// 鍦ㄧ敓鎴愭ā鏉挎暟鎹箣鍓�,妫�娴嬫ā鏉挎暟鎹� + /// </summary> + private void CheckTempLateDataBeforCreat() + { + //涓轰簡淇濊瘉妯℃澘閲岀殑璁惧鏁板拰鏈湴鐨勪竴鑷�,鎵�浠ユ娴嬩竴涓� + //濡傛灉缂哄皯,鍒欐坊鍔犱慨鏀筂ac鐨勬暟鎹繘鍘� + //鍏朵粬鐨�,濡傛灉涓嶇偣鍑诲悇鑷殑閰嶇疆鐣岄潰,鍒欏綋鍋氭槸榛樿璁惧鍘熸潵鐨勯厤缃姸鎬� + + //鑾峰彇鐩墠宸茬粡璁剧疆璁惧鐗╃悊鍚嶇О鐨勮澶嘙ac + var listMac = new HashSet<string>(); + foreach (var listData in this.modelData.dicDeviceTemplateData.Values) + { + if (listData.Count > 0 && listMac.Contains(listData[0].DeviceMac) == true) + { + //宸茬粡鍔犱簡 + continue; + } + foreach (var data in listData) + { + if (data.DataSaveDiv == ModelDeviceSaveEnum.A璁惧鍚嶇О) + { + listMac.Add(data.DeviceMac); + } + } + } + var listDevice = Common.LocalDevice.Current.listAllDevice; + foreach (var device in listDevice) + { + if (listMac.Contains(device.DeviceAddr) == false) + { + listMac.Add(device.DeviceAddr); + //閲嶆柊娣诲姞Mac鍚嶅瓧缂撳瓨 + TemplateDeviceDataLogic.Current.ReDeviceMacName(device, Common.LocalDevice.Current.GetDeviceMacName(device)); + } + //閲嶆柊娣诲姞绔偣鍚嶅瓧缂撳瓨 + TemplateDeviceDataLogic.Current.ReDeviceEpointName(device, Common.LocalDevice.Current.GetDeviceEpointName(device)); + } + } + #endregion + + #region 鈻� 鑾峰彇妯℃澘瀵硅薄鐩稿叧___________________ + + /// <summary> + /// 鑾峰彇鏈湴鍏ㄩ儴鐨勬ā鏉垮垪琛ㄧ殑鍩烘湰淇℃伅 + /// </summary> + /// <returns></returns> + public List<LocalModelBaseInfo> GetLocalAllModelList() + { + var dicData = new Dictionary<string, List<LocalModelBaseInfo>>(); + var listTime = new List<string>(); + + var strPath = DirNameResourse.AllResidenceTemplateDirectory; + //鑾峰彇鍏ㄩ儴鏂囦欢 + var arryFile = System.IO.Directory.GetFiles(strPath, "ModelData_*"); + foreach (string modelFile in arryFile) + { + //璇诲彇鏂囦欢鍐呭 + var textValue = HdlFileLogic.Current.ReadFileTextContent(modelFile); + if (textValue == null) + { + continue; + } + //浠庢枃浠朵腑鑾峰彇鎸囧畾鐨勫唴瀹� + string modelBaseInfo = this.GetDataFromFileContent(textValue, "#START#", "#TemplateData END#"); + if (modelBaseInfo != string.Empty) + { + var myModel = Newtonsoft.Json.JsonConvert.DeserializeObject<LocalModelBaseInfo>(modelBaseInfo); + myModel.FileName = modelFile.Substring(strPath.Length + 1); + if (dicData.ContainsKey(myModel.EditorTime) == false) + { + dicData[myModel.EditorTime] = new List<LocalModelBaseInfo>(); + listTime.Add(myModel.EditorTime); + } + dicData[myModel.EditorTime].Add(myModel); + + string homeData = this.GetDataFromFileContent(textValue, "#START#", "#TemplateHomeInfo END#"); + if (homeData != string.Empty) + { + var homeInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<TemplateHomeInfo>(homeData); + myModel.ListUintContent.AddRange(homeInfo.ListUintContent); + myModel.ListUintName.AddRange(homeInfo.ListUintName); + myModel.ResidenceAddressName = homeInfo.ResidenceAddressName; + } + } + } + //鎸夋椂闂存帓搴� + listTime.Sort(); + + var listData = new List<LocalModelBaseInfo>(); + for (int i = listTime.Count - 1; i >= 0; i--) + { + listData.AddRange(dicData[listTime[i]]); + } + return listData; + } + + /// <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_Local_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".bin"; + } + + /// <summary> + /// 鑾峰彇涓�涓柊鐨勬ā鏉夸繚瀛樻枃浠跺悕 + /// </summary> + /// <returns></returns> + public string GetNewTemplateFileName(DateTime dateTime) + { + return "ModelData_Local_" + dateTime.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 + + #region 鈻� 鐢熸垚妯℃澘鏁版嵁鐩稿叧___________________ + + /// <summary> + /// 淇濆瓨妯℃澘鏁版嵁鍒版枃浠�(杩斿洖淇濆瓨鏂囦欢鐨勫叏璺緞) + /// </summary> + /// <param name="fileName">淇濆瓨鏂囦欢鐨勫悕瀛�(鏂板缓鏃剁敤 GetNewTemplateFileName鍑芥暟鏂板缓)</param> + /// <param name="backName">妯℃澘澶囦唤鐨勫悕瀛�</param> + public string SaveTemplateDataToFile(string fileName, string backUpName) + { + //鍐欏叆鏂囦欢鐨勫唴瀹� + string writeText = string.Empty; + + //鍦ㄧ敓鎴愭ā鏉挎暟鎹箣鍓�,妫�娴嬫ā鏉挎暟鎹� + this.CheckTempLateDataBeforCreat(); + + //鐢熸垚鍐欏叆鏂囦欢鐨勩�愭ā鏉垮熀鏈暟鎹�� + this.CreatWriteTemplateBaseData(ref writeText, backUpName); + + //鐢熸垚鍐欏叆鏂囦欢鐨勩�愭ā鏉夸綇瀹呬俊鎭暟鎹�� + this.CreatWriteTemplateHomeData(ref writeText); + + //鐢熸垚鍐欏叆鏂囦欢鐨勩�愯澶囨ā鏉挎暟鎹�� + this.CreatWriteDeviceTemplateData(ref writeText); + + //鐢熸垚鍐欏叆鏂囦欢鐨勩�愯澶囧璞℃暟鎹�� + this.CreatWriteCommonDeviceData(ref writeText); + + //鐢熸垚鍐欏叆鏂囦欢鐨勩�愮綉鍏冲璞℃暟鎹�� + this.CreatWriteGatewayData(ref writeText); + + //鐢熸垚鍐欏叆鏂囦欢鐨勩�愬満鏅ā鏉挎暟鎹�� + this.CreatWriteSceneData(ref writeText); + + //鐢熸垚鍐欏叆鏂囦欢鐨勩�愭埧闂存ā鏉挎暟鎹�� + this.CrearWriteRoomTemplateData(ref writeText); + + //鐢熸垚鍐欏叆鏂囦欢鐨勩�愯澶囧拰缃戝叧閫夋嫨鐨勬ā鏉跨殑鏁版嵁銆� + this.CrearWriteDeviceSelectTemplateData(ref writeText); + + //鍐欏叆鍐呭 + string saveFile = DirNameResourse.AllResidenceTemplateDirectory; + saveFile = System.IO.Path.Combine(saveFile, fileName); + + HdlFileLogic.Current.SaveTextToFile(saveFile, writeText); + + return saveFile; + } + + /// <summary> + /// 鐢熸垚鍐欏叆鏂囦欢鐨勩�愭ā鏉垮熀鏈暟鎹�� + /// </summary> + /// <param name="writeText"></param> + private void CreatWriteTemplateBaseData(ref string writeText, string backUpName) + { + var modelData = new LocalModelBaseInfo(); + modelData.EditorTime = DateTime.Now.ToString("yyyy.MM.dd HH:mm"); + modelData.ModelName = backUpName; + modelData.FloorCount = Common.Config.Instance.Home.FloorDics.Count; + modelData.DeviceCount = this.modelData.dicDeviceTemplateData.Count; + //鍔熻兘鏁� + int funcCount = 0; + foreach (var listData in this.modelData.dicDeviceTemplateData.Values) + { + if (listData.Count > 0) + { + var listDevice = Common.LocalDevice.Current.GetDevicesByMac(listData[0].DeviceMac, false); + funcCount += listDevice.Count; + } + } + modelData.FunctionCount = funcCount; + + writeText += "#START#\r\n"; + string dataInfo = Newtonsoft.Json.JsonConvert.SerializeObject(modelData); + writeText += dataInfo + "\r\n"; + writeText += "#TemplateData END#\r\n\r\n"; + } + + /// <summary> + /// 鐢熸垚鍐欏叆鏂囦欢鐨勩�愭ā鏉夸綇瀹呬俊鎭暟鎹�� + /// </summary> + /// <param name="writeText"></param> + private void CreatWriteTemplateHomeData(ref string writeText) + { + var homeData = new TemplateHomeInfo(); + homeData.ResidenceAddressName = Common.Config.Instance.Home.ResidenceAddressName; + homeData.ListUintName.AddRange(Common.Config.Instance.Home.ListUintName); + homeData.ListUintContent.AddRange(Common.Config.Instance.Home.ListUintContent); + + writeText += "#START#\r\n"; + string dataInfo = Newtonsoft.Json.JsonConvert.SerializeObject(homeData); + writeText += dataInfo + "\r\n"; + writeText += "#TemplateHomeInfo END#\r\n\r\n"; + } + + /// <summary> + /// 鐢熸垚鍐欏叆鏂囦欢鐨勩�愯澶囨ā鏉挎暟鎹�� + /// </summary> + /// <param name="writeText"></param> + private void CreatWriteDeviceTemplateData(ref string writeText) + { + foreach (var list in this.modelData.dicDeviceTemplateData.Values) + { + foreach (var data in list) + { + writeText += "#DeviceTemplate START#" + (int)data.DataSaveDiv + "\r\n"; + string dataInfo = Newtonsoft.Json.JsonConvert.SerializeObject(data); + writeText += dataInfo + "\r\n"; + writeText += "#DeviceTemplate END#\r\n\r\n"; + } + } + } + + /// <summary> + /// 鐢熸垚鍐欏叆鏂囦欢鐨勩�愯澶囧璞℃暟鎹�� + /// </summary> + /// <param name="writeText"></param> + private void CreatWriteCommonDeviceData(ref string writeText) + { + var listDevice = Common.LocalDevice.Current.listAllDevice; + var listCheck = new HashSet<string>(); + foreach (var device in listDevice) + { + //璁惧绔偣 + writeText += "#DeviceInfo START#" + (int)device.Type + "\r\n"; + string dataInfo = Newtonsoft.Json.JsonConvert.SerializeObject(device); + writeText += dataInfo + "\r\n"; + writeText += "#DeviceInfo END#\r\n\r\n"; + + //娣诲姞Ota璁惧瀵硅薄鐨勭紦瀛� + if (listCheck.Contains(device.DeviceAddr) == false) + { + listCheck.Add(device.DeviceAddr); + var otaDevice = Common.LocalDevice.Current.GetOTADevice(device.DeviceAddr); + if (otaDevice != null) + { + writeText += "#DeviceInfo START#" + (int)otaDevice.Type + "\r\n"; + string dataInfo2 = Newtonsoft.Json.JsonConvert.SerializeObject(otaDevice); + writeText += dataInfo2 + "\r\n"; + writeText += "#DeviceInfo END#\r\n\r\n"; + } + } + } + } + + /// <summary> + /// 鐢熸垚鍐欏叆鏂囦欢鐨勩�愮綉鍏冲璞℃暟鎹�� + /// </summary> + /// <param name="writeText"></param> + private void CreatWriteGatewayData(ref string writeText) + { + var listGateway = HdlGatewayLogic.Current.GetAllLocalGateway(); + foreach (var gateway in listGateway) + { + //璁惧绔偣 + writeText += "#START#\r\n"; + string dataInfo = Newtonsoft.Json.JsonConvert.SerializeObject(gateway); + writeText += dataInfo + "\r\n"; + writeText += "#GatewayInfo END#\r\n\r\n"; + } + } + + /// <summary> + /// 鐢熸垚鍐欏叆鏂囦欢鐨勩�愬満鏅暟鎹�� + /// </summary> + /// <param name="writeText"></param> + private void CreatWriteSceneData(ref string writeText) + { + //鍏ㄩ儴鐨勫満鏅� + var listScene = HdlSceneLogic.Current.GetAllLocalScene(); + + foreach (var scene in listScene) + { + writeText += "#START#\r\n"; + string dataInfo = Newtonsoft.Json.JsonConvert.SerializeObject(scene); + writeText += dataInfo + "\r\n"; + writeText += "#SceneTemplate END#\r\n\r\n"; + } + } + + /// <summary> + /// 鐢熸垚鍐欏叆鏂囦欢鐨勩�愭埧闂存ā鏉挎暟鎹�� + /// </summary> + /// <param name="writeText"></param> + private void CrearWriteRoomTemplateData(ref string writeText) + { + //妤煎眰鏁版嵁 + writeText += "#START#\r\n"; + string dataInfo1 = Newtonsoft.Json.JsonConvert.SerializeObject(Common.Config.Instance.Home.FloorDics); + writeText += dataInfo1 + "\r\n"; + writeText += "#FloorInfo END#\r\n\r\n"; + + //鎴块棿鏁版嵁 + var listRoom = HdlRoomLogic.Current.GetAllListRooms(); + foreach (var room in listRoom) + { + writeText += "#START#\r\n"; + string dataInfo2 = Newtonsoft.Json.JsonConvert.SerializeObject(room); + writeText += dataInfo2 + "\r\n"; + writeText += "#RoomInfo END#\r\n\r\n"; + } + //鐗╃悊缃戝叧鎵�鍦ㄧ殑鎴块棿 + var dicRealRoom = new Dictionary<string, string>(); + var listGateway = HdlGatewayLogic.Current.GetAllLocalGateway(); + foreach (var gateway in listGateway) + { + dicRealRoom[gateway.GwId] = gateway.RoomId; + } + //鑾峰彇鍏ㄩ儴鐗╃悊璁惧鎵�灞炴埧闂寸殑璁板綍 + var dicDeviceRoom = HdlRoomLogic.Current.GetAllRealDeviceRoomData(); + foreach (var strMac in dicDeviceRoom.Keys) + { + dicRealRoom[strMac] = dicDeviceRoom[strMac]; + } + + writeText += "#START#\r\n"; + string dataInfo = Newtonsoft.Json.JsonConvert.SerializeObject(dicRealRoom); + writeText += dataInfo + "\r\n"; + writeText += "#DeviceTemplateRealRoom END#\r\n\r\n"; + } + + /// <summary> + /// 鐢熸垚鍐欏叆鏂囦欢鐨勩�愯澶囧拰缃戝叧閫夋嫨鐨勬ā鏉跨殑鏁版嵁銆� + /// </summary> + /// <param name="writeText"></param> + private void CrearWriteDeviceSelectTemplateData(ref string writeText) + { + //璁惧閫夋嫨妯℃澘鐨勬暟鎹� + writeText += "#START#\r\n"; + string dataInfo1 = Newtonsoft.Json.JsonConvert.SerializeObject(this.modelData.dicDeviceTemplateSelect); + writeText += dataInfo1 + "\r\n"; + writeText += "#DeviceSelectTemplate END#\r\n\r\n"; + + //缃戝叧閫夋嫨妯℃澘鐨勬暟鎹� + writeText += "#START#\r\n"; + string dataInfo2 = Newtonsoft.Json.JsonConvert.SerializeObject(this.modelData.dicGatewayTemplateSelect); + writeText += dataInfo2 + "\r\n"; + writeText += "#GatewaySelectTemplate END#\r\n\r\n"; + } + + #endregion + + #region 鈻� 寮哄埗鎵ц鐨勭壒娈婂嚱鏁癬________________ + + /// <summary> + /// 寮哄埗浠庣紦瀛樺綋涓敓鎴愯澶囧拰缃戝叧鏂囦欢 + /// </summary> + public void CreatDeviceAndGatewayFileFromMemoryByForce() + { + //鍘熸潵鐨勭姸鎬� + bool oldShowTemplate = Common.Config.Instance.Home.IsShowTemplate; + //璁╁畠鍙互鐢熸垚鏂囦欢 + Common.Config.Instance.Home.IsShowTemplate = false; + + //鐢熸垚璁惧鏂囦欢 + foreach (var listDevice in this.modelData.dicDeviceInfo.Values) + { + foreach (var device in listDevice) + { + device.ReSave(); + } + } + //鐢熸垚缃戝叧鏂囦欢 + foreach (var gateway in this.modelData.dicGatewayInfo.Values) + { + gateway.ReSave(); + } + //杩樺師鐘舵�� + Common.Config.Instance.Home.IsShowTemplate = oldShowTemplate; + } + + #endregion + + #region 鈻� 璁惧鍜岀綉鍏虫ā鏉块�夋嫨鐩稿叧_____________ + + /// <summary> + /// 娣诲姞/淇敼 璁惧妯℃澘閫夋嫨鐩爣 + /// </summary> + /// <param name="sourceMac">璁惧Mac瀵硅薄</param> + /// <param name="targetMac">鐩爣Mac瀵硅薄</param> + public void AddDeviceTemplateSelect(string sourceMac, string targetMac) + { + //鑾峰彇鏈湴鎸囧畾鐨凪ac鐨勫叏閮ㄨ澶� + var listDevice = Common.LocalDevice.Current.GetDevicesByMac(sourceMac, false); + var otaDevice = Common.LocalDevice.Current.GetOTADevice(sourceMac); + if (otaDevice != null) + { + //杩欓噷ota璁惧涔熻鍔犺繘鍘�,閲嶄腑涔嬮噸 + listDevice.Add(otaDevice); + } + + foreach (var device in listDevice) + { + //妯℃澘閫夋嫨鐨勬椂鍊�,浠栦滑鐨勭鐐规槸涓�鑷寸殑 + string localDeviceKey = Common.LocalDevice.Current.GetDeviceMainKeys(device); + string templateDeviceKey = Common.LocalDevice.Current.GetDeviceMainKeys(targetMac, device.DeviceEpoint); + + if (this.modelData.dicDeviceTemplateRoom.ContainsKey(templateDeviceKey) == true + && (device is OTADevice) == false) + { + //濡傛灉妯℃澘閲岄潰,杩欎釜绔偣璁剧疆鏈夋埧闂寸殑璇� + HdlRoomLogic.Current.ChangedRoom(device, this.modelData.dicDeviceTemplateRoom[templateDeviceKey], false); + } + //濡傛灉杩欎釜绔偣鏈夋ā鏉挎暟鎹殑璇� + if (this.modelData.dicDeviceFileContent.ContainsKey(templateDeviceKey) == true) + { + //濡傛灉鍘熸潵瀹冮�夋嫨鏈夋ā鏉挎暟鎹殑璇� + if (this.modelData.dicDeviceTemplateData.ContainsKey(localDeviceKey) == true) + { + //鍒犻櫎杩欎釜璁惧鐨勬ā鏉夸繚瀛樻枃浠� + HdlFileLogic.Current.DeleteFile(System.IO.Path.Combine(DirNameResourse.LocalTemplateDirectory, device.FilePath)); + //绉婚櫎褰撳墠绔偣淇濆瓨鐨勬ā鏉挎暟鎹� + this.modelData.dicDeviceTemplateData.Remove(localDeviceKey); + } + + //鍙嶅簭鍒楀寲璁惧鏁版嵁 + foreach (var strCentent in this.modelData.dicDeviceFileContent[templateDeviceKey]) + { + var tempData = this.DeserializeDeviceDataByDiv(strCentent.saveDiv, strCentent.FileContent); + //杩欓噷闇�瑕佹浛鎹㈡帀Mac + tempData.DeviceMac = sourceMac; + //娣诲姞缂撳瓨(涓婚敭涓烘湰鍦拌澶囩殑涓婚敭) + this.SetTemplateDeviceDataToMemmory(tempData, null, localDeviceKey, true); + //淇敼绔偣缂撳瓨鍚嶅瓧 + if (tempData.DataSaveDiv == ModelDeviceSaveEnum.A绔偣鍚嶇О) + { + Common.LocalDevice.Current.SetEpointName(device, ((ModelDeviceEpointNameInfo)tempData).deviceEpointName); + } + else if (tempData.DataSaveDiv == ModelDeviceSaveEnum.A璁惧鍚嶇О) + { + Common.LocalDevice.Current.SetMacName(device, ((ModelDeviceMacNameInfo)tempData).deviceMacName); + } + } + } + //涓嶇濡備綍,閮介渶瑕佸垯閲嶆柊淇濆瓨鎴愭枃浠� + this.SaveDeviceMemmoryData(device.DeviceAddr, device.DeviceEpoint); + //杩樺師鍙婂彉鏇村満鏅殑鎵ц鐩爣 + //this.RecoverAndChangedSceneAdjustTarget(device, targetMac); + } + //鏇存敼鐗╃悊璁惧鎵�鍦ㄧ殑鎴块棿 + if (this.modelData.dicDeviceTemplateRealRoom.ContainsKey(targetMac) == true) + { + HdlRoomLogic.Current.SaveRealDeviceRoomId(listDevice, this.modelData.dicDeviceTemplateRealRoom[targetMac], false); + } + + //璁板綍缂撳瓨 + this.modelData.dicDeviceTemplateSelect[sourceMac] = targetMac; + //淇濆瓨鐨勮矾寰� + string fileData = Newtonsoft.Json.JsonConvert.SerializeObject(this.modelData.dicDeviceTemplateSelect); + HdlFileLogic.Current.SaveTextToFile(DirNameResourse.DeviceTemplateSelectFile, fileData); + } + + /// <summary> + /// 杩樺師鍙婂彉鏇村満鏅殑鎵ц鐩爣 + /// </summary> + /// <param name="device">鏈湴璁惧瀵硅薄</param> + /// <param name="targetMac">闇�瑕佸彉鏇寸殑妯℃澘璁惧鐨凪ac</param> + private void RecoverAndChangedSceneAdjustTarget(CommonDevice device, string targetMac) + { + //濡傛灉杩欎釜璁惧鏄浛鎹㈤�夋嫨鐨勬ā鏉跨殑璇� + if (this.modelData.dicDeviceTemplateSelect.ContainsKey(device.DeviceAddr) == false) + { + return; + } + //鍏ㄩ儴鐨勫満鏅� + var listScene = HdlSceneLogic.Current.GetAllLocalScene(); + + //杩樻病鏈夋墽琛屽彉鏇村墠,瀹冪洰鍓嶉�夋嫨鐨勬ā鏉跨殑Mac + string oldTemplateMac = this.modelData.dicDeviceTemplateSelect[device.DeviceAddr]; + //灏嗗満鏅璞′腑,杩欎釜鍥炶矾鐨勪富閿�,鏇挎崲鍥炲師鏉ユā鏉夸腑鐨凪ac+绔彛 + foreach (var scene in listScene) + { + bool save = false; + for (int i = 0; i < scene.AdjustTargetList.Count; i++) + { + if (scene.AdjustTargetList[i].Type != 0) + { + //鍙鐞嗚澶囩粦瀹氱洰鏍� + continue; + } + //濡傛灉鏄綋鍓嶅洖璺� + if (scene.AdjustTargetList[i].DeviceAddr == device.DeviceAddr && + scene.AdjustTargetList[i].Epoint == device.DeviceEpoint) + { + //鏇挎崲鎺塎ac + scene.AdjustTargetList[i].DeviceAddr = oldTemplateMac; + save = true; + continue; + } + //濡傛灉鏄ā鏉跨洰鏍囧洖璺� + if (targetMac != null && + scene.AdjustTargetList[i].DeviceAddr == targetMac && + scene.AdjustTargetList[i].Epoint == device.DeviceEpoint) + { + //灏嗙洰鏍囩殑Mac鍙樻洿涓哄綋鍓嶈澶囩殑Mac + scene.AdjustTargetList[i].DeviceAddr = device.DeviceAddr; + save = true; + continue; + } + } + if (save == true) + { + //淇濆瓨缂撳瓨 + scene.Save(); + } + } + } + + /// <summary> + /// 鑾峰彇璁惧宸茬粡閫夋嫨浜嗙殑妯℃澘鐩爣鐨勮澶囩殑Mac(娌℃湁鐩爣鏃�,杩斿洖null) + /// </summary> + /// <param name="sourceMac">璁惧鐨凪ac</param> + /// <returns></returns> + public string GetDeviceTemplateSelectMac(string sourceMac) + { + if (this.modelData.dicDeviceTemplateSelect.ContainsKey(sourceMac) == true) + { + return this.modelData.dicDeviceTemplateSelect[sourceMac]; + } + return null; + } + + /// <summary> + /// 鑾峰彇璁惧宸茬粡閫夋嫨浜嗙殑妯℃澘鐩爣鐨勮澶囩殑Mac鍚嶅瓧(娌℃湁鐩爣鏃�,杩斿洖null) + /// </summary> + /// <param name="sourceMac">璁惧鐨凪ac</param> + /// <returns></returns> + public string GetDeviceTemplateSelectName(string sourceMac) + { + if (this.modelData.dicDeviceTemplateSelect.ContainsKey(sourceMac) == true) + { + string tempMac = this.modelData.dicDeviceTemplateSelect[sourceMac]; + foreach (var listData in this.modelData.dicDeviceFileContent.Values) + { + foreach (var data in listData) + { + if (data.DeviceMac != tempMac || data.saveDiv != ModelDeviceSaveEnum.A璁惧鍚嶇О) + { + continue; + } + var tempModel = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelDeviceMacNameInfo>(data.FileContent); + return tempModel.deviceMacName; + } + } + } + return null; + } + + /// <summary> + /// 鍙栨秷璁惧妯℃澘鐨勯�夋嫨鐩爣 + /// </summary> + /// <param name="sourceMac">璁惧鐨凪ac</param> + public void RemoveDeviceTemplateSelect(string sourceMac) + { + ////鑾峰彇鏈湴鎸囧畾鐨凪ac鐨勫叏閮ㄨ澶� + //var listDevice = Common.LocalDevice.Current.GetDevicesByMac(sourceMac, false); + //foreach (var device in listDevice) + //{ + // //杩樺師鍦烘櫙鐨勬墽琛岀洰鏍� + // this.RecoverAndChangedSceneAdjustTarget(device, null); + //} + + //璁板綍缂撳瓨 + this.modelData.dicDeviceTemplateSelect.Remove(sourceMac); + //淇濆瓨鐨勮矾寰� + string fileData = Newtonsoft.Json.JsonConvert.SerializeObject(this.modelData.dicDeviceTemplateSelect); + HdlFileLogic.Current.SaveTextToFile(DirNameResourse.DeviceTemplateSelectFile, fileData); + } + + /// <summary> + /// 娣诲姞/淇敼 缃戝叧妯℃澘閫夋嫨鐩爣 + /// </summary> + /// <param name="sourceGwid">缃戝叧id</param> + /// <param name="targetGwid">鐩爣缃戝叧id(妯℃澘)</param> + public void AddGatewayTemplateSelect(string sourceGwid, string targetGwid) + { + //璁板綍缂撳瓨 + this.modelData.dicGatewayTemplateSelect[sourceGwid] = targetGwid; + //淇濆瓨鐨勮矾寰� + string fileData = Newtonsoft.Json.JsonConvert.SerializeObject(this.modelData.dicGatewayTemplateSelect); + HdlFileLogic.Current.SaveTextToFile(DirNameResourse.GatewayTemplateSelectFile, fileData); + + //鍙樻洿缃戝叧鎴块棿 + if (this.modelData.dicDeviceTemplateRealRoom.ContainsKey(targetGwid) == true) + { + HdlRoomLogic.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); + //} + } + + /// <summary> + /// 鍙栨秷缃戝叧妯℃澘閫夋嫨鐩爣 + /// </summary> + /// <param name="sourceGwid">缃戝叧id</param> + /// <param name="targetGwid">鐩爣缃戝叧id</param> + public void RemoveGatewayTemplateSelect(string sourceGwid) + { + //璁板綍缂撳瓨 + this.modelData.dicGatewayTemplateSelect.Remove(sourceGwid); + //淇濆瓨鐨勮矾寰� + string fileData = Newtonsoft.Json.JsonConvert.SerializeObject(this.modelData.dicGatewayTemplateSelect); + HdlFileLogic.Current.SaveTextToFile(DirNameResourse.GatewayTemplateSelectFile, fileData); + } + + /// <summary> + /// 鑾峰彇缃戝叧宸茬粡閫夋嫨浜嗙殑妯℃澘鐩爣鐨勭綉鍏崇殑鍚嶅瓧(娌℃湁鐩爣鏃�,杩斿洖null) + /// </summary> + /// <param name="sourceMac">缃戝叧id</param> + /// <returns></returns> + public string GetGatewayTemplateSelectName(string sourceGwid) + { + if (this.modelData.dicGatewayTemplateSelect.ContainsKey(sourceGwid) == true) + { + string tempMac = this.modelData.dicGatewayTemplateSelect[sourceGwid]; + if (this.modelData.dicGatewayInfo.ContainsKey(tempMac) == true) + { + string gwName = HdlGatewayLogic.Current.GetGatewayName(this.modelData.dicGatewayInfo[tempMac]); + return gwName; + } + } + return null; + } + + /// <summary> + /// 鑾峰彇缃戝叧宸茬粡閫夋嫨浜嗙殑妯℃澘鐩爣鐨勭綉鍏崇殑鍚嶅瓧(娌℃湁鐩爣鏃�,杩斿洖null) + /// </summary> + /// <param name="sourceMac">缃戝叧id</param> + /// <returns></returns> + public string GetGatewayTemplateSelectId(string sourceGwid) + { + if (this.modelData.dicGatewayTemplateSelect.ContainsKey(sourceGwid) == true) + { + return this.modelData.dicGatewayTemplateSelect[sourceGwid]; + } + return null; + } + + /// <summary> + /// 鑾峰彇妯℃澘涓叏閮ㄧ綉鍏崇殑鍚嶅瓧 + /// </summary> + /// <returns></returns> + public Dictionary<string, string> GetAllGatewayTemplateName() + { + var dic = new Dictionary<string, string>(); + foreach (string gwId in this.modelData.dicGatewayInfo.Keys) + { + string gwName = HdlGatewayLogic.Current.GetGatewayName(this.modelData.dicGatewayInfo[gwId]); + dic[gwId] = gwName; + } + return dic; + } + + /// <summary> + /// 鑾峰彇鎸囧畾缃戝叧鑳藉閫夋嫨鐨勬ā鏉垮悕瀛� + /// </summary> + /// <returns></returns> + public Dictionary<string, string> GetGatewayCanSelectTemplateName(ZbGateway zbGateway) + { + var dic = new Dictionary<string, string>(); + foreach (var zbway in this.modelData.dicGatewayInfo.Values) + { + if (zbGateway.LinuxImageType == zbway.LinuxImageType) + { + string gwName = HdlGatewayLogic.Current.GetGatewayName(zbway); + dic[zbway.GwId] = gwName; + } + } + return dic; + } + + /// <summary> + /// 娓呴櫎鍏ㄩ儴宸茬粡宸茬粡閫夋嫨濂戒簡妯℃澘瀵硅薄鐨勮澶囧拰缃戝叧 + /// </summary> + public void ClearAllSelectDeviceAndGateway() + { + //閲嶆柊鍒濆鍖� + this.modelData.dicDeviceTemplateSelect = new Dictionary<string, string>(); + this.modelData.dicGatewayTemplateSelect = new Dictionary<string, string>(); + //鍒犳帀杩欎袱涓繚瀛橀�夋嫨妯℃澘鐨勬枃浠� + HdlFileLogic.Current.DeleteFile(DirNameResourse.DeviceTemplateSelectFile); + HdlFileLogic.Current.DeleteFile(DirNameResourse.GatewayTemplateSelectFile); + } + + #endregion + + #region 鈻� 鑾峰彇鑳藉閫夋嫨鐨勬ā鏉縚________________ + + /// <summary> + /// 鑾峰彇鑳藉閫夋嫨鐨勬ā鏉� + /// </summary> + /// <param name="localDevice">鏈湴璁惧</param> + /// <returns></returns> + public List<TemplateCanSelectContent> GetCanSelectDeviceTemplate(CommonDevice localDevice) + { + var listCanSelect = new List<TemplateCanSelectContent>(); + if (this.modelData.dicGatewayTemplateSelect.ContainsKey(localDevice.CurrentGateWayId) == false) + { + //璇ョ綉鍏虫病鏈夊尮閰嶆ā鏉�,涓嶆彁渚涙ā鏉块�夋嫨 + return listCanSelect; + } + var listHadSelect = new HashSet<string>(); + foreach (var localMac in this.modelData.dicDeviceTemplateSelect.Keys) + { + //瀹冭嚜宸辩殑璇�,鍙互鏄剧ず(鍥犱负鏈変釜鍙栨秷缁戝畾鐨勫姛鑳�) + if (localMac != localDevice.DeviceAddr) + { + //鐩墠宸茬粡琚�夋嫨浜嗙殑妯℃澘Mac + listHadSelect.Add(this.modelData.dicDeviceTemplateSelect[localMac]); + } + } + + var listCheck = new HashSet<string>(); + //璁惧鐨勬ā鍧桰D + string modelId = this.GetDeviceModelId(localDevice.DeviceAddr); + //妯℃澘涓殑缃戝叧ID + string gatewayTemplateId = this.modelData.dicGatewayTemplateSelect[localDevice.CurrentGateWayId]; + foreach (var listData in this.modelData.dicDeviceFileContent.Values) + { + foreach (var data in listData) + { + if (data.saveDiv != ModelDeviceSaveEnum.A璁惧鍚嶇О) + { + //鍙幏鍙栬澶噈ac鍚嶇О鐨勬ā鏉挎暟鎹� + continue; + } + if (listHadSelect.Contains(data.DeviceMac) == true + || listCheck.Contains(data.DeviceMac) == true) + { + //濡傛灉杩欎釜妯℃澘宸茬粡琚叾浠栬澶囬�夋嫨浜�,鎴栬�呰繖涓狹ac宸茬粡澶勭悊浜�,鍒檅reak + break; + } + if (this.modelData.dicDeviceInfo.ContainsKey(data.DeviceMac) == false + || this.modelData.dicDeviceInfo[data.DeviceMac][0].CurrentGateWayId != gatewayTemplateId + || this.modelData.dicDeviceInfo[data.DeviceMac][0].ModelIdentifier != modelId) + { + //璇ユā鏉夸笉鏄繖涓綉鍏崇殑,鎴栬�呮ā鍧桰D涓嶄竴鏍风殑 + break; + } + listCheck.Add(data.DeviceMac); + + var info = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelDeviceMacNameInfo>(data.FileContent); + var selectData = new TemplateCanSelectContent(); + selectData.DeviceMac = data.DeviceMac; + selectData.DeviceName = info.deviceMacName; + + Common.Room room = null; + if (this.modelData.dicDeviceTemplateRealRoom.ContainsKey(data.DeviceMac) == true) + { + room = HdlRoomLogic.Current.GetRoomById(this.modelData.dicDeviceTemplateRealRoom[data.DeviceMac]); + } + selectData.RoomName = HdlRoomLogic.Current.GetRoomName(room); + + listCanSelect.Add(selectData); + } + } + return listCanSelect; + } + + #endregion + + #region 鈻� 鍒犻櫎璁惧___________________________ + + /// <summary> + /// 鍒犻櫎璁惧 + /// </summary> + /// <param name="device"></param> + public void DeleteDevice(CommonDevice device) + { + //鍒犻櫎淇濆瓨鏂囦欢 + string saveFile = DirNameResourse.LocalTemplateDirectory; + saveFile = System.IO.Path.Combine(saveFile, device.FilePath); + HdlFileLogic.Current.DeleteFile(saveFile); + + //绉婚櫎妯℃澘缂撳瓨 + this.modelData.dicDeviceTemplateData.Remove(Common.LocalDevice.Current.GetDeviceMainKeys(device)); + //绉婚櫎璁惧閫夋嫨缂撳瓨 + if (this.modelData.dicDeviceTemplateSelect.ContainsKey(device.DeviceAddr) == true) + { + this.modelData.dicDeviceTemplateSelect.Remove(device.DeviceAddr); + HdlFileLogic.Current.SaveFileContent(DirNameResourse.DeviceTemplateSelectFile, this.modelData.dicDeviceTemplateSelect); + } + } + + #endregion + + #region 鈻� 淇濆瓨璁惧缂撳瓨_______________________ + + /// <summary> + /// 淇濆瓨璁惧缂撳瓨(鑰冭檻鏈夌殑璁惧鐢ㄧ殑鏄�200绔偣,鎵�浠ヨ繖閲屾渶濂戒笉鐢ㄨ澶囧璞′綔涓哄弬鏁�) + /// </summary> + /// <param name="deviceMac">璁惧mac</param> + /// <param name="deviceEpoint">璁惧Epoint</param> + public void SaveDeviceMemmoryData(string deviceMac, int deviceEpoint) + { + string mainkey = Common.LocalDevice.Current.GetDeviceMainKeys(deviceMac, deviceEpoint); + if (this.modelData.dicDeviceTemplateData.ContainsKey(mainkey) == false) + { + return; + } + + //淇濆瓨璺緞 + string saveFile = DirNameResourse.LocalTemplateDirectory; + saveFile = System.IO.Path.Combine(saveFile, "Device_" + mainkey); + + var listData = this.modelData.dicDeviceTemplateData[mainkey]; + if (listData.Count == 0) + { + //鍒犻櫎鎺夎繖涓枃浠� + HdlFileLogic.Current.DeleteFile(saveFile); + return; + } + //鍐欏叆鏂囦欢鐨勫唴瀹� + string writeText = string.Empty; + foreach (var data in listData) + { + writeText += "===>" + (int)data.DataSaveDiv + "\r\n"; + string dataInfo = Newtonsoft.Json.JsonConvert.SerializeObject(data); + writeText += dataInfo + "\r\n"; + } + //鍐欏叆鍐呭 + HdlFileLogic.Current.SaveTextToFile(saveFile, writeText); + } + + #endregion + + #region 鈻� 鑾峰彇闇�瑕佸崌绾х殑璁惧瀵硅薄_____________ + + /// <summary> + /// 鑾峰彇闇�瑕佸崌绾х殑璁惧瀵硅薄,鎸夌綉鍏冲垎缁�(key:缃戝叧ID value鐨刱ey:鏈湴Ota璁惧鐨凪ac value:鍗囩骇鍥轰欢鍦板潃) + /// </summary> + /// <returns></returns> + public Dictionary<string, Dictionary<string, string>> GetNeedToUpdateDevice() + { + var dicGatewayDevice = new Dictionary<string, Dictionary<string, string>>(); + //寰幆璁惧鍖归厤鐨勬ā鏉� + foreach (var localMac in this.modelData.dicDeviceTemplateSelect.Keys) + { + string tempMac = this.modelData.dicDeviceTemplateSelect[localMac]; + if (this.modelData.dicDeviceInfo.ContainsKey(tempMac) == false) + { + //搴旇涓嶄細杩涙潵,鍗充娇杩涙潵,鎴戜篃涓嶇煡閬撲负浠�涔� + continue; + } + //鍙栨湰鍦癘ta璁惧瀵硅薄 + var localOta = Common.LocalDevice.Current.GetOTADevice(localMac); + if (localOta == null) + { + //搴旇涓嶄細杩涙潵,鍗充娇杩涙潵,鎴戜篃涓嶇煡閬撲负浠�涔� + continue; + } + foreach (var tempDevice in this.modelData.dicDeviceInfo[tempMac]) + { + //鍙栨ā鏉縊ta璁惧瀵硅薄 + if (tempDevice is OTADevice) + { + //鍙涓よ�呯殑鍥轰欢鐗堟湰涓嶄竴鏍�,骞朵笖鏈湴鏈夎繖涓崌绾у浐浠�,鍒欓兘闇�瑕佸崌绾� + if (localOta.ImgVersion != tempDevice.ImgVersion + && HdlFirmwareUpdateLogic.IsEsixtDeviceFirmwareFile((OTADevice)tempDevice) == true) + { + //鎸夌綉鍏冲垎缁� + if (dicGatewayDevice.ContainsKey(localOta.CurrentGateWayId) == false) + { + dicGatewayDevice[localOta.CurrentGateWayId] = new Dictionary<string, string>(); + } + var dicDevice = dicGatewayDevice[localOta.CurrentGateWayId]; + dicDevice[localMac] = HdlFirmwareUpdateLogic.GetDeviceFirmwareFile((OTADevice)tempDevice); + } + break; + } + } + } + + return dicGatewayDevice; + } + + /// <summary> + /// 鑾峰彇闇�瑕佸崌绾х殑缃戝叧瀵硅薄(key:鏈湴缃戝叧鐨刬d value:鍗囩骇鍥轰欢鍦板潃,绗竴浣嶆槸Linux,绗簩浣嶆槸鍗忚皟鍣�,涔嬪悗閮芥槸铏氭嫙椹卞姩) + /// </summary> + /// <returns></returns> + public Dictionary<string, List<GatewayNeedUpdateInfo>> GetNeedToUpdateGateway() + { + var dicGateway = new Dictionary<string, List<GatewayNeedUpdateInfo>>(); + //寰幆缃戝叧鍖归厤鐨勬ā鏉� + foreach (var localId in this.modelData.dicGatewayTemplateSelect.Keys) + { + string tempId = this.modelData.dicGatewayTemplateSelect[localId]; + if (this.modelData.dicGatewayInfo.ContainsKey(tempId) == false) + { + //搴旇涓嶄細杩涙潵,鍗充娇杩涙潵,鎴戜篃涓嶇煡閬撲负浠�涔� + continue; + } + //鍙栨湰鍦扮綉鍏冲璞� + var localGateway = HdlGatewayLogic.Current.GetLocalGateway(localId); + if (localGateway == null) + { + //搴旇涓嶄細杩涙潵,鍗充娇杩涙潵,鎴戜篃涓嶇煡閬撲负浠�涔� + continue; + } + var tempGateway = this.modelData.dicGatewayInfo[tempId]; + //鍒濆鍖栧鍣� + var listUpdateInfo = new List<GatewayNeedUpdateInfo>() { null, null }; + + bool needUpdate = false; + //Linux鐗堟湰姣旇緝 + if (tempGateway.LinuxFirmwareVersion != localGateway.LinuxFirmwareVersion) + { + //Linux鍗囩骇鍥轰欢鏂囦欢鍏ㄨ矾寰� + string updateFile = HdlFirmwareUpdateLogic.GetGatewayLinuxFirmwareFile(tempGateway); + if (System.IO.File.Exists(updateFile) == true) + { + //濡傛灉瀛樺湪鐨勮瘽 + var info = new GatewayNeedUpdateInfo(); + info.Div = 1; + info.FullFileName = updateFile; + listUpdateInfo[0] = info; + needUpdate = true; + } + } + //鍗忚皟鍣ㄧ増鏈瘮杈� + if (tempGateway.CoordinatorFirmwareVersion != localGateway.CoordinatorFirmwareVersion) + { + //鍗忚皟鍣ㄥ崌绾у浐浠舵枃浠跺叏璺緞 + string updateFile = HdlFirmwareUpdateLogic.GetGatewayCoordinatorFirmwareFile(tempGateway); + if (System.IO.File.Exists(updateFile) == true) + { + //濡傛灉瀛樺湪鐨勮瘽 + var info = new GatewayNeedUpdateInfo(); + info.Div = 2; + info.FullFileName = updateFile; + listUpdateInfo[1] = info; + needUpdate = true; + } + } + //铏氭嫙椹卞姩姣旇緝 + if (HdlGatewayLogic.Current.CheckGatewayHadDriveCode(localGateway) == true) + { + foreach (var localCode in localGateway.DriveCodeList) + { + foreach (var tempCode in tempGateway.DriveCodeList) + { + //闃叉瀹冩斁鐨勯『搴忎笉鏍� + if (localCode.DriveCode == tempCode.DriveCode && localCode.DriveFwVersion != tempCode.DriveFwVersion) + { + //铏氭嫙椹卞姩鍗囩骇鍥轰欢鏂囦欢鍏ㄨ矾寰� + string updateFile = HdlFirmwareUpdateLogic.GetGatewayDriveCodeFirmwareFile(tempCode); + if (System.IO.File.Exists(updateFile) == true) + { + //濡傛灉瀛樺湪鐨勮瘽 + var info = new GatewayNeedUpdateInfo(); + info.Div = 3; + info.DriveCode = tempCode.DriveCode; + info.FullFileName = updateFile; + listUpdateInfo.Add(info); + needUpdate = true; + } + } + } + } + } + //娣诲姞鐩爣缂撳瓨 + if (needUpdate == true) + { + dicGateway[localId] = listUpdateInfo; + } + } + + return dicGateway; + } + + #endregion + + #region 鈻� 浠庢ā鏉挎枃浠朵腑鑾峰彇瀵硅薄(澶栭儴璋冪敤)_____ + + /// <summary> + /// 浠庢ā鏉挎枃浠朵腑,鑾峰彇璁惧鍜岀綉鍏冲璞� + /// </summary> + /// <param name="fullFileName">妯℃澘鏂囦欢鐨勫叏璺緞</param> + /// <param name="listDevice">ota璁惧鍒楄〃</param> + /// <param name="listGateway">缃戝叧鍒楄〃</param> + public void GetDeviceObjectFromTemplate(string fullFileName, ref List<OTADevice> listDevice, ref List<ZbGateway> listGateway) + { + var fileData = HdlFileLogic.Current.ReadFileTextContent(fullFileName); + if (fileData == null) + { + return; + } + var deviceType = string.Empty; + var strTempContentData = string.Empty; + + //鏍规嵁鎹㈣绗﹀垏鍒嗘暟鎹枃鏈� + string[] arryData = fileData.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); + foreach (string strData in arryData) + { + if (strData == "#START#") + { + //鏃犻檮鍔犳暟鎹殑銆愭暟鎹爣棰樸�� + strTempContentData = string.Empty; + continue; + } + if (strData.StartsWith("#DeviceInfo START#") == true) + { + //闄勫姞鏁版嵁:璁惧瀵硅薄绫诲瀷 + deviceType = strData.Substring(18); + strTempContentData = string.Empty; + continue; + } + try + { + //璁惧瀵硅薄 + if (strData == "#DeviceInfo END#") + { + //鍙嶅簭鍒楀寲璁惧 + if (deviceType == "OtaDevice" || deviceType == "OtaPanelDevice") + { + var device = Newtonsoft.Json.JsonConvert.DeserializeObject<OTADevice>(strTempContentData); + if (device != null) + { + listDevice.Add(device); + } + } + + strTempContentData = string.Empty; + continue; + } + //缃戝叧瀵硅薄鏁版嵁 + else if (strData == "#GatewayInfo END#") + { + //鍙嶅簭鍒楀寲璁惧 + var gateway = Newtonsoft.Json.JsonConvert.DeserializeObject<ZbGateway>(strTempContentData); + if (gateway != null) + { + listGateway.Add(gateway); + } + strTempContentData = string.Empty; + return; + } + strTempContentData += strData; + } + catch (Exception ex) + { + HdlLogLogic.Current.WriteLog(ex, "妯℃澘bin鏂囦欢鍑洪棶棰榎r\n" + strTempContentData); + strTempContentData = string.Empty; + } + } + } + + + #endregion + + #region 鈻� 涓�鑸柟娉昣__________________________ + + /// <summary> + /// 澶嶅埗妯℃澘bin鏂囦欢鍒版湰鍦扮殑妯℃澘鏂囦欢澶归噷 + /// </summary> + /// <param name="templateFileName">妯℃澘鏂囦欢鐨勫悕瀛�(鍏ㄤ綇瀹呭瓨鏀剧殑妯℃澘)</param> + public void CopyTemplateFileToLocalDirectory(string templateFileName) + { + string sourceFile = System.IO.Path.Combine(DirNameResourse.AllResidenceTemplateDirectory, templateFileName); + + this.CopyTemplateFileToLocalDirectory2(sourceFile); + } + + /// <summary> + /// 澶嶅埗妯℃澘bin鏂囦欢鍒版湰鍦扮殑妯℃澘鏂囦欢澶归噷 + /// </summary> + /// <param name="fullTemplateName">妯℃澘鏂囦欢鐨勫叏璺緞</param> + public void CopyTemplateFileToLocalDirectory2(string fullTemplateName) + { + if (System.IO.File.Exists(fullTemplateName) == false) + { + return; + } + + //淇濆瓨鐨勮矾寰� + string targetFile = DirNameResourse.LocalTemplateDirectory; + targetFile = System.IO.Path.Combine(targetFile, TemplateFileName); + + try { System.IO.File.Copy(fullTemplateName, targetFile, true); } + catch (Exception ex) { HdlLogLogic.Current.WriteLog(ex, "澶嶅埗妯℃澘bin鏂囦欢澶辫触"); } + } + + /// <summary> + /// 鍒犻櫎鏈湴鍏ㄩ儴鐨勬ā鏉跨紦瀛樻枃浠� + /// </summary> + public void DeleteAllLocalFile() + { + //鑾峰彇杩欎釜璺緞涓嬮潰鍏ㄩ儴鐨勬枃浠� + var listFile = HdlFileLogic.Current.GetFileFromDirectory(DirNameResourse.LocalTemplateDirectory, false); ; + foreach (var file in listFile) + { + HdlFileLogic.Current.DeleteFile(file); + } + } + + /// <summary> + /// 鑾峰彇妯℃澘涓殑璁惧鏁� + /// </summary> + /// <returns></returns> + public int GetTemplateDeviceCount() + { + return this.modelData.dicDeviceInfo.Count; + } + + /// <summary> + /// 妫�娴嬭澶囨ā鏉挎暟鍜屽綋鍓嶈澶囬�夋嫨鐨勬ā鏉挎暟鏄惁涓�鑷� + /// </summary> + /// <returns></returns> + public bool CheckTemplateDeviceCountAndSelectCountIsEqual() + { + return this.modelData.dicDeviceInfo.Count == this.modelData.dicDeviceTemplateSelect.Count; + } + + /// <summary> + /// 鑾峰彇鏂囦欢閲屾寚瀹氱殑鍐呭 + /// </summary> + /// <param name="fileContrnt">鏂囦欢鏂囨湰</param> + /// <param name="startFlage">寮�濮嬪瓧绗�</param> + /// <param name="endFlage">缁撴潫瀛楃</param> + /// <returns></returns> + private string GetDataFromFileContent(string fileContrnt, string startFlage, string endFlage) + { + string[] arryValue = fileContrnt.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); + string modelBaseInfo = string.Empty; + bool getData = false; + bool success = false; + foreach (var strValue in arryValue) + { + //寮�濮� + if (strValue == startFlage) + { + getData = true; + //鍚屼竴寮�濮嬪瓧绗︾殑涓滆タ寰堝 + modelBaseInfo = string.Empty; + continue; + } + //缁撴潫 + if (strValue == endFlage) + { + success = true; + break; + } + if (getData == true) + { + modelBaseInfo += strValue; + } + } + return success == true ? modelBaseInfo : string.Empty; + } + + /// <summary> + /// 鑾峰彇妯″潡ID + /// </summary> + /// <param name="deviceMac"></param> + /// <returns></returns> + private string GetDeviceModelId(string deviceMac) + { + var listDevice = Common.LocalDevice.Current.GetDevicesByMac(deviceMac); + foreach (var device in listDevice) + { + if (device.ModelIdentifier != string.Empty) + { + return device.ModelIdentifier; + } + } + return string.Empty; + } + + /// <summary> + /// 鑾峰彇璁惧淇濆瓨鐨勬ā鏉垮璞�(鑰冭檻鏈夌殑璁惧鐢ㄧ殑鏄�200绔偣,鎵�浠ヨ繖閲屾渶濂戒笉鐢ㄨ澶囧璞′綔涓哄弬鏁�) + /// </summary> + /// <param name="device"></param> + /// <param name="saveEnum"></param> + /// <returns></returns> + public TemplateDeviceDataCommon GetDeviceModelDataClass(string deviceMac, int deviceEpoint, ModelDeviceSaveEnum saveEnum, TemplateDeviceDataCommon newClass) + { + string mainkey = Common.LocalDevice.Current.GetDeviceMainKeys(deviceMac, deviceEpoint); + + //鍒涘缓瀛樺偍绌洪棿 + if (this.modelData.dicDeviceTemplateData.ContainsKey(mainkey) == false) + { + this.modelData.dicDeviceTemplateData[mainkey] = new List<TemplateDeviceDataCommon>(); + } + foreach (var data in this.modelData.dicDeviceTemplateData[mainkey]) + { + //濡傛灉鏄凡缁忓瓨鍦ㄤ簡鐨� + if (data.DataSaveDiv == saveEnum) + { + return data; + } + } + //鏂板缓涓�涓柊鐨勫璞� + newClass.DataSaveDiv = saveEnum; + newClass.DeviceEpoint = deviceEpoint; + newClass.DeviceMac = deviceMac; + + //榛樿鍒涘缓涓�涓储寮曚綅 + newClass.ListReceiveResult.Add(string.Empty); + newClass.ListReceiveTopic.Add(string.Empty); + newClass.ListSendTopic.Add(string.Empty); + + this.modelData.dicDeviceTemplateData[mainkey].Add(newClass); + + return newClass; + } + + /// <summary> + /// 鏄剧ず淇℃伅妗� + /// </summary> + /// <param name="msgType">淇℃伅绫诲瀷</param> + /// <param name="msg">淇℃伅</param> + /// <param name="action">鍗曞嚮纭鍚庢墽琛岀殑鍥炶皟鍑芥暟</param> + /// <param name="buttonText">鎸夐挳鐨勬枃鏈�</param> + private void ShowMassage(ShowMsgType msgType, string msg, Action action = null, string buttonText = null) + { + HdlMessageLogic.Current.ShowMassage(msgType, msg, action, buttonText); + } + + #endregion + } +} -- Gitblit v1.8.0