2017-11-20 15:43:13 +01:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const parseDateTime = require('./date-time')
|
|
|
|
|
|
|
|
const clone = obj => Object.assign({}, obj)
|
|
|
|
|
2017-12-28 16:56:27 +01:00
|
|
|
const createParseJourneyLeg = (profile, stations, lines, remarks) => {
|
2017-11-20 15:43:13 +01:00
|
|
|
// todo: finish parse/remark.js first
|
|
|
|
const applyRemark = (j, rm) => {}
|
|
|
|
|
|
|
|
// 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?
|
2017-12-28 16:56:27 +01:00
|
|
|
const parseJourneyLeg = (j, pt, passed = true) => { // j = journey, pt = part
|
2017-12-11 17:21:50 +01:00
|
|
|
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)]),
|
2017-12-11 17:21:50 +01:00
|
|
|
departure: dep.toISO(),
|
|
|
|
arrival: arr.toISO()
|
2017-11-20 15:43:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pt.dep.dTimeR && pt.dep.dTimeS) {
|
2017-12-11 17:21:50 +01:00
|
|
|
const realtime = profile.parseDateTime(profile, j.date, pt.dep.dTimeR)
|
|
|
|
const planned = profile.parseDateTime(profile, j.date, pt.dep.dTimeS)
|
2018-02-01 15:10:37 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
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) {
|
2017-12-12 03:28:54 +01:00
|
|
|
const parse = profile.parseStopover(profile, stations, lines, remarks, j)
|
2017-12-29 09:00:18 +01:00
|
|
|
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)
|
2017-11-20 15:43:13 +01:00
|
|
|
}
|
|
|
|
if (Array.isArray(pt.jny.remL)) {
|
|
|
|
for (let remark of pt.jny.remL) applyRemark(j, remark)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pt.jny.freq && pt.jny.freq.jnyL) {
|
|
|
|
const parseAlternative = (a) => {
|
2017-12-12 03:28:54 +01:00
|
|
|
const t = a.stopL[0].dTimeS || a.stopL[0].dTimeR
|
|
|
|
const when = profile.parseDateTime(profile, j.date, t)
|
2017-11-20 15:43:13 +01:00
|
|
|
return {
|
|
|
|
line: lines[parseInt(a.prodX)] || null,
|
2017-12-11 17:21:50 +01:00
|
|
|
when: when.toISO()
|
2017-11-20 15:43:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
res.alternatives = pt.jny.freq.jnyL
|
|
|
|
.filter(a => a.stopL[0].locX === pt.dep.locX)
|
|
|
|
.map(parseAlternative)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-07 20:21:14 +01:00
|
|
|
// todo: follow public-transport/friendly-public-transport-format#27 here
|
|
|
|
// see also derhuerst/vbb-rest#19
|
2017-12-18 21:11:35 +01:00
|
|
|
if (pt.arr.aCncl) {
|
|
|
|
res.cancelled = true
|
2018-03-08 03:26:39 +01:00
|
|
|
Object.defineProperty(res, 'canceled', {value: true})
|
2018-02-01 15:10:37 +01:00
|
|
|
res.arrival = res.arrivalPlatform = res.arrivalDelay = null
|
2018-03-08 03:26:39 +01:00
|
|
|
const arr = profile.parseDateTime(profile, j.date, pt.arr.aTimeS)
|
|
|
|
res.formerScheduledArrival = arr.toISO()
|
2017-12-18 21:11:35 +01:00
|
|
|
}
|
|
|
|
if (pt.dep.dCncl) {
|
|
|
|
res.cancelled = true
|
2018-03-08 03:26:39 +01:00
|
|
|
Object.defineProperty(res, 'canceled', {value: true})
|
2018-02-01 15:10:37 +01:00
|
|
|
res.departure = res.departurePlatform = res.departureDelay = null
|
2018-03-08 03:26:39 +01:00
|
|
|
const dep = profile.parseDateTime(profile, j.date, pt.dep.dTimeS)
|
|
|
|
res.formerScheduledDeparture = dep.toISO()
|
2017-12-07 20:21:14 +01:00
|
|
|
}
|
|
|
|
|
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
|