From 8aaee587fa89420b067f64860cf92e110b82f1ae Mon Sep 17 00:00:00 2001 From: maqiao <625215135@qq.com> Date: Fri, 14 Mar 2025 09:45:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=86=B0=E7=AE=B1=E8=AE=BE=E7=BD=AE=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E8=AF=BB=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ViewModels/FridgeOnlyWindowViewModel.cs | 88 ++++++++++++------- DM_Weight/ViewModels/HomeWindowViewModel.cs | 1 + 2 files changed, 59 insertions(+), 30 deletions(-) diff --git a/DM_Weight/ViewModels/FridgeOnlyWindowViewModel.cs b/DM_Weight/ViewModels/FridgeOnlyWindowViewModel.cs index e3c080a..3260ece 100644 --- a/DM_Weight/ViewModels/FridgeOnlyWindowViewModel.cs +++ b/DM_Weight/ViewModels/FridgeOnlyWindowViewModel.cs @@ -30,19 +30,15 @@ namespace DM_Weight.ViewModels set { SetProperty(ref _temperatureRange, value); - //更新配置文件中冰箱温度区间 - CommonClass.SaveAppSetting("temperatureRange", _temperatureRange); } } //温度不在范围超时时间 - private string _outRangeTime = ConfigurationManager.AppSettings["OutRangeTime"].ToString(); + private string _outRangeTime = CommonClass.ReadAppSetting("OutRangeTime").ToString(); public string OutRangeTime { get => _outRangeTime; set { SetProperty(ref _outRangeTime, value); - //更新配置文件中冰箱温度区间 - CommonClass.SaveAppSetting("OutRangeTime", _outRangeTime); } } @@ -89,7 +85,7 @@ namespace DM_Weight.ViewModels { get => _alarmState; set => SetProperty(ref _alarmState, value); - } + } @@ -126,42 +122,60 @@ namespace DM_Weight.ViewModels await _portUtil.FridgeOff(1); Thread.Sleep(100); CommonClass.SaveAppSetting("FridgeState", "1"); + + //Configuration _configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); + //_configuration.AppSettings.Settings["FridgeState"].Value = "1"; + //_configuration.Save(); + //ConfigurationManager.RefreshSection("FridgeState"); } else { await _portUtil.FridegOpen(1); Thread.Sleep(100); CommonClass.SaveAppSetting("FridgeState", "0"); + //Configuration _configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); + //_configuration.AppSettings.Settings["FridgeState"].Value = "0"; + //_configuration.Save(); + //ConfigurationManager.RefreshSection("FridgeState"); //冰箱打开定时获取冰箱温度 _eventAggregator.GetEvent().Publish(); - } + } //发送警报使能/失能指令 if (AlarmState) { await _portUtil.FridgeAlarmOff(1); Thread.Sleep(100); CommonClass.SaveAppSetting("AlarmState", "1"); + //Configuration _configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); + //_configuration.AppSettings.Settings["AlarmState"].Value = "1"; + //_configuration.Save(); + //ConfigurationManager.RefreshSection("AlarmState"); } else { await _portUtil.FridgeAlarmOn(1); Thread.Sleep(100); CommonClass.SaveAppSetting("AlarmState", "0"); - } + + //Configuration _configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); + //_configuration.AppSettings.Settings["AlarmState"].Value = "0"; + //_configuration.Save(); + //ConfigurationManager.RefreshSection("AlarmState"); + } //设定冰箱1温度区间 - string[] range = ConfigurationManager.AppSettings["temperatureRange"].Split('-'); + //string[] range = ConfigurationManager.AppSettings["temperatureRange"].Split('-'); string[] newRange = TemperatureRange.Split('-'); - if (range.Length >= 2) + if (newRange.Length >= 2) { - bool bMix = float.TryParse(range[0], out float Min); - bool bMax = float.TryParse(range[1], out float Max); + bool bMix = float.TryParse(newRange[0], out float Min); + bool bMax = float.TryParse(newRange[1], out float Max); if (bMix && bMax) { if (Min != Convert.ToSingle(newRange[0])) { //设定冰箱温度最小值 - await _portUtil.FridgeMinSetting(Convert.ToSingle(newRange[0]), 1); + await _portUtil.FridgeMinSetting(Convert.ToSingle(newRange[0]),1); Thread.Sleep(100); } if (Max != Convert.ToSingle(newRange[1])) @@ -172,8 +186,22 @@ namespace DM_Weight.ViewModels Thread.Sleep(100); } } + //Configuration _configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); + //_configuration.AppSettings.Settings["TemperatureRange"].Value = c; + //_configuration.Save(); + //ConfigurationManager.RefreshSection("TemperatureRange"); + CommonClass.SaveAppSetting("TemperatureRange", TemperatureRange); } - + //超时时间 + if (OutRangeTime != null) + { + //Configuration _configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); + //_configuration.AppSettings.Settings["OutRangeTime"].Value = OutRangeTime; + //_configuration.Save(); + //ConfigurationManager.RefreshSection("OutRangeTime"); + CommonClass.SaveAppSetting("OutRangeTime", OutRangeTime); + } + _portUtil.FridgeOperate = false; AlertMsg alertMsg = new AlertMsg @@ -181,7 +209,7 @@ namespace DM_Weight.ViewModels Message = $"保存成功", Type = MsgType.SUCCESS }; - _eventAggregator.GetEvent().Publish(alertMsg); + _eventAggregator.GetEvent().Publish(alertMsg); } catch (Exception ex) { @@ -194,18 +222,18 @@ namespace DM_Weight.ViewModels _eventAggregator.GetEvent().Publish(alertMsg); _portUtil.FridgeOperate = false; } - } - //手动实现调用配置的逻辑 规避修改配置文件后不起作用的问题 - public int ReadAppSetting(string key) - { - string xPath = "/configuration/appSettings//add[@key='" + key + "']"; - XmlDocument doc = new XmlDocument(); - string exeFileName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name; - doc.Load(exeFileName + ".dll.config"); - XmlNode node = doc.SelectSingleNode(xPath); - logger.Info($"xPath:{xPath} exeFileName:{exeFileName} ReadAppSetting key:{key} value:{node.Attributes["value"].Value}"); - return Convert.ToInt32(node.Attributes["value"].Value); } + //手动实现调用配置的逻辑 规避修改配置文件后不起作用的问题 + //public int ReadAppSetting(string key) + //{ + // string xPath = "/configuration/appSettings//add[@key='" + key + "']"; + // XmlDocument doc = new XmlDocument(); + // string exeFileName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name; + // doc.Load(exeFileName + ".dll.config"); + // XmlNode node = doc.SelectSingleNode(xPath); + // logger.Info($"xPath:{xPath} exeFileName:{exeFileName} ReadAppSetting key:{key} value:{node.Attributes["value"].Value}"); + // return Convert.ToInt32(node.Attributes["value"].Value); + //} public void ConfirmNavigationRequest(NavigationContext navigationContext, Action continuationCallback) { @@ -213,9 +241,9 @@ namespace DM_Weight.ViewModels public void OnNavigatedTo(NavigationContext navigationContext) { - FridgeState = ReadAppSetting("FridgeState").Equals(1); - - AlarmState = ReadAppSetting("AlarmState").Equals(1); + FridgeState =CommonClass.ReadAppSetting("FridgeState").Equals("1"); + + AlarmState = CommonClass.ReadAppSetting("AlarmState").Equals("1"); GetTemperature(); } @@ -227,7 +255,7 @@ namespace DM_Weight.ViewModels public void OnNavigatedFrom(NavigationContext navigationContext) { - } + } //获取冰箱温度值,如有更改则保存更改 private async Task GetTemperature() { diff --git a/DM_Weight/ViewModels/HomeWindowViewModel.cs b/DM_Weight/ViewModels/HomeWindowViewModel.cs index 6f759d9..a1c771f 100644 --- a/DM_Weight/ViewModels/HomeWindowViewModel.cs +++ b/DM_Weight/ViewModels/HomeWindowViewModel.cs @@ -692,6 +692,7 @@ namespace DM_Weight.ViewModels { try { + FridgeState = CommonClass.ReadAppSetting("FridgeState"); if (!_portUtil.FridgeOperate && FridgeState.Equals("0")) { string retStr = string.Empty;