Raccordement VM Gitéa
This commit is contained in:
5
.env
5
.env
@@ -1,5 +1,5 @@
|
|||||||
# connexion mysql
|
# connexion mysql
|
||||||
DB_HOST=162.19.78.131
|
DB_HOST=localhost
|
||||||
DB_USER=sondes
|
DB_USER=sondes
|
||||||
DB_PASS=TX.)-U1!zq5Axdk4
|
DB_PASS=TX.)-U1!zq5Axdk4
|
||||||
DB_NAME=Sondes
|
DB_NAME=Sondes
|
||||||
@@ -25,9 +25,12 @@ SYNO_CHAT_WEBHOOK_GYRO_MEUDON=https://mj91.fr/webapi/entry.cgi?api=SYNO.Chat.Ext
|
|||||||
|
|
||||||
SYNO_CHAT_WEBHOOK_CONNEXIONS=https://mj91.fr/webapi/entry.cgi?api=SYNO.Chat.External&method=incoming&version=2&token=R585242twVz04qmzukxbtSTMe7p0GgdroKtO8opBglDx3VLtaLwJhYb93btH6Hya
|
SYNO_CHAT_WEBHOOK_CONNEXIONS=https://mj91.fr/webapi/entry.cgi?api=SYNO.Chat.External&method=incoming&version=2&token=R585242twVz04qmzukxbtSTMe7p0GgdroKtO8opBglDx3VLtaLwJhYb93btH6Hya
|
||||||
|
|
||||||
|
SYNO_CHAT_WEBHOOK_CONNEXIONS_SIMPLE=https://mj91.fr/webapi/entry.cgi?api=SYNO.Chat.External&method=incoming&version=2&token=TahhvDjYKqvA7KbGgVOK7uZI8ri0JS1HSPvCGGXteyZaHGKpKJGvaJS4Favf9Xyj
|
||||||
|
|
||||||
SYNO_CHAT_BOTNAME_MONITOR="Injection données dans tables"
|
SYNO_CHAT_BOTNAME_MONITOR="Injection données dans tables"
|
||||||
SYNO_CHAT_BOTNAME_GYRO="Gestion Gyro"
|
SYNO_CHAT_BOTNAME_GYRO="Gestion Gyro"
|
||||||
SYNO_CHAT_BOTNAME_CONNEXIONS="Journal Connexions"
|
SYNO_CHAT_BOTNAME_CONNEXIONS="Journal Connexions"
|
||||||
|
SYNO_CHAT_BOTNAME_CONNEXIONS_SIMPLE="Connexions Simples"
|
||||||
|
|
||||||
SYNO_CHAT_TIMEOUT=10
|
SYNO_CHAT_TIMEOUT=10
|
||||||
SYNO_CHAT_VERIFY_SSL=true
|
SYNO_CHAT_VERIFY_SSL=true
|
||||||
|
|||||||
@@ -1,13 +1,17 @@
|
|||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
import logging
|
import logging
|
||||||
from typing import Optional
|
|
||||||
import json
|
import json
|
||||||
|
from typing import Optional
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
import pymysql
|
import pymysql
|
||||||
import requests
|
import requests
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
load_dotenv()
|
|
||||||
|
BASE_DIR = Path("/home/debian/Gestion_sondes")
|
||||||
|
load_dotenv(BASE_DIR / ".env")
|
||||||
|
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
level=logging.INFO,
|
level=logging.INFO,
|
||||||
@@ -37,6 +41,10 @@ DB_CONFIG = {
|
|||||||
|
|
||||||
SYNO_CHAT_WEBHOOK = env_str("SYNO_CHAT_WEBHOOK_CONNEXIONS")
|
SYNO_CHAT_WEBHOOK = env_str("SYNO_CHAT_WEBHOOK_CONNEXIONS")
|
||||||
SYNO_CHAT_BOTNAME = env_str("SYNO_CHAT_BOTNAME_CONNEXIONS", "Journal Connexions")
|
SYNO_CHAT_BOTNAME = env_str("SYNO_CHAT_BOTNAME_CONNEXIONS", "Journal Connexions")
|
||||||
|
|
||||||
|
SYNO_CHAT_WEBHOOK_SIMPLE = env_str("SYNO_CHAT_WEBHOOK_CONNEXIONS_SIMPLE")
|
||||||
|
SYNO_CHAT_BOTNAME_SIMPLE = env_str("SYNO_CHAT_BOTNAME_CONNEXIONS_SIMPLE", "Connexions Simples")
|
||||||
|
|
||||||
POLL_INTERVAL = int(env_str("POLL_INTERVAL", "10"))
|
POLL_INTERVAL = int(env_str("POLL_INTERVAL", "10"))
|
||||||
|
|
||||||
|
|
||||||
@@ -62,12 +70,20 @@ def format_message(row: dict) -> str:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def format_simple_message(row: dict) -> str:
|
||||||
|
user = row.get("NomUtilisateur", "")
|
||||||
|
site = row.get("SiteDemande", "")
|
||||||
|
statut = row.get("Statut", "")
|
||||||
|
return f"[Connexion site]\n{user} -> {site} ({statut})"
|
||||||
|
|
||||||
|
|
||||||
def send_synology_chat(message: str) -> None:
|
def send_synology_chat(message: str) -> None:
|
||||||
if not SYNO_CHAT_WEBHOOK:
|
if not SYNO_CHAT_WEBHOOK:
|
||||||
raise RuntimeError("SYNO_CHAT_WEBHOOK_CONNEXIONS non configuré")
|
raise RuntimeError("SYNO_CHAT_WEBHOOK_CONNEXIONS non configure")
|
||||||
|
|
||||||
syno_payload = {
|
syno_payload = {
|
||||||
"text": message
|
"text": message,
|
||||||
|
"botName": SYNO_CHAT_BOTNAME
|
||||||
}
|
}
|
||||||
|
|
||||||
response = requests.post(
|
response = requests.post(
|
||||||
@@ -76,13 +92,37 @@ def send_synology_chat(message: str) -> None:
|
|||||||
timeout=10
|
timeout=10
|
||||||
)
|
)
|
||||||
|
|
||||||
log.info("Synology Chat HTTP=%s body=%s", response.status_code, response.text)
|
log.info("Synology Chat DETAIL HTTP=%s body=%s", response.status_code, response.text)
|
||||||
|
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
|
||||||
body = response.json()
|
body = response.json()
|
||||||
if not body.get("success", False):
|
if not body.get("success", False):
|
||||||
raise RuntimeError(f"Synology Chat erreur: {body}")
|
raise RuntimeError(f"Synology Chat DETAIL erreur: {body}")
|
||||||
|
|
||||||
|
|
||||||
|
def send_synology_chat_simple(message: str) -> None:
|
||||||
|
if not SYNO_CHAT_WEBHOOK_SIMPLE:
|
||||||
|
return
|
||||||
|
|
||||||
|
syno_payload = {
|
||||||
|
"text": message,
|
||||||
|
"botName": SYNO_CHAT_BOTNAME_SIMPLE
|
||||||
|
}
|
||||||
|
|
||||||
|
response = requests.post(
|
||||||
|
SYNO_CHAT_WEBHOOK_SIMPLE,
|
||||||
|
data={"payload": json.dumps(syno_payload, ensure_ascii=False)},
|
||||||
|
timeout=10
|
||||||
|
)
|
||||||
|
|
||||||
|
log.info("Synology Chat SIMPLE HTTP=%s body=%s", response.status_code, response.text)
|
||||||
|
|
||||||
|
response.raise_for_status()
|
||||||
|
|
||||||
|
body = response.json()
|
||||||
|
if not body.get("success", False):
|
||||||
|
raise RuntimeError(f"Synology Chat SIMPLE erreur: {body}")
|
||||||
|
|
||||||
|
|
||||||
def fetch_pending_rows(conn) -> list[dict]:
|
def fetch_pending_rows(conn) -> list[dict]:
|
||||||
@@ -106,9 +146,6 @@ def fetch_pending_rows(conn) -> list[dict]:
|
|||||||
ORDER BY DateHeure ASC, Id_Journal ASC
|
ORDER BY DateHeure ASC, Id_Journal ASC
|
||||||
LIMIT 50
|
LIMIT 50
|
||||||
"""
|
"""
|
||||||
print("SQL fetch_pending_rows =")
|
|
||||||
print(sql)
|
|
||||||
|
|
||||||
with conn.cursor() as cur:
|
with conn.cursor() as cur:
|
||||||
cur.execute(sql)
|
cur.execute(sql)
|
||||||
return cur.fetchall()
|
return cur.fetchall()
|
||||||
@@ -116,7 +153,7 @@ def fetch_pending_rows(conn) -> list[dict]:
|
|||||||
|
|
||||||
def mark_sent(conn, row_id: int) -> None:
|
def mark_sent(conn, row_id: int) -> None:
|
||||||
sql = """
|
sql = """
|
||||||
UPDATE Acces.JournalConnexions
|
UPDATE `Acces`.`JournalConnexions`
|
||||||
SET NotificationEnvoyee = 1,
|
SET NotificationEnvoyee = 1,
|
||||||
DateNotification = NOW(),
|
DateNotification = NOW(),
|
||||||
ErreurNotification = NULL
|
ErreurNotification = NULL
|
||||||
@@ -148,18 +185,26 @@ def process_once() -> None:
|
|||||||
|
|
||||||
for row in rows:
|
for row in rows:
|
||||||
row_id = row["Id_Journal"]
|
row_id = row["Id_Journal"]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
message = format_message(row)
|
detailed_message = format_message(row)
|
||||||
send_synology_chat(message)
|
send_synology_chat(detailed_message)
|
||||||
|
|
||||||
|
# Envoi simplifie uniquement pour la vraie connexion site,
|
||||||
|
# pas pour la ligne meta Domo91 / Acces
|
||||||
|
if row.get("SiteDemande") and row.get("DSN") != "Domo91":
|
||||||
|
simple_message = format_simple_message(row)
|
||||||
|
send_synology_chat_simple(simple_message)
|
||||||
|
|
||||||
mark_sent(conn, row_id)
|
mark_sent(conn, row_id)
|
||||||
log.info("Notification envoyée pour Id_Journal=%s", row_id)
|
log.info("Notification envoyee pour Id_Journal=%s", row_id)
|
||||||
|
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
log.exception("Erreur d'envoi pour Id_Journal=%s", row_id)
|
log.exception("Erreur d'envoi pour Id_Journal=%s", row_id)
|
||||||
try:
|
try:
|
||||||
mark_error(conn, row_id, str(exc))
|
mark_error(conn, row_id, str(exc))
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception("Impossible d'écrire ErreurNotification pour Id_Journal=%s", row_id)
|
log.exception("Impossible d'ecrire ErreurNotification pour Id_Journal=%s", row_id)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@@ -170,12 +215,12 @@ def main():
|
|||||||
if not SYNO_CHAT_WEBHOOK:
|
if not SYNO_CHAT_WEBHOOK:
|
||||||
raise RuntimeError("SYNO_CHAT_WEBHOOK_CONNEXIONS manquant")
|
raise RuntimeError("SYNO_CHAT_WEBHOOK_CONNEXIONS manquant")
|
||||||
|
|
||||||
log.info("Surveillance de JournalConnexions démarrée.")
|
log.info("Surveillance de JournalConnexions demarree.")
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
process_once()
|
process_once()
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception("Erreur générale dans la boucle de surveillance")
|
log.exception("Erreur generale dans la boucle de surveillance")
|
||||||
time.sleep(POLL_INTERVAL)
|
time.sleep(POLL_INTERVAL)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""
|
"""
|
||||||
Mqtt_meudon.py (version nettoyée)
|
Mqtt_Meudon.py (version nettoyée)
|
||||||
--------------------------------
|
--------------------------------
|
||||||
- S'abonne à Meudon/# sur le broker MQTT
|
- S'abonne à Meudon/# sur le broker MQTT
|
||||||
- Parse les messages (topic -> nom de sonde, payload -> température)
|
- Parse les messages (topic -> nom de sonde, payload -> température)
|
||||||
BIN
requirements.txt
BIN
requirements.txt
Binary file not shown.
12
scripts/update_gestion_sondes.sh
Normal file
12
scripts/update_gestion_sondes.sh
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
cd /home/domo91/Gestion_sondes || exit 1
|
||||||
|
|
||||||
|
echo "Mise à jour depuis Gitea..."
|
||||||
|
git pull --ff-only origin master
|
||||||
|
|
||||||
|
echo "Redémarrage de l'application..."
|
||||||
|
sudo supervisorctl restart Interface
|
||||||
|
|
||||||
|
echo "Statut Supervisor :"
|
||||||
|
sudo supervisorctl status
|
||||||
Reference in New Issue
Block a user