初步添加报警功能

This commit is contained in:
2025-09-13 12:30:12 +08:00
parent 5a39796f0e
commit 15e2caed22
12 changed files with 457 additions and 1 deletions

View File

@@ -71,12 +71,19 @@ public partial class App : System.Windows.Application
dataProcessingService.AddProcessor(Host.Services.GetRequiredService<HistoryProcessor>());
dataProcessingService.AddProcessor(Host.Services.GetRequiredService<MqttPublishProcessor>());
dataProcessingService.AddProcessor(Host.Services.GetRequiredService<UpdateDbVariableProcessor>());
// 添加报警处理器
dataProcessingService.AddProcessor(Host.Services.GetRequiredService<DMS.Application.Services.Processors.AlarmProcessor>());
}
catch (Exception exception)
{
var notificationService = Host.Services.GetRequiredService<INotificationService>();
notificationService.ShowError($"加载数据时发生错误,如果是连接字符串不正确,可以在设置界面更改:{exception.Message}", exception);
}
// 订阅报警事件
var alarmService = Host.Services.GetRequiredService<DMS.Application.Interfaces.IAlarmService>();
var alarmEventHandler = Host.Services.GetRequiredService<DMS.Application.EventHandlers.AlarmEventHandler>();
alarmService.OnAlarmTriggered += alarmEventHandler.HandleAlarm;
var splashWindow = Host.Services.GetRequiredService<SplashWindow>();
splashWindow.Show();
@@ -184,6 +191,10 @@ public partial class App : System.Windows.Application
services.AddSingleton<UpdateDbVariableProcessor>();
services.AddSingleton<HistoryProcessor>();
services.AddSingleton<MqttPublishProcessor>();
// 注册报警服务和处理器
services.AddSingleton<DMS.Application.Interfaces.IAlarmService, DMS.Application.Services.AlarmService>();
services.AddSingleton<DMS.Application.Services.Processors.AlarmProcessor>();
services.AddSingleton<DMS.Application.EventHandlers.AlarmEventHandler>();
// 注册Core中的仓库
services.AddSingleton<AppSettings>();
@@ -204,6 +215,7 @@ public partial class App : System.Windows.Application
services.AddSingleton<IUserRepository, UserRepository>();
services.AddSingleton<INlogRepository, NlogRepository>();
services.AddSingleton<IRepositoryManager, RepositoryManager>();
services.AddSingleton<IAlarmHistoryRepository, AlarmHistoryRepository>(); // 添加这行
services.AddSingleton<IExcelService, ExcelService>();
services.AddTransient<IOpcUaService, OpcUaService>();

View File

@@ -54,6 +54,11 @@ public partial class VariableItemViewModel : ObservableObject
[ObservableProperty]
private string? _displayValue;
/// <summary>
/// 上一次的数值,用于死区报警计算
/// </summary>
private double _previousNumericValue;
partial void OnDataValueChanged(string? oldValue, string? newValue)
{
// 当DataValue发生变化时更新NumericValue
@@ -66,16 +71,22 @@ public partial class VariableItemViewModel : ObservableObject
// 尝试将字符串转换为数值
if (double.TryParse(newValue, out double numericValue))
{
// 更新上一次的数值
_previousNumericValue = NumericValue;
NumericValue = numericValue;
}
// 如果是布尔值
else if (bool.TryParse(newValue, out bool boolValue))
{
// 更新上一次的数值
_previousNumericValue = NumericValue;
NumericValue = boolValue ? 1.0 : 0.0;
}
// 如果无法转换保持为0.0
else
{
// 更新上一次的数值
_previousNumericValue = NumericValue;
NumericValue = 0.0;
}
}
@@ -224,4 +235,45 @@ public partial class VariableItemViewModel : ObservableObject
private OpcUaUpdateType _opcUaUpdateType;
/// <summary>
/// 上一次的布尔值,用于布尔值变化报警计算
/// </summary>
private bool? _previousBoolValue = null;
/// <summary>
/// 检查死区报警和布尔值变化报警
/// </summary>
public void CheckAdvancedAlarms(VariableDto variable)
{
// 检查死区报警
if (variable.IsAlarmEnabled && variable.AlarmDeadband > 0)
{
double difference = Math.Abs(NumericValue - _previousNumericValue);
if (difference > variable.AlarmDeadband)
{
// 触发死区报警
// 这里可以触发一个事件或调用报警服务
// 为了简单起见,我们暂时只打印日志
Console.WriteLine($"变量 {variable.Name} 的值变化 {difference} 超过了死区 {variable.AlarmDeadband}。");
}
}
// 检查布尔值变化报警
if (variable.IsAlarmEnabled && variable.DataType == DataType.Bool)
{
if (bool.TryParse(DataValue, out bool currentBoolValue))
{
// 如果上一次的值不为空,并且当前值与上一次的值不同,则触发报警
if (_previousBoolValue.HasValue && _previousBoolValue.Value != currentBoolValue)
{
// 触发布尔值变化报警
// 这里可以触发一个事件或调用报警服务
// 为了简单起见,我们暂时只打印日志
Console.WriteLine($"变量 {variable.Name} 的布尔值从 {_previousBoolValue.Value} 变化为 {currentBoolValue}。");
}
// 更新上一次的布尔值
_previousBoolValue = currentBoolValue;
}
}
}
}