"Refactor job scheduler and printer monitor for improved performance (feat)"

This commit is contained in:
2025-05-29 21:30:44 +02:00
parent 962d395017
commit fe94a3b58c
4 changed files with 552 additions and 15 deletions

View File

@@ -9,6 +9,7 @@ from sqlalchemy.orm import joinedload
from utils.logging_config import get_logger
from models import Job, Printer, get_db_session
from config.settings import TAPO_USERNAME, TAPO_PASSWORD
# Lazy logger initialization
_logger = None
@@ -313,15 +314,22 @@ def toggle_plug(printer_id: int, state: bool) -> bool:
db_session.close()
return False
# Konfiguration validieren
if not printer.plug_ip or not printer.plug_username or not printer.plug_password:
logger.error(f"Unvollständige Tapo-Konfiguration für Drucker {printer.name} (ID: {printer_id})")
# Konfiguration validieren und Fallback verwenden
plug_ip = printer.plug_ip
plug_username = printer.plug_username or TAPO_USERNAME
plug_password = printer.plug_password or TAPO_PASSWORD
if not plug_ip:
logger.error(f"Keine Steckdosen-IP für Drucker {printer.name} (ID: {printer_id}) konfiguriert")
db_session.close()
return False
if not plug_username or not plug_password:
logger.error(f"Unvollständige Tapo-Konfiguration für Drucker {printer.name} (ID: {printer_id}) - verwende globale Anmeldedaten")
# TP-Link Tapo P110 Verbindung herstellen
logger.debug(f"Verbinde zu Tapo-Steckdose {printer.plug_ip} für Drucker {printer.name}")
p110 = PyP110.P110(printer.plug_ip, printer.plug_username, printer.plug_password)
logger.debug(f"Verbinde zu Tapo-Steckdose {plug_ip} für Drucker {printer.name}")
p110 = PyP110.P110(plug_ip, plug_username, plug_password)
p110.handshake() # Authentifizierung
p110.login() # Login
@@ -341,14 +349,14 @@ def toggle_plug(printer_id: int, state: bool) -> bool:
return False
def test_tapo_connection(ip_address: str, username: str, password: str) -> dict:
def test_tapo_connection(ip_address: str, username: str = None, password: str = None) -> dict:
"""
Testet die Verbindung zu einer Tapo-Steckdose und gibt detaillierte Informationen zurück.
Args:
ip_address: IP-Adresse der Steckdose
username: Benutzername
password: Passwort
username: Benutzername (optional, verwendet globale Konfiguration als Fallback)
password: Passwort (optional, verwendet globale Konfiguration als Fallback)
Returns:
dict: Testergebnis mit Status und Informationen
@@ -361,6 +369,12 @@ def test_tapo_connection(ip_address: str, username: str, password: str) -> dict:
"status": "unknown"
}
# Fallback zu globalen Anmeldedaten
if not username or not password:
username = TAPO_USERNAME
password = TAPO_PASSWORD
logger.debug(f"🔧 Verwende globale Tapo-Anmeldedaten für {ip_address}")
try:
logger.debug(f"Teste Tapo-Verbindung zu {ip_address}")
p110 = PyP110.P110(ip_address, username, password)