cnc-service/src/mqtt_methods.py

31 lines
868 B
Python
Raw Normal View History

2020-07-20 17:09:05 +02:00
#!/usr/bin/env python3
2020-10-14 20:43:36 +02:00
from flask import current_app as app
2020-07-26 17:45:19 +02:00
import config
2020-07-20 17:09:05 +02:00
"""
MQTT Stuff
"""
__author__ = "@tormakris"
__copyright__ = "Copyright 2020, Birbnetes Team"
__module_name__ = "mqtt_methods"
__version__text__ = "1"
2020-07-20 18:19:31 +02:00
2020-07-20 17:09:05 +02:00
def handle_status_message(client, userdata, message):
data = dict(
topic=message.topic,
payload=message.payload.decode()
)
2020-09-20 02:14:41 +02:00
app.logger.info(data)
2020-07-20 17:09:05 +02:00
try:
2020-07-26 17:45:19 +02:00
ids = data['topic'].replace(f"{config.MQTT_STATUS_TOPIC}/", "").split("/")
if len(ids) == 1:
2021-06-12 20:06:18 +02:00
app.logger.info(f"Recieved status message from {ids[0]} it was: {data['payload']}.")
2020-07-26 17:45:19 +02:00
else:
if len(ids) == 2:
2021-06-12 20:06:18 +02:00
app.logger.info(
f"Recieved status message from sensor {ids[1]} on device {ids[0]} it was: {data['payload']}.")
2020-07-20 17:09:05 +02:00
except Exception as e:
2020-09-20 02:14:41 +02:00
app.logger.exception(e)