Skip to content

Commit

Permalink
Prevent using outdated robot during mission check
Browse files Browse the repository at this point in the history
  • Loading branch information
andchiind committed Dec 16, 2024
1 parent c496dec commit 0060030
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 18 deletions.
12 changes: 7 additions & 5 deletions backend/api/EventHandlers/MissionEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private async void OnMissionRunCreated(object? sender, MissionRunCreatedEventArg
await MissionScheduling.AbortActiveReturnToHomeMission(missionRun.Robot.Id);
}

try { await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(missionRun.Robot.Id); }
try { await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(missionRun.Robot); }
catch (MissionRunNotFoundException) { return; }
finally { _startMissionSemaphore.Release(); }
}
Expand All @@ -90,7 +90,7 @@ private async void OnRobotAvailable(object? sender, RobotAvailableEventArgs e)
}

_startMissionSemaphore.WaitOne();
try { await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(robot.Id); }
try { await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(robot); }
catch (MissionRunNotFoundException) { return; }
finally { _startMissionSemaphore.Release(); }
}
Expand Down Expand Up @@ -153,7 +153,7 @@ private async void OnSendRobotToDockTriggered(object? sender, RobotEmergencyEven
}

_startMissionSemaphore.WaitOne();
try { await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(robot.Id); }
try { await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(robot); }
catch (MissionRunNotFoundException) { return; }
finally { _startMissionSemaphore.Release(); }
}
Expand All @@ -174,9 +174,11 @@ private async void OnReleaseRobotFromDockTriggered(object? sender, RobotEmergenc
return;
}

try { await MissionScheduling.UnfreezeMissionRunQueueForRobot(e.RobotId); }
try { await MissionScheduling.UnfreezeMissionRunQueueForRobot(robot.Id); }
catch (RobotNotFoundException) { return; }

robot.MissionQueueFrozen = false;

try { await RobotService.UpdateFlotillaStatus(e.RobotId, e.RobotFlotillaStatus ?? RobotFlotillaStatus.Normal); }
catch (Exception ex)
{
Expand All @@ -185,7 +187,7 @@ private async void OnReleaseRobotFromDockTriggered(object? sender, RobotEmergenc
}

_startMissionSemaphore.WaitOne();
try { await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(robot.Id); }
try { await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(robot); }
catch (MissionRunNotFoundException) { return; }
finally { _startMissionSemaphore.Release(); }
}
Expand Down
2 changes: 1 addition & 1 deletion backend/api/EventHandlers/MqttEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private async void OnIsarStatus(object? sender, MqttReceivedArgs mqttArgs)
_updateRobotSemaphore.Release();
_logger.LogDebug("Semaphore released after updating robot current mission id");
}
await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(robot.Id);
await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(robot);
}
}

Expand Down
7 changes: 7 additions & 0 deletions backend/api/Services/MissionRunService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,13 @@ private IQueryable<MissionRun> GetMissionRunsWithSubModels(bool readOnly = true)
.ThenInclude(defaultLocalizationPose => defaultLocalizationPose != null ? defaultLocalizationPose.Pose : null)
.Include(missionRun => missionRun.Robot)
.Include(missionRun => missionRun.Robot)
.ThenInclude(robot => robot.CurrentInspectionArea)
.ThenInclude(a => a != null ? a.Installation : null)
.Include(missionRun => missionRun.Robot)
.ThenInclude(robot => robot.CurrentInspectionArea)
.ThenInclude(a => a != null ? a.Plant : null)
.ThenInclude(p => p != null ? p.Installation : null)
.Include(missionRun => missionRun.Robot)
.ThenInclude(robot => robot.Model)
.Include(missionRun => missionRun.Tasks)
.ThenInclude(task => task.Inspection)
Expand Down
14 changes: 3 additions & 11 deletions backend/api/Services/MissionSchedulingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Api.Services
{
public interface IMissionSchedulingService
{
public Task StartNextMissionRunIfSystemIsAvailable(string robotId);
public Task StartNextMissionRunIfSystemIsAvailable(Robot robot);

public Task<bool> OngoingMission(string robotId);

Expand All @@ -33,23 +33,15 @@ public interface IMissionSchedulingService
public class MissionSchedulingService(ILogger<MissionSchedulingService> logger, IMissionRunService missionRunService, IRobotService robotService,
IIsarService isarService, ILocalizationService localizationService, IReturnToHomeService returnToHomeService, ISignalRService signalRService, IErrorHandlingService errorHandlingService) : IMissionSchedulingService
{
public async Task StartNextMissionRunIfSystemIsAvailable(string robotId)
public async Task StartNextMissionRunIfSystemIsAvailable(Robot robot)
{
logger.LogInformation("Starting next mission run if system is available for robot ID: {RobotId}", robotId);
var robot = await robotService.ReadById(robotId, readOnly: true);
if (robot == null)
{
logger.LogError("Robot with ID: {RobotId} was not found in the database", robotId);
return;
}

logger.LogInformation("Robot {robotName} has status {robotStatus} and current area {areaName}", robot.Name, robot.Status, robot.CurrentInspectionArea?.Name);

MissionRun? missionRun;
try { missionRun = await SelectNextMissionRun(robot.Id); }
catch (RobotNotFoundException)
{
logger.LogError("Robot with ID: {RobotId} was not found in the database", robotId);
logger.LogError("Robot with ID: {RobotId} was not found in the database", robot.Id);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion backend/api/Services/ReturnToHomeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ReturnToHomeService(ILogger<ReturnToHomeService> logger, IRobotServ
logger.LogInformation("Scheduling return to home mission if not already scheduled or the robot is home for robot {RobotId}", robotId);
var lastMissionRun = await missionRunService.ReadLastExecutedMissionRunByRobot(robotId);

if (await IsReturnToHomeMissionAlreadyScheduled(robotId) || (lastMissionRun != null && lastMissionRun.IsReturnHomeMission()))
if (await IsReturnToHomeMissionAlreadyScheduled(robotId) || (lastMissionRun != null && (lastMissionRun.IsReturnHomeMission() || lastMissionRun.IsEmergencyMission())))
{
logger.LogInformation("ReturnToHomeMission is already scheduled for Robot {RobotId}", robotId);
return null;
Expand Down

0 comments on commit 0060030

Please sign in to comment.