new api IT WORKS !

This commit is contained in:
bach 2025-06-12 15:56:55 +02:00
parent c2b3f9650b
commit ddc7446d8c

40
main.py
View File

@ -9,10 +9,12 @@ import subprocess
# Configuration de l'API REFMAR
# API_URL = "https://services.data.shom.fr/maregraphie/service/completetidegauge"
API_URL = "https://services.data.shom.fr/maregraphie/observation/json"
# API_URL = "https://services.data.shom.fr/maregraphie/observation/json"
# curl -X GET "https://services.data.shom.fr/maregraphie/observation/json/386?sources=1&dtStart=2025-05-22T15%3A56%3A00Z&dtEnd=2025-05-22T15%3A57%3A00Z&interval=1" -H "accept: application/json"
# API_KEY = "votre_cle_api" # Remplacez par votre clé API
STATION = '386' # Identifiant de la station du Havre
# STATION = '386' # Identifiant de la station du Havre
API_URL = "https://data.haropaport.com/sig/capteur/hauteur"
UDP_IP = "127.0.0.1"
UDP_PORT = 8765
@ -28,13 +30,13 @@ def fetch_data():
# "Ocp-Apim-Subscription-Key": API_KEY,
}
params = {
"sources": 1,
"dtStart": "2025-05-22T10:00:00Z",
"dtEnd": "2025-05-22T17:00:00Z",
# "interval": 1
# "sources": 1,
# "dtStart": "2025-05-22T10:00:00Z",
# "dtEnd": "2025-05-22T17:00:00Z",
# # "interval": 1
}
try:
response = requests.get(API_URL+"/"+STATION, headers=headers, params=params)
response = requests.get(API_URL, headers=headers, params=params)
response.raise_for_status()
return response.json()
except requests.RequestException as e:
@ -44,12 +46,11 @@ def fetch_data():
def process_data(data):
"""Traite les données récupérées."""
try:
zh_ref = data.get("verticalRef", {}).get("zh_ref", 0)
logging.info(f"Hauteur de la marée : {zh_ref} mètres")
zh_ref_bytes = f"{zh_ref};".encode('utf-8')
sock.sendto(zh_ref_bytes, (UDP_IP, UDP_PORT))
# sock.sendto(float(zh_ref).to_bytes(8, byteorder='big'), (UDP_IP, UDP_PORT))
# sock.sendto(float(zh_ref), (UDP_IP, UDP_PORT))
for item in data['items']:
if item['id'] == "hauteur_shom":
logging.info(f"{item["capteur"]} Hauteur d'eau : {item["valeur"]} mètres")
hauteurdeau_bytes = f"{item["valeur"]};".encode('utf-8')
sock.sendto(hauteurdeau_bytes, (UDP_IP, UDP_PORT))
except Exception as e:
logging.error(f"Erreur lors du traitement des données : {e}")
@ -58,22 +59,21 @@ def main():
logging.info("Démarrage de l'application de traitement des données du marégraphe.")
# lancement du patch pd
pd_patch = "./maregraphe.pd" # Remplace par le chemin de ton patch
pd_process = subprocess.Popen(["pd", "-nogui", "-open", pd_patch])
# pd_process = subprocess.Popen(["pd", "-nogui", "-open", pd_patch])
pd_process = subprocess.Popen(["pd", "-open", pd_patch])
try:
while True:
print('/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / /')
data = fetch_data()
logging.info(json.dumps(data, indent=2))
# print(data)
# if data:
# process_data(data)
time.sleep(60) # 300 Attendre 5 minutes
# logging.info(json.dumps(data, indent=2))
if data:
process_data(data)
time.sleep(10) # 300 Attendre 5 minutes
except KeyboardInterrupt:
print("Arrêt du script, fermeture de Pure Data...")
pd_process.terminate()
if __name__ == "__main__":
main()