db-vendo-client/p/bvg/index.js

148 lines
4.4 KiB
JavaScript
Raw Normal View History

2018-07-28 13:43:15 +02:00
'use strict'
const shorten = require('vbb-short-station-name')
const {to12Digit, to9Digit} = require('vbb-translate-ids')
const parseLineName = require('vbb-parse-line')
const getStations = require('vbb-stations')
2019-10-20 00:19:52 +02:00
const {parseHook} = require('../../lib/profile-hooks')
2018-07-28 13:43:15 +02:00
2019-10-20 00:19:52 +02:00
const _parseLine = require('../../parse/line')
2018-07-28 13:43:15 +02:00
const _parseLocation = require('../../parse/location')
2019-10-20 00:19:52 +02:00
const _parseDeparture = require('../../parse/departure')
const _parseJourneyLeg = require('../../parse/journey-leg')
2018-07-28 13:43:15 +02:00
const _formatStation = require('../../format/station')
const products = require('./products')
2019-10-20 00:19:52 +02:00
const transformReqBody = (ctx, body) => {
2018-10-28 16:36:49 +01:00
body.client = {type: 'IPA', id: 'BVG', name: 'FahrInfo', v: '6020000'}
2018-07-28 13:43:15 +02:00
body.ext = 'BVG.1'
2018-10-28 16:36:49 +01:00
body.ver = '1.21'
body.auth = {type: 'AID', aid: 'Mz0YdF9Fgx0Mb9'}
2018-07-28 13:43:15 +02:00
return body
}
2019-10-20 00:19:52 +02:00
const parseLineWithMoreDetails = ({parsed}, p) => {
parsed.name = p.name.replace(/^(bus|tram)\s+/i, '')
const details = parseLineName(parsed.name)
parsed.symbol = details.symbol
parsed.nr = details.nr
parsed.metro = details.metro
parsed.express = details.express
parsed.night = details.night
2018-07-28 13:43:15 +02:00
2019-10-20 00:19:52 +02:00
return parsed
2018-07-28 13:43:15 +02:00
}
2019-10-20 00:19:52 +02:00
const parseLocation = ({parsed}, l) => {
if (parsed.type === 'stop' || parsed.type === 'station') {
parsed.name = shorten(parsed.name)
parsed.id = to12Digit(parsed.id)
if (!parsed.location.latitude || !parsed.location.longitude) {
const [s] = getStations(parsed.id)
if (s) Object.assign(parsed.location, s.location)
2018-07-28 13:43:15 +02:00
}
}
2019-10-20 00:19:52 +02:00
return parsed
2018-07-28 13:43:15 +02:00
}
2019-10-20 00:19:52 +02:00
const ringbahnClockwise = /^ringbahn s\s?41$/i
const ringbahnAnticlockwise = /^ringbahn s\s?42$/i
const parseDepartureRenameRingbahn = ({parsed}) => {
if (parsed.line && parsed.line.product === 'suburban') {
const d = parsed.direction && parsed.direction.trim()
if (ringbahnClockwise.test(d)) {
parsed.direction = 'Ringbahn S41 ⟳'
} else if (ringbahnAnticlockwise.test(d)) {
parsed.direction = 'Ringbahn S42 ⟲'
2018-07-28 13:43:15 +02:00
}
}
2019-10-20 00:19:52 +02:00
return parsed
2018-07-28 13:43:15 +02:00
}
2019-10-20 00:19:52 +02:00
const parseJourneyLegWithBerlkönig = (ctx, leg, date) => {
if (leg.type === 'KISS') {
const icon = ctx.common.icons[leg.icoX]
if (icon && icon.type === 'prod_berl') {
const res = _parseJourneyLeg(ctx, {
...leg, type: 'WALK'
}, date)
delete res.walking
const mcp = leg.dep.mcp || {}
const mcpData = mcp.mcpData || {}
// todo: mcp.lid
// todo: mcpData.occupancy, mcpData.type
// todo: journey.trfRes.bkgData
res.line = {
type: 'line',
id: null, // todo
// todo: fahrtNr?
name: mcpData.providerName,
public: true,
mode: 'taxi',
product: 'berlkoenig'
// todo: operator
2018-10-28 16:37:19 +01:00
}
2019-10-20 00:19:52 +02:00
return res
2018-10-28 16:37:19 +01:00
}
}
2019-10-20 00:19:52 +02:00
return _parseJourneyLeg(ctx, leg, date)
2018-10-28 16:37:19 +01:00
}
2018-07-28 13:43:15 +02:00
const validIBNR = /^\d+$/
const formatStation = (id) => {
if ('string' !== typeof id) throw new Error('station ID must be a string.')
const l = id.length
if ((l !== 7 && l !== 9 && l !== 12) || !validIBNR.test(id)) {
throw new Error('station ID must be a valid IBNR.')
}
// BVG has some 7-digit stations. We don't convert them to 12 digits,
// because it only recognizes in the 7-digit format. see derhuerst/vbb-hafas#22
if (l !== 7) id = to9Digit(id)
return _formatStation(id)
}
2018-10-28 16:37:19 +01:00
// use the Berlkönig ride sharing service?
2019-10-20 00:19:52 +02:00
const requestJourneysWithBerlkoenig = ({opt}, query) => {
2018-10-28 16:37:19 +01:00
if (('numF' in query) && opt.berlkoenig) {
// todo: check if this is still true
throw new Error('The `berlkoenig` and `results` options are mutually exclusive.')
}
query.jnyFltrL.push({type: 'GROUP', mode: 'INC', value: 'OEV'})
if (opt.berlkoenig) query.jnyFltrL.push({type: 'GROUP', mode: 'INC', value: 'BERLKOENIG'})
query.gisFltrL = [{meta: 'foot_speed_normal', type: 'M', mode: 'FB'}]
return query
}
2018-07-28 13:43:15 +02:00
// todo: adapt/extend `vbb-parse-ticket` to support the BVG markup
const bvgProfile = {
locale: 'de-DE',
timezone: 'Europe/Berlin',
endpoint: 'https://bvg-apps.hafas.de/bin/mgate.exe',
transformReqBody,
2018-10-28 16:37:19 +01:00
transformJourneysQuery: requestJourneysWithBerlkoenig,
2018-07-28 13:43:15 +02:00
products,
2019-10-20 00:19:52 +02:00
parseLine: parseHook(_parseLine, parseLineWithMoreDetails),
parseLocation: parseHook(_parseLocation, parseLocation),
parseStationName: (ctx, name) => shorten(name),
parseDeparture: parseHook(_parseDeparture, parseDepartureRenameRingbahn),
parseJourneyLeg: parseJourneyLegWithBerlkönig,
2018-07-28 13:43:15 +02:00
formatStation,
2018-10-28 16:36:49 +01:00
departuresGetPasslist: false, // `departures()` method: support for `getPasslist` field?
departuresStbFltrEquiv: false, // `departures()` method: support for `stbFltrEquiv` field?
2018-07-28 13:43:15 +02:00
trip: true,
2018-08-26 18:35:27 +02:00
radar: true,
refreshJourney: true,
reachableFrom: true
2018-07-28 13:43:15 +02:00
}
module.exports = bvgProfile