继续修改触发器

This commit is contained in:
2025-09-14 21:18:01 +08:00
parent d923b6a116
commit d622d73057
3 changed files with 229 additions and 81 deletions

View File

@@ -113,22 +113,20 @@ namespace DMS.Application.Services.Triggers.Impl
// await _emailService.SendEmailAsync(recipients, subject, body); // await _emailService.SendEmailAsync(recipients, subject, body);
} }
/*
private async Task ExecuteActivateAlarm(TriggerContext context) private async Task ExecuteActivateAlarm(TriggerContext context)
{ {
var alarmId = $"trigger_{context.Trigger.Id}_{context.Variable.Id}"; var alarmId = $"trigger_{context.Trigger.Id}_{context.Variable.Id}";
var message = $"Trigger '{context.Trigger.Description}' activated for variable '{context.Variable.Name}' with value '{context.CurrentValue}'."; var message = $"Trigger '{context.Trigger.Description}' activated for variable '{context.Variable.Name}' with value '{context.CurrentValue}'.";
// 假设 INotificationService 有 RaiseAlarmAsync 方法 // 假设 INotificationService 有 RaiseAlarmAsync 方法
await _notificationService.RaiseAlarmAsync(alarmId, message); // await _notificationService.RaiseAlarmAsync(alarmId, message);
} }
private async Task ExecuteWriteToLog(TriggerContext context) private async Task ExecuteWriteToLog(TriggerContext context)
{ {
var message = $"Trigger '{context.Trigger.Description}' activated for variable '{context.Variable.Name}' with value '{context.CurrentValue}'."; var message = $"Trigger '{context.Trigger.Description}' activated for variable '{context.Variable.Name}' with value '{context.CurrentValue}'.";
// 假设 ILoggingService 有 LogTriggerAsync 方法 // 假设 ILoggingService 有 LogTriggerAsync 方法
await _loggingService.LogTriggerAsync(message); // await _loggingService.LogTriggerAsync(message);
} }
*/
#endregion #endregion
} }

View File

@@ -42,59 +42,59 @@ namespace DMS.Application.Services.Triggers.Impl
/// </summary> /// </summary>
public async Task EvaluateTriggersAsync(int variableId, object currentValue) public async Task EvaluateTriggersAsync(int variableId, object currentValue)
{ {
// try try
// { {
// var triggers = await _triggerManagementService.GetTriggersForVariableAsync(variableId); var triggers = await _triggerManagementService.GetTriggersForVariableAsync(variableId);
// 注意:这里不再通过 _variableAppService 获取 VariableDto // 注意:这里不再通过 _variableAppService 获取 VariableDto
// 而是在调用 ExecuteActionAsync 时由上层DataEventService提供。 // 而是在调用 ExecuteActionAsync 时由上层DataEventService提供。
// 如果需要 VariableDto 信息,可以在 ExecuteActionAsync 的 TriggerContext 中携带。 // 如果需要 VariableDto 信息,可以在 ExecuteActionAsync 的 TriggerContext 中携带。
// _logger.LogDebug($"Evaluating {triggers.Count(t => t.IsActive)} active triggers for variable ID: {variableId}"); _logger.LogDebug($"Evaluating {triggers.Count(t => t.IsActive)} active triggers for variable ID: {variableId}");
//
// foreach (var trigger in triggers.Where(t => t.IsActive)) foreach (var trigger in triggers.Where(t => t.IsActive))
// { {
// if (!IsWithinSuppressionWindow(trigger)) // Check suppression first if (!IsWithinSuppressionWindow(trigger)) // Check suppression first
// { {
// if (EvaluateCondition(trigger, currentValue)) if (EvaluateCondition(trigger, currentValue))
// { {
// // 创建一个临时的上下文对象,其中 VariableDto 可以为 null // 创建一个临时的上下文对象,其中 VariableDto 可以为 null
// // 因为我们目前没有从 _variableAppService 获取它。 // 因为我们目前没有从 _variableAppService 获取它。
// // 在实际应用中,你可能需要通过某种方式获取 VariableDto。 // 在实际应用中,你可能需要通过某种方式获取 VariableDto。
// var context = new TriggerContext(trigger, currentValue, null); var context = new TriggerContext(trigger, currentValue, null);
//
// await _actionExecutor.ExecuteActionAsync(context); await _actionExecutor.ExecuteActionAsync(context);
//
// // Update last triggered time and start suppression timer if needed // Update last triggered time and start suppression timer if needed
// trigger.LastTriggeredAt = DateTime.UtcNow; trigger.LastTriggeredAt = DateTime.UtcNow;
// // For simplicity, we'll assume it's updated periodically or on next load. // For simplicity, we'll assume it's updated periodically or on next load.
// // In a production scenario, you'd likely want to persist this back to the database. // In a production scenario, you'd likely want to persist this back to the database.
//
// // Start suppression timer if duration is set (in-memory suppression) // Start suppression timer if duration is set (in-memory suppression)
// if (trigger.SuppressionDuration.HasValue) if (trigger.SuppressionDuration.HasValue)
// { {
// // 使用 ThreadingTimer 避免歧义 // 使用 ThreadingTimer 避免歧义
// var timer = new ThreadingTimer(_ => var timer = new ThreadingTimer(_ =>
// { {
// trigger.LastTriggeredAt = null; // Reset suppression flag after delay trigger.LastTriggeredAt = null; // Reset suppression flag after delay
// _logger.LogInformation($"Suppression lifted for trigger {trigger.Id}"); _logger.LogInformation($"Suppression lifted for trigger {trigger.Id}");
// // Note: Modifying 'trigger' directly affects the object in the list returned by GetTriggersForVariableAsync(). // Note: Modifying 'trigger' directly affects the object in the list returned by GetTriggersForVariableAsync().
// // This works for in-memory state but won't persist changes. Consider updating DB explicitly if needed. // This works for in-memory state but won't persist changes. Consider updating DB explicitly if needed.
// }, null, trigger.SuppressionDuration.Value, Timeout.InfiniteTimeSpan); // Single shot timer }, null, trigger.SuppressionDuration.Value, Timeout.InfiniteTimeSpan); // Single shot timer
//
// // Replace any existing timer for this trigger ID // Replace any existing timer for this trigger ID
// _suppressionTimers.AddOrUpdate(trigger.Id, timer, (key, oldTimer) => { _suppressionTimers.AddOrUpdate(trigger.Id, timer, (key, oldTimer) => {
// oldTimer?.Dispose(); oldTimer?.Dispose();
// return timer; return timer;
// }); });
// } }
// } }
// } }
// } }
// } }
// catch (Exception ex) catch (Exception ex)
// { {
// _logger.LogError(ex, "An error occurred while evaluating triggers for variable ID: {VariableId}", variableId); _logger.LogError(ex, "An error occurred while evaluating triggers for variable ID: {VariableId}", variableId);
// } }
} }
/// <summary> /// <summary>

