Move tasks to owning domains
parent
27fbe9d886
commit
6f5374ec22
|
@ -1,42 +1,9 @@
|
||||||
# App initialization
|
# App initialization
|
||||||
import sys
|
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
from .tasks import celery as celery_configurator
|
from .tasks.celery_configurator import make_celery
|
||||||
|
|
||||||
app = Flask(__name__, instance_relative_config=True)
|
app = Flask(__name__, instance_relative_config=True)
|
||||||
app.config.from_object('config')
|
app.config.from_object('config')
|
||||||
app.config.from_pyfile('config.py', silent=True)
|
app.config.from_pyfile('config.py', silent=True)
|
||||||
app.config['MQTT_CLIENT_ID'] = 'final-iot-backend-server-worker'
|
app.config['MQTT_CLIENT_ID'] = 'final-iot-backend-server-worker'
|
||||||
celery = celery_configurator.make_celery(app)
|
task_builder = 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
|
|
||||||
|
|
|
@ -45,7 +45,7 @@ def set_device_configuration(device_id, configuration_json):
|
||||||
:type configuration_json: JSON
|
:type configuration_json: JSON
|
||||||
:rtype: Boolean
|
:rtype: Boolean
|
||||||
"""
|
"""
|
||||||
from app.celery_builder import send_config
|
from .tasks import send_config
|
||||||
device = Device.get(id=device_id)
|
device = Device.get(id=device_id)
|
||||||
device.configuration = configuration_json
|
device.configuration = configuration_json
|
||||||
device.save()
|
device.save()
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
import sys
|
||||||
|
from app.celery_builder import task_builder
|
||||||
|
from .blueprint import devices_bp
|
||||||
|
|
||||||
|
|
||||||
|
@task_builder.task()
|
||||||
|
def send_config(device_id, config):
|
||||||
|
from flask_mqtt import Mqtt, MQTT_ERR_SUCCESS
|
||||||
|
mqtt = Mqtt(devices_bp)
|
||||||
|
|
||||||
|
@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
|
|
@ -28,7 +28,7 @@ CSRF_SESSION_KEY = "secret"
|
||||||
SECRET_KEY = "?['Z(Z\x83Y \x06T\x12\x96<\xff\x12\xe0\x1b\xd1J\xe0\xd9ld"
|
SECRET_KEY = "?['Z(Z\x83Y \x06T\x12\x96<\xff\x12\xe0\x1b\xd1J\xe0\xd9ld"
|
||||||
|
|
||||||
# MQTT configuration
|
# MQTT configuration
|
||||||
MQTT_CLIENT_ID = 'final-iot-backend-server'
|
MQTT_CLIENT_ID = 'final-iot-backend-server-local2'
|
||||||
MQTT_BROKER_URL = 'broker.hivemq.com'
|
MQTT_BROKER_URL = 'broker.hivemq.com'
|
||||||
MQTT_BROKER_PORT = 1883
|
MQTT_BROKER_PORT = 1883
|
||||||
MQTT_USERNAME = 'user'
|
MQTT_USERNAME = 'user'
|
||||||
|
|
Loading…
Reference in New Issue