functional system simclient<-> listener<->server

This commit is contained in:
2025-07-15 15:37:16 +00:00
parent f37744b31e
commit 84a92ab9c2
5 changed files with 145 additions and 24 deletions

View File

@@ -1,4 +1,5 @@
# simclient/simclient.py
import time
import uuid
import json
@@ -9,23 +10,43 @@ import os
import re
import platform
import logging
DEBUG_MODE = True # Auf False setzen für Produktion
from dotenv import load_dotenv
# ENV laden
load_dotenv("/workspace/simclient/.env")
# Konfiguration aus ENV
ENV = os.getenv("ENV", "development")
HEARTBEAT_INTERVAL = int(
os.getenv("HEARTBEAT_INTERVAL", 5 if ENV == "development" else 60))
LOG_LEVEL = os.getenv("LOG_LEVEL", "DEBUG" if ENV == "development" else "INFO")
MQTT_BROKER = os.getenv("MQTT_BROKER", "mqtt")
MQTT_PORT = int(os.getenv("MQTT_PORT", 1883))
DEBUG_MODE = os.getenv("DEBUG_MODE", "1" if ENV ==
"development" else "0") in ("1", "true", "True")
# Logging-Konfiguration
LOG_DIR = os.path.dirname(os.path.abspath(__file__))
LOG_PATH = os.path.join(LOG_DIR, "simclient.log")
LOG_PATH = "/tmp/simclient.log"
log_handlers = [logging.FileHandler(LOG_PATH, encoding="utf-8")]
if DEBUG_MODE:
log_handlers.append(logging.StreamHandler())
logging.basicConfig(
level=logging.DEBUG if DEBUG_MODE else logging.INFO,
level=getattr(logging, LOG_LEVEL.upper(), logging.INFO),
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=log_handlers
)
discovered = False
def on_message(client, userdata, msg):
global discovered
logging.info(f"Empfangen: {msg.topic} {msg.payload.decode()}")
# ACK-Quittung empfangen?
if msg.topic.endswith("/discovery_ack"):
discovered = True
logging.info("Discovery-ACK empfangen. Starte Heartbeat.")
def get_mac_addresses():
@@ -107,7 +128,7 @@ SOFTWARE_VERSION = "1.0.0" # Optional: Anpassen bei neuen Releases
def send_discovery(client, client_id, hardware_token, ip_addr):
macs = get_mac_addresses()
discovery_msg = {
"client_id": client_id,
"uuid": client_id,
"hardware_token": hardware_token,
"ip": ip_addr,
"type": "infoscreen",
@@ -122,20 +143,30 @@ def send_discovery(client, client_id, hardware_token, ip_addr):
def main():
global discovered
client_id = str(uuid.uuid4())
hardware_token = get_hardware_token()
ip_addr = get_ip()
client = mqtt.Client(protocol=mqtt.MQTTv311, callback_api_version=2)
client.on_message = on_message
client.connect("mqtt", 1883)
client.connect(MQTT_BROKER, MQTT_PORT)
# Discovery-ACK-Topic abonnieren
ack_topic = f"infoscreen/{client_id}/discovery_ack"
client.subscribe(ack_topic)
client.subscribe(f"infoscreen/{client_id}/config")
send_discovery(client, client_id, hardware_token, ip_addr)
# 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
while True:
# Heartbeat senden
client.publish(f"infoscreen/{client_id}/heartbeat", "alive")
logging.debug("Heartbeat gesendet.")
client.loop(timeout=1.0)
time.sleep(5)
time.sleep(HEARTBEAT_INTERVAL)
if __name__ == "__main__":