"Add test files for P110 and Tapo Direct integration in backend"

This commit is contained in:
2025-05-29 22:17:10 +02:00
parent c7c27452e7
commit b3a1c66f8b
3 changed files with 235 additions and 17 deletions

View File

@@ -341,37 +341,42 @@ class PrinterMonitor:
def _ping_address(self, ip_address: str, timeout: int = 3) -> bool:
"""
Führt einen Ping-Test zu einer IP-Adresse durch.
Führt einen Konnektivitätstest zu einer IP-Adresse durch.
Verwendet TCP-Verbindung statt Ping, um Encoding-Probleme zu vermeiden.
Args:
ip_address: Zu testende IP-Adresse
timeout: Timeout in Sekunden
Returns:
bool: True wenn Ping erfolgreich
bool: True wenn Verbindung erfolgreich
"""
try:
# IP-Adresse validieren
ipaddress.ip_address(ip_address.strip())
# Platform-spezifische Ping-Befehle
if os.name == 'nt': # Windows
cmd = ['ping', '-n', '1', '-w', str(timeout * 1000), ip_address.strip()]
else: # Unix/Linux/macOS
cmd = ['ping', '-c', '1', '-W', str(timeout), ip_address.strip()]
# TCP-Verbindung zu Port 80 (HTTP) oder 443 (HTTPS) versuchen
import socket
result = subprocess.run(
cmd,
capture_output=True,
text=True,
encoding='utf-8',
errors='ignore',
timeout=timeout + 1
)
# Erst Port 80 versuchen (HTTP)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(timeout)
result = sock.connect_ex((ip_address.strip(), 80))
sock.close()
return result.returncode == 0
if result == 0:
return True
# Falls Port 80 nicht erfolgreich, Port 443 versuchen (HTTPS)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(timeout)
result = sock.connect_ex((ip_address.strip(), 443))
sock.close()
except Exception:
return result == 0
except Exception as e:
monitor_logger.debug(f"❌ Fehler beim Verbindungstest zu {ip_address}: {str(e)}")
return False
def _check_outlet_status(self, ip_address: str, username: str, password: str, timeout: int = 5) -> Tuple[bool, str]: