db-vendo-client/parse/journey.js

109 lines
3.1 KiB
JavaScript
Raw Normal View History

2017-11-11 22:35:41 +01:00
'use strict'
const parseDateTime = require('./date-time')
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 = {
station: stations[parseInt(st.locX)]
2017-11-11 22:35:41 +01:00
}
if (st.aTimeR || st.aTimeS) {
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) {
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
}
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
}
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) => {
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 = {
// 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) {
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
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) {
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)) {
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) => ({
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
.filter(a => a.stopL[0].locX === pt.dep.locX)
2017-11-11 22:35:41 +01:00
.map(parseAlternative)
}
}
return res
}
return parsePart
}
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
const parseJourney = (journey) => {
const parts = journey.secL.map(p(tz, stations, lines, remarks, journey))
2017-11-11 22:35:41 +01:00
return {
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