forked from equinor/flotilla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMqttEventHandler.cs
435 lines (349 loc) · 21.2 KB
/
MqttEventHandler.cs
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
using Api.Controllers.Models;
using Api.Database.Models;
using Api.Mqtt;
using Api.Mqtt.Events;
using Api.Mqtt.MessageModels;
using Api.Services;
using Api.Services.ActionServices;
using Api.Services.Events;
using Api.Services.Models;
using Api.Utilities;
using Microsoft.EntityFrameworkCore;
namespace Api.EventHandlers
{
/// <summary>
/// A background service which listens to events and performs callback functions.
/// </summary>
public class MqttEventHandler : EventHandlerBase
{
private readonly ILogger<MqttEventHandler> _logger;
private readonly IServiceScopeFactory _scopeFactory;
private readonly Semaphore _updateRobotSemaphore = new(1, 1);
public MqttEventHandler(ILogger<MqttEventHandler> logger, IServiceScopeFactory scopeFactory)
{
_logger = logger;
// Reason for using factory: https://www.thecodebuzz.com/using-dbcontext-instance-in-ihostedservice/
_scopeFactory = scopeFactory;
Subscribe();
}
private IBatteryTimeseriesService BatteryTimeseriesService => _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<IBatteryTimeseriesService>();
private IInspectionService InspectionService => _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<IInspectionService>();
private IInstallationService InstallationService => _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<IInstallationService>();
private ILastMissionRunService LastMissionRunService => _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<ILastMissionRunService>();
private IMissionRunService MissionRunService => _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<IMissionRunService>();
private IMissionSchedulingService MissionScheduling => _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<IMissionSchedulingService>();
private IMissionTaskService MissionTaskService => _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<IMissionTaskService>();
private IPressureTimeseriesService PressureTimeseriesService => _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<IPressureTimeseriesService>();
private IRobotService RobotService => _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<IRobotService>();
private IPoseTimeseriesService PoseTimeseriesService => _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<IPoseTimeseriesService>();
private ISignalRService SignalRService => _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<ISignalRService>();
private ITaskDurationService TaskDurationService => _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<ITaskDurationService>();
private ITeamsMessageService TeamsMessageService => _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<ITeamsMessageService>();
private IEmergencyActionService EmergencyActionService => _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<IEmergencyActionService>();
public override void Subscribe()
{
MqttService.MqttIsarStatusReceived += OnIsarStatus;
MqttService.MqttIsarRobotInfoReceived += OnIsarRobotInfo;
MqttService.MqttIsarMissionReceived += OnIsarMissionUpdate;
MqttService.MqttIsarTaskReceived += OnIsarTaskUpdate;
MqttService.MqttIsarBatteryReceived += OnIsarBatteryUpdate;
MqttService.MqttIsarPressureReceived += OnIsarPressureUpdate;
MqttService.MqttIsarPoseReceived += OnIsarPoseUpdate;
MqttService.MqttIsarCloudHealthReceived += OnIsarCloudHealthUpdate;
}
public override void Unsubscribe()
{
MqttService.MqttIsarStatusReceived -= OnIsarStatus;
MqttService.MqttIsarRobotInfoReceived -= OnIsarRobotInfo;
MqttService.MqttIsarMissionReceived -= OnIsarMissionUpdate;
MqttService.MqttIsarTaskReceived -= OnIsarTaskUpdate;
MqttService.MqttIsarBatteryReceived -= OnIsarBatteryUpdate;
MqttService.MqttIsarPressureReceived -= OnIsarPressureUpdate;
MqttService.MqttIsarPoseReceived -= OnIsarPoseUpdate;
MqttService.MqttIsarCloudHealthReceived -= OnIsarCloudHealthUpdate;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { await stoppingToken; }
private async void OnIsarStatus(object? sender, MqttReceivedArgs mqttArgs)
{
var isarStatus = (IsarStatusMessage)mqttArgs.Message;
var robot = await RobotService.ReadByIsarId(isarStatus.IsarId, readOnly: true);
if (robot == null)
{
_logger.LogInformation("Received message from unknown ISAR instance {Id} with robot name {Name}", isarStatus.IsarId, isarStatus.RobotName);
return;
}
if (robot.Status == isarStatus.Status) { return; }
var preUpdatedRobot = await RobotService.ReadByIsarId(isarStatus.IsarId, readOnly: true);
if (preUpdatedRobot == null)
{
_logger.LogInformation("Received message from unknown ISAR instance {Id} with robot name {Name}", isarStatus.IsarId, isarStatus.RobotName);
return;
}
_logger.LogInformation("OnIsarStatus: Robot {robotName} has status {robotStatus} and current inspection area {areaName}", preUpdatedRobot.Name, preUpdatedRobot.Status, preUpdatedRobot.CurrentInspectionArea?.Name);
_updateRobotSemaphore.WaitOne();
_logger.LogDebug("Semaphore acquired for updating robot status");
await RobotService.UpdateRobotStatus(robot.Id, isarStatus.Status);
robot.Status = isarStatus.Status;
_updateRobotSemaphore.Release();
_logger.LogDebug("Semaphore released after updating robot status");
_logger.LogInformation("Updated status for robot {Name} to {Status}", robot.Name, robot.Status);
_logger.LogInformation("OnIsarStatus: Robot {robotName} has status {robotStatus} and current inspection area {areaName}", robot.Name, robot.Status, robot.CurrentInspectionArea?.Name);
if (isarStatus.Status == RobotStatus.Available)
{
try
{
_updateRobotSemaphore.WaitOne();
_logger.LogDebug("Semaphore acquired for updating robot current mission id");
await RobotService.UpdateCurrentMissionId(robot.Id, null);
}
catch (RobotNotFoundException)
{
_logger.LogError("Robot {robotName} not found when updating current mission id to null", robot.Name);
return;
}
finally
{
_updateRobotSemaphore.Release();
_logger.LogDebug("Semaphore released after updating robot current mission id");
}
await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(robot);
}
}
private async void CreateRobot(IsarRobotInfoMessage isarRobotInfo, Installation installation)
{
_logger.LogInformation(
"Received message from new ISAR instance '{Id}' with robot name '{Name}'. Adding new robot to database",
isarRobotInfo.IsarId, isarRobotInfo.RobotName);
var robotQuery = new CreateRobotQuery
{
IsarId = isarRobotInfo.IsarId,
Name = isarRobotInfo.RobotName,
RobotType = isarRobotInfo.RobotType,
SerialNumber = isarRobotInfo.SerialNumber,
CurrentInstallationCode = installation.InstallationCode,
Documentation = isarRobotInfo.DocumentationQueries,
Host = isarRobotInfo.Host,
Port = isarRobotInfo.Port,
RobotCapabilities = isarRobotInfo.Capabilities,
Status = RobotStatus.Available,
};
try
{
var newRobot = await RobotService.CreateFromQuery(robotQuery);
_logger.LogInformation("Added robot '{RobotName}' with ISAR id '{IsarId}' to database", newRobot.Name, newRobot.IsarId);
}
catch (DbUpdateException)
{
_logger.LogError($"Failed to add robot {robotQuery.Name} with to the database");
return;
}
}
private async void OnIsarRobotInfo(object? sender, MqttReceivedArgs mqttArgs)
{
var isarRobotInfo = (IsarRobotInfoMessage)mqttArgs.Message;
var installation = await InstallationService.ReadByInstallationCode(isarRobotInfo.CurrentInstallation, readOnly: true);
if (installation is null)
{
_logger.LogError(
new InstallationNotFoundException($"No installation with code {isarRobotInfo.CurrentInstallation} found"),
"Could not create new robot due to missing installation"
);
return;
}
try
{
var robot = await RobotService.ReadByIsarId(isarRobotInfo.IsarId, readOnly: false);
if (robot == null)
{
CreateRobot(isarRobotInfo, installation);
return;
}
try
{
_updateRobotSemaphore.WaitOne();
_logger.LogDebug("Semaphore acquired for updating robot");
List<string> updatedFields = [];
if (isarRobotInfo.Host is not null) UpdateHostIfChanged(isarRobotInfo.Host, ref robot, ref updatedFields);
UpdatePortIfChanged(isarRobotInfo.Port, ref robot, ref updatedFields);
if (isarRobotInfo.CurrentInstallation is not null) UpdateCurrentInstallationIfChanged(installation, ref robot, ref updatedFields);
if (isarRobotInfo.Capabilities is not null) UpdateRobotCapabilitiesIfChanged(isarRobotInfo.Capabilities, ref robot, ref updatedFields);
if (updatedFields.Count < 1) return;
await RobotService.Update(robot);
_logger.LogInformation("Updated robot '{Id}' ('{RobotName}') in database: {Updates}", robot.Id, robot.Name, updatedFields);
}
finally
{
_updateRobotSemaphore.Release();
_logger.LogDebug("Semaphore released after updating robot");
}
}
catch (DbUpdateException e) { _logger.LogError(e, "Could not add robot to db"); }
catch (Exception e) { _logger.LogError(e, "Could not update robot in db"); }
}
private static void UpdateHostIfChanged(string host, ref Robot robot, ref List<string> updatedFields)
{
if (host.Equals(robot.Host, StringComparison.Ordinal)) return;
updatedFields.Add($"\nHost ({robot.Host} -> {host})\n");
robot.Host = host;
}
private static void UpdatePortIfChanged(int port, ref Robot robot, ref List<string> updatedFields)
{
if (port.Equals(robot.Port)) return;
updatedFields.Add($"\nPort ({robot.Port} -> {port})\n");
robot.Port = port;
}
private static void UpdateCurrentInstallationIfChanged(Installation newCurrentInstallation, ref Robot robot, ref List<string> updatedFields)
{
if (newCurrentInstallation.InstallationCode.Equals(robot.CurrentInstallation?.InstallationCode, StringComparison.Ordinal)) return;
updatedFields.Add($"\nCurrentInstallation ({robot.CurrentInstallation} -> {newCurrentInstallation})\n");
robot.CurrentInstallation = newCurrentInstallation;
}
public static void UpdateRobotCapabilitiesIfChanged(IList<RobotCapabilitiesEnum> newRobotCapabilities, ref Robot robot, ref List<string> updatedFields)
{
if (robot.RobotCapabilities != null && Enumerable.SequenceEqual(newRobotCapabilities, robot.RobotCapabilities)) return;
updatedFields.Add($"\nRobotCapabilities ({robot.RobotCapabilities} -> {newRobotCapabilities})\n");
robot.RobotCapabilities = newRobotCapabilities;
}
private async void OnIsarMissionUpdate(object? sender, MqttReceivedArgs mqttArgs)
{
var isarMission = (IsarMissionMessage)mqttArgs.Message;
MissionStatus status;
try { status = MissionRun.GetMissionStatusFromString(isarMission.Status); }
catch (ArgumentException e)
{
_logger.LogError(e, "Failed to parse mission status from MQTT message. Mission with ISARMissionId '{IsarMissionId}' was not updated", isarMission.MissionId);
return;
}
var flotillaMissionRun = await MissionRunService.ReadByIsarMissionId(isarMission.MissionId, readOnly: true);
if (flotillaMissionRun is null)
{
string errorMessage = $"Mission with isar mission Id {isarMission.IsarId} was not found";
_logger.LogError("{Message}", errorMessage);
return;
}
if (flotillaMissionRun.Status == status) { return; }
if (flotillaMissionRun.Status == MissionStatus.Aborted && status == MissionStatus.Cancelled) { status = MissionStatus.Aborted; }
MissionRun updatedFlotillaMissionRun;
try { updatedFlotillaMissionRun = await MissionRunService.UpdateMissionRunStatusByIsarMissionId(isarMission.MissionId, status); }
catch (MissionRunNotFoundException) { return; }
_logger.LogInformation(
"Mission '{Id}' (ISARMissionID='{IsarMissionId}') status updated to '{Status}' for robot '{RobotName}' with ISAR id '{IsarId}'",
updatedFlotillaMissionRun.Id, isarMission.MissionId, isarMission.Status, isarMission.RobotName, isarMission.IsarId
);
if (!updatedFlotillaMissionRun.IsCompleted) return;
var robot = await RobotService.ReadByIsarId(isarMission.IsarId, readOnly: true);
if (robot is null)
{
_logger.LogError("Could not find robot '{RobotName}' with ISAR id '{IsarId}'", isarMission.RobotName, isarMission.IsarId);
return;
}
_logger.LogInformation("Robot '{Id}' ('{Name}') - completed mission run {MissionRunId}", robot.IsarId, robot.Name, updatedFlotillaMissionRun.Id);
if (updatedFlotillaMissionRun.MissionId == null)
{
_logger.LogInformation("Mission run {missionRunId} does not have a mission definition assosiated with it", updatedFlotillaMissionRun.Id);
return;
}
try { await LastMissionRunService.SetLastMissionRun(updatedFlotillaMissionRun.Id, updatedFlotillaMissionRun.MissionId); }
catch (MissionNotFoundException)
{
_logger.LogError("Mission not found when setting last mission run for mission definition {missionId}", updatedFlotillaMissionRun.MissionId);
return;
}
await TaskDurationService.UpdateAverageDurationPerTask(robot.Model.Type);
}
private async void OnIsarTaskUpdate(object? sender, MqttReceivedArgs mqttArgs)
{
var task = (IsarTaskMessage)mqttArgs.Message;
IsarTaskStatus status;
try { status = IsarTask.StatusFromString(task.Status); }
catch (ArgumentException e)
{
_logger.LogError(e, "Failed to parse mission status from MQTT message. Mission '{Id}' was not updated", task.MissionId);
return;
}
try { await MissionTaskService.UpdateMissionTaskStatus(task.TaskId, status); }
catch (MissionTaskNotFoundException) { return; }
if (task.GetMissionTaskTypeFromIsarTask(task.TaskType) == MissionTaskType.Inspection)
{
try { await InspectionService.UpdateInspectionStatus(task.TaskId, status); }
catch (InspectionNotFoundException) { return; }
}
var missionRun = await MissionRunService.ReadByIsarMissionId(task.MissionId, readOnly: true);
if (missionRun is null)
{
_logger.LogWarning("Mission run with ID {Id} was not found", task.MissionId);
}
_ = SignalRService.SendMessageAsync("Mission run updated", missionRun?.InspectionArea?.Installation, missionRun != null ? new MissionRunResponse(missionRun) : null);
_logger.LogInformation(
"Task '{Id}' updated to '{Status}' for robot '{RobotName}' with ISAR id '{IsarId}'", task.TaskId, task.Status, task.RobotName, task.IsarId);
}
private async void OnIsarBatteryUpdate(object? sender, MqttReceivedArgs mqttArgs)
{
var batteryStatus = (IsarBatteryMessage)mqttArgs.Message;
_updateRobotSemaphore.WaitOne();
_logger.LogDebug("Semaphore acquired for updating battery");
var robot = await BatteryTimeseriesService.AddBatteryEntry(batteryStatus.BatteryLevel, batteryStatus.IsarId);
if (robot != null && robot.BatteryState != batteryStatus.BatteryState)
{
await RobotService.UpdateRobotBatteryState(robot.Id, batteryStatus.BatteryState);
}
_updateRobotSemaphore.Release();
_logger.LogDebug("Semaphore released after updating battery");
if (robot == null) return;
robot.BatteryLevel = batteryStatus.BatteryLevel;
if (robot.FlotillaStatus == RobotFlotillaStatus.Normal && robot.IsRobotBatteryTooLow())
{
_logger.LogInformation("Sending robot '{RobotName}' to its dock as its battery level is too low.", robot.Name);
EmergencyActionService.SendRobotToDock(new RobotEmergencyEventArgs(robot.Id, RobotFlotillaStatus.Recharging));
}
else if (robot.FlotillaStatus == RobotFlotillaStatus.Recharging && robot.IsRobotReadyToStartMissions())
{
_logger.LogInformation("Releasing robot '{RobotName}' from its dock as its battery and pressure levels are good enough to run missions.", robot.Name);
EmergencyActionService.ReleaseRobotFromDock(new RobotEmergencyEventArgs(robot.Id, RobotFlotillaStatus.Normal));
}
}
private async void OnIsarPressureUpdate(object? sender, MqttReceivedArgs mqttArgs)
{
var pressureStatus = (IsarPressureMessage)mqttArgs.Message;
_updateRobotSemaphore.WaitOne();
_logger.LogDebug("Semaphore acquired for updating pressure");
var robot = await PressureTimeseriesService.AddPressureEntry(pressureStatus.PressureLevel, pressureStatus.IsarId);
_updateRobotSemaphore.Release();
_logger.LogDebug("Semaphore released after updating pressure");
if (robot == null) return;
robot.PressureLevel = pressureStatus.PressureLevel;
if (robot.FlotillaStatus == RobotFlotillaStatus.Normal && (robot.IsRobotPressureTooLow() || robot.IsRobotPressureTooHigh()))
{
_logger.LogInformation("Sending robot '{RobotName}' to its dock as its pressure is too low or high.", robot.Name);
EmergencyActionService.SendRobotToDock(new RobotEmergencyEventArgs(robot.Id, RobotFlotillaStatus.Recharging));
}
else if (robot.FlotillaStatus == RobotFlotillaStatus.Recharging && robot.IsRobotReadyToStartMissions())
{
_logger.LogInformation("Releasing robot '{RobotName}' from its dock as its battery and pressure levels are good enough to run missions.", robot.Name);
EmergencyActionService.ReleaseRobotFromDock(new RobotEmergencyEventArgs(robot.Id, RobotFlotillaStatus.Normal));
}
}
private async void OnIsarPoseUpdate(object? sender, MqttReceivedArgs mqttArgs)
{
var poseStatus = (IsarPoseMessage)mqttArgs.Message;
var pose = new Pose(poseStatus.Pose);
_updateRobotSemaphore.WaitOne();
_logger.LogDebug("Semaphore acquired for updating pose");
await PoseTimeseriesService.AddPoseEntry(pose, poseStatus.IsarId);
_updateRobotSemaphore.Release();
_logger.LogDebug("Semaphore released after updating pose");
}
private async void OnIsarCloudHealthUpdate(object? sender, MqttReceivedArgs mqttArgs)
{
var cloudHealthStatus = (IsarCloudHealthMessage)mqttArgs.Message;
var robot = await RobotService.ReadByIsarId(cloudHealthStatus.IsarId, readOnly: true);
if (robot == null)
{
_logger.LogInformation("Received message from unknown ISAR instance {Id} with robot name {Name}", cloudHealthStatus.IsarId, cloudHealthStatus.RobotName);
return;
}
string message = $"Failed telemetry request for robot {cloudHealthStatus.RobotName}.";
TeamsMessageService.TriggerTeamsMessageReceived(new TeamsMessageEventArgs(message));
}
}
}