87 lines
3.0 KiB
Python
87 lines
3.0 KiB
Python
# app/routes/main.py
|
|
import os
|
|
import logging
|
|
import json
|
|
|
|
from flask import Blueprint, render_template, redirect, url_for, request, jsonify, send_from_directory, current_app
|
|
from app.utils.helpers import load_configs, load_default_sounds
|
|
import app.state as state
|
|
|
|
from config import Config
|
|
|
|
main_bp = Blueprint('main', __name__)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
@main_bp.route('/')
|
|
def index():
|
|
configs = load_configs(current_app.config['CONFIG_DIR'])
|
|
return render_template('index.html', configs=configs)
|
|
|
|
"""
|
|
@app.route('/')
|
|
def index():
|
|
return render_template('index.html', configs=load_configs())
|
|
"""
|
|
|
|
|
|
@main_bp.route('/configs/<path:filename>')
|
|
def serve_config_file(filename):
|
|
"""
|
|
Serviert Bilder aus dem configs-Ordner (für Lok-Bilder in Kacheln)
|
|
"""
|
|
try:
|
|
return send_from_directory(current_app.config['CONFIG_DIR'], filename)
|
|
except Exception as e:
|
|
current_app.logger.error(f"Fehler beim Servieren von {filename}: {e}")
|
|
return "Datei nicht gefunden", 404
|
|
|
|
|
|
@main_bp.route('/load_config/<filename>')
|
|
def load_config(filename):
|
|
path = os.path.join(current_app.config['CONFIG_DIR'], filename)
|
|
|
|
if not os.path.exists(path):
|
|
logger.error(f"Config nicht gefunden: {path}")
|
|
return "Konfiguration nicht gefunden", 404
|
|
|
|
try:
|
|
with open(path, 'r', encoding='utf-8') as f:
|
|
state.current_config = json.load(f)
|
|
state.current_config['filename'] = filename
|
|
logger.info(f"Config erfolgreich geladen: {filename} mit {len(state.current_config.get('channels', []))} Kanälen")
|
|
return redirect(url_for('main.control_page'))
|
|
except json.JSONDecodeError as e:
|
|
logger.error(f"Ungültiges JSON in {filename}: {e}")
|
|
return "Ungültiges JSON-Format in der Konfigurationsdatei", 500
|
|
except Exception as e:
|
|
logger.error(f"Ladefehler {filename}: {e}")
|
|
return "Fehler beim Laden der Konfiguration", 500
|
|
|
|
|
|
@main_bp.route('/control')
|
|
def control_page():
|
|
if state.current_config is None:
|
|
logger.warning("current_config ist None → Redirect zu index")
|
|
return redirect(url_for('main.index'))
|
|
|
|
# Globale Sounds immer laden
|
|
global_sounds = load_default_sounds(current_app.config['CONFIG_DIR'])
|
|
|
|
logger.info(f"Steuerseite geladen für Config: {state.current_config.get('filename', 'unbekannt')} mit {len(state.current_config.get('channels', []))} Kanälen")
|
|
|
|
logger.info(f"Übergebe config an Template: {state.current_config}")
|
|
print("DEBUG: config hat channels?", 'channels' in state.current_config, len(state.current_config.get('channels', [])))
|
|
|
|
return render_template('control.html', config=state.current_config, global_sounds=global_sounds)
|
|
|
|
|
|
@main_bp.route('/soundboard')
|
|
def soundboard():
|
|
if state.current_config is None or 'sounds' not in state.current_config:
|
|
return redirect(url_for('main.index'))
|
|
|
|
sounds = state.current_config.get('sounds', [])
|
|
return render_template('soundboard.html', sounds=sounds, config=state.current_config)
|
|
pass
|