there is an issue concerning output_topic

This commit is contained in:
2024-11-05 21:37:41 +01:00
parent 9582790160
commit e2b5eaff6e
3 changed files with 73 additions and 49 deletions

View File

@ -11,10 +11,12 @@ from message_processor import process_message # Import the moved function
# MQTT configuration parameters
BROKER = os.getenv("MQTT_BROKER") # Read broker from environment variable
PORT = int(os.getenv("MQTT_PORT", 1883)) # Default port if not set
BOXES_CONFIG = os.getenv("MQTT_BOXES") # JSON configuration of boxes from environment variable
BOXES_CONFIG = os.getenv("BOXES") # JSON configuration of boxes from environment variable
BOX_TOPIC_PREFIXES_CONFIG = os.getenv("MQTT_BOX_TOPIC_PREFIXES") # JSON configuration of box-specific topic prefixes
CENTRAL_TOPICS_CONFIG = os.getenv("MQTT_CENTRAL_TOPICS") # JSON configuration of central topics
PUBLISH_PREFIX = os.getenv("MQTT_PUBLISH_PREFIX", "output/") # Globales Publish-Präfix
OFF_TEMPERATURE = os.getenv("OFF_TEMPERATURE", "5.0")
LOW_TEMPERATURE = os.getenv("LOW_TEMPERATURE", "15.0")
MAINTENANCE_TEMPERATURE = os.getenv("MAINTENANCE_TEMPERATURE", "30.0")
# Check if required environment variables are set
missing_vars = []
@ -26,8 +28,6 @@ if not BOX_TOPIC_PREFIXES_CONFIG:
missing_vars.append('MQTT_BOX_TOPIC_PREFIXES')
if not CENTRAL_TOPICS_CONFIG:
missing_vars.append('MQTT_CENTRAL_TOPICS')
if not PUBLISH_PREFIX:
missing_vars.append('MQTT_PUBLISH_PREFIX')
if missing_vars:
logger.error(f"Error: The following environment variables are not set: {', '.join(missing_vars)}")
@ -36,8 +36,10 @@ if missing_vars:
# context for box operations
context = {}
# publish topic prefix for boxes
context['publish'] = PUBLISH_PREFIX
# configuration values for boxes
context['off_temperature'] = OFF_TEMPERATURE
context['low_temperature'] = LOW_TEMPERATURE
context['maintenance_temperature'] = MAINTENANCE_TEMPERATURE
# Load box configurations from JSON
try:
@ -49,6 +51,8 @@ try:
# add context dict to each box in the list
for box_name, config in boxes.items():
config['context'] = {}
logger.info(f"{boxes=}")
except json.JSONDecodeError as e:
logger.error(f"Error parsing JSON configuration for boxes: {e}")
sys.exit(1)
@ -58,7 +62,7 @@ try:
box_topic_prefixes = json.loads(BOX_TOPIC_PREFIXES_CONFIG)
# Validation: Check if the required keys are present
required_keys = {'window', 'high_temp', 'cmd'}
required_keys = {'high_temp', 'cmd'}
missing_keys = required_keys - box_topic_prefixes.keys()
if missing_keys:
@ -96,23 +100,19 @@ def on_connect(client, userdata, flags, rc):
# Generate topics based on configured box-specific prefixes and box label
for topic_key, prefix in box_topic_prefixes.items():
if topic_key == 'window':
windows = config.get("windows", [])
if not windows:
logger.warning(f"[{box_name}] No 'windows' defined.")
continue
for window in windows:
# Form the new topic_key by combining 'window' and the window name
enhanced_topic_key = f"{topic_key}/{window}"
topic = f"{prefix}{label}/{window}"
client.subscribe(topic)
topic_mapping[topic] = (box_name, enhanced_topic_key)
logger.info(f"[{box_name}] Subscribed to '{topic}' (Key: '{enhanced_topic_key}')")
else:
topic = f"{prefix}{label}"
client.subscribe(topic)
topic_mapping[topic] = (box_name, topic_key)
logger.info(f"[{box_name}] Subscribed to '{topic}' (Key: '{topic_key}')")
topic = f"{prefix}{label}"
client.subscribe(topic)
topic_mapping[topic] = (box_name, topic_key)
logger.info(f"[{box_name}] Subscribed to '{topic}' (Key: '{topic_key}')")
# Subscribe window topics from box
for window_topic in config.get("windows"):
topic = window_topic.get("topic")
label = window_topic.get("label")
topic_key = f"window/{label}"
client.subscribe(topic)
topic_mapping[topic] = (box_name, topic_key)
logger.info(f"[{box_name}] Subscribed to '{topic}' (Key: '{topic_key}')")
# Subscribe to central topics and create mappings
for central_key, central_topic in central_topics.items():