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')
|
2017-12-11 15:41:27 +01:00
|
|
|
|
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
|
|
|
|
|
2018-01-05 18:46:19 +01:00
|
|
|
|
const _createParseLine = require('../../parse/line')
|
2017-11-18 17:53:12 +01:00
|
|
|
|
const _parseLocation = require('../../parse/location')
|
2017-12-11 15:41:27 +01:00
|
|
|
|
const _createParseJourney = require('../../parse/journey')
|
2017-12-12 18:31:41 +01:00
|
|
|
|
const _createParseDeparture = require('../../parse/departure')
|
2017-12-11 15:41:27 +01:00
|
|
|
|
const _formatStation = require('../../format/station')
|
2017-11-18 17:53:12 +01:00
|
|
|
|
|
2018-03-16 14:23:27 +01:00
|
|
|
|
const products = require('./products')
|
2017-11-18 17:53:12 +01:00
|
|
|
|
|
|
|
|
|
const transformReqBody = (body) => {
|
2017-12-11 15:41:27 +01:00
|
|
|
|
body.client = {type: 'IPA', id: 'VBB', name: 'vbbPROD', v: '4010300'}
|
|
|
|
|
body.ext = 'VBB.1'
|
2018-01-19 15:47:41 +01:00
|
|
|
|
body.ver = '1.16'
|
2017-11-18 17:53:12 +01:00
|
|
|
|
body.auth = {type: 'AID', aid: 'hafas-vbb-apps'}
|
|
|
|
|
|
|
|
|
|
return body
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-13 19:59:44 +02:00
|
|
|
|
const createParseLine = (profile, opt, data) => {
|
|
|
|
|
const parseLine = _createParseLine(profile, opt, data)
|
2017-11-18 17:53:12 +01:00
|
|
|
|
|
2018-03-16 17:00:06 +01:00
|
|
|
|
const parseLineWithMoreDetails = (l) => {
|
2018-01-05 18:46:19 +01:00
|
|
|
|
const res = parseLine(l)
|
|
|
|
|
|
2018-03-31 14:03:23 +02:00
|
|
|
|
res.name = l.name.replace(/^(bus|tram)\s+/i, '')
|
2018-03-29 23:17:50 +02:00
|
|
|
|
const details = parseLineName(res.name)
|
2018-01-05 18:46:19 +01:00
|
|
|
|
res.symbol = details.symbol
|
|
|
|
|
res.nr = details.nr
|
|
|
|
|
res.metro = details.metro
|
|
|
|
|
res.express = details.express
|
|
|
|
|
res.night = details.night
|
2017-12-11 14:51:09 +01:00
|
|
|
|
|
2018-01-05 18:46:19 +01:00
|
|
|
|
return res
|
|
|
|
|
}
|
2018-03-16 17:00:06 +01:00
|
|
|
|
return parseLineWithMoreDetails
|
2017-11-18 17:53:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-13 19:59:44 +02:00
|
|
|
|
const parseLocation = (profile, opt, data, l) => {
|
|
|
|
|
const res = _parseLocation(profile, opt, data, 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)
|
2018-01-23 00:50:21 +01:00
|
|
|
|
if (s) Object.assign(res.location, s.location)
|
2017-12-12 03:28:54 +01:00
|
|
|
|
}
|
2017-11-20 01:05:48 +01:00
|
|
|
|
}
|
2017-11-18 17:53:12 +01:00
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-13 19:59:44 +02:00
|
|
|
|
const createParseJourney = (profile, opt, data) => {
|
|
|
|
|
const parseJourney = _createParseJourney(profile, opt, data)
|
2017-12-11 15:41:27 +01:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-13 19:59:44 +02:00
|
|
|
|
const createParseDeparture = (profile, opt, data) => {
|
|
|
|
|
const parseDeparture = _createParseDeparture(profile, opt, data)
|
2017-12-12 18:31:41 +01:00
|
|
|
|
|
|
|
|
|
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-12-16 03:20:11 +01:00
|
|
|
|
const validIBNR = /^\d+$/
|
2017-11-18 17:53:12 +01:00
|
|
|
|
const formatStation = (id) => {
|
2017-12-16 03:20:11 +01:00
|
|
|
|
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.')
|
|
|
|
|
}
|
|
|
|
|
// The VBB 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)
|
2017-11-18 17:53:12 +01:00
|
|
|
|
return _formatStation(id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const vbbProfile = {
|
2017-12-11 17:21:50 +01:00
|
|
|
|
locale: 'de-DE',
|
2017-11-18 17:53:12 +01:00
|
|
|
|
timezone: 'Europe/Berlin',
|
|
|
|
|
endpoint: 'https://fahrinfo.vbb.de/bin/mgate.exe',
|
2018-01-19 15:47:41 +01:00
|
|
|
|
|
|
|
|
|
// https://gist.github.com/derhuerst/a8d94a433358abc015ff77df4481070c#file-haf_config_base-properties-L39
|
|
|
|
|
// https://runkit.com/derhuerst/hafas-decrypt-encrypted-mac-salt
|
|
|
|
|
salt: Buffer.from('5243544a4d3266467846667878516649', 'hex'),
|
|
|
|
|
addMicMac: true,
|
|
|
|
|
|
2017-11-18 17:53:12 +01:00
|
|
|
|
transformReqBody,
|
|
|
|
|
|
2018-03-16 17:00:06 +01:00
|
|
|
|
products: products,
|
2017-12-12 03:28:54 +01:00
|
|
|
|
|
2017-11-20 15:08:30 +01:00
|
|
|
|
parseStationName: shorten,
|
2017-11-18 17:53:12 +01:00
|
|
|
|
parseLocation,
|
2018-01-05 18:46:19 +01:00
|
|
|
|
parseLine: createParseLine,
|
2017-12-11 15:41:27 +01:00
|
|
|
|
parseJourney: createParseJourney,
|
2017-12-12 18:31:41 +01:00
|
|
|
|
parseDeparture: createParseDeparture,
|
2017-11-18 17:53:12 +01:00
|
|
|
|
|
2017-11-20 01:05:48 +01:00
|
|
|
|
formatStation,
|
2017-11-27 19:45:33 +01:00
|
|
|
|
|
2018-03-07 13:32:40 +01:00
|
|
|
|
journeysNumF: false,
|
2017-12-28 16:56:27 +01:00
|
|
|
|
journeyLeg: true,
|
2017-11-27 19:45:33 +01:00
|
|
|
|
radar: true
|
2017-11-18 17:53:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = vbbProfile
|