This commit is contained in:
xyy
2026-04-09 10:49:28 +08:00
parent a1532d7954
commit ae7b329792
5 changed files with 123 additions and 2 deletions

View File

@@ -5,6 +5,7 @@ using System.Net;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using static OfficeOpenXml.ExcelErrorValue;
namespace MembranePoreTester.ViewModels
@@ -12,6 +13,41 @@ namespace MembranePoreTester.ViewModels
public class MainViewModel : ViewModelBase
{
public ObservableCollection<StationItem> Stations { get; } = new();
// 报警信息集合(用于显示多条)
private ObservableCollection<string> _alarmMessages;
public ObservableCollection<string> AlarmMessages
{
get => _alarmMessages;
set => SetProperty(ref _alarmMessages, value);
}
private bool _hasAlarm;
public bool HasAlarm
{
get => _hasAlarm;
set => SetProperty(ref _hasAlarm, value);
}
private string _alarmText;
public string AlarmText
{
get => _alarmText;
set => SetProperty(ref _alarmText, value);
}
private DispatcherTimer _alarmTimer;
public class PressureModeItem
{
public string Text { get; set; }
@@ -396,6 +432,52 @@ namespace MembranePoreTester.ViewModels
};
Stations.Add(station);
}
AlarmMessages = new ObservableCollection<string>();
_alarmTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) };
_alarmTimer.Tick += async (s, e) => await RefreshAlarms();
_alarmTimer.Start();
}
private async Task RefreshAlarms()
{
try
{
var plc = App.PlcService;
var config = App.PlcConfig;
// 读取4个报警线圈状态
bool smallFlowAlarm = await plc.ReadCoilAsync(config.SmallFlowAlarm);
bool bigFlowAlarm = await plc.ReadCoilAsync(config.BigFlowAlarm);
bool highPressAlarm = await plc.ReadCoilAsync(config.HighPressAlarm);
bool lowPressAlarm = await plc.ReadCoilAsync(config.LowPressAlarm);
// 收集当前报警信息
var newAlarms = new List<string>();
if (smallFlowAlarm) newAlarms.Add("小流量计报警");
if (bigFlowAlarm) newAlarms.Add("大流量报警");
if (highPressAlarm) newAlarms.Add("高压超限");
if (lowPressAlarm) newAlarms.Add("低压超限");
// 更新UI避免频繁刷新集合导致界面闪烁直接替换内容
Application.Current.Dispatcher.Invoke(() =>
{
AlarmMessages.Clear();
foreach (var msg in newAlarms)
AlarmMessages.Add(msg);
HasAlarm = newAlarms.Any();
AlarmText = HasAlarm ? string.Join("", newAlarms) : "";
});
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"读取报警状态失败: {ex.Message}");
}
}
}
}