冰箱设置页面读配置文件信息修改

This commit is contained in:
maqiao 2025-03-14 09:45:33 +08:00
parent 30c807ecd5
commit 8aaee587fa
2 changed files with 59 additions and 30 deletions

View File

@ -30,19 +30,15 @@ namespace DM_Weight.ViewModels
set set
{ {
SetProperty(ref _temperatureRange, value); 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 public string OutRangeTime
{ {
get => _outRangeTime; set get => _outRangeTime; set
{ {
SetProperty(ref _outRangeTime, value); SetProperty(ref _outRangeTime, value);
//更新配置文件中冰箱温度区间
CommonClass.SaveAppSetting("OutRangeTime", _outRangeTime);
} }
} }
@ -89,7 +85,7 @@ namespace DM_Weight.ViewModels
{ {
get => _alarmState; get => _alarmState;
set => SetProperty(ref _alarmState, value); set => SetProperty(ref _alarmState, value);
} }
@ -126,42 +122,60 @@ namespace DM_Weight.ViewModels
await _portUtil.FridgeOff(1); await _portUtil.FridgeOff(1);
Thread.Sleep(100); Thread.Sleep(100);
CommonClass.SaveAppSetting("FridgeState", "1"); CommonClass.SaveAppSetting("FridgeState", "1");
//Configuration _configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//_configuration.AppSettings.Settings["FridgeState"].Value = "1";
//_configuration.Save();
//ConfigurationManager.RefreshSection("FridgeState");
} }
else else
{ {
await _portUtil.FridegOpen(1); await _portUtil.FridegOpen(1);
Thread.Sleep(100); Thread.Sleep(100);
CommonClass.SaveAppSetting("FridgeState", "0"); CommonClass.SaveAppSetting("FridgeState", "0");
//Configuration _configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//_configuration.AppSettings.Settings["FridgeState"].Value = "0";
//_configuration.Save();
//ConfigurationManager.RefreshSection("FridgeState");
//冰箱打开定时获取冰箱温度 //冰箱打开定时获取冰箱温度
_eventAggregator.GetEvent<FridgeEvent>().Publish(); _eventAggregator.GetEvent<FridgeEvent>().Publish();
} }
//发送警报使能/失能指令 //发送警报使能/失能指令
if (AlarmState) if (AlarmState)
{ {
await _portUtil.FridgeAlarmOff(1); await _portUtil.FridgeAlarmOff(1);
Thread.Sleep(100); Thread.Sleep(100);
CommonClass.SaveAppSetting("AlarmState", "1"); CommonClass.SaveAppSetting("AlarmState", "1");
//Configuration _configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//_configuration.AppSettings.Settings["AlarmState"].Value = "1";
//_configuration.Save();
//ConfigurationManager.RefreshSection("AlarmState");
} }
else else
{ {
await _portUtil.FridgeAlarmOn(1); await _portUtil.FridgeAlarmOn(1);
Thread.Sleep(100); Thread.Sleep(100);
CommonClass.SaveAppSetting("AlarmState", "0"); CommonClass.SaveAppSetting("AlarmState", "0");
}
//Configuration _configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//_configuration.AppSettings.Settings["AlarmState"].Value = "0";
//_configuration.Save();
//ConfigurationManager.RefreshSection("AlarmState");
}
//设定冰箱1温度区间 //设定冰箱1温度区间
string[] range = ConfigurationManager.AppSettings["temperatureRange"].Split('-'); //string[] range = ConfigurationManager.AppSettings["temperatureRange"].Split('-');
string[] newRange = TemperatureRange.Split('-'); string[] newRange = TemperatureRange.Split('-');
if (range.Length >= 2) if (newRange.Length >= 2)
{ {
bool bMix = float.TryParse(range[0], out float Min); bool bMix = float.TryParse(newRange[0], out float Min);
bool bMax = float.TryParse(range[1], out float Max); bool bMax = float.TryParse(newRange[1], out float Max);
if (bMix && bMax) if (bMix && bMax)
{ {
if (Min != Convert.ToSingle(newRange[0])) 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); Thread.Sleep(100);
} }
if (Max != Convert.ToSingle(newRange[1])) if (Max != Convert.ToSingle(newRange[1]))
@ -172,8 +186,22 @@ namespace DM_Weight.ViewModels
Thread.Sleep(100); 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; _portUtil.FridgeOperate = false;
AlertMsg alertMsg = new AlertMsg AlertMsg alertMsg = new AlertMsg
@ -181,7 +209,7 @@ namespace DM_Weight.ViewModels
Message = $"保存成功", Message = $"保存成功",
Type = MsgType.SUCCESS Type = MsgType.SUCCESS
}; };
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg); _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -194,18 +222,18 @@ namespace DM_Weight.ViewModels
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg); _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
_portUtil.FridgeOperate = false; _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<bool> continuationCallback) public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
{ {
@ -213,9 +241,9 @@ namespace DM_Weight.ViewModels
public void OnNavigatedTo(NavigationContext navigationContext) public void OnNavigatedTo(NavigationContext navigationContext)
{ {
FridgeState = ReadAppSetting("FridgeState").Equals(1); FridgeState =CommonClass.ReadAppSetting("FridgeState").Equals("1");
AlarmState = ReadAppSetting("AlarmState").Equals(1); AlarmState = CommonClass.ReadAppSetting("AlarmState").Equals("1");
GetTemperature(); GetTemperature();
} }
@ -227,7 +255,7 @@ namespace DM_Weight.ViewModels
public void OnNavigatedFrom(NavigationContext navigationContext) public void OnNavigatedFrom(NavigationContext navigationContext)
{ {
} }
//获取冰箱温度值,如有更改则保存更改 //获取冰箱温度值,如有更改则保存更改
private async Task GetTemperature() private async Task GetTemperature()
{ {

View File

@ -692,6 +692,7 @@ namespace DM_Weight.ViewModels
{ {
try try
{ {
FridgeState = CommonClass.ReadAppSetting("FridgeState");
if (!_portUtil.FridgeOperate && FridgeState.Equals("0")) if (!_portUtil.FridgeOperate && FridgeState.Equals("0"))
{ {
string retStr = string.Empty; string retStr = string.Empty;