filtre tableau sondes en panne
This commit is contained in:
155
Technique.py
155
Technique.py
@@ -1,74 +1,99 @@
|
||||
import streamlit as st
|
||||
import pandas as pd
|
||||
from utils.db import get_latest_chaufferie, get_history_by_sonde, verifier_utilisateur_commun
|
||||
import mysql.connector
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
st.set_page_config(page_title="Tech Chaufferie", layout="wide")
|
||||
load_dotenv()
|
||||
|
||||
def login_commun():
|
||||
login = st.text_input("Identifiant", type="default")
|
||||
password = st.text_input("Mot de passe", type="password")
|
||||
def lire_alertes_sondes():
|
||||
conn = None
|
||||
cursor = None
|
||||
try:
|
||||
conn = get_connection()
|
||||
cursor = conn.cursor(dictionary=True)
|
||||
cursor.execute("SELECT * FROM Alertes_Chaufferie ORDER BY Debut_defaut DESC")
|
||||
return cursor.fetchall()
|
||||
except Exception as e:
|
||||
import streamlit as st
|
||||
st.warning(f"Erreur lecture alertes : {e}")
|
||||
return []
|
||||
finally:
|
||||
if cursor:
|
||||
cursor.close()
|
||||
if conn and conn.is_connected():
|
||||
conn.close()
|
||||
|
||||
if st.button("Se connecter"):
|
||||
user = verifier_utilisateur_commun(login, password)
|
||||
if user:
|
||||
st.session_state["authenticated"] = True
|
||||
st.session_state["utilisateur"] = user["NomUtilisateur"]
|
||||
st.success("✅ Connexion réussie")
|
||||
st.rerun()
|
||||
else:
|
||||
st.error("❌ Identifiants incorrects ou expirés")
|
||||
def acquitter_alerte(id_alerte):
|
||||
conn = None
|
||||
cursor = None
|
||||
try:
|
||||
conn = get_connection()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("UPDATE Alertes_Chaufferie SET Etat = 'Acquitté' WHERE Id = %s", (id_alerte,))
|
||||
conn.commit()
|
||||
except Exception as e:
|
||||
import streamlit as st
|
||||
st.warning(f"Erreur lors de l'acquittement : {e}")
|
||||
finally:
|
||||
if cursor:
|
||||
cursor.close()
|
||||
if conn and conn.is_connected():
|
||||
conn.close()
|
||||
|
||||
# Authentification
|
||||
if "authenticated" not in st.session_state:
|
||||
st.session_state["authenticated"] = False
|
||||
|
||||
if not st.session_state["authenticated"]:
|
||||
st.title("🔧 Suivi des sondes de la chaufferie")
|
||||
st.subheader("🔐 Connexion technicien chaufferie")
|
||||
login_commun()
|
||||
st.stop()
|
||||
with st.sidebar:
|
||||
st.markdown(f"👤 Connecté en tant que **{st.session_state['utilisateur']}**")
|
||||
if st.button("🔓 Se déconnecter"):
|
||||
st.session_state.clear()
|
||||
st.rerun()
|
||||
# --- Affichage principal ---
|
||||
st.title("🔧 Suivi des sondes de la chaufferie")
|
||||
def verifier_utilisateur_commun(login, password):
|
||||
conn = get_connection()
|
||||
cursor = conn.cursor(dictionary=True)
|
||||
|
||||
data = get_latest_chaufferie()
|
||||
df = pd.DataFrame(data)
|
||||
query = """
|
||||
SELECT NomUtilisateur FROM Commun.AccesUtilisateurs
|
||||
WHERE NomUtilisateur = %s
|
||||
AND MotDePasse = %s
|
||||
AND (DateExpiration IS NULL OR DateExpiration >= CURDATE())
|
||||
"""
|
||||
cursor.execute(query, (login, password))
|
||||
user = cursor.fetchone()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
return user # None si non trouvée
|
||||
|
||||
# Ajout d'un statut couleur
|
||||
def statut(temp):
|
||||
if temp > 75:
|
||||
return "❌ Très chaud"
|
||||
elif temp > 60:
|
||||
return "⚠️ Surveiller"
|
||||
else:
|
||||
return "✅ OK"
|
||||
def get_connection():
|
||||
return mysql.connector.connect(
|
||||
host=os.getenv("DB_HOST"),
|
||||
user=os.getenv("DB_USER"),
|
||||
password=os.getenv("DB_PASSWORD"),
|
||||
database=os.getenv("DB_NAME")
|
||||
)
|
||||
|
||||
df["Statut"] = df["Temperature"].apply(statut)
|
||||
def get_latest_chaufferie():
|
||||
conn = get_connection()
|
||||
cursor = conn.cursor(dictionary=True)
|
||||
query = """
|
||||
SELECT c1.Sonde, c1.Temperature, c1.Date, c1.Topic
|
||||
FROM Chaufferie c1
|
||||
INNER JOIN (
|
||||
SELECT Sonde, MAX(Date) AS MaxDate
|
||||
FROM Chaufferie
|
||||
GROUP BY Sonde
|
||||
) c2 ON c1.Sonde = c2.Sonde AND c1.Date = c2.MaxDate
|
||||
ORDER BY c1.Topic, c1.Sonde
|
||||
"""
|
||||
cursor.execute(query)
|
||||
data = cursor.fetchall()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
return data
|
||||
|
||||
# Séparer par topic
|
||||
for topic in df["Topic"].unique():
|
||||
st.subheader(f"Zone : {topic}")
|
||||
st.dataframe(df[df["Topic"] == topic][["Sonde", "Temperature", "Date", "Statut"]], use_container_width=True)
|
||||
|
||||
# Graphique historique
|
||||
st.markdown("---")
|
||||
st.header("📈 Historique par sonde (24h)")
|
||||
|
||||
sonde_selection = st.selectbox("Choisir une sonde à afficher", df["Sonde"].unique())
|
||||
|
||||
if sonde_selection:
|
||||
historique = get_history_by_sonde(sonde_selection)
|
||||
if historique:
|
||||
df_hist = pd.DataFrame(historique)
|
||||
df_hist["Date"] = pd.to_datetime(df_hist["Date"])
|
||||
|
||||
st.line_chart(
|
||||
df_hist.set_index("Date")["Temperature"],
|
||||
use_container_width=True
|
||||
)
|
||||
else:
|
||||
st.info("Aucune donnée disponible pour cette sonde dans les dernières 24h.")
|
||||
def get_history_by_sonde(sonde_name):
|
||||
conn = get_connection()
|
||||
cursor = conn.cursor(dictionary=True)
|
||||
query = """
|
||||
SELECT Date, Temperature
|
||||
FROM Chaufferie
|
||||
WHERE Sonde = %s AND Date >= NOW() - INTERVAL 1 DAY
|
||||
ORDER BY Date ASC
|
||||
"""
|
||||
cursor.execute(query, (sonde_name,))
|
||||
data = cursor.fetchall()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
return data
|
||||
|
||||
Reference in New Issue
Block a user