db-vendo-client/parse/arrival-or-departure.js

93 lines
3.1 KiB
JavaScript
Raw Normal View History

import {findRemarks} from './find-remarks.js';
2018-06-26 15:49:50 +02:00
const ARRIVAL = 'a';
const DEPARTURE = 'd';
2022-10-24 17:29:23 +02:00
// todo: pt.jny.dirFlg https://github.com/alexander-albers/tripkit/blob/07047c6ddef24339ebd49a86a78158bca8047421/Sources/TripKit/Provider/AbstractHafasClientInterfaceProvider.swift#L347-L353 & https://github.com/alexander-albers/tripkit/commit/07047c6ddef24339ebd49a86a78158bca8047421#commitcomment-68471656
2018-06-26 15:49:50 +02:00
// todo: d.stbStop.dProgType/d.stbStop.aProgType
2020-03-18 21:35:43 +01:00
// todo: d.stbStop.dProdX/aProdX can be different than d.prodX
2018-06-26 15:49:50 +02:00
const createParseArrOrDep = (prefix) => {
if (prefix !== ARRIVAL && prefix !== DEPARTURE) {
throw new Error('invalid prefix');
}
2018-06-26 15:49:50 +02:00
const parseArrOrDep = (ctx, d) => { // d = raw arrival/departure
const {profile, opt} = ctx;
const {locL} = ctx.res.common;
const tPlanned = d.stbStop[prefix + 'TimeS'];
const tPrognosed = d.stbStop[prefix + 'TimeR'];
const tzOffset = d.stbStop[prefix + 'TZOffset'] || null;
const cancelled = Boolean(d.stbStop[prefix + 'Cncl']);
const plPlanned = d.stbStop[prefix + 'PlatfS'] || d.stbStop[prefix + 'PltfS'] && d.stbStop[prefix + 'PltfS'].txt || null;
const plPrognosed = d.stbStop[prefix + 'PlatfR'] || d.stbStop[prefix + 'PltfR'] && d.stbStop[prefix + 'PltfR'].txt || null;
2018-06-26 15:49:50 +02:00
const res = {
tripId: d.jid,
stop: d.stbStop.location || null,
...profile.parseWhen(ctx, d.date, tPlanned, tPrognosed, tzOffset, cancelled),
...profile.parsePlatform(ctx, plPlanned, plPrognosed, cancelled),
prognosisType: profile.parsePrognosisType(ctx, d.stbStop[prefix + 'ProgType']) || null,
// todo: for arrivals, this is the *origin*, not the *direction*
direction: prefix === DEPARTURE && d.dirTxt && profile.parseStationName(ctx, d.dirTxt) || null,
2020-06-09 12:46:13 +02:00
provenance: prefix === ARRIVAL && d.dirTxt && profile.parseStationName(ctx, d.dirTxt) || null,
2019-08-23 16:06:21 +02:00
line: d.line || null,
remarks: [],
origin: null,
destination: null,
};
if (prefix === DEPARTURE && Array.isArray(d.prodL) && d.prodL[0] && locL[d.prodL[0].tLocX]) {
res.destination = profile.parseLocation(ctx, locL[d.prodL[0].tLocX]);
}
if (prefix === ARRIVAL && Array.isArray(d.prodL) && d.prodL[0] && locL[d.prodL[0].fLocX]) {
res.origin = profile.parseLocation(ctx, locL[d.prodL[0].fLocX]);
2018-06-26 15:49:50 +02:00
}
if (d.pos) {
res.currentTripPosition = {
type: 'location',
latitude: d.pos.y / 1000000,
longitude: d.pos.x / 1000000,
};
}
if (cancelled) {
res.cancelled = true;
Object.defineProperty(res, 'canceled', {value: true});
2018-06-26 15:49:50 +02:00
}
2018-06-28 13:45:56 +02:00
if (opt.remarks) {
res.remarks = findRemarks([
...d.remL || [],
...d.msgL || [],
...d.stbStop.remL || [],
...d.stbStop.msgL || [],
])
.map(([remark]) => remark);
2018-06-28 13:45:56 +02:00
}
if (opt.stopovers && Array.isArray(d.stopL)) {
// Filter stations the train passes without stopping, as this doesn't comply with FPTF (yet).
const stopovers = d.stopL
.map(st => profile.parseStopover(ctx, st, d.date))
.filter(st => !st.passBy);
if (prefix === ARRIVAL) {
res.previousStopovers = stopovers;
} else if (prefix === DEPARTURE) {
res.nextStopovers = stopovers;
}
}
return res;
};
2018-06-26 15:49:50 +02:00
return parseArrOrDep;
};
2018-06-26 15:49:50 +02:00
2022-05-07 16:17:37 +02:00
export {
createParseArrOrDep,
};