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