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

137 lines
3.2 KiB
JavaScript
Raw Normal View History

2017-11-18 17:53:12 +01:00
'use strict'
const shorten = require('vbb-short-station-name')
2017-11-20 01:05:48 +01:00
const {to12Digit, to9Digit} = require('vbb-translate-ids')
2017-12-11 14:51:09 +01:00
const parseLineName = require('vbb-parse-line')
const parseTicket = require('vbb-parse-ticket')
2017-11-18 17:53:12 +01:00
const _parseLine = require('../../parse/line')
const _parseLocation = require('../../parse/location')
const _createParseJourney = require('../../parse/journey')
const _formatStation = require('../../format/station')
2017-11-27 19:39:18 +01:00
const createParseBitmask = require('../../parse/products-bitmask')
const createFormatBitmask = require('../../format/products-bitmask')
2017-11-18 17:53:12 +01:00
const modes = require('./modes')
2017-11-27 19:39:18 +01:00
const formatBitmask = createFormatBitmask(modes)
2017-11-18 17:53:12 +01:00
const transformReqBody = (body) => {
body.client = {type: 'IPA', id: 'VBB', name: 'vbbPROD', v: '4010300'}
body.ext = 'VBB.1'
2017-11-18 17:53:12 +01:00
body.ver = '1.11'
body.auth = {type: 'AID', aid: 'hafas-vbb-apps'}
return body
}
const parseLine = (profile, l) => {
const res = _parseLine(profile, l)
res.mode = res.product = null
if ('class' in res) {
const data = modes.bitmasks[parseInt(res.class)]
if (data) {
res.mode = data.mode
res.product = data.product
2017-11-18 17:53:12 +01:00
}
}
2017-12-11 14:51:09 +01:00
const details = parseLineName(l.name)
res.symbol = details.symbol
res.nr = details.nr
res.metro = details.metro
res.express = details.express
res.night = details.night
2017-11-18 17:53:12 +01:00
return res
}
const parseLocation = (profile, l) => {
const res = _parseLocation(profile, l)
2017-11-20 15:08:30 +01:00
// todo: shorten has been made for stations, not any type of location
res.name = shorten(res.name)
2017-11-20 01:05:48 +01:00
if (res.type === 'station') {
res.id = to12Digit(res.id)
2017-11-20 15:08:30 +01:00
// todo: https://github.com/derhuerst/vbb-hafas/blob/1c64bfe42422e2648b21016d233c808460250308/lib/parse.js#L67-L75
2017-11-20 01:05:48 +01:00
}
2017-11-18 17:53:12 +01:00
return res
}
const createParseJourney = (profile, stations, lines, remarks) => {
const parseJourney = _createParseJourney(profile, stations, lines, remarks)
const parseJourneyWithTickets = (j) => {
const res = parseJourney(j)
if (
j.trfRes &&
Array.isArray(j.trfRes.fareSetL) &&
j.trfRes.fareSetL[0] &&
Array.isArray(j.trfRes.fareSetL[0].fareL)
) {
res.tickets = []
const sets = j.trfRes.fareSetL[0].fareL
for (let s of sets) {
if (!Array.isArray(s.ticketL) || s.ticketL.length === 0) continue
for (let t of s.ticketL) {
const ticket = parseTicket(t)
ticket.name = s.name + ' ' + ticket.name
res.tickets.push(ticket)
}
}
}
return res
}
return parseJourneyWithTickets
}
2017-11-18 17:53:12 +01:00
const isIBNR = /^\d{9,}$/
const formatStation = (id) => {
if (!isIBNR.test(id)) throw new Error('station ID must be an IBNR.')
2017-11-20 01:05:48 +01:00
id = to9Digit(id)
2017-11-18 17:53:12 +01:00
return _formatStation(id)
}
const defaultProducts = {
suburban: true,
subway: true,
tram: true,
bus: true,
ferry: true,
express: true,
regional: true
}
const formatProducts = (products) => {
products = Object.assign(Object.create(null), defaultProducts, products)
return {
type: 'PROD',
mode: 'INC',
2017-11-27 19:39:18 +01:00
value: formatBitmask(products) + ''
2017-11-18 17:53:12 +01:00
}
}
const vbbProfile = {
timezone: 'Europe/Berlin',
endpoint: 'https://fahrinfo.vbb.de/bin/mgate.exe',
transformReqBody,
2017-11-20 15:08:30 +01:00
parseStationName: shorten,
2017-11-18 17:53:12 +01:00
parseLocation,
parseLine,
2017-11-27 19:39:18 +01:00
parseProducts: createParseBitmask(modes.bitmasks),
parseJourney: createParseJourney,
2017-11-18 17:53:12 +01:00
2017-11-20 01:05:48 +01:00
formatStation,
formatProducts,
journeyPart: true,
radar: true
2017-11-18 17:53:12 +01:00
}
module.exports = vbbProfile