2018-06-26 15:49:50 +02:00
|
|
|
'use strict'
|
|
|
|
|
2019-08-23 16:21:44 +02:00
|
|
|
const findRemarks = require('./find-remarks')
|
2018-06-26 15:49:50 +02:00
|
|
|
|
2018-09-03 15:12:06 +02:00
|
|
|
const ARRIVAL = 'a'
|
|
|
|
const DEPARTURE = 'd'
|
|
|
|
|
2018-06-26 15:49:50 +02:00
|
|
|
// todo: what is d.jny.dirFlg?
|
|
|
|
// todo: d.stbStop.dProgType/d.stbStop.aProgType
|
|
|
|
|
2019-10-20 00:19:11 +02:00
|
|
|
const createParseArrOrDep = (prefix) => {
|
2018-09-03 15:12:06 +02: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
|
|
|
|
const {profile, opt} = ctx
|
|
|
|
|
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
|
|
|
const tPlanned = d.stbStop[prefix + 'TimeS']
|
|
|
|
const tPrognosed = d.stbStop[prefix + 'TimeR']
|
|
|
|
const tzOffset = d.stbStop[prefix + 'TZOffset'] || null
|
|
|
|
const cancelled = !!d.stbStop[prefix + 'Cncl']
|
2019-10-20 00:19:11 +02:00
|
|
|
const plPlanned = d.stbStop[prefix + 'PlatfS']
|
|
|
|
const plPrognosed = d.stbStop[prefix + 'PlatfR']
|
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),
|
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,
|
2019-08-23 16:06:21 +02:00
|
|
|
line: d.line || null,
|
2019-02-01 18:01:51 +01:00
|
|
|
remarks: []
|
2018-06-26 15:49:50 +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) {
|
2018-06-26 15:49:50 +02:00
|
|
|
res.cancelled = true
|
|
|
|
Object.defineProperty(res, 'canceled', {value: true})
|
|
|
|
}
|
|
|
|
|
2018-06-28 13:45:56 +02:00
|
|
|
if (opt.remarks) {
|
2019-08-23 16:21:44 +02:00
|
|
|
res.remarks = findRemarks([
|
|
|
|
...(d.remL || []),
|
|
|
|
...(d.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
|
|
|
|
.map(st => profile.parseStopover(ctx, st, d.date))
|
|
|
|
.filter(st => !st.passBy)
|
|
|
|
if (prefix === ARRIVAL) res.previousStopovers = stopovers
|
2018-12-27 20:17:23 +01:00
|
|
|
else if (prefix === DEPARTURE) res.nextStopovers = stopovers
|
2020-02-27 15:57:54 +01:00
|
|
|
}
|
2018-12-27 20:17:23 +01:00
|
|
|
|
2018-06-26 15:49:50 +02:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseArrOrDep
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = createParseArrOrDep
|