test communication scheduler<->simclient

This commit is contained in:
2025-07-18 14:49:53 +00:00
parent a1d6d83488
commit 2e9f22f5cc
11 changed files with 389 additions and 88 deletions

View File

@@ -1,5 +1,6 @@
# simclient/simclient.py
from logging.handlers import RotatingFileHandler
import time
import uuid
import json
@@ -11,6 +12,7 @@ import re
import platform
import logging
from dotenv import load_dotenv
import requests
# ENV laden
load_dotenv("/workspace/simclient/.env")
@@ -26,8 +28,11 @@ DEBUG_MODE = os.getenv("DEBUG_MODE", "1" if ENV ==
"development" else "0") in ("1", "true", "True")
# Logging-Konfiguration
LOG_PATH = "/tmp/simclient.log"
log_handlers = [logging.FileHandler(LOG_PATH, encoding="utf-8")]
LOG_PATH = os.path.join(os.path.dirname(__file__), "simclient.log")
os.makedirs(os.path.dirname(LOG_PATH), exist_ok=True)
log_handlers = []
log_handlers.append(RotatingFileHandler(
LOG_PATH, maxBytes=2*1024*1024, backupCount=5, encoding="utf-8"))
if DEBUG_MODE:
log_handlers.append(logging.StreamHandler())
logging.basicConfig(
@@ -43,6 +48,10 @@ discovered = False
def on_message(client, userdata, msg):
global discovered
logging.info(f"Empfangen: {msg.topic} {msg.payload.decode()}")
# Event-Messages vom Scheduler explizit loggen
if msg.topic.startswith("infoscreen/events/"):
logging.info(
f"Event-Message vom Scheduler empfangen: {msg.payload.decode()}")
# ACK-Quittung empfangen?
if msg.topic.endswith("/discovery_ack"):
discovered = True
@@ -168,14 +177,45 @@ def main():
client.subscribe(ack_topic)
client.subscribe(f"infoscreen/{client_id}/config")
# Hilfsfunktion: Hole group_id per API
def get_group_id(client_id):
api_url = f"http://server:8000/api/clients/{client_id}/group"
try:
resp = requests.get(api_url, timeout=5)
logging.debug(
f"API-Request: {api_url} - Status: {resp.status_code} - Response: {resp.text}")
if resp.ok:
group_id = resp.json().get("group_id")
logging.info(
f"Ermittelte group_id für Client {client_id}: {group_id}")
return group_id
except Exception as e:
logging.error(f"Fehler beim API-Request für group_id: {e}")
return None
# Discovery-Phase: Sende Discovery bis ACK empfangen
while not discovered:
send_discovery(client, client_id, hardware_token, ip_addr)
client.loop(timeout=1.0)
time.sleep(HEARTBEAT_INTERVAL)
# Heartbeat-Phase
# Event-Topic abonnieren (und bei Änderung wechseln)
current_group_id = None
event_topic = None
while True:
group_id = get_group_id(client_id)
logging.debug(
f"Aktuelle group_id: {group_id}, vorherige group_id: {current_group_id}")
if group_id != current_group_id:
# Topic wechseln
if event_topic:
client.unsubscribe(event_topic)
logging.info(f"Event-Topic abbestellt: {event_topic}")
event_topic = f"infoscreen/events/{group_id}"
client.subscribe(event_topic)
current_group_id = group_id
logging.info(
f"Abonniere Event-Topic: {event_topic} für group_id: {group_id}")
client.publish(f"infoscreen/{client_id}/heartbeat", "alive")
logging.debug("Heartbeat gesendet.")
client.loop(timeout=1.0)