Skip to content

Commit

Permalink
Move OnIsarUnavailable to RobotService and rename
Browse files Browse the repository at this point in the history
  • Loading branch information
Afonso-2403 committed Dec 28, 2023
1 parent fa88584 commit 979a43f
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 66 deletions.
2 changes: 1 addition & 1 deletion backend/api.test/Database/DatabaseUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public DatabaseUtilities(FlotillaDbContext context)
_areaService = new AreaService(context, _installationService, _plantService, _deckService, defaultLocalizationPoseService, _accessRoleService);
_missionRunService = new MissionRunService(context, new MockSignalRService(), new Mock<ILogger<MissionRunService>>().Object, _accessRoleService);
_robotModelService = new RobotModelService(context);
_robotService = new RobotService(context, new Mock<ILogger<RobotService>>().Object, _robotModelService, new MockSignalRService(), _accessRoleService, _installationService, _areaService);
_robotService = new RobotService(context, new Mock<ILogger<RobotService>>().Object, _robotModelService, new MockSignalRService(), _accessRoleService, _installationService, _areaService, _missionRunService);
}

public void Dispose()
Expand Down
2 changes: 1 addition & 1 deletion backend/api.test/EventHandlers/TestMissionEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public TestMissionEventHandler(DatabaseFixture fixture)
_plantService = new PlantService(_context, _installationService, _accessRoleService);
_deckService = new DeckService(_context, _defaultLocalisationPoseService, _installationService, _plantService, _accessRoleService);
_areaService = new AreaService(_context, _installationService, _plantService, _deckService, _defaultLocalisationPoseService, _accessRoleService);
_robotService = new RobotService(_context, robotServiceLogger, _robotModelService, _signalRService, _accessRoleService, _installationService, _areaService);
_robotService = new RobotService(_context, robotServiceLogger, _robotModelService, _signalRService, _accessRoleService, _installationService, _areaService, _missionRunService);
_missionSchedulingService = new MissionSchedulingService(missionSchedulingServiceLogger, _missionRunService, _robotService, _robotControllerMock.Mock.Object, _areaService,
_isarServiceMock);
_localizationService = new LocalizationService(localizationServiceLogger, _robotService, _missionRunService, _installationService, _areaService);
Expand Down
10 changes: 6 additions & 4 deletions backend/api.test/Services/RobotService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class RobotServiceTest : IDisposable
private readonly IDefaultLocalizationPoseService _defaultLocalizationPoseService;
private readonly IDeckService _deckService;
private readonly IAreaService _areaService;
private readonly IMissionRunService _missionRunService;

public RobotServiceTest(DatabaseFixture fixture)
{
Expand All @@ -38,6 +39,7 @@ public RobotServiceTest(DatabaseFixture fixture)
_defaultLocalizationPoseService = new DefaultLocalizationPoseService(_context);
_deckService = new DeckService(_context, _defaultLocalizationPoseService, _installationService, _plantService, _accessRoleService);
_areaService = new AreaService(_context, _installationService, _plantService, _deckService, _defaultLocalizationPoseService, _accessRoleService);
_missionRunService = new MissionRunService(_context, _signalRService, new Mock<ILogger<MissionRunService>>().Object, _accessRoleService);
}

