🎉 Improved documentation and code organization in Backend 🌐
This commit is contained in:
136
backend/app.py
136
backend/app.py
@@ -943,85 +943,26 @@ def api_get_printers():
|
||||
@app.route("/api/printers/status", methods=["GET"])
|
||||
@login_required
|
||||
def api_get_printer_status():
|
||||
"""API-Endpunkt für Drucker-Status"""
|
||||
"""API-Endpunkt für Drucker-Status mit verbessertem Status-Management"""
|
||||
try:
|
||||
from models import get_db_session, Printer
|
||||
# Verwende den neuen TapoStatusManager
|
||||
from utils.tapo_status_manager import tapo_status_manager
|
||||
|
||||
db_session = get_db_session()
|
||||
# Alle Drucker für Status-Abfragen anzeigen (unabhängig von active-Status)
|
||||
printers = db_session.query(Printer).all()
|
||||
# Status für alle Drucker abrufen
|
||||
status_list = tapo_status_manager.get_all_printer_status()
|
||||
|
||||
status_list = []
|
||||
|
||||
# Tapo-Controller nur importieren, wenn benötigt
|
||||
tapo_controller = None
|
||||
has_tapo_printers = any(printer.plug_ip for printer in printers)
|
||||
|
||||
if has_tapo_printers:
|
||||
try:
|
||||
from utils.hardware_integration import tapo_controller
|
||||
app_logger.info(f"✅ Tapo-Controller erfolgreich importiert: {type(tapo_controller)}")
|
||||
except Exception as import_error:
|
||||
app_logger.warning(f"⚠️ Tapo-Controller konnte nicht importiert werden: {str(import_error)}")
|
||||
tapo_controller = None
|
||||
|
||||
for printer in printers:
|
||||
# Basis-Status-Informationen
|
||||
status_dict = {
|
||||
"id": printer.id,
|
||||
"name": printer.name,
|
||||
"status": printer.status or "offline",
|
||||
"location": printer.location,
|
||||
"model": printer.model,
|
||||
"ip_address": printer.ip_address,
|
||||
"active": getattr(printer, 'active', True)
|
||||
}
|
||||
|
||||
# Tapo-Steckdosen-Status prüfen, wenn verfügbar
|
||||
if printer.plug_ip:
|
||||
if tapo_controller:
|
||||
try:
|
||||
reachable, plug_status = tapo_controller.check_outlet_status(
|
||||
printer.plug_ip,
|
||||
printer_id=printer.id
|
||||
)
|
||||
|
||||
status_dict.update({
|
||||
"plug_status": plug_status,
|
||||
"plug_reachable": reachable,
|
||||
"plug_ip": printer.plug_ip,
|
||||
"has_plug": True
|
||||
})
|
||||
except Exception as e:
|
||||
app_logger.warning(f"⚠️ Fehler bei Steckdosen-Status für {printer.name}: {str(e)}")
|
||||
status_dict.update({
|
||||
"plug_status": "error",
|
||||
"plug_reachable": False,
|
||||
"plug_ip": printer.plug_ip,
|
||||
"has_plug": True,
|
||||
"plug_error": str(e)
|
||||
})
|
||||
else:
|
||||
# Tapo-Controller nicht verfügbar
|
||||
status_dict.update({
|
||||
"plug_status": "unavailable",
|
||||
"plug_reachable": False,
|
||||
"plug_ip": printer.plug_ip,
|
||||
"has_plug": True,
|
||||
"plug_error": "Tapo-Controller nicht verfügbar"
|
||||
})
|
||||
# Erweitere Status mit UI-freundlichen Informationen
|
||||
for status in status_list:
|
||||
# Status-Display-Informationen hinzufügen
|
||||
plug_status = status.get("plug_status", "unknown")
|
||||
if plug_status in tapo_status_manager.STATUS_DISPLAY:
|
||||
status["status_display"] = tapo_status_manager.STATUS_DISPLAY[plug_status]
|
||||
else:
|
||||
# Kein Smart-Plug konfiguriert
|
||||
status_dict.update({
|
||||
"plug_status": "no_plug",
|
||||
"plug_reachable": False,
|
||||
"plug_ip": None,
|
||||
"has_plug": False
|
||||
})
|
||||
|
||||
status_list.append(status_dict)
|
||||
|
||||
db_session.close()
|
||||
status["status_display"] = {
|
||||
"text": "Unbekannt",
|
||||
"color": "gray",
|
||||
"icon": "question"
|
||||
}
|
||||
|
||||
app_logger.info(f"✅ API: Status für {len(status_list)} Drucker abgerufen")
|
||||
|
||||
@@ -1035,13 +976,44 @@ def api_get_printer_status():
|
||||
|
||||
except Exception as e:
|
||||
app_logger.error(f"❌ API-Fehler beim Abrufen des Drucker-Status: {str(e)}", exc_info=True)
|
||||
return jsonify({
|
||||
"success": False,
|
||||
"error": "Fehler beim Laden des Drucker-Status",
|
||||
"details": str(e),
|
||||
"printers": [],
|
||||
"count": 0
|
||||
}), 500
|
||||
|
||||
# Fallback: Mindestens die Drucker-Grunddaten zurückgeben
|
||||
try:
|
||||
from models import get_db_session, Printer
|
||||
db_session = get_db_session()
|
||||
printers = db_session.query(Printer).all()
|
||||
|
||||
basic_status = []
|
||||
for printer in printers:
|
||||
basic_status.append({
|
||||
"id": printer.id,
|
||||
"name": printer.name,
|
||||
"location": printer.location,
|
||||
"model": printer.model,
|
||||
"plug_status": "unreachable",
|
||||
"plug_reachable": False,
|
||||
"has_plug": bool(printer.plug_ip),
|
||||
"error": "Status-Manager nicht verfügbar"
|
||||
})
|
||||
|
||||
db_session.close()
|
||||
|
||||
return jsonify({
|
||||
"success": False,
|
||||
"error": "Eingeschränkte Status-Informationen",
|
||||
"printers": basic_status,
|
||||
"count": len(basic_status),
|
||||
"timestamp": datetime.now().isoformat()
|
||||
})
|
||||
|
||||
except:
|
||||
return jsonify({
|
||||
"success": False,
|
||||
"error": "Fehler beim Laden des Drucker-Status",
|
||||
"details": str(e),
|
||||
"printers": [],
|
||||
"count": 0
|
||||
}), 500
|
||||
|
||||
@app.route("/api/health", methods=["GET"])
|
||||
def api_health_check():
|
||||
|
Reference in New Issue
Block a user