2024-02-06 22:58:49 +01:00
|
|
|
|
import {findRemarks} from './find-remarks.js';
|
2018-06-26 15:49:50 +02:00
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
|
const ARRIVAL = 'a';
|
|
|
|
|
const DEPARTURE = 'd';
|
2018-09-03 15:12:06 +02:00
|
|
|
|
|
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
|
|
|
|
|
2019-10-20 00:19:11 +02:00
|
|
|
|
const createParseArrOrDep = (prefix) => {
|
2024-02-06 22:58:49 +01:00
|
|
|
|
if (prefix !== ARRIVAL && prefix !== DEPARTURE) {
|
|
|
|
|
throw new Error('invalid prefix');
|
|
|
|
|
}
|
2018-06-26 15:49:50 +02:00
|
|
|
|
|
2019-10-20 00:19:11 +02:00
|
|
|
|
const parseArrOrDep = (ctx, d) => { // d = raw arrival/departure
|
2024-02-06 22:58:49 +01:00
|
|
|
|
const {profile, opt} = ctx;
|
|
|
|
|
const {locL} = ctx.res.common;
|
2019-10-20 00:19:11 +02:00
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
|
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,
|
2019-08-23 16:17:20 +02:00
|
|
|
|
stop: d.stbStop.location || null,
|
2019-10-20 00:19:11 +02:00
|
|
|
|
...profile.parseWhen(ctx, d.date, tPlanned, tPrognosed, tzOffset, cancelled),
|
|
|
|
|
...profile.parsePlatform(ctx, plPlanned, plPrognosed, cancelled),
|
2022-07-22 00:28:33 +02:00
|
|
|
|
prognosisType: profile.parsePrognosisType(ctx, d.stbStop[prefix + 'ProgType']) || null,
|
2018-09-03 15:12:06 +02:00
|
|
|
|
// todo: for arrivals, this is the *origin*, not the *direction*
|
2019-10-20 00:19:11 +02:00
|
|
|
|
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,
|
2022-02-21 00:52:37 +01:00
|
|
|
|
remarks: [],
|
|
|
|
|
origin: null,
|
2024-02-06 22:58:49 +01:00
|
|
|
|
destination: null,
|
|
|
|
|
};
|
2022-02-21 00:52:37 +01:00
|
|
|
|
|
2022-10-15 14:02:09 +02:00
|
|
|
|
if (prefix === DEPARTURE && Array.isArray(d.prodL) && d.prodL[0] && locL[d.prodL[0].tLocX]) {
|
2024-02-06 22:58:49 +01:00
|
|
|
|
res.destination = profile.parseLocation(ctx, locL[d.prodL[0].tLocX]);
|
2022-02-21 00:52:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-15 14:02:09 +02:00
|
|
|
|
if (prefix === ARRIVAL && Array.isArray(d.prodL) && d.prodL[0] && locL[d.prodL[0].fLocX]) {
|
2024-02-06 22:58:49 +01:00
|
|
|
|
res.origin = profile.parseLocation(ctx, locL[d.prodL[0].fLocX]);
|
2018-06-26 15:49:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 17:50:05 +02:00
|
|
|
|
if (d.pos) {
|
|
|
|
|
res.currentTripPosition = {
|
|
|
|
|
type: 'location',
|
|
|
|
|
latitude: d.pos.y / 1000000,
|
|
|
|
|
longitude: d.pos.x / 1000000,
|
2024-02-06 22:58:49 +01:00
|
|
|
|
};
|
2021-10-12 17:50:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
add planned(Arrival|Departure|When), scheduled* -> planned*/prognosed* :boom:
when not cancelled:
{when, delay} -> {when, plannedWhen, delay}
{arrival, arrivalDelay} -> {arrival, plannedArrival, arrivalDelay}
{departure, departureDelay} -> {departure, plannedDeparture, departureDelay}
when cancelled:
{when: null, delay: null, scheduledWhen} -> {when: null, plannedWhen, prognosedWhen, delay}
{arrival: null, arrivalDelay: null, scheduledArrival, formerArrivalDelay} -> {arrival: null, plannedArrival, arrivalDelay, prognosedArrival}
{departure: null, departureDelay: null, scheduledDeparture, formerDepartureDelay} -> {departure: null, plannedDeparture, departureDelay, prognosedDeparture}
2019-08-23 18:51:03 +02:00
|
|
|
|
if (cancelled) {
|
2024-02-06 22:58:49 +01:00
|
|
|
|
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) {
|
2019-08-23 16:21:44 +02:00
|
|
|
|
res.remarks = findRemarks([
|
2024-02-06 22:58:49 +01:00
|
|
|
|
...d.remL || [],
|
|
|
|
|
...d.msgL || [],
|
|
|
|
|
...d.stbStop.remL || [],
|
|
|
|
|
...d.stbStop.msgL || [],
|
|
|
|
|
])
|
|
|
|
|
.map(([remark]) => remark);
|
2018-06-28 13:45:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-27 15:57:54 +01: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
|
2024-02-06 22:58:49 +01:00
|
|
|
|
.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;
|
|
|
|
|
}
|
2020-02-27 15:57:54 +01:00
|
|
|
|
}
|
2018-12-27 20:17:23 +01:00
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
|
return res;
|
|
|
|
|
};
|
2018-06-26 15:49:50 +02:00
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
|
return parseArrOrDep;
|
|
|
|
|
};
|
2018-06-26 15:49:50 +02:00
|
|
|
|
|
2022-05-07 16:17:37 +02:00
|
|
|
|
export {
|
|
|
|
|
createParseArrOrDep,
|
2024-02-06 22:58:49 +01:00
|
|
|
|
};
|