2017-11-20 15:43:13 +01:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const parseDateTime = require('./date-time')
|
|
|
|
|
|
|
|
const clone = obj => Object.assign({}, obj)
|
|
|
|
|
2018-06-13 19:59:44 +02:00
|
|
|
const createParseJourneyLeg = (profile, opt, data) => {
|
2018-06-13 18:59:56 +02:00
|
|
|
const {locations, lines, remarks, polylines} = data
|
|
|
|
|
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 = {
|
2018-06-13 18:59:56 +02:00
|
|
|
origin: clone(locations[parseInt(pt.dep.locX)]) || null,
|
|
|
|
destination: clone(locations[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
|
|
|
}
|
|
|
|
|
2018-06-07 18:52:12 +02:00
|
|
|
// todo: DRY with parseDeparture
|
|
|
|
// todo: DRY with parseStopover
|
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
|
|
|
}
|
|
|
|
|
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
|
2018-05-16 21:07:43 +02:00
|
|
|
p = Array.isArray(p) && polylines[p[0]]
|
2018-04-30 12:49:58 +02:00
|
|
|
// todo: there can be >1 polyline
|
2018-06-13 19:59:44 +02:00
|
|
|
const parse = profile.parsePolyline(profile, opt, data)
|
2018-05-16 21:07:43 +02:00
|
|
|
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
|
2018-05-13 22:52:33 +02:00
|
|
|
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) {
|
2018-06-13 19:59:44 +02:00
|
|
|
const parse = profile.parseStopover(profile, opt, data, j.date)
|
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)
|
|
|
|
}
|
|
|
|
|
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) => {
|
2018-04-29 13:41:21 +02:00
|
|
|
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)
|
2018-04-29 13:41:21 +02:00
|
|
|
// todo: expose a.stopL[0]
|
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
|
|
|
}
|
|
|
|
}
|
2018-04-29 14:29:29 +02:00
|
|
|
res.alternatives = freq.jnyL.map(parseAlternative)
|
2017-11-20 15:43:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-17 17:14:47 +01:00
|
|
|
// todo: DRY with parseDeparture
|
|
|
|
// todo: DRY with parseStopover
|
|
|
|
if (pt.arr.aCncl || pt.dep.dCncl) {
|
2017-12-18 21:11:35 +01:00
|
|
|
res.cancelled = true
|
2018-03-08 03:26:39 +01:00
|
|
|
Object.defineProperty(res, 'canceled', {value: true})
|
2018-03-17 17:14:47 +01:00
|
|
|
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-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
|