CreateOrUpdateChargingPileCommandHandler.cs
10.8 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
using MassTransit;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Rcs.Application.Common;
using Rcs.Application.MessageBus.Commands.ChargingPile;
using Rcs.Domain.Entities;
using Rcs.Domain.Repositories;
namespace Rcs.Infrastructure.MessageBus.Handlers.Commands.ChargingPile
{
/// <summary>
/// Create or update charging pile command handler.
/// </summary>
public class CreateOrUpdateChargingPileCommandHandler : IConsumer<CreateOrUpdateChargingPileCommand>
{
private readonly ILogger<CreateOrUpdateChargingPileCommandHandler> _logger;
private readonly IChargingPileRepository _repository;
private readonly IMapNodeRepository _mapNodeRepository;
private readonly IRobotRepository _robotRepository;
public CreateOrUpdateChargingPileCommandHandler(
ILogger<CreateOrUpdateChargingPileCommandHandler> logger,
IChargingPileRepository repository,
IMapNodeRepository mapNodeRepository,
IRobotRepository robotRepository)
{
_logger = logger;
_repository = repository;
_mapNodeRepository = mapNodeRepository;
_robotRepository = robotRepository;
}
public async Task Consume(ConsumeContext<CreateOrUpdateChargingPileCommand> context)
{
var command = context.Message;
try
{
ValidateCommand(command);
var supportedRobotModels = command.SupportedRobotModels
.Where(x => !string.IsNullOrWhiteSpace(x))
.Select(x => x.Trim())
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToList();
var boundRobotIds = command.BoundRobotIds
.Where(x => Guid.TryParse(x, out _))
.Select(Guid.Parse)
.Distinct()
.ToList();
Guid? currentChargingRobotId = null;
if (!string.IsNullOrWhiteSpace(command.CurrentChargingRobotId))
{
if (!Guid.TryParse(command.CurrentChargingRobotId, out var parsedCurrentChargingRobotId))
{
throw new InvalidOperationException("CurrentChargingRobotId format is invalid");
}
var currentChargingRobot = await _robotRepository.GetByIdAsync(parsedCurrentChargingRobotId, context.CancellationToken);
if (currentChargingRobot == null)
{
throw new InvalidOperationException($"Current charging robot not found: {parsedCurrentChargingRobotId}");
}
currentChargingRobotId = parsedCurrentChargingRobotId;
}
Guid? mapNodeId = null;
if (!string.IsNullOrWhiteSpace(command.MapNodeId))
{
if (!Guid.TryParse(command.MapNodeId, out var parsedMapNodeId))
{
throw new InvalidOperationException("MapNodeId format is invalid");
}
var mapNode = await _mapNodeRepository.GetByIdAsync(parsedMapNodeId, context.CancellationToken);
if (mapNode == null)
{
throw new InvalidOperationException($"Map node not found: {parsedMapNodeId}");
}
if (mapNode.Type != MapNodeTYPE.charger)
{
throw new InvalidOperationException($"Map node must be charger type: {parsedMapNodeId}");
}
mapNodeId = parsedMapNodeId;
}
if (!Guid.TryParse(command.PileId, out var pileId))
{
await EnsureUniqueFields(command, null, mapNodeId, context.CancellationToken);
var entity = new Domain.Entities.ChargingPile
{
PileId = Guid.NewGuid(),
PileCode = command.PileCode.Trim(),
PileName = command.PileName.Trim(),
IpAddress = command.IpAddress.Trim(),
Port = command.Port,
MinChargingMinutes = command.MinChargingMinutes,
FullChargeThreshold = command.FullChargeThreshold,
SupportedRobotModels = supportedRobotModels,
BoundRobotIds = boundRobotIds,
CurrentChargingRobotId = currentChargingRobotId,
MapNodeId = mapNodeId,
AutoStartThreshold = command.AutoStartThreshold,
ResumeThreshold = command.ResumeThreshold,
MaxChargingMinutes = command.MaxChargingMinutes,
QueueTimeoutMinutes = command.QueueTimeoutMinutes,
Priority = command.Priority,
AllowTaskInterrupt = command.AllowTaskInterrupt,
IsActive = command.IsActive,
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now,
};
await _repository.AddAsync(entity, context.CancellationToken);
await _repository.SaveChangesAsync(context.CancellationToken);
}
else
{
var entity = await _repository.GetByIdAsync(pileId, context.CancellationToken);
if (entity == null)
{
throw new InvalidOperationException($"Charging pile not found: {pileId}");
}
await EnsureUniqueFields(command, pileId, mapNodeId, context.CancellationToken);
entity.PileCode = command.PileCode.Trim();
entity.PileName = command.PileName.Trim();
entity.IpAddress = command.IpAddress.Trim();
entity.Port = command.Port;
entity.MinChargingMinutes = command.MinChargingMinutes;
entity.FullChargeThreshold = command.FullChargeThreshold;
entity.SupportedRobotModels = supportedRobotModels;
entity.BoundRobotIds = boundRobotIds;
entity.CurrentChargingRobotId = currentChargingRobotId;
entity.MapNodeId = mapNodeId;
entity.AutoStartThreshold = command.AutoStartThreshold;
entity.ResumeThreshold = command.ResumeThreshold;
entity.MaxChargingMinutes = command.MaxChargingMinutes;
entity.QueueTimeoutMinutes = command.QueueTimeoutMinutes;
entity.Priority = command.Priority;
entity.AllowTaskInterrupt = command.AllowTaskInterrupt;
entity.IsActive = command.IsActive;
entity.UpdatedAt = DateTime.Now;
await _repository.UpdateAsync(entity, context.CancellationToken);
await _repository.SaveChangesAsync(context.CancellationToken);
}
await context.RespondAsync(ApiResponse.Successful());
}
catch (Exception ex)
{
_logger.LogError(ex, "Create or update charging pile failed");
await context.RespondAsync(ApiResponse.Failed(ex.Message));
}
}
private static void ValidateCommand(CreateOrUpdateChargingPileCommand command)
{
if (string.IsNullOrWhiteSpace(command.PileCode))
{
throw new InvalidOperationException("PileCode is required");
}
if (string.IsNullOrWhiteSpace(command.PileName))
{
throw new InvalidOperationException("PileName is required");
}
if (string.IsNullOrWhiteSpace(command.IpAddress))
{
throw new InvalidOperationException("IpAddress is required");
}
if (command.Port <= 0 || command.Port > 65535)
{
throw new InvalidOperationException("Port must be between 1 and 65535");
}
if (command.MinChargingMinutes < 0)
{
throw new InvalidOperationException("MinChargingMinutes cannot be negative");
}
if (command.MaxChargingMinutes < command.MinChargingMinutes)
{
throw new InvalidOperationException("MaxChargingMinutes cannot be less than MinChargingMinutes");
}
if (command.FullChargeThreshold is < 0 or > 100)
{
throw new InvalidOperationException("FullChargeThreshold must be between 0 and 100");
}
if (command.AutoStartThreshold is < 0 or > 100)
{
throw new InvalidOperationException("AutoStartThreshold must be between 0 and 100");
}
if (command.ResumeThreshold is < 0 or > 100)
{
throw new InvalidOperationException("ResumeThreshold must be between 0 and 100");
}
if (command.AutoStartThreshold > command.ResumeThreshold)
{
throw new InvalidOperationException("AutoStartThreshold cannot be greater than ResumeThreshold");
}
if (command.QueueTimeoutMinutes < 0)
{
throw new InvalidOperationException("QueueTimeoutMinutes cannot be negative");
}
}
private async Task EnsureUniqueFields(
CreateOrUpdateChargingPileCommand command,
Guid? currentId,
Guid? mapNodeId,
CancellationToken cancellationToken)
{
var byCode = await _repository.GetByPileCodeAsync(command.PileCode.Trim(), cancellationToken);
if (byCode != null && byCode.PileId != currentId)
{
throw new InvalidOperationException($"PileCode already exists: {command.PileCode}");
}
var byIpPort = await _repository.GetByIpAndPortAsync(command.IpAddress.Trim(), command.Port, cancellationToken);
if (byIpPort != null && byIpPort.PileId != currentId)
{
throw new InvalidOperationException($"IP and Port already exists: {command.IpAddress}:{command.Port}");
}
if (mapNodeId.HasValue)
{
var byMapNode = await _repository.GetQueryable()
.FirstOrDefaultAsync(p => p.MapNodeId == mapNodeId.Value, cancellationToken);
if (byMapNode != null && byMapNode.PileId != currentId)
{
throw new InvalidOperationException($"MapNodeId already bound by another charging pile: {mapNodeId.Value}");
}
}
}
}
}