From a53061b4648e2b28e65049368ad0d042f0714a18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Chirico=20Indreb=C3=B8?= Date: Fri, 19 Jul 2024 13:08:33 +0200 Subject: [PATCH] Only schedule return home if supported by robot --- .../api.test/Database/DatabaseUtilities.cs | 3 +- .../EventHandlers/TestMissionEventHandler.cs | 37 ++++++++++++++++++ .../api/Services/MissionSchedulingService.cs | 39 ++++++++++++------- 3 files changed, 65 insertions(+), 14 deletions(-) diff --git a/backend/api.test/Database/DatabaseUtilities.cs b/backend/api.test/Database/DatabaseUtilities.cs index 3978b6ba5..710b34350 100644 --- a/backend/api.test/Database/DatabaseUtilities.cs +++ b/backend/api.test/Database/DatabaseUtilities.cs @@ -155,7 +155,8 @@ public async Task NewRobot(RobotStatus status, Installation installation, VideoStreams = new List(), Host = "localhost", Port = 3000, - Status = status + Status = status, + RobotCapabilities = [RobotCapabilitiesEnum.drive_to_pose, RobotCapabilitiesEnum.take_image, RobotCapabilitiesEnum.return_to_home, RobotCapabilitiesEnum.localize] }; var robotModel = await _robotModelService.ReadByRobotType(createRobotQuery.RobotType); diff --git a/backend/api.test/EventHandlers/TestMissionEventHandler.cs b/backend/api.test/EventHandlers/TestMissionEventHandler.cs index 5e7cafd68..20f235073 100644 --- a/backend/api.test/EventHandlers/TestMissionEventHandler.cs +++ b/backend/api.test/EventHandlers/TestMissionEventHandler.cs @@ -260,6 +260,43 @@ public async Task ReturnToHomeMissionIsStartedIfQueueIsEmptyWhenRobotBecomesAvai Assert.True(ongoingMission.Any()); } + [Fact] + public async Task ReturnToHomeMissionIsNotStartedIfReturnToHomeIsNotSupported() + { + // Arrange + var installation = await _databaseUtilities.NewInstallation(); + var plant = await _databaseUtilities.NewPlant(installation.InstallationCode); + var deck = await _databaseUtilities.NewDeck(installation.InstallationCode, plant.PlantCode); + var area = await _databaseUtilities.NewArea(installation.InstallationCode, plant.PlantCode, deck.Name); + var robot = await _databaseUtilities.NewRobot(RobotStatus.Busy, installation, area); + robot.RobotCapabilities!.Remove(RobotCapabilitiesEnum.return_to_home); + + var mqttEventArgs = new MqttReceivedArgs( + new IsarStatusMessage + { + RobotName = robot.Name, + IsarId = robot.IsarId, + Status = RobotStatus.Available, + Timestamp = DateTime.UtcNow + }); + + // Act + _mqttService.RaiseEvent(nameof(MqttService.MqttIsarStatusReceived), mqttEventArgs); + + // Assert + Thread.Sleep(1000); + var ongoingMission = await _missionRunService.ReadAll( + new MissionRunQueryStringParameters + { + Statuses = [ + MissionStatus.Ongoing + ], + OrderBy = "DesiredStartTime", + PageSize = 100 + }); + Assert.False(ongoingMission.Any()); + } + [Fact] public async Task MissionRunIsStartedForOtherAvailableRobotIfOneRobotHasAnOngoingMissionRun() { diff --git a/backend/api/Services/MissionSchedulingService.cs b/backend/api/Services/MissionSchedulingService.cs index bb68d984a..fc43727fd 100644 --- a/backend/api/Services/MissionSchedulingService.cs +++ b/backend/api/Services/MissionSchedulingService.cs @@ -65,24 +65,37 @@ public async Task StartNextMissionRunIfSystemIsAvailable(string robotId) return; } - try { missionRun = await returnToHomeService.ScheduleReturnToHomeMissionRunIfNotAlreadyScheduledOrRobotIsHome(robot.Id); } - catch (ReturnToHomeMissionFailedToScheduleException) + if (robot.RobotCapabilities == null || !robot.RobotCapabilities.Contains(RobotCapabilitiesEnum.return_to_home)) { - signalRService.ReportGeneralFailToSignalR(robot, $"Failed to schedule return to home for robot {robot.Name}", ""); - logger.LogError("Failed to schedule a return to home mission for robot {RobotId}", robot.Id); await robotService.UpdateCurrentArea(robot.Id, null); + return; } - - if (missionRun == null) { return; } // The robot is already home - - var postReturnToHomeMissionCreatedRobot = await robotService.ReadById(missionRun.Robot.Id); - if (postReturnToHomeMissionCreatedRobot == null) + else { - logger.LogInformation("Could not find robot {Name}", missionRun.Robot.Name); - return; + try { missionRun = await returnToHomeService.ScheduleReturnToHomeMissionRunIfNotAlreadyScheduledOrRobotIsHome(robot.Id); } + catch (ReturnToHomeMissionFailedToScheduleException) + { + signalRService.ReportGeneralFailToSignalR(robot, $"Failed to schedule return to home for robot {robot.Name}", ""); + logger.LogError("Failed to schedule a return to home mission for robot {RobotId}", robot.Id); + await robotService.UpdateCurrentArea(robot.Id, null); + } + + if (missionRun == null) { return; } // The robot is already home + + var postReturnToHomeMissionCreatedRobot = await robotService.ReadById(missionRun.Robot.Id); + if (postReturnToHomeMissionCreatedRobot == null) + { + logger.LogInformation("Could not find robot {Name}", missionRun.Robot.Name); + return; + } + + logger.LogInformation( + "Post return to home mission created: Robot {robotName} has status {robotStatus} and current area {areaName}", + postReturnToHomeMissionCreatedRobot.Name, + postReturnToHomeMissionCreatedRobot.Status, + postReturnToHomeMissionCreatedRobot.CurrentArea?.Name + ); } - - logger.LogInformation("Post return to home mission created: Robot {robotName} has status {robotStatus} and current area {areaName}", postReturnToHomeMissionCreatedRobot.Name, postReturnToHomeMissionCreatedRobot.Status, postReturnToHomeMissionCreatedRobot.CurrentArea?.Name); } if (!await TheSystemIsAvailableToRunAMission(robot.Id, missionRun.Id))