filtre tableau sondes en panne
This commit is contained in:
159
Technique.py
159
Technique.py
@@ -1,74 +1,99 @@
|
|||||||
|
import mysql.connector
|
||||||
|
import os
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
|
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
|
import streamlit as st
|
||||||
import pandas as pd
|
st.warning(f"Erreur lecture alertes : {e}")
|
||||||
from utils.db import get_latest_chaufferie, get_history_by_sonde, verifier_utilisateur_commun
|
return []
|
||||||
|
finally:
|
||||||
|
if cursor:
|
||||||
|
cursor.close()
|
||||||
|
if conn and conn.is_connected():
|
||||||
|
conn.close()
|
||||||
|
|
||||||
st.set_page_config(page_title="Tech Chaufferie", layout="wide")
|
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()
|
||||||
|
|
||||||
def login_commun():
|
|
||||||
login = st.text_input("Identifiant", type="default")
|
|
||||||
password = st.text_input("Mot de passe", type="password")
|
|
||||||
|
|
||||||
if st.button("Se connecter"):
|
def verifier_utilisateur_commun(login, password):
|
||||||
user = verifier_utilisateur_commun(login, password)
|
conn = get_connection()
|
||||||
if user:
|
cursor = conn.cursor(dictionary=True)
|
||||||
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")
|
|
||||||
|
|
||||||
# Authentification
|
query = """
|
||||||
if "authenticated" not in st.session_state:
|
SELECT NomUtilisateur FROM Commun.AccesUtilisateurs
|
||||||
st.session_state["authenticated"] = False
|
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
|
||||||
|
|
||||||
if not st.session_state["authenticated"]:
|
def get_connection():
|
||||||
st.title("🔧 Suivi des sondes de la chaufferie")
|
return mysql.connector.connect(
|
||||||
st.subheader("🔐 Connexion technicien chaufferie")
|
host=os.getenv("DB_HOST"),
|
||||||
login_commun()
|
user=os.getenv("DB_USER"),
|
||||||
st.stop()
|
password=os.getenv("DB_PASSWORD"),
|
||||||
with st.sidebar:
|
database=os.getenv("DB_NAME")
|
||||||
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")
|
|
||||||
|
|
||||||
data = get_latest_chaufferie()
|
|
||||||
df = pd.DataFrame(data)
|
|
||||||
|
|
||||||
# Ajout d'un statut couleur
|
|
||||||
def statut(temp):
|
|
||||||
if temp > 75:
|
|
||||||
return "❌ Très chaud"
|
|
||||||
elif temp > 60:
|
|
||||||
return "⚠️ Surveiller"
|
|
||||||
else:
|
|
||||||
return "✅ OK"
|
|
||||||
|
|
||||||
df["Statut"] = df["Temperature"].apply(statut)
|
|
||||||
|
|
||||||
# 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_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
|
||||||
|
|
||||||
|
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