Skip to content

Commit

Permalink
Move new mission during return home to service
Browse files Browse the repository at this point in the history
  • Loading branch information
mrica-equinor committed May 23, 2024
1 parent 74ae22e commit a26c178
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 39 deletions.
41 changes: 2 additions & 39 deletions backend/api/Controllers/MissionSchedulingController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class MissionSchedulingController(
IMissionDefinitionService missionDefinitionService,
ICustomMissionSchedulingService customMissionSchedulingService,
IMissionRunService missionRunService,
IMissionSchedulingService missionSchedulingService,
IInstallationService installationService,
IRobotService robotService,
IEchoService echoService,
Expand Down Expand Up @@ -176,27 +175,6 @@ [FromBody] ScheduleMissionQuery scheduledMissionQuery
missionRun.CalculateEstimatedDuration();
}

IList<MissionStatus> missionStatuses = [MissionStatus.Ongoing, MissionStatus.Pending, MissionStatus.Paused];
var missionRunType = MissionRunType.ReturnHome;
var missionRuns = await missionRunService.ReadMissionRuns(robot.Id, missionRunType, missionStatuses);

if (missionRuns.Count != 0)
{
var returnToHomeMission = missionRuns[0];

if (!await localizationService.RobotIsOnSameDeckAsMission(robot.Id, missionRun.Area.Id))
{
return Conflict($"The robot {robot.Name} is localized on a different deck so the mission was not scheduled.");
}

try { await missionSchedulingService.StopCurrentMissionRun(robot.Id); }
catch (RobotNotFoundException e) { logger.LogError("{Message}", e); }
catch (MissionRunNotFoundException e) { logger.LogWarning("{Message}", e); }

returnToHomeMission.Status = MissionStatus.Cancelled;
await missionRunService.Update(returnToHomeMission);
}

MissionRun newMissionRun;
try
{
Expand Down Expand Up @@ -375,24 +353,9 @@ [FromBody] ScheduledMissionQuery scheduledMissionQuery
await missionDefinitionService.Create(scheduledMissionDefinition);
}

IList<MissionStatus> missionStatuses = [MissionStatus.Ongoing, MissionStatus.Pending, MissionStatus.Paused];
var missionRunType = MissionRunType.ReturnHome;
var missionRuns = await missionRunService.ReadMissionRuns(robot.Id, missionRunType, missionStatuses);

if (missionRuns.Count != 0)
if (await localizationService.RobotIsLocalized(missionRun.Robot.Id) && !await localizationService.RobotIsOnSameDeckAsMission(missionRun.Robot.Id, missionRun.Area.Id))
{
var returnToHomeMission = missionRuns[0];
if (!await localizationService.RobotIsOnSameDeckAsMission(robot.Id, missionRun.Area.Id))
{
return Conflict($"The robot {robot.Name} is localized on a different deck so the mission was not scheduled.");
}

try { await missionSchedulingService.StopCurrentMissionRun(robot.Id); }
catch (RobotNotFoundException e) { logger.LogError("{Message}", e); }
catch (MissionRunNotFoundException e) { logger.LogWarning("{Message}", e); }

returnToHomeMission.Status = MissionStatus.Cancelled;
await missionRunService.Update(returnToHomeMission);
return Conflict($"The robot {missionRun.Robot.Name} is localized on a different deck so the mission was not scheduled.");
}

MissionRun newMissionRun;
Expand Down
39 changes: 39 additions & 0 deletions backend/api/EventHandlers/MissionEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ or IsarCommunicationException
}
}

await CancellReturnToHomeOnNewMissionSchedule(missionRun);

_startMissionSemaphore.WaitOne();
try { await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(missionRun.Robot.Id); }
catch (MissionRunNotFoundException) { return; }
Expand Down Expand Up @@ -299,5 +301,42 @@ private async void OnEmergencyButtonDepressedForRobot(object? sender, EmergencyB

return area;
}

public async Task CancellReturnToHomeOnNewMissionSchedule(MissionRun missionRun)
{
IList<MissionStatus> missionStatuses = [MissionStatus.Ongoing, MissionStatus.Pending, MissionStatus.Paused];
var existingReturnToHomeMissions = await MissionService.ReadMissionRuns(missionRun.Robot.Id, MissionRunType.ReturnHome, missionStatuses);

if (existingReturnToHomeMissions.Count == 1 && existingReturnToHomeMissions[0].Id != missionRun.Id)
{
var returnToHomeMission = existingReturnToHomeMissions[0];

if (!await LocalizationService.RobotIsOnSameDeckAsMission(missionRun.Robot.Id, missionRun.Area.Id))
{
_logger.LogWarning($"The robot {missionRun.Robot.Name} is localized on a different deck so the mission was not scheduled.");
return;
}

if (returnToHomeMission.Status != MissionStatus.Pending)
{
try { await MissionScheduling.StopCurrentMissionRun(missionRun.Robot.Id); }
catch (RobotNotFoundException) { return; }
catch (MissionRunNotFoundException) { return; }
}

var missionTask = returnToHomeMission.Tasks.FirstOrDefault();
if (missionTask != null)
{
missionTask.Status = Database.Models.TaskStatus.Cancelled;
}
returnToHomeMission.Status = MissionStatus.Cancelled;
await MissionService.Update(returnToHomeMission);
}

if (existingReturnToHomeMissions.Count > 1)
{
_logger.LogError($"Two Return to Home missions should not be queued or ongoing simoultaneously for robot {missionRun.Robot.Name}.");
}
}
}
}

0 comments on commit a26c178

Please sign in to comment.