Nouvelle version avec création de sondes, gestion des temp_max seulement en admin

This commit is contained in:
2025-04-18 16:01:37 +02:00
parent e536ab738d
commit 91ccfda03f

View File

@@ -7,6 +7,7 @@ import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from fpdf import FPDF
import os
import random
st.set_page_config(page_title="Domo91 - Surveillance", layout="wide")
st.title("📡 Supervision Températures")
@@ -156,6 +157,7 @@ if st.session_state["authenticated"]:
site_selectionne = st.session_state["lieu_autorise"]
st.info(f"Site imposé : {site_selectionne}")
selected_date = st.date_input("📅 Date du relevé", value=date.today())
st.session_state["selected_date"] = selected_date
@@ -245,5 +247,85 @@ if st.session_state["authenticated"]:
except Exception as e:
st.error(f"Erreur MySQL : {e}")
if st.session_state["role"] == "superviseur":
with st.expander(" Ajouter une nouvelle chambre froide", expanded=False):
with st.form("ajout_sonde"):
nouvelle_sonde = st.text_input("Nom de la sonde")
temp_max = st.number_input("Température maximale autorisée (°C)", value=4)
etat_on = st.checkbox("État actif (ON)", value=True)
submitted = st.form_submit_button("✅ Ajouter")
if submitted:
try:
conn_add = mysql.connector.connect(**db_config)
cursor_add = conn_add.cursor()
cursor_add.execute(
"INSERT INTO Chambres_froides (Lieu, Sonde, Temp_Max, Etat) VALUES (%s, %s, %s, %s)",
(site_selectionne, nouvelle_sonde, temp_max, "ON" if etat_on else "OFF")
)
conn_add.commit()
cursor_add.close()
conn_add.close()
st.success(f"Sonde '{nouvelle_sonde}' ajoutée avec succès au site {site_selectionne} 🎉")
# Refresh immédiat
st.session_state["refresh_admin"] = random.randint(1000, 9999)
except Exception as e:
st.error(f"Erreur lors de l'ajout : {e}")
with st.expander("🛠️ Gestion des chambres froides (administrateur)", expanded=True):
# Bouton d'actualisation
if st.button("🔄 Actualiser la liste"):
st.session_state["refresh_admin"] = random.randint(0, 9999) # Force le rerun
try:
conn_admin = mysql.connector.connect(**db_config)
cursor_admin = conn_admin.cursor(dictionary=True)
cursor_admin.execute("SELECT * FROM Chambres_froides WHERE Lieu = %s", (site_selectionne,))
chambres = cursor_admin.fetchall()
if not chambres:
st.warning("Aucune chambre froide pour ce site.")
else:
for chambre in chambres:
col1, col2, col3 = st.columns([3, 1, 2])
with col1:
st.markdown(f"**{chambre['Sonde']}**")
with col2:
etat = st.checkbox("ON", value=(chambre["Etat"] == "ON"),
key=f"etat_{chambre['Id']}_{st.session_state.get('refresh_admin', 0)}")
new_etat = "ON" if etat else "OFF"
with col3:
temp_max = chambre["Temp_Max"]
moins, val, plus = st.columns([1, 2, 1])
with moins:
if st.button("", key=f"moins_{chambre['Id']}"):
temp_max -= 1
with val:
st.markdown(f"<div style='text-align:center;font-size:20px'>{temp_max}°C</div>",
unsafe_allow_html=True)
with plus:
if st.button("", key=f"plus_{chambre['Id']}"):
temp_max += 1
if new_etat != chambre["Etat"] or temp_max != chambre["Temp_Max"]:
cursor_admin.execute(
"UPDATE Chambres_froides SET Etat = %s, Temp_Max = %s WHERE Id = %s",
(new_etat, temp_max, chambre["Id"])
)
conn_admin.commit()
st.success(f"{chambre['Sonde']} mise à jour")
cursor_admin.close()
conn_admin.close()
except Exception as e:
st.error(f"Erreur SQL (admin) : {e}")