"feat: Implement printer test suite in backend"

This commit is contained in:
2025-05-26 12:58:23 +02:00
parent 2643ef1814
commit cf077ffcb8
3 changed files with 146 additions and 18 deletions

View File

@@ -136,28 +136,43 @@ def check_printer_status(ip_address: str, timeout: int = 7) -> Tuple[str, bool]:
Returns:
Tuple[str, bool]: (Status, Aktiv) - Status ist "online" oder "offline", Aktiv ist True/False
"""
if not ip_address:
if not ip_address or ip_address.strip() == "":
printers_logger.debug(f"Keine IP-Adresse angegeben")
return "offline", False
try:
# IP-Adresse validieren
import ipaddress
try:
ipaddress.ip_address(ip_address.strip())
except ValueError:
printers_logger.warning(f"Ungültige IP-Adresse: {ip_address}")
return "offline", False
# Windows-spezifischer Ping-Befehl mit Timeout
if os.name == 'nt': # Windows
cmd = ['ping', '-n', '1', '-w', str(timeout * 1000), ip_address]
cmd = ['ping', '-n', '1', '-w', str(timeout * 1000), ip_address.strip()]
else: # Unix/Linux/macOS
cmd = ['ping', '-c', '1', '-W', str(timeout), ip_address]
cmd = ['ping', '-c', '1', '-W', str(timeout), ip_address.strip()]
printers_logger.debug(f"Ping-Befehl für {ip_address}: {' '.join(cmd)}")
# Ping ausführen mit Timeout
result = subprocess.run(
cmd,
capture_output=True,
text=True,
timeout=timeout + 1 # Zusätzlicher Timeout für subprocess
encoding='utf-8',
errors='ignore', # Ignoriere Unicode-Fehler
timeout=timeout + 2 # Zusätzlicher Timeout für subprocess
)
# Erfolgreicher Ping (Return Code 0)
if result.returncode == 0:
printers_logger.debug(f"Ping erfolgreich für {ip_address}")
return "online", True
else:
printers_logger.debug(f"Ping fehlgeschlagen für {ip_address} (Return Code: {result.returncode})")
return "offline", False
except subprocess.TimeoutExpired:
@@ -869,36 +884,50 @@ def get_printers_status():
# Drucker-Daten für Status-Check vorbereiten
printer_data = []
for printer in printers:
# Verwende plug_ip als primäre IP-Adresse, fallback auf ip_address
ip_to_check = printer.plug_ip if printer.plug_ip else printer.ip_address
printer_data.append({
'id': printer.id,
'name': printer.name,
'ip_address': printer.ip_address,
'ip_address': ip_to_check,
'location': printer.location
})
# Status aller Drucker parallel überprüfen mit 7-Sekunden-Timeout
printers_logger.info(f"Starte Status-Check für {len(printer_data)} Drucker mit 7-Sekunden-Timeout")
status_results = check_multiple_printers_status(printer_data, timeout=7)
# Fallback: Wenn keine IP-Adressen vorhanden sind, alle als offline markieren
if not any(p['ip_address'] for p in printer_data):
printers_logger.warning("Keine IP-Adressen für Drucker gefunden - alle als offline markiert")
status_results = {p['id']: ("offline", False) for p in printer_data}
else:
status_results = check_multiple_printers_status(printer_data, timeout=7)
# Ergebnisse zusammenstellen und Datenbank aktualisieren
status_data = []
for printer in printers:
if printer.id in status_results:
status, active = status_results[printer.id]
# Mapping für Frontend-Kompatibilität
if status == "online":
frontend_status = "available"
else:
frontend_status = "offline"
else:
# Fallback falls kein Ergebnis vorliegt
status, active = "offline", False
frontend_status = "offline"
active = False
# Status in der Datenbank aktualisieren
printer.status = status
printer.status = frontend_status
printer.active = active
status_data.append({
"id": printer.id,
"name": printer.name,
"status": status,
"status": frontend_status,
"active": active,
"ip_address": printer.ip_address,
"ip_address": printer.plug_ip if printer.plug_ip else printer.ip_address,
"location": printer.location,
"last_checked": datetime.now().isoformat()
})
@@ -907,7 +936,8 @@ def get_printers_status():
db_session.commit()
db_session.close()
printers_logger.info(f"Status-Check abgeschlossen: {len([s for s in status_data if s['status'] == 'online'])} von {len(status_data)} Drucker online")
online_count = len([s for s in status_data if s['status'] == 'available'])
printers_logger.info(f"Status-Check abgeschlossen: {online_count} von {len(status_data)} Drucker online")
return jsonify(status_data)
except Exception as e: