-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.py
More file actions
282 lines (240 loc) · 8.97 KB
/
engine.py
File metadata and controls
282 lines (240 loc) · 8.97 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env python3
"""F1 Live Widget — Event Detection Engine.
Läuft alle 3 Sekunden via Übersicht-Shell-Kommando,
vergleicht session.json mit gespeichertem state.json,
erkennt Ereignisse (Positionswechsel, Boxenstopps, schnellste Runde,
Safety Car, virtuelles Safety Car, Rote Flagge, Rennstart, Zielflagge)
und gibt sie per TTS und Sound aus.
"""
import json
import os
import subprocess
import sys
import time
from datetime import datetime
from pathlib import Path
DATA_DIR = Path.home() / ".f1"
VOICE = "Anna"
STATE_PATH = DATA_DIR / "state.json"
SESSION_PATH = DATA_DIR / "session.json"
FEED_PATH = DATA_DIR / "feed.json"
AUDIO_ON_PATH = DATA_DIR / "audio_on"
def load_json(path):
try:
with open(path, encoding="utf-8") as f:
return json.load(f)
except Exception:
return {}
def save_json(path, data):
try:
with open(path, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2)
except Exception:
pass
def speak(text):
try:
subprocess.Popen(
["say", "-v", VOICE, text],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
except Exception:
pass
def play_sound(sound):
try:
subprocess.Popen(
["afplay", f"/System/Library/Sounds/{sound}.aiff"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
except Exception:
pass
def _now():
return time.time()
def _should_trigger(protection, event_key, cooldown=30):
"""Duplicate protection: don't trigger same event within cooldown seconds."""
last = protection.get(event_key, 0)
if _now() - last < cooldown:
return False
protection[event_key] = _now()
return True
def detect_events(old_state, new_state):
"""Compare old and new session state and return list of event dicts."""
events = []
protection = old_state.get("_protection", {})
old_session = old_state.get("session") or {}
new_session = new_state.get("session") or {}
old_standings = old_state.get("standings", [])
new_standings = new_state.get("standings", [])
old_race_control = old_state.get("race_control", [])
new_race_control = new_state.get("race_control", [])
old_rc_ids = {rc.get("time", "") + rc.get("message", "") for rc in old_race_control}
new_rc_entries = [rc for rc in new_race_control if (rc.get("time", "") + rc.get("message", "")) not in old_rc_ids]
# 1. POSITION_CHANGE — position changed in top-5
old_pos = {s.get("driver_number"): s.get("position") for s in old_standings}
for s in new_standings:
num = s.get("driver_number")
pos = s.get("position")
if num is None or pos is None:
continue
if pos > 5:
continue
old_p = old_pos.get(num)
if old_p is not None and old_p != pos:
key = f"pos_change_{num}"
if _should_trigger(protection, key, 30):
events.append({
"type": "POSITION_CHANGE",
"text": f"{s.get('code', '')} ist jetzt {pos}.",
"sound": None,
"driver_number": num,
"position": pos,
})
# 2. PIT_STOP — on_pit_lap became True
old_pit = {s.get("driver_number"): bool(s.get("on_pit_lap")) for s in old_standings}
for s in new_standings:
num = s.get("driver_number")
if num is None:
continue
if s.get("on_pit_lap") and not old_pit.get(num, False):
key = f"pit_{num}"
if _should_trigger(protection, key, 30):
events.append({
"type": "PIT_STOP",
"text": f"{s.get('code', '')} in der Box.",
"sound": None,
"driver_number": num,
})
# 3. FASTEST_LAP — fastest lap changed
def _fastest_lap_driver(standings):
best = None
best_driver = None
for s in standings:
lap = s.get("last_lap", "")
if not lap:
continue
try:
parts = lap.split(":")
if len(parts) == 2:
total = int(parts[0]) * 60 + float(parts[1])
else:
total = float(lap)
if best is None or total < best:
best = total
best_driver = s
except Exception:
continue
return best_driver
old_fastest = _fastest_lap_driver(old_standings)
new_fastest = _fastest_lap_driver(new_standings)
if new_fastest and old_fastest:
if new_fastest.get("driver_number") != old_fastest.get("driver_number"):
key = f"fastest_{new_fastest.get('driver_number')}"
if _should_trigger(protection, key, 30):
events.append({
"type": "FASTEST_LAP",
"text": f"Schnellste Runde — {new_fastest.get('code', '')}!",
"sound": "Purr",
"driver_number": new_fastest.get("driver_number"),
})
# 4. SAFETY_CAR — race_control new entry with category="SafetyCar" and "DEPLOYED"
for rc in new_rc_entries:
cat = (rc.get("category") or "").upper()
msg = (rc.get("message") or "").upper()
if cat == "SAFETYCAR" and "DEPLOYED" in msg:
key = "safety_car"
if _should_trigger(protection, key, 30):
events.append({
"type": "SAFETY_CAR",
"text": "Safety Car! Das Sicherheitsfahrzeug ist auf der Strecke.",
"sound": "Sosumi",
})
# 5. VIRTUAL_SAFETY_CAR — race_control contains "VIRTUAL SAFETY CAR DEPLOYED"
for rc in new_rc_entries:
msg = (rc.get("message") or "").upper()
if "VIRTUAL SAFETY CAR DEPLOYED" in msg:
key = "vsc"
if _should_trigger(protection, key, 30):
events.append({
"type": "VIRTUAL_SAFETY_CAR",
"text": "Virtuelles Safety Car aktiviert.",
"sound": None,
})
# 6. RED_FLAG — race_control new entry with flag="RED"
for rc in new_rc_entries:
if rc.get("flag") == "RED":
key = "red_flag"
if _should_trigger(protection, key, 30):
events.append({
"type": "RED_FLAG",
"text": "Rote Flagge! Das Rennen ist unterbrochen.",
"sound": "Basso",
})
# 7. RACE_START — old session.live was false, new is true and name == "Race"
old_live = bool(old_session.get("live", False))
new_live = bool(new_session.get("live", False))
new_name = new_session.get("session_name", "")
if not old_live and new_live and new_name == "Race":
key = "race_start"
if _should_trigger(protection, key, 30):
events.append({
"type": "RACE_START",
"text": "Das Rennen hat begonnen!",
"sound": "Hero",
})
# 8. CHEQUERED_FLAG — race_control new entry with flag="CHEQUERED"
for rc in new_rc_entries:
if rc.get("flag") == "CHEQUERED":
key = "chequered_flag"
if _should_trigger(protection, key, 30):
p1 = None
for s in new_standings:
if s.get("position") == 1:
p1 = s
break
winner_code = p1.get("code", "") if p1 else ""
events.append({
"type": "CHEQUERED_FLAG",
"text": f"{winner_code} gewinnt das Rennen!" if winner_code else "Zielflagge! Das Rennen ist beendet.",
"sound": "Hero",
})
return events, protection
def main():
audio_on = AUDIO_ON_PATH.exists()
old_state = load_json(STATE_PATH)
new_state = load_json(SESSION_PATH)
# Ensure basic structure
if "_protection" not in old_state:
old_state["_protection"] = {}
new_session = new_state.get("session") or {}
session_live = bool(new_session.get("live", False))
events = []
protection = old_state.get("_protection", {})
if session_live:
events, protection = detect_events(old_state, new_state)
# Update feed
feed = load_json(FEED_PATH)
if not isinstance(feed, list):
feed = []
for ev in events:
feed.insert(0, {
"time": datetime.now().strftime("%H:%M:%S"),
"type": ev["type"],
"text": ev["text"],
})
feed = feed[:20]
# Save state
new_state["_protection"] = protection
save_json(STATE_PATH, new_state)
save_json(FEED_PATH, feed)
if audio_on and events:
for ev in events:
sound = ev.get("sound")
if sound:
play_sound(sound)
speak(ev["text"])
if __name__ == "__main__":
try:
main()
except Exception as e:
sys.stderr.write(str(e))