Skip to content

Commit

Permalink
Use tasks for dome movements and emergency shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Jul 25, 2024
1 parent 3352cc0 commit 7a73297
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 48 deletions.
42 changes: 10 additions & 32 deletions src/lvmapi/routers/enclosure.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@

from __future__ import annotations

import asyncio

from typing import Any

from fastapi import APIRouter, HTTPException, Request
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel, field_validator

from lvmapi.tools.gort import get_gort_client
from lvmapi.tasks import move_dome_task
from lvmapi.tools.rabbitmq import send_clu_command


Expand Down Expand Up @@ -99,39 +97,19 @@ async def status() -> EnclosureStatus:


@router.get("/open")
async def open_enclosure(request: Request, block: bool = True):
"""Opens the enclosure."""

try:
# We use GORT here even if it's a bit slower because it will make sure
# the telescopes are pointing down before moving the dome.
async with get_gort_client(request.app) as gort:
task = asyncio.create_task(gort.enclosure.open())

if block:
await task
async def open_enclosure():
"""Opens the enclosure. Scheduled as a task."""

except Exception as ee:
raise HTTPException(status_code=500, detail=str(ee))

return True
task = await move_dome_task.kiq(direction="open")
return task.task_id


@router.get("/close")
async def close_enclosure(request: Request, block: bool = True, force: bool = False):
"""Closes the enclosure."""
async def close_enclosure(force: bool = False):
"""Closes the enclosure. Scheduled as a task."""

try:
async with get_gort_client(request.app) as gort:
task = asyncio.create_task(gort.enclosure.close(force=force))

if block:
await task

except Exception as ee:
raise HTTPException(status_code=500, detail=str(ee))

return True
task = await move_dome_task.kiq(direction="close", force=force)
return task.task_id


@router.get("/stop")
Expand Down
23 changes: 7 additions & 16 deletions src/lvmapi/routers/macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,17 @@

from __future__ import annotations

import asyncio
from fastapi import APIRouter

from fastapi import APIRouter, HTTPException, Request

from lvmapi.tools.gort import get_gort_client
from lvmapi.tasks import shutdown_task


router = APIRouter(prefix="/macros", tags=["macros"])


@router.get("/shutdown")
async def shutdown(request: Request, block: bool = True) -> bool:
"""Performs an emergency shutdown of the enclosure and telescopes."""

try:
async with get_gort_client(request.app) as gort:
task = asyncio.create_task(gort.shutdown(park_telescopes=True))
if block:
await task
except Exception as ee:
raise HTTPException(status_code=500, detail=str(ee))

return True
async def shutdown_route() -> str:
"""Schedules an emergency shutdown of the enclosure and telescopes."""

task = await shutdown_task.kiq()
return task.task_id

0 comments on commit 7a73297

Please sign in to comment.