db-vendo-client/parse/arrival-or-departure.js
Jannis R 0f7382e3b8
parse{Arrival,Departure}: properly parse .origin & .destination 🐛
fixes #273

Co-Authored-By: Jürgen Bergmann <bergmannj@posteo.de>
2022-10-15 14:02:11 +02:00

86 lines
2.8 KiB
JavaScript

'use strict'
const findRemarks = require('./find-remarks')
const ARRIVAL = 'a'
const DEPARTURE = 'd'
// todo: what is d.jny.dirFlg?
// todo: d.stbStop.dProgType/d.stbStop.aProgType
// todo: d.stbStop.dProdX/aProdX can be different than d.prodX
const createParseArrOrDep = (prefix) => {
if (prefix !== ARRIVAL && prefix !== DEPARTURE) throw new Error('invalid prefix')
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 = !!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
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,
provenance: prefix === ARRIVAL && d.dirTxt && profile.parseStationName(ctx, d.dirTxt) || null,
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])
}
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})
}
if (opt.remarks) {
res.remarks = findRemarks([
...(d.remL || []),
...(d.msgL || []),
...(d.stbStop.remL || []),
...(d.stbStop.msgL || []),
]).map(([remark]) => remark)
}
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
}
return parseArrOrDep
}
module.exports = createParseArrOrDep