2017-11-11 22:35:41 +01:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const parseDateTime = require('./date-time')
|
|
|
|
|
2017-11-11 23:56:09 +01:00
|
|
|
const clone = obj => Object.assign({}, obj)
|
|
|
|
|
|
|
|
const createParseStopover = (tz, stations, lines, remarks, j) => { // j = journey
|
2017-11-11 22:35:41 +01:00
|
|
|
const parseStopover = (st) => {
|
|
|
|
const res = {
|
2017-11-11 23:56:09 +01:00
|
|
|
station: stations[parseInt(st.locX)]
|
2017-11-11 22:35:41 +01:00
|
|
|
}
|
|
|
|
if (st.aTimeR || st.aTimeS) {
|
2017-11-11 23:56:09 +01:00
|
|
|
const arr = parseDateTime(tz, j.date, st.aTimeR || st.aTimeS)
|
2017-11-11 22:35:41 +01:00
|
|
|
res.arrival = arr.format()
|
|
|
|
}
|
|
|
|
if (st.dTimeR || st.dTimeS) {
|
2017-11-11 23:56:09 +01:00
|
|
|
const dep = parseDateTime(tz, j.date, st.dTimeR || st.dTimeS)
|
2017-11-11 22:35:41 +01:00
|
|
|
res.departure = dep.format()
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseStopover
|
|
|
|
}
|
|
|
|
|
2017-11-11 23:56:09 +01:00
|
|
|
const createApplyRemark = (stations, lines, remarks, j) => { // j = journey
|
2017-11-11 22:35:41 +01:00
|
|
|
// todo: finish parse/remark.js first
|
|
|
|
const applyRemark = (rm) => {}
|
|
|
|
return applyRemark
|
|
|
|
}
|
|
|
|
|
2017-11-11 23:56:09 +01:00
|
|
|
const createParsePart = (tz, stations, lines, remarks, j) => { // j = journey
|
2017-11-11 22:35:41 +01:00
|
|
|
// todo: pt.sDays
|
|
|
|
// todo: pt.dep.dProgType, pt.arr.dProgType
|
|
|
|
// todo: what is pt.jny.dirFlg?
|
|
|
|
// todo: how does pt.freq work?
|
|
|
|
const parsePart = (pt) => {
|
2017-11-11 23:56:09 +01:00
|
|
|
const dep = parseDateTime(tz, j.date, pt.dep.dTimeR || pt.dep.dTimeS)
|
|
|
|
const arr = parseDateTime(tz, j.date, pt.arr.aTimeR || pt.arr.aTimeS)
|
2017-11-11 22:35:41 +01:00
|
|
|
const res = {
|
2017-11-11 23:56:09 +01:00
|
|
|
// todo: what about null?
|
|
|
|
origin: clone(stations[parseInt(pt.dep.locX)]),
|
|
|
|
destination: clone(stations[parseInt(pt.arr.locX)]),
|
|
|
|
departure: dep.format(),
|
|
|
|
arrival: arr.format()
|
2017-11-11 22:35:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pt.dep.dTimeR && pt.dep.dTimeS) {
|
2017-11-11 23:56:09 +01:00
|
|
|
const realtime = parseDateTime(tz, j.date, pt.dep.dTimeR)
|
|
|
|
const planned = parseDateTime(tz, j.date, pt.dep.dTimeS)
|
2017-11-11 22:35:41 +01:00
|
|
|
res.delay = Math.round((realtime - planned) / 1000)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pt.type === 'WALK') {
|
|
|
|
res.mode = 'walking'
|
|
|
|
} else if (pt.type === 'JNY') {
|
|
|
|
res.id = pt.jny.jid
|
2017-11-11 23:56:09 +01:00
|
|
|
res.line = lines[parseInt(pt.jny.prodX)] // todo: default null
|
2017-11-11 22:35:41 +01:00
|
|
|
res.direction = pt.jny.dirTxt // todo: parse this
|
|
|
|
|
|
|
|
if (pt.dep.dPlatfS) res.departurePlatform = pt.dep.dPlatfS
|
|
|
|
if (pt.arr.aPlatfS) res.arrivalPlatform = pt.arr.aPlatfS
|
|
|
|
|
|
|
|
if (pt.jny.stopL) {
|
2017-11-11 23:56:09 +01:00
|
|
|
const parseStopover = createParseStopover(tz, stations, lines, remarks, j)
|
|
|
|
res.passed = pt.jny.stopL.map(parseStopover)
|
2017-11-11 22:35:41 +01:00
|
|
|
}
|
|
|
|
if (Array.isArray(pt.jny.remL)) {
|
2017-11-11 23:56:09 +01:00
|
|
|
pt.jny.remL.forEach(createApplyRemark(stations, lines, remarks, j))
|
2017-11-11 22:35:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pt.jny.freq && pt.jny.freq.jnyL) {
|
|
|
|
const parseAlternative = (a) => ({
|
2017-11-11 23:56:09 +01:00
|
|
|
line: lines[parseInt(a.prodX)], // todo: default null
|
|
|
|
when: parseDateTime(tz, j.date, a.stopL[0].dTimeS).format() // todo: realtime
|
2017-11-11 22:35:41 +01:00
|
|
|
})
|
|
|
|
res.alternatives = pt.jny.freq.jnyL
|
2017-11-11 23:56:09 +01:00
|
|
|
.filter(a => a.stopL[0].locX === pt.dep.locX)
|
2017-11-11 22:35:41 +01:00
|
|
|
.map(parseAlternative)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
return parsePart
|
|
|
|
}
|
|
|
|
|
2017-11-11 23:56:09 +01:00
|
|
|
const createParseJourney = (tz, stations, lines, remarks, p = createParsePart) => {
|
2017-11-11 22:35:41 +01:00
|
|
|
// todo: c.sDays
|
|
|
|
// todo: c.dep.dProgType, c.arr.dProgType
|
|
|
|
// todo: c.conSubscr
|
|
|
|
// todo: c.trfRes x vbb-parse-ticket
|
|
|
|
// todo: use computed information from part
|
2017-11-11 23:56:09 +01:00
|
|
|
const parseJourney = (journey) => {
|
|
|
|
const parts = journey.secL.map(p(tz, stations, lines, remarks, journey))
|
2017-11-11 22:35:41 +01:00
|
|
|
return {
|
2017-11-11 23:56:09 +01:00
|
|
|
parts,
|
|
|
|
origin: parts[0].origin,
|
|
|
|
destination: parts[parts.length - 1].destination,
|
|
|
|
departure: parts[0].departure,
|
|
|
|
arrival: parts[parts.length - 1].arrival
|
2017-11-11 22:35:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseJourney
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = createParseJourney
|