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

105 lines
3.5 KiB
JavaScript
Raw Normal View History

2024-12-07 16:16:31 +00:00
import {parseRemarks, isStopCancelled} from './remarks.js';
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;
const res = {
2024-12-17 19:41:00 +00:00
origin: pt.halte?.length > 0 ? profile.parseLocation(ctx, pt.halte[0]) : locationFallback(pt.abfahrtsOrtExtId, pt.abfahrtsOrt, fallbackLocations),
destination: pt.halte?.length > 0 ? profile.parseLocation(ctx, pt.halte[pt.halte.length - 1]) : locationFallback(pt.ankunftsOrtExtId, pt.ankunftsOrt, fallbackLocations),
};
2019-06-08 12:54:59 +02:00
2024-12-07 16:16:31 +00:00
const cancelledDep = pt.halte?.length > 0 && isStopCancelled(pt.halte[0]);
const dep = profile.parseWhen(ctx, date, pt.abfahrtsZeitpunkt, pt.ezAbfahrtsZeitpunkt, cancelledDep);
res.departure = dep.when;
res.plannedDeparture = dep.plannedWhen;
res.departureDelay = dep.delay;
if (dep.prognosedWhen) {
res.prognosedDeparture = dep.prognosedWhen;
}
2024-12-08 21:42:57 +00:00
const cancelledArr = pt.halte?.length > 0 && isStopCancelled(pt.halte[pt.halte.length - 1]);
2024-12-07 16:16:31 +00:00
const arr = profile.parseWhen(ctx, date, pt.ankunftsZeitpunkt, pt.ezAnkunftsZeitpunkt, 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-07 16:16:31 +00:00
if (opt.polylines && 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
2024-12-07 16:16:31 +00:00
if (pt.verkehrsmittel?.typ === 'WALK') {
res.public = true;
res.walking = true;
2024-12-07 16:16:31 +00:00
res.distance = pt.distanz || null;
2024-12-08 21:42:57 +00:00
// TODO res.transfer, res.checkin
2024-12-07 16:16:31 +00:00
} else {
res.tripId = pt.journeyId;
res.line = profile.parseLine(ctx, pt) || null;
res.direction = pt.verkehrsmittel?.richtung || null;
// TODO res.currentLocation
if (pt.halte?.length > 0) {
2024-12-08 21:42:57 +00:00
const arrPl = profile.parsePlatform(ctx, pt.halte[pt.halte.length - 1].gleis, pt.halte[pt.halte.length - 1].ezGleis, 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
2024-12-07 16:16:31 +00:00
const depPl = profile.parsePlatform(ctx, pt.halte[0].gleis, pt.halte[0].ezGleis, cancelledDep);
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) {
res.stopovers = pt.halte.map(s => profile.parseStopover(ctx, s, date));
// 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) {
res.remarks = 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
2024-12-07 16:16:31 +00:00
if (cancelledDep || cancelledArr) {
res.cancelled = true;
Object.defineProperty(res, 'canceled', {value: true});
2017-11-20 15:43:13 +01:00
}
return res;
};
2017-11-20 15:43:13 +01:00
2022-05-07 16:17:37 +02:00
export {
parseJourneyLeg,
};