-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGolemioAPI.cpp
More file actions
404 lines (347 loc) · 13 KB
/
GolemioAPI.cpp
File metadata and controls
404 lines (347 loc) · 13 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#include "GolemioAPI.h"
#include "../utils/Logger.h"
#include "../utils/HttpUtils.h"
#include "../utils/TimeUtils.h"
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <time.h>
GolemioAPI::GolemioAPI() : statusCallback(nullptr) {}
void GolemioAPI::setStatusCallback(APIStatusCallback callback)
{
statusCallback = callback;
}
TransitAPI::APIResult GolemioAPI::fetchDepartures(const Config& config)
{
TransitAPI::APIResult result = {};
result.departureCount = 0;
result.hasError = false;
result.stopName[0] = '\0';
result.errorMsg[0] = '\0';
// Validate inputs (use Prague-specific fields)
if (strlen(config.pragueApiKey) == 0 || strlen(config.pragueStopIds) == 0)
{
result.hasError = true;
strlcpy(result.errorMsg, "Missing API key or stop IDs", sizeof(result.errorMsg));
return result;
}
logTimestamp();
debugPrintln("API: Fetching departures...");
logMemory("api_start");
// Temporary array to collect all departures from all stops
// IMPORTANT: Static to avoid stack overflow (~2KB array)
static Departure tempDepartures[MAX_TEMP_DEPARTURES];
int tempCount = 0;
// Parse comma-separated stop IDs (use Prague-specific field)
char stopIdsCopy[128];
strlcpy(stopIdsCopy, config.pragueStopIds, sizeof(stopIdsCopy));
char* stopId = strtok(stopIdsCopy, ",");
bool firstStop = true;
while (stopId != NULL && tempCount < MAX_TEMP_DEPARTURES)
{
// Trim whitespace
while (*stopId == ' ')
stopId++;
if (strlen(stopId) == 0)
{
stopId = strtok(NULL, ",");
continue;
}
querySingleStop(stopId, config, tempDepartures, tempCount, result.stopName, firstStop);
delay(1000);
stopId = strtok(NULL, ",");
}
// Sort all collected departures by ETA
if (tempCount > 0)
{
qsort(tempDepartures, tempCount, sizeof(Departure), compareDepartures);
char msg[64];
snprintf(msg, sizeof(msg), "Collected %d departures from all stops", tempCount);
logTimestamp();
debugPrintln(msg);
}
// Copy to final array (no filtering here - main.cpp handles filtering after fetch)
// Note: Device-side filtering is needed because:
// 1. Network latency causes departures to age during HTTP request/response
// 2. API issues may return stale data despite server-side filtering
// 3. Server-side offset filtering is useful but not sufficient alone
result.departureCount = 0;
for (int i = 0; i < tempCount && result.departureCount < MAX_DEPARTURES; i++)
{
result.departures[result.departureCount] = tempDepartures[i];
result.departureCount++;
}
char filterMsg[64];
snprintf(filterMsg, sizeof(filterMsg), "Final departures after filtering: %d", result.departureCount);
logTimestamp();
debugPrintln(filterMsg);
// Set error status if no departures found
if (tempCount == 0)
{
result.hasError = true;
strlcpy(result.errorMsg, "No departures", sizeof(result.errorMsg));
}
logMemory("api_complete");
return result;
}
bool GolemioAPI::querySingleStop(const char* stopId,
const Config& config,
Departure* tempDepartures,
int& tempCount,
char* stopName,
bool& isFirstStop)
{
char queryMsg[96];
snprintf(queryMsg, sizeof(queryMsg), "API: Querying stop %s", stopId);
logTimestamp();
debugPrintln(queryMsg);
HTTPClient http;
char url[512];
// Query each stop with MAX_DEPARTURES to ensure good caching and sorting
// minutesBefore semantics (per official Golemio docs):
// Positive: start from PAST (e.g., 5 = "5 minutes ago")
// Negative: start from FUTURE (e.g., -3 = "3 minutes from now", excludes 0-3 min)
// Zero: start from NOW (includes all future departures)
// Use negative value to implement minDepartureTime filtering server-side
// Device side filtering will happen after API call also, we request with filter to make response space effective
int minutesBeforeParam = config.minDepartureTime > 0 ? (config.minDepartureTime * -1) : 0;
snprintf(url,
sizeof(url),
"https://api.golemio.cz/v2/pid/departureboards?ids=%s&total=%d&preferredTimezone=Europe/"
"Prague&minutesBefore=%d&minutesAfter=120",
stopId,
DEPS_PER_STOP,
minutesBeforeParam);
logTimestamp();
debugPrint("Golemio API: minDepartureTime=");
debugPrint(config.minDepartureTime);
debugPrint(" → minutesBefore=");
debugPrint(minutesBeforeParam);
debugPrintln("");
http.begin(url);
http.addHeader("x-access-token", config.pragueApiKey);
http.addHeader("Content-Type", "application/json");
http.setTimeout(HTTP_TIMEOUT_MS);
const int MAX_RETRIES = 3;
int httpCode = -1;
for (int retry = 0; retry < MAX_RETRIES; retry++)
{
if (retry > 0)
{
int delayMs = 2000 * retry; // 2s, 4s, 6s backoff
// Update display with retry status
char statusMsg[64];
snprintf(statusMsg, sizeof(statusMsg), "API Retry %d/%d", retry, MAX_RETRIES);
if (statusCallback)
{
statusCallback(statusMsg);
}
char retryMsg[64];
snprintf(retryMsg, sizeof(retryMsg), "API: Retry %d after %dms", retry, delayMs);
logTimestamp();
debugPrintln(retryMsg);
delay(delayMs);
}
httpCode = http.GET();
// Success - break out of retry loop
if (httpCode == HTTP_CODE_OK)
break;
// Don't retry on 4xx errors (client errors - won't fix with retry)
if (httpCode >= 400 && httpCode < 500)
{
char clientErrMsg[64];
snprintf(clientErrMsg, sizeof(clientErrMsg), "API: Client error %d - no retry", httpCode);
logTimestamp();
debugPrintln(clientErrMsg);
break;
}
// Log retry-able errors
if (retry < MAX_RETRIES - 1)
{
char retryableErrMsg[64];
snprintf(retryableErrMsg, sizeof(retryableErrMsg), "API Error: HTTP %d - will retry", httpCode);
logTimestamp();
debugPrintln(retryableErrMsg);
// Show error on display before first retry
if (retry == 0 && statusCallback)
{
char errorMsg[64];
if (httpCode == -1)
{
snprintf(errorMsg, sizeof(errorMsg), "API Error: No Connection");
}
else
{
snprintf(errorMsg, sizeof(errorMsg), "API Error: HTTP %d", httpCode);
}
statusCallback(errorMsg);
delay(1000); // Show error message briefly
}
}
}
if (httpCode == HTTP_CODE_OK)
{
String payload = readHttpResponse(http, JSON_BUFFER_SIZE, config.debugMode);
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, payload);
if (error)
{
char jsonErrMsg[128];
snprintf(jsonErrMsg, sizeof(jsonErrMsg), "JSON Parse Error for stop %s: %s", stopId, error.c_str());
logTimestamp();
debugPrintln(jsonErrMsg);
http.end();
return false;
}
// Get stop name from first stop (for display)
if (isFirstStop && doc.containsKey("stops") && doc["stops"].size() > 0)
{
const char* name = doc["stops"][0]["stop_name"];
if (name)
{
strlcpy(stopName, name, 64);
// Note: UTF-8 to ISO-8859-2 conversion now handled by DisplayManager
}
isFirstStop = false;
}
// Parse departures from this stop
if (doc.containsKey("departures"))
{
JsonArray deps = doc["departures"];
for (JsonObject dep : deps)
{
if (tempCount >= MAX_TEMP_DEPARTURES)
break;
parseDepartureObject(dep, config, tempDepartures, tempCount, stopId);
}
}
http.end();
return true;
}
else
{
char failMsg[128];
snprintf(failMsg,
sizeof(failMsg),
"API: Failed after %d attempts for stop %s - HTTP %d",
MAX_RETRIES,
stopId,
httpCode);
logTimestamp();
debugPrintln(failMsg);
http.end();
return false;
}
}
void GolemioAPI::parseDepartureObject(JsonObject depJson,
const Config& config,
Departure* tempDepartures,
int& tempCount,
const char* stopId)
{
// Route/Line info
const char* line = depJson["route"]["short_name"];
if (line)
{
strlcpy(tempDepartures[tempCount].line, line, sizeof(tempDepartures[0].line));
stripSpaces(tempDepartures[tempCount].line);
stripBrackets(tempDepartures[tempCount].line);
}
else
{
tempDepartures[tempCount].line[0] = '\0';
}
// Destination/Headsign
const char* headsign = depJson["trip"]["headsign"];
if (headsign)
{
strlcpy(tempDepartures[tempCount].destination, headsign, sizeof(tempDepartures[0].destination));
shortenDestination(tempDepartures[tempCount].destination); // Shorten while still UTF-8
// Note: UTF-8 to ISO-8859-2 conversion now handled by DisplayManager
}
else
{
tempDepartures[tempCount].destination[0] = '\0';
}
// Parse and store departure timestamp
const char* timestamp = depJson["departure_timestamp"]["predicted"];
if (!timestamp)
{
timestamp = depJson["departure_timestamp"]["scheduled"];
}
if (timestamp)
{
time_t depTime = parseTimestamp(timestamp);
if (depTime == -1)
{
logTimestamp();
debugPrint("Golemio API: Skipping departure - failed to parse timestamp: ");
debugPrintln(timestamp);
return; // Skip if timestamp parse fails
}
// Store timestamp for future recalculation
tempDepartures[tempCount].departureTime = depTime;
// Calculate initial ETA for sorting/filtering
time_t now;
time(&now);
int diffSec = difftime(depTime, now);
int eta = (diffSec > 0) ? (diffSec / 60) : 0;
tempDepartures[tempCount].eta = eta;
}
else
{
tempDepartures[tempCount].departureTime = 0;
tempDepartures[tempCount].eta = 0;
}
// Air conditioning
tempDepartures[tempCount].hasAC = depJson["trip"]["is_air_conditioned"] | false;
// Delay info
if (depJson.containsKey("delay") && !depJson["delay"].isNull())
{
tempDepartures[tempCount].isDelayed = true;
tempDepartures[tempCount].delayMinutes = depJson["delay"]["minutes"] | 0;
}
else
{
tempDepartures[tempCount].isDelayed = false;
tempDepartures[tempCount].delayMinutes = 0;
}
// Platform/track info (optional, from stop object)
tempDepartures[tempCount].platform[0] = '\0'; // Initialize empty
// Extract platform_code from stop object (nested in departure)
if (depJson.containsKey("stop") && depJson["stop"].containsKey("platform_code"))
{
const char* platformCode = depJson["stop"]["platform_code"];
if (platformCode && strlen(platformCode) > 0)
{
// Truncate to 3 characters if longer
strncpy(tempDepartures[tempCount].platform, platformCode, 3);
tempDepartures[tempCount].platform[3] = '\0';
if (config.debugMode && strlen(platformCode) > 3)
{
char warnMsg[64];
snprintf(
warnMsg, sizeof(warnMsg), "Golemio: Platform truncated '%s' -> '%.3s'", platformCode, platformCode);
debugPrintln(warnMsg);
}
}
}
// Source stop ID (for platform symbol matching)
strlcpy(tempDepartures[tempCount].sourceStopId, stopId, sizeof(tempDepartures[0].sourceStopId));
// Debug log for first few departures (only if debug mode enabled)
if (config.debugMode && tempCount < 3)
{
logTimestamp();
char debugMsg[128];
snprintf(debugMsg,
sizeof(debugMsg),
"Golemio: Line %s to %s - ETA:%d (Plt:%s, AC:%d, Delay:%d)",
tempDepartures[tempCount].line,
tempDepartures[tempCount].destination,
tempDepartures[tempCount].eta,
tempDepartures[tempCount].platform[0] ? tempDepartures[tempCount].platform : "-",
tempDepartures[tempCount].hasAC ? 1 : 0,
tempDepartures[tempCount].delayMinutes);
debugPrintln(debugMsg);
}
tempCount++;
}