TaskCancelledDomainEventHandler.cs
1.7 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
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);
}
}
}
}