From cb0c604253439b7614f7db0a8fd31167431173b7 Mon Sep 17 00:00:00 2001 From: Amund Isaksen <31344542+Teddy-1000@users.noreply.github.com> Date: Fri, 13 Sep 2024 11:54:40 +0200 Subject: [PATCH] Handle encoding of mqtt message (#166) --- ingest/api/send_mqtt.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ingest/api/send_mqtt.py b/ingest/api/send_mqtt.py index c0a2b4dd..5baacee9 100644 --- a/ingest/api/send_mqtt.py +++ b/ingest/api/send_mqtt.py @@ -1,4 +1,5 @@ import logging +import json from paho.mqtt import client as mqtt_client from fastapi import HTTPException @@ -35,8 +36,13 @@ def send_message(topic: str, message: str, client: object): if len(topic) != 0: mqtt_topic = topic try: - client.publish(mqtt_topic, message) + if isinstance(message, dict): + client.publish(mqtt_topic, json.dumps(message)) + elif isinstance(message, (str, bytes)): + client.publish(mqtt_topic, message) + else: + raise TypeError("Mqtt message of unknown type") except Exception as e: - logger.critical(str(e)) + logger.critical(str(e), message, type(message)) raise HTTPException(status_code=500, detail="Failed to publish to mqtt")