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

'''REST API for Switch related operations'''

# System modules

# Third-party modules
from flask import request

# Custom modules
from config import constants
from .baseresource import ResourceDev
# @api.route("/dome/light")
# @api.route("/telescope/lamp")
# @api.route("/camera/power")
class State(ResourceDev):
    '''Manage a switch state.'''

    def __init__(self, *args, **kwargs):
        '''Constructor.'''
Davide Ricci's avatar
Davide Ricci committed

        super().__init__(self, *args, **kwargs)
        self.dev = kwargs["dev"]

    def get(self):
        '''Check if the switch on or off.'''
        res = {
            "raw": self.dev.state,
            "response": constants.on_off[self.dev.state],
            "error": self.dev.error,
            "timestamp": self.timestamp,
        }
        return res

    def put(self):
        '''Switch on or off.'''
        state = request.json
        self.dev.state = state
        res = {
            "raw": self.dev.state,
            "response": constants.on_off[self.dev.state],
            "error": self.dev.error,
            "timestamp": self.timestamp,