initial commit
This commit is contained in:
0
dashboard/utils/__init__.py
Normal file
0
dashboard/utils/__init__.py
Normal file
12
dashboard/utils/auth.py
Normal file
12
dashboard/utils/auth.py
Normal file
@@ -0,0 +1,12 @@
|
||||
# dashboard/utils/auth.py
|
||||
import bcrypt
|
||||
|
||||
def hash_password(plain_text: str) -> str:
|
||||
return bcrypt.hashpw(plain_text.encode("utf-8"), bcrypt.gensalt()).decode("utf-8")
|
||||
|
||||
def check_password(plain_text: str, hashed: str) -> bool:
|
||||
return bcrypt.checkpw(plain_text.encode("utf-8"), hashed.encode("utf-8"))
|
||||
|
||||
def get_user_role(username: str) -> str:
|
||||
# Beispiel: aus der Datenbank auslesen (oder Hardcode während Dev-Phase)
|
||||
pass
|
||||
46
dashboard/utils/db.py
Normal file
46
dashboard/utils/db.py
Normal file
@@ -0,0 +1,46 @@
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
from sqlalchemy import create_engine, text
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
# .env laden
|
||||
load_dotenv()
|
||||
|
||||
# Datenbank-Zugangsdaten aus .env
|
||||
DB_USER = os.getenv("DB_USER")
|
||||
DB_PASSWORD = os.getenv("DB_PASSWORD")
|
||||
DB_HOST = os.getenv("DB_HOST", "localhost")
|
||||
DB_PORT = os.getenv("DB_PORT", "3306")
|
||||
DB_NAME = os.getenv("DB_NAME")
|
||||
|
||||
# Pooling Parameter aus .env (optional mit Default-Werten)
|
||||
POOL_SIZE = int(os.getenv("POOL_SIZE", 10))
|
||||
MAX_OVERFLOW = int(os.getenv("MAX_OVERFLOW", 20))
|
||||
POOL_TIMEOUT = int(os.getenv("POOL_TIMEOUT", 30))
|
||||
POOL_RECYCLE = int(os.getenv("POOL_RECYCLE", 1800))
|
||||
|
||||
# Connection-String zusammenbauen
|
||||
DATABASE_URL = (
|
||||
f"mysql+pymysql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
|
||||
)
|
||||
|
||||
# Engine mit Pooling konfigurieren
|
||||
engine = create_engine(
|
||||
DATABASE_URL,
|
||||
pool_size=POOL_SIZE,
|
||||
max_overflow=MAX_OVERFLOW,
|
||||
pool_timeout=POOL_TIMEOUT,
|
||||
pool_recycle=POOL_RECYCLE,
|
||||
echo=True, # für Debug, später False
|
||||
)
|
||||
|
||||
# Session Factory
|
||||
SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False)
|
||||
|
||||
def get_session():
|
||||
return SessionLocal()
|
||||
|
||||
def execute_query(query):
|
||||
with engine.connect() as connection:
|
||||
result = connection.execute(text(query))
|
||||
return [dict(row) for row in result]
|
||||
121
dashboard/utils/mqtt_client.py
Normal file
121
dashboard/utils/mqtt_client.py
Normal file
@@ -0,0 +1,121 @@
|
||||
# dashboard/utils/mqtt_client.py
|
||||
|
||||
import os
|
||||
import threading
|
||||
import time
|
||||
from dotenv import load_dotenv
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
# 1. Laden der Umgebungsvariablen aus .env
|
||||
load_dotenv(dotenv_path=os.path.join(os.path.dirname(__file__), "..", ".env"))
|
||||
|
||||
# 2. Lese MQTT‐Einstellungen
|
||||
MQTT_BROKER_HOST = os.getenv("MQTT_BROKER_HOST", "localhost")
|
||||
MQTT_BROKER_PORT = int(os.getenv("MQTT_BROKER_PORT", "1883"))
|
||||
MQTT_USERNAME = os.getenv("MQTT_USERNAME", None)
|
||||
MQTT_PASSWORD = os.getenv("MQTT_PASSWORD", None)
|
||||
MQTT_KEEPALIVE = int(os.getenv("MQTT_KEEPALIVE", "60"))
|
||||
MQTT_CLIENT_ID = os.getenv("MQTT_CLIENT_ID", f"dash-{int(time.time())}")
|
||||
|
||||
# 3. Erstelle eine globale Client‐Instanz
|
||||
client = mqtt.Client(client_id=MQTT_CLIENT_ID)
|
||||
|
||||
# Falls Nutzer/Passwort gesetzt sind, authentifizieren
|
||||
if MQTT_USERNAME and MQTT_PASSWORD:
|
||||
client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD)
|
||||
|
||||
|
||||
# 4. Callback‐Stubs (kannst du bei Bedarf anpassen)
|
||||
def _on_connect(client, userdata, flags, rc):
|
||||
if rc == 0:
|
||||
print(f"[mqtt_client.py] Erfolgreich mit MQTT‐Broker verbunden (Code {rc})")
|
||||
else:
|
||||
print(f"[mqtt_client.py] Verbindungsfehler, rc={rc}")
|
||||
|
||||
|
||||
def _on_disconnect(client, userdata, rc):
|
||||
print(f"[mqtt_client.py] Verbindung getrennt (rc={rc}). Versuche, neu zu verbinden …")
|
||||
|
||||
|
||||
def _on_message(client, userdata, msg):
|
||||
"""
|
||||
Diese Callback‐Funktion wird aufgerufen, sobald eine Nachricht auf einem
|
||||
Topic ankommt, auf das wir subscribed haben. Du kannst hier eine Queue
|
||||
füllen oder direkt eine Datenbank‐Funktion aufrufen.
|
||||
"""
|
||||
topic = msg.topic
|
||||
payload = msg.payload.decode("utf-8", errors="ignore")
|
||||
print(f"[mqtt_client.py] Nachricht eingegangen – Topic: {topic}, Payload: {payload}")
|
||||
# Beispiel: Wenn du Live‐Statusdaten in die Datenbank schreibst,
|
||||
# könntest du hier utils/db.execute_non_query(...) aufrufen.
|
||||
|
||||
|
||||
# 5. Setze die Callbacks
|
||||
client.on_connect = _on_connect
|
||||
client.on_disconnect = _on_disconnect
|
||||
client.on_message = _on_message
|
||||
|
||||
|
||||
def start_loop():
|
||||
"""
|
||||
Startet die Endlos‐Schleife, in der der Client auf eingehende
|
||||
MQTT‐Nachrichten hört und automatisch reconnectet.
|
||||
Muss idealerweise in einem eigenen Thread laufen, damit Dash‐Callbacks
|
||||
nicht blockieren.
|
||||
"""
|
||||
try:
|
||||
client.connect(MQTT_BROKER_HOST, MQTT_BROKER_PORT, keepalive=MQTT_KEEPALIVE)
|
||||
client.loop_start()
|
||||
except Exception as e:
|
||||
print(f"[mqtt_client.py] Konnte keine Verbindung zum MQTT‐Broker herstellen: {e}")
|
||||
|
||||
|
||||
def stop_loop():
|
||||
"""
|
||||
Stoppt die MQTT‐Loop und trennt die Verbindung.
|
||||
"""
|
||||
try:
|
||||
client.loop_stop()
|
||||
client.disconnect()
|
||||
except Exception as e:
|
||||
print(f"[mqtt_client.py] Fehler beim Stoppen der MQTT‐Schleife: {e}")
|
||||
|
||||
|
||||
def publish(topic: str, payload: str, qos: int = 0, retain: bool = False) -> bool:
|
||||
"""
|
||||
Verschickt eine MQTT‐Nachricht:
|
||||
- topic: z. B. "clients/{client_id}/control"
|
||||
- payload: z. B. '{"command":"restart"}'
|
||||
- qos: 0, 1 oder 2
|
||||
- retain: True/False
|
||||
Rückgabe: True, falls Veröffentlichung bestätigt wurde; sonst False.
|
||||
"""
|
||||
try:
|
||||
result = client.publish(topic, payload, qos=qos, retain=retain)
|
||||
status = result.rc # 0=Erfolg, sonst Fehler
|
||||
if status == mqtt.MQTT_ERR_SUCCESS:
|
||||
return True
|
||||
else:
|
||||
print(f"[mqtt_client.py] Publish-Fehler für Topic {topic}, rc={status}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"[mqtt_client.py] Exception beim Publish: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def subscribe(topic: str, qos: int = 0) -> bool:
|
||||
"""
|
||||
Abonniert ein MQTT‐Topic, sodass _on_message gerufen wird, sobald Nachrichten
|
||||
ankommen.
|
||||
Rückgabe: True bei Erfolg, ansonsten False.
|
||||
"""
|
||||
try:
|
||||
result, mid = client.subscribe(topic, qos=qos)
|
||||
if result == mqtt.MQTT_ERR_SUCCESS:
|
||||
return True
|
||||
else:
|
||||
print(f"[mqtt_client.py] Subscribe‐Fehler für Topic {topic}, rc={result}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"[mqtt_client.py] Exception beim Subscribe: {e}")
|
||||
return False
|
||||
Reference in New Issue
Block a user