From 3732f2035ef2ad0af9adeb4ea4faa8caa6f1894d Mon Sep 17 00:00:00 2001 From: "Mariana R. Santos" Date: Tue, 16 Apr 2024 12:40:41 +0200 Subject: [PATCH] Improve start missions with pressure/battery value --- .../MissionSchedulingController.cs | 21 +++++------- .../api/Services/MissionSchedulingService.cs | 33 +++++++++++++++++++ backend/api/Services/RobotService.cs | 1 + 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/backend/api/Controllers/MissionSchedulingController.cs b/backend/api/Controllers/MissionSchedulingController.cs index a70f54655..a59a34a57 100644 --- a/backend/api/Controllers/MissionSchedulingController.cs +++ b/backend/api/Controllers/MissionSchedulingController.cs @@ -49,12 +49,11 @@ [FromBody] ScheduleMissionQuery scheduledMissionQuery if (!robot.IsRobotPressureHighEnoughToStartMission()) { - return BadRequest($"The robot pressure on {robot.Name} is too low to start a mission"); + return BadRequest($"Low pressure value for robot {robot.Name}, Pressure value is too low to start a mission."); } - if (!robot.IsRobotBatteryLevelHighEnoughToStartMissions()) { - return BadRequest($"The robot battery level on {robot.Name} is too low to start a mission"); + return BadRequest($"Low battery value for robot {robot.Name}. Battery value is too low to start a mission."); } var missionRun = await missionRunService.ReadById(missionRunId); @@ -134,15 +133,13 @@ [FromBody] ScheduleMissionQuery scheduledMissionQuery { return NotFound($"Could not find robot with id {scheduledMissionQuery.RobotId}"); } - if (!robot.IsRobotPressureHighEnoughToStartMission()) { - return BadRequest($"The robot pressure on {robot.Name} is too low to start a mission"); + return BadRequest($"Low pressure value for robot {robot.Name}, Pressure value is too low to start a mission."); } - if (!robot.IsRobotBatteryLevelHighEnoughToStartMissions()) { - return BadRequest($"The robot battery level on {robot.Name} is too low to start a mission"); + return BadRequest($"Low battery value for robot {robot.Name}. Battery value is too low to start a mission."); } var missionDefinition = await missionDefinitionService.ReadById(missionDefinitionId); @@ -221,12 +218,11 @@ [FromBody] ScheduledMissionQuery scheduledMissionQuery if (!robot.IsRobotPressureHighEnoughToStartMission()) { - return BadRequest($"The robot pressure on {robot.Name} is too low to start a mission"); + return BadRequest($"Low pressure value for robot {robot.Name}, Pressure value is too low to start a mission."); } - if (!robot.IsRobotBatteryLevelHighEnoughToStartMissions()) { - return BadRequest($"The robot battery level on {robot.Name} is too low to start a mission"); + return BadRequest($"Low battery value for robot {robot.Name}. Battery value is too low to start a mission."); } EchoMission? echoMission; @@ -394,12 +390,11 @@ [FromBody] CustomMissionQuery customMissionQuery if (!robot.IsRobotPressureHighEnoughToStartMission()) { - return BadRequest($"The robot pressure on {robot.Name} is too low to start a mission"); + return BadRequest($"Low pressure value for robot {robot.Name}, Pressure value is too low to start a mission."); } - if (!robot.IsRobotBatteryLevelHighEnoughToStartMissions()) { - return BadRequest($"The robot battery level on {robot.Name} is too low to start a mission"); + return BadRequest($"Low battery value for robot {robot.Name}. Battery value is too low to start a mission."); } var installation = await installationService.ReadByName(customMissionQuery.InstallationCode); diff --git a/backend/api/Services/MissionSchedulingService.cs b/backend/api/Services/MissionSchedulingService.cs index 2c6334ad2..5be3ee4c0 100644 --- a/backend/api/Services/MissionSchedulingService.cs +++ b/backend/api/Services/MissionSchedulingService.cs @@ -102,6 +102,12 @@ public async Task StartNextMissionRunIfSystemIsAvailable(string robotId) return; } + if ((!robot.IsRobotPressureHighEnoughToStartMission() || !robot.IsRobotBatteryLevelHighEnoughToStartMissions()) && !missionRun.IsReturnHomeMission()) + { + missionRun = await HandleBatteryAndPressureLevel(robot); + if (missionRun == null) { return; } + } + try { await StartMissionRun(missionRun); } catch (Exception ex) when ( ex is MissionException @@ -123,6 +129,33 @@ or MissionRunNotFoundException } } + public async Task HandleBatteryAndPressureLevel(Robot robot) + { + if (!robot.IsRobotPressureHighEnoughToStartMission()) + { + logger.LogError("Robot with ID: {RobotId} cannot start missions because pressure value is too low.", robot.Id); + signalRService.ReportGeneralFailToSignalR(robot, $"Low pressure value for robot {robot.Name}", "Pressure value is too low to start a mission."); + } + if (!robot.IsRobotBatteryLevelHighEnoughToStartMissions()) + { + logger.LogError("Robot with ID: {RobotId} cannot start missions because battery value is too low.", robot.Id); + signalRService.ReportGeneralFailToSignalR(robot, $"Low battery value for robot {robot.Name}", "Battery value is too low to start a mission."); + } + + try { await AbortAllScheduledMissions(robot.Id, "Aborted: Robot pressure or battery values are too low."); } + catch (RobotNotFoundException) { logger.LogError("Failed to abort scheduled missions for robot {RobotId}", robot.Id); } + + MissionRun? missionRun; + try { missionRun = await returnToHomeService.ScheduleReturnToHomeMissionRunIfNotAlreadyScheduledOrRobotIsHome(robot.Id); } + catch (ReturnToHomeMissionFailedToScheduleException) + { + logger.LogError("Failed to schedule a return to home mission for robot {RobotId}", robot.Id); + await robotService.UpdateCurrentArea(robot.Id, null); + return null; + } + return missionRun; + } + public async Task OngoingMission(string robotId) { var ongoingMissions = await GetOngoingMissions(robotId); diff --git a/backend/api/Services/RobotService.cs b/backend/api/Services/RobotService.cs index 454c16e9a..14634a27d 100644 --- a/backend/api/Services/RobotService.cs +++ b/backend/api/Services/RobotService.cs @@ -373,5 +373,6 @@ private void NotifySignalROfUpdatedRobot(Robot robot, Installation installation) { _ = signalRService.SendMessageAsync("Robot updated", installation, robot != null ? new RobotResponse(robot) : null); } + } }