public void Dispose()
Expand All @@ -49,7 +51,7 @@ public void Dispose()
[Fact]
public async Task ReadAll()
{
var robotService = new RobotService(_context, _logger, _robotModelService, _signalRService, _accessRoleService, _installationService, _areaService);
var robotService = new RobotService(_context, _logger, _robotModelService, _signalRService, _accessRoleService, _installationService, _areaService, _missionRunService);
var robots = await robotService.ReadAll();

Assert.True(robots.Any());
Expand All @@ -58,7 +60,7 @@ public async Task ReadAll()
[Fact]
public async Task Read()
{
var robotService = new RobotService(_context, _logger, _robotModelService, _signalRService, _accessRoleService, _installationService, _areaService);
var robotService = new RobotService(_context, _logger, _robotModelService, _signalRService, _accessRoleService, _installationService, _areaService, _missionRunService);
var robots = await robotService.ReadAll();
var firstRobot = robots.First();
var robotById = await robotService.ReadById(firstRobot.Id);
Expand All @@ -69,15 +71,15 @@ public async Task Read()
[Fact]
public async Task ReadIdDoesNotExist()
{
var robotService = new RobotService(_context, _logger, _robotModelService, _signalRService, _accessRoleService, _installationService, _areaService);
var robotService = new RobotService(_context, _logger, _robotModelService, _signalRService, _accessRoleService, _installationService, _areaService, _missionRunService);
var robot = await robotService.ReadById("some_id_that_does_not_exist");
Assert.Null(robot);
}

[Fact]
public async Task Create()
{
var robotService = new RobotService(_context, _logger, _robotModelService, _signalRService, _accessRoleService, _installationService, _areaService);
var robotService = new RobotService(_context, _logger, _robotModelService, _signalRService, _accessRoleService, _installationService, _areaService, _missionRunService);
var installationService = new InstallationService(_context, _accessRoleService);

var installation = await installationService.Create(new CreateInstallationQuery
Expand Down
32 changes: 6 additions & 26 deletions backend/api/Controllers/RobotController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ [FromRoute] string missionRunId
{
string errorMessage = $"Could not reach ISAR at {robot.IsarUri}";
logger.LogError(e, "{Message}", errorMessage);
await OnIsarUnavailable(robot);
await robotService.SetRobotOffline(robot.Id);
return StatusCode(StatusCodes.Status502BadGateway, errorMessage);
}
catch (MissionException e)
Expand Down Expand Up @@ -451,7 +451,7 @@ public async Task<ActionResult> StopMission([FromRoute] string robotId)
{
const string Message = "Error connecting to ISAR while stopping mission";
logger.LogError(e, "{Message}", Message);
await OnIsarUnavailable(robot);
await robotService.SetRobotOffline(robot.Id);
return StatusCode(StatusCodes.Status502BadGateway, Message);
}
catch (MissionException e)
Expand Down Expand Up @@ -510,7 +510,7 @@ public async Task<ActionResult> PauseMission([FromRoute] string robotId)
{
const string Message = "Error connecting to ISAR while pausing mission";
logger.LogError(e, "{Message}", Message);
await OnIsarUnavailable(robot);
await robotService.SetRobotOffline(robot.Id);
return StatusCode(StatusCodes.Status502BadGateway, Message);
}
catch (MissionException e)
Expand Down Expand Up @@ -560,7 +560,7 @@ public async Task<ActionResult> ResumeMission([FromRoute] string robotId)
{
const string Message = "Error connecting to ISAR while resuming mission";
logger.LogError(e, "{Message}", Message);
await OnIsarUnavailable(robot);
await robotService.SetRobotOffline(robot.Id);
return StatusCode(StatusCodes.Status502BadGateway, Message);
}
catch (MissionException e)
Expand Down Expand Up @@ -618,7 +618,7 @@ [FromRoute] string armPosition
{
string errorMessage = $"Error connecting to ISAR at {robot.IsarUri}";
logger.LogError(e, "{Message}", errorMessage);
await OnIsarUnavailable(robot);
await robotService.SetRobotOffline(robot.Id);
return StatusCode(StatusCodes.Status502BadGateway, errorMessage);
}
catch (MissionException e)
Expand Down Expand Up @@ -703,7 +703,7 @@ [FromBody] ScheduleLocalizationMissionQuery scheduleLocalizationMissionQuery
{
string message = $"Could not reach ISAR at {robot.IsarUri}";
logger.LogError(e, "{Message}", message);
await OnIsarUnavailable(robot);
await robotService.SetRobotOffline(robot.Id);
return StatusCode(StatusCodes.Status502BadGateway, message);
}
catch (MissionException e)
Expand Down Expand Up @@ -734,25 +734,5 @@ [FromBody] ScheduleLocalizationMissionQuery scheduleLocalizationMissionQuery
return Ok(missionRun);
}

private async Task OnIsarUnavailable(Robot robot)
{
robot.Enabled = false;
robot.Status = RobotStatus.Offline;
if (robot.CurrentMissionId != null)
{
var missionRun = await missionRunService.ReadById(robot.CurrentMissionId);
if (missionRun != null)
{
missionRun.SetToFailed();
await missionRunService.Update(missionRun);
logger.LogWarning(
"Mission '{Id}' failed because ISAR could not be reached",
missionRun.Id
);
}
}

await robotService.UpdateCurrentMissionId(robot.Id, null);
}
}
}
34 changes: 1 addition & 33 deletions backend/api/Services/MissionSchedulingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public async Task StopCurrentMissionRun(string robotId)
{
const string Message = "Error connecting to ISAR while stopping mission";
logger.LogError(e, "{Message}", Message);
await OnIsarUnavailable(robot.Id);
await robotService.SetRobotOffline(robot.Id);
throw new MissionException(Message, (int)e.StatusCode!);
}
catch (MissionException e)
Expand Down Expand Up @@ -257,38 +257,6 @@ private async Task StartMissionRun(MissionRun queuedMissionRun)
logger.LogInformation("Started mission run '{Id}'", queuedMissionRun.Id);
}

