Files
loustique-home/flask/main.py

159 lines
4.6 KiB
Python
Raw Normal View History

2026-03-24 23:06:07 +01:00
from flask import Flask, render_template, request, jsonify
2026-04-01 22:23:25 +02:00
import requests
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-04-01 22:23:25 +02:00
import threading
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,
2026-04-01 18:39:00 +02:00
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)
2026-04-02 22:20:07 +02:00
2026-04-01 18:39:00 +02:00
from lumieres import SystemeLumieres
2026-04-02 22:20:07 +02:00
from board1main import call_board1
import alarme
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-04-01 18:39:00 +02:00
dernier_badge_scanne = None
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()
2026-04-01 18:39:00 +02:00
badge_id = data.get("badge_id")
2026-03-30 18:16:20 +02:00
username = auth.get_user_by_rfid(badge_id)
if username:
dernier_badge_scanne = username
return jsonify({"success": True, "username": username})
else:
2026-03-30 14:00:03 +02:00
return jsonify({"success": False})
2026-04-01 18:39:00 +02:00
@app.route("/check-rfid-login", methods=["GET"])
def check_rfid_login():
global dernier_badge_scanne
global current_user
if dernier_badge_scanne:
user = dernier_badge_scanne
current_user = user
dernier_badge_scanne = None
return jsonify({"success": True, "username": user})
return jsonify({"success": False})
2026-04-02 22:20:07 +02:00
"""
2026-03-27 15:00:11 +01:00
@app.route("/alarme",methods=["POST"])
def armer_alarme():
SystemeAlarme.armer()
return jsonify({"success": True})
2026-04-02 22:20:07 +02:00
"""
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")
@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-04-02 22:20:07 +02:00
@app.route("/alarme_status")
def get_alarme_status_info(): # Nom différent de l'import 'alarme'
try:
# On accède à la variable du fichier
statut = alarme.etat_alarme
return jsonify({"success": True, "status": statut})
except Exception as e:
return jsonify({"success": False, "message": str(e)}), 500
2026-04-01 22:23:25 +02:00
@app.route("/api/<action>", methods=["GET"])
2026-04-01 18:39:00 +02:00
def relais_pi2(action):
2026-04-01 22:23:25 +02:00
url_pi2 = f"https://pi32.local:8000/{action}"
2026-04-01 18:39:00 +02:00
try:
reponse = requests.get(url_pi2, timeout=5, verify=False)
2026-04-01 22:23:25 +02:00
if not reponse.ok:
2026-04-02 22:20:07 +02:00
return jsonify({"success": False, "message": "Erreur Pi 2"}), reponse.status_code
return jsonify(reponse.json())
2026-04-01 22:23:25 +02:00
2026-04-01 18:39:00 +02:00
except Exception as e:
2026-04-02 22:20:07 +02:00
# On ne garde que l'erreur si vraiment ça plante
print(f"[RELAIS] ERREUR : {e}")
return jsonify({"success": False, "message": str(e)}), 500
2026-04-01 18:39:00 +02:00
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')
)
)