修改批量导入变量返回实体,防止Io为0的问题

This commit is contained in:
2025-09-15 13:12:14 +08:00
parent d622d73057
commit 4773e87886
26 changed files with 171 additions and 139 deletions

View File

@@ -5,7 +5,6 @@ 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;
@@ -23,7 +22,6 @@ public class DataEventService : IDataEventService
private readonly IDataStorageService _dataStorageService;
private readonly IAppDataCenterService _appDataCenterService;
private readonly IWPFDataService _wpfDataService;
private readonly ITriggerEvaluationService _triggerEvaluationService; // 新增依赖
/// <summary>
/// DataEventService类的构造函数。
@@ -31,15 +29,12 @@ public class DataEventService : IDataEventService
public DataEventService(IMapper mapper,
IDataStorageService dataStorageService,
IAppDataCenterService appDataCenterService,
IWPFDataService wpfDataService,
ITriggerEvaluationService triggerEvaluationService // 新增参数
)
IWPFDataService wpfDataService)
{
_mapper = mapper;
_dataStorageService = dataStorageService;
_appDataCenterService = appDataCenterService;
_wpfDataService = wpfDataService;
_triggerEvaluationService = triggerEvaluationService; // 赋值
// 监听变量值变更事件
_appDataCenterService.VariableManagementService.OnVariableValueChanged += OnVariableValueChanged;
@@ -65,7 +60,7 @@ public class DataEventService : IDataEventService
/// <summary>
/// 处理变量值变更事件。
/// </summary>
private async void OnVariableValueChanged(object? sender, VariableValueChangedEventArgs e) // 改为 async void 以便调用 await
private void OnVariableValueChanged(object? sender, VariableValueChangedEventArgs e)
{
// 在UI线程上更新变量值
App.Current.Dispatcher.BeginInvoke(new Action(() =>
@@ -79,27 +74,6 @@ 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.
}
});
}