ResetRobotCommandHandler.cs
2.06 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
51
52
53
54
55
56
57
58
59
using MassTransit;
using Microsoft.Extensions.Logging;
using Rcs.Application.Common;
using Rcs.Application.MessageBus.Commands;
using Rcs.Cyaninetech.Services;
using Rcs.Domain.Extensions;
using Rcs.Domain.Repositories;
namespace Rcs.Infrastructure.MessageBus.Handlers.Commands;
/// <summary>
/// 复位机器人命令处理器
/// @author zzy
/// </summary>
public class ResetRobotCommandHandler : IConsumer<ResetRobotCommand>
{
private readonly ILogger<ResetRobotCommandHandler> _logger;
private readonly IRobotRepository _robotRepository;
private readonly ILanYinService _lanYinService;
public ResetRobotCommandHandler(
ILogger<ResetRobotCommandHandler> logger,
IRobotRepository robotRepository,
ILanYinService lanYinService)
{
_logger = logger;
_robotRepository = robotRepository;
_lanYinService = lanYinService;
}
public async Task Consume(ConsumeContext<ResetRobotCommand> context)
{
var command = context.Message;
try
{
var robot = await _robotRepository.GetByIdAsync(command.RobotId, context.CancellationToken);
if (robot == null)
{
throw new BusinessException($"机器人ID {command.RobotId} 不存在");
}
var resReset = await _lanYinService.ResetRobotAsync(robot.RobotSerialNumber, context.CancellationToken);
if (!resReset.Success)
{
throw new BusinessException($"解除故障失败{resReset.Message}");
}
var resCE = await _lanYinService.ConfirmExceptionAsync(robot.RobotSerialNumber, context.CancellationToken);
if (!resCE.Success)
{
throw new BusinessException($"确认工况失败{resCE.Message}");
}
await context.RespondAsync(ApiResponse.Successful());
}
catch (Exception ex)
{
_logger.LogError(ex, "复位机器人失败: {RobotId}", command.RobotId);
await context.RespondAsync(ApiResponse.Failed(ex.Message));
}
}
}