RobotPauseDomainEventHandler.cs 1.91 KB
using Microsoft.Extensions.Logging;
using Rcs.Application.Services.Protocol;
using Rcs.Domain.Entities.DomainEvents.Robot;
using Rcs.Domain.Extensions;
using Rcs.Domain.Repositories;

namespace Rcs.Infrastructure.MessageBus.Handlers.Events.Robot
{
    /// <summary>
    /// 机器人暂停领域事件处理器
    /// 负责向机器人下发取消指令并更新任务状态
    /// 支持多协议类型(VDA5050、自定义协议等)
    /// @author zzy
    /// </summary>
    public class RobotPauseDomainEventHandler
    {
        private readonly ILogger<RobotPauseDomainEventHandler> _logger;
        private readonly IProtocolServiceFactory _protocolServiceFactory;
        private readonly IRobotRepository _robotRepository;

        public RobotPauseDomainEventHandler(
            ILogger<RobotPauseDomainEventHandler> logger,
            IProtocolServiceFactory protocolServiceFactory,
            IRobotRepository robotRepository,
            IRobotTaskRepository robotTaskRepository)
        {
            _logger = logger;
            _protocolServiceFactory = protocolServiceFactory;
            _robotRepository = robotRepository;
        }

        public async System.Threading.Tasks.Task Handle(RobotPauseDomainEvent domainEvent)
        {
            _logger.LogInformation(
                "RobotPauseDomainEvent - 机器人ID: {RobotId}, 序列号: {RobotSerialNumber}",
                domainEvent.RobotId,
                domainEvent.RobotSerialNumber);

            var robot = await _robotRepository.GetByIdAsync(domainEvent.RobotId);
            if (robot == null)
            {
                _logger.LogError("机器人不存在: {RobotId}", domainEvent.RobotId);
                throw new BusinessException($"机器人不存在: {domainEvent.RobotId}");
            }
            var protocolService = _protocolServiceFactory.GetService(robot);

            await protocolService.RobotPauseAsync(robot);
        }
    }
}