- Calendar Profile Scheduler: pianificazione temporizzata con switch automatico e rollback del profilo a fine evento - Tactical APRS Map: tracciamento stazioni in mobilità, filtro dei nodi di flotta e garbage collection dopo 2h - Dynamic Timeframe Analytics: filtri temporali 12H, 24H, 48H, 7D sui grafici Chart.js con etichette intelligenti - Time Machine & Visual Diff: storico backup configurazioni .ini con visualizzatore differenze semantico - Header UX Refactor: riorganizzazione a tre righe compatte con integrazione badge operatori online in tempo reale - Complete i18n: allineamento del dizionario bilingue (IT/EN) su tutta la piattaforma
106 lines
3.5 KiB
Python
106 lines
3.5 KiB
Python
import gevent.monkey
|
|
gevent.monkey.patch_all()
|
|
|
|
import os
|
|
import subprocess
|
|
import threading
|
|
from flask import Flask, render_template, request
|
|
from flask_socketio import emit
|
|
|
|
# --- IMPORT MODULI CORE ---
|
|
from core.config import BASE_DIR, get_secret_key, web_cfg, logger
|
|
from core.database import init_db
|
|
import core.state as state
|
|
|
|
# --- IMPORT SERVIZI ---
|
|
from services.push_service import push_worker
|
|
from services.watchdog_service import watchdog_loop
|
|
from services.scheduler_service import auto_update_ids
|
|
import services.mqtt_service as mqtt_svc
|
|
|
|
# --- IMPORT ROTTE / BLUEPRINTS ---
|
|
from routes.auth_routes import auth_bp
|
|
from routes.api_routes import api_bp
|
|
|
|
# Inizializzazione Flask
|
|
template_dir = os.path.join(BASE_DIR, 'templates') if os.path.isdir(os.path.join(BASE_DIR, 'templates')) else BASE_DIR
|
|
app = Flask(__name__, template_folder=template_dir)
|
|
|
|
# Configurazione Sicurezza Sessioni
|
|
app.secret_key = get_secret_key()
|
|
app.config.update(
|
|
SESSION_COOKIE_HTTPONLY=True,
|
|
SESSION_COOKIE_SAMESITE='Lax',
|
|
SESSION_COOKIE_SECURE=bool(web_cfg.get('cookie_secure', False)),
|
|
PERMANENT_SESSION_LIFETIME=int(web_cfg.get('session_hours', 12)) * 3600,
|
|
)
|
|
|
|
# Collegamento SocketIO
|
|
state.socketio.init_app(app)
|
|
|
|
# Registrazione Blueprint
|
|
app.register_blueprint(auth_bp)
|
|
app.register_blueprint(api_bp)
|
|
|
|
# Security Headers
|
|
@app.after_request
|
|
def add_security_headers(resp):
|
|
resp.headers.setdefault('X-Content-Type-Options', 'nosniff')
|
|
resp.headers.setdefault('X-Frame-Options', 'DENY')
|
|
resp.headers.setdefault('Referrer-Policy', 'same-origin')
|
|
if request.path.startswith('/api/'):
|
|
resp.headers['Cache-Control'] = 'no-store'
|
|
return resp
|
|
|
|
def get_git_commit():
|
|
try:
|
|
return subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'], cwd=BASE_DIR).decode('utf-8').strip()
|
|
except Exception:
|
|
return "sconosciuta"
|
|
|
|
SERVER_COMMIT = get_git_commit()
|
|
|
|
@app.route('/')
|
|
def index():
|
|
return render_template('index.html', git_commit=SERVER_COMMIT)
|
|
|
|
from flask import session
|
|
|
|
@state.socketio.on('connect')
|
|
def handle_connect():
|
|
state.connected_web_clients.add(request.sid)
|
|
|
|
# Se l'utente è loggato (session), associamo il suo username al SID
|
|
user = session.get('user')
|
|
if user:
|
|
state.active_logged_users[request.sid] = user
|
|
|
|
emit('mqtt_status', {'connected': mqtt_svc.mqtt_connected_status})
|
|
|
|
# Prepariamo la lista unica degli utenti loggati attivi (togliendo i duplicati se un utente ha più tab aperti)
|
|
logged_names = list(set(state.active_logged_users.values()))
|
|
state.socketio.emit('web_users_count', {'count': len(state.connected_web_clients), 'users': logged_names})
|
|
|
|
@state.socketio.on('disconnect')
|
|
def handle_disconnect():
|
|
state.connected_web_clients.discard(request.sid)
|
|
state.active_logged_users.pop(request.sid, None)
|
|
|
|
logged_names = list(set(state.active_logged_users.values()))
|
|
state.socketio.emit('web_users_count', {'count': len(state.connected_web_clients), 'users': logged_names})
|
|
|
|
# --- INIZIALIZZAZIONE SISTEMA & THREAD ---
|
|
init_db()
|
|
state.seed_last_seen()
|
|
mqtt_svc.start_mqtt()
|
|
|
|
threading.Thread(target=auto_update_ids, daemon=True).start()
|
|
threading.Thread(target=push_worker, daemon=True).start()
|
|
threading.Thread(target=watchdog_loop, daemon=True).start()
|
|
|
|
logger.info(f"🚀 Fleet Control Console modulare pronta (Git Rev: {SERVER_COMMIT})")
|
|
|
|
if __name__ == '__main__':
|
|
port = int(config.get('web', {}).get('port', 9000)) if 'config' in locals() else 9000
|
|
state.socketio.run(app, host='0.0.0.0', port=port)
|