db-vendo-client/parse/journey-leg.js

134 lines
4.9 KiB
JavaScript
Raw Normal View History

2024-12-17 19:41:00 +00:00
const locationFallback = (id, name, fallbackLocations) => {
if (fallbackLocations && (id && fallbackLocations[id] || name && fallbackLocations[name])) {
return fallbackLocations[id] || fallbackLocations[name];
}
2024-12-07 16:16:31 +00:00
return {
2024-12-17 19:41:00 +00:00
type: 'location',
2024-12-07 16:16:31 +00:00
id: id,
name: name,
2024-12-08 21:42:57 +00:00
location: null,
};
};
2017-11-20 15:43:13 +01:00
2024-12-17 19:41:00 +00:00
const parseJourneyLeg = (ctx, pt, date, fallbackLocations) => { // pt = raw leg
const {profile, opt} = ctx;
2025-01-14 18:13:58 +00:00
const stops = pt.halte?.length && pt.halte || pt.stops?.length && pt.stops || [];
const res = {
origin: stops.length && profile.parseLocation(ctx, stops[0].ort || stops[0].station || stops[0])
2025-02-16 23:48:29 +01:00
|| pt.abgangsOrt?.name && profile.parseLocation(ctx, pt.abgangsOrt)
|| locationFallback(pt.abfahrtsOrtExtId, pt.abfahrtsOrt, fallbackLocations),
destination: stops.length && profile.parseLocation(ctx, stops[stops.length - 1].ort || stops[stops.length - 1].station || stops[stops.length - 1])
2025-02-16 23:48:29 +01:00
|| pt.ankunftsOrt?.name && profile.parseLocation(ctx, pt.ankunftsOrt)
|| locationFallback(pt.ankunftsOrtExtId, pt.ankunftsOrt, fallbackLocations),
};
2019-06-08 12:54:59 +02:00
2025-01-14 18:13:58 +00:00
const cancelledDep = stops.length && profile.parseCancelled(stops[0]);
const dep = profile.parseWhen(ctx, date, pt.abfahrtsZeitpunkt || pt.abgangsDatum || stops.length && (stops[0].abgangsDatum || stops[0].departureTime?.target), pt.ezAbfahrtsZeitpunkt || pt.ezAbgangsDatum || stops.length && (stops[0].ezAbgangsDatum || stops[0].departureTime?.timeType != 'SCHEDULE' && stops[0].departureTime?.predicted), cancelledDep,
);
res.departure = dep.when;
res.plannedDeparture = dep.plannedWhen;
res.departureDelay = dep.delay;
if (dep.prognosedWhen) {
res.prognosedDeparture = dep.prognosedWhen;
}
2025-01-14 18:13:58 +00:00
const cancelledArr = stops.length && profile.parseCancelled(stops[stops.length - 1]);
const arr = profile.parseWhen(ctx, date, pt.ankunftsZeitpunkt || pt.ankunftsDatum || stops.length && (stops[stops.length - 1].ankunftsDatum || stops[stops.length - 1].arrivalTime?.target), pt.ezAnkunftsZeitpunkt || pt.ezAnkunftsDatum || stops.length && (stops[stops.length - 1].ezAnkunftsDatum || stops[stops.length - 1].arrivalTime?.timeType != 'SCHEDULE' && stops[stops.length - 1].arrivalTime?.predicted), cancelledArr,
);
res.arrival = arr.when;
res.plannedArrival = arr.plannedWhen;
res.arrivalDelay = arr.delay;
if (arr.prognosedWhen) {
res.prognosedArrival = arr.prognosedWhen;
}
2020-09-10 23:57:03 +02:00
2024-12-07 16:16:31 +00:00
/* TODO res.reachable risNotizen [
{
"key": "text.realtime.connection.brokentrip",
"value": "Due to delays a connecting service may not be reachable."
}
] */
2017-11-20 15:43:13 +01:00
2024-12-18 01:16:57 +00:00
if ((opt.polylines || opt.polyline) && pt.polylineGroup) {
2024-12-17 19:41:00 +00:00
res.polyline = profile.parsePolyline(ctx, pt.polylineGroup); // TODO polylines not returned anymore, set "poly": true in request, apparently only works for /reiseloesung/verbindung
}
2018-04-30 12:49:58 +02:00
2025-01-03 10:57:24 +00:00
const type = pt.verkehrsmittel?.typ || pt.typ;
if (type == 'WALK' || type == 'FUSSWEG' || type == 'TRANSFER') {
2025-01-03 10:57:24 +00:00
if (res.origin?.id == res.destination?.id) {
res.arrival = res.departure;
res.plannedArrival = res.plannedDeparture;
res.arrivalDelay = res.departureDelay;
}
res.public = true;
res.walking = true;
2024-12-07 16:16:31 +00:00
res.distance = pt.distanz || null;
2025-01-03 10:57:24 +00:00
if (type == 'TRANSFER') {
res.transfer = true;
}
// TODO res.checkin
2024-12-07 16:16:31 +00:00
} else {
2025-01-03 10:57:24 +00:00
res.tripId = pt.journeyId || pt.zuglaufId;
2024-12-07 16:16:31 +00:00
res.line = profile.parseLine(ctx, pt) || null;
2025-01-03 10:57:24 +00:00
res.direction = pt.verkehrsmittel?.richtung || pt.richtung || null;
2024-12-07 16:16:31 +00:00
// TODO res.currentLocation
2025-01-14 18:13:58 +00:00
// TODO trainStartDate?
2025-01-14 18:13:58 +00:00
if (stops.length) {
const arrPl = profile.parsePlatform(ctx,
stops[stops.length - 1].gleis || stops[stops.length - 1].track?.target,
stops[stops.length - 1].ezGleis || stops[stops.length - 1].track?.prediction,
cancelledArr,
);
2024-12-07 16:16:31 +00:00
res.arrivalPlatform = arrPl.platform;
res.plannedArrivalPlatform = arrPl.plannedPlatform;
if (arrPl.prognosedPlatform) {
res.prognosedArrivalPlatform = arrPl.prognosedPlatform;
}
2024-12-08 21:42:57 +00:00
// res.arrivalPrognosisType = null; // TODO
const depPl = profile.parsePlatform(ctx,
stops[0].gleis || stops[0].track?.target,
stops[0].ezGleis || stops[0].track?.prediction,
cancelledDep,
);
2024-12-07 16:16:31 +00:00
res.departurePlatform = depPl.platform;
res.plannedDeparturePlatform = depPl.plannedPlatform;
if (depPl.prognosedPlatform) {
res.prognosedDeparturePlatform = depPl.prognosedPlatform;
}
2024-12-08 21:42:57 +00:00
// res.departurePrognosisType = null; // TODO
2024-12-07 16:16:31 +00:00
if (opt.stopovers) {
2025-01-14 18:13:58 +00:00
res.stopovers = stops.map(s => profile.parseStopover(ctx, s, date));
2024-12-07 16:16:31 +00:00
// filter stations the train passes without stopping, as this doesn't comply with fptf (yet)
res.stopovers = res.stopovers.filter((x) => !x.passBy);
2024-12-08 21:42:57 +00:00
}
2024-12-07 16:16:31 +00:00
if (opt.remarks) {
2025-01-02 14:00:45 +00:00
res.remarks = profile.parseRemarks(ctx, pt);
}
}
2018-12-02 01:05:01 +01:00
2024-12-07 16:16:31 +00:00
// TODO cycle, alternatives
}
2017-11-20 15:43:13 +01:00
2025-01-03 10:57:24 +00:00
if (cancelledDep || cancelledArr || pt.cancelled || pt.canceled) {
res.cancelled = true;
Object.defineProperty(res, 'canceled', {value: true});
2017-11-20 15:43:13 +01:00
}
2025-01-03 10:57:24 +00:00
const load = profile.parseLoadFactor(opt, pt.auslastungsmeldungen || pt.auslastungsInfos);
2024-12-21 15:26:49 +00:00
if (load) {
res.loadFactor = load;
}
return res;
};
2017-11-20 15:43:13 +01:00
2022-05-07 16:17:37 +02:00
export {
parseJourneyLeg,
};