From bf5ae79fab155c7cd1a6d66a1c3b049289b2291e Mon Sep 17 00:00:00 2001 From: Afonso Date: Thu, 9 Nov 2023 17:24:30 +0100 Subject: [PATCH] Stop mission if robot is idle If the robot is reported as idle from ISAR, stopping a mission should not crash --- backend/api/Controllers/RobotController.cs | 7 +++++++ backend/api/Services/IsarService.cs | 9 ++++++++- backend/api/Services/MissionSchedulingService.cs | 2 ++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/backend/api/Controllers/RobotController.cs b/backend/api/Controllers/RobotController.cs index 4d4c327f1..11802d539 100644 --- a/backend/api/Controllers/RobotController.cs +++ b/backend/api/Controllers/RobotController.cs @@ -473,6 +473,13 @@ public async Task StopMission([FromRoute] string robotId) _logger.LogError(e, "{Message}", Message); return StatusCode(StatusCodes.Status500InternalServerError, Message); } + catch (MissionNotFoundException) + { + _logger.LogWarning($"No mission was runnning for robot {robot.Id}"); + robot.CurrentMissionId = null; + await _robotService.Update(robot); + + } try { await _robotService.SetCurrentMissionId(robotId, null); } catch (RobotNotFoundException e) { return NotFound(e.Message); } diff --git a/backend/api/Services/IsarService.cs b/backend/api/Services/IsarService.cs index ce69bd7bc..1a16f5c82 100644 --- a/backend/api/Services/IsarService.cs +++ b/backend/api/Services/IsarService.cs @@ -1,4 +1,5 @@ -using System.Text.Json; +using System.Net; +using System.Text.Json; using Api.Database.Models; using Api.Services.Models; using Api.Utilities; @@ -88,6 +89,12 @@ public async Task StopMission(Robot robot) { (string message, int statusCode) = GetErrorDescriptionFoFailedIsarRequest(response); string errorResponse = await response.Content.ReadAsStringAsync(); + if (response.StatusCode == HttpStatusCode.Conflict && errorResponse.Contains("idle", StringComparison.CurrentCultureIgnoreCase)) + { + _logger.LogError("No mission was running for robot '{Id}", robot.Id); + throw new MissionNotFoundException($"No mission was running for robot {robot.Id}"); + } + _logger.LogError("{Message}: {ErrorResponse}", message, errorResponse); throw new MissionException(message, statusCode); } diff --git a/backend/api/Services/MissionSchedulingService.cs b/backend/api/Services/MissionSchedulingService.cs index 85f6facaf..6db345b3c 100644 --- a/backend/api/Services/MissionSchedulingService.cs +++ b/backend/api/Services/MissionSchedulingService.cs @@ -165,6 +165,8 @@ public async Task StopCurrentMissionRun(string robotId) throw new MissionException(Message, 0); } + catch (MissionNotFoundException) { _logger.LogWarning($"No mission was running for robot {robot.Id}"); } + robot.CurrentMissionId = null; await _robotService.Update(robot); }