2017-11-20 15:43:13 +01:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const parseDateTime = require('./date-time')
|
2018-06-07 16:38:54 +02:00
|
|
|
const findRemark = require('./find-remark')
|
2017-11-20 15:43:13 +01:00
|
|
|
|
|
|
|
const clone = obj => Object.assign({}, obj)
|
|
|
|
|
2018-06-25 13:54:30 +02:00
|
|
|
const locX = Symbol('locX')
|
|
|
|
|
2018-06-11 11:29:32 +02:00
|
|
|
const applyRemarks = (leg, hints, warnings, refs) => {
|
2018-06-07 16:38:54 +02:00
|
|
|
for (let ref of refs) {
|
2018-06-07 18:48:45 +02:00
|
|
|
const remark = findRemark(hints, warnings, ref)
|
2018-06-25 13:54:30 +02:00
|
|
|
if (!remark) continue
|
|
|
|
|
2018-06-11 11:29:32 +02:00
|
|
|
if ('number' === typeof ref.fLocX && 'number' === typeof ref.tLocX) {
|
2018-06-25 13:54:30 +02:00
|
|
|
const fromI = leg.stopovers.findIndex(s => s[locX] === ref.fLocX)
|
|
|
|
const toI = leg.stopovers.findIndex(s => s[locX] === ref.tLocX)
|
|
|
|
if (fromI < 0 || toI < 0) continue
|
|
|
|
|
2018-07-16 14:41:41 +02:00
|
|
|
const wholeLeg = fromI === 0 && toI === (leg.stopovers.length - 1)
|
|
|
|
if (!wholeLeg) {
|
|
|
|
for (let i = fromI; i <= toI; i++) {
|
|
|
|
const stopover = leg.stopovers[i]
|
|
|
|
if (!stopover) continue
|
|
|
|
if (Array.isArray(stopover.remarks)) {
|
|
|
|
stopover.remarks.push(remark)
|
|
|
|
} else {
|
|
|
|
stopover.remarks = [remark]
|
|
|
|
}
|
2018-06-11 11:29:32 +02:00
|
|
|
}
|
2018-07-16 14:41:41 +02:00
|
|
|
|
|
|
|
continue
|
2018-06-07 16:38:54 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-16 14:41:41 +02:00
|
|
|
|
|
|
|
if (Array.isArray(leg.remarks)) leg.remarks.push(remark)
|
|
|
|
else leg.remarks = [remark]
|
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) => {
|
|
|
|
const {locations, lines, hints, warnings, polylines} = data
|
2018-06-27 11:13:55 +02:00
|
|
|
// todo: pt.status, pt.isPartCncl
|
2018-07-02 16:49:21 +02:00
|
|
|
// 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?
|
2018-06-13 20:39:33 +02:00
|
|
|
|
|
|
|
// j = journey, pt = part
|
2018-06-11 19:50:44 +02:00
|
|
|
// todo: pt.planrtTS
|
2018-06-13 20:39:33 +02:00
|
|
|
const parseJourneyLeg = (j, pt, parseStopovers = true) => {
|
2018-10-15 16:21:29 +02:00
|
|
|
const dep = profile.parseDateTime(profile, j.date, pt.dep.dTimeR || pt.dep.dTimeS, pt.dep.dTZOffset)
|
|
|
|
const arr = profile.parseDateTime(profile, j.date, pt.arr.aTimeR || pt.arr.aTimeS, pt.arr.aTZOffset)
|
2017-11-20 15:43:13 +01:00
|
|
|
const res = {
|
2018-06-13 18:59:56 +02:00
|
|
|
origin: clone(locations[parseInt(pt.dep.locX)]) || null,
|
|
|
|
destination: clone(locations[parseInt(pt.arr.locX)]),
|
2018-10-15 15:50:43 +02:00
|
|
|
departure: dep,
|
2019-06-03 17:51:37 +02:00
|
|
|
arrival: arr,
|
|
|
|
reachable: !!pt.jny.isRchbl
|
2017-11-20 15:43:13 +01:00
|
|
|
}
|
|
|
|
|
2018-06-07 18:52:12 +02:00
|
|
|
// todo: DRY with parseDeparture
|
|
|
|
// todo: DRY with parseStopover
|
2017-11-20 15:43:13 +01:00
|
|
|
if (pt.dep.dTimeR && pt.dep.dTimeS) {
|
2018-10-15 16:21:29 +02:00
|
|
|
const realtime = profile.parseDateTime(profile, j.date, pt.dep.dTimeR, pt.dep.dTZOffset, true)
|
|
|
|
const planned = profile.parseDateTime(profile, j.date, pt.dep.dTimeS, pt.dep.dTZOffset, true)
|
2018-02-01 15:10:37 +01:00
|
|
|
res.departureDelay = Math.round((realtime - planned) / 1000)
|
|
|
|
}
|
|
|
|
if (pt.arr.aTimeR && pt.arr.aTimeS) {
|
2018-10-15 16:21:29 +02:00
|
|
|
const realtime = profile.parseDateTime(profile, j.date, pt.arr.aTimeR, pt.dep.aTZOffset, true)
|
|
|
|
const planned = profile.parseDateTime(profile, j.date, pt.arr.aTimeS, pt.dep.aTZOffset, true)
|
2018-02-01 15:10:37 +01:00
|
|
|
res.arrivalDelay = Math.round((realtime - planned) / 1000)
|
2017-11-20 15:43:13 +01:00
|
|
|
}
|
|
|
|
|
2018-04-30 12:49:58 +02:00
|
|
|
if (pt.jny && pt.jny.polyG) {
|
2018-04-30 13:14:19 +02:00
|
|
|
let p = pt.jny.polyG.polyXL
|
2018-05-16 21:07:43 +02:00
|
|
|
p = Array.isArray(p) && polylines[p[0]]
|
2018-04-30 12:49:58 +02:00
|
|
|
// todo: there can be >1 polyline
|
2018-06-13 19:59:44 +02:00
|
|
|
const parse = profile.parsePolyline(profile, opt, data)
|
2018-05-16 21:07:43 +02:00
|
|
|
res.polyline = p && parse(p) || null
|
2018-04-30 12:49:58 +02:00
|
|
|
}
|
|
|
|
|
2018-07-02 16:49:21 +02:00
|
|
|
if (pt.type === 'WALK' || pt.type === 'TRSF') {
|
2017-12-12 03:28:54 +01:00
|
|
|
res.public = true
|
2019-02-01 14:54:33 +01:00
|
|
|
res.walking = true
|
2018-06-28 14:11:41 +02:00
|
|
|
res.distance = pt.gis && pt.gis.dist || null
|
2018-07-05 12:13:29 +02:00
|
|
|
if (pt.type === 'TRSF') res.transfer = true
|
2018-07-16 12:18:23 +02:00
|
|
|
|
|
|
|
if (opt.remarks && Array.isArray(pt.gis.msgL)) {
|
|
|
|
applyRemarks(res, hints, warnings, 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
|
2017-11-20 15:43:13 +01:00
|
|
|
res.line = lines[parseInt(pt.jny.prodX)] || 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
|
|
|
|
2019-05-29 14:10:12 +02: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
|
|
|
|
2018-06-13 20:39:33 +02:00
|
|
|
if (parseStopovers && pt.jny.stopL) {
|
2018-06-13 19:59:44 +02:00
|
|
|
const parse = profile.parseStopover(profile, opt, data, j.date)
|
2018-06-25 13:54:30 +02:00
|
|
|
const stopL = pt.jny.stopL
|
|
|
|
res.stopovers = stopL.map(parse)
|
2018-06-07 16:00:28 +02:00
|
|
|
|
2018-06-11 11:29:32 +02:00
|
|
|
// todo: is there a `pt.jny.remL`?
|
2018-06-28 13:45:56 +02:00
|
|
|
if (opt.remarks && Array.isArray(pt.jny.msgL)) {
|
2018-06-25 13:54:30 +02:00
|
|
|
for (let i = 0; i < stopL.length; i++) {
|
|
|
|
Object.defineProperty(res.stopovers[i], locX, {
|
|
|
|
value: stopL[i].locX
|
|
|
|
})
|
|
|
|
}
|
2018-06-28 13:45:56 +02:00
|
|
|
// todo: apply leg-wide remarks if `parseStopovers` is false
|
2018-06-11 11:29:32 +02:00
|
|
|
applyRemarks(res, hints, warnings, pt.jny.msgL)
|
2018-06-07 16:38:54 +02:00
|
|
|
}
|
2018-06-25 13:54:30 +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) => {
|
2018-12-02 01:15:46 +01:00
|
|
|
// todo: parse this just like a `leg` (breaking)
|
|
|
|
// todo: parse `a.stopL`, `a.ctxRecon`, `a.msgL`
|
|
|
|
const st0 = a.stopL[0]
|
|
|
|
|
|
|
|
let when = null, delay = null
|
|
|
|
if (st0) {
|
2018-10-15 16:21:29 +02:00
|
|
|
const planned = st0.dTimeS && profile.parseDateTime(profile, j.date, st0.dTimeS, st0.dTZOffset)
|
2018-12-02 01:15:46 +01:00
|
|
|
if (st0.dTimeR && planned) {
|
2018-10-15 16:21:29 +02:00
|
|
|
const realtime = profile.parseDateTime(profile, j.date, st0.dTimeR, st0.dTZOffset)
|
2018-10-15 15:50:43 +02:00
|
|
|
when = realtime
|
|
|
|
delay = Math.round((new Date(realtime) - new Date(planned)) / 1000)
|
|
|
|
} else if (planned) when = planned
|
2018-12-02 01:15:46 +01:00
|
|
|
}
|
2017-11-20 15:43:13 +01:00
|
|
|
return {
|
2018-12-02 01:15:46 +01:00
|
|
|
tripId: a.jid,
|
2017-11-20 15:43:13 +01:00
|
|
|
line: lines[parseInt(a.prodX)] || null,
|
2018-12-02 01:15:46 +01:00
|
|
|
direction: a.dirTxt || null,
|
|
|
|
when, delay
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-17 17:14:47 +01:00
|
|
|
// todo: DRY with parseDeparture
|
|
|
|
// todo: DRY with parseStopover
|
|
|
|
if (pt.arr.aCncl || pt.dep.dCncl) {
|
2017-12-18 21:11:35 +01:00
|
|
|
res.cancelled = true
|
2018-03-08 03:26:39 +01:00
|
|
|
Object.defineProperty(res, 'canceled', {value: true})
|
2018-03-17 17:14:47 +01:00
|
|
|
if (pt.arr.aCncl) {
|
|
|
|
res.arrival = res.arrivalPlatform = res.arrivalDelay = null
|
2018-10-15 16:21:29 +02:00
|
|
|
const arr = profile.parseDateTime(profile, j.date, pt.arr.aTimeS, pt.arr.aTZOffset)
|
2019-02-14 14:04:31 +01:00
|
|
|
res.scheduledArrival = arr
|
2018-03-17 17:14:47 +01:00
|
|
|
}
|
|
|
|
if (pt.dep.dCncl) {
|
|
|
|
res.departure = res.departurePlatform = res.departureDelay = null
|
2018-10-15 16:21:29 +02:00
|
|
|
const dep = profile.parseDateTime(profile, j.date, pt.dep.dTimeS, pt.dep.dTZOffset)
|
2019-02-14 14:04:31 +01:00
|
|
|
res.scheduledDeparture = dep
|
2018-03-17 17:14:47 +01:00
|
|
|
}
|
2017-12-07 20:21:14 +01:00
|
|
|
}
|
|
|
|
|
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
|