| | 1 | | using LGDXRobotCloud.API.Configurations; |
| | 2 | | using Microsoft.AspNetCore.Mvc; |
| | 3 | | using Microsoft.Extensions.Options; |
| | 4 | | using System.Text.Json; |
| | 5 | | using Microsoft.AspNetCore.Authorization; |
| | 6 | | using Microsoft.AspNetCore.Authentication.JwtBearer; |
| | 7 | | using LGDXRobotCloud.API.Authorisation; |
| | 8 | | using LGDXRobotCloud.Data.Models.DTOs.V1.Responses; |
| | 9 | | using LGDXRobotCloud.Data.Models.DTOs.V1.Commands; |
| | 10 | | using LGDXRobotCloud.Data.Models.DTOs.V1.Requests; |
| | 11 | | using LGDXRobotCloud.API.Services.Navigation; |
| | 12 | | using LGDXRobotCloud.Data.Models.Business.Navigation; |
| | 13 | |
|
| | 14 | | namespace LGDXRobotCloud.API.Areas.Navigation.Controllers; |
| | 15 | |
|
| | 16 | | [ApiController] |
| | 17 | | [Area("Navigation")] |
| | 18 | | [Route("[area]/[controller]")] |
| | 19 | | [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] |
| | 20 | | [ValidateLgdxUserAccess] |
| 0 | 21 | | public sealed class RobotsController( |
| 0 | 22 | | IRobotService robotService, |
| 0 | 23 | | IOnlineRobotsService OnlineRobotsService, |
| 0 | 24 | | IOptionsSnapshot<LgdxRobotCloudConfiguration> options |
| 0 | 25 | | ) : ControllerBase |
| | 26 | | { |
| 0 | 27 | | private readonly IRobotService _robotService = robotService ?? throw new ArgumentNullException(nameof(robotService)); |
| 0 | 28 | | private readonly IOnlineRobotsService _onlineRobotsService = OnlineRobotsService ?? throw new ArgumentNullException(na |
| 0 | 29 | | private readonly LgdxRobotCloudConfiguration _lgdxRobotCloudConfiguration = options.Value ?? throw new ArgumentNullExc |
| | 30 | |
|
| | 31 | | [HttpGet("")] |
| | 32 | | [ProducesResponseType(typeof(IEnumerable<RobotListDto>), StatusCodes.Status200OK)] |
| | 33 | | public async Task<ActionResult<IEnumerable<RobotListDto>>> GetRobots(int? realmId, string? name, int pageNumber = 1, i |
| 0 | 34 | | { |
| 0 | 35 | | pageSize = (pageSize > _lgdxRobotCloudConfiguration.ApiMaxPageSize) ? _lgdxRobotCloudConfiguration.ApiMaxPageSize : |
| 0 | 36 | | var (robots, paginationHelper) = await _robotService.GetRobotsAsync(realmId, name, pageNumber, pageSize); |
| 0 | 37 | | Response.Headers.Append("X-Pagination", JsonSerializer.Serialize(paginationHelper)); |
| 0 | 38 | | return Ok(robots.ToDto()); |
| 0 | 39 | | } |
| | 40 | |
|
| | 41 | | [HttpGet("Search")] |
| | 42 | | [ProducesResponseType(typeof(IEnumerable<RobotSearchDto>), StatusCodes.Status200OK)] |
| | 43 | | public async Task<ActionResult<IEnumerable<RobotSearchDto>>> SearchRobots(int realmId, string? name, Guid? robotId) |
| 0 | 44 | | { |
| 0 | 45 | | var robots = await _robotService.SearchRobotsAsync(realmId, name, robotId); |
| 0 | 46 | | return Ok(robots.ToDto()); |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | [HttpGet("{id}", Name = "GetRobot")] |
| | 50 | | [ProducesResponseType(typeof(RobotDto), StatusCodes.Status200OK)] |
| | 51 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 52 | | public async Task<ActionResult<RobotDto>> GetRobot(Guid id) |
| 0 | 53 | | { |
| 0 | 54 | | var robot = await _robotService.GetRobotAsync(id); |
| 0 | 55 | | return Ok(robot.ToDto()); |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | [HttpPost(Name = "CreateRobot")] |
| | 59 | | [ProducesResponseType(typeof(RobotCertificateIssueDto), StatusCodes.Status201Created)] |
| | 60 | | [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] |
| | 61 | | public async Task<ActionResult> CreateRobot(RobotCreateDto robotCreateDto) |
| 0 | 62 | | { |
| 0 | 63 | | var robot = await _robotService.CreateRobotAsync(robotCreateDto.ToBusinessModel()); |
| 0 | 64 | | return CreatedAtAction(nameof(CreateRobot), robot.ToDto()); |
| 0 | 65 | | } |
| | 66 | |
|
| | 67 | | [HttpPatch("{id}/EmergencyStop")] |
| | 68 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | 69 | | [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] |
| | 70 | | public async Task<ActionResult> SetSoftwareEmergencyStopAsync(Guid id, EnableDto data) |
| 0 | 71 | | { |
| 0 | 72 | | if (await _onlineRobotsService.SetSoftwareEmergencyStopAsync(id, data.Enable)) |
| 0 | 73 | | { |
| 0 | 74 | | return NoContent(); |
| | 75 | | } |
| 0 | 76 | | ModelState.AddModelError(nameof(id), "Robot is offline or not found."); |
| 0 | 77 | | return ValidationProblem(); |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | [HttpPatch("{id}/PauseTaskAssigement")] |
| | 81 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | 82 | | [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] |
| | 83 | | public async Task<ActionResult> SetPauseTaskAssigementAsync(Guid id, EnableDto data) |
| 0 | 84 | | { |
| 0 | 85 | | if (await _onlineRobotsService.SetPauseTaskAssigementAsync(id, data.Enable)) |
| 0 | 86 | | { |
| 0 | 87 | | return NoContent(); |
| | 88 | | } |
| 0 | 89 | | ModelState.AddModelError(nameof(id), "Robot is offline or not found."); |
| 0 | 90 | | return ValidationProblem(); |
| 0 | 91 | | } |
| | 92 | |
|
| | 93 | | [HttpPut("{id}")] |
| | 94 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | 95 | | [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] |
| | 96 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 97 | | public async Task<ActionResult> UpdateRobot(Guid id, RobotUpdateDto robotUpdateDto) |
| 0 | 98 | | { |
| 0 | 99 | | if (!await _robotService.UpdateRobotAsync(id, robotUpdateDto.ToBusinessModel())) |
| 0 | 100 | | { |
| 0 | 101 | | return NotFound(); |
| | 102 | | } |
| 0 | 103 | | return NoContent(); |
| 0 | 104 | | } |
| | 105 | |
|
| | 106 | | [HttpPut("{id}/Chassis")] |
| | 107 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | 108 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 109 | | public async Task<ActionResult> UpdateRobotChassisInfo(Guid id, RobotChassisInfoUpdateDto robotChassisInfoUpdateDto) |
| 0 | 110 | | { |
| 0 | 111 | | if (!await _robotService.UpdateRobotChassisInfoAsync(id, robotChassisInfoUpdateDto.ToBusinessModel())) |
| 0 | 112 | | { |
| 0 | 113 | | return NotFound(); |
| | 114 | | } |
| 0 | 115 | | return NoContent(); |
| 0 | 116 | | } |
| | 117 | |
|
| | 118 | | [HttpPost("{id}/TestDelete")] |
| | 119 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | 120 | | [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] |
| | 121 | | public async Task<ActionResult> TestDeleteRobot(Guid id) |
| 0 | 122 | | { |
| 0 | 123 | | await _robotService.TestDeleteRobotAsync(id); |
| 0 | 124 | | return Ok(); |
| 0 | 125 | | } |
| | 126 | |
|
| | 127 | | [HttpDelete("{id}")] |
| | 128 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | 129 | | [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] |
| | 130 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 131 | | public async Task<ActionResult> DeleteRobot(Guid id) |
| 0 | 132 | | { |
| 0 | 133 | | await _robotService.TestDeleteRobotAsync(id); |
| 0 | 134 | | if (!await _robotService.DeleteRobotAsync(id)) |
| 0 | 135 | | { |
| 0 | 136 | | return NotFound(); |
| | 137 | | } |
| 0 | 138 | | return NoContent(); |
| 0 | 139 | | } |
| | 140 | | } |