db-vendo-client/parse/journey-leg.js

135 lines
4.2 KiB
JavaScript
Raw Normal View History

2017-11-20 15:43:13 +01:00
'use strict'
const parseDateTime = require('./date-time')
2018-06-07 16:38:54 +02:00
const findRemark = require('./find-remark')
2017-11-20 15:43:13 +01:00
const clone = obj => Object.assign({}, obj)
const applyRemarks = (leg, hints, warnings, refs) => {
2018-06-07 16:38:54 +02:00
for (let ref of refs) {
const remark = findRemark(hints, warnings, ref)
if ('number' === typeof ref.fLocX && 'number' === typeof ref.tLocX) {
for (let i = ref.fLocX; i <= ref.tLocX; i++) {
const stopover = leg.passed[i]
if (Array.isArray(stopover.remarks)) {
stopover.remarks.push(remark)
} else {
stopover.remarks = [remark]
}
2018-06-07 16:38:54 +02:00
}
} else {
if (Array.isArray(leg.remarks)) leg.remarks.push(remark)
else leg.remarks = [remark]
2018-06-07 16:38:54 +02:00
}
// todo: `ref.tagL`
}
}
const createParseJourneyLeg = (profile, stations, lines, hints, warnings, polylines) => {
2018-06-11 19:50:44 +02:00
// todo: pt.status
2017-11-20 15:43:13 +01:00
// todo: pt.sDays
// todo: pt.dep.dProgType, pt.arr.dProgType
// todo: what is pt.jny.dirFlg?
// todo: how does pt.freq work?
2017-12-12 03:28:54 +01:00
// todo: what is pt.himL?
2018-06-11 19:50:44 +02:00
// todo: pt.planrtTS
2017-12-28 16:56:27 +01:00
const parseJourneyLeg = (j, pt, passed = true) => { // j = journey, pt = part
const dep = profile.parseDateTime(profile, j.date, pt.dep.dTimeR || pt.dep.dTimeS)
const arr = profile.parseDateTime(profile, j.date, pt.arr.aTimeR || pt.arr.aTimeS)
2017-11-20 15:43:13 +01:00
const res = {
origin: clone(stations[parseInt(pt.dep.locX)]) || null,
destination: clone(stations[parseInt(pt.arr.locX)]),
departure: dep.toISO(),
arrival: arr.toISO()
2017-11-20 15:43:13 +01:00
}
if (pt.dep.dTimeR && pt.dep.dTimeS) {
const realtime = profile.parseDateTime(profile, j.date, pt.dep.dTimeR)
const planned = profile.parseDateTime(profile, j.date, pt.dep.dTimeS)
res.departureDelay = Math.round((realtime - planned) / 1000)
}
if (pt.arr.aTimeR && pt.arr.aTimeS) {
const realtime = profile.parseDateTime(profile, j.date, pt.arr.aTimeR)
const planned = profile.parseDateTime(profile, j.date, pt.arr.aTimeS)
res.arrivalDelay = Math.round((realtime - planned) / 1000)
2017-11-20 15:43:13 +01:00
}
2018-04-30 12:49:58 +02:00
if (pt.jny && pt.jny.polyG) {
2018-04-30 13:14:19 +02:00
let p = pt.jny.polyG.polyXL
p = Array.isArray(p) && polylines[p[0]]
2018-04-30 12:49:58 +02:00
// todo: there can be >1 polyline
const parse = profile.parsePolyline(stations)
res.polyline = p && parse(p) || null
2018-04-30 12:49:58 +02:00
}
2017-11-20 15:43:13 +01:00
if (pt.type === 'WALK') {
res.mode = 'walking'
2017-12-12 03:28:54 +01:00
res.public = true
2017-11-20 15:43:13 +01:00
} else if (pt.type === 'JNY') {
2017-12-12 03:28:54 +01:00
// todo: pull `public` value from `profile.products`
2017-11-20 15:43:13 +01:00
res.id = pt.jny.jid
res.line = lines[parseInt(pt.jny.prodX)] || null
res.direction = profile.parseStationName(pt.jny.dirTxt) || null
2017-11-20 15:43:13 +01:00
if (pt.dep.dPlatfS) res.departurePlatform = pt.dep.dPlatfS
if (pt.arr.aPlatfS) res.arrivalPlatform = pt.arr.aPlatfS
2017-12-17 20:33:04 +01:00
if (passed && pt.jny.stopL) {
const parse = profile.parseStopover(profile, stations, lines, hints, warnings, j.date)
const passedStations = pt.jny.stopL.map(parse)
// filter stations the train passes without stopping, as this doesn't comply with fptf (yet)
res.passed = passedStations.filter((x) => !x.passBy)
// todo: is there a `pt.jny.remL`?
if (Array.isArray(pt.jny.msgL)) {
applyRemarks(res, hints, warnings, pt.jny.msgL)
2018-06-07 16:38:54 +02:00
}
2017-11-20 15:43:13 +01:00
}
2018-04-29 14:29:29 +02:00
const freq = pt.jny.freq || {}
if (freq.minC && freq.maxC) {
// todo: what is freq.numC?
res.cycle = {
min: freq.minC * 60,
max: freq.maxC * 60
}
}
if (freq.jnyL) {
2017-11-20 15:43:13 +01:00
const parseAlternative = (a) => {
const t = a.stopL[0].dTimeR || a.stopL[0].dTimeS
2017-12-12 03:28:54 +01:00
const when = profile.parseDateTime(profile, j.date, t)
// todo: expose a.stopL[0]
2017-11-20 15:43:13 +01:00
return {
line: lines[parseInt(a.prodX)] || null,
when: when.toISO()
2017-11-20 15:43:13 +01:00
}
}
2018-04-29 14:29:29 +02:00
res.alternatives = freq.jnyL.map(parseAlternative)
2017-11-20 15:43:13 +01:00
}
}
// todo: DRY with parseDeparture
// todo: DRY with parseStopover
if (pt.arr.aCncl || pt.dep.dCncl) {
res.cancelled = true
Object.defineProperty(res, 'canceled', {value: true})
if (pt.arr.aCncl) {
res.arrival = res.arrivalPlatform = res.arrivalDelay = null
const arr = profile.parseDateTime(profile, j.date, pt.arr.aTimeS)
res.formerScheduledArrival = arr.toISO()
}
if (pt.dep.dCncl) {
res.departure = res.departurePlatform = res.departureDelay = null
const dep = profile.parseDateTime(profile, j.date, pt.dep.dTimeS)
res.formerScheduledDeparture = dep.toISO()
}
}
2017-11-20 15:43:13 +01:00
return res
}
2017-12-28 16:56:27 +01:00
return parseJourneyLeg
2017-11-20 15:43:13 +01:00
}
2017-12-28 16:56:27 +01:00
module.exports = createParseJourneyLeg