Skip to content

Commit

Permalink
Return error when using unsupported sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
andchiind committed Apr 15, 2024
1 parent 92d5dc7 commit 2136cdf
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 9 deletions.
33 changes: 29 additions & 4 deletions backend/api/Controllers/MissionSchedulingController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,15 @@ [FromBody] ScheduleMissionQuery scheduledMissionQuery

// Compare with GetTasksFromSource

newMissionRun = await missionRunService.Create(newMissionRun);
try
{
newMissionRun = await missionRunService.Create(newMissionRun);
}
catch (UnsupportedRobotCapabilityException)
{
return BadRequest($"The robot {robot.Name} does not have the necessary sensors to run the mission.");
}


return CreatedAtAction(nameof(Rerun), new
{
Expand Down Expand Up @@ -171,7 +179,15 @@ [FromBody] ScheduleMissionQuery scheduledMissionQuery
missionRun.CalculateEstimatedDuration();
}

var newMissionRun = await missionRunService.Create(missionRun);
MissionRun newMissionRun;
try
{
newMissionRun = await missionRunService.Create(missionRun);
}
catch (UnsupportedRobotCapabilityException)
{
return BadRequest($"The robot {robot.Name} does not have the necessary sensors to run the mission.");
}

return CreatedAtAction(nameof(Schedule), new
{
Expand Down Expand Up @@ -337,7 +353,15 @@ [FromBody] ScheduledMissionQuery scheduledMissionQuery
await missionDefinitionService.Create(scheduledMissionDefinition);
}

var newMissionRun = await missionRunService.Create(missionRun);
MissionRun newMissionRun;
try
{
newMissionRun = await missionRunService.Create(missionRun);
}
catch (UnsupportedRobotCapabilityException)
{
return BadRequest($"The robot {robot.Name} does not have the necessary sensors to run the mission.");
}

return CreatedAtAction(nameof(Create), new
{
Expand Down Expand Up @@ -393,8 +417,9 @@ [FromBody] CustomMissionQuery customMissionQuery

MissionRun? newMissionRun;
try { newMissionRun = await customMissionSchedulingService.QueueCustomMissionRun(customMissionQuery, customMissionDefinition.Id, robot.Id, missionTasks); }
catch (Exception e) when (e is RobotPressureTooLowException or RobotBatteryLevelTooLowException) { return BadRequest(e.Message); }
catch (Exception e) when (e is RobotPressureTooLowException or RobotBatteryLevelTooLowException or UnsupportedRobotCapabilityException) { return BadRequest(e.Message); }
catch (Exception e) when (e is RobotNotFoundException or MissionNotFoundException) { return NotFound(e.Message); }
catch (Exception e) when (e is UnsupportedRobotCapabilityException) { return BadRequest($"The robot {robot.Name} does not have the necessary sensors to run the mission."); }

return CreatedAtAction(nameof(Create), new
{
Expand Down
13 changes: 13 additions & 0 deletions backend/api/Database/Models/Inspection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ public void UpdateStatus(IsarStepStatus isarStatus)
)
};
}

public bool IsSupportedInspectionType(IList<RobotCapabilitiesEnum> capabilities)
{
return InspectionType switch
{
InspectionType.Image => capabilities.Contains(RobotCapabilitiesEnum.take_image),
InspectionType.ThermalImage => capabilities.Contains(RobotCapabilitiesEnum.take_thermal_image),
InspectionType.Video => capabilities.Contains(RobotCapabilitiesEnum.take_video),
InspectionType.ThermalVideo => capabilities.Contains(RobotCapabilitiesEnum.take_thermal_video),
InspectionType.Audio => capabilities.Contains(RobotCapabilitiesEnum.record_audio),
_ => false,
};
}
}

public enum InspectionStatus
Expand Down
11 changes: 9 additions & 2 deletions backend/api/Services/LocalizationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,15 @@ public async Task<MissionRun> CreateLocalizationMissionInArea(string robotId, st
};
await mapService.AssignMapToMission(localizationMissionRun);

logger.LogWarning("Starting localization mission");
await missionRunService.Create(localizationMissionRun, triggerCreatedMissionRunEvent: false);
try
{
logger.LogWarning("Starting localization mission");
await missionRunService.Create(localizationMissionRun, triggerCreatedMissionRunEvent: false);
}
catch (UnsupportedRobotCapabilityException)
{
logger.LogError($"Unsupported robot capability detected when starting localisation mission for robot {localizationMissionRun.Robot.Name}. This should not happen.");
}
return localizationMissionRun;
}

Expand Down
18 changes: 18 additions & 0 deletions backend/api/Services/MissionRunService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public interface IMissionRunService

public Task<bool> PendingOrOngoingReturnToHomeMissionRunExists(string robotId);

public bool IncludesUnsupportedInspectionType(MissionRun missionRun);

public Task<MissionRun> Update(MissionRun mission);

public Task<MissionRun> UpdateMissionRunStatusByIsarMissionId(
Expand Down Expand Up @@ -74,6 +76,11 @@ public async Task<MissionRun> Create(MissionRun missionRun, bool triggerCreatedM
// Making sure database does not try to create new robot
context.Entry(missionRun.Robot).State = EntityState.Unchanged;

if (IncludesUnsupportedInspectionType(missionRun))
{
throw new UnsupportedRobotCapabilityException($"Mission {missionRun.Name} contains inspection types not supported by robot: {missionRun.Robot.Name}.");
}

if (missionRun.Area is not null) { context.Entry(missionRun.Area).State = EntityState.Unchanged; }
await context.MissionRuns.AddAsync(missionRun);
await ApplyDatabaseUpdate(missionRun.Area?.Installation);
Expand Down Expand Up @@ -224,6 +231,17 @@ public async Task<bool> PendingOrOngoingReturnToHomeMissionRunExists(string robo

}

public bool IncludesUnsupportedInspectionType(MissionRun missionRun)
{
if (missionRun.Robot.RobotCapabilities == null) return false;

foreach (var task in missionRun.Tasks)
foreach (var inspection in task.Inspections)
if (!inspection.IsSupportedInspectionType(missionRun.Robot.RobotCapabilities))
return true;
return false;
}

public async Task<MissionRun> Update(MissionRun missionRun)
{
context.Entry(missionRun.Robot).State = EntityState.Unchanged;
Expand Down
18 changes: 16 additions & 2 deletions backend/api/Services/MissionSchedulingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,14 @@ public async Task ScheduleMissionToDriveToSafePosition(string robotId, string ar
Map = new MapMetadata()
};

await missionRunService.Create(missionRun);
try
{
await missionRunService.Create(missionRun);
}
catch (UnsupportedRobotCapabilityException)
{
logger.LogError($"Unsupported robot capability detected when driving to safe position for robot {missionRun.Robot.Name}. This should not happen.");
}
}

public bool MissionRunQueueIsEmpty(IList<MissionRun> missionRunQueue)
Expand Down Expand Up @@ -315,7 +322,14 @@ private async Task MoveInterruptedMissionsToQueue(IEnumerable<string> interrupte
Map = new MapMetadata()
};

await missionRunService.Create(newMissionRun);
try
{
await missionRunService.Create(missionRun);
}
catch (UnsupportedRobotCapabilityException)
{
logger.LogError($"Unsupported robot capability detected when restarting interrupted missions for robot {missionRun.Robot.Name}. This should not happen.");
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion backend/api/Services/ReturnToHomeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ public class ReturnToHomeService(ILogger<ReturnToHomeService> logger, IRobotServ

MissionRun missionRun;
try { missionRun = await ScheduleReturnToHomeMissionRun(robotId); }
catch (Exception ex) when (ex is RobotNotFoundException or AreaNotFoundException or DeckNotFoundException or PoseNotFoundException)
catch (Exception ex) when (ex is RobotNotFoundException or AreaNotFoundException or DeckNotFoundException or PoseNotFoundException or UnsupportedRobotCapabilityException)
{
// TODO: if we make ISAR aware of return to home missions, we can avoid scheduling them when the robot does not need them
throw new ReturnToHomeMissionFailedToScheduleException(ex.Message);
}

Expand Down
4 changes: 4 additions & 0 deletions backend/api/Utilities/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,8 @@ public class ReturnToHomeMissionFailedToScheduleException(string message) : Exce
public class RobotCurrentAreaMissingException(string message) : Exception(message)
{
}

public class UnsupportedRobotCapabilityException(string message) : Exception(message)
{
}
}

0 comments on commit 2136cdf

Please sign in to comment.