diff --git a/index.js b/index.js index d31dd065..91d644f4 100644 --- a/index.js +++ b/index.js @@ -51,7 +51,7 @@ const createClient = (profile, request = _request) => { }) .then((d) => { if (!Array.isArray(d.jnyL)) return [] // todo: throw err? - const parse = profile.parseDeparture(profile, d.locations, d.lines, d.remarks) + const parse = profile.parseDeparture(profile, d.locations, d.lines, d.hints, d.warnings) return d.jnyL.map(parse) .sort((a, b) => new Date(a.when) - new Date(b.when)) }) @@ -164,7 +164,7 @@ const createClient = (profile, request = _request) => { if (!Array.isArray(d.outConL)) return [] const polylines = opt.polylines && d.common.polyL || [] - const parse = profile.parseJourney(profile, d.locations, d.lines, d.remarks, polylines) + const parse = profile.parseJourney(profile, d.locations, d.lines, d.hints, d.warnings, polylines) if (!journeys.earlierRef) journeys.earlierRef = d.outCtxScrB @@ -310,7 +310,7 @@ const createClient = (profile, request = _request) => { }) .then((d) => { const polylines = opt.polyline && d.common.polyL || [] - const parse = profile.parseJourneyLeg(profile, d.locations, d.lines, d.remarks, polylines) + const parse = profile.parseJourneyLeg(profile, d.locations, d.lines, d.hints, d.warnings, polylines) const leg = { // pretend the leg is contained in a journey type: 'JNY', @@ -364,7 +364,7 @@ const createClient = (profile, request = _request) => { if (!Array.isArray(d.jnyL)) return [] const polylines = opt.polyline && d.common.polyL || [] - const parse = profile.parseMovement(profile, d.locations, d.lines, d.remarks, polylines) + const parse = profile.parseMovement(profile, d.locations, d.lines, d.hints, d.warnings, polylines) return d.jnyL.map(parse) }) } diff --git a/lib/default-profile.js b/lib/default-profile.js index 57d7eb36..c7dbd27b 100644 --- a/lib/default-profile.js +++ b/lib/default-profile.js @@ -10,7 +10,8 @@ const parsePolyline = require('../parse/polyline') const parseMovement = require('../parse/movement') const parseNearby = require('../parse/nearby') const parseOperator = require('../parse/operator') -const parseRemark = require('../parse/remark') +const parseHint = require('../parse/hint') +const parseWarning = require('../parse/warning') const parseStopover = require('../parse/stopover') const formatAddress = require('../format/address') @@ -47,7 +48,8 @@ const defaultProfile = { parseMovement, parseNearby, parseOperator, - parseRemark, + parseHint, + parseWarning, parseStopover, formatAddress, diff --git a/lib/request.js b/lib/request.js index 7ecaabba..fe65257f 100644 --- a/lib/request.js +++ b/lib/request.js @@ -82,7 +82,11 @@ const request = (profile, data) => { const c = d.common || {} if (Array.isArray(c.remL)) { - d.remarks = c.remL.map(rem => profile.parseRemark(profile, rem)) + d.hints = c.remL.map(hint => profile.parseHint(profile, hint)) + } + d.warnings = [] + if (Array.isArray(c.himL)) { + d.warnings = c.himL.map(w => profile.parseWarning(profile, w)) } if (Array.isArray(c.opL)) { d.operators = c.opL.map(op => profile.parseOperator(profile, op)) diff --git a/lib/validate-profile.js b/lib/validate-profile.js index 88e3d0c1..38ed6cd8 100644 --- a/lib/validate-profile.js +++ b/lib/validate-profile.js @@ -20,7 +20,8 @@ const types = { parseMovement: 'function', parseNearby: 'function', parseOperator: 'function', - parseRemark: 'function', + parseHint: 'function', + parseWarning: 'function', parseStopover: 'function', formatAddress: 'function', diff --git a/p/db/index.js b/p/db/index.js index b8b4fc60..928f4989 100644 --- a/p/db/index.js +++ b/p/db/index.js @@ -34,8 +34,8 @@ const transformJourneysQuery = (query, opt) => { return query } -const createParseJourney = (profile, stations, lines, remarks, polylines) => { - const parseJourney = _createParseJourney(profile, stations, lines, remarks, polylines) +const createParseJourney = (profile, stations, lines, hints, polylines) => { + const parseJourney = _createParseJourney(profile, stations, lines, hints, polylines) // todo: j.sotRating, j.conSubscr, j.isSotCon, j.showARSLink, k.sotCtxt // todo: j.conSubscr, j.showARSLink, j.useableTime diff --git a/p/nahsh/index.js b/p/nahsh/index.js index 28d1a08d..bc691687 100644 --- a/p/nahsh/index.js +++ b/p/nahsh/index.js @@ -36,8 +36,8 @@ const parseLocation = (profile, l, lines) => { return res } -const createParseJourney = (profile, stations, lines, remarks) => { - const parseJourney = _createParseJourney(profile, stations, lines, remarks) +const createParseJourney = (profile, stations, lines, hints) => { + const parseJourney = _createParseJourney(profile, stations, lines, hints) const parseJourneyWithTickets = (j) => { const res = parseJourney(j) @@ -78,8 +78,8 @@ const createParseJourney = (profile, stations, lines, remarks) => { return parseJourneyWithTickets } -const createParseMovement = (profile, locations, lines, remarks) => { - const _parseMovement = _createParseMovement(profile, locations, lines, remarks) +const createParseMovement = (profile, locations, lines, hints) => { + const _parseMovement = _createParseMovement(profile, locations, lines, hints) const parseMovement = (m) => { const res = _parseMovement(m) // filter out empty nextStops entries diff --git a/p/oebb/index.js b/p/oebb/index.js index 99496761..c334e482 100644 --- a/p/oebb/index.js +++ b/p/oebb/index.js @@ -44,8 +44,8 @@ const parseLocation = (profile, l, lines) => { return res } -const createParseMovement = (profile, locations, lines, remarks) => { - const _parseMovement = _createParseMovement(profile, locations, lines, remarks) +const createParseMovement = (profile, locations, lines, hints) => { + const _parseMovement = _createParseMovement(profile, locations, lines, hints) const parseMovement = (m) => { const res = _parseMovement(m) // filter out POIs diff --git a/p/vbb/index.js b/p/vbb/index.js index 1366e49f..e293a6a1 100644 --- a/p/vbb/index.js +++ b/p/vbb/index.js @@ -56,8 +56,8 @@ const parseLocation = (profile, l, lines) => { return res } -const createParseJourney = (profile, stations, lines, remarks, polylines) => { - const parseJourney = _createParseJourney(profile, stations, lines, remarks, polylines) +const createParseJourney = (profile, stations, lines, hints, polylines) => { + const parseJourney = _createParseJourney(profile, stations, lines, hints, polylines) const parseJourneyWithTickets = (j) => { const res = parseJourney(j) @@ -86,8 +86,8 @@ const createParseJourney = (profile, stations, lines, remarks, polylines) => { return parseJourneyWithTickets } -const createParseDeparture = (profile, stations, lines, remarks) => { - const parseDeparture = _createParseDeparture(profile, stations, lines, remarks) +const createParseDeparture = (profile, stations, lines, hints) => { + const parseDeparture = _createParseDeparture(profile, stations, lines, hints) const ringbahnClockwise = /^ringbahn s\s?41$/i const ringbahnAnticlockwise = /^ringbahn s\s?42$/i diff --git a/package.json b/package.json index ad545fd1..ed3e4151 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ }, "dependencies": { "@mapbox/polyline": "^1.0.0", + "br2nl": "^1.0.0", "capture-stack-trace": "^1.0.0", "create-hash": "^1.2.0", "fetch-ponyfill": "^6.0.0", diff --git a/parse/departure.js b/parse/departure.js index 1a496bc7..4474ef8a 100644 --- a/parse/departure.js +++ b/parse/departure.js @@ -1,14 +1,14 @@ 'use strict' +const findRemark = require('./find-remark') + // todos from public-transport/hafas-client#2 // - stdStop.dPlatfS, stdStop.dPlatfR // todo: what is d.jny.dirFlg? // todo: d.stbStop.dProgType // todo: d.freq, d.freq.jnyL, see https://github.com/public-transport/hafas-client/blob/9203ed1481f08baacca41ac5e3c19bf022f01b0b/parse.js#L115 -const createParseDeparture = (profile, stations, lines, remarks) => { - const findRemark = rm => remarks[parseInt(rm.remX)] || null - +const createParseDeparture = (profile, stations, lines, hints, warnings) => { const parseDeparture = (d) => { const when = profile.parseDateTime(profile, d.date, d.stbStop.dTimeR || d.stbStop.dTimeS) const res = { @@ -17,7 +17,10 @@ const createParseDeparture = (profile, stations, lines, remarks) => { when: when.toISO(), direction: profile.parseStationName(d.dirTxt), line: lines[parseInt(d.prodX)] || null, - remarks: d.remL ? d.remL.map(findRemark) : [], + remarks: (d.remL + ? d.remL.map(ref => findRemark(hints, warnings, ref)) + : [] + ), trip: +d.jid.split('|')[1] // todo: this seems brittle } // todo: res.trip from rawLine.prodCtx.num? diff --git a/parse/find-remark.js b/parse/find-remark.js new file mode 100644 index 00000000..9bf89884 --- /dev/null +++ b/parse/find-remark.js @@ -0,0 +1,17 @@ +'use strict' + +// There are two kinds of notes: "remarks" (in `remL`) and HAFAS +// Information Manager (HIM) notes (in `himL`). The former describe +// the regular operating situation, e.g. "bicycles allows", whereas +// the latter describe cancellations, construction work, etc. + +// hafas-client's naming scheme: +// - hints: notes from `remL` for regular operation +// - warnings: notes from `himL` for cancellations, construction, etc +// - remarks: both "notes" and "warnings" + +const findRemark = (hints, warnings, ref) => { + return warnings[ref.himX] || hints[ref.remX] || null +} + +module.exports = findRemark diff --git a/parse/hint.js b/parse/hint.js new file mode 100644 index 00000000..bbc5a04e --- /dev/null +++ b/parse/hint.js @@ -0,0 +1,161 @@ +'use strict' + +const hints = Object.assign(Object.create(null), { + fb: { + type: 'hint', + code: 'bicycle-conveyance', + text: 'bicycles conveyed' + }, + nf: { + type: 'hint', + code: 'no-bicycle-conveyance', + text: 'bicycles not conveyed' + }, + k2: { + type: 'hint', + code: '2nd-class-only', + text: '2. class only' + }, + eh: { + type: 'hint', + code: 'boarding-ramp', + text: 'vehicle-mounted boarding ramp available' + }, + wv: { + type: 'hint', + code: 'wifi', + text: 'WiFi available' + }, + sn: { + type: 'hint', + code: 'snacks', + text: 'snacks available for purchase' + }, + bf: { + type: 'hint', + code: 'barrier-free', + text: 'barrier-free' + }, + rg: { + type: 'hint', + code: 'barrier-free-vehicle', + text: 'barrier-free vehicle' + }, + bt: { + type: 'hint', + code: 'on-board-bistro', + text: 'Bordbistro available' + }, + br: { + type: 'hint', + code: 'on-board-restaurant', + text: 'Bordrestaurant available' + }, + ki: { + type: 'hint', + code: 'childrens-area', + text: `children's area available` + }, + ls: { + type: 'hint', + code: 'power-sockets', + text: 'power sockets available' + }, + ev: { + type: 'hint', + code: 'replacement-service', + text: 'replacement service' + }, + kl: { + type: 'hint', + code: 'air-conditioned', + text: 'air-conditioned vehicle' + }, + r0: { + type: 'hint', + code: 'upward-escalator', + text: 'upward escalator' + }, + au: { + type: 'hint', + code: 'elevator', + text: 'elevator available' + } +}) + +// todo: is passing in profile necessary? +const parseHint = (profile, h) => { + // todo: C + + const text = h.txtN && h.txtN.trim() || '' + + if (h.type === 'M') { + return { + type: 'status', + code: h.code || null, + summary: h.txtS && h.txtS.trim() || '', + text + } + } + if (h.type === 'D') { + return { + type: 'status', + code: h.code || null, + text + } + } + + // todo: find sth more reliable than this + if (h.type === 'P' && text.toLowerCase() === 'journey cancelled') { + return { + type: 'status', + code: 'journey-cancelled', + text + // todo: `h.sIdx` + } + } + if (h.type === 'U' && text.toLowerCase() === 'stop cancelled') { + return { + type: 'status', + code: 'stop-cancelled', + text + } + } + // todo: + // { + // "type": "U", + // "code": "", + // "icoX": 3, + // "txtN": "entrance and exit not possible" + // } + if (h.type === 'U') { + return { + type: 'status', + code: h.code || null, + text + } + } + + if (h.type === 'L') { + return { + type: 'status', + code: 'alternative-trip', + text, + journeyId: h.jid + } + } + if (h.type === 'A') { + return hints[h.code && h.code.trim().toLowerCase()] || null + } + if (h.type === 'R') { + // todo: how can we identify the individual types? + return { + type: 'status', + code: h.code, + text + } + } + return null +} + +module.exports = parseHint diff --git a/parse/index.js b/parse/index.js index 11a5bd0f..e73bfb79 100644 --- a/parse/index.js +++ b/parse/index.js @@ -4,7 +4,7 @@ module.exports = { dateTime: require('./date-time'), location: require('./location'), line: require('./line'), - remark: require('./remark'), + hint: require('./hint'), operator: require('./operator'), stopover: require('./stopover'), journeyLeg: require('./journey-leg'), diff --git a/parse/journey-leg.js b/parse/journey-leg.js index d7e5eb77..e0378adc 100644 --- a/parse/journey-leg.js +++ b/parse/journey-leg.js @@ -1,13 +1,32 @@ 'use strict' const parseDateTime = require('./date-time') +const findRemark = require('./find-remark') const clone = obj => Object.assign({}, obj) -const createParseJourneyLeg = (profile, stations, lines, remarks, polylines) => { - // todo: finish parse/remark.js first - const applyRemark = (j, rm) => {} +const applyRemarks = (leg, hints, warnings, refs) => { + for (let ref of refs) { + const remark = findRemark(hints, warnings, ref) + if ('number' === typeof ref.fLocX && 'number' === typeof ref.tLocX) { + for (let i = ref.fLocX; i <= ref.tLocX; i++) { + const stopover = leg.passed[i] + if (Array.isArray(stopover.remarks)) { + stopover.remarks.push(remark) + } else { + stopover.remarks = [remark] + } + } + } else { + if (Array.isArray(leg.remarks)) leg.remarks.push(remark) + else leg.remarks = [remark] + } + // todo: `ref.tagL` + } +} +const createParseJourneyLeg = (profile, stations, lines, hints, warnings, polylines) => { + // todo: pt.status // todo: pt.sDays // todo: pt.dep.dProgType, pt.arr.dProgType // todo: what is pt.jny.dirFlg? @@ -15,6 +34,7 @@ const createParseJourneyLeg = (profile, stations, lines, remarks, polylines) => // todo: what is pt.himL? // j = journey, pt = part + // todo: pt.planrtTS const parseJourneyLeg = (j, pt, parseStopovers = true) => { const dep = profile.parseDateTime(profile, j.date, pt.dep.dTimeR || pt.dep.dTimeS) const arr = profile.parseDateTime(profile, j.date, pt.arr.aTimeR || pt.arr.aTimeS) @@ -63,9 +83,11 @@ const createParseJourneyLeg = (profile, stations, lines, remarks, polylines) => const stopovers = pt.jny.stopL.map(parse) // filter stations the train passes without stopping, as this doesn't comply with fptf (yet) res.stopovers = stopovers.filter((x) => !x.passBy) - } - if (Array.isArray(pt.jny.remL)) { - for (let remark of pt.jny.remL) applyRemark(j, remark) + + // todo: is there a `pt.jny.remL`? + if (Array.isArray(pt.jny.msgL)) { + applyRemarks(res, hints, warnings, pt.jny.msgL) + } } const freq = pt.jny.freq || {} diff --git a/parse/journey.js b/parse/journey.js index 24aaf8c8..fabcf15c 100644 --- a/parse/journey.js +++ b/parse/journey.js @@ -1,13 +1,20 @@ 'use strict' -const clone = obj => Object.assign({}, obj) +const findRemark = require('./find-remark') -const createParseJourney = (profile, stations, lines, remarks, polylines) => { - const parseLeg = profile.parseJourneyLeg(profile, stations, lines, remarks, polylines) +const createParseJourney = (profile, stations, lines, hints, warnings, polylines) => { + const parseLeg = profile.parseJourneyLeg(profile, stations, lines, hints, warnings, polylines) // todo: c.sDays // todo: c.conSubscr // todo: c.trfRes x vbb-parse-ticket + // todo: c.sotRating, c.isSotCon, c.sotCtxt + // todo: c.showARSLink + // todo: c.useableTime + // todo: c.cksum + // todo: c.isNotRdbl + // todo: c.badSecRefX + // todo: c.bfATS, c.bfIOSTS const parseJourney = (j) => { const legs = j.secL.map(leg => parseLeg(j, leg)) const res = { @@ -15,6 +22,14 @@ const createParseJourney = (profile, stations, lines, remarks, polylines) => { legs } + if (Array.isArray(j.msgL)) { + res.remarks = [] + for (let ref of j.msgL) { + const remark = findRemark(hints, warnings, ref) + if (remark) res.remarks.push(remark) + } + } + return res } diff --git a/parse/movement.js b/parse/movement.js index 8a3ab401..2aa48b84 100644 --- a/parse/movement.js +++ b/parse/movement.js @@ -1,6 +1,6 @@ 'use strict' -const createParseMovement = (profile, locations, lines, remarks, polylines = []) => { +const createParseMovement = (profile, locations, lines, hints, warnings, polylines = []) => { // todo: what is m.dirGeo? maybe the speed? // todo: what is m.stopL? // todo: what is m.proc? wut? @@ -8,7 +8,7 @@ const createParseMovement = (profile, locations, lines, remarks, polylines = []) // todo: what is m.ani.dirGeo[n]? maybe the speed? // todo: what is m.ani.proc[n]? wut? const parseMovement = (m) => { - const pStopover = profile.parseStopover(profile, locations, lines, remarks, m.date) + const pStopover = profile.parseStopover(profile, locations, lines, hints, warnings, m.date) const res = { direction: profile.parseStationName(m.dirTxt), diff --git a/parse/remark.js b/parse/remark.js deleted file mode 100644 index 5b173391..00000000 --- a/parse/remark.js +++ /dev/null @@ -1,7 +0,0 @@ -'use strict' - -const parseRemark = (profile, r) => { - return null // todo -} - -module.exports = parseRemark diff --git a/parse/stopover.js b/parse/stopover.js index 763b27e8..8856a982 100644 --- a/parse/stopover.js +++ b/parse/stopover.js @@ -1,6 +1,10 @@ 'use strict' -const createParseStopover = (profile, stations, lines, remarks, date) => { +const findRemark = require('./find-remark') + +// todo: arrivalDelay, departureDelay or only delay ? +// todo: arrivalPlatform, departurePlatform +const createParseStopover = (profile, stations, lines, hints, warnings, date) => { const parseStopover = (st) => { const res = { type: 'stopover', @@ -55,6 +59,14 @@ const createParseStopover = (profile, stations, lines, remarks, date) => { } } + if (Array.isArray(st.msgL)) { + res.remarks = [] + for (let ref of st.msgL) { + const remark = findRemark(hints, warnings, ref) + if (remark) res.remarks.push(remark) + } + } + return res } diff --git a/parse/warning.js b/parse/warning.js new file mode 100644 index 00000000..f6e1ce2f --- /dev/null +++ b/parse/warning.js @@ -0,0 +1,35 @@ +'use strict' + +const brToNewline = require('br2nl') + +const parseDateTime = require('./date-time') + +// todo: is passing in profile necessary? +const parseWarning = (profile, w) => { + // todo: hid, act, pub, lead, tckr, icoX, fLocX, tLocX, prod, comp, + // todo: cat (1, 2), pubChL + // pubChL: + // [ { name: 'timetable', + // fDate: '20180606', + // fTime: '073000', + // tDate: '20180713', + // tTime: '030000' }, + // { name: 'export', + // fDate: '20180606', + // fTime: '073000', + // tDate: '20180713', + // tTime: '030000' } ] + + return { + type: 'warning', + summary: brToNewline(w.head), + text: brToNewline(w.text), + priority: w.prio, + category: w.cat, // todo: parse to sth meaningful + validFrom: parseDateTime(profile, w.sDate, w.sTime).toISO(), + validUntil: parseDateTime(profile, w.eDate, w.eTime).toISO(), + modified: parseDateTime(profile, w.lModDate, w.lModTime).toISO() + } +} + +module.exports = parseWarning