RobotPauseDomainEventHandler.cs
1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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);
}
}
}