88 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			88 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
	
	
| #!/usr/bin/env python
 | |
| 
 | |
| import requests
 | |
| import time
 | |
| import logging
 | |
| import socket
 | |
| import json
 | |
| 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"
 | |
| # 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
 | |
| 
 | |
| API_URL = "https://data.haropaport.com/sig/capteur/hauteur"
 | |
| 
 | |
| UDP_IP = "127.0.0.1"
 | |
| UDP_PORT = 8765
 | |
| 
 | |
| sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
 | |
| 
 | |
| # Configuration du logging
 | |
| logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
 | |
| 
 | |
| def fetch_data():
 | |
|     """Récupère les données du marégraphe via l'API REFMAR."""
 | |
|     headers = {
 | |
|         # "Ocp-Apim-Subscription-Key": API_KEY,
 | |
|     }
 | |
|     params = {
 | |
|         # "sources": 1,
 | |
|         # "dtStart": "2025-05-22T10:00:00Z",
 | |
|         # "dtEnd": "2025-05-22T17:00:00Z",
 | |
|         # # "interval": 1
 | |
|     }
 | |
|     try:
 | |
|         response = requests.get(API_URL, headers=headers, params=params)
 | |
|         response.raise_for_status()
 | |
|         return response.json()
 | |
|     except requests.RequestException as e:
 | |
|         logging.error(f"Erreur lors de la récupération des données de l'API : {e}")
 | |
|         return None
 | |
|     
 | |
| 
 | |
| def process_data(data):
 | |
|     """Traite les données récupérées."""
 | |
|     try:
 | |
|         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))
 | |
|             # else:
 | |
|             #     logging.error('error in parsing')
 | |
|             #     sock.sendto(f"{0};".encode('utf-8'), (UDP_IP, UDP_PORT))
 | |
|     except Exception as e:
 | |
|         logging.error(f"Erreur lors du traitement des données : {e}")
 | |
|         sock.sendto(f"{0};".encode('utf-8'), (UDP_IP, UDP_PORT))
 | |
| 
 | |
| def main():
 | |
|     """Fonction principale qui exécute la boucle de récupération et de traitement des données."""
 | |
|     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", "-open", pd_patch])
 | |
| 
 | |
|     try:
 | |
|         while True:
 | |
|             print('/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / /')
 | |
|             data = fetch_data()
 | |
|             # logging.info(json.dumps(data, indent=2))
 | |
|             if data:
 | |
|                 process_data(data)
 | |
|             else:
 | |
|                 logging.warning('no data')
 | |
|                 sock.sendto(f"{0};".encode('utf-8'), (UDP_IP, UDP_PORT))
 | |
|             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()
 |