Complete Redesign of Backend Handling for Client Group Assignments
This commit is contained in:
@@ -6,6 +6,9 @@ WORKDIR /app
|
||||
COPY listener/requirements.txt ./
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
# Mosquitto-Tools für MQTT-Tests installieren
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends mosquitto-clients && rm -rf /var/lib/apt/lists/*
|
||||
|
||||
COPY listener/ ./listener
|
||||
COPY models/ ./models
|
||||
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import os
|
||||
import json
|
||||
import logging
|
||||
import threading
|
||||
import time
|
||||
import datetime
|
||||
import paho.mqtt.client as mqtt
|
||||
from sqlalchemy import create_engine
|
||||
@@ -27,12 +25,11 @@ logging.basicConfig(level=logging.DEBUG,
|
||||
engine = create_engine(DB_URL)
|
||||
Session = sessionmaker(bind=engine)
|
||||
|
||||
# MQTT-Callback
|
||||
|
||||
|
||||
def on_message(client, userdata, msg):
|
||||
topic = msg.topic
|
||||
logging.debug(f"Empfangene Nachricht auf Topic: {topic}")
|
||||
|
||||
try:
|
||||
# Heartbeat-Handling
|
||||
if topic.startswith("infoscreen/") and topic.endswith("/heartbeat"):
|
||||
@@ -41,57 +38,53 @@ def on_message(client, userdata, msg):
|
||||
client_obj = session.query(Client).filter_by(uuid=uuid).first()
|
||||
if client_obj:
|
||||
client_obj.last_alive = datetime.datetime.now(datetime.UTC)
|
||||
session.commit()
|
||||
logging.info(
|
||||
f"Heartbeat von {uuid} empfangen, last_alive (UTC) aktualisiert.")
|
||||
session.commit()
|
||||
logging.info(
|
||||
f"Heartbeat von {uuid} empfangen, last_alive (UTC) aktualisiert.")
|
||||
session.close()
|
||||
return
|
||||
|
||||
# Discovery-Handling
|
||||
payload = json.loads(msg.payload.decode())
|
||||
logging.info(f"Discovery empfangen: {payload}")
|
||||
if "uuid" in payload:
|
||||
uuid = payload["uuid"]
|
||||
session = Session()
|
||||
existing = session.query(Client).filter_by(uuid=uuid).first()
|
||||
if not existing:
|
||||
new_client = Client(
|
||||
uuid=uuid,
|
||||
hardware_token=payload.get("hardware_token"),
|
||||
ip=payload.get("ip"),
|
||||
type=payload.get("type"),
|
||||
hostname=payload.get("hostname"),
|
||||
os_version=payload.get("os_version"),
|
||||
software_version=payload.get("software_version"),
|
||||
macs=",".join(payload.get("macs", [])),
|
||||
model=payload.get("model"),
|
||||
registration_time=datetime.datetime.now(datetime.UTC),
|
||||
)
|
||||
session.add(new_client)
|
||||
session.commit()
|
||||
logging.info(f"Neuer Client registriert: {uuid}")
|
||||
if topic == "infoscreen/discovery":
|
||||
payload = json.loads(msg.payload.decode())
|
||||
logging.info(f"Discovery empfangen: {payload}")
|
||||
|
||||
if "uuid" in payload:
|
||||
uuid = payload["uuid"]
|
||||
session = Session()
|
||||
existing = session.query(Client).filter_by(uuid=uuid).first()
|
||||
|
||||
if not existing:
|
||||
new_client = Client(
|
||||
uuid=uuid,
|
||||
hardware_token=payload.get("hardware_token"),
|
||||
ip=payload.get("ip"),
|
||||
type=payload.get("type"),
|
||||
hostname=payload.get("hostname"),
|
||||
os_version=payload.get("os_version"),
|
||||
software_version=payload.get("software_version"),
|
||||
macs=",".join(payload.get("macs", [])),
|
||||
model=payload.get("model"),
|
||||
registration_time=datetime.datetime.now(datetime.UTC),
|
||||
)
|
||||
session.add(new_client)
|
||||
session.commit()
|
||||
logging.info(f"Neuer Client registriert: {uuid}")
|
||||
else:
|
||||
logging.info(f"Client bereits bekannt: {uuid}")
|
||||
|
||||
session.close()
|
||||
|
||||
# Discovery-ACK senden
|
||||
ack_topic = f"infoscreen/{uuid}/discovery_ack"
|
||||
client.publish(ack_topic, json.dumps({"status": "ok"}))
|
||||
logging.info(f"Discovery-ACK gesendet an {ack_topic}")
|
||||
else:
|
||||
logging.info(f"Client bereits bekannt: {uuid}")
|
||||
session.close()
|
||||
# Discovery-ACK senden
|
||||
ack_topic = f"infoscreen/{uuid}/discovery_ack"
|
||||
client.publish(ack_topic, json.dumps({"status": "ok"}))
|
||||
logging.info(f"Discovery-ACK gesendet an {ack_topic}")
|
||||
else:
|
||||
logging.warning("Discovery ohne UUID empfangen, ignoriert.")
|
||||
logging.warning("Discovery ohne UUID empfangen, ignoriert.")
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"Fehler bei Verarbeitung: {e}")
|
||||
|
||||
topic_parts = msg.topic.split('/')
|
||||
if len(topic_parts) == 3 and topic_parts[0] == "infoscreen" and topic_parts[1] == "request_group_id":
|
||||
client_id = topic_parts[2]
|
||||
session = Session()
|
||||
client_obj = session.query(Client).filter_by(uuid=client_id).first()
|
||||
group_id = client_obj.group_id if client_obj else None
|
||||
session.close()
|
||||
response_topic = f"infoscreen/response_group_id/{client_id}"
|
||||
client.publish(response_topic, json.dumps({"group_id": group_id}))
|
||||
|
||||
|
||||
def main():
|
||||
mqtt_client = mqtt.Client(protocol=mqtt.MQTTv311, callback_api_version=2)
|
||||
@@ -99,9 +92,9 @@ def main():
|
||||
mqtt_client.connect("mqtt", 1883)
|
||||
mqtt_client.subscribe("infoscreen/discovery")
|
||||
mqtt_client.subscribe("infoscreen/+/heartbeat")
|
||||
mqtt_client.subscribe("infoscreen/request_group_id/#")
|
||||
|
||||
logging.info(
|
||||
"Listener gestartet und abonniert auf infoscreen/discovery, infoscreen/+/heartbeat und infoscreen/request_group_id/#")
|
||||
"Listener gestartet und abonniert auf infoscreen/discovery und infoscreen/+/heartbeat")
|
||||
mqtt_client.loop_forever()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user