Consolidation de Domo91 et cosmétique
This commit is contained in:
@@ -165,24 +165,24 @@ def lire_sondes_depuis_db(site: str):
|
||||
|
||||
def lire_cfg_chambres(site: str):
|
||||
"""
|
||||
Retourne {sonde: {"temp_max": float, "active": bool, "entretien": bool}}
|
||||
Retourne {sonde: {"temp_max": float, "active": bool}}
|
||||
depuis Chambres_froides pour le site.
|
||||
"""
|
||||
sql = """
|
||||
SELECT Sonde, Temp_Max, Etat, En_entretien
|
||||
FROM Sondes.Chambres_froides
|
||||
dbname = os.getenv("DB_NAME", "Sondes")
|
||||
sql = f"""
|
||||
SELECT Sonde, Temp_Max, Etat
|
||||
FROM `{dbname}`.`Chambres_froides`
|
||||
WHERE Lieu=%s
|
||||
"""
|
||||
cnx = get_db()
|
||||
cfg: dict[str, dict] = {}
|
||||
try:
|
||||
cur = cnx.cursor()
|
||||
cur.execute(sql, (site, ))
|
||||
for sonde, temp_max, etat, en_entretien in cur.fetchall():
|
||||
cur.execute(sql, (site,))
|
||||
for sonde, temp_max, etat in cur.fetchall():
|
||||
cfg[str(sonde)] = {
|
||||
"temp_max": float(temp_max),
|
||||
"active": str(etat).upper() == "ON",
|
||||
"entretien": bool(int(en_entretien or 0)),
|
||||
}
|
||||
return cfg
|
||||
except MySQLError as err:
|
||||
@@ -198,11 +198,12 @@ def compute_site_alarm(last_values: list[dict], cfg: dict[str, dict], hysteresis
|
||||
for row in last_values:
|
||||
sonde = str(row["Sonde"])
|
||||
meta = cfg.get(sonde)
|
||||
if not meta or not meta["active"] or meta["entretien"]:
|
||||
if not meta or not meta.get("active", False):
|
||||
continue
|
||||
temp = float(row["Temperature"])
|
||||
if temp > float(meta["temp_max"]) + float(hysteresis):
|
||||
return True, (sonde, temp, float(meta["temp_max"]))
|
||||
seuil = float(meta["temp_max"])
|
||||
if temp > seuil + float(hysteresis):
|
||||
return True, (sonde, temp, seuil)
|
||||
return False, None
|
||||
|
||||
def lire_seuils_depuis_db(site: str):
|
||||
@@ -883,24 +884,32 @@ def run_monitor_cycle(site: str = SITE):
|
||||
for r in last_rows:
|
||||
nom = str(r["Sonde"])
|
||||
temp = float(r["Temperature"])
|
||||
seuil = float(seuils.get(nom, 6.0))
|
||||
|
||||
if nom not in seuils:
|
||||
continue # sonde non gérée dans Chambres_froides → ignorée
|
||||
|
||||
seuil = float(seuils[nom])
|
||||
now_ = now_paris()
|
||||
|
||||
if temp > seuil:
|
||||
if depassement_depuis_30min(site, nom, seuil):
|
||||
conn = None
|
||||
try:
|
||||
conn = get_db()
|
||||
if open_alert(conn, f"Alertes_{site}", nom, now_):
|
||||
notifier_sur_depassement(site, nom, temp, seuil) # MAIL + SMS client
|
||||
notifier_sur_depassement(site, nom, temp, seuil)
|
||||
finally:
|
||||
conn.close()
|
||||
if conn:
|
||||
conn.close()
|
||||
else:
|
||||
conn = None
|
||||
try:
|
||||
conn = get_db()
|
||||
if close_alert(conn, f"Alertes_{site}", nom):
|
||||
notifier_acquittement(site, nom, temp, seuil) # MAIL acquittement
|
||||
notifier_acquittement(site, nom, temp, seuil)
|
||||
finally:
|
||||
conn.close()
|
||||
if conn:
|
||||
conn.close()
|
||||
|
||||
def run_monitor_loop(site: str = SITE, period_sec: int = 300):
|
||||
log.info("%s démarré (site=%s, période=%ss) ✅", PROGRAM_NAME, site, period_sec)
|
||||
|
||||
Reference in New Issue
Block a user