Skip to content

Commit

Permalink
Move create return home to starting mission
Browse files Browse the repository at this point in the history
  • Loading branch information
andchiind committed Jan 17, 2025
1 parent 8c24d5d commit 724aa78
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 118 deletions.
2 changes: 1 addition & 1 deletion backend/api/Controllers/ReturnToHomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ [FromRoute] string robotId
}

var returnToHomeMission =
await returnToHomeService.ScheduleReturnToHomeMissionRunIfNotAlreadyScheduledOrRobotIsHome(
await returnToHomeService.ScheduleReturnToHomeMissionRunIfNotAlreadyScheduled(
robot.Id
);
if (returnToHomeMission is null)
Expand Down
2 changes: 1 addition & 1 deletion backend/api/Controllers/RobotController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ public async Task<ActionResult> ResetRobot([FromRoute] string robotId)

try
{
await missionSchedulingService.AbortAllScheduledMissions(
await missionSchedulingService.AbortAllScheduledNormalMissions(
robot.Id,
"Aborted: Robot was reset"
);
Expand Down
33 changes: 8 additions & 25 deletions backend/api/Services/MissionRunService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,14 @@ public Task<PagedList<MissionRun>> ReadAll(

public Task<MissionRun?> ReadByIsarMissionId(string isarMissionId, bool readOnly = true);

public Task<IList<MissionRun>> ReadMissionRunQueue(string robotId, bool readOnly = true);
public Task<IList<MissionRun>> ReadMissionRunQueue(string robotId, MissionRunType type = MissionRunType.Normal, bool readOnly = true);

public Task<MissionRun?> ReadNextScheduledRunByMissionId(
string missionId,
bool readOnly = true
);

public Task<MissionRun?> ReadNextScheduledMissionRun(string robotId, bool readOnly = true);

public Task<MissionRun?> ReadNextScheduledEmergencyMissionRun(
string robotId,
bool readOnly = true
);
public Task<MissionRun?> ReadNextScheduledMissionRun(string robotId, MissionRunType type = MissionRunType.Normal, bool readOnly = true);

public Task<IList<MissionRun>> ReadMissionRuns(
string robotId,
Expand Down Expand Up @@ -188,40 +183,28 @@ public async Task<PagedList<MissionRun>> ReadAll(

public async Task<IList<MissionRun>> ReadMissionRunQueue(
string robotId,
MissionRunType type = MissionRunType.Normal,
bool readOnly = true
)
{
return await GetMissionRunsWithSubModels(readOnly: readOnly)
.Where(missionRun =>
missionRun.Robot.Id == robotId && missionRun.Status == MissionStatus.Pending
missionRun.Robot.Id == robotId && missionRun.Status == MissionStatus.Pending && missionRun.MissionRunType == type
)
.OrderBy(missionRun => missionRun.DesiredStartTime)
.ToListAsync();
}

public async Task<MissionRun?> ReadNextScheduledMissionRun(
string robotId,
MissionRunType type = MissionRunType.Normal,
bool readOnly = true
)
{
return await GetMissionRunsWithSubModels(readOnly: readOnly)
.OrderBy(missionRun => missionRun.DesiredStartTime)
.FirstOrDefaultAsync(missionRun =>
missionRun.Robot.Id == robotId && missionRun.Status == MissionStatus.Pending
);
}

public async Task<MissionRun?> ReadNextScheduledEmergencyMissionRun(
string robotId,
bool readOnly = true
)
{
return await GetMissionRunsWithSubModels(readOnly: readOnly)
.OrderBy(missionRun => missionRun.DesiredStartTime)
.FirstOrDefaultAsync(missionRun =>
missionRun.Robot.Id == robotId
&& missionRun.MissionRunType == MissionRunType.Emergency
&& missionRun.Status == MissionStatus.Pending
missionRun.Robot.Id == robotId && missionRun.Status == MissionStatus.Pending && missionRun.MissionRunType == type
);
}

Expand Down Expand Up @@ -273,8 +256,8 @@ public async Task<IList<MissionRun>> ReadMissionRuns(

public async Task<bool> PendingOrOngoingReturnToHomeMissionRunExists(string robotId)
{
var pendingMissionRuns = await ReadMissionRunQueue(robotId, readOnly: true);
if (pendingMissionRuns.Any((m) => m.IsReturnHomeMission()))
var pendingMissionRuns = await ReadNextScheduledMissionRun(robotId, type: MissionRunType.ReturnHome, readOnly: true);
if (pendingMissionRuns != null)
return true;

var ongoingMissionRuns = await GetMissionRunsWithSubModels(readOnly: true)
Expand Down
119 changes: 38 additions & 81 deletions backend/api/Services/MissionSchedulingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface IMissionSchedulingService

public Task StopCurrentMissionRun(string robotId);

public Task AbortAllScheduledMissions(string robotId, string? abortReason = null);
public Task AbortAllScheduledNormalMissions(string robotId, string? abortReason = null);

public Task ScheduleMissionToDriveToDockPosition(string robotId);

Expand Down Expand Up @@ -61,9 +61,11 @@ public async Task StartNextMissionRunIfSystemIsAvailable(Robot robot)
return;
}

if (missionRun == null)
return;

if (
robot.MissionQueueFrozen
&& missionRun != null
&& !(missionRun.IsEmergencyMission() || missionRun.IsReturnHomeMission())
)
{
Expand All @@ -74,44 +76,6 @@ public async Task StartNextMissionRunIfSystemIsAvailable(Robot robot)
return;
}

if (missionRun == null)
{
logger.LogInformation(
"The robot was ready to start mission, but no mission is scheduled"
);

if (
robot.RobotCapabilities != null
&& robot.RobotCapabilities.Contains(RobotCapabilitiesEnum.return_to_home)
)
{
try
{
missionRun =
await returnToHomeService.ScheduleReturnToHomeMissionRunIfNotAlreadyScheduledOrRobotIsHome(
robot.Id
);
}
catch (ReturnToHomeMissionFailedToScheduleException)
{
signalRService.ReportGeneralFailToSignalR(
robot,
$"Failed to schedule return home for robot {robot.Name}",
""
);
logger.LogError(
"Failed to schedule a return home mission for robot {RobotId}",
robot.Id
);
}
}
}

if (missionRun == null)
{
return;
}

if (!TheSystemIsAvailableToRunAMission(robot, missionRun))
{
logger.LogInformation(
Expand Down Expand Up @@ -151,7 +115,7 @@ await robotService.UpdateCurrentInspectionArea(
);
try
{
await AbortAllScheduledMissions(
await AbortAllScheduledNormalMissions(
robot.Id,
"Aborted: Robot was at different inspection area"
);
Expand All @@ -164,19 +128,6 @@ await AbortAllScheduledMissions(
);
}

try
{
await returnToHomeService.ScheduleReturnToHomeMissionRunIfNotAlreadyScheduledOrRobotIsHome(
robot.Id
);
}
catch (ReturnToHomeMissionFailedToScheduleException)
{
logger.LogError(
"Failed to schedule a return home mission for robot {RobotId}",
robot.Id
);
}
return;
}

Expand All @@ -185,11 +136,8 @@ await returnToHomeService.ScheduleReturnToHomeMissionRunIfNotAlreadyScheduledOrR
&& !(missionRun.IsReturnHomeMission() || missionRun.IsEmergencyMission())
)
{
missionRun = await HandleBatteryAndPressureLevel(robot);
if (missionRun == null)
{
return;
}
await HandleBatteryAndPressureLevel(robot);
return;
}

try
Expand Down Expand Up @@ -219,7 +167,7 @@ await missionRunService.SetMissionRunToFailed(
catch (RobotBusyException) { }
}

public async Task<MissionRun?> HandleBatteryAndPressureLevel(Robot robot)
public async Task HandleBatteryAndPressureLevel(Robot robot)
{
if (robot.IsRobotPressureTooLow())
{
Expand Down Expand Up @@ -248,7 +196,7 @@ await missionRunService.SetMissionRunToFailed(

try
{
await AbortAllScheduledMissions(
await AbortAllScheduledNormalMissions(
robot.Id,
"Aborted: Robot pressure or battery values are too low."
);
Expand All @@ -257,24 +205,6 @@ await AbortAllScheduledMissions(
{
logger.LogError("Failed to abort scheduled missions for robot {RobotId}", robot.Id);
}

MissionRun? missionRun;
try
{
missionRun =
await returnToHomeService.ScheduleReturnToHomeMissionRunIfNotAlreadyScheduledOrRobotIsHome(
robot.Id
);
}
catch (ReturnToHomeMissionFailedToScheduleException)
{
logger.LogError(
"Failed to schedule a return home mission for robot {RobotId}",
robot.Id
);
return null;
}
return missionRun;
}

public async Task<bool> OngoingMission(string robotId)
Expand Down Expand Up @@ -358,7 +288,7 @@ public async Task StopCurrentMissionRun(string robotId)
catch (RobotNotFoundException) { }
}

public async Task AbortAllScheduledMissions(string robotId, string? abortReason)
public async Task AbortAllScheduledNormalMissions(string robotId, string? abortReason)
{
var robot = await robotService.ReadById(robotId, readOnly: true);
if (robot == null)
Expand All @@ -370,6 +300,7 @@ public async Task AbortAllScheduledMissions(string robotId, string? abortReason)

var pendingMissionRuns = await missionRunService.ReadMissionRunQueue(
robotId,
type: MissionRunType.Normal,
readOnly: true
);
if (pendingMissionRuns is null)
Expand Down Expand Up @@ -472,17 +403,24 @@ public void TriggerRobotAvailable(RobotAvailableEventArgs e)

private async Task<MissionRun?> SelectNextMissionRun(Robot robot)
{
var missionRun = await missionRunService.ReadNextScheduledEmergencyMissionRun(
var missionRun = await missionRunService.ReadNextScheduledMissionRun(
robot.Id,
type: MissionRunType.Emergency,
readOnly: true
);
if (robot.MissionQueueFrozen == false && missionRun == null)
{
missionRun = await missionRunService.ReadNextScheduledMissionRun(
robot.Id,
type: MissionRunType.Normal,
readOnly: true
);
}
missionRun ??= await missionRunService.ReadNextScheduledMissionRun(
robot.Id,
type: MissionRunType.ReturnHome,
readOnly: true
);
return missionRun;
}

Expand Down Expand Up @@ -587,6 +525,25 @@ private async Task StartMissionRun(MissionRun queuedMissionRun, Robot robot)
throw new IsarCommunicationException(ErrorMessage);
}

try
{
await returnToHomeService.ScheduleReturnToHomeMissionRunIfNotAlreadyScheduled(
robot.Id
);
}
catch (ReturnToHomeMissionFailedToScheduleException)
{
signalRService.ReportGeneralFailToSignalR(
robot,
$"Failed to schedule return home for robot {robot.Name}",
""
);
logger.LogError(
"Failed to schedule a return home mission for robot {RobotId}",
robot.Id
);
}

await missionRunService.UpdateWithIsarInfo(missionRun.Id, isarMission);
await missionRunService.UpdateMissionRunProperty(
missionRun.Id,
Expand Down
13 changes: 3 additions & 10 deletions backend/api/Services/ReturnToHomeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Api.Services
{
public interface IReturnToHomeService
{
public Task<MissionRun?> ScheduleReturnToHomeMissionRunIfNotAlreadyScheduledOrRobotIsHome(
public Task<MissionRun?> ScheduleReturnToHomeMissionRunIfNotAlreadyScheduled(
string robotId
);
public Task<MissionRun?> GetActiveReturnToHomeMissionRun(
Expand All @@ -20,23 +20,16 @@ public class ReturnToHomeService(
IMissionRunService missionRunService
) : IReturnToHomeService
{
public async Task<MissionRun?> ScheduleReturnToHomeMissionRunIfNotAlreadyScheduledOrRobotIsHome(
public async Task<MissionRun?> ScheduleReturnToHomeMissionRunIfNotAlreadyScheduled(
string robotId
)
{
logger.LogInformation(
"Scheduling return 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() || lastMissionRun.IsEmergencyMission())
)
)
if (await IsReturnToHomeMissionAlreadyScheduled(robotId))
{
logger.LogInformation(
"ReturnToHomeMission is already scheduled for Robot {RobotId}",
Expand Down

0 comments on commit 724aa78

Please sign in to comment.