Skip to content

Commit

Permalink
Handle custom and echo mission in same files
Browse files Browse the repository at this point in the history
  • Loading branch information
andchiind committed Aug 1, 2024
1 parent 1ba6de0 commit 3f963f8
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 284 deletions.
5 changes: 3 additions & 2 deletions backend/api.test/EventHandlers/TestMissionEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public TestMissionEventHandler(DatabaseFixture fixture)
var returnToHomeServiceLogger = new Mock<ILogger<ReturnToHomeService>>().Object;
var missionDefinitionServiceLogger = new Mock<ILogger<MissionDefinitionService>>().Object;
var lastMissionRunServiceLogger = new Mock<ILogger<LastMissionRunService>>().Object;
var sourceServiceLogger = new Mock<ILogger<SourceService>>().Object;

var configuration = WebApplication.CreateBuilder().Configuration;

Expand All @@ -66,8 +67,8 @@ public TestMissionEventHandler(DatabaseFixture fixture)

var echoServiceMock = new MockEchoService();
var stidServiceMock = new MockStidService(context);
var customMissionServiceMock = new MockCustomMissionService();
var missionDefinitionService = new MissionDefinitionService(context, echoServiceMock, customMissionServiceMock, signalRService, accessRoleService, missionDefinitionServiceLogger, _missionRunService);
var sourceService = new SourceService(context, echoServiceMock, sourceServiceLogger);
var missionDefinitionService = new MissionDefinitionService(context, echoServiceMock, sourceService, signalRService, accessRoleService, missionDefinitionServiceLogger, _missionRunService);
var robotModelService = new RobotModelService(context);
var taskDurationServiceMock = new MockTaskDurationService();
var isarServiceMock = new MockIsarService();
Expand Down
65 changes: 0 additions & 65 deletions backend/api.test/Mocks/CustomMissionServiceMock.cs

This file was deleted.

1 change: 0 additions & 1 deletion backend/api.test/TestWebApplicationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
services.AddScoped<IMapService, MockMapService>();
services.AddScoped<IBlobService, MockBlobService>();
services.AddScoped<IStidService, MockStidService>();
services.AddScoped<ICustomMissionService, MockCustomMissionService>();
services.AddAuthorizationBuilder().AddFallbackPolicy(
TestAuthHandler.AuthenticationScheme, policy => policy.RequireAuthenticatedUser()
);
Expand Down
65 changes: 61 additions & 4 deletions backend/api/Controllers/MissionSchedulingController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace Api.Controllers
[Route("missions")]
public class MissionSchedulingController(
IMissionDefinitionService missionDefinitionService,
ICustomMissionSchedulingService customMissionSchedulingService,
IMissionRunService missionRunService,
IInstallationService installationService,
IEchoService echoService,
Expand All @@ -23,7 +22,8 @@ public class MissionSchedulingController(
IStidService stidService,
ILocalizationService localizationService,
IRobotService robotService,
ISourceService sourceService
ISourceService sourceService,
IAreaService areaService
) : ControllerBase

{
Expand Down Expand Up @@ -371,7 +371,41 @@ [FromBody] CustomMissionQuery customMissionQuery
var missionTasks = customMissionQuery.Tasks.Select(task => new MissionTask(task)).ToList();

MissionDefinition? customMissionDefinition;
try { customMissionDefinition = await customMissionSchedulingService.FindExistingOrCreateCustomMissionDefinition(customMissionQuery, missionTasks); }
try
{
Area? area = null;
if (customMissionQuery.AreaName != null) { area = await areaService.ReadByInstallationAndName(customMissionQuery.InstallationCode, customMissionQuery.AreaName); }

if (area == null)
{
throw new AreaNotFoundException($"No area with name {customMissionQuery.AreaName} in installation {customMissionQuery.InstallationCode} was found");
}

var source = await sourceService.CheckForExistingCustomSource(missionTasks);

MissionDefinition? existingMissionDefinition = null;
if (source == null)
{
source = await sourceService.CreateSourceIfDoesNotExist(missionTasks);
}
else
{
var missionDefinitions = await missionDefinitionService.ReadBySourceId(source.SourceId);
if (missionDefinitions.Count > 0) { existingMissionDefinition = missionDefinitions.First(); }
}

customMissionDefinition = existingMissionDefinition ?? new MissionDefinition
{
Id = Guid.NewGuid().ToString(),
Source = source,
Name = customMissionQuery.Name,
InspectionFrequency = customMissionQuery.InspectionFrequency,
InstallationCode = customMissionQuery.InstallationCode,
Area = area
};

if (existingMissionDefinition == null) { await missionDefinitionService.Create(customMissionDefinition); }
}
catch (SourceException e) { return StatusCode(StatusCodes.Status502BadGateway, e.Message); }
catch (AreaNotFoundException) { return NotFound($"No area with name {customMissionQuery.AreaName} in installation {customMissionQuery.InstallationCode} was found"); }

Expand All @@ -380,7 +414,30 @@ [FromBody] CustomMissionQuery customMissionQuery
catch (RobotNotInSameInstallationAsMissionException e) { return Conflict(e.Message); }

MissionRun? newMissionRun;
try { newMissionRun = await customMissionSchedulingService.QueueCustomMissionRun(customMissionQuery, customMissionDefinition.Id, robot.Id, missionTasks); }
try
{
var scheduledMission = new MissionRun
{
Name = customMissionQuery.Name,
Description = customMissionQuery.Description,
MissionId = customMissionDefinition.Id,
Comment = customMissionQuery.Comment,
Robot = robot,
Status = MissionStatus.Pending,
MissionRunType = MissionRunType.Normal,
DesiredStartTime = customMissionQuery.DesiredStartTime ?? DateTime.UtcNow,
Tasks = missionTasks,
InstallationCode = customMissionQuery.InstallationCode,
Area = customMissionDefinition.Area,
Map = new MapMetadata()
};

await mapService.AssignMapToMission(scheduledMission);

if (scheduledMission.Tasks.Any()) { scheduledMission.CalculateEstimatedDuration(); }

newMissionRun = await missionRunService.Create(scheduledMission);
}
catch (Exception e) when (e is UnsupportedRobotCapabilityException) { return BadRequest(e.Message); }
catch (Exception e) when (e is MissionNotFoundException) { return NotFound(e.Message); }
catch (Exception e) when (e is RobotNotFoundException) { return NotFound(e.Message); }
Expand Down
2 changes: 0 additions & 2 deletions backend/api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
builder.Services.AddScoped<ISourceService, SourceService>();

builder.Services.AddScoped<IMissionSchedulingService, MissionSchedulingService>();
builder.Services.AddScoped<ICustomMissionSchedulingService, CustomMissionSchedulingService>();

builder.Services.AddScoped<IIsarService, IsarService>();
builder.Services.AddScoped<IEchoService, EchoService>();
Expand Down Expand Up @@ -106,7 +105,6 @@
builder.Services.AddScoped<ITimeseriesService, TimeseriesService>();
builder.Services.AddScoped<RobotController>();
builder.Services.AddScoped<EmergencyActionController>();
builder.Services.AddScoped<ICustomMissionService, CustomMissionService>();
builder.Services.AddScoped<InspectionFindingService>();

builder.Services.AddTransient<ISignalRService, SignalRService>();
Expand Down
114 changes: 0 additions & 114 deletions backend/api/Services/ActionServices/CustomMissionSchedulingService.cs

This file was deleted.

Loading

0 comments on commit 3f963f8

Please sign in to comment.