"Refactor job scheduling and printer monitoring"
This commit is contained in:
parent
2a1489b438
commit
a02f382607
@ -1297,45 +1297,61 @@ def check_printer_status(ip_address: str, timeout: int = 7) -> Tuple[str, bool]:
|
|||||||
return "offline", False
|
return "offline", False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# IP-Adresse validieren
|
# TCP-Verbindung zu Port 80 oder 443 testen (statt Ping)
|
||||||
import ipaddress
|
|
||||||
try:
|
try:
|
||||||
ipaddress.ip_address(ip_address.strip())
|
import socket
|
||||||
except ValueError:
|
# Zuerst Port 80 versuchen
|
||||||
printers_logger.debug(f"Ungültige IP-Adresse: {ip_address}")
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
sock.settimeout(timeout)
|
||||||
|
result = sock.connect_ex((ip_address.strip(), 80))
|
||||||
|
sock.close()
|
||||||
|
|
||||||
|
connection_ok = result == 0
|
||||||
|
|
||||||
|
# Falls Port 80 nicht erfolgreich, Port 443 testen
|
||||||
|
if not connection_ok:
|
||||||
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
sock.settimeout(timeout)
|
||||||
|
result = sock.connect_ex((ip_address.strip(), 443))
|
||||||
|
sock.close()
|
||||||
|
connection_ok = result == 0
|
||||||
|
except:
|
||||||
|
connection_ok = False
|
||||||
|
|
||||||
|
if not connection_ok:
|
||||||
|
printers_logger.debug(f"Keine Verbindung zu {ip_address} möglich")
|
||||||
return "offline", False
|
return "offline", False
|
||||||
|
|
||||||
# Importiere PyP100 für Tapo-Unterstützung
|
# TP-Link Tapo P100-Verbindung aufbauen
|
||||||
try:
|
try:
|
||||||
from PyP100 import PyP110
|
# Passwort aus config/settings.py (mit 'A' am Ende)
|
||||||
except ImportError:
|
from config.settings import TAPO_USERNAME, TAPO_PASSWORD
|
||||||
printers_logger.error("⚠️ PyP100-Modul nicht verfügbar - kann Tapo-Steckdosen nicht abfragen")
|
|
||||||
|
# Verbindung aufbauen
|
||||||
|
from PyP100 import PyP100
|
||||||
|
p100 = PyP100.P100(ip_address, TAPO_USERNAME, TAPO_PASSWORD)
|
||||||
|
p100.handshake() # Authentifizierung
|
||||||
|
p100.login() # Login
|
||||||
|
|
||||||
|
# Geräteinformationen abrufen
|
||||||
|
device_info = p100.getDeviceInfo()
|
||||||
|
|
||||||
|
# Status auswerten
|
||||||
|
device_on = device_info.get('device_on', False)
|
||||||
|
|
||||||
|
if device_on:
|
||||||
|
printers_logger.debug(f"Steckdose {ip_address} ist eingeschaltet")
|
||||||
|
return "online", True
|
||||||
|
else:
|
||||||
|
printers_logger.debug(f"Steckdose {ip_address} ist ausgeschaltet")
|
||||||
|
return "offline", False
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
printers_logger.debug(f"Fehler bei Tapo-Verbindung zu {ip_address}: {str(e)}")
|
||||||
return "offline", False
|
return "offline", False
|
||||||
|
|
||||||
# Verwende IMMER die globalen hardkodierten Tapo-Anmeldedaten
|
|
||||||
username = TAPO_USERNAME
|
|
||||||
password = TAPO_PASSWORD
|
|
||||||
|
|
||||||
printers_logger.debug(f"🔌 Teste Tapo-Steckdose {ip_address} mit hardkodierten Anmeldedaten")
|
|
||||||
|
|
||||||
# TP-Link Tapo P110 Verbindung herstellen
|
|
||||||
p110 = PyP110.P110(ip_address.strip(), username, password)
|
|
||||||
p110.handshake() # Authentifizierung
|
|
||||||
p110.login() # Login
|
|
||||||
|
|
||||||
# Geräteinformationen abrufen
|
|
||||||
device_info = p110.getDeviceInfo()
|
|
||||||
device_on = device_info.get('device_on', False)
|
|
||||||
|
|
||||||
if device_on:
|
|
||||||
printers_logger.debug(f"✅ Drucker {ip_address}: ONLINE (Steckdose eingeschaltet)")
|
|
||||||
return "online", True
|
|
||||||
else:
|
|
||||||
printers_logger.debug(f"🔄 Drucker {ip_address}: STANDBY (Steckdose ausgeschaltet)")
|
|
||||||
return "standby", False
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
printers_logger.debug(f"❌ Fehler beim Tapo-Status-Check für {ip_address}: {str(e)}")
|
printers_logger.error(f"Fehler bei Status-Check für {ip_address}: {str(e)}")
|
||||||
return "offline", False
|
return "offline", False
|
||||||
|
|
||||||
@measure_execution_time(logger=printers_logger, task_name="Mehrere-Drucker-Status-Prüfung")
|
@measure_execution_time(logger=printers_logger, task_name="Mehrere-Drucker-Status-Prüfung")
|
||||||
|
@ -314,38 +314,58 @@ def toggle_plug(printer_id: int, state: bool) -> bool:
|
|||||||
db_session.close()
|
db_session.close()
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Konfiguration validieren und Fallback verwenden
|
# Konfiguration validieren
|
||||||
plug_ip = printer.plug_ip
|
if not printer.plug_ip or not printer.plug_username or not printer.plug_password:
|
||||||
plug_username = printer.plug_username or TAPO_USERNAME
|
logger.error(f"Unvollständige Steckdosen-Konfiguration für Drucker {printer.name}")
|
||||||
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()
|
db_session.close()
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if not plug_username or not plug_password:
|
# Importiere PyP100 für Tapo-Unterstützung
|
||||||
logger.error(f"Unvollständige Tapo-Konfiguration für Drucker {printer.name} (ID: {printer_id}) - verwende globale Anmeldedaten")
|
try:
|
||||||
|
from PyP100 import PyP100
|
||||||
|
except ImportError:
|
||||||
|
logger.error("PyP100-Modul nicht verfügbar - kann Tapo-Steckdosen nicht steuern")
|
||||||
|
db_session.close()
|
||||||
|
return False
|
||||||
|
|
||||||
# TP-Link Tapo P110 Verbindung herstellen
|
# Verwende die in der Datenbank gespeicherten Anmeldedaten
|
||||||
logger.debug(f"Verbinde zu Tapo-Steckdose {plug_ip} für Drucker {printer.name}")
|
# Fallback zu config/settings.py wenn nicht vorhanden
|
||||||
p110 = PyP110.P110(plug_ip, plug_username, plug_password)
|
username = printer.plug_username
|
||||||
p110.handshake() # Authentifizierung
|
password = printer.plug_password
|
||||||
p110.login() # Login
|
|
||||||
|
|
||||||
# Steckdose ein-/ausschalten
|
if not username or not password:
|
||||||
|
from config.settings import TAPO_USERNAME, TAPO_PASSWORD
|
||||||
|
username = TAPO_USERNAME
|
||||||
|
password = TAPO_PASSWORD
|
||||||
|
logger.debug(f"Verwende globale Tapo-Anmeldedaten für {printer.name}")
|
||||||
|
|
||||||
|
# TP-Link Tapo P100 Verbindung herstellen
|
||||||
|
p100 = PyP100.P100(printer.plug_ip, username, password)
|
||||||
|
p100.handshake() # Authentifizierung
|
||||||
|
p100.login() # Login
|
||||||
|
|
||||||
|
# Steckdose schalten
|
||||||
if state:
|
if state:
|
||||||
p110.turnOn()
|
p100.turnOn()
|
||||||
logger.info(f"✅ Steckdose für Drucker {printer.name} (ID: {printer_id}) eingeschaltet")
|
logger.info(f"Steckdose für {printer.name} eingeschaltet")
|
||||||
else:
|
else:
|
||||||
p110.turnOff()
|
p100.turnOff()
|
||||||
logger.info(f"✅ Steckdose für Drucker {printer.name} (ID: {printer_id}) ausgeschaltet")
|
logger.info(f"Steckdose für {printer.name} ausgeschaltet")
|
||||||
|
|
||||||
|
# Status in Datenbank aktualisieren
|
||||||
|
printer.status = "online" if state else "offline"
|
||||||
|
printer.last_checked = datetime.now()
|
||||||
|
db_session.commit()
|
||||||
db_session.close()
|
db_session.close()
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"❌ Fehler beim Schalten der Steckdose für Drucker {printer_id}: {str(e)}")
|
logger.error(f"Fehler beim Schalten der Steckdose für Drucker {printer_id}: {str(e)}")
|
||||||
db_session.close()
|
try:
|
||||||
|
db_session.close()
|
||||||
|
except:
|
||||||
|
pass
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ from config.settings import PRINTERS, TAPO_USERNAME, TAPO_PASSWORD, DEFAULT_TAPO
|
|||||||
|
|
||||||
# TP-Link Tapo P110 Unterstützung hinzufügen
|
# TP-Link Tapo P110 Unterstützung hinzufügen
|
||||||
try:
|
try:
|
||||||
from PyP100 import PyP110
|
from PyP100 import PyP100
|
||||||
TAPO_AVAILABLE = True
|
TAPO_AVAILABLE = True
|
||||||
except ImportError:
|
except ImportError:
|
||||||
TAPO_AVAILABLE = False
|
TAPO_AVAILABLE = False
|
||||||
@ -139,13 +139,13 @@ class PrinterMonitor:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# TP-Link Tapo P110 Verbindung herstellen
|
# TP-Link Tapo P100 Verbindung herstellen (P100 statt P110 verwenden)
|
||||||
p110 = PyP110.P110(ip_address, username, password)
|
p100 = PyP100.P100(ip_address, username, password)
|
||||||
p110.handshake() # Authentifizierung
|
p100.handshake() # Authentifizierung
|
||||||
p110.login() # Login
|
p100.login() # Login
|
||||||
|
|
||||||
# Steckdose ausschalten
|
# Steckdose ausschalten
|
||||||
p110.turnOff()
|
p100.turnOff()
|
||||||
monitor_logger.debug(f"✅ Tapo-Steckdose {ip_address} erfolgreich ausgeschaltet")
|
monitor_logger.debug(f"✅ Tapo-Steckdose {ip_address} erfolgreich ausgeschaltet")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@ -403,13 +403,14 @@ class PrinterMonitor:
|
|||||||
monitor_logger.debug(f"🔧 Verwende globale Tapo-Anmeldedaten für {ip_address}")
|
monitor_logger.debug(f"🔧 Verwende globale Tapo-Anmeldedaten für {ip_address}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# TP-Link Tapo P110 Verbindung herstellen
|
# TP-Link Tapo P100 Verbindung herstellen (P100 statt P110 verwenden)
|
||||||
p110 = PyP110.P110(ip_address, username, password)
|
from PyP100 import PyP100
|
||||||
p110.handshake() # Authentifizierung
|
p100 = PyP100.P100(ip_address, username, password)
|
||||||
p110.login() # Login
|
p100.handshake() # Authentifizierung
|
||||||
|
p100.login() # Login
|
||||||
|
|
||||||
# Geräteinformationen abrufen
|
# Geräteinformationen abrufen
|
||||||
device_info = p110.getDeviceInfo()
|
device_info = p100.getDeviceInfo()
|
||||||
|
|
||||||
# Status auswerten
|
# Status auswerten
|
||||||
device_on = device_info.get('device_on', False)
|
device_on = device_info.get('device_on', False)
|
||||||
@ -489,10 +490,10 @@ class PrinterMonitor:
|
|||||||
# Tapo-Verbindung testen
|
# Tapo-Verbindung testen
|
||||||
if TAPO_AVAILABLE:
|
if TAPO_AVAILABLE:
|
||||||
try:
|
try:
|
||||||
p110 = PyP110.P110(ip, TAPO_USERNAME, TAPO_PASSWORD)
|
p100 = PyP100.P100(ip, TAPO_USERNAME, TAPO_PASSWORD)
|
||||||
p110.handshake()
|
p100.handshake()
|
||||||
p110.login()
|
p100.login()
|
||||||
device_info = p110.getDeviceInfo()
|
device_info = p100.getDeviceInfo()
|
||||||
|
|
||||||
# Steckdose gefunden und verbunden
|
# Steckdose gefunden und verbunden
|
||||||
nickname = device_info.get('nickname', f"Tapo P110 ({ip})")
|
nickname = device_info.get('nickname', f"Tapo P110 ({ip})")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user