2026-03-24 23:06:07 +01:00
|
|
|
from flask import Flask, render_template, request, jsonify
|
2026-03-25 01:05:42 +01:00
|
|
|
from flask_talisman import Talisman
|
2026-03-24 23:06:07 +01:00
|
|
|
from led import led
|
2026-03-21 10:53:02 +01:00
|
|
|
import os
|
2026-03-27 15:00:11 +01:00
|
|
|
import sys
|
2026-03-30 14:00:03 +02:00
|
|
|
import log
|
2026-03-24 23:06:07 +01:00
|
|
|
from add_user import add_user
|
|
|
|
|
import auth
|
|
|
|
|
import re
|
|
|
|
|
|
2026-03-21 10:53:02 +01:00
|
|
|
app = Flask(__name__)
|
2026-03-25 01:05:42 +01:00
|
|
|
Talisman(app, force_https=True,
|
|
|
|
|
content_security_policy=False )
|
2026-03-24 23:06:07 +01:00
|
|
|
current_user = None
|
|
|
|
|
|
2026-03-27 15:00:11 +01:00
|
|
|
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
|
composants = os.path.join(BASE_DIR, "composants", "byPanda")
|
|
|
|
|
sys.path.insert(0, composants)
|
|
|
|
|
from alarme import SystemeAlarme
|
2026-03-30 14:00:03 +02:00
|
|
|
from lumiere import SystemeLumieres
|
|
|
|
|
from board1main import *
|
2026-03-27 15:00:11 +01:00
|
|
|
|
2026-03-21 10:53:02 +01:00
|
|
|
@app.route("/")
|
|
|
|
|
def index():
|
2026-03-24 23:06:07 +01:00
|
|
|
return render_template("index.html")
|
2026-03-21 10:53:02 +01:00
|
|
|
|
|
|
|
|
@app.route("/login", methods=["POST"])
|
|
|
|
|
def login():
|
2026-03-24 23:06:07 +01:00
|
|
|
global current_user
|
2026-03-21 10:53:02 +01:00
|
|
|
data = request.get_json()
|
|
|
|
|
succes = auth.login(data["username"], data["password"])
|
|
|
|
|
if succes:
|
2026-03-24 23:06:07 +01:00
|
|
|
current_user = data["username"]
|
|
|
|
|
return jsonify({"success": True})
|
2026-03-21 10:53:02 +01:00
|
|
|
else:
|
2026-03-24 23:06:07 +01:00
|
|
|
return jsonify({"success": False})
|
|
|
|
|
|
2026-03-21 10:53:02 +01:00
|
|
|
@app.route("/dashboard")
|
|
|
|
|
def dashboard():
|
2026-03-24 23:06:07 +01:00
|
|
|
return render_template("dashboard.html")
|
2026-03-21 10:53:02 +01:00
|
|
|
|
2026-03-24 23:06:07 +01:00
|
|
|
@app.route("/led", methods=["POST"])
|
2026-03-21 10:53:02 +01:00
|
|
|
def call_led():
|
2026-03-30 14:00:03 +02:00
|
|
|
etat = SystemeLumieres.mettreAJourEtat()
|
|
|
|
|
if (etat == 0):
|
|
|
|
|
SystemeLumieres.allumerLumieres
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
SystemeLumieres.eteindreLumieres()
|
2026-03-24 23:06:07 +01:00
|
|
|
return jsonify({"success": True})
|
2026-03-30 18:16:20 +02:00
|
|
|
@app.route("/rfid-scan", methods=["POST"])
|
|
|
|
|
def rfid_scan():
|
|
|
|
|
global dernier_badge_scanne
|
|
|
|
|
data = request.get_json()
|
|
|
|
|
badge_id = str(data.get("badge_id"))
|
|
|
|
|
username = auth.get_user_by_rfid(badge_id)
|
|
|
|
|
|
|
|
|
|
if username:
|
|
|
|
|
|
|
|
|
|
dernier_badge_scanne = username
|
|
|
|
|
return jsonify({"success": True, "username": username})
|
|
|
|
|
else:
|
|
|
|
|
# Badge inconnu dans la BDD
|
2026-03-30 14:00:03 +02:00
|
|
|
return jsonify({"success": False})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-03-27 15:00:11 +01:00
|
|
|
@app.route("/alarme",methods=["POST"])
|
|
|
|
|
def armer_alarme():
|
|
|
|
|
SystemeAlarme.armer()
|
|
|
|
|
return jsonify({"success": True})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-03-24 23:06:07 +01:00
|
|
|
|
|
|
|
|
@app.route("/admin")
|
|
|
|
|
def admin_page():
|
|
|
|
|
return render_template("admin.html")
|
|
|
|
|
|
|
|
|
|
@app.route("/admin/logs")
|
|
|
|
|
def logs_page():
|
|
|
|
|
return render_template("log.html")
|
2026-03-21 10:53:02 +01:00
|
|
|
|
2026-03-24 23:06:07 +01:00
|
|
|
|
|
|
|
|
@app.route("/admin/logs/data")
|
|
|
|
|
def get_logs():
|
|
|
|
|
try:
|
|
|
|
|
with open('/var/log/loustique.log', 'r') as f:
|
|
|
|
|
lines = f.readlines()
|
|
|
|
|
ansi_escape = re.compile(r'\x1b\[[0-9;]*m')
|
|
|
|
|
lines = [ansi_escape.sub('', line) for line in lines[-200:]]
|
|
|
|
|
return jsonify({"success": True, "logs": lines})
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return jsonify({"success": False, "message": str(e)})
|
|
|
|
|
|
|
|
|
|
@app.route("/admin/add_user",methods=["POST"])
|
|
|
|
|
@app.route("/admin/add_user", methods=["POST"])
|
|
|
|
|
def create_user():
|
|
|
|
|
data = request.get_json()
|
|
|
|
|
succes = add_user(data["username"], data["password"], data["role"])
|
|
|
|
|
if succes:
|
|
|
|
|
return jsonify({"success": True})
|
|
|
|
|
else:
|
|
|
|
|
return jsonify({"success": False, "message": "Utilisateur déjà existant"})
|
|
|
|
|
@app.route("/admin/get_users")
|
|
|
|
|
def get_users():
|
|
|
|
|
users = auth.get_users()
|
|
|
|
|
return jsonify({"success": True, "users": users})
|
|
|
|
|
|
2026-03-25 00:26:41 +01:00
|
|
|
|
2026-03-24 23:06:07 +01:00
|
|
|
if __name__ == "__main__":
|
2026-03-30 18:20:42 +02:00
|
|
|
print("[*] Démarrage du lecteur RFID et de l'alarme en arrière-plan...")
|
|
|
|
|
thread_hardware = threading.Thread(target=call_board1, daemon=True)
|
|
|
|
|
thread_hardware.start()
|
2026-03-25 00:26:41 +01:00
|
|
|
app.run(
|
|
|
|
|
host="0.0.0.0",
|
2026-03-25 01:05:42 +01:00
|
|
|
port=443,
|
2026-03-25 00:26:41 +01:00
|
|
|
ssl_context=(
|
|
|
|
|
os.path.join(BASE_DIR, 'web_secu', 'ssl', 'cert.pem'),
|
|
|
|
|
os.path.join(BASE_DIR, 'web_secu', 'ssl', 'key.pem')
|
|
|
|
|
)
|
|
|
|
|
)
|