From 817eda7d03628ecbde887a07a8410b25dd3dfa6d Mon Sep 17 00:00:00 2001 From: Roby Date: Mon, 27 Apr 2026 17:57:51 +0200 Subject: [PATCH] feat(mqtt): make subscription topics configurable via config.json --- app.py | 28 ++++++++++++++++------------ config.json.example | 14 +++++++++++++- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/app.py b/app.py index 951dcf7..574169d 100644 --- a/app.py +++ b/app.py @@ -172,18 +172,22 @@ def on_connect(client, userdata, flags, reason_code, properties=None): logger.info("✅ Successfully connected to MQTT Broker! Subscribing to topics...") # Invia lo stato Online ai client web socketio.emit('mqtt_status', {'connected': True}) - client.subscribe([ - ("servizi/+/stat", 0), - ("dmr-gateway/+/json", 0), - ("devices/+/services", 0), - ("nxdn-gateway/+/json", 0), - ("ysf-gateway/+/json", 0), - ("p25-gateway/+/json", 0), - ("dstar-gateway/+/json", 0), - ("mmdvm/+/json", 0), - ("devices/#", 0), - ("data/#", 0) - ]) + + # --- LETTURA DINAMICA DEI TOPIC --- + default_topics = [ + "servizi/+/stat", "dmr-gateway/+/json", "devices/+/services", + "nxdn-gateway/+/json", "ysf-gateway/+/json", "p25-gateway/+/json", + "dstar-gateway/+/json", "mmdvm/+/json", "devices/#", "data/#" + ] + + # Cerca la lista "topics" nel config.json, se non la trova usa quella di default + topics_list = config.get('mqtt', {}).get('topics', default_topics) + + # Converte la lista di stringhe nel formato richiesto da paho-mqtt: [(topic, qos), (topic, qos)...] + subscribe_list = [(topic, 0) for topic in topics_list] + + client.subscribe(subscribe_list) + logger.info(f"Subscribed to {len(subscribe_list)} MQTT topics.") else: mqtt_connected_status = False socketio.emit('mqtt_status', {'connected': False}) diff --git a/config.json.example b/config.json.example index 9b7337e..47cba20 100644 --- a/config.json.example +++ b/config.json.example @@ -4,7 +4,19 @@ "port": 1883, "user": "your_username", "password": "your_password", - "client_id": "fleet_backend_prod" + "client_id": "fleet_backend_prod", + "topics": [ + "servizi/+/stat", + "dmr-gateway/+/json", + "devices/+/services", + "nxdn-gateway/+/json", + "ysf-gateway/+/json", + "p25-gateway/+/json", + "dstar-gateway/+/json", + "mmdvm/+/json", + "devices/#", + "data/#" + ] }, "web_admin": { "default_user": "admin",