From 91ccfda03f3d4ca25cd917b8d459015837654243 Mon Sep 17 00:00:00 2001 From: Michel Date: Fri, 18 Apr 2025 16:01:37 +0200 Subject: [PATCH] =?UTF-8?q?Nouvelle=20version=20avec=20cr=C3=A9ation=20de?= =?UTF-8?q?=20sondes,=20gestion=20des=20temp=5Fmax=20seulement=20en=20admi?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- domo91.py | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/domo91.py b/domo91.py index dbddaa6..6df5cd9 100644 --- a/domo91.py +++ b/domo91.py @@ -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"
{temp_max}°C
", + 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}")