CreateOrUpdateRobotCommandHandler.cs
11.5 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using MassTransit;
using Microsoft.Extensions.Logging;
using Rcs.Application.Common;
using Rcs.Application.MessageBus.Commands;
using Rcs.Domain.Entities;
using Rcs.Domain.Repositories;
using Rcs.Domain.ValueObjects;
using Rcs.Shared.Utils;
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,
MovementType = (MovementType)command.MovementType,
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,
ForkRadOffset = command.ForkAngleOffset.Select(fa => (double)AngleConverter.ToCycleRadians(fa)).ToList(),
CacheRows = command.CacheRows,
CacheColumns = command.CacheColumns,
CacheLevels = command.CacheLevels
};
// 根据容量配置自动创建缓存储位并赋值给导航属性
robot.CacheLocations = CreateCacheLocationsList(robot);
await _robotRepository.AddAsync(robot, context.CancellationToken);
}
else
{
// 更新
var robot = await _robotRepository.GetByIdFullAsync(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.MovementType = (MovementType)command.MovementType;
robot.Radius = command.Radius;
robot.CurrentMapCodeId = existingMap?.MapId;
robot.Active = command.Active;
robot.MotionCenterToEdge = command.MotionCenterToEdge;
robot.RobotModel = command.RobotModel;
robot.UpdatedAt = DateTime.Now;
robot.ForkRadOffset = command.ForkAngleOffset.Select(fa => (double)AngleConverter.ToCycleRadians(fa)).ToList();
robot.CacheRows = command.CacheRows;
robot.CacheColumns = command.CacheColumns;
robot.CacheLevels = command.CacheLevels;
// 同步缓存储位:对比并更新集合
SyncCacheLocations(robot);
await _robotRepository.UpdateAsync(robot, context.CancellationToken);
}
await context.RespondAsync(ApiResponse.Successful());
}
catch (Exception ex)
{
await context.RespondAsync(ApiResponse.Failed(ex.Message));
}
}
/// <summary>
/// 根据Robot容量配置创建缓存储位列表
/// </summary>
private ICollection<RobotCacheLocation> CreateCacheLocationsList(Robot robot)
{
var locations = new List<RobotCacheLocation>();
if (!robot.CacheRows.HasValue || !robot.CacheColumns.HasValue || !robot.CacheLevels.HasValue)
return locations;
for (int row = 1; row <= robot.CacheRows.Value; row++)
{
for (int col = 1; col <= robot.CacheColumns.Value; col++)
{
for (int level = 1; level <= robot.CacheLevels.Value; level++)
{
var locationCode = $"{robot.RobotCode}-R{row:D2}C{col:D2}L{level:D2}";
var cacheLocation = RobotCacheLocation.Create(
robot.RobotId,
locationCode,
row,
col,
level,
$"自动生成的缓存储位 排{row}列{col}层{level}"
);
locations.Add(cacheLocation);
}
}
}
if (locations.Any())
{
_logger.LogInformation($"为Robot {robot.RobotCode} 创建了 {locations.Count} 个缓存储位");
}
return locations;
}
/// <summary>
/// 同步缓存储位:对比现有储位,处理新增和删除
/// </summary>
private void SyncCacheLocations(Robot robot)
{
// 如果没有配置容量,清空所有储位
if (!robot.CacheRows.HasValue || !robot.CacheColumns.HasValue || !robot.CacheLevels.HasValue)
{
if (robot.CacheLocations.Any())
{
robot.CacheLocations.Clear();
_logger.LogInformation($"清空Robot {robot.RobotCode} 的所有缓存储位(容量未配置)");
}
return;
}
// 生成期望的储位集合
var expectedLocations = new List<(int Row, int Column, int Level)>();
for (int row = 1; row <= robot.CacheRows.Value; row++)
{
for (int col = 1; col <= robot.CacheColumns.Value; col++)
{
for (int level = 1; level <= robot.CacheLevels.Value; level++)
{
expectedLocations.Add((row, col, level));
}
}
}
// 找出需要删除的储位(现有但不在期望中)
var locationsToDelete = robot.CacheLocations
.Where(existing => !expectedLocations.Any(expected =>
expected.Row == existing.Row &&
expected.Column == existing.Column &&
expected.Level == existing.Level))
.ToList();
// 找出需要新增的储位(期望中但不在现有中)
var locationsToAdd = expectedLocations
.Where(expected => !robot.CacheLocations.Any(existing =>
existing.Row == expected.Row &&
existing.Column == expected.Column &&
existing.Level == expected.Level))
.Select(pos =>
{
var locationCode = $"{robot.RobotCode}-R{pos.Row:D2}C{pos.Column:D2}L{pos.Level:D2}";
return RobotCacheLocation.Create(
robot.RobotId,
locationCode,
pos.Row,
pos.Column,
pos.Level,
$"自动生成的缓存储位 排{pos.Row}列{pos.Column}层{pos.Level}"
);
})
.ToList();
// 执行删除
foreach (var location in locationsToDelete)
{
robot.CacheLocations.Remove(location);
}
// 执行新增
foreach (var location in locationsToAdd)
{
robot.CacheLocations.Add(location);
}
if (locationsToDelete.Any() || locationsToAdd.Any())
{
_logger.LogInformation(
$"同步Robot {robot.RobotCode} 的缓存储位: 删除 {locationsToDelete.Count} 个, 新增 {locationsToAdd.Count} 个");
}
}
}