CreateOrUpdateRobotCommandHandler.cs
6.51 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using MassTransit;
using Microsoft.Extensions.Logging;
using Rcs.Application.Common;
using Rcs.Application.MessageBus.Commands;
using Rcs.Domain.Entities;
using Rcs.Domain.Repositories;
namespace Rcs.Infrastructure.MessageBus.Handlers.Commands;
/// <summary>
/// 创建机器人命令处理器
/// @author zzy - 修改为使用ManufacturerId和RobotTypeId
/// </summary>
public class CreateOrUpdateRobotCommandHandler : IConsumer<CreateOrUpdateRobotCommand>
{
private readonly ILogger<CreateOrUpdateRobotCommandHandler> _logger;
private readonly IRobotRepository _robotRepository;
private readonly IMapRepository _mapRepository;
public CreateOrUpdateRobotCommandHandler(
ILogger<CreateOrUpdateRobotCommandHandler> logger,
IRobotRepository robotRepository,
IMapRepository mapRepository)
{
_logger = logger;
_robotRepository = robotRepository;
_mapRepository = mapRepository;
}
public async Task Consume(ConsumeContext<CreateOrUpdateRobotCommand> context)
{
var command = context.Message;
try
{
Map? existingMap = null;
if (!string.IsNullOrWhiteSpace(command.CurrentMapCode))
{
existingMap = await _mapRepository.GetByMapCodeAsync(command.CurrentMapCode, context.CancellationToken);
if (existingMap == null)
{
throw new InvalidOperationException($"地图编码 {command.CurrentMapCode} 不存在");
}
}
if (!Guid.TryParse(command.RobotId, out Guid robotId))
{
// 新建
var existingCode = await _robotRepository.GetByRobotCodeAsync(command.RobotCode, context.CancellationToken);
if (existingCode != null)
{
throw new InvalidOperationException($"机器人编码 {command.RobotCode} 已存在");
}
var existingBySerial = await _robotRepository.GetBySerialNumberAsync(command.RobotSerialNumber, context.CancellationToken);
if (existingBySerial != null)
{
throw new InvalidOperationException($"机器人序列号 {command.RobotSerialNumber} 已存在");
}
var robot = new Robot
{
RobotId = Guid.NewGuid(),
RobotCode = command.RobotCode,
RobotName = command.RobotName,
RobotVersion = command.RobotVersion,
ProtocolName = command.ProtocolName,
ProtocolVersion = command.ProtocolVersion,
ProtocolType = (ProtocolType)command.ProtocolType,
RobotManufacturer = command.RobotManufacturer,
RobotSerialNumber = command.RobotSerialNumber,
RobotType = (RobotType)command.RobotType,
IpAddress = command.IpAddress,
NetPort = command.NetPort,
MacAddress = command.MacAddress,
CoordinateScale = command.CoordinateScale,
PayloadCapacity = command.PayloadCapacity,
SpeedMax = command.SpeedMax,
SpeedMin = command.SpeedMin,
AccelerationMax = command.AccelerationMax,
DecelerationMax = command.DecelerationMax,
HeightMin = command.HeightMin,
HeightMax = command.HeightMax,
Width = command.Width,
Length = command.Length,
IsOmnidirectional = command.IsOmnidirectional,
Radius = command.Radius,
CurrentMapCodeId = existingMap?.MapId,
Active = command.Active,
MotionCenterToEdge = command.MotionCenterToEdge,
RobotModel = command.RobotModel,
Status = RobotStatus.Idle,
Online = OnlineStatus.Offline,
OperatingMode = OperatingMode.Automatic,
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now
};
await _robotRepository.AddAsync(robot, context.CancellationToken);
}
else
{
// 更新
var robot = await _robotRepository.GetByIdAsync(robotId, context.CancellationToken);
if (robot == null)
{
throw new InvalidOperationException($"未找到机器人ID为 {robotId} 的机器人");
}
robot.RobotCode = command.RobotCode;
robot.RobotName = command.RobotName;
robot.RobotVersion = command.RobotVersion;
robot.ProtocolName = command.ProtocolName;
robot.ProtocolVersion = command.ProtocolVersion;
robot.ProtocolType = (ProtocolType)command.ProtocolType;
robot.RobotManufacturer = command.RobotManufacturer;
robot.RobotSerialNumber = command.RobotSerialNumber;
robot.RobotType = (RobotType)command.RobotType;
robot.IpAddress = command.IpAddress;
robot.NetPort = command.NetPort;
robot.MacAddress = command.MacAddress;
robot.CoordinateScale = command.CoordinateScale;
robot.PayloadCapacity = command.PayloadCapacity;
robot.SpeedMax = command.SpeedMax;
robot.SpeedMin = command.SpeedMin;
robot.AccelerationMax = command.AccelerationMax;
robot.DecelerationMax = command.DecelerationMax;
robot.HeightMin = command.HeightMin;
robot.HeightMax = command.HeightMax;
robot.Width = command.Width;
robot.Length = command.Length;
robot.IsOmnidirectional = command.IsOmnidirectional;
robot.Radius = command.Radius;
robot.CurrentMapCodeId = existingMap?.MapId;
robot.Active = command.Active;
robot.MotionCenterToEdge = command.MotionCenterToEdge;
robot.RobotModel = command.RobotModel;
robot.UpdatedAt = DateTime.Now;
await _robotRepository.UpdateAsync(robot, context.CancellationToken);
}
await context.RespondAsync(ApiResponse.Successful());
}
catch (Exception ex)
{
await context.RespondAsync(ApiResponse.Failed(ex.Message));
}
}
}