db-vendo-client/parse/journey-leg.js

151 lines
4.6 KiB
JavaScript
Raw Normal View History

2017-11-20 15:43:13 +01:00
'use strict'
const parseWhen = require('./when')
const findRemarks = require('./find-remarks')
2017-11-20 15:43:13 +01:00
const clone = obj => Object.assign({}, obj)
const addRemark = (stopoverOrLeg, remark) => {
if (!Array.isArray(stopoverOrLeg.remarks)) stopoverOrLeg.remarks = []
stopoverOrLeg.remarks.push(remark)
}
const applyRemarks = (leg, refs) => {
for (let [remark, ref] of findRemarks(refs)) {
const {fromLocation, toLocation} = ref
2019-08-23 16:24:09 +02:00
const fromI = fromLocation ? leg.stopovers.findIndex(s => s.stop === fromLocation) : -1
const toI = toLocation ? leg.stopovers.findIndex(s => s.stop === toLocation) : -1
if (fromI < 0 || toI < 0) continue
const wholeLeg = fromI === 0 && toI === (leg.stopovers.length - 1)
if (wholeLeg) addRemark(leg, remark)
else {
for (let i = fromI; i <= toI; i++) {
const stopover = leg.stopovers[i]
if (stopover) addRemark(stopover, remark)
2018-06-07 16:38:54 +02:00
}
}
2018-06-07 16:38:54 +02:00
// todo: `ref.tagL`
}
}
2017-11-20 15:43:13 +01:00
2018-06-26 12:52:33 +02:00
const createParseJourneyLeg = (profile, opt, data) => {
// todo: pt.status, pt.isPartCncl
// todo: pt.isRchbl, pt.chRatingRT, pt.chgDurR, pt.minChg
2017-11-20 15:43:13 +01:00
// todo: pt.dep.dProgType, pt.arr.dProgType
// todo: what is pt.jny.dirFlg?
2019-08-30 16:27:26 +02:00
// todo: what is pt.recState?
// todo: what is `sty: 'UNDEF'`?
// todo: pt.prodL
// todo: pt.parJnyL (list of coupled trains)
// j = journey, pt = part
2018-06-11 19:50:44 +02:00
// todo: pt.planrtTS
const parseJourneyLeg = (j, pt, parseStopovers = true) => {
2017-11-20 15:43:13 +01:00
const res = {
origin: clone(pt.dep.location) || null,
destination: clone(pt.arr.location)
2019-06-08 12:54:59 +02:00
}
const arr = parseWhen(profile, j.date, pt.arr.aTimeS, pt.arr.aTimeR, pt.arr.aTZOffset, pt.arr.aCncl)
res.arrival = arr.when
res.plannedArrival = arr.plannedWhen
res.arrivalDelay = arr.delay
if (arr.prognosedWhen) res.prognosedArrival = arr.prognosedWhen
const dep = parseWhen(profile, j.date, pt.dep.dTimeS, pt.dep.dTimeR, pt.dep.dTZOffset, pt.dep.dCncl)
res.departure = dep.when
res.plannedDeparture = dep.plannedWhen
res.departureDelay = dep.delay
if (dep.prognosedWhen) res.prognosedDeparture = dep.prognosedWhen
2019-06-08 12:54:59 +02:00
if (pt.jny) {
res.reachable = !!pt.jny.isRchbl
2017-11-20 15:43:13 +01:00
}
2019-08-23 15:39:42 +02:00
if (pt.jny && pt.jny.polyline) {
res.polyline = pt.jny.polyline || null
2018-04-30 12:49:58 +02:00
}
if (pt.type === 'WALK' || pt.type === 'TRSF') {
2017-12-12 03:28:54 +01:00
res.public = true
res.walking = true
2018-06-28 14:11:41 +02:00
res.distance = pt.gis && pt.gis.dist || null
if (pt.type === 'TRSF') res.transfer = true
2019-08-30 16:27:26 +02:00
// https://gist.github.com/derhuerst/426d4b95aeae701843b1e9c23105b8d4#file-tripsearch-2018-12-05-http-L4207-L4229
if (opt.remarks && Array.isArray(pt.gis.msgL)) {
applyRemarks(res, pt.gis.msgL)
}
2017-11-20 15:43:13 +01:00
} else if (pt.type === 'JNY') {
2017-12-12 03:28:54 +01:00
// todo: pull `public` value from `profile.products`
2018-11-21 23:42:13 +01:00
res.tripId = pt.jny.jid
2019-08-23 16:06:21 +02:00
res.line = pt.jny.line || null
2019-04-01 19:22:10 +02:00
res.direction = pt.jny.dirTxt && profile.parseStationName(pt.jny.dirTxt) || null
2017-11-20 15:43:13 +01:00
res.arrivalPlatform = pt.arr.aPlatfR ||pt.arr.aPlatfS || null
if (pt.arr.aPlatfR && pt.arr.aPlatfS && pt.arr.aPlatfR !== pt.arr.aPlatfS) {
res.scheduledArrivalPlatform = pt.arr.aPlatfS
}
res.departurePlatform = pt.dep.dPlatfR || pt.dep.dPlatfS || null
if (pt.dep.dPlatfR && pt.dep.dPlatfS && pt.dep.dPlatfR !== pt.dep.dPlatfS) {
res.scheduledDeparturePlatform = pt.dep.dPlatfS
}
2017-11-20 15:43:13 +01:00
if (parseStopovers && pt.jny.stopL) {
const parse = profile.parseStopover(profile, opt, data, j.date)
const stopL = pt.jny.stopL
res.stopovers = stopL.map(parse)
2018-06-28 13:45:56 +02:00
if (opt.remarks && Array.isArray(pt.jny.msgL)) {
// todo: apply leg-wide remarks if `parseStopovers` is false
applyRemarks(res, pt.jny.msgL)
2018-06-07 16:38:54 +02:00
}
// filter stations the train passes without stopping, as this doesn't comply with fptf (yet)
res.stopovers = res.stopovers.filter((x) => !x.passBy)
2017-11-20 15:43:13 +01:00
}
2018-04-29 14:29:29 +02:00
const freq = pt.jny.freq || {}
2018-12-02 01:05:01 +01:00
// todo: expose `res.cycle` even if only one field exists (breaking)
2018-04-29 14:29:29 +02:00
if (freq.minC && freq.maxC) {
res.cycle = {
min: freq.minC * 60,
max: freq.maxC * 60
}
2018-12-02 01:05:01 +01:00
// nr of connections in this frequency, from now on
if (freq.numC) res.cycle.nr = freq.numC
2018-04-29 14:29:29 +02:00
}
2018-12-02 01:05:01 +01:00
2018-04-29 14:29:29 +02:00
if (freq.jnyL) {
2017-11-20 15:43:13 +01:00
const parseAlternative = (a) => {
// todo: parse this just like a `leg` (breaking)
// todo: parse `a.stopL`, `a.ctxRecon`, `a.msgL`
const st0 = a.stopL[0] || {}
2017-11-20 15:43:13 +01:00
return {
tripId: a.jid,
2019-08-23 16:06:21 +02:00
line: a.line || null,
direction: a.dirTxt || null,
...parseWhen(profile, j.date, st0.dTimeS, st0.dTimeR, st0.dTZOffset, st0.dCncl)
2017-11-20 15:43:13 +01:00
}
}
2018-04-29 14:29:29 +02:00
res.alternatives = freq.jnyL.map(parseAlternative)
2017-11-20 15:43:13 +01:00
}
}
if (pt.arr.aCncl || pt.dep.dCncl) {
res.cancelled = true
Object.defineProperty(res, 'canceled', {value: true})
}
2017-11-20 15:43:13 +01:00
return res
}
2017-12-28 16:56:27 +01:00
return parseJourneyLeg
2017-11-20 15:43:13 +01:00
}
2017-12-28 16:56:27 +01:00
module.exports = createParseJourneyLeg