Fixed database operations in MQTT calls
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Pünkösd Marcell 2020-09-19 16:39:35 +02:00
parent 23468384d2
commit b7773c65ef
2 changed files with 9 additions and 2 deletions

View File

@ -11,7 +11,7 @@ from db import db
from marshm import ma
from mqtt_flask_instance import mqtt
import mqtt_methods # This file contains a decorator
from mqtt_methods import handle_status_message
"""
Main Flask RESTful API
@ -77,6 +77,14 @@ def handle_connect(client, userdata, flags, rc):
mqtt.subscribe(f"{MQTT_STATUS_TOPIC}/#")
@mqtt.on_topic(f"{config.MQTT_STATUS_TOPIC}/#")
def handle_status_message_proxy(*args, **kwargs):
"""
This proxy puts the call into a Flask context, so database operations can be performed
"""
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")

View File

@ -20,7 +20,6 @@ sensorschema = SensorSchema(many=False)
LOGGER = logging.getLogger(__name__)
@mqtt.on_topic(f"{config.MQTT_STATUS_TOPIC}/#")
def handle_status_message(client, userdata, message):
data = dict(
topic=message.topic,