Skip to content
telescope.py 8.37 KiB
Newer Older
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'''REST API for Telescope related operations'''

# System modules
from datetime import datetime

from flask import request
from utils.coordinates import to_hms, to_hms_dms, to_radec
Davide Ricci's avatar
Davide Ricci committed
from .baseresource import ResourceDev


# @api.route("/cover")
Davide Ricci's avatar
Davide Ricci committed
class Cover(ResourceDev):
    '''Petals covering the primary mirror.'''
            "response": self.dev.cover,
            "error": self.dev.error,
            "timestamp": self.timestamp,
vertighel's avatar
vertighel committed

# @api.route("/cover/movement")
Davide Ricci's avatar
Davide Ricci committed
class CoverMovement(ResourceDev):
    '''Manage the primary mirror cover petals.'''
        '''Set a new position for cover petals
        target = request.json # api.payload
        self.dev.open = target
            "response": self.dev.open,
            "error": self.dev.error,
            "timestamp": self.timestamp,
# @api.route("/error")
Davide Ricci's avatar
Davide Ricci committed
class Error(ResourceDev):
            "response": self.dev.state,  # state is global
            "error": self.dev.error,
            "timestamp": self.timestamp,
        error_number = request.json
            "response": self.dev.clear(error_number),
            "error": self.dev.error,
            "timestamp": self.timestamp,
# @api.route("/error/details")
Davide Ricci's avatar
Davide Ricci committed
class ErrorDetails(ResourceDev):
    '''Get the global status to have more info.'''

    def get(self):
        '''Check if there are errors.'''
        res = {
            "response": self.dev.status,  # state is global
            "error": self.dev.error,
            "timestamp": self.timestamp,
# @api.route("/clock")
Davide Ricci's avatar
Davide Ricci committed
class Clock(ResourceDev):
        '''Get the telescope time from GPS.'''
        res = {
            "response": self.dev.clock,
            "error": self.dev.error,
            "timestamp": self.timestamp,
# @api.route("/coordinates")
Davide Ricci's avatar
Davide Ricci committed
class Coordinates(ResourceDev):
        '''Get the telescope coordinates.'''
        coor = self.dev.coordinates
        lst = coor["lst"]
        ra = coor["radec"][0]
        coor["ha"] = to_hms(lst-ra)
        coor["radec"] = to_hms_dms(coor["radec"])
        coor["lst"] = to_hms(coor["lst"])
        utc = coor["utc"]
        coor["utc"] = datetime.fromtimestamp(utc).isoformat()
Davide Ricci's avatar
Davide Ricci committed

            "error": self.dev.error,
            "timestamp": self.timestamp,
# @api.route("/coordinates/movement")
Davide Ricci's avatar
Davide Ricci committed
class CoordinatesMovement(ResourceDev):
    '''Manage the pointing of the telescope.'''
        '''Check the status of the telescope movement.'''
        res = {
            "response": self.dev.is_moving,
            "error": self.dev.error,
            "timestamp": self.timestamp,
            "response": self.dev.abort(),
            "error": self.dev.error,
            "timestamp": self.timestamp,
# @api.route("/coordinates/movement/radec")
Davide Ricci's avatar
Davide Ricci committed
class CoordinatesMovementRadec(ResourceDev):
        target = request.json
Davide Ricci's avatar
Davide Ricci committed

Davide Ricci's avatar
Davide Ricci committed

        print(radec)
        self.dev.radec = radec
            "response": self.dev.radec,
            "error": self.dev.error,
            "timestamp": self.timestamp,
# @api.route("/coordinates/movement/altaz")
Davide Ricci's avatar
Davide Ricci committed
class CoordinatesMovementAltaz(ResourceDev):
        target = request.json
        self.dev.altaz = target
            "response": self.dev.altaz,
            "error": self.dev.error,
            "timestamp": self.timestamp,
# @api.route("/coordinates/movement/atpark")
Davide Ricci's avatar
Davide Ricci committed
class CoordinatesMovementAtpark(ResourceDev):
    '''Park the telescope.'''

    def get(self):
        '''Send the telescope to park position.'''
Davide Ricci's avatar
Davide Ricci committed
        parked = self.dev.park
            "raw": parked,
            "response": constants.yes_no[parked],
            "error": self.dev.error,
            "timestamp": self.timestamp,
Davide Ricci's avatar
Davide Ricci committed

# @api.route("/coordinates/movement/park")
Davide Ricci's avatar
Davide Ricci committed
class CoordinatesMovementPark(ResourceDev):
        '''Send the telescope to park position.'''
        self.dev.park = True
            "response": self.dev.park,
            "error": self.dev.error,
            "timestamp": self.timestamp,
# @api.route("/coordinates/movement/unpark")
Davide Ricci's avatar
Davide Ricci committed
class CoordinatesMovementUnpark(ResourceDev):
        '''Remove the telescope from park position.'''
        self.dev.park = False
        res = {
            "response": self.dev.park,
            "error": self.dev.error,
            "timestamp": self.timestamp,
# @api.route("/coordinates/offset")
Davide Ricci's avatar
Davide Ricci committed
class CoordinatesOffset(ResourceDev):
        '''Retrieve the telescope offset in Zdist, Az.'''
            "response": self.dev.offset,
            "error": self.dev.error,
            "timestamp": self.timestamp,
        '''Apply to the telescope new offsets in degrees.'''
        offset = request.json
        self.dev.offset = offset
            "response": self.dev.offset,
            "error": self.dev.error,
            "timestamp": self.timestamp,
# @api.route("/coordinates/tracking")
Davide Ricci's avatar
Davide Ricci committed
class CoordinatesTracking(ResourceDev):
    '''Enable or disable the telescope tracking.'''
        '''Check if the telescope is tracking or not.'''
            "raw": self.dev.tracking,
            "response": constants.yes_no[self.dev.tracking],
            "error": self.dev.error,
            "timestamp": self.timestamp,
        '''Set the telescope in tracking mode or not'''
        tracking = request.json
        self.dev.tracking = tracking
            "response": self.dev.tracking,
            "error": self.dev.error,
            "timestamp": self.timestamp,
# @api.route("/connection")
Davide Ricci's avatar
Davide Ricci committed
class Connection(ResourceDev):
    '''Manage the connection to ASCOM.'''

    def get(self):
        '''Check if the telescope is connected to ASCOM.'''
            "raw": self.dev.connection,
            "response": constants.yes_no[self.dev.connection],
            "error": self.dev.error,
            "timestamp": self.timestamp,
        }
        return res

    def put(self):
        '''Connect or disconnect the telescope to ASCOM.'''
        connection = request.json
        self.dev.connection = connection
            "raw": self.dev.connection,
            "response": constants.yes_no[self.dev.connection],
            "error": self.dev.error,
            "timestamp": self.timestamp,
Davide Ricci's avatar
Davide Ricci committed
#         clock = Clock(dev=self.dev).get()
#         if clock["response"] and type(clock["response"]) != str:
Davide Ricci's avatar
Davide Ricci committed
#             # 2) astelOS has data from the GPS
Davide Ricci's avatar
Davide Ricci committed
#             connection = Connection(dev=self.dev).get()
#             if connection["response"]:
Davide Ricci's avatar
Davide Ricci committed
#                 # 3) Telescope manually connected to ASCOM

#             else:

#                 # 3) No telescope ASCOM connection

#         else:
#             # 2) AstelOS not ready

#         return res