#!/usr/bin/env python3 import logging from flask_restful import Resource from db import db from mqtt_flask_instance import mqtt import config import models import schemas """ Flask Restful endpoints """ __author__ = '@tormakris' __copyright__ = "Copyright 2020, Birbnetes Team" __module_name__ = "resources" __version__text__ = "1" LOGGER = logging.getLogger(__name__) class AllDevicesResource(Resource): """ Query all known devices """ alldeviceschema = schemas.DeviceSchema(many=True) def get(self): """ Get all stored items :return: """ alldevices = models.Device.query.all() return self.alldeviceschema.dump(list(alldevices)), 200 class AllDevicesOfflineResource(Resource): """ Shut down all devices """ def post(self): """ Shut down every device :return: """ mqtt.publish(f"{config.MQTT_COMMAND_TOPIC}/+", {"command": "offline"}) class AllDevicesOnlineResource(Resource): """ Bring every device online """ def post(self): """ Bring every device online :return: """ mqtt.publish(f"{config.MQTT_COMMAND_TOPIC}/+", {"command": "online"}) class DeviceResource(Resource): """ Query and control a particular device """ deviceschema = schemas.DeviceSchema(many=False) def get(self, deviceid: str): """ Query a device :param deviceid: UUID of device :return: """ device = models.Device.query.filter_by(id=deviceid).first_or_404() return self.deviceschema.dump(device), 200 class DeviceOfflineResrouce(Resource): """ Bring a device offline """ def post(self, deviceid: str): """ Shut down a device :param deviceid: UUID of device :return: """ device = db.session.query(models.Device.id).filter(str(models.Device.id) == deviceid).first_or_404()[0] mqtt.publish(f"{config.MQTT_COMMAND_TOPIC}/{device}", {"command": "offline"}) class DeviceOnlineResrouce(Resource): """ Bring a device online """ def post(self, deviceid: str): """ Bring a device online :param deviceid: UUID of device :return: """ device = db.session.query(models.Device.id).filter(str(models.Device.id) == deviceid).first_or_404()[0] mqtt.publish(f"{config.MQTT_COMMAND_TOPIC}/{device}", {"command": "online"}) class SensorResource(Resource): """ Query and control a particular sensor of a device """ sensorschema = schemas.SensorSchema(many=False) def get(self, deviceid: str, sensorid: str): """ Query a sensor :param deviceid: UUID of device :param sensorid: UUID of sensor :return: """ sensor = models.Sensor.query.filter_by(device_id=deviceid, sensorid=sensorid).first_or_404() return self.sensorschema.dump(sensor) class SensorOfflineResource(Resource): """ Bring a sensor offline """ def post(self, deviceid: str, sensorid: str): """ Shut down a sensor of a device :param deviceid: UUID of device :param sensorid: UUID of sensor :return: """ sensor = db.session.query(models.Sensor.device_id, models.Sensor.id).filter( str(models.Sensor.device_id) == deviceid and str(models.Sensor.id) == sensorid).first_or_404() mqtt.publish(f"{config.MQTT_COMMAND_TOPIC}/{sensor[0]}/{sensor[1]}", {"command": "offline"}) class SensorOnlineResource(Resource): """ Bring a sensor online """ def post(self, deviceid: str, sensorid: str): """ Bring a sensor online :param deviceid: UUID of device :param sensorid: UUID of sensor :return: """ sensor = db.session.query(models.Sensor.device_id, models.Sensor.id).filter( str(models.Sensor.device_id) == deviceid and str(models.Sensor.id) == sensorid).first_or_404() mqtt.publish(f"{config.MQTT_COMMAND_TOPIC}/{sensor[0]}/{sensor[1]}", {"command": "online"})