Skip to content

Commit

Permalink
Send SignalR updates on changes to task and step
Browse files Browse the repository at this point in the history
  • Loading branch information
aeshub committed Nov 10, 2023
1 parent fc3f891 commit 304a45a
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 19 deletions.
22 changes: 15 additions & 7 deletions backend/api/EventHandlers/MqttEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,10 @@ private async void OnMissionUpdate(object? sender, MqttReceivedArgs mqttArgs)
return;
}

MissionRun? flotillaMissionRun;
MissionRun flotillaMissionRun;
try { flotillaMissionRun = await missionRunService.UpdateMissionRunStatusByIsarMissionId(isarMission.MissionId, status); }
catch (MissionRunNotFoundException) { return; }

if (flotillaMissionRun is null)
{
_logger.LogError("No mission found with ISARMissionId '{IsarMissionId}'. Could not update status to '{Status}'", isarMission.MissionId, status);
return;
}

_logger.LogInformation(
"Mission '{Id}' (ISARMissionID='{IsarMissionId}') status updated to '{Status}' for robot '{RobotName}' with ISAR id '{IsarId}'",
flotillaMissionRun.Id, isarMission.MissionId, isarMission.Status, isarMission.RobotName, isarMission.IsarId
Expand Down Expand Up @@ -245,7 +239,9 @@ private async void OnMissionUpdate(object? sender, MqttReceivedArgs mqttArgs)
private async void OnTaskUpdate(object? sender, MqttReceivedArgs mqttArgs)
{
var provider = GetServiceProvider();
var missionRunService = provider.GetRequiredService<IMissionRunService>();
var missionTaskService = provider.GetRequiredService<IMissionTaskService>();
var signalRService = provider.GetRequiredService<ISignalRService>();
var task = (IsarTaskMessage)mqttArgs.Message;

IsarTaskStatus status;
Expand All @@ -259,14 +255,21 @@ private async void OnTaskUpdate(object? sender, MqttReceivedArgs mqttArgs)
try { await missionTaskService.UpdateMissionTaskStatus(task.TaskId, status); }
catch (MissionTaskNotFoundException) { return; }

var missionRun = await missionRunService.ReadByIsarMissionId(task.MissionId);
if (missionRun is null) { _logger.LogWarning("Mission run with ID {Id} was not found", task.MissionId); }

_ = signalRService.SendMessageAsync("Mission run updated", missionRun);

_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 OnStepUpdate(object? sender, MqttReceivedArgs mqttArgs)
{
var provider = GetServiceProvider();
var missionRunService = provider.GetRequiredService<IMissionRunService>();
var inspectionService = provider.GetRequiredService<IInspectionService>();
var signalRService = provider.GetRequiredService<ISignalRService>();

var step = (IsarStepMessage)mqttArgs.Message;

Expand All @@ -285,6 +288,11 @@ private async void OnStepUpdate(object? sender, MqttReceivedArgs mqttArgs)
try { await inspectionService.UpdateInspectionStatus(step.StepId, status); }
catch (InspectionNotFoundException) { return; }

var missionRun = await missionRunService.ReadByIsarMissionId(step.MissionId);
if (missionRun is null) { _logger.LogWarning("Mission run with ID {Id} was not found", step.MissionId); }

_ = signalRService.SendMessageAsync("Mission run updated", missionRun);

_logger.LogInformation(
"Inspection '{Id}' updated to '{Status}' for robot '{RobotName}' with ISAR id '{IsarId}'", step.StepId, step.Status, step.RobotName, step.IsarId);
}
Expand Down
11 changes: 7 additions & 4 deletions backend/api/Services/InspectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ public class InspectionService : IInspectionService
{
private readonly FlotillaDbContext _context;
private readonly ILogger<InspectionService> _logger;
private readonly ISignalRService _signalRService;

public InspectionService(FlotillaDbContext context, ILogger<InspectionService> logger)
public InspectionService(FlotillaDbContext context, ILogger<InspectionService> logger, ISignalRService signalRService)
{
_context = context;
_logger = logger;
_signalRService = signalRService;
}

public async Task<Inspection> UpdateInspectionStatus(string isarStepId, IsarStepStatus isarStepStatus)
Expand All @@ -38,7 +40,9 @@ public async Task<Inspection> UpdateInspectionStatus(string isarStepId, IsarStep
}

inspection.UpdateStatus(isarStepStatus);
return await Update(inspection);
inspection = await Update(inspection);
_ = _signalRService.SendMessageAsync("Inspection updated", inspection);
return inspection;
}

private async Task<Inspection> Update(Inspection inspection)
Expand All @@ -50,8 +54,7 @@ private async Task<Inspection> Update(Inspection inspection)

private async Task<Inspection?> ReadByIsarStepId(string id)
{
// TODO: Discuss nullable with someone
return await GetInspections().FirstOrDefaultAsync(inspection => inspection.IsarStepId!.Equals(id));
return await GetInspections().FirstOrDefaultAsync(inspection => inspection.IsarStepId != null && inspection.IsarStepId.Equals(id));
}

private IQueryable<Inspection> GetInspections()
Expand Down
3 changes: 1 addition & 2 deletions backend/api/Services/MissionDefinitionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,13 @@ await _customMissionService.GetMissionTasksFromSourceId(source.SourceId),

private IQueryable<MissionDefinition> GetMissionDefinitionsWithSubModels()
{
// TODO: Discuss warning here
return _context.MissionDefinitions
.Include(missionDefinition => missionDefinition.Area != null ? missionDefinition.Area.Deck : null)
.ThenInclude(deck => deck != null ? deck.Plant : null)
.ThenInclude(plant => plant != null ? plant.Installation : null)
.Include(missionDefinition => missionDefinition.Source)
.Include(missionDefinition => missionDefinition.LastSuccessfulRun)
.ThenInclude(missionRun => missionRun != null ? missionRun.Tasks : null)
.ThenInclude(missionRun => missionRun != null ? missionRun.Tasks : null)!
.ThenInclude(missionTask => missionTask.Inspections)
.ThenInclude(inspection => inspection.InspectionFindings);
}
Expand Down
10 changes: 6 additions & 4 deletions backend/api/Services/MissionRunService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ public interface IMissionRunService

public Task<MissionRun?> ReadById(string id);

public Task<MissionRun?> ReadByIsarMissionId(string isarMissionId);

public Task<MissionRun?> ReadNextScheduledRunByMissionId(string missionId);

public Task<MissionRun> Update(MissionRun mission);

public Task<MissionRun?> UpdateMissionRunStatusByIsarMissionId(
public Task<MissionRun> UpdateMissionRunStatusByIsarMissionId(
string isarMissionId,
MissionStatus missionStatus
);
Expand Down Expand Up @@ -310,7 +312,7 @@ MissionRunQueryStringParameters parameters

#region ISAR Specific methods

private async Task<MissionRun?> ReadByIsarMissionId(string isarMissionId)
public async Task<MissionRun?> ReadByIsarMissionId(string isarMissionId)
{
return await GetMissionRunsWithSubModels()
.FirstOrDefaultAsync(
Expand All @@ -319,7 +321,7 @@ MissionRunQueryStringParameters parameters
);
}

public async Task<MissionRun?> UpdateMissionRunStatusByIsarMissionId(string isarMissionId, MissionStatus missionStatus)
public async Task<MissionRun> UpdateMissionRunStatusByIsarMissionId(string isarMissionId, MissionStatus missionStatus)
{
var missionRun = await ReadByIsarMissionId(isarMissionId);
if (missionRun is null)
Expand All @@ -331,7 +333,7 @@ MissionRunQueryStringParameters parameters

missionRun.Status = missionStatus;

await Update(missionRun);
missionRun = await Update(missionRun);

if (missionRun.Status == MissionStatus.Failed) { _ = _signalRService.SendMessageAsync("Mission run failed", missionRun); }
return missionRun;
Expand Down
3 changes: 1 addition & 2 deletions backend/api/Services/MissionTaskService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ private async Task<MissionTask> Update(MissionTask missionTask)

private async Task<MissionTask?> ReadByIsarTaskId(string id)
{
// TODO: Discuss nullable with someone
return await GetMissionTasks().FirstOrDefaultAsync(missionTask => missionTask.IsarTaskId!.Equals(id));
return await GetMissionTasks().FirstOrDefaultAsync(missionTask => missionTask.IsarTaskId != null && missionTask.IsarTaskId.Equals(id));
}

private IQueryable<MissionTask> GetMissionTasks()
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/Contexts/SignalRContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,5 @@ export enum SignalREventLabels {
missionRunDeleted = 'Mission run deleted',
missionRunFailed = 'Mission run failed',
robotListUpdated = 'Robot list updated',
inspectionUpdated = 'Inspection updated',
}

0 comments on commit 304a45a

Please sign in to comment.