This commit is contained in:
Wolfgang Hottgenroth 2024-11-05 15:53:03 +01:00
parent db89541352
commit d57becf736
Signed by: wn
GPG Key ID: 836E9E1192A6B132
4 changed files with 33 additions and 2 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
.*~
*.pyc
.venv/
__pycache__/

6
env
View File

@ -2,10 +2,12 @@ export MQTT_BROKER="172.23.1.102"
export MQTT_PORT=1883
export MQTT_BOXES='{
"box1": {
"label": "living_room"
"label": "living_room",
"windows": [ "street_side", "garden_side" ]
},
"box2": {
"label": "kitchen"
"label": "kitchen",
"windows": [ "street_side", "garden_side", "garden_door" ]
}
}'
export MQTT_BOX_TOPIC_PREFIXES='{

View File

@ -42,6 +42,11 @@ context['publish'] = PUBLISH_PREFIX
# Load box configurations from JSON
try:
boxes = json.loads(BOXES_CONFIG)
# here, boxes store their internal state
boxes['context'] = {}
# boxes structure added to global context to give process_message access to it
context['boxes'] = boxes
except json.JSONDecodeError as e:
logger.error(f"Error parsing JSON configuration for boxes: {e}")
@ -83,6 +88,9 @@ def on_connect(client, userdata, flags, rc):
# Generate topics based on configured box-specific prefixes and box label
topics = {}
for topic_key, prefix in box_topic_prefixes.items():
if topic_key == 'window':
for window in boxes.windows:
topics[topic_key] = f"{prefix}{label}"
config["subscribe"] = topics # Store the generated topics

View File

@ -1,9 +1,29 @@
from loguru import logger
# context
# boxes: structure of boxes
# client: MQTT client
#
# boxes['context']
# store here what ever is require to represent the state of the box
def process_message(box_name, topic_key, payload, context):
try:
logger.info(f"[{box_name}] Processing message for '{topic_key}': {payload}")
match topic_key:
case 'window':
pass
case 'high_temp':
pass
case 'cmd':
pass
case 'general_off':
pass
case 'maintenance_mode':
pass
# Example processing: Implement your specific logic here
# For example, perform calculations or store data.
result = f"Processed by {box_name} for '{topic_key}': {payload}"