#!/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 and control 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 def delete(self): """ Shut down every device :return: """ ids = db.session.query(models.Device.id).filter(models.Device.status != models.DeviceStatusEnum.offline) for d_id in ids: mqtt.publish(config.MQTT_COMMAND_TOPIC, {"deviceID": d_id, "command": "offline"}) def post(self): """ Bring every device online :return: """ ids = db.session.query(models.Device.id).filter(models.Device.status != models.DeviceStatusEnum.online) for d_id in ids: mqtt.publish(config.MQTT_COMMAND_TOPIC, {"deviceID": d_id, "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 def delete(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() mqtt.publish(config.MQTT_COMMAND_TOPIC, {"deviceID": device, "command": "offline"}) 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() mqtt.publish(config.MQTT_COMMAND_TOPIC, {"deviceID": device, "command": "offline"}) 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) def delete(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 & & str(models.Sensor.id) == sensorid) mqtt.publish(config.MQTT_COMMAND_TOPIC, {"deviceID": sensor, "sensorID": sensor, "command": "offline"}) 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 & & str(models.Sensor.id) == sensorid) mqtt.publish(config.MQTT_COMMAND_TOPIC, {"deviceID": sensor, "sensorID": sensor, "command": "online"})