This commit is contained in:
root
2025-03-12 12:33:05 +01:00
parent 6cdc437d3e
commit 3972860be8
185 changed files with 33 additions and 27 deletions

0
backend/.gitignore vendored Normal file → Executable file
View File

0
backend/Dockerfile Normal file → Executable file
View File

View File

@ -32,8 +32,8 @@ app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=7)
# Steckdosen-Konfiguration
TAPO_USERNAME = os.environ.get('TAPO_USERNAME')
TAPO_PASSWORD = os.environ.get('TAPO_PASSWORD')
# SOCKET_DEVICES Format: {"192.168.1.100": {"number": "1"}, "192.168.1.101": {"number": "2"}, ...}
SOCKET_DEVICES = json.loads(os.environ.get('SOCKET_DEVICES', '{}'))
# PRINTERS Format: {"Printer 1": {"ip": "192.168.1.100"}, "Printer 2": {"ip": "192.168.1.101"}, ...}
PRINTERS = json.loads(os.environ.get('PRINTERS', '{}'))
# Logging
if not os.path.exists('logs'):
@ -111,42 +111,48 @@ def init_db():
# Initialisierung der Steckdosen
def init_sockets():
"""
Initialisiert die Steckdosen-Einträge in der Datenbank basierend auf SOCKET_DEVICES Umgebungsvariable.
Initialisiert die Steckdosen-Einträge in der Datenbank basierend auf PRINTERS Umgebungsvariable.
Stellt sicher, dass alle Steckdosen zu Beginn ausgeschaltet sind.
"""
app.logger.info("Initialisiere Steckdosen aus Umgebungsvariablen")
app.logger.info("Initialisiere Drucker aus Umgebungsvariablen")
db = get_db()
# Alle IP-Adressen aus der Datenbank abrufen
existing_ips = {row['ip_address']: row['id'] for row in db.execute('SELECT id, ip_address FROM socket').fetchall() if row['ip_address']}
# Alle Druckernamen aus der Datenbank abrufen
existing_printers = {row['name']: {'id': row['id'], 'ip': row['ip_address']}
for row in db.execute('SELECT id, name, ip_address FROM socket').fetchall()}
for ip_address, device_config in SOCKET_DEVICES.items():
socket_number = device_config.get('number', '0')
name = f"Printer {socket_number}"
for printer_name, printer_config in PRINTERS.items():
ip_address = printer_config.get('ip', '')
description = f"3D-Drucker mit SmartPlug (IP: {ip_address})"
if ip_address in existing_ips:
# Steckdose existiert bereits, nichts zu tun
app.logger.info(f"Steckdose mit IP {ip_address} existiert bereits in der Datenbank")
socket_id = existing_ips[ip_address]
if printer_name in existing_printers:
# Drucker existiert bereits, überprüfe auf Änderungen an der IP
socket_id = existing_printers[printer_name]['id']
if existing_printers[printer_name]['ip'] != ip_address:
# Aktualisiere die IP-Adresse, wenn sie sich geändert hat
update_socket(socket_id, ip_address=ip_address)
app.logger.info(f"IP-Adresse für Drucker {printer_name} aktualisiert: {ip_address}")
else:
app.logger.info(f"Drucker {printer_name} existiert bereits in der Datenbank")
else:
# Steckdose erstellen, wenn sie noch nicht existiert
socket = create_socket(name=name, description=description, ip_address=ip_address, status=0)
# Drucker erstellen, wenn er noch nicht existiert
socket = create_socket(name=printer_name, description=description, ip_address=ip_address, status=0)
socket_id = socket['id']
app.logger.info(f"Neue Steckdose angelegt: {name} mit IP {ip_address}")
app.logger.info(f"Neuer Drucker angelegt: {printer_name} mit IP {ip_address}")
# Steckdose ausschalten, um sicherzustellen, dass alle Steckdosen im AUS-Zustand starten
try:
turn_off_socket(ip_address)
app.logger.info(f"Steckdose {ip_address} wurde beim Start ausgeschaltet")
except Exception as e:
app.logger.error(f"Fehler beim Ausschalten der Steckdose {ip_address}: {e}")
if ip_address:
try:
turn_off_socket(ip_address)
app.logger.info(f"Steckdose für {printer_name} (IP: {ip_address}) wurde beim Start ausgeschaltet")
except Exception as e:
app.logger.error(f"Fehler beim Ausschalten der Steckdose für {printer_name} (IP: {ip_address}): {e}")
# Initialisiere die Datenbank und Steckdosen beim Starten der Anwendung
with app.app_context():
init_db()
# Nur initialisieren, wenn Steckdosen konfiguriert sind
if SOCKET_DEVICES:
# Nur initialisieren, wenn Drucker konfiguriert sind
if PRINTERS:
init_sockets()
app.teardown_appcontext(close_db)

View File

@ -45,7 +45,7 @@ CREATE TABLE IF NOT EXISTS session (
FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS printer (
CREATE TABLE IF NOT EXISTS socket (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
description TEXT NOT NULL,
@ -53,16 +53,16 @@ CREATE TABLE IF NOT EXISTS printer (
ip_address TEXT
);
CREATE TABLE IF NOT EXISTS print_job (
CREATE TABLE IF NOT EXISTS job (
id TEXT PRIMARY KEY,
printer_id TEXT NOT NULL,
socket_id TEXT NOT NULL,
user_id TEXT NOT NULL,
start_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
duration_in_minutes INTEGER NOT NULL,
comments TEXT,
aborted INTEGER DEFAULT 0,
abort_reason TEXT,
FOREIGN KEY (printer_id) REFERENCES printer (id) ON DELETE CASCADE,
FOREIGN KEY (socket_id) REFERENCES socket (id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE
);
EOF

0
backend/docker-compose.yml Normal file → Executable file
View File