functional system simclient<-> listener<->server
This commit is contained in:
@@ -1,19 +1,26 @@
|
||||
|
||||
import os
|
||||
import json
|
||||
import logging
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy import create_engine, func
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from models.models import Client
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv("/workspace/.env")
|
||||
|
||||
# ENV-abhängige Konfiguration
|
||||
ENV = os.getenv("ENV", "development")
|
||||
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO" if ENV == "production" else "DEBUG")
|
||||
DB_URL = os.environ.get(
|
||||
"DB_CONN", "mysql+pymysql://user:password@db/infoscreen")
|
||||
|
||||
# Logging
|
||||
logging.basicConfig(level=logging.INFO,
|
||||
logging.basicConfig(level=getattr(logging, LOG_LEVEL.upper(), logging.INFO),
|
||||
format='%(asctime)s [%(levelname)s] %(message)s')
|
||||
|
||||
# DB-Konfiguration (Beispiel: MariaDB/MySQL, anpassen!)
|
||||
DB_URL = os.environ.get(
|
||||
"DB_URL", "mysql+pymysql://user:password@db/infoscreen")
|
||||
# DB-Konfiguration
|
||||
engine = create_engine(DB_URL)
|
||||
Session = sessionmaker(bind=engine)
|
||||
|
||||
@@ -22,16 +29,31 @@ Session = sessionmaker(bind=engine)
|
||||
|
||||
|
||||
def on_message(client, userdata, msg):
|
||||
topic = msg.topic
|
||||
try:
|
||||
# Heartbeat-Handling
|
||||
if topic.startswith("infoscreen/") and topic.endswith("/heartbeat"):
|
||||
uuid = topic.split("/")[1]
|
||||
session = Session()
|
||||
client_obj = session.query(Client).filter_by(uuid=uuid).first()
|
||||
if client_obj:
|
||||
from sqlalchemy import func
|
||||
client_obj.last_alive = func.current_timestamp()
|
||||
session.commit()
|
||||
logging.info(
|
||||
f"Heartbeat von {uuid} empfangen, last_alive aktualisiert.")
|
||||
session.close()
|
||||
return
|
||||
|
||||
# Discovery-Handling
|
||||
payload = json.loads(msg.payload.decode())
|
||||
logging.info(f"Discovery empfangen: {payload}")
|
||||
session = Session()
|
||||
# Prüfen, ob Client schon existiert
|
||||
existing = session.query(Client).filter_by(
|
||||
client_id=payload["client_id"]).first()
|
||||
uuid=payload["uuid"]).first()
|
||||
if not existing:
|
||||
new_client = Client(
|
||||
client_id=payload.get("client_id"),
|
||||
uuid=payload.get("uuid"),
|
||||
hardware_token=payload.get("hardware_token"),
|
||||
ip=payload.get("ip"),
|
||||
type=payload.get("type"),
|
||||
@@ -43,10 +65,14 @@ def on_message(client, userdata, msg):
|
||||
)
|
||||
session.add(new_client)
|
||||
session.commit()
|
||||
logging.info(f"Neuer Client registriert: {payload['client_id']}")
|
||||
logging.info(f"Neuer Client registriert: {payload['uuid']}")
|
||||
else:
|
||||
logging.info(f"Client bereits bekannt: {payload['client_id']}")
|
||||
logging.info(f"Client bereits bekannt: {payload['uuid']}")
|
||||
session.close()
|
||||
# Discovery-ACK senden
|
||||
ack_topic = f"infoscreen/{payload['uuid']}/discovery_ack"
|
||||
client.publish(ack_topic, json.dumps({"status": "ok"}))
|
||||
logging.info(f"Discovery-ACK gesendet an {ack_topic}")
|
||||
except Exception as e:
|
||||
logging.error(f"Fehler bei Verarbeitung: {e}")
|
||||
|
||||
@@ -56,7 +82,9 @@ def main():
|
||||
mqtt_client.on_message = on_message
|
||||
mqtt_client.connect("mqtt", 1883)
|
||||
mqtt_client.subscribe("infoscreen/discovery")
|
||||
logging.info("Listener gestartet und abonniert auf infoscreen/discovery")
|
||||
mqtt_client.subscribe("infoscreen/+/heartbeat")
|
||||
logging.info(
|
||||
"Listener gestartet und abonniert auf infoscreen/discovery und infoscreen/+/heartbeat")
|
||||
mqtt_client.loop_forever()
|
||||
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
paho-mqtt>=2.0
|
||||
SQLAlchemy>=2.0
|
||||
pymysql
|
||||
python-dotenv
|
||||
|
||||
Reference in New Issue
Block a user