using MediatR; using Microsoft.Extensions.Logging; using X1.Application.Features.TaskExecution.Events.Interfaces; using X1.Application.Features.TaskExecution.Events.NodeExecutionEvents; using X1.Domain.Events; using X1.Domain.Entities.TestCase; namespace X1.Application.Features.TaskExecution.Events.ControllerHandlers; /// /// 关闭飞行模式控制器处理器 /// 处理关闭飞行模式相关的节点执行事件 /// public class DisableFlightModeControllerHandler : INodeExecutionHandlerBase { /// /// 初始化关闭飞行模式控制器处理器 /// /// MediatR 中介者 /// 日志记录器 public DisableFlightModeControllerHandler(IMediator mediator, ILogger logger) : base(mediator, logger) { } /// /// 处理关闭飞行模式节点执行事件 /// /// 节点执行事件 /// 取消令牌 /// 处理任务 public override async Task HandleAsync(ControllerExecutionEvent notification, CancellationToken cancellationToken) { _logger.LogInformation("开始执行关闭飞行模式,任务执行ID: {TaskExecutionId}, 节点ID: {NodeId}, 运行时编码: {RuntimeCode}", notification.TaskExecutionId, notification.NodeId, notification.RuntimeCode); try { // 1. 更新节点状态为运行中 await UpdateNodeStatusAsync(notification.NodeId, NodeExecutionStatus.Running); // 2. 执行关闭飞行模式业务逻辑 var result = await ExecuteDisableFlightModeAsync(notification, cancellationToken); // 3. 创建执行结果 var executionResult = new NodeExecutionResult { TaskExecutionId = notification.TaskExecutionId, NodeId = notification.NodeId, StepMapping = notification.StepMapping, Status = NodeExecutionStatus.Completed, IsSuccess = true, ResultData = result, ExecutorId = notification.ExecutorId, RuntimeCode = notification.RuntimeCode, CreatedAt = DateTime.UtcNow }; // 4. 发布完成事件 await PublishCompletedEventAsync(notification, executionResult, cancellationToken); _logger.LogInformation("关闭飞行模式执行成功,任务执行ID: {TaskExecutionId}, 节点ID: {NodeId}", notification.TaskExecutionId, notification.NodeId); } catch (Exception ex) { _logger.LogError(ex, "关闭飞行模式执行失败,任务执行ID: {TaskExecutionId}, 节点ID: {NodeId}", notification.TaskExecutionId, notification.NodeId); // 发布失败事件 await PublishFailedEventAsync(notification, ex.Message, cancellationToken); } } /// /// 检查是否支持处理指定的事件类型 /// /// 步骤映射类型 /// 是否支持 public override bool CanHandle(StepMapping stepMapping) { return stepMapping == StepMapping.DisableFlightMode; } /// /// 获取处理器支持的步骤映射类型 /// /// 支持的步骤映射类型 public override StepMapping GetSupportedStepMapping() { return StepMapping.DisableFlightMode; } /// /// 执行关闭飞行模式业务逻辑 /// /// 事件通知 /// 取消令牌 /// 执行结果 private async Task ExecuteDisableFlightModeAsync(ControllerExecutionEvent notification, CancellationToken cancellationToken) { // TODO: 实现具体的关闭飞行模式逻辑 // 这里应该调用实际的设备控制服务 _logger.LogInformation("正在执行关闭飞行模式,运行时编码: {RuntimeCode}", notification.RuntimeCode); // 模拟异步操作 await Task.Delay(600, cancellationToken); // 模拟成功结果 var result = new { Status = "Success", Message = "飞行模式关闭成功", Timestamp = DateTime.UtcNow, RuntimeCode = notification.RuntimeCode, FlightModeDisabled = true, WirelessEnabled = true }; return System.Text.Json.JsonSerializer.Serialize(result); } /// /// 更新节点状态 /// /// 节点ID /// 新状态 /// 更新任务 private async Task UpdateNodeStatusAsync(string nodeId, NodeExecutionStatus status) { // TODO: 实现节点状态更新逻辑 // 这里应该更新数据库中的节点状态 _logger.LogInformation("更新节点状态,节点ID: {NodeId}, 新状态: {Status}", nodeId, status); // 模拟异步操作 await Task.CompletedTask; } }