diff --git a/app/celery_builder.py b/app/celery_builder.py index c3126e0..5a54ede 100644 --- a/app/celery_builder.py +++ b/app/celery_builder.py @@ -1,4 +1,5 @@ # App initialization +import sys from flask import Flask from .tasks import celery as celery_configurator @@ -7,3 +8,35 @@ app.config.from_object('config') app.config.from_pyfile('config.py', silent=True) app.config['MQTT_CLIENT_ID'] = 'final-iot-backend-server-worker' celery = celery_configurator.make_celery(app) + + +@celery.task() +def send_config(device_id, config): + from flask_mqtt import Mqtt, MQTT_ERR_SUCCESS + mqtt = Mqtt(app) + + @mqtt.on_log() + def handle_logging(client, userdata, level, buf): + print(level, buf) + + @mqtt.on_connect() + def handle_connect(client, userdata, flags, rc): + print('MQTT worker client connected') + print("Sending configuration to device: " + str(device_id)) + print("Configuration: " + str(config)) + topic = 'device/' + str(device_id) + '/config' + print("Targeting topic: " + topic) + try: + (result, mid) = mqtt.publish(topic, config, 2) + if (result == MQTT_ERR_SUCCESS): + print("Success!!!") + print("Result: " + str(result)) + print("Message id: " + str(mid)) + mqtt.client.disconnect() + except Exception: + print("ERROR!") + error_type, error_instance, traceback = sys.exc_info() + print("Type: " + str(error_type)) + print("Instance: " + str(error_instance)) + mqtt.client.disconnect() + return diff --git a/app/devices/__init__.py b/app/devices/__init__.py index 6f76473..30a5ec2 100644 --- a/app/devices/__init__.py +++ b/app/devices/__init__.py @@ -48,11 +48,11 @@ def set_device_configuration(device_id, configuration_json): :type configuration_json: JSON :rtype: Boolean """ - from app.mqtt.mqtt_client import MqttClient + from app.celery_builder import send_config device = Device.get(id=device_id) device.configuration = configuration_json device.save() - MqttClient.send_config(device_id, str(configuration_json)) + send_config.delay(device_id, str(configuration_json)) def get_device_configuration(device_id): diff --git a/app/mqtt/mqtt_client.py b/app/mqtt/mqtt_client.py index c5c7000..283e01a 100644 --- a/app/mqtt/mqtt_client.py +++ b/app/mqtt/mqtt_client.py @@ -1,6 +1,6 @@ import sys import json -from flask_mqtt import Mqtt, MQTT_ERR_SUCCESS +from flask_mqtt import Mqtt import app.devices as devices @@ -74,22 +74,3 @@ class MqttClient: return int(device_id) else: raise ValueError("Topic is in invalid format") - - @staticmethod - def send_config(device_id, config): - print("Sending configuration to device: " + str(device_id)) - print("Configuration: " + str(config)) - topic = 'device/' + str(device_id) + '/config' - print("Targeting topic: " + topic) - try: - (result, mid) = MqttClient.mqtt.publish(topic, config, 2) - if (result == MQTT_ERR_SUCCESS): - print("Success!!!") - print("Result: " + str(result)) - print("Message id: " + str(mid)) - except Exception: - print("ERROR!") - error_type, error_instance, traceback = sys.exc_info() - print("Type: " + str(error_type)) - print("Instance: " + str(error_instance)) - return