python soket sendTo pd netreveive
This commit is contained in:
parent
973e5ae657
commit
cf4e506523
@ -10,3 +10,5 @@ pip install uv
|
|||||||
uv sync
|
uv sync
|
||||||
./main.py
|
./main.py
|
||||||
```
|
```
|
||||||
|
|
||||||
|

|
||||||
|
|||||||
56
main.py
Normal file → Executable file
56
main.py
Normal file → Executable file
@ -1,11 +1,21 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
import time
|
import time
|
||||||
import logging
|
import logging
|
||||||
|
import socket
|
||||||
|
import json
|
||||||
|
import subprocess
|
||||||
|
|
||||||
# Configuration de l'API REFMAR
|
# Configuration de l'API REFMAR
|
||||||
API_URL = "https://services.data.shom.fr/refmar/v1.0/GetTideData"
|
API_URL = "https://services.data.shom.fr/maregraphie/service/completetidegauge"
|
||||||
API_KEY = "votre_cle_api" # Remplacez par votre clé API
|
# API_KEY = "votre_cle_api" # Remplacez par votre clé API
|
||||||
STATION = "LEHAVRE" # Identifiant de la station du Havre
|
STATION = '386' # Identifiant de la station du Havre
|
||||||
|
|
||||||
|
UDP_IP = "127.0.0.1"
|
||||||
|
UDP_PORT = 8765
|
||||||
|
|
||||||
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
|
|
||||||
# Configuration du logging
|
# Configuration du logging
|
||||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
||||||
@ -13,14 +23,14 @@ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(
|
|||||||
def fetch_data():
|
def fetch_data():
|
||||||
"""Récupère les données du marégraphe via l'API REFMAR."""
|
"""Récupère les données du marégraphe via l'API REFMAR."""
|
||||||
headers = {
|
headers = {
|
||||||
"Ocp-Apim-Subscription-Key": API_KEY,
|
# "Ocp-Apim-Subscription-Key": API_KEY,
|
||||||
}
|
}
|
||||||
params = {
|
params = {
|
||||||
"station": STATION,
|
# "station": STATION,
|
||||||
"time": "current", # Vous pouvez ajuster ce paramètre selon vos besoins
|
# "time": "current", # Vous pouvez ajuster ce paramètre selon vos besoins
|
||||||
}
|
}
|
||||||
try:
|
try:
|
||||||
response = requests.get(API_URL, headers=headers, params=params)
|
response = requests.get(API_URL+"/"+STATION, headers=headers, params=params)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
return response.json()
|
return response.json()
|
||||||
except requests.RequestException as e:
|
except requests.RequestException as e:
|
||||||
@ -30,21 +40,35 @@ def fetch_data():
|
|||||||
def process_data(data):
|
def process_data(data):
|
||||||
"""Traite les données récupérées."""
|
"""Traite les données récupérées."""
|
||||||
try:
|
try:
|
||||||
# Exemple de traitement des données
|
zh_ref = data.get("verticalRef", {}).get("zh_ref", 0)
|
||||||
tide_height = data.get("height", 0)
|
logging.info(f"Hauteur de la marée : {zh_ref} mètres")
|
||||||
logging.info(f"Hauteur de la marée : {tide_height} mètres")
|
zh_ref_bytes = f"{zh_ref};".encode('utf-8')
|
||||||
# Ajoutez ici le traitement spécifique que vous souhaitez effectuer avec les données
|
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))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"Erreur lors du traitement des données : {e}")
|
logging.error(f"Erreur lors du traitement des données : {e}")
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""Fonction principale qui exécute la boucle de récupération et de traitement des données."""
|
"""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.")
|
logging.info("Démarrage de l'application de traitement des données du marégraphe.")
|
||||||
while True:
|
# lancement du patch pd
|
||||||
data = fetch_data()
|
pd_patch = "./maregraphe.pd" # Remplace par le chemin de ton patch
|
||||||
if data:
|
pd_process = subprocess.Popen(["pd", "-nogui", "-open", pd_patch])
|
||||||
process_data(data)
|
|
||||||
time.sleep(300) # Attendre 5 minutes
|
try:
|
||||||
|
while True:
|
||||||
|
data = fetch_data()
|
||||||
|
logging.info(json.dumps(data, indent=2))
|
||||||
|
# print(data)
|
||||||
|
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__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
8
maregraphe.pd
Normal file
8
maregraphe.pd
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#N canvas 960 30 952 485 12;
|
||||||
|
#X obj 118 116 float;
|
||||||
|
#X obj 67 116 print;
|
||||||
|
#X obj 118 59 netreceive -u -b 8765;
|
||||||
|
#X obj 118 89 list tosymbol;
|
||||||
|
#X connect 0 0 1 0;
|
||||||
|
#X connect 2 0 3 0;
|
||||||
|
#X connect 3 0 0 0;
|
||||||
BIN
maregraphe_pd.png
Normal file
BIN
maregraphe_pd.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 16 KiB |
Loading…
Reference in New Issue
Block a user