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

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

@@ -0,0 +1,133 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Threading.Tasks;
using DMS.Application.DTOs.Triggers;
using DMS.Application.Services.Triggers;
using Microsoft.Extensions.Logging; // 使用标准日志接口
namespace DMS.Application.Services.Triggers.Impl
{
/// <summary>
/// 触发器动作执行器实现
/// </summary>
public class TriggerActionExecutor : ITriggerActionExecutor
{
// 假设这些服务将在未来实现或通过依赖注入提供
// 目前我们将它们设为 null并在使用时进行空检查
private readonly IEmailService _emailService; // 假设已在项目中存在或将来实现
private readonly ILogger<TriggerActionExecutor> _logger; // 使用标准日志接口
public TriggerActionExecutor(
// 可以选择性地注入这些服务,如果暂时不需要可以留空或使用占位符
IEmailService emailService,
ILogger<TriggerActionExecutor> logger)
{
_emailService = emailService; // 可能为 null
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
/// <summary>
/// 执行触发动作
/// </summary>
public async Task ExecuteActionAsync(TriggerContext context)
{
try
{
switch (context.Trigger.Action)
{
case ActionType.SendEmail:
await ExecuteSendEmail(context);
break;
case ActionType.ActivateAlarm:
_logger.LogWarning("Action 'ActivateAlarm' is not implemented yet.");
// await ExecuteActivateAlarm(context);
break;
case ActionType.WriteToLog:
_logger.LogWarning("Action 'WriteToLog' is not implemented yet.");
// await ExecuteWriteToLog(context);
break;
default:
_logger.LogWarning("Unsupported action type: {ActionType}", context.Trigger.Action);
break;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error executing action '{ActionType}' for trigger '{TriggerId}'.", context.Trigger.Action, context.Trigger.Id);
// 可以选择是否重新抛出异常或静默处理
// throw;
}
}
#region
private async Task ExecuteSendEmail(TriggerContext context)
{
if (_emailService == null)
{
_logger.LogWarning("Email service is not configured, skipping SendEmail action for trigger '{TriggerId}'.", context.Trigger.Id);
return;
}
var config = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(context.Trigger.ActionConfigurationJson);
if (config == null ||
!config.TryGetValue("Recipients", out var recipientsElement) ||
!config.TryGetValue("SubjectTemplate", out var subjectTemplateElement) ||
!config.TryGetValue("BodyTemplate", out var bodyTemplateElement))
{
_logger.LogError("Invalid configuration for SendEmail action for trigger '{TriggerId}'.", context.Trigger.Id);
return;
}
var recipients = recipientsElement.Deserialize<List<string>>();
var subjectTemplate = subjectTemplateElement.GetString();
var bodyTemplate = bodyTemplateElement.GetString();
if (recipients == null || string.IsNullOrEmpty(subjectTemplate) || string.IsNullOrEmpty(bodyTemplate))
{
_logger.LogError("Missing required fields in SendEmail configuration for trigger '{TriggerId}'.", context.Trigger.Id);
return;
}
// Simple token replacement - in practice, use a templating engine like Scriban, RazorLight etc.
// Note: This assumes context.Variable and context.CurrentValue have Name properties/values.
// You might need to adjust the token names and values based on your actual VariableDto structure.
var subject = subjectTemplate
.Replace("{VariableName}", context.Variable?.Name ?? "Unknown")
.Replace("{CurrentValue}", context.CurrentValue?.ToString() ?? "N/A")
.Replace("{Threshold}", context.Trigger.Threshold?.ToString() ?? "N/A")
.Replace("{LowerBound}", context.Trigger.LowerBound?.ToString() ?? "N/A")
.Replace("{UpperBound}", context.Trigger.UpperBound?.ToString() ?? "N/A");
var body = bodyTemplate
.Replace("{VariableName}", context.Variable?.Name ?? "Unknown")
.Replace("{CurrentValue}", context.CurrentValue?.ToString() ?? "N/A")
.Replace("{Threshold}", context.Trigger.Threshold?.ToString() ?? "N/A")
.Replace("{LowerBound}", context.Trigger.LowerBound?.ToString() ?? "N/A")
.Replace("{UpperBound}", context.Trigger.UpperBound?.ToString() ?? "N/A")
.Replace("{Timestamp}", DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss"));
await _emailService.SendEmailAsync(recipients, subject, body);
}
/*
private async Task ExecuteActivateAlarm(TriggerContext context)
{
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}'.";
// 假设 INotificationService 有 RaiseAlarmAsync 方法
await _notificationService.RaiseAlarmAsync(alarmId, message);
}
private async Task ExecuteWriteToLog(TriggerContext context)
{
var message = $"Trigger '{context.Trigger.Description}' activated for variable '{context.Variable.Name}' with value '{context.CurrentValue}'.";
// 假设 ILoggingService 有 LogTriggerAsync 方法
await _loggingService.LogTriggerAsync(message);
}
*/
#endregion
}
}

View File

@@ -0,0 +1,168 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
// 明确指定 Timer 类型,避免歧义
using ThreadingTimer = System.Threading.Timer;
using TimersTimer = System.Timers.Timer;
using DMS.Application.DTOs.Triggers;
using DMS.Application.Services.Triggers;
using Microsoft.Extensions.Logging; // 使用 Microsoft.Extensions.Logging.ILogger
namespace DMS.Application.Services.Triggers.Impl
{
/// <summary>
/// 触发器评估服务实现
/// </summary>
public class TriggerEvaluationService : ITriggerEvaluationService, IDisposable
{
private readonly ITriggerManagementService _triggerManagementService;
// 移除了 IVariableAppService 依赖
private readonly ITriggerActionExecutor _actionExecutor;
private readonly ILogger<TriggerEvaluationService> _logger; // 使用标准日志接口
// 为每个触发器存储抑制定时器
private readonly ConcurrentDictionary<Guid, ThreadingTimer> _suppressionTimers = new();
public TriggerEvaluationService(
ITriggerManagementService triggerManagementService,
// IVariableAppService variableAppService, // 移除此参数
ITriggerActionExecutor actionExecutor,
ILogger<TriggerEvaluationService> logger) // 使用标准日志接口
{
_triggerManagementService = triggerManagementService ?? throw new ArgumentNullException(nameof(triggerManagementService));
// _variableAppService = variableAppService ?? throw new ArgumentNullException(nameof(variableAppService));
_actionExecutor = actionExecutor ?? throw new ArgumentNullException(nameof(actionExecutor));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
/// <summary>
/// 评估与指定变量关联的所有激活状态的触发器
/// </summary>
public async Task EvaluateTriggersAsync(Guid variableId, object currentValue)
{
try
{
var triggers = await _triggerManagementService.GetTriggersForVariableAsync(variableId);
// 注意:这里不再通过 _variableAppService 获取 VariableDto
// 而是在调用 ExecuteActionAsync 时由上层DataEventService提供。
// 如果需要 VariableDto 信息,可以在 ExecuteActionAsync 的 TriggerContext 中携带。
_logger.LogDebug($"Evaluating {triggers.Count(t => t.IsActive)} active triggers for variable ID: {variableId}");
foreach (var trigger in triggers.Where(t => t.IsActive))
{
if (!IsWithinSuppressionWindow(trigger)) // Check suppression first
{
if (EvaluateCondition(trigger, currentValue))
{
// 创建一个临时的上下文对象,其中 VariableDto 可以为 null
// 因为我们目前没有从 _variableAppService 获取它。
// 在实际应用中,你可能需要通过某种方式获取 VariableDto。
var context = new TriggerContext(trigger, currentValue, null);
await _actionExecutor.ExecuteActionAsync(context);
// Update last triggered time and start suppression timer if needed
trigger.LastTriggeredAt = DateTime.UtcNow;
// 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.
// Start suppression timer if duration is set (in-memory suppression)
if (trigger.SuppressionDuration.HasValue)
{
// 使用 ThreadingTimer 避免歧义
var timer = new ThreadingTimer(_ =>
{
trigger.LastTriggeredAt = null; // Reset suppression flag after delay
_logger.LogInformation($"Suppression lifted for trigger {trigger.Id}");
// 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.
}, null, trigger.SuppressionDuration.Value, Timeout.InfiniteTimeSpan); // Single shot timer
// Replace any existing timer for this trigger ID
_suppressionTimers.AddOrUpdate(trigger.Id, timer, (key, oldTimer) => {
oldTimer?.Dispose();
return timer;
});
}
}
}
}
}
catch (Exception ex)
{
_logger.LogError(ex, "An error occurred while evaluating triggers for variable ID: {VariableId}", variableId);
}
}
/// <summary>
/// 内部方法:评估单个触发器的条件
/// </summary>
private bool EvaluateCondition(TriggerDefinitionDto trigger, object currentValueObj)
{
if (currentValueObj == null)
{
_logger.LogWarning("Cannot evaluate trigger condition: Current value is null for trigger ID: {TriggerId}", trigger.Id);
return false; // Cannot evaluate null
}
// Attempt conversion from object to double - adjust parsing logic as needed for your data types
if (!double.TryParse(currentValueObj.ToString(), out double currentValue))
{
_logger.LogWarning("Could not parse current value '{CurrentValue}' to double for trigger evaluation (trigger ID: {TriggerId}).", currentValueObj, trigger.Id);
return false;
}
bool result = trigger.Condition switch
{
ConditionType.GreaterThan => currentValue > trigger.Threshold,
ConditionType.LessThan => currentValue < trigger.Threshold,
ConditionType.EqualTo => Math.Abs(currentValue - trigger.Threshold.GetValueOrDefault()) < double.Epsilon,
ConditionType.NotEqualTo => Math.Abs(currentValue - trigger.Threshold.GetValueOrDefault()) >= double.Epsilon,
ConditionType.InRange => currentValue >= trigger.LowerBound && currentValue <= trigger.UpperBound,
ConditionType.OutOfRange => currentValue < trigger.LowerBound || currentValue > trigger.UpperBound,
_ => false
};
if(result)
{
_logger.LogInformation("Trigger condition met: Variable value {CurrentValue} satisfies {Condition} for trigger ID: {TriggerId}",
currentValue, trigger.Condition, trigger.Id);
}
return result;
}
/// <summary>
/// 内部方法:检查触发器是否处于抑制窗口期内
/// </summary>
private bool IsWithinSuppressionWindow(TriggerDefinitionDto trigger)
{
if (!trigger.SuppressionDuration.HasValue || !trigger.LastTriggeredAt.HasValue)
return false;
var suppressionEndTime = trigger.LastTriggeredAt.Value.Add(trigger.SuppressionDuration.Value);
bool isSuppressed = DateTime.UtcNow < suppressionEndTime;
if(isSuppressed)
{
_logger.LogTrace("Trigger is suppressed (until {SuppressionEnd}) for trigger ID: {TriggerId}", suppressionEndTime, trigger.Id);
}
return isSuppressed;
}
/// <summary>
/// 实现 IDisposable 以清理计时器资源
/// </summary>
public void Dispose()
{
foreach (var kvp in _suppressionTimers)
{
kvp.Value?.Dispose();
}
_suppressionTimers.Clear();
}
}
}

View File

@@ -0,0 +1,132 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using DMS.Application.DTOs.Triggers;
using DMS.Application.Services.Triggers;
using DMS.Core.Interfaces;
using DMS.Core.Models.Triggers;
namespace DMS.Application.Services.Triggers.Impl
{
/// <summary>
/// 触发器管理服务实现
/// </summary>
public class TriggerManagementService : ITriggerManagementService
{
private readonly IRepositoryManager _repositoryManager;
private readonly IMapper _mapper;
public TriggerManagementService(IRepositoryManager repositoryManager, IMapper mapper)
{
_repositoryManager = repositoryManager ?? throw new ArgumentNullException(nameof(repositoryManager));
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
}
/// <summary>
/// 获取所有触发器定义
/// </summary>
public async Task<List<TriggerDefinitionDto>> GetAllTriggersAsync()
{
var triggers = await _repositoryManager.Triggers.GetAllAsync();
return _mapper.Map<List<TriggerDefinitionDto>>(triggers);
}
/// <summary>
/// 根据 ID 获取触发器定义
/// </summary>
public async Task<TriggerDefinitionDto?> GetTriggerByIdAsync(Guid id)
{
var trigger = await _repositoryManager.Triggers.GetByIdAsync(id);
return trigger != null ? _mapper.Map<TriggerDefinitionDto>(trigger) : null;
}
/// <summary>
/// 创建一个新的触发器定义
/// </summary>
public async Task<TriggerDefinitionDto> CreateTriggerAsync(TriggerDefinitionDto triggerDto)
{
// 1. 验证 DTO (可以在应用层或领域层做)
ValidateTriggerDto(triggerDto);
// 2. 转换 DTO 到实体
var triggerEntity = _mapper.Map<TriggerDefinition>(triggerDto);
triggerEntity.CreatedAt = DateTime.UtcNow;
triggerEntity.UpdatedAt = DateTime.UtcNow;
// 3. 调用仓储保存实体
var createdTrigger = await _repositoryManager.Triggers.AddAsync(triggerEntity);
// 4. 转换回 DTO 并返回
return _mapper.Map<TriggerDefinitionDto>(createdTrigger);
}
/// <summary>
/// 更新一个已存在的触发器定义
/// </summary>
public async Task<TriggerDefinitionDto?> UpdateTriggerAsync(Guid id, TriggerDefinitionDto triggerDto)
{
// 1. 获取现有实体
var existingTrigger = await _repositoryManager.Triggers.GetByIdAsync(id);
if (existingTrigger == null)
return null;
// 2. 验证 DTO
ValidateTriggerDto(triggerDto);
// 3. 将 DTO 映射到现有实体 (排除不可变字段如 Id, CreatedAt)
_mapper.Map(triggerDto, existingTrigger, opts => opts.Items["IgnoreIdAndCreatedAt"] = true);
existingTrigger.UpdatedAt = DateTime.UtcNow;
// 4. 调用仓储更新实体
var updatedTrigger = await _repositoryManager.Triggers.UpdateAsync(existingTrigger);
if (updatedTrigger == null)
return null;
// 5. 转换回 DTO 并返回
return _mapper.Map<TriggerDefinitionDto>(updatedTrigger);
}
/// <summary>
/// 删除一个触发器定义
/// </summary>
public async Task<bool> DeleteTriggerAsync(Guid id)
{
return await _repositoryManager.Triggers.DeleteAsync(id);
}
/// <summary>
/// 获取与指定变量关联的所有触发器定义
/// </summary>
public async Task<List<TriggerDefinitionDto>> GetTriggersForVariableAsync(Guid variableId)
{
var triggers = await _repositoryManager.Triggers.GetByVariableIdAsync(variableId);
return _mapper.Map<List<TriggerDefinitionDto>>(triggers);
}
/// <summary>
/// 内部方法:验证 TriggerDefinitionDto 的有效性
/// </summary>
private void ValidateTriggerDto(TriggerDefinitionDto dto)
{
// 添加必要的验证逻辑
switch (dto.Condition)
{
case ConditionType.GreaterThan:
case ConditionType.LessThan:
case ConditionType.EqualTo:
case ConditionType.NotEqualTo:
if (!dto.Threshold.HasValue)
throw new ArgumentException($"{dto.Condition} requires Threshold.");
break;
case ConditionType.InRange:
case ConditionType.OutOfRange:
if (!dto.LowerBound.HasValue || !dto.UpperBound.HasValue)
throw new ArgumentException($"{dto.Condition} requires LowerBound and UpperBound.");
if (dto.LowerBound > dto.UpperBound)
throw new ArgumentException("LowerBound must be less than or equal to UpperBound.");
break;
}
}
}
}