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

mac_lorenzo's avatar
mac_lorenzo committed

Davide Ricci's avatar
Davide Ricci committed
from flask_restx import Namespace, Resource

# Custom modules
import devices

mac_lorenzo's avatar
mac_lorenzo committed
###########################
# REST API
############################

vertighel's avatar
vertighel committed
api = Namespace(
    'environment', description='Environment data from multiple devices')
@api.route("/status")
class Status(Resource):
    """Asks all the environment data"""

    def get(self):
        """Downloads all the environment values available """
        meteo = Meteo().get()
        internal = Internal().get()
        # meteo.update(internal)
        res = {"external": meteo, "internal": internal}

        return res
vertighel's avatar
vertighel committed

@api.route("/meteo")
class Meteo(Resource):
    """Asks all the meteo station data"""

    @api.response(200, 'Success')
    def get(self):
        """Downloads the current meteo station values"""
vertighel's avatar
vertighel committed
        return devices.met.data
vertighel's avatar
vertighel committed

@api.route("/meteo/clock")
class MeteoClock(Resource):
    """Manages the meteo panel clock"""

    @api.response(200, 'Success')
    def get(self):
        """Retrieve the current date and time of the panel"""
vertighel's avatar
vertighel committed
        return devices.met.time

    @api.response(200, 'Success')
    def put(self):
        """Syncronize the panel date and time with the pc calling this."""
vertighel's avatar
vertighel committed
        return devices.met.synctime()
vertighel's avatar
vertighel committed

@api.route("/internal")
class Internal(Resource):
    """Asks all the meteo station data"""

    @api.response(200, 'Success')
    def get(self):
        """Downloads the current meteo station values"""
vertighel's avatar
vertighel committed

        try:
Davide Ricci's avatar
Davide Ricci committed
            telescope_temperatures = devices.tel_temp.temperature
            if not telescope_temperatures:
                print("telescope_temperatures device error!")
Davide Ricci's avatar
Davide Ricci committed
                telescope_temperatures = [None, None, None]
        except Exception as e:
            print("telescope_temperatures exception error:")
            print(e)
vertighel's avatar
vertighel committed
            telescope_temperatures = [None, None, None]
        # try:
Davide Ricci's avatar
Davide Ricci committed
        #     if devices.sof.state:
        #         camera_ambient = devices.cam.ambient
Davide Ricci's avatar
Davide Ricci committed
        #     camera_ambient = None
        # except Exception as e:
        #     log.debug(f"Environment camera_ambient error: {e}")
        #     camera_ambient = None
vertighel's avatar
vertighel committed

        res = {
            # "Temperature_dome": camera_ambient,
vertighel's avatar
vertighel committed
            "Temperature_M1": telescope_temperatures[0],
            "Temperature_M2": telescope_temperatures[1],
            "Temperature_cabinet": telescope_temperatures[2],
vertighel's avatar
vertighel committed

Davide Ricci's avatar
Davide Ricci committed
            "Temperature_fork": devices.fork.temperature,
            "Humidity_fork": devices.fork.humidity,
            "Time_fork_data": devices.fork.last_update,
vertighel's avatar
vertighel committed

            "Temperature_rack": devices.rack.temperature,
            "Humidity_rack": devices.rack.humidity,
            "Time_rack_data": devices.rack.last_update,
vertighel's avatar
vertighel committed

            "Temperature_reception": devices.rec.temperature,
            "Humidity_reception": devices.rec.humidity,
            "Time_reception_data": devices.rec.last_update,
vertighel's avatar
vertighel committed
        }

        return res