|
| 1 | +using Api.Controllers.Models; |
| 2 | +using Api.Services; |
| 3 | +using Api.Utilities; |
| 4 | +using Microsoft.AspNetCore.Authorization; |
| 5 | +using Microsoft.AspNetCore.Mvc; |
| 6 | + |
| 7 | +namespace Api.Controllers |
| 8 | +{ |
| 9 | + [ApiController] |
| 10 | + [Route("robots")] |
| 11 | + public class MaintenanceModeController( |
| 12 | + ILogger<RobotController> logger, |
| 13 | + IRobotService robotService, |
| 14 | + IIsarService isarService |
| 15 | + ) : ControllerBase |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// Send the robot to maintenance mode. |
| 19 | + /// </summary> |
| 20 | + [HttpPost("set-maintenance-mode/{robotId}")] |
| 21 | + [Authorize(Roles = Role.User)] |
| 22 | + [ProducesResponseType(StatusCodes.Status204NoContent)] |
| 23 | + [ProducesResponseType(StatusCodes.Status401Unauthorized)] |
| 24 | + [ProducesResponseType(StatusCodes.Status403Forbidden)] |
| 25 | + [ProducesResponseType(StatusCodes.Status404NotFound)] |
| 26 | + [ProducesResponseType(StatusCodes.Status409Conflict)] |
| 27 | + [ProducesResponseType(StatusCodes.Status500InternalServerError)] |
| 28 | + public async Task<ActionResult> SetMaintenanceMode([FromRoute] string robotId) |
| 29 | + { |
| 30 | + robotId = Sanitize.SanitizeUserInput(robotId); |
| 31 | + |
| 32 | + var robot = await robotService.ReadById(robotId, readOnly: true); |
| 33 | + if (robot is null) |
| 34 | + { |
| 35 | + logger.LogWarning("Could not find robot with id {Id}", robotId); |
| 36 | + return NotFound(); |
| 37 | + } |
| 38 | + |
| 39 | + try |
| 40 | + { |
| 41 | + await isarService.SetMaintenanceMode(robot.IsarUri); |
| 42 | + } |
| 43 | + catch (Exception ex) |
| 44 | + { |
| 45 | + logger.LogError(ex, "Failed to set maintenance mode for robot {RobotId}", robot.Id); |
| 46 | + return StatusCode(StatusCodes.Status500InternalServerError); |
| 47 | + } |
| 48 | + |
| 49 | + return NoContent(); |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Release the robot from maintenance mode. |
| 54 | + /// </summary> |
| 55 | + [HttpPost("release-maintenance-mode/{robotId}")] |
| 56 | + [Authorize(Roles = Role.User)] |
| 57 | + [ProducesResponseType(StatusCodes.Status204NoContent)] |
| 58 | + [ProducesResponseType(StatusCodes.Status401Unauthorized)] |
| 59 | + [ProducesResponseType(StatusCodes.Status403Forbidden)] |
| 60 | + [ProducesResponseType(StatusCodes.Status404NotFound)] |
| 61 | + [ProducesResponseType(StatusCodes.Status409Conflict)] |
| 62 | + [ProducesResponseType(StatusCodes.Status500InternalServerError)] |
| 63 | + public async Task<ActionResult> ReleaseMaintenanceMode([FromRoute] string robotId) |
| 64 | + { |
| 65 | + robotId = Sanitize.SanitizeUserInput(robotId); |
| 66 | + |
| 67 | + var robot = await robotService.ReadById(robotId, readOnly: true); |
| 68 | + if (robot is null) |
| 69 | + { |
| 70 | + logger.LogWarning("Could not find robot with id {Id}", robotId); |
| 71 | + return NotFound(); |
| 72 | + } |
| 73 | + |
| 74 | + try |
| 75 | + { |
| 76 | + await isarService.ReleaseMaintenanceMode(robot.IsarUri); |
| 77 | + } |
| 78 | + catch (Exception ex) |
| 79 | + { |
| 80 | + logger.LogError( |
| 81 | + ex, |
| 82 | + "Failed to release maintenance mode for robot {RobotId}", |
| 83 | + robot.Id |
| 84 | + ); |
| 85 | + return StatusCode(StatusCodes.Status500InternalServerError); |
| 86 | + } |
| 87 | + |
| 88 | + return NoContent(); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments