From e3e2a305ee3c06f8a19d8722e836496c17a1fee9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Chirico=20Indreb=C3=B8?= Date: Thu, 12 Dec 2024 15:16:53 +0100 Subject: [PATCH] Prevent using outdated robot during mission check --- backend/api/EventHandlers/MissionEventHandler.cs | 12 +++++++----- backend/api/EventHandlers/MqttEventHandler.cs | 2 +- backend/api/Services/MissionRunService.cs | 7 +++++++ backend/api/Services/MissionSchedulingService.cs | 14 +++----------- backend/api/Services/ReturnToHomeService.cs | 2 +- 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/backend/api/EventHandlers/MissionEventHandler.cs b/backend/api/EventHandlers/MissionEventHandler.cs index e3540bc0..9a19a353 100644 --- a/backend/api/EventHandlers/MissionEventHandler.cs +++ b/backend/api/EventHandlers/MissionEventHandler.cs @@ -74,7 +74,7 @@ private async void OnMissionRunCreated(object? sender, MissionRunCreatedEventArg await MissionScheduling.AbortActiveReturnToHomeMission(missionRun.Robot.Id); } - try { await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(missionRun.Robot.Id); } + try { await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(missionRun.Robot); } catch (MissionRunNotFoundException) { return; } finally { _startMissionSemaphore.Release(); } } @@ -90,7 +90,7 @@ private async void OnRobotAvailable(object? sender, RobotAvailableEventArgs e) } _startMissionSemaphore.WaitOne(); - try { await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(robot.Id); } + try { await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(robot); } catch (MissionRunNotFoundException) { return; } finally { _startMissionSemaphore.Release(); } } @@ -153,7 +153,7 @@ private async void OnSendRobotToDockTriggered(object? sender, RobotEmergencyEven } _startMissionSemaphore.WaitOne(); - try { await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(robot.Id); } + try { await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(robot); } catch (MissionRunNotFoundException) { return; } finally { _startMissionSemaphore.Release(); } } @@ -174,9 +174,11 @@ private async void OnReleaseRobotFromDockTriggered(object? sender, RobotEmergenc return; } - try { await MissionScheduling.UnfreezeMissionRunQueueForRobot(e.RobotId); } + try { await MissionScheduling.UnfreezeMissionRunQueueForRobot(robot.Id); } catch (RobotNotFoundException) { return; } + robot.MissionQueueFrozen = false; + try { await RobotService.UpdateFlotillaStatus(e.RobotId, e.RobotFlotillaStatus ?? RobotFlotillaStatus.Normal); } catch (Exception ex) { @@ -185,7 +187,7 @@ private async void OnReleaseRobotFromDockTriggered(object? sender, RobotEmergenc } _startMissionSemaphore.WaitOne(); - try { await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(robot.Id); } + try { await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(robot); } catch (MissionRunNotFoundException) { return; } finally { _startMissionSemaphore.Release(); } } diff --git a/backend/api/EventHandlers/MqttEventHandler.cs b/backend/api/EventHandlers/MqttEventHandler.cs index ecef8c1b..cbfe4298 100644 --- a/backend/api/EventHandlers/MqttEventHandler.cs +++ b/backend/api/EventHandlers/MqttEventHandler.cs @@ -129,7 +129,7 @@ private async void OnIsarStatus(object? sender, MqttReceivedArgs mqttArgs) _updateRobotSemaphore.Release(); _logger.LogDebug("Semaphore released after updating robot current mission id"); } - await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(robot.Id); + await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(robot); } } diff --git a/backend/api/Services/MissionRunService.cs b/backend/api/Services/MissionRunService.cs index 916cc0fd..127f075c 100644 --- a/backend/api/Services/MissionRunService.cs +++ b/backend/api/Services/MissionRunService.cs @@ -252,6 +252,13 @@ private IQueryable GetMissionRunsWithSubModels(bool readOnly = true) .ThenInclude(defaultLocalizationPose => defaultLocalizationPose != null ? defaultLocalizationPose.Pose : null) .Include(missionRun => missionRun.Robot) .Include(missionRun => missionRun.Robot) + .ThenInclude(robot => robot.CurrentInspectionArea) + .ThenInclude(a => a != null ? a.Installation : null) + .Include(missionRun => missionRun.Robot) + .ThenInclude(robot => robot.CurrentInspectionArea) + .ThenInclude(a => a != null ? a.Plant : null) + .ThenInclude(p => p != null ? p.Installation : null) + .Include(missionRun => missionRun.Robot) .ThenInclude(robot => robot.Model) .Include(missionRun => missionRun.Tasks) .ThenInclude(task => task.Inspection) diff --git a/backend/api/Services/MissionSchedulingService.cs b/backend/api/Services/MissionSchedulingService.cs index 2d5c46b6..94387a25 100644 --- a/backend/api/Services/MissionSchedulingService.cs +++ b/backend/api/Services/MissionSchedulingService.cs @@ -8,7 +8,7 @@ namespace Api.Services { public interface IMissionSchedulingService { - public Task StartNextMissionRunIfSystemIsAvailable(string robotId); + public Task StartNextMissionRunIfSystemIsAvailable(Robot robot); public Task OngoingMission(string robotId); @@ -33,23 +33,15 @@ public interface IMissionSchedulingService public class MissionSchedulingService(ILogger logger, IMissionRunService missionRunService, IRobotService robotService, IIsarService isarService, ILocalizationService localizationService, IReturnToHomeService returnToHomeService, ISignalRService signalRService, IErrorHandlingService errorHandlingService) : IMissionSchedulingService { - public async Task StartNextMissionRunIfSystemIsAvailable(string robotId) + public async Task StartNextMissionRunIfSystemIsAvailable(Robot robot) { - logger.LogInformation("Starting next mission run if system is available for robot ID: {RobotId}", robotId); - var robot = await robotService.ReadById(robotId, readOnly: true); - if (robot == null) - { - logger.LogError("Robot with ID: {RobotId} was not found in the database", robotId); - return; - } - logger.LogInformation("Robot {robotName} has status {robotStatus} and current area {areaName}", robot.Name, robot.Status, robot.CurrentInspectionArea?.Name); MissionRun? missionRun; try { missionRun = await SelectNextMissionRun(robot.Id); } catch (RobotNotFoundException) { - logger.LogError("Robot with ID: {RobotId} was not found in the database", robotId); + logger.LogError("Robot with ID: {RobotId} was not found in the database", robot.Id); return; } diff --git a/backend/api/Services/ReturnToHomeService.cs b/backend/api/Services/ReturnToHomeService.cs index 8db1a573..84ff6825 100644 --- a/backend/api/Services/ReturnToHomeService.cs +++ b/backend/api/Services/ReturnToHomeService.cs @@ -15,7 +15,7 @@ public class ReturnToHomeService(ILogger logger, IRobotServ logger.LogInformation("Scheduling return to 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())) + if (await IsReturnToHomeMissionAlreadyScheduled(robotId) || (lastMissionRun != null && (lastMissionRun.IsReturnHomeMission() || lastMissionRun.IsEmergencyMission()))) { logger.LogInformation("ReturnToHomeMission is already scheduled for Robot {RobotId}", robotId); return null;