Skip to content

Commit

Permalink
Basic location complete post route for task
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Chong <[email protected]>
  • Loading branch information
aaronchongth committed May 23, 2024
1 parent 907f739 commit a18d89b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
7 changes: 6 additions & 1 deletion packages/api-server/api_server/models/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,10 @@ class Tier(str, Enum):

async def save(self) -> None:
await ttm.AlertRequest.update_or_create(
{"data": self.json(), "task_id": self.task_id}, id=self.id
{
"data": self.json(),
"response_expected": (len(self.responses_available) > 0),
"task_id": self.task_id,
},
id=self.id,
)
2 changes: 1 addition & 1 deletion packages/api-server/api_server/routes/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async def create_new_alert(alert: AlertRequest):
await ttm.AlertRequest.create(
id=alert.id,
data=alert.json(),
response_expected=(len(alert.responses_available) > 0 and alert.display),
response_expected=(len(alert.responses_available) > 0),
task_id=alert.task_id,
)

Expand Down
44 changes: 44 additions & 0 deletions packages/api-server/api_server/routes/tasks/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from api_server.repositories import RmfRepository, TaskRepository
from api_server.response import RawJSONResponse
from api_server.rmf_io import task_events, tasks_service
from api_server.routes.alerts import get_alerts_of_task, respond_to_alert
from api_server.routes.building_map import get_building_map

router = FastIORouter(tags=["Tasks"])
Expand Down Expand Up @@ -391,3 +392,46 @@ async def post_undo_skip_phase(
request: mdl.UndoPhaseSkipRequest = Body(...),
):
return RawJSONResponse(await tasks_service().call(request.json(exclude_none=True)))


@router.post("/location_complete")
async def location_complete(
task_id: str,
location: str,
success: bool,
logger: LoggerAdapter = Depends(get_logger),
):
alerts = await get_alerts_of_task(task_id=task_id, unresponded=True)
if len(alerts) == 0:
raise HTTPException(
404, f"There are no locations awaiting completion for task {task_id}"
)

# TODO: not hardcode the expected responses from the fleet adapter
# TODO: not hardcode the alert parameter expected from the fleet adapter
TimeoutResponse = "timeout"
MoveOffResponse = "move_off"
ParameterName = "waiting_at"

for alert in alerts:
if (
len(alert.alert_parameters) == 0
or TimeoutResponse not in alert.responses_available
or MoveOffResponse not in alert.responses_available
):
continue

# TODO: make sure that there are no duplicated locations that have
# not been responded to yet
for param in alert.alert_parameters:
if param.name == ParameterName and param.value == location:
response = await respond_to_alert(
alert_id=alert.id,
response=TimeoutResponse if not success else MoveOffResponse,
)
logger.info(response)
return

error = f"Task {task_id} is not awaiting completion of location {location}"
logger.error(error)
raise HTTPException(404, error)

0 comments on commit a18d89b

Please sign in to comment.