📚 Improved backend structure & logging (#123) - Streamlined database connections, optimized log files, and refactored configurations for better maintainability. 🌳🔍📈💻🖥️
This commit is contained in:
Binary file not shown.
Binary file not shown.
@ -418,60 +418,53 @@ def control_printer_power(printer_id):
|
||||
}), 404
|
||||
|
||||
# Prüfen, ob Drucker eine Steckdose konfiguriert hat
|
||||
if not printer.plug_ip or not printer.plug_username or not printer.plug_password:
|
||||
if not printer.plug_ip:
|
||||
db_session.close()
|
||||
return jsonify({
|
||||
"success": False,
|
||||
"error": f"Drucker {printer.name} hat keine Steckdose konfiguriert"
|
||||
}), 400
|
||||
|
||||
# Steckdose steuern
|
||||
from PyP100 import PyP110
|
||||
|
||||
db_session.close() # Session früh schließen
|
||||
|
||||
# Hardware-Integration für Steuerung verwenden (zentrale Logik)
|
||||
try:
|
||||
# TP-Link Tapo P110 Verbindung herstellen
|
||||
p110 = PyP110.P110(printer.plug_ip, printer.plug_username, printer.plug_password)
|
||||
p110.handshake() # Authentifizierung
|
||||
p110.login() # Login
|
||||
drucker_steuerung = get_drucker_steuerung()
|
||||
|
||||
# Steckdose ein- oder ausschalten
|
||||
if action == "on":
|
||||
p110.turnOn()
|
||||
success = True
|
||||
message = "Steckdose erfolgreich eingeschaltet"
|
||||
printer.status = "starting" # Status aktualisieren
|
||||
ergebnis = drucker_steuerung.drucker_einschalten(
|
||||
printer_id,
|
||||
grund=f"API-Steuerung von {current_user.name}"
|
||||
)
|
||||
else:
|
||||
p110.turnOff()
|
||||
success = True
|
||||
message = "Steckdose erfolgreich ausgeschaltet"
|
||||
printer.status = "offline" # Status aktualisieren
|
||||
ergebnis = drucker_steuerung.drucker_ausschalten(
|
||||
printer_id,
|
||||
grund=f"API-Steuerung von {current_user.name}"
|
||||
)
|
||||
|
||||
if ergebnis['success']:
|
||||
printers_logger.info(f"✅ {action.upper()}: Drucker {printer.name} erfolgreich über API gesteuert")
|
||||
return jsonify({
|
||||
"success": True,
|
||||
"message": ergebnis['message'],
|
||||
"printer_id": printer_id,
|
||||
"printer_name": printer.name,
|
||||
"action": action,
|
||||
"timestamp": datetime.now().isoformat()
|
||||
})
|
||||
else:
|
||||
printers_logger.error(f"❌ API-Steuerung für {printer.name} fehlgeschlagen: {ergebnis['error']}")
|
||||
return jsonify({
|
||||
"success": False,
|
||||
"error": ergebnis['error']
|
||||
}), 500
|
||||
|
||||
# Zeitpunkt der letzten Prüfung aktualisieren
|
||||
printer.last_checked = datetime.now()
|
||||
db_session.commit()
|
||||
|
||||
# Cache leeren, damit neue Status-Abfragen aktuell sind
|
||||
monitor = get_printer_monitor()
|
||||
monitor.clear_all_caches()
|
||||
|
||||
printers_logger.info(f"✅ {action.upper()}: Drucker {printer.name} erfolgreich {message}")
|
||||
|
||||
except Exception as e:
|
||||
printers_logger.error(f"❌ Fehler bei Steckdosensteuerung für {printer.name}: {str(e)}")
|
||||
db_session.close()
|
||||
printers_logger.error(f"❌ Fehler bei Hardware-Integration für {printer.name}: {str(e)}")
|
||||
return jsonify({
|
||||
"success": False,
|
||||
"error": f"Fehler bei Steckdosensteuerung: {str(e)}"
|
||||
"error": f"Hardware-Integrationsfehler: {str(e)}"
|
||||
}), 500
|
||||
|
||||
db_session.close()
|
||||
return jsonify({
|
||||
"success": True,
|
||||
"message": message,
|
||||
"printer_id": printer_id,
|
||||
"printer_name": printer.name,
|
||||
"action": action,
|
||||
"timestamp": datetime.now().isoformat()
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
printers_logger.error(f"❌ Allgemeiner Fehler bei Stromsteuerung: {str(e)}")
|
||||
|
Reference in New Issue
Block a user