Skip to content

Commit

Permalink
Generalise read mission runs in the service
Browse files Browse the repository at this point in the history
  • Loading branch information
mrica-equinor committed May 23, 2024
1 parent 8c050e3 commit 74ae22e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
20 changes: 14 additions & 6 deletions backend/api/Controllers/MissionSchedulingController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,16 @@ [FromBody] ScheduleMissionQuery scheduledMissionQuery
}

IList<MissionStatus> missionStatuses = [MissionStatus.Ongoing, MissionStatus.Pending, MissionStatus.Paused];
var returnToHomeMission = await missionRunService.ReadReturnToHomeMissionRuns(robot.Id, missionStatuses);
if (returnToHomeMission != null)
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 BadRequest($"The robot {robot.Name} is localized on a different deck so the mission was not scheduled.");
return Conflict($"The robot {robot.Name} is localized on a different deck so the mission was not scheduled.");
}

try { await missionSchedulingService.StopCurrentMissionRun(robot.Id); }
Expand Down Expand Up @@ -223,6 +227,7 @@ [FromBody] ScheduleMissionQuery scheduledMissionQuery
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
public async Task<ActionResult<MissionRun>> Create(
[FromBody] ScheduledMissionQuery scheduledMissionQuery
)
Expand Down Expand Up @@ -371,12 +376,15 @@ [FromBody] ScheduledMissionQuery scheduledMissionQuery
}

IList<MissionStatus> missionStatuses = [MissionStatus.Ongoing, MissionStatus.Pending, MissionStatus.Paused];
var returnToHomeMission = await missionRunService.ReadReturnToHomeMissionRuns(robot.Id, missionStatuses);
if (returnToHomeMission != null)
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 BadRequest($"The robot {robot.Name} is localized on a different deck so the mission was not scheduled.");
return Conflict($"The robot {robot.Name} is localized on a different deck so the mission was not scheduled.");
}

try { await missionSchedulingService.StopCurrentMissionRun(robot.Id); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,15 @@ public MissionRunQueryStringParameters()
/// </summary>
public List<InspectionType>? InspectionTypes { get; set; }

/// <summary>
/// Filter for a mission run type in the mission equal to any of MissionRunType
/// </summary>
public MissionRunType? MissionRunType { get; set; }

/// <summary>
/// Filter for whether the result should exclude localization missions. The default is false
/// </summary>
///
public bool ExcludeLocalization { get; set; }

/// <summary>
Expand Down
35 changes: 20 additions & 15 deletions backend/api/Services/MissionRunService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public interface IMissionRunService

public Task<MissionRun?> ReadNextScheduledLocalizationMissionRun(string robotId);

public Task<MissionRun?> ReadReturnToHomeMissionRuns(string robotId, IList<MissionStatus>? filterStatuses = null);
public Task<IList<MissionRun>> ReadMissionRuns(string robotId, MissionRunType? missionRunType, IList<MissionStatus>? filterStatuses = null);

public Task<MissionRun?> ReadLastExecutedMissionRunByRobotWithoutTracking(string robotId);

Expand Down Expand Up @@ -153,24 +153,22 @@ public async Task<IList<MissionRun>> ReadMissionRunQueue(string robotId)
return nextScheduledMissionRun;
}

public async Task<MissionRun?> ReadReturnToHomeMissionRuns(string robotId, IList<MissionStatus>? filterStatuses = null)
public async Task<IList<MissionRun>> ReadMissionRuns(string robotId, MissionRunType? missionRunType, IList<MissionStatus>? filterStatuses = null)
{
var missionFilter = ConstructFilter(new MissionRunQueryStringParameters
{
Statuses = filterStatuses as List<MissionStatus> ?? new List<MissionStatus>([]),
Statuses = filterStatuses as List<MissionStatus> ?? null,
RobotId = robotId,
MissionRunType = missionRunType,
PageSize = 100
});

var missionRuns = await GetMissionRunsWithSubModels()
.Where(missionFilter)
.OrderBy(missionRun => missionRun.DesiredStartTime)
.ToListAsync();
foreach (var missionRun in missionRuns)
{
if (missionRun.IsReturnHomeMission()) { return missionRun; }
}
return null;

return missionRuns;
}

public async Task<MissionRun?> ReadNextScheduledRunByMissionId(string missionId)
Expand Down Expand Up @@ -440,6 +438,10 @@ MissionRunQueryStringParameters parameters
? missionRun => true
: missionRun => missionRun.MissionId != null && missionRun.MissionId.Equals(parameters.MissionId);

Expression<Func<MissionRun, bool>> missionTypeFilter = parameters.MissionRunType is null
? missionRun => true
: missionRun => missionRun.MissionRunType.Equals(parameters.MissionRunType);

Expression<Func<MissionRun, bool>> inspectionTypeFilter = parameters.InspectionTypes is null
? mission => true
: mission => mission.Tasks.Any(
Expand Down Expand Up @@ -490,18 +492,21 @@ MissionRunQueryStringParameters parameters
Expression.AndAlso(
Expression.Invoke(missionIdFilter, missionRun),
Expression.AndAlso(
Expression.Invoke(inspectionTypeFilter, missionRun),
Expression.Invoke(missionTypeFilter, missionRun),
Expression.AndAlso(
Expression.Invoke(localizationFilter, missionRun),
Expression.Invoke(inspectionTypeFilter, missionRun),
Expression.AndAlso(
Expression.Invoke(returnTohomeFilter, missionRun),
Expression.Invoke(localizationFilter, missionRun),
Expression.AndAlso(
Expression.Invoke(desiredStartTimeFilter, missionRun),
Expression.Invoke(returnTohomeFilter, missionRun),
Expression.AndAlso(
Expression.Invoke(startTimeFilter, missionRun),
Expression.Invoke(desiredStartTimeFilter, missionRun),
Expression.AndAlso(
Expression.Invoke(endTimeFilter, missionRun),
Expression.Invoke(robotTypeFilter, missionRun)
Expression.Invoke(startTimeFilter, missionRun),
Expression.AndAlso(
Expression.Invoke(endTimeFilter, missionRun),
Expression.Invoke(robotTypeFilter, missionRun)
)
)
)
)
Expand Down

0 comments on commit 74ae22e

Please sign in to comment.