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

180 lines
4.5 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-12-12 03:28:54 +01:00
const getStations = require('vbb-stations')
2017-11-18 17:53:12 +01:00
const _parseLine = require('../../parse/line')
const _parseLocation = require('../../parse/location')
const _createParseJourney = require('../../parse/journey')
2017-12-12 03:28:54 +01:00
const _createParseStopover = require('../../parse/stopover')
const _createParseDeparture = require('../../parse/departure')
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-12-11 15:49:58 +01:00
body.ver = '1.11' // todo: 1.16 with `mic` and `mac` query params
2017-11-18 17:53:12 +01:00
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
2017-11-20 01:05:48 +01:00
if (res.type === 'station') {
2017-12-11 19:40:46 +01:00
res.name = shorten(res.name)
2017-11-20 01:05:48 +01:00
res.id = to12Digit(res.id)
2017-12-12 03:28:54 +01:00
if (!res.location.latitude || !res.location.longitude) {
const [s] = getStations(res.id)
if (s) Object.assign(res.location, s.coordinates)
}
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-12-12 03:28:54 +01:00
const createParseStopover = (profile, stations, lines, remarks, connection) => {
const parseStopover = _createParseStopover(profile, stations, lines, remarks, connection)
const parseStopoverWithShorten = (st) => {
const res = parseStopover(st)
if (res.station && res.station.name) {
res.station.name = shorten(res.station.name)
}
return res
}
return parseStopoverWithShorten
}
const createParseDeparture = (profile, stations, lines, remarks) => {
const parseDeparture = _createParseDeparture(profile, stations, lines, remarks)
const ringbahnClockwise = /^ringbahn s\s?41$/i
const ringbahnAnticlockwise = /^ringbahn s\s?42$/i
const parseDepartureRenameRingbahn = (j) => {
const res = parseDeparture(j)
if (res.line && res.line.product === 'suburban') {
const d = res.direction && res.direction.trim()
if (ringbahnClockwise.test(d)) res.direction = 'Ringbahn S41 ⟳'
else if (ringbahnAnticlockwise.test(d)) res.direction = 'Ringbahn S42 ⟲'
}
return res
}
return parseDepartureRenameRingbahn
}
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 = {
locale: 'de-DE',
2017-11-18 17:53:12 +01:00
timezone: 'Europe/Berlin',
endpoint: 'https://fahrinfo.vbb.de/bin/mgate.exe',
transformReqBody,
2017-12-12 03:28:54 +01:00
products: modes.allProducts,
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,
parseDeparture: createParseDeparture,
2017-12-12 03:28:54 +01:00
parseStopover: createParseStopover,
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