From 1c44ac4a30c80d5c63d9b0e20593228b1a6f3ec3 Mon Sep 17 00:00:00 2001 From: Amund Isaksen Date: Mon, 9 Dec 2024 08:49:02 +0100 Subject: [PATCH] Add env variable to configure ingest topic prepend string --- ingest/api/send_mqtt.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ingest/api/send_mqtt.py b/ingest/api/send_mqtt.py index 5baacee9..c70714b6 100644 --- a/ingest/api/send_mqtt.py +++ b/ingest/api/send_mqtt.py @@ -1,3 +1,4 @@ +import os import logging import json from paho.mqtt import client as mqtt_client @@ -5,9 +6,12 @@ logger = logging.getLogger(__name__) +if "MQTT_TOPIC_PREPEND" in os.environ: + mqtt_topic_prepend = os.getenv("MQTT_TOPIC_PREPEND", "") + mqtt_topic_prepend = mqtt_topic_prepend if not mqtt_topic_prepend.endswith("/") else mqtt_topic_prepend + "/" -def connect_mqtt(mqtt_conf: dict): +def connect_mqtt(mqtt_conf: dict): def on_connect(client, userdata, flags, rc, properties=None): if rc == 0: logger.info("Connected to MQTT Broker!") @@ -34,7 +38,7 @@ def on_disconnect(client, userdata, flags, rc, properties): def send_message(topic: str, message: str, client: object): if len(topic) != 0: - mqtt_topic = topic + mqtt_topic = mqtt_topic_prepend + topic try: if isinstance(message, dict): client.publish(mqtt_topic, json.dumps(message))