View File

@@ -4,18 +4,174 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern" xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
xmlns:hc="https://handyorg.github.io/handycontrol"
xmlns:converters="clr-namespace:DMS.WPF.Converters"
mc:Ignorable="d" mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"> d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>
<!-- Converters -->
<converters:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
<converters:BoolToStringConverter x:Key="BoolToStringConverter" />
<converters:BoolToColorConverter x:Key="BoolToColorConverter"/>
<!-- 触发器项模板 -->
<DataTemplate x:Key="TriggerItemTemplate">
<Border Background="White"
BorderBrush="#E0E0E0"
BorderThickness="1"
CornerRadius="8"
Margin="5"
Padding="15">
<Border.Effect>
<DropShadowEffect ShadowDepth="2"
BlurRadius="5"
Opacity="0.1"
Color="#888888"/>
</Border.Effect>
<Grid>
<Grid Margin="10"> <Grid.RowDefinitions>
<Grid.RowDefinitions> <RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/>
<RowDefinition Height="*"/> </Grid.RowDefinitions>
</Grid.RowDefinitions>
<!-- 触发器基本信息 -->
<StackPanel Grid.Row="0" Margin="0,0,0,10">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<!-- 触发器描述和变量信息 -->
<StackPanel Grid.Column="0">
<StackPanel Orientation="Horizontal" Margin="0,0,0,5">
<ui:FontIcon Glyph="&#xE76C;"
FontSize="20"
Foreground="{DynamicResource SystemAccentColorBrush}"
Margin="0,0,8,0"/>
<TextBlock Text="{Binding Description}"
FontWeight="SemiBold"
FontSize="16"
VerticalAlignment="Center"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<ui:FontIcon Glyph="&#xE975;"
FontSize="14"
Foreground="#888888"
Margin="0,0,5,0"/>
<TextBlock Text="{Binding VariableId}"
Foreground="#888888"
FontSize="12"
Margin="0,0,15,0"/>
<Rectangle Width="1"
Height="12"
Fill="#CCCCCC"
Margin="5,0"/>
<ui:FontIcon Glyph="&#xE76C;"
FontSize="14"
Foreground="#888888"
Margin="10,0,5,0"/>
<TextBlock Text="{Binding Condition}"
Foreground="#888888"
FontSize="12"/>
</StackPanel>
</StackPanel>
<!-- 状态指示器 -->
<StackPanel Grid.Column="1"
Orientation="Horizontal"
VerticalAlignment="Top">
<Border Background="{Binding IsActive,
Converter={StaticResource BoolToColorConverter},
ConverterParameter='Green;Red'}"
CornerRadius="8"
Width="16"
Height="16"
Margin="0,0,10,0">
<Border.ToolTip>
<TextBlock Text="{Binding IsActive,
Converter={StaticResource BoolToStringConverter},
ConverterParameter='已激活;未激活'}"/>
</Border.ToolTip>
</Border>
</StackPanel>
</Grid>
</StackPanel>
<!-- 触发器详细信息 -->
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<!-- 左侧信息 -->
<StackPanel Grid.Column="0">
<StackPanel Orientation="Horizontal" Margin="0,0,0,3">
<TextBlock Text="阈值/范围: "
FontWeight="SemiBold"
Foreground="#666666"
FontSize="12"/>
<TextBlock Text="{Binding Threshold, StringFormat='{}{0:N2}'}"
Foreground="#333333"
FontSize="12"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="下限: "
FontWeight="SemiBold"
Foreground="#666666"
FontSize="12"/>
<TextBlock Text="{Binding LowerBound, StringFormat='{}{0:N2}'}"
Foreground="#333333"
FontSize="12"
Margin="0,0,15,0"/>
<TextBlock Text="上限: "
FontWeight="SemiBold"
Foreground="#666666"
FontSize="12"/>
<TextBlock Text="{Binding UpperBound, StringFormat='{}{0:N2}'}"
Foreground="#333333"
FontSize="12"/>
</StackPanel>
</StackPanel>
<!-- 右侧信息 -->
<StackPanel Grid.Column="1">
<StackPanel Orientation="Horizontal" Margin="0,0,0,3">
<TextBlock Text="动作: "
FontWeight="SemiBold"
Foreground="#666666"
FontSize="12"/>
<TextBlock Text="{Binding Action}"
Foreground="#333333"
FontSize="12"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="最后触发: "
FontWeight="SemiBold"
Foreground="#666666"
FontSize="12"/>
<TextBlock Text="{Binding LastTriggeredAt, StringFormat='{}{0:yyyy-MM-dd HH:mm:ss}'}"
Foreground="#333333"
FontSize="12"/>
</StackPanel>
</StackPanel>
</Grid>
</Grid>
</Border>
</DataTemplate>
</UserControl.Resources>
<StackPanel>
<!-- Toolbar --> <!-- Toolbar -->
<ui:CommandBar Grid.Row="0" DefaultLabelPosition="Right" IsOpen="False" Margin="0,0,0,10"> <ui:CommandBar DefaultLabelPosition="Right" IsOpen="False" Margin="0,0,0,10">
<ui:AppBarButton Command="{Binding AddTriggerCommand}" Label="添加触发器"> <ui:AppBarButton Command="{Binding AddTriggerCommand}" Label="添加触发器">
<ui:AppBarButton.Icon> <ui:AppBarButton.Icon>
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Add}" /> <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Add}" />
@@ -39,25 +195,19 @@
</ui:CommandBar> </ui:CommandBar>
<!-- Triggers List --> <!-- Triggers List -->
<DataGrid Grid.Row="1" <ui:GridView
ItemsSource="{Binding Triggers}" x:Name="TriggersGridView"
SelectedItem="{Binding SelectedTrigger}" Margin="20"
AutoGenerateColumns="False" IsItemClickEnabled="True"
IsReadOnly="True" ItemTemplate="{StaticResource TriggerItemTemplate}"
SelectionMode="Single"> ItemsSource="{Binding Triggers}"
<DataGrid.Columns> SelectedItem="{Binding SelectedTrigger}"
<DataGridTextColumn Header="描述" Binding="{Binding Description}" Width="*" /> SelectionMode="Single">
<DataGridTextColumn Header="变量ID" Binding="{Binding VariableId}" Width="150" /> <hc:Interaction.Triggers>
<DataGridTextColumn Header="条件" Binding="{Binding Condition}" Width="100" /> <hc:EventTrigger EventName="MouseDoubleClick">
<DataGridTextColumn Header="阈值/范围" Binding="{Binding Threshold, StringFormat='{}{0:N2}'}" Width="100" /> <hc:InvokeCommandAction Command="{Binding EditTriggerCommand}" />
<DataGridTextColumn Header="下限" Binding="{Binding LowerBound, StringFormat='{}{0:N2}'}" Width="100" /> </hc:EventTrigger>
<DataGridTextColumn Header="上限" Binding="{Binding UpperBound, StringFormat='{}{0:N2}'}" Width="100" /> </hc:Interaction.Triggers>
<DataGridTextColumn Header="动作" Binding="{Binding Action}" Width="100" /> </ui:GridView>
<DataGridTextColumn Header="激活" Binding="{Binding IsActive}" Width="60" /> </StackPanel>
<DataGridTextColumn Header="最后触发" Binding="{Binding LastTriggeredAt, StringFormat='{}{0:yyyy-MM-dd HH:mm:ss}'}" Width="150" />
<DataGridTextColumn Header="创建时间" Binding="{Binding CreatedAt, StringFormat='{}{0:yyyy-MM-dd HH:mm:ss}'}" Width="150" />
<DataGridTextColumn Header="更新时间" Binding="{Binding UpdatedAt, StringFormat='{}{0:yyyy-MM-dd HH:mm:ss}'}" Width="150" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</UserControl> </UserControl>