千问写完触发器功能,错误未修复

This commit is contained in:
2025-09-14 16:16:10 +08:00
parent 25cd43d436
commit a079cf8de8
24 changed files with 1684 additions and 6 deletions

View File

@@ -5,6 +5,7 @@ using CommunityToolkit.Mvvm.Messaging;
using DMS.Application.DTOs;
using DMS.Application.DTOs.Events;
using DMS.Application.Interfaces;
using DMS.Application.Services.Triggers; // 添加触发器服务引用
using DMS.Core.Enums;
using DMS.Core.Models;
using DMS.Message;
@@ -22,19 +23,23 @@ public class DataEventService : IDataEventService
private readonly IDataStorageService _dataStorageService;
private readonly IAppDataCenterService _appDataCenterService;
private readonly IWPFDataService _wpfDataService;
private readonly ITriggerEvaluationService _triggerEvaluationService; // 新增依赖
/// <summary>
/// DataEventService类的构造函数。
/// </summary>
public DataEventService(IMapper mapper,IDataStorageService dataStorageService,
IAppDataCenterService appDataCenterService,IWPFDataService wpfDataService
public DataEventService(IMapper mapper,
IDataStorageService dataStorageService,
IAppDataCenterService appDataCenterService,
IWPFDataService wpfDataService,
ITriggerEvaluationService triggerEvaluationService // 新增参数
)
{
_mapper = mapper;
_dataStorageService = dataStorageService;
_appDataCenterService = appDataCenterService;
_wpfDataService = wpfDataService;
_triggerEvaluationService = triggerEvaluationService; // 赋值
// 监听变量值变更事件
_appDataCenterService.VariableManagementService.OnVariableValueChanged += OnVariableValueChanged;
@@ -60,7 +65,7 @@ public class DataEventService : IDataEventService
/// <summary>
/// 处理变量值变更事件。
/// </summary>
private void OnVariableValueChanged(object? sender, VariableValueChangedEventArgs e)
private async void OnVariableValueChanged(object? sender, VariableValueChangedEventArgs e) // 改为 async void 以便调用 await
{
// 在UI线程上更新变量值
App.Current.Dispatcher.BeginInvoke(new Action(() =>
@@ -74,6 +79,27 @@ public class DataEventService : IDataEventService
variableToUpdate.UpdatedAt = e.UpdateTime;
}
}));
// 在后台线程上调用触发器评估服务
// 使用 Task.Run 将其放到线程池线程上执行,避免阻塞 UI 线程
// 注意:这里调用的是 Fire-and-forget因为我们不等待结果。
// 如果将来需要处理执行结果或错误,可以考虑使用 async Task 并在适当的地方等待。
_ = Task.Run(async () =>
{
try
{
await _triggerEvaluationService.EvaluateTriggersAsync(e.VariableId, e.NewValue);
}
catch (Exception ex)
{
// Log the exception appropriately.
// Since this is fire-and-forget, we must handle exceptions internally.
// You might have a logging service injected or use a static logger.
// For now, let's assume a static logger or inline comment.
System.Diagnostics.Debug.WriteLine($"Error evaluating triggers for variable {e.VariableId}: {ex}");
// Consider integrating with your logging framework (e.g., NLog) here.
}
});
}