2017-11-20 15:43:13 +01:00
|
|
|
'use strict'
|
|
|
|
|
2019-08-23 16:21:44 +02:00
|
|
|
const findRemarks = require('./find-remarks')
|
2017-11-20 15:43:13 +01:00
|
|
|
|
|
|
|
const clone = obj => Object.assign({}, obj)
|
|
|
|
|
2019-08-23 16:21:44 +02:00
|
|
|
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)) {
|
2019-08-23 16:17:20 +02:00
|
|
|
const {fromLocation, toLocation} = ref
|
2018-06-25 13:54:30 +02:00
|
|
|
|
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-07-16 14:41:41 +02:00
|
|
|
|
2018-06-07 16:38:54 +02:00
|
|
|
// todo: `ref.tagL`
|
|
|
|
}
|
|
|
|
}
|
2017-11-20 15:43:13 +01:00
|
|
|
|
2019-10-20 00:19:11 +02:00
|
|
|
// todo: pt.status, pt.isPartCncl
|
|
|
|
// todo: pt.isRchbl, pt.chRatingRT, pt.chgDurR, pt.minChg
|
|
|
|
// todo: pt.dep.dProgType, pt.arr.dProgType
|
|
|
|
// todo: what is pt.jny.dirFlg?
|
|
|
|
// todo: what is pt.recState?
|
|
|
|
// todo: what is `sty: 'UNDEF'`?
|
|
|
|
// todo: pt.prodL
|
|
|
|
// todo: pt.parJnyL (list of coupled trains)
|
|
|
|
// todo: pt.planrtTS
|
|
|
|
|
|
|
|
const parseJourneyLeg = (ctx, pt, date) => { // pt = raw leg
|
|
|
|
const {profile, opt} = ctx
|
|
|
|
|
|
|
|
const res = {
|
|
|
|
origin: clone(pt.dep.location) || null,
|
|
|
|
destination: clone(pt.arr.location)
|
|
|
|
}
|
2019-06-08 12:54:59 +02:00
|
|
|
|
2019-10-20 00:19:11 +02:00
|
|
|
const arr = profile.parseWhen(ctx, 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
|
add planned(Arrival|Departure|When), scheduled* -> planned*/prognosed* :boom:
when not cancelled:
{when, delay} -> {when, plannedWhen, delay}
{arrival, arrivalDelay} -> {arrival, plannedArrival, arrivalDelay}
{departure, departureDelay} -> {departure, plannedDeparture, departureDelay}
when cancelled:
{when: null, delay: null, scheduledWhen} -> {when: null, plannedWhen, prognosedWhen, delay}
{arrival: null, arrivalDelay: null, scheduledArrival, formerArrivalDelay} -> {arrival: null, plannedArrival, arrivalDelay, prognosedArrival}
{departure: null, departureDelay: null, scheduledDeparture, formerDepartureDelay} -> {departure: null, plannedDeparture, departureDelay, prognosedDeparture}
2019-08-23 18:51:03 +02:00
|
|
|
|
2019-10-20 00:19:11 +02:00
|
|
|
const dep = profile.parseWhen(ctx, 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
|
add planned(Arrival|Departure|When), scheduled* -> planned*/prognosed* :boom:
when not cancelled:
{when, delay} -> {when, plannedWhen, delay}
{arrival, arrivalDelay} -> {arrival, plannedArrival, arrivalDelay}
{departure, departureDelay} -> {departure, plannedDeparture, departureDelay}
when cancelled:
{when: null, delay: null, scheduledWhen} -> {when: null, plannedWhen, prognosedWhen, delay}
{arrival: null, arrivalDelay: null, scheduledArrival, formerArrivalDelay} -> {arrival: null, plannedArrival, arrivalDelay, prognosedArrival}
{departure: null, departureDelay: null, scheduledDeparture, formerDepartureDelay} -> {departure: null, plannedDeparture, departureDelay, prognosedDeparture}
2019-08-23 18:51:03 +02:00
|
|
|
|
2020-03-18 21:42:29 +01:00
|
|
|
if (pt.jny && ('isRchbl' in pt.jny)) {
|
2019-10-20 00:19:11 +02:00
|
|
|
res.reachable = !!pt.jny.isRchbl
|
|
|
|
}
|
2017-11-20 15:43:13 +01:00
|
|
|
|
2019-10-20 00:19:11 +02:00
|
|
|
if (pt.jny && pt.jny.polyline) {
|
|
|
|
res.polyline = pt.jny.polyline || null
|
2020-07-26 13:20:15 +02:00
|
|
|
} else if (pt.jny && pt.jny.poly) {
|
|
|
|
res.polyline = profile.parsePolyline(ctx, pt.jny.poly)
|
2019-10-20 00:19:11 +02:00
|
|
|
}
|
2018-04-30 12:49:58 +02:00
|
|
|
|
2020-04-18 17:22:56 +02:00
|
|
|
if (pt.type === 'WALK' || pt.type === 'TRSF' || pt.type === 'DEVI') {
|
2019-10-20 00:19:11 +02:00
|
|
|
res.public = true
|
|
|
|
res.walking = true
|
|
|
|
res.distance = pt.gis && pt.gis.dist || null
|
|
|
|
if (pt.type === 'TRSF') res.transfer = true
|
2020-04-18 17:22:56 +02:00
|
|
|
if (pt.type === 'DEVI') {
|
|
|
|
// todo: pt.resState, pt.resRecommendation
|
|
|
|
res.transfer = true
|
|
|
|
}
|
2018-07-16 12:18:23 +02:00
|
|
|
|
2019-10-20 00:19:11 +02:00
|
|
|
// https://gist.github.com/derhuerst/426d4b95aeae701843b1e9c23105b8d4#file-tripsearch-2018-12-05-http-L4207-L4229
|
2020-04-18 17:22:56 +02:00
|
|
|
if (opt.remarks && pt.gis && Array.isArray(pt.gis.msgL)) {
|
2019-10-20 00:19:11 +02:00
|
|
|
applyRemarks(res, pt.gis.msgL)
|
|
|
|
}
|
|
|
|
} else if (pt.type === 'JNY') {
|
|
|
|
// todo: pull `public` value from `profile.products`
|
|
|
|
res.tripId = pt.jny.jid
|
|
|
|
res.line = pt.jny.line || null
|
|
|
|
res.direction = pt.jny.dirTxt && profile.parseStationName(ctx, pt.jny.dirTxt) || null
|
2020-08-01 16:51:42 +02:00
|
|
|
const arrPl = profile.parsePlatform(ctx, pt.arr.aPlatfS || (pt.arr.aPltfS !== undefined ? pt.arr.aPltfS.txt : null), pt.arr.aPlatfR || (pt.arr.aPltfR !== undefined ? pt.arr.aPltfR.txt : null), pt.arr.aCncl)
|
2019-10-20 00:19:11 +02:00
|
|
|
res.arrivalPlatform = arrPl.platform
|
|
|
|
res.plannedArrivalPlatform = arrPl.plannedPlatform
|
|
|
|
if (arrPl.prognosedPlatform) res.prognosedArrivalPlatform = arrPl.prognosedPlatform
|
|
|
|
|
2020-08-01 16:51:42 +02:00
|
|
|
const depPl = profile.parsePlatform(ctx, pt.dep.dPlatfS || (pt.dep.dPltfS !== undefined ? pt.dep.dPltfS.txt : null), pt.dep.dPlatfR || (pt.dep.dPltfR !== undefined ? pt.dep.dPltfR.txt : null), pt.dep.dCncl)
|
2019-10-20 00:19:11 +02:00
|
|
|
res.departurePlatform = depPl.platform
|
|
|
|
res.plannedDeparturePlatform = depPl.plannedPlatform
|
|
|
|
if (depPl.prognosedPlatform) res.prognosedDeparturePlatform = depPl.prognosedPlatform
|
|
|
|
|
|
|
|
if (opt.stopovers && pt.jny.stopL) {
|
|
|
|
const stopL = pt.jny.stopL
|
|
|
|
res.stopovers = stopL.map(s => profile.parseStopover(ctx, s, date))
|
|
|
|
|
|
|
|
if (opt.remarks && Array.isArray(pt.jny.msgL)) {
|
|
|
|
// todo: apply leg-wide remarks if `opt.stopovers` is false
|
|
|
|
applyRemarks(res, pt.jny.msgL)
|
2018-07-16 12:18:23 +02:00
|
|
|
}
|
2018-06-25 13:54:30 +02:00
|
|
|
|
2019-10-20 00:19:11 +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
|
|
|
|
2019-10-20 00:19:11 +02:00
|
|
|
const freq = pt.jny.freq || {}
|
|
|
|
// todo: expose `res.cycle` even if only one field exists (breaking)
|
|
|
|
if (freq.minC && freq.maxC) {
|
|
|
|
res.cycle = {
|
|
|
|
min: freq.minC * 60,
|
|
|
|
max: freq.maxC * 60
|
2018-04-29 14:29:29 +02:00
|
|
|
}
|
2019-10-20 00:19:11 +02:00
|
|
|
// nr of connections in this frequency, from now on
|
|
|
|
if (freq.numC) res.cycle.nr = freq.numC
|
|
|
|
}
|
2018-12-02 01:05:01 +01:00
|
|
|
|
2019-10-20 00:19:11 +02:00
|
|
|
if (freq.jnyL) {
|
|
|
|
const parseAlternative = (a) => {
|
|
|
|
// todo: parse this just like a `leg` (breaking)
|
|
|
|
// todo: parse `a.stopL`, `a.ctxRecon`, `a.msgL`
|
|
|
|
const st0 = a.stopL[0] || {}
|
|
|
|
return {
|
|
|
|
tripId: a.jid,
|
|
|
|
line: a.line || null,
|
|
|
|
direction: a.dirTxt || null,
|
|
|
|
...profile.parseWhen(ctx, date, st0.dTimeS, st0.dTimeR, st0.dTZOffset, st0.dCncl)
|
2017-11-20 15:43:13 +01:00
|
|
|
}
|
|
|
|
}
|
2019-10-20 00:19:11 +02:00
|
|
|
res.alternatives = freq.jnyL.map(parseAlternative)
|
2017-11-20 15:43:13 +01:00
|
|
|
}
|
2019-10-20 00:19:11 +02:00
|
|
|
}
|
2017-11-20 15:43:13 +01:00
|
|
|
|
2019-10-20 00:19:11 +02: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
|
|
|
}
|
|
|
|
|
2019-10-20 00:19:11 +02:00
|
|
|
return res
|
2017-11-20 15:43:13 +01:00
|
|
|
}
|
|
|
|
|
2019-10-20 00:19:11 +02:00
|
|
|
module.exports = parseJourneyLeg
|