Repository navigation
Expand file tree
/
Copy pathapi-client.js
More file actions
706 lines (610 loc) · 30.1 KB
/
Copy pathapi-client.js
File metadata and controls
706 lines (610 loc) · 30.1 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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
const API_CONFIG = {
PROXY_URL: './proxy.php',
REQUESTOR_REF: 'NOWE26',
LOCATION_URL: 'https://api.opentransportdata.swiss/ojp20'
};
// ─── OJP Stop-Event Request ─────────────────────────────────────────────────
function createOJPRequest(stopId, queryTime = null) {
const now = new Date().toISOString();
// Falls queryTime ein naiver String ist (z.B. aus buildISO),
// müssen wir sicherstellen, dass er für die Schweiz gilt.
// Wir hängen '+02:00' (Sommerzeit) oder '+01:00' (Winterzeit) an,
// oder wir lassen queryTime als lokalen String, wenn die API das akzeptiert.
// Sicherster Weg für OJP: Ein echtes ISO-Format mit Zeitzone.
let depArrTime = queryTime;
if (queryTime && !queryTime.includes('+') && !queryTime.includes('Z')) {
// Hier könntest du prüfen, ob wir Sommer- oder Winterzeit haben.
// Einfacher Workaround: Da du lokal suchst,
// konvertiere queryTime kurz in ein Date-Objekt.
depArrTime = new Date(queryTime).toISOString();
} else if (!queryTime) {
depArrTime = now;
}
return `<?xml version="1.0" encoding="UTF-8"?>
<OJP xmlns="http://www.vdv.de/ojp"
xmlns:siri="http://www.siri.org.uk/siri"
version="2.0">
<OJPRequest>
<siri:ServiceRequest>
<siri:RequestTimestamp>${now}</siri:RequestTimestamp>
<siri:RequestorRef>${API_CONFIG.REQUESTOR_REF}</siri:RequestorRef>
<OJPStopEventRequest>
<siri:RequestTimestamp>${now}</siri:RequestTimestamp>
<Location>
<PlaceRef>
<StopPlaceRef>${stopId}</StopPlaceRef>
</PlaceRef>
<DepArrTime>${depArrTime}</DepArrTime>
</Location>
<Params>
<NumberOfResults>25</NumberOfResults>
<StopEventType>departure</StopEventType>
<IncludePreviousCalls>true</IncludePreviousCalls>
<IncludeOnwardCalls>true</IncludeOnwardCalls>
<IncludeOperatingDays>true</IncludeOperatingDays>
</Params>
</OJPStopEventRequest>
</siri:ServiceRequest>
</OJPRequest>
</OJP>`;
}
export async function fetchDepartures(stopId, queryTime = null) {
try {
let actualStopId = stopId;
// ✅ Legacy-Fallback: Falls stopId eine alte DiDok-ID ist, resolve zu SLOID
if (window.didokMapping && window.didokMapping[stopId]) {
const stationName = window.didokMapping[stopId];
console.log(`🔄 DIDOK-Fallback: "${stopId}" → "${stationName}"`);
const results = await searchStops(stationName);
if (results && results.length > 0) {
actualStopId = String(results[0].ref);
console.log(`✓ Aufgelöst zu SLOID: "${actualStopId}"`);
} else {
console.warn(`⚠️ DIDOK-Code konnte nicht aufgelöst werden: ${stopId}`);
// Fallback: Nutze stopId direkt (wird wahrscheinlich fehlschlag)
actualStopId = String(stopId);
}
}
// ✅ Validierung: actualStopId muss entweder SLOID oder externe ID sein
// SLOID: ch:1:sloid:XXXXX
// Extern: beliebiges Format (z.B. 8014440_gen:missingSLOID_pf:1, 8718462)
if (!/(^ch:1:sloid:\d+$|^\d{3,})/.test(actualStopId)) {
console.warn(`⚠️ WARNUNG: Stop-ID ist nicht im erwarteten Format: "${actualStopId}". Versuche trotzdem.`);
// Nicht werfen, aber loggen — externe IDs können ungewöhnliche Formate haben
}
const xmlBody = createOJPRequest(actualStopId, queryTime);
const response = await fetch(API_CONFIG.PROXY_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/xml' },
body: xmlBody
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Proxy-Status: ${response.status} - ${errorText}`);
}
const xmlData = await response.text();
return parseOJPResponse(xmlData);
} catch (error) {
console.error('OJP API Error:', error);
throw error;
}
}
// ─── Timezone Conversion Helper ─────────────────────────────────────────────
/**
* Konvertiert einen UTC-ISO-String der API sicher in die lokale Schweizer Zeit.
* Gibt einen naiven ISO-String zurück (YYYY-MM-DDTHH:mm:ss).
*/
function utcToLocalISO(utcString) {
if (!utcString) return null;
try {
const d = new Date(utcString);
// "sv-SE" Format ist YYYY-MM-DD HH:mm:ss, sehr nah an ISO
// Wir erzwingen die Europe/Zurich Zeitzone
const localFormatter = new Intl.DateTimeFormat('sv-SE', {
timeZone: 'Europe/Zurich',
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
});
const parts = localFormatter.formatToParts(d);
const f = (type) => parts.find(p => p.type === type).value;
return `${f('year')}-${f('month')}-${f('day')}T${f('hour')}:${f('minute')}:${f('second')}`;
} catch (e) {
return null;
}
}
// ─── XML Escape Helper ──────────────────────────────────────────────────────
function escapeXml(unsafe) {
// ✅ Konvertiere zu String, falls nicht schon einer
const str = String(unsafe || '');
return str.replace(/[<>&'"]/g, function (c) {
switch (c) {
case '<': return '<';
case '>': return '>';
case '&': return '&';
case '\'': return ''';
case '"': return '"';
}
});
}
// ─── OJP Location Information Request (Stopssuche) ──────────────────────────
function createLocationRequest(queryString) {
const now = new Date().toISOString();
const escapedQuery = escapeXml(queryString);
const msgId = 'LIR-' + Math.random().toString(36).substr(2, 9);
return `<?xml version="1.0" encoding="UTF-8"?>
<OJP xmlns="http://www.vdv.de/ojp" xmlns:siri="http://www.siri.org.uk/siri" version="2.0">
<OJPRequest>
<siri:ServiceRequest>
<siri:RequestTimestamp>${now}</siri:RequestTimestamp>
<siri:RequestorRef>${API_CONFIG.REQUESTOR_REF}</siri:RequestorRef>
<OJPLocationInformationRequest>
<siri:RequestTimestamp>${now}</siri:RequestTimestamp>
<siri:MessageIdentifier>${msgId}</siri:MessageIdentifier>
<InitialInput>
<Name>${escapedQuery}</Name>
</InitialInput>
<Restrictions>
<NumberOfResults>10</NumberOfResults>
</Restrictions>
</OJPLocationInformationRequest>
</siri:ServiceRequest>
</OJPRequest>
</OJP>`;
}
export async function searchStops(queryString) {
try {
const xmlBody = createLocationRequest(queryString);
//console.log('🔍 Suche nach:', queryString);
//console.log('📤 XML-Body:', xmlBody.substring(0, 300));
const response = await fetch(API_CONFIG.PROXY_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/xml' },
body: xmlBody
});
const xmlData = await response.text();
//console.log('📥 Response Status:', response.status);
//console.log('📥 Response Body:', xmlData.substring(0, 500));
if (!response.ok) {
console.warn('⚠️ Response nicht OK:', response.status);
return [];
}
const results = parseLocationResponse(xmlData);
//console.log('✅ Gefundene Halte:', results);
return results;
} catch (error) {
console.error('❌ Location search error:', error);
return [];
}
}
function parseLocationResponse(xmlString) {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlString, 'text/xml');
const results = [];
if (xmlDoc.getElementsByTagName('parsererror').length > 0) {
console.error('❌ XML Parse-Fehler:', xmlDoc.getElementsByTagName('parsererror')[0].textContent);
return [];
}
console.log('🔍 Parsing XML, Root-Element:', xmlDoc.documentElement.tagName);
let placeResults = xmlDoc.getElementsByTagName('PlaceResult');
console.log('📍 Gefundene PlaceResult-Elemente:', placeResults.length);
for (let i = 0; i < placeResults.length; i++) {
const result = placeResults[i];
console.log(`\n--- PlaceResult #${i} ---`);
const place = result.getElementsByTagName('Place')[0];
if (!place) {
console.log(' ❌ Kein Place-Element gefunden');
continue;
}
const stopPlace = place.getElementsByTagName('StopPlace')[0];
if (!stopPlace) {
console.log(' ❌ Kein StopPlace-Element gefunden');
continue;
}
const ref = stopPlace.getElementsByTagName('StopPlaceRef')[0]?.textContent?.trim() ?? '';
console.log(` 📌 StopPlaceRef: "${ref}"`);
// Logging: Alle Namen-Quellen anschauen
const placeName = place.getElementsByTagName('Name')[0]
?.getElementsByTagName('Text')[0]?.textContent?.trim() ?? '';
const stopPlaceName = stopPlace.getElementsByTagName('StopPlaceName')[0]
?.getElementsByTagName('Text')[0]?.textContent?.trim() ?? '';
console.log(` 📝 Place/Name/Text: "${placeName}"`);
console.log(` 📝 StopPlace/StopPlaceName/Text: "${stopPlaceName}"`);
// Verwende StopPlaceName (sauber und kurz)
const name = stopPlaceName || placeName;
console.log(` ✅ Verwendet: "${name}"`);
if (ref && name) {
results.push({ ref, name });
console.log(` ➕ Zur Liste hinzugefügt`);
} else {
console.log(` ⚠️ Übersprungen (ref="${ref}", name="${name}")`);
}
}
console.log(`\n📊 Finale Ergebnisse vor Deduplication: ${results.length} Einträge`);
const deduped = results.filter((v, i, a) => a.findIndex(t => t.ref === v.ref) === i);
console.log(`📊 Nach Deduplication: ${deduped.length} Einträge`);
deduped.forEach((r, idx) => console.log(` [${idx}] ${r.name} (${r.ref})`));
return deduped;
}
/**
* Dedupliziert Abfahrten nach: Zugnummer + Zielstation
* Priorität: Eintrag mit "Fahrplanänderung" gewinnt
*/
function deduplicateDepartures(departures) {
const seen = new Map();
const result = [];
for (let i = 0; i < departures.length; i++) {
const dep = departures[i];
const onwardFingerprint = dep.calls.onward
.map(c => c.stopRef)
.join('|');
const key = `${dep.journeyRef}|${onwardFingerprint}`;
if (seen.has(key)) {
const prevIdx = seen.get(key);
const prev = result[prevIdx];
const currHasFABA = dep.situations?.some(s => s.summary?.includes('Fahrplanänderung'));
const prevHasFABA = prev.situations?.some(s => s.summary?.includes('Fahrplanänderung'));
if (currHasFABA && !prevHasFABA) {
result[prevIdx] = dep;
console.warn(`🔄 ${key} → Neue Version mit Fahrplanänderung`);
} else if (!currHasFABA && !prevHasFABA) {
result[prevIdx] = dep;
console.warn(`🔄 ${key} → Neuerer Eintrag`);
}
} else {
seen.set(key, result.length);
result.push(dep);
}
}
return result;
}
// ─── Parse Stop-Event Response ───────────────────────────────────────────────
function parseOJPResponse(xmlString) {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlString, 'text/xml');
// 1. Hinweistexte (Situations) extrahieren
const situationsMap = {};
const ptSituations = xmlDoc.getElementsByTagName('PtSituation');
console.log(`📍 Gefundene PtSituation-Elemente: ${ptSituations.length}`);
for (const sit of ptSituations) {
const idNode = sit.getElementsByTagName('siri:SituationNumber')[0] || sit.getElementsByTagName('SituationNumber')[0];
const id = idNode?.textContent;
const sumNode = sit.getElementsByTagName('siri:SummaryText')[0] || sit.getElementsByTagName('SummaryText')[0];
const reasonNode = sit.getElementsByTagName('siri:ReasonText')[0] || sit.getElementsByTagName('ReasonText')[0];
const descNode = sit.getElementsByTagName('siri:DescriptionText')[0] || sit.getElementsByTagName('DescriptionText')[0];
const consNode = sit.getElementsByTagName('siri:ConsequenceText')[0] || sit.getElementsByTagName('ConsequenceText')[0];
const recNode = sit.getElementsByTagName('siri:RecommendationText')[0] || sit.getElementsByTagName('RecommendationText')[0];
const durNode = sit.getElementsByTagName('siri:DurationText')[0] || sit.getElementsByTagName('DurationText')[0];
if (id) {
situationsMap[id] = {
summary: sumNode?.textContent || '',
reason: reasonNode?.textContent || '',
desc: descNode?.textContent || '',
consequence: consNode?.textContent || '',
rec: recNode?.textContent || '',
duration: durNode?.textContent || ''
};
console.log(` ✓ Situation ${id}: "${situationsMap[id].summary.substring(0, 50)}..."`);
}
}
console.log(`📊 Gesamt Situations in situationsMap: ${Object.keys(situationsMap).length}`);
// --- Interne Helfer ---
function getStopName(callNode) {
const nameEl = callNode.getElementsByTagName('StopPointName')[0];
return nameEl?.getElementsByTagName('Text')[0]?.textContent?.trim() ?? null;
}
function getStopRef(callNode) {
const spRef = callNode.getElementsByTagName('siri:StopPointRef')[0]?.textContent?.trim();
if (!spRef) return null;
// ✅ Swiss SLOID: Extrahiere Parent-SLOID ohne Perron/Quay
// Format: ch:1:sloid:79897:0:1 → ch:1:sloid:79897
// ch:1:sloid:90015 → ch:1:sloid:90015
const swissMatch = spRef.match(/^(ch:1:sloid:\d+)(?::|$)/);
if (swissMatch) {
return swissMatch[1];
}
// ✅ External IDs: Behalte sie als-is
// Deutschland: 8014440_gen:missingSLOID_pf:1
// Frankreich: 8718462
return spRef;
}
function getTimeInfo(callNode, tagName) {
const el = callNode.getElementsByTagName(tagName)[0];
if (!el) return { timetabled: null, estimated: null, delayed: false, delayDisplay: null, delayMinutes: 0 };
let ttRaw = el.getElementsByTagName('TimetabledTime')[0]?.textContent?.trim() ?? null;
let estRaw = el.getElementsByTagName('EstimatedTime')[0]?.textContent?.trim() ?? null;
const timetabled = utcToLocalISO(ttRaw);
const estimated = utcToLocalISO(estRaw);
const delayed = !!estimated && estimated !== timetabled;
let delayMinutes = 0;
let delayDisplay = null;
if (delayed && estRaw && ttRaw) {
const diffMs = new Date(estRaw) - new Date(ttRaw);
delayMinutes = Math.round(diffMs / 60000);
// ✅ Sowohl positive als auch negative Delays anzeigen
if (delayMinutes > 0) {
delayDisplay = `+${delayMinutes}'`;
} else if (delayMinutes < 0) {
delayDisplay = `${delayMinutes}'`; // Negativ, z.B. "-2'"
}
}
return { timetabled, estimated, delayed, delayDisplay, delayMinutes };
}
function getQuay(callNode) {
const planned = callNode.getElementsByTagName('PlannedQuay')[0]
?.getElementsByTagName('Text')[0]?.textContent?.trim() ?? null;
const estimated = callNode.getElementsByTagName('EstimatedQuay')[0]
?.getElementsByTagName('Text')[0]?.textContent?.trim() ?? null;
// Aussteigeseite spezifisch für diesen EINEN Halt suchen
let side = '';
const attributes = callNode.getElementsByTagName('Attribute');
for (const attr of attributes) {
const textNode = attr.getElementsByTagName('Text')[0];
if (textNode) {
const val = textNode.textContent;
if (val.includes('Aussteigeseite: Links')) { side = 'G'; break; }
if (val.includes('Aussteigeseite: Rechts')) { side = 'D'; break; }
}
}
return {
planned,
estimated,
text: estimated ?? planned ?? '',
isChanged: estimated !== null && planned !== null && estimated !== planned,
side: side
};
}
function parseCalls(event, currentName, thisCallNode) {
const previous = [];
const onward = [];
// ─── PreviousCall ───────────────────────────────────────────────────────
const prevCalls = event.getElementsByTagName('PreviousCall');
for (let i = 0; i < prevCalls.length; i++) {
const call = prevCalls[i];
const name = getStopName(call);
if (!name) continue;
const q = getQuay(call);
const arr = getTimeInfo(call, 'ServiceArrival');
const dep = getTimeInfo(call, 'ServiceDeparture');
const isCancelled = call.getElementsByTagName('NotServicedStop')[0]?.textContent === 'true';
const noBoardingAtStop = call.getElementsByTagName('NoBoardingAtStop')[0]?.textContent === 'true';
const noAlightingAtStop = call.getElementsByTagName('NoAlightingAtStop')[0]?.textContent === 'true';
const isRequestStop = call.getElementsByTagName('RequestStop')[0]?.textContent === 'true';
const unplannedStop = call.getElementsByTagName('UnplannedStop')[0]?.textContent === 'true';
previous.push({
name,
arrivalTime: arr.timetabled,
arrivalEstimated: arr.estimated,
arrivalDelayed: arr.delayed,
arrivalDelayDisplay: arr.delayDisplay,
arrivalDelayMinutes: arr.delayMinutes,
departureTime: dep.timetabled,
departureEstimated: dep.estimated,
departureDelayed: dep.delayed,
departureDelayDisplay:dep.delayDisplay,
departureDelayMinutes:dep.delayMinutes,
platform: q.text,
plannedPlatform: q.planned,
estimatedPlatform: q.estimated,
platformChanged: q.isChanged,
cancelled: isCancelled,
unplannedStop,
noBoardingAtStop,
noAlightingAtStop,
requestStop: isRequestStop,
stopRef: getStopRef(call)
});
}
// ─── CurrentCall ────────────────────────────────────────────────────────
const current = thisCallNode ? (() => {
const q = getQuay(thisCallNode);
const arr = getTimeInfo(thisCallNode, 'ServiceArrival');
const dep = getTimeInfo(thisCallNode, 'ServiceDeparture');
const isCancelled = thisCallNode.getElementsByTagName('NotServicedStop')[0]?.textContent === 'true';
const isRequestStop = thisCallNode.getElementsByTagName('RequestStop')[0]?.textContent === 'true';
const unplannedStop = thisCallNode.getElementsByTagName('UnplannedStop')[0]?.textContent === 'true';
return {
name: getStopName(thisCallNode) ?? currentName,
arrivalTime: arr.timetabled,
arrivalEstimated: arr.estimated,
arrivalDelayed: arr.delayed,
arrivalDelayDisplay: arr.delayDisplay,
arrivalDelayMinutes: arr.delayMinutes,
departureTime: dep.timetabled,
departureEstimated: dep.estimated,
departureDelayed: dep.delayed,
departureDelayDisplay:dep.delayDisplay,
departureDelayMinutes:dep.delayMinutes,
platform: q.text,
plannedPlatform: q.planned,
estimatedPlatform: q.estimated,
platformChanged: q.isChanged,
cancelled: isCancelled,
unplannedStop,
requestStop: isRequestStop,
stopRef: null
};
})() : null;
// ─── OnwardCall ─────────────────────────────────────────────────────────
const onwCalls = event.getElementsByTagName('OnwardCall');
for (let i = 0; i < onwCalls.length; i++) {
const call = onwCalls[i];
const name = getStopName(call);
if (!name) continue;
const q = getQuay(call);
const arr = getTimeInfo(call, 'ServiceArrival');
const dep = getTimeInfo(call, 'ServiceDeparture');
const isCancelled = call.getElementsByTagName('NotServicedStop')[0]?.textContent === 'true';
const noBoardingAtStop = call.getElementsByTagName('NoBoardingAtStop')[0]?.textContent === 'true';
const noAlightingAtStop = call.getElementsByTagName('NoAlightingAtStop')[0]?.textContent === 'true';
const isRequestStop = call.getElementsByTagName('RequestStop')[0]?.textContent === 'true';
const unplannedStop = call.getElementsByTagName('UnplannedStop')[0]?.textContent === 'true';
onward.push({
name,
arrivalTime: arr.timetabled,
arrivalEstimated: arr.estimated,
arrivalDelayed: arr.delayed,
arrivalDelayDisplay: arr.delayDisplay,
arrivalDelayMinutes: arr.delayMinutes,
departureTime: dep.timetabled,
departureEstimated: dep.estimated,
departureDelayed: dep.delayed,
departureDelayDisplay:dep.delayDisplay,
departureDelayMinutes:dep.delayMinutes,
platform: q.text,
plannedPlatform: q.planned,
estimatedPlatform: q.estimated,
platformChanged: q.isChanged,
cancelled: isCancelled,
unplannedStop,
noBoardingAtStop,
noAlightingAtStop,
requestStop: isRequestStop,
stopRef: getStopRef(call)
});
}
return { previous, current, onward };
}
function getText(node, tagName) {
const el = node?.getElementsByTagName(tagName)[0];
if (!el) return null;
const textNode = el.getElementsByTagName('Text')[0];
return textNode ? textNode.textContent.trim() : el.textContent.trim();
}
// ─── Service-Attribute extrahieren ──────────────────────────────────────────
function getServiceAttributes(service) {
const attributes = [];
const attrElems = service.getElementsByTagName('Attribute');
for (const attr of attrElems) {
const userTextEl = attr.getElementsByTagName('UserText')[0];
const textEl = userTextEl?.getElementsByTagName('Text')[0];
const codeEl = attr.getElementsByTagName('Code')[0];
if (textEl) {
const text = textEl.textContent.trim();
const code = codeEl?.textContent?.trim() ?? '';
attributes.push({ text, code });
}
}
return attributes;
}
// --- Hauptverarbeitung ---
const results = xmlDoc.getElementsByTagName('StopEventResult');
const departures = [];
for (const result of results) {
const event = result.getElementsByTagName('StopEvent')[0];
const service = event?.getElementsByTagName('Service')[0];
const thisCall = event?.getElementsByTagName('ThisCall')[0];
const stopName = getStopName(thisCall) ?? 'Unbekannter Halt';
if (!service || !thisCall) continue;
const journeyNumber = getText(service, 'TrainNumber') ?? '';
let timetabled = thisCall.getElementsByTagName('TimetabledTime')[0]?.textContent?.trim();
let estimated = thisCall.getElementsByTagName('EstimatedTime')[0]?.textContent?.trim();
// Konvertiere in lokale Zeit
if (timetabled) timetabled = utcToLocalISO(timetabled);
if (estimated) estimated = utcToLocalISO(estimated);
const delayed = !!estimated && estimated !== timetabled;
const timeFormatted = timetabled
? (() => {
const match = timetabled.match(/T(\d{2}):(\d{2})/);
return match ? `${match[1]}:${match[2]}` : '--:--';
})()
: '--:--';
let delayMinutes = 0;
if (delayed) {
delayMinutes = Math.round((new Date(estimated) - new Date(timetabled)) / 60000);
}
let delayDisplay = "+0:00";
if (delayed && estimated && timetabled) {
const diffMs = new Date(estimated) - new Date(timetabled);
const totalSeconds = Math.max(0, Math.floor(diffMs / 1000));
const mins = Math.floor(totalSeconds / 60);
const secs = totalSeconds % 60;
delayDisplay = `+${mins}:${secs.toString().padStart(2, '0')}`;
}
const timeData = getTimeInfo(thisCall, 'ServiceDeparture'); // Nutzt die Hilfsfunktion für den Haupt-Stop
const destination = service.getElementsByTagName('DestinationText')[0]
?.getElementsByTagName('Text')[0]?.textContent?.trim() ?? 'Unbekannt';
const calls = parseCalls(event, destination, thisCall);
const vias = calls.onward
.slice(0, 10)
.map(c => c.name)
.filter(n => n !== destination);
// Gleis und Seite für den aktuellen Halt holen
const currentQuay = getQuay(thisCall);
const alightingSide = currentQuay.side; // Nimmt die Seite vom aktuellen Halt (getQuay prüft die Attribute)
// 2. Ersatzzug (Unplanned)
const unplanned = service.getElementsByTagName('Unplanned')[0]?.textContent === 'true';
// 4. Hinweistexte verknüpfen
const sitRefs = service.getElementsByTagName('SituationFullRef');
const activeSituations = [];
for (const ref of sitRefs) {
const idNode = ref.getElementsByTagName('siri:SituationNumber')[0] || ref.getElementsByTagName('SituationNumber')[0];
const id = idNode?.textContent;
if (id && situationsMap[id]) {
if (!activeSituations.find(s => s === situationsMap[id])) {
activeSituations.push(situationsMap[id]);
}
}
}
const journeyRef = service.getElementsByTagName('JourneyRef')[0]?.textContent?.trim()
?? service.getElementsByTagName('siri:FramedVehicleJourneyRef')[0]
?.getElementsByTagName('siri:DatedVehicleJourneyRef')[0]?.textContent?.trim()
?? event.getElementsByTagName('JourneyRef')[0]?.textContent?.trim()
?? '';
const operatorRef = service.getElementsByTagName('OperatorRef')[0]?.textContent?.trim()
?? service.getElementsByTagName('siri:OperatorRef')[0]?.textContent?.trim()
?? '';
const operatorName = getText(service, 'OperatorName') ?? operatorRef;
// ─── OperatingDays Parsing ──────────────────────────────────────────────
const operatingDaysEl = event?.getElementsByTagName('OperatingDays')[0];
let operatingDays = null;
if (operatingDaysEl) {
const fromEl = operatingDaysEl.getElementsByTagName('From')[0];
const toEl = operatingDaysEl.getElementsByTagName('To')[0];
const patternEl = operatingDaysEl.getElementsByTagName('Pattern')[0];
if (fromEl && toEl && patternEl) {
operatingDays = {
from: fromEl.textContent?.trim() ?? '',
to: toEl.textContent?.trim() ?? '',
pattern: patternEl.textContent?.trim() ?? ''
};
console.log(`✓ OperatingDays: ${operatingDays.from} – ${operatingDays.to} (${operatingDays.pattern.length} Zeichen)`);
}
}
// ─── Service-Attribute extrahieren ──────────────────────────────────────
const serviceAttributes = getServiceAttributes(service);
departures.push({
stopName,
cat: getText(service, 'ShortName') ?? '??',
line: getText(service, 'PublishedServiceName') ?? '??',
journeyNumber,
unplanned,
situations: activeSituations,
alightingSide,
destination,
vias,
platform: currentQuay.text,
platformChanged: currentQuay.isChanged,
time: timeFormatted,
timetabledIso: timetabled ?? null,
estimatedIso: estimated ?? null,
delayed,
delayDisplay,
delayMinutes,
calls,
journeyRef,
operatorRef,
operatorName,
operatingDays,
serviceAttributes
});
}
return deduplicateDepartures(departures);
}
export default {
fetchDepartures,
searchStops
};