TaskCancelledDomainEventHandler.cs 1.7 KB
using Microsoft.Extensions.Logging;
using Rcs.Application.Services.Protocol;
using Rcs.Domain.Entities.DomainEvents.RobotTask;
using Rcs.Domain.Repositories;

namespace Rcs.Infrastructure.MessageBus.Handlers.Events.RobotTask
{
    /// <summary>
    /// 任务取消领域事件处理器
    /// @author zzy
    /// </summary>
    public class TaskCancelledDomainEventHandler
    {
        private readonly ILogger<TaskCancelledDomainEventHandler> _logger;
        private readonly IProtocolServiceFactory _protocolServiceFactory;
        private readonly IRobotTaskRepository _robotTaskRepository;
        private readonly IRobotRepository _robotRepository;
        public TaskCancelledDomainEventHandler(
            IProtocolServiceFactory protocolServiceFactory,
            IRobotTaskRepository robotTaskRepository,
            IRobotRepository robotRepository,
            ILogger<TaskCancelledDomainEventHandler> logger)
        {
            _logger = logger;
            _protocolServiceFactory = protocolServiceFactory;
            _robotTaskRepository = robotTaskRepository;
            _robotRepository = robotRepository;
        }

        public async System.Threading.Tasks.Task Handle(TaskCancelledDomainEvent domainEvent)
        {
            if (domainEvent.RobotId.HasValue)
            {
                var robot = await _robotRepository.GetByIdAsync(domainEvent.RobotId);
                if (robot == null) return;
                // 根据协议类型获取对应的协议服务
                var protocolService = _protocolServiceFactory.GetService(robot);

                // 取消机器人任务
                await protocolService.CancelRobotTasksAsync(robot);
            }
            
        }
    }
}