Skip to content

Commit

Permalink
Add possibility to returning the robot home in robot page
Browse files Browse the repository at this point in the history
  • Loading branch information
mrica-equinor committed Mar 8, 2024
1 parent 60e9912 commit da7e58e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
50 changes: 50 additions & 0 deletions backend/api/Controllers/ReturnToHomeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Api.Controllers.Models;
using Api.Database.Models;
using Api.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Api.Controllers
{
[ApiController]
[Route("return-to-home")]
public class ReturnToHomeController(
ILogger<RobotController> logger,
IReturnToHomeService returnToHomeService,
IRobotService robotService
) : ControllerBase
{
/// <summary>
/// Sends the robots to their home.
/// </summary>
[HttpPost("schedule-return-to-home/{robotId}")]
[Authorize(Roles = Role.User)]
[ProducesResponseType(typeof(MissionRun), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<IList<MissionRun>>> ScheduleReturnToHomeMission(
[FromRoute] string robotId
)
{
var robot = await robotService.ReadById(robotId);
if (robot is null)
{
logger.LogWarning("Could not find robot with id={Id}", robotId);
return NotFound();
}

var returnToHomeMission = await returnToHomeService.ScheduleReturnToHomeMissionRunIfNotAlreadyScheduledOrRobotIsHome(robot.Id);
if (returnToHomeMission is null)
{
string errorMessage = "Error while scheduling Return to Home mission";
logger.LogError(errorMessage);
return StatusCode(StatusCodes.Status502BadGateway, $"{errorMessage}");
}

return Ok(returnToHomeMission);

}

}
}
11 changes: 11 additions & 0 deletions frontend/src/api/ApiCaller.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -436,4 +436,15 @@ export class BackendAPICaller {
const result = await this.POST<unknown, unknown>(path, body).catch(BackendAPICaller.handleError('POST', path))
return result.content
}

static async returnRobotToHome(robotId: string) {
const path: string = `return-to-home/schedule-return-to-home/` + robotId
const body = {}

const result = await BackendAPICaller.POST<unknown, unknown>(path, body).catch((e) => {
console.error(`Failed to POST /${path}: ` + e)
throw e
})
return result.content
}
}
12 changes: 11 additions & 1 deletion frontend/src/components/Pages/RobotPage/RobotPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Typography } from '@equinor/eds-core-react'
import { Button, Typography } from '@equinor/eds-core-react'
import { useParams } from 'react-router-dom'
import styled from 'styled-components'
import { BackButton } from 'utils/BackButton'
Expand All @@ -12,6 +12,7 @@ import { RobotStatus } from 'models/Robot'
import { useLanguageContext } from 'components/Contexts/LanguageContext'
import { RobotType } from 'models/RobotModel'
import { useRobotContext } from 'components/Contexts/RobotContext'
import { BackendAPICaller } from 'api/ApiCaller'

const StyledRobotPage = styled.div`
display: flex;
Expand Down Expand Up @@ -47,6 +48,12 @@ export const RobotPage = () => {

const selectedRobot = enabledRobots.find((robot) => robot.id === robotId)

const returnRobotToHome = () => {
if (robotId) {
BackendAPICaller.returnRobotToHome(robotId)
}
}

return (
<>
<Header page={'robot'} />
Expand Down Expand Up @@ -82,6 +89,9 @@ export const RobotPage = () => {
</>
)}
<RobotStatusChip status={selectedRobot.status} />
<Button variant="outlined" onClick={returnRobotToHome}>
{TranslateText('Return robot to home')}
</Button>
</VerticalContent>
</RobotInfo>

Expand Down

0 comments on commit da7e58e

Please sign in to comment.