🎉 Refactor Backend Database Files & Add Session Logs 📚💄
This commit is contained in:
@ -43,7 +43,7 @@ _cache_lock = threading.Lock()
|
||||
_cache_ttl = {} # Time-to-live für Cache-Einträge
|
||||
|
||||
# Alle exportierten Modelle
|
||||
__all__ = ['User', 'Printer', 'Job', 'Stats', 'SystemLog', 'Base', 'GuestRequest', 'UserPermission', 'Notification', 'JobOrder', 'SystemTimer', 'PlugStatusLog', 'init_db', 'init_database', 'create_initial_admin', 'get_db_session', 'get_cached_session', 'clear_cache', 'engine']
|
||||
__all__ = ['User', 'Printer', 'Job', 'Stats', 'SystemLog', 'Base', 'GuestRequest', 'UserPermission', 'Notification', 'JobOrder', 'SystemTimer', 'PlugStatusLog', 'init_db', 'init_database', 'create_initial_admin', 'create_initial_printers', 'get_db_session', 'get_cached_session', 'clear_cache', 'engine']
|
||||
|
||||
# ===== DATENBANK-KONFIGURATION MIT WAL UND OPTIMIERUNGEN =====
|
||||
|
||||
@ -2121,6 +2121,124 @@ def create_initial_admin(email: str = "admin@mercedes-benz.com", password: str =
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Fehler beim Erstellen des Admin-Benutzers: {str(e)}")
|
||||
return False
|
||||
|
||||
|
||||
def create_initial_printers() -> bool:
|
||||
"""
|
||||
Erstellt die statischen Drucker für Mercedes-Benz TBA Marienfelde.
|
||||
|
||||
Diese Funktion erstellt automatisch die konfigurierten Drucker mit festen IP-Adressen
|
||||
und Standorten für die Production-Umgebung.
|
||||
|
||||
Returns:
|
||||
bool: True, wenn die Drucker erfolgreich erstellt/aktualisiert wurden, False sonst
|
||||
"""
|
||||
# Statische Drucker-Konfiguration für TBA Marienfelde
|
||||
STATIC_PRINTERS = [
|
||||
{
|
||||
"name": "Drucker 1",
|
||||
"ip_address": "192.168.0.100",
|
||||
"location": "TBA Marienfelde",
|
||||
"model": "Mercedes 3D Printer",
|
||||
"plug_ip": "192.168.0.100", # Smart Plug hat gleiche IP
|
||||
"status": "offline"
|
||||
},
|
||||
{
|
||||
"name": "Drucker 2",
|
||||
"ip_address": "192.168.0.101",
|
||||
"location": "TBA Marienfelde",
|
||||
"model": "Mercedes 3D Printer",
|
||||
"plug_ip": "192.168.0.101",
|
||||
"status": "offline"
|
||||
},
|
||||
{
|
||||
"name": "Drucker 3",
|
||||
"ip_address": "192.168.0.102",
|
||||
"location": "TBA Marienfelde",
|
||||
"model": "Mercedes 3D Printer",
|
||||
"plug_ip": "192.168.0.102",
|
||||
"status": "offline"
|
||||
},
|
||||
{
|
||||
"name": "Drucker 4",
|
||||
"ip_address": "192.168.0.103",
|
||||
"location": "TBA Marienfelde",
|
||||
"model": "Mercedes 3D Printer",
|
||||
"plug_ip": "192.168.0.103",
|
||||
"status": "offline"
|
||||
},
|
||||
{
|
||||
"name": "Drucker 5",
|
||||
"ip_address": "192.168.0.104",
|
||||
"location": "TBA Marienfelde",
|
||||
"model": "Mercedes 3D Printer",
|
||||
"plug_ip": "192.168.0.104",
|
||||
"status": "offline"
|
||||
},
|
||||
{
|
||||
"name": "Drucker 6",
|
||||
"ip_address": "192.168.0.106",
|
||||
"location": "TBA Marienfelde",
|
||||
"model": "Mercedes 3D Printer",
|
||||
"plug_ip": "192.168.0.106",
|
||||
"status": "offline"
|
||||
}
|
||||
]
|
||||
|
||||
try:
|
||||
with get_cached_session() as session:
|
||||
created_count = 0
|
||||
updated_count = 0
|
||||
|
||||
for printer_config in STATIC_PRINTERS:
|
||||
# Prüfen ob Drucker bereits existiert (nach IP-Adresse)
|
||||
existing_printer = session.query(Printer).filter(
|
||||
Printer.ip_address == printer_config["ip_address"]
|
||||
).first()
|
||||
|
||||
if existing_printer:
|
||||
# Bestehenden Drucker aktualisieren
|
||||
existing_printer.name = printer_config["name"]
|
||||
existing_printer.location = printer_config["location"]
|
||||
existing_printer.model = printer_config["model"]
|
||||
existing_printer.plug_ip = printer_config["plug_ip"]
|
||||
existing_printer.status = printer_config["status"]
|
||||
existing_printer.active = True
|
||||
existing_printer.updated_at = datetime.now()
|
||||
updated_count += 1
|
||||
logger.info(f"Drucker aktualisiert: {printer_config['name']} ({printer_config['ip_address']})")
|
||||
else:
|
||||
# Neuen Drucker erstellen
|
||||
new_printer = Printer(
|
||||
name=printer_config["name"],
|
||||
ip_address=printer_config["ip_address"],
|
||||
location=printer_config["location"],
|
||||
model=printer_config["model"],
|
||||
plug_ip=printer_config["plug_ip"],
|
||||
status=printer_config["status"],
|
||||
active=True,
|
||||
created_at=datetime.now(),
|
||||
updated_at=datetime.now()
|
||||
)
|
||||
session.add(new_printer)
|
||||
created_count += 1
|
||||
logger.info(f"Drucker erstellt: {printer_config['name']} ({printer_config['ip_address']})")
|
||||
|
||||
session.commit()
|
||||
|
||||
# Cache für Drucker invalidieren
|
||||
invalidate_model_cache("Printer")
|
||||
|
||||
total_operations = created_count + updated_count
|
||||
logger.info(f"✅ Statische Drucker-Initialisierung abgeschlossen: {created_count} erstellt, {updated_count} aktualisiert")
|
||||
logger.info(f"📍 Alle Drucker sind für Standort 'TBA Marienfelde' konfiguriert")
|
||||
logger.info(f"🌐 IP-Bereich: 192.168.0.100-106 (außer .105)")
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"❌ Fehler beim Erstellen/Aktualisieren der statischen Drucker: {str(e)}")
|
||||
return False
|
||||
|
||||
# Engine für Export verfügbar machen
|
||||
|
Reference in New Issue
Block a user