cnc-service/src/resources.py

152 lines
4.1 KiB
Python
Raw Normal View History

2020-07-20 17:09:05 +02:00
#!/usr/bin/env python3
import logging
2020-07-20 18:19:31 +02:00
from flask_restful import Resource
from db import db
from mqtt_flask_instance import mqtt
import config
import models
import schemas
2020-07-20 17:09:05 +02:00
"""
Flask Restful endpoints
"""
__author__ = '@tormakris'
__copyright__ = "Copyright 2020, Birbnetes Team"
__module_name__ = "resources"
__version__text__ = "1"
2020-07-20 18:19:31 +02:00
LOGGER = logging.getLogger(__name__)
class AllDevicesResource(Resource):
"""
2020-07-26 17:45:19 +02:00
Query all known devices
2020-07-20 18:19:31 +02:00
"""
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
2020-07-26 17:45:19 +02:00
class AllDevicesOfflineResource(Resource):
"""
Shut down all devices
"""
def post(self):
2020-07-20 18:19:31 +02:00
"""
Shut down every device
:return:
"""
2020-07-26 17:45:19 +02:00
mqtt.publish(f"{config.MQTT_COMMAND_TOPIC}/+", {"command": "offline"})
2020-07-20 18:19:31 +02:00
2020-07-26 17:45:19 +02:00
class AllDevicesOnlineResource(Resource):
"""
Bring every device online
"""
2020-07-20 18:19:31 +02:00
def post(self):
"""
Bring every device online
:return:
"""
2020-07-26 17:45:19 +02:00
mqtt.publish(f"{config.MQTT_COMMAND_TOPIC}/+", {"command": "online"})
2020-07-20 18:19:31 +02:00
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
2020-07-26 17:45:19 +02:00
class DeviceOfflineResrouce(Resource):
"""
Bring a device offline
"""
def post(self, deviceid: str):
2020-07-20 18:19:31 +02:00
"""
Shut down a device
:param deviceid: UUID of device
:return:
"""
2020-07-20 18:26:49 +02:00
device = db.session.query(models.Device.id).filter(str(models.Device.id) == deviceid).first_or_404()[0]
2020-07-26 17:45:19 +02:00
mqtt.publish(f"{config.MQTT_COMMAND_TOPIC}/{device}", {"command": "offline"})
2020-07-20 18:19:31 +02:00
2020-07-26 17:45:19 +02:00
class DeviceOnlineResrouce(Resource):
"""
Bring a device online
"""
2020-07-20 18:19:31 +02:00
def post(self, deviceid: str):
"""
Bring a device online
:param deviceid: UUID of device
:return:
"""
2020-07-20 18:26:49 +02:00
device = db.session.query(models.Device.id).filter(str(models.Device.id) == deviceid).first_or_404()[0]
2020-07-26 17:45:19 +02:00
mqtt.publish(f"{config.MQTT_COMMAND_TOPIC}/{device}", {"command": "online"})
2020-07-20 18:19:31 +02:00
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)
2020-07-26 17:45:19 +02:00
class SensorOfflineResource(Resource):
"""
Bring a sensor offline
"""
def post(self, deviceid: str, sensorid: str):
2020-07-20 18:19:31 +02:00
"""
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(
2020-07-20 18:26:49 +02:00
str(models.Sensor.device_id) == deviceid and str(models.Sensor.id) == sensorid).first_or_404()
2020-07-26 17:45:19 +02:00
mqtt.publish(f"{config.MQTT_COMMAND_TOPIC}/{sensor[0]}/{sensor[1]}", {"command": "offline"})
2020-07-20 18:19:31 +02:00
2020-07-26 17:45:19 +02:00
class SensorOnlineResource(Resource):
"""
Bring a sensor online
"""
2020-07-20 18:19:31 +02:00
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(
2020-07-20 18:26:49 +02:00
str(models.Sensor.device_id) == deviceid and str(models.Sensor.id) == sensorid).first_or_404()
2020-07-26 17:45:19 +02:00
mqtt.publish(f"{config.MQTT_COMMAND_TOPIC}/{sensor[0]}/{sensor[1]}", {"command": "online"})