mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 07:09:35 +02:00
parse arrivals
This commit is contained in:
parent
00ea10d9c8
commit
1d5f4ff31c
6 changed files with 82 additions and 49 deletions
10
index.js
10
index.js
|
@ -22,11 +22,15 @@ const createClient = (profile, request = _request) => {
|
||||||
}
|
}
|
||||||
validateProfile(profile)
|
validateProfile(profile)
|
||||||
|
|
||||||
const departures = (station, opt = {}) => {
|
const _stationBoard = (station, type, opt = {}) => {
|
||||||
if (isObj(station)) station = profile.formatStation(station.id)
|
if (isObj(station)) station = profile.formatStation(station.id)
|
||||||
else if ('string' === typeof station) station = profile.formatStation(station)
|
else if ('string' === typeof station) station = profile.formatStation(station)
|
||||||
else throw new Error('station must be an object or a string.')
|
else throw new Error('station must be an object or a string.')
|
||||||
|
|
||||||
|
if ('string' !== typeof type || !type) {
|
||||||
|
throw new Error('type must be a non-empty string.')
|
||||||
|
}
|
||||||
|
|
||||||
opt = Object.assign({
|
opt = Object.assign({
|
||||||
direction: null, // only show departures heading to this station
|
direction: null, // only show departures heading to this station
|
||||||
duration: 10 // show departures for the next n minutes
|
duration: 10 // show departures for the next n minutes
|
||||||
|
@ -49,6 +53,10 @@ const createClient = (profile, request = _request) => {
|
||||||
getPasslist: false
|
getPasslist: false
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const departures = (station, opt = {}) => {
|
||||||
|
return _stationBoard(station, 'DEP', opt)
|
||||||
.then((d) => {
|
.then((d) => {
|
||||||
if (!Array.isArray(d.jnyL)) return [] // todo: throw err?
|
if (!Array.isArray(d.jnyL)) return [] // todo: throw err?
|
||||||
const parse = profile.parseDeparture(profile, opt, {
|
const parse = profile.parseDeparture(profile, opt, {
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
const parseDateTime = require('../parse/date-time')
|
const parseDateTime = require('../parse/date-time')
|
||||||
const parseDeparture = require('../parse/departure')
|
const parseDeparture = require('../parse/departure')
|
||||||
|
const parseArrival = require('../parse/arrival')
|
||||||
const parseJourneyLeg = require('../parse/journey-leg')
|
const parseJourneyLeg = require('../parse/journey-leg')
|
||||||
const parseJourney = require('../parse/journey')
|
const parseJourney = require('../parse/journey')
|
||||||
const parseLine = require('../parse/line')
|
const parseLine = require('../parse/line')
|
||||||
|
@ -39,6 +40,7 @@ const defaultProfile = {
|
||||||
|
|
||||||
parseDateTime,
|
parseDateTime,
|
||||||
parseDeparture,
|
parseDeparture,
|
||||||
|
parseArrival,
|
||||||
parseJourneyLeg,
|
parseJourneyLeg,
|
||||||
parseJourney,
|
parseJourney,
|
||||||
parseLine,
|
parseLine,
|
||||||
|
|
|
@ -11,6 +11,7 @@ const types = {
|
||||||
|
|
||||||
parseDateTime: 'function',
|
parseDateTime: 'function',
|
||||||
parseDeparture: 'function',
|
parseDeparture: 'function',
|
||||||
|
parseArrival: 'function',
|
||||||
parseJourneyLeg: 'function',
|
parseJourneyLeg: 'function',
|
||||||
parseJourney: 'function',
|
parseJourney: 'function',
|
||||||
parseLine: 'function',
|
parseLine: 'function',
|
||||||
|
|
57
parse/arrival-or-departure.js
Normal file
57
parse/arrival-or-departure.js
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const findRemark = require('./find-remark')
|
||||||
|
|
||||||
|
// todo: what is d.jny.dirFlg?
|
||||||
|
// todo: d.stbStop.dProgType/d.stbStop.aProgType
|
||||||
|
|
||||||
|
const createParseArrOrDep = (profile, opt, data, prefix) => {
|
||||||
|
const {locations, lines, hints, warnings} = data
|
||||||
|
if (prefix !== 'a' && prefix !== 'd') throw new Error('invalid prefix')
|
||||||
|
|
||||||
|
const parseArrOrDep = (d) => {
|
||||||
|
const t = d.stbStop[prefix + 'TimeR'] || d.stbStop[prefix + 'TimeS']
|
||||||
|
const when = profile.parseDateTime(profile, d.date, t)
|
||||||
|
|
||||||
|
const res = {
|
||||||
|
tripId: d.jid,
|
||||||
|
station: locations[parseInt(d.stbStop.locX)] || null,
|
||||||
|
when: when.toISO(),
|
||||||
|
direction: profile.parseStationName(d.dirTxt),
|
||||||
|
line: lines[parseInt(d.prodX)] || null,
|
||||||
|
remarks: (d.remL
|
||||||
|
? d.remL.map(ref => findRemark(hints, warnings, ref))
|
||||||
|
: []
|
||||||
|
),
|
||||||
|
// todo: res.trip from rawLine.prodCtx.num?
|
||||||
|
trip: +d.jid.split('|')[1] // todo: this seems brittle
|
||||||
|
}
|
||||||
|
|
||||||
|
// todo: DRY with parseStopover
|
||||||
|
// todo: DRY with parseJourneyLeg
|
||||||
|
const tR = d.stbStop[prefix + 'TimeR']
|
||||||
|
const tP = d.stbStop[prefix + 'TimeS']
|
||||||
|
if (tR && tP) {
|
||||||
|
const realtime = profile.parseDateTime(profile, d.date, tR)
|
||||||
|
const planned = profile.parseDateTime(profile, d.date, tP)
|
||||||
|
res.delay = Math.round((realtime - planned) / 1000)
|
||||||
|
} else res.delay = null
|
||||||
|
|
||||||
|
// todo: DRY with parseStopover
|
||||||
|
// todo: DRY with parseJourneyLeg
|
||||||
|
if (d.stbStop[prefix + 'Cncl']) {
|
||||||
|
res.cancelled = true
|
||||||
|
Object.defineProperty(res, 'canceled', {value: true})
|
||||||
|
res.when = res.delay = null
|
||||||
|
|
||||||
|
const when = profile.parseDateTime(profile, d.date, tP)
|
||||||
|
res.formerScheduledWhen = when.toISO()
|
||||||
|
}
|
||||||
|
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
return parseArrOrDep
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = createParseArrOrDep
|
10
parse/arrival.js
Normal file
10
parse/arrival.js
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const createParseArrOrDep = require('./arrival-or-departure')
|
||||||
|
|
||||||
|
const ARRIVAL = 'a'
|
||||||
|
const createParseArrival = (profile, opt, data) => {
|
||||||
|
return createParseArrOrDep(profile, opt, data, ARRIVAL)
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = createParseArrival
|
|
@ -1,55 +1,10 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const findRemark = require('./find-remark')
|
const createParseArrOrDep = require('./arrival-or-departure')
|
||||||
|
|
||||||
// todos from public-transport/hafas-client#2
|
|
||||||
// - stdStop.dPlatfS, stdStop.dPlatfR
|
|
||||||
// todo: what is d.jny.dirFlg?
|
|
||||||
// todo: d.stbStop.dProgType
|
|
||||||
// todo: d.freq, d.freq.jnyL, see https://github.com/public-transport/hafas-client/blob/9203ed1481f08baacca41ac5e3c19bf022f01b0b/parse.js#L115
|
|
||||||
|
|
||||||
|
const DEPARTURE = 'd'
|
||||||
const createParseDeparture = (profile, opt, data) => {
|
const createParseDeparture = (profile, opt, data) => {
|
||||||
const {locations, lines, hints, warnings} = data
|
return createParseArrOrDep(profile, opt, data, DEPARTURE)
|
||||||
|
|
||||||
const parseDeparture = (d) => {
|
|
||||||
const when = profile.parseDateTime(profile, d.date, d.stbStop.dTimeR || d.stbStop.dTimeS)
|
|
||||||
const res = {
|
|
||||||
tripId: d.jid,
|
|
||||||
station: locations[parseInt(d.stbStop.locX)] || null,
|
|
||||||
when: when.toISO(),
|
|
||||||
direction: profile.parseStationName(d.dirTxt),
|
|
||||||
line: lines[parseInt(d.prodX)] || null,
|
|
||||||
remarks: (d.remL
|
|
||||||
? d.remL.map(ref => findRemark(hints, warnings, ref))
|
|
||||||
: []
|
|
||||||
),
|
|
||||||
trip: +d.jid.split('|')[1] // todo: this seems brittle
|
|
||||||
}
|
|
||||||
// todo: res.trip from rawLine.prodCtx.num?
|
|
||||||
|
|
||||||
// todo: DRY with parseStopover
|
|
||||||
// todo: DRY with parseJourneyLeg
|
|
||||||
if (d.stbStop.dTimeR && d.stbStop.dTimeS) {
|
|
||||||
const realtime = profile.parseDateTime(profile, d.date, d.stbStop.dTimeR)
|
|
||||||
const planned = profile.parseDateTime(profile, d.date, d.stbStop.dTimeS)
|
|
||||||
res.delay = Math.round((realtime - planned) / 1000)
|
|
||||||
} else res.delay = null
|
|
||||||
|
|
||||||
// todo: DRY with parseStopover
|
|
||||||
// todo: DRY with parseJourneyLeg
|
|
||||||
if (d.stbStop.aCncl || d.stbStop.dCncl) {
|
|
||||||
res.cancelled = true
|
|
||||||
Object.defineProperty(res, 'canceled', {value: true})
|
|
||||||
res.when = res.delay = null
|
|
||||||
|
|
||||||
const when = profile.parseDateTime(profile, d.date, d.stbStop.dTimeS)
|
|
||||||
res.formerScheduledWhen = when.toISO()
|
|
||||||
}
|
|
||||||
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|
||||||
return parseDeparture
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = createParseDeparture
|
module.exports = createParseDeparture
|
||||||
|
|
Loading…
Add table
Reference in a new issue