2018-06-26 15:49:50 +02:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const findRemark = require('./find-remark')
|
|
|
|
|
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
|
|
|
|
|
|
|
|
const createParseArrOrDep = (profile, opt, data, prefix) => {
|
2019-08-23 16:17:20 +02:00
|
|
|
const {hints, warnings} = data
|
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
|
|
|
|
|
|
|
const parseArrOrDep = (d) => {
|
|
|
|
const t = d.stbStop[prefix + 'TimeR'] || d.stbStop[prefix + 'TimeS']
|
2018-10-15 16:21:29 +02:00
|
|
|
const tz = d.stbStop[prefix + 'TZOffset'] || 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,
|
2018-10-15 16:21:29 +02:00
|
|
|
when: profile.parseDateTime(profile, d.date, t, tz),
|
2018-09-03 15:12:06 +02:00
|
|
|
// todo: for arrivals, this is the *origin*, not the *direction*
|
2019-04-01 19:22:10 +02:00
|
|
|
direction: prefix === DEPARTURE && d.dirTxt && profile.parseStationName(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
|
|
|
}
|
|
|
|
|
|
|
|
// todo: DRY with parseStopover
|
|
|
|
// todo: DRY with parseJourneyLeg
|
|
|
|
const tR = d.stbStop[prefix + 'TimeR']
|
|
|
|
const tP = d.stbStop[prefix + 'TimeS']
|
|
|
|
if (tR && tP) {
|
2018-10-15 16:21:29 +02:00
|
|
|
const realtime = profile.parseDateTime(profile, d.date, tR, tz, true)
|
|
|
|
const planned = profile.parseDateTime(profile, d.date, tP, tz, true)
|
2018-06-26 15:49:50 +02:00
|
|
|
res.delay = Math.round((realtime - planned) / 1000)
|
|
|
|
} else res.delay = null
|
|
|
|
|
2018-06-27 17:17:04 +02:00
|
|
|
// todo: DRY with parseStopover
|
|
|
|
// todo: DRY with parseJourneyLeg
|
|
|
|
const pR = d.stbStop[prefix + 'PlatfR']
|
|
|
|
const pP = d.stbStop[prefix + 'PlatfS']
|
|
|
|
res.platform = pR || pP || null
|
2019-02-14 14:04:31 +01:00
|
|
|
if (pR && pP && pR !== pP) res.scheduledPlatform = pP
|
2018-06-27 17:17:04 +02:00
|
|
|
|
2018-06-26 15:49:50 +02:00
|
|
|
// todo: DRY with parseStopover
|
|
|
|
// todo: DRY with parseJourneyLeg
|
|
|
|
if (d.stbStop[prefix + 'Cncl']) {
|
|
|
|
res.cancelled = true
|
|
|
|
Object.defineProperty(res, 'canceled', {value: true})
|
|
|
|
res.when = res.delay = null
|
2019-02-14 14:04:31 +01:00
|
|
|
res.scheduledWhen = profile.parseDateTime(profile, d.date, tP, tz)
|
2018-06-26 15:49:50 +02:00
|
|
|
}
|
|
|
|
|
2018-06-28 13:45:56 +02:00
|
|
|
if (opt.remarks) {
|
|
|
|
res.remarks = []
|
|
|
|
.concat(d.remL || [], d.msgL || [])
|
|
|
|
.map(ref => findRemark(hints, warnings, ref))
|
|
|
|
.filter(rem => !!rem) // filter unparsable
|
|
|
|
}
|
|
|
|
|
2019-10-28 17:43:12 +01:00
|
|
|
if (opt.stopovers && Array.isArray(d.stopL)) {
|
2018-12-27 20:17:23 +01:00
|
|
|
const parse = profile.parseStopover(profile, opt, data, d.date)
|
|
|
|
// Filter stations the train passes without stopping, as this doesn't comply with FPTF (yet).
|
|
|
|
const stopovers = d.stopL.map(parse).filter(st => !st.passBy)
|
|
|
|
if (prefix === ARRIVAL) res.previousStopovers = stopovers
|
|
|
|
else if (prefix === DEPARTURE) res.nextStopovers = stopovers
|
|
|
|
}
|
|
|
|
|
2018-06-26 15:49:50 +02:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseArrOrDep
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = createParseArrOrDep
|