private async Task OnIsarUnavailable(string robotId)
{
var robot = await robotService.ReadById(robotId);
if (robot == null)
{
logger.LogError("Robot with ID: {RobotId} was not found in the database", robotId);
return;
}

if (robot.CurrentMissionId != null)
{
var missionRun = await missionRunService.ReadById(robot.CurrentMissionId);
if (missionRun != null)
{
missionRun.SetToFailed();
await missionRunService.Update(missionRun);
logger.LogWarning(
"Mission '{Id}' failed because ISAR could not be reached",
missionRun.Id
);
}
}

try
{
await robotService.UpdateRobotStatus(robot.Id, RobotStatus.Offline);
await robotService.UpdateCurrentMissionId(robot.Id, null);
await robotService.UpdateRobotEnabled(robot.Id, false);
}
catch (RobotNotFoundException) { }
}

private static Pose ClosestSafePosition(Pose robotPose, IList<SafePosition> safePositions)
{
if (safePositions == null || !safePositions.Any())
Expand Down
37 changes: 36 additions & 1 deletion backend/api/Services/RobotService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public interface IRobotService
public Task<Robot> UpdateCurrentArea(string robotId, Area? area);
public Task<Robot> UpdateMissionQueueFrozen(string robotId, bool missionQueueFrozen);
public Task<Robot?> Delete(string id);
public Task SetRobotOffline(string robotId);
}

[SuppressMessage(
Expand All @@ -39,7 +40,8 @@ public class RobotService(FlotillaDbContext context,
ISignalRService signalRService,
IAccessRoleService accessRoleService,
IInstallationService installationService,
IAreaService areaService) : IRobotService, IDisposable
IAreaService areaService,
IMissionRunService missionRunService) : IRobotService, IDisposable
{
private readonly Semaphore _robotSemaphore = new(1, 1);

Expand Down Expand Up @@ -163,6 +165,39 @@ public async Task<IList<Robot>> ReadLocalizedRobotsForInstallation(string instal
.ToListAsync();
}

public async Task SetRobotOffline(string robotId)
{
var robot = await ReadById(robotId);
if (robot == null)
{
logger.LogError("Robot with ID: {RobotId} was not found in the database", robotId);
return;
}

if (robot.CurrentMissionId != null)
{
var missionRun = await missionRunService.ReadById(robot.CurrentMissionId);
if (missionRun != null)
{
missionRun.SetToFailed();
await missionRunService.Update(missionRun);
logger.LogWarning(
"Mission '{Id}' failed because ISAR could not be reached",
missionRun.Id
);
}
}

try
{
await UpdateRobotStatus(robot.Id, RobotStatus.Offline);
await UpdateCurrentMissionId(robot.Id, null);
await UpdateRobotEnabled(robot.Id, false);
await UpdateCurrentArea(robot.Id, null);
}
catch (RobotNotFoundException) { }
}

private async Task<Robot> UpdateRobotProperty(string robotId, string propertyName, object? value)
{
_robotSemaphore.WaitOne();
Expand Down

0 comments on commit 979a43f

Please sign in to comment.