This commit is contained in:
parent
b7773c65ef
commit
66799cd380
@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python3
|
||||
import logging
|
||||
from flask import Flask
|
||||
from flask_restful import Api
|
||||
import sentry_sdk
|
||||
@ -85,6 +86,7 @@ def handle_status_message_proxy(*args, **kwargs):
|
||||
with app.app_context():
|
||||
handle_status_message(*args, **kwargs)
|
||||
|
||||
|
||||
api.add_resource(AllDevicesResource, "/devices")
|
||||
api.add_resource(AllDevicesOfflineResource, "/devices/offline")
|
||||
api.add_resource(AllDevicesOnlineResource, "/devices/online")
|
||||
|
@ -1,6 +1,5 @@
|
||||
#!/usr/bin/env python3
|
||||
import logging
|
||||
from mqtt_flask_instance import mqtt
|
||||
from app import app
|
||||
import config
|
||||
from db import db
|
||||
from schemas import DeviceSchema, SensorSchema
|
||||
@ -17,31 +16,29 @@ __version__text__ = "1"
|
||||
deviceschema = DeviceSchema(many=False)
|
||||
sensorschema = SensorSchema(many=False)
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def handle_status_message(client, userdata, message):
|
||||
data = dict(
|
||||
topic=message.topic,
|
||||
payload=message.payload.decode()
|
||||
)
|
||||
LOGGER.info(data)
|
||||
app.logger.info(data)
|
||||
try:
|
||||
ids = data['topic'].replace(f"{config.MQTT_STATUS_TOPIC}/", "").split("/")
|
||||
if len(ids) == 1:
|
||||
data['payload']['deviceID'] = ids[0]
|
||||
status_message = deviceschema.load(data['payload'], session=db.session, transient=True).data
|
||||
LOGGER.info(f"Recieved status message from {data['payload']['deviceID']}, persisting to db.")
|
||||
app.logger.info(f"Recieved status message from {data['payload']['deviceID']}, persisting to db.")
|
||||
db.session.merge(status_message)
|
||||
else:
|
||||
if len(ids) == 2:
|
||||
data['payload']['deviceID'] = ids[0]
|
||||
data['payload']['sensorID'] = ids[1]
|
||||
status_message = sensorschema.load(data['payload'], session=db.session, transient=True).data
|
||||
LOGGER.info(f"Recieved status message from sensor {data['payload']['sensorID']}, persisting to db.")
|
||||
app.logger.info(f"Recieved status message from sensor {data['payload']['sensorID']}, persisting to db.")
|
||||
db.session.merge(status_message)
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
LOGGER.exception(e)
|
||||
app.logger.exception(e)
|
||||
else:
|
||||
db.session.commit()
|
||||
|
@ -1,5 +1,4 @@
|
||||
#!/usr/bin/env python3
|
||||
import logging
|
||||
from flask_restful import Resource
|
||||
from db import db
|
||||
from mqtt_flask_instance import mqtt
|
||||
@ -16,8 +15,6 @@ __copyright__ = "Copyright 2020, Birbnetes Team"
|
||||
__module_name__ = "resources"
|
||||
__version__text__ = "1"
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class AllDevicesResource(Resource):
|
||||
"""
|
||||
|
Loading…
Reference in New Issue
Block a user