flask endpoints done
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Torma Kristóf 2020-07-20 18:19:31 +02:00
parent 009e3e4933
commit 422a7efa6f
Signed by: tormakris
GPG Key ID: DC83C4F2C41B1047
4 changed files with 122 additions and 4 deletions

View File

@ -28,7 +28,8 @@ class SensorStatusEnum(StatusEnum):
class Device(db.Model): class Device(db.Model):
__tablename__ = 'device' __tablename__ = 'device'
id = db.Column(db.UUID, primary_key=True) id = db.Column(db.Integer, primary_key=True)
sensorid = db.Column(db.UUID, nullable=False)
status = db.Column(db.Enum(DeviceStatusEnum), nullable=False) status = db.Column(db.Enum(DeviceStatusEnum), nullable=False)
url = db.Column(db.String, nullable=False) url = db.Column(db.String, nullable=False)
lastupdate = db.Column(db.TIMESTAMP, nullable=False, server_default=func.now(), onupdate=func.current_timestamp()) lastupdate = db.Column(db.TIMESTAMP, nullable=False, server_default=func.now(), onupdate=func.current_timestamp())

View File

@ -16,6 +16,9 @@ __version__text__ = "1"
deviceschema = DeviceSchema(many=False) deviceschema = DeviceSchema(many=False)
LOGGER = logging.getLogger(__name__)
@mqtt.on_message() @mqtt.on_message()
def handle_status_message(client, userdata, message): def handle_status_message(client, userdata, message):
data = dict( data = dict(
@ -24,10 +27,10 @@ def handle_status_message(client, userdata, message):
) )
try: try:
status_message = deviceschema.load(data['payload'], session=db.session, transient=True).data status_message = deviceschema.load(data['payload'], session=db.session, transient=True).data
logging.info(f"Recieved status message from {data['payload']['id']}, persisting to db.") LOGGER.info(f"Recieved status message from {data['payload']['id']}, persisting to db.")
db.session.merge(status_message) db.session.merge(status_message)
except Exception as e: except Exception as e:
db.session.rollback() db.session.rollback()
logging.exception(e) LOGGER.exception(e)
else: else:
db.session.commit() db.session.commit()

View File

@ -1,6 +1,11 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import logging 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 Flask Restful endpoints
@ -10,3 +15,111 @@ __author__ = '@tormakris'
__copyright__ = "Copyright 2020, Birbnetes Team" __copyright__ = "Copyright 2020, Birbnetes Team"
__module_name__ = "resources" __module_name__ = "resources"
__version__text__ = "1" __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"})

View File

@ -21,6 +21,7 @@ class SensorSchema(ma.SQLAlchemyAutoSchema):
class Meta: class Meta:
model = models.Sensor model = models.Sensor
include_fk = True include_fk = True
exclude = ('id',)
class DeviceSchema(ma.SQLAlchemyAutoSchema): class DeviceSchema(ma.SQLAlchemyAutoSchema):