-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
173 lines (155 loc) · 5.4 KB
/
app.py
File metadata and controls
173 lines (155 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from flask import Flask, jsonify, render_template
# import serial
import threading
import time
import requests
import random
import csv
from io import StringIO
import json
app = Flask(__name__)
# Données GPS partagées
gps_data = {
"lat": 0.0,
"lon": 0.0,
"vitesse": 0.0
}
# Clé API météo (à remplacer si besoin)
API_KEY = "GRF4DXAG9HC8A34UAQSHG7FSP"
# Active/désactive l’utilisation du GPS Arduino
capt = False
# Thread lecture série
def lire_gps():
if capt:
try:
import serial
ser = serial.Serial('COM3', 9600, timeout=1)
while True:
ligne = ser.readline().decode('utf-8').strip()
if ligne.startswith("LAT:"):
try:
parts = ligne.split(',')
gps_data["lat"] = float(parts[0].split(':')[1])
gps_data["lon"] = float(parts[1].split(':')[1])
gps_data["vitesse"] = float(parts[2].split(':')[1])
except:
continue
except Exception as e:
print(f"Erreur série : {e}")
else:
while True:
# Coordonnées fixes pour simulation
gps_data["lat"] = 39.1627943
gps_data["lon"] = 11.8036229
gps_data["vitesse"] = 6.0
time.sleep(2)
# Données météo via Visual Crossing
def get_weather(lat, lon, API_KEY):
try:
with open("meteo.json", "r") as f:
data = json.load(f)
info_day = data["days"][0]
day_info = {
"tempMax": info_day["tempmax"],
"tempMin": info_day["tempmin"],
"temp": info_day["temp"],
"feelsLike": info_day["feelslike"],
"humidity": info_day["humidity"],
"precip": info_day["precip"],
"windSpeed": info_day["windspeed"],
"windDir": info_day["winddir"],
"pressure": info_day["pressure"],
"cloudCover": info_day["cloudcover"]
}
last_hour_info = info_day["hours"][-1] if "hours" in info_day and len(info_day["hours"]) > 0 else {}
last_hour_data = {
"temp": last_hour_info.get("temp", 0),
"feelsLike": last_hour_info.get("feelslike", 0),
"humidity": last_hour_info.get("humidity", 0),
"precip": last_hour_info.get("precip", 0),
"windSpeed": last_hour_info.get("windspeed", 0),
"windDir": last_hour_info.get("winddir", 0),
"pressure": last_hour_info.get("pressure", 0),
"cloudCover": last_hour_info.get("cloudcover", 0)
}
return {
"day_info": day_info,
"last_hour_info": last_hour_data
}
except requests.exceptions.RequestException as e:
print(f"Error with the weather API request: {e}")
return {"error": "API request failed."}
except KeyError as e:
print(f"Missing expected data in the response: {e}")
return {"error": "Missing data in the response."}
except Exception as e:
print(f"An unexpected error occurred: {e}")
return {"error": "An unexpected error occurred."}
def get_capt():
try:
with open("capteurs.json", "r") as f:
capt_d = json.load(f)
capt = {
"temp": capt_d["temp"],
"feelsLike": capt_d["feelslike"],
"humidity": capt_d["humidity"],
"precip": capt_d["precip"],
"windSpeed": capt_d["windspeed"],
"windDir": capt_d["winddir"],
"pressure": capt_d["pressure"],
"cloudCover": capt_d["cloudcover"],
"conditions":capt_d["conditions"],
"icon" : capt_d["icon"]
}
return {
"capt": capt
}
except requests.exceptions.RequestException as e:
print(f"Error with the weather API request: {e}")
return {"error": "API request failed."}
except KeyError as e:
print(f"Missing expected data in the response: {e}")
return {"error": "Missing data in the response."}
except Exception as e:
print(f"An unexpected error occurred: {e}")
return {"error": "An unexpected error occurred."}
# Simule 5 bateaux autour
def generate_ais():
return [
{
"lat": gps_data["lat"] + random.uniform(-1, 1),
"lon": gps_data["lon"] + random.uniform(-1, 1)
}
for _ in range(5)
]
@app.route("/")
def index():
return render_template("index.html")
@app.route("/donnees")
def donnees():
meteo = get_weather(gps_data["lat"], gps_data["lon"],API_KEY)
print(meteo)
ais = generate_ais()
capt = get_capt()
print(capt)
return jsonify({
"gps": gps_data,
"meteo": meteo,
"ais": ais,
"capt": capt["capt"]
})
@app.route('/angles')
def get_angles():
angle_bateau = random.randint(0, 360) # Angle du bateau
angle_vent = random.randint(0, 360) # Angle du vent
angle_voile = random.randint(0, 360) # Angle de la voile
return jsonify({
'bateau': angle_bateau,
'vent': angle_vent,
'voile': angle_voile
})
if __name__ == "__main__":
t = threading.Thread(target=lire_gps)
t.daemon = True
t.start()
app.run(debug=True)