2016-06-22 02:09:02 +02:00
|
|
|
|
'use strict'
|
|
|
|
|
|
2016-06-25 20:54:33 +02:00
|
|
|
|
const moment = require('moment-timezone')
|
2017-04-25 01:57:09 +02:00
|
|
|
|
const slugg = require('slugg')
|
2016-06-25 20:54:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-06-26 18:16:10 +02:00
|
|
|
|
const dateTime = (tz, date, time) => {
|
2016-06-25 20:54:33 +02:00
|
|
|
|
let offset = 0 // in days
|
|
|
|
|
if (time.length > 6) {
|
|
|
|
|
offset = +time.slice(0, -6)
|
|
|
|
|
time = time.slice(-6)
|
|
|
|
|
}
|
2016-06-26 18:16:10 +02:00
|
|
|
|
return moment.tz(date + 'T' + time, tz)
|
2016-06-25 20:54:33 +02:00
|
|
|
|
.add(offset, 'days')
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-22 02:09:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const types = {P: 'poi', S: 'station', A: 'address'}
|
|
|
|
|
// todo: what is s.rRefL?
|
|
|
|
|
const location = (l) => {
|
|
|
|
|
const type = types[l.type] || 'unknown'
|
|
|
|
|
const result = {
|
2017-04-25 01:57:09 +02:00
|
|
|
|
type,
|
|
|
|
|
name: l.name,
|
|
|
|
|
coordinates: l.crd ? {
|
|
|
|
|
latitude: l.crd.y / 1000000,
|
|
|
|
|
longitude: l.crd.x / 1000000
|
|
|
|
|
} : null
|
2016-06-22 02:09:02 +02:00
|
|
|
|
}
|
|
|
|
|
if (type === 'poi' || type === 'station') result.id = parseInt(l.extId)
|
|
|
|
|
if ('pCls' in l) result.products = l.pCls
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// todo: what is p.number vs p.line?
|
|
|
|
|
// todo: what is p.icoX?
|
|
|
|
|
// todo: what is p.oprX?
|
2017-05-16 01:30:43 +02:00
|
|
|
|
const line = (p) => {
|
2016-08-12 15:26:44 +02:00
|
|
|
|
if (!p) return null
|
2017-05-16 01:30:43 +02:00
|
|
|
|
const result = {type: 'line', name: p.line || p.name}
|
|
|
|
|
if (p.cls) result.class = p.cls
|
2016-08-12 17:41:28 +02:00
|
|
|
|
if (p.prodCtx) {
|
|
|
|
|
result.productCode = +p.prodCtx.catCode
|
|
|
|
|
result.productName = p.prodCtx.catOutS
|
2016-06-22 02:09:02 +02:00
|
|
|
|
}
|
2016-08-12 17:41:28 +02:00
|
|
|
|
return result
|
2016-06-22 02:09:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const remark = (r) => null // todo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-25 01:57:09 +02:00
|
|
|
|
const operator = (a) => ({
|
|
|
|
|
type: 'operator',
|
|
|
|
|
id: slugg(a.name),
|
|
|
|
|
name: a.name
|
|
|
|
|
})
|
2016-06-26 18:16:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-05-16 01:30:43 +02:00
|
|
|
|
// s = stations, ln = lines, r = remarks, c = connection
|
2017-05-18 13:14:05 +02:00
|
|
|
|
const stopover = (tz, s, ln, r, c) => (st) => {
|
2016-06-27 23:03:26 +02:00
|
|
|
|
const result = {station: s[parseInt(st.locX)]}
|
2017-04-25 01:57:09 +02:00
|
|
|
|
if (st.aTimeR || st.aTimeS) {
|
2017-05-16 13:11:43 +02:00
|
|
|
|
result.arrival = dateTime(tz, c.date, st.aTimeR || st.aTimeS).format()
|
2017-04-25 01:57:09 +02:00
|
|
|
|
}
|
|
|
|
|
if (st.dTimeR || st.dTimeS) {
|
2017-05-16 13:11:43 +02:00
|
|
|
|
result.departure = dateTime(tz, c.date, st.dTimeR || st.dTimeS).format()
|
2017-04-25 01:57:09 +02:00
|
|
|
|
}
|
2016-06-27 23:03:26 +02:00
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// todo: finish parseRemark first
|
2017-05-16 01:30:43 +02:00
|
|
|
|
// s = stations, ln = lines, r = remarks, c = connection
|
|
|
|
|
const applyRemark = (s, ln, r, c) => (rm) => null
|
2016-06-27 23:03:26 +02:00
|
|
|
|
|
|
|
|
|
// todo: pt.sDays
|
|
|
|
|
// todo: pt.dep.dProgType, pt.arr.dProgType
|
|
|
|
|
// todo: what is pt.jny.dirFlg?
|
|
|
|
|
// todo: how does pt.freq work?
|
2017-05-16 01:30:43 +02:00
|
|
|
|
// s = stations, ln = lines, r = remarks, c = connection
|
|
|
|
|
const part = (tz, s, ln, r, c) => (pt) => {
|
2016-06-27 23:03:26 +02:00
|
|
|
|
const result = {
|
2017-04-25 01:57:09 +02:00
|
|
|
|
origin: Object.assign({}, s[parseInt(pt.dep.locX)])
|
|
|
|
|
, destination: Object.assign({}, s[parseInt(pt.arr.locX)])
|
2017-05-16 13:11:43 +02:00
|
|
|
|
, departure: dateTime(tz, c.date, pt.dep.dTimeR || pt.dep.dTimeS).format()
|
|
|
|
|
, arrival: dateTime(tz, c.date, pt.arr.aTimeR || pt.arr.aTimeS).format()
|
2016-06-27 23:03:26 +02:00
|
|
|
|
}
|
2017-05-16 13:11:43 +02:00
|
|
|
|
if (pt.dep.dTimeR && pt.dep.dTimeS) {
|
|
|
|
|
const realtime = dateTime(tz, c.date, pt.dep.dTimeR)
|
|
|
|
|
const planned = dateTime(tz, c.date, pt.dep.dTimeS)
|
2017-05-16 17:48:57 +02:00
|
|
|
|
result.delay = Math.round((realtime - planned) / 1000)
|
2017-05-16 13:11:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-25 01:57:09 +02:00
|
|
|
|
if (pt.type === 'WALK') result.mode = 'walking'
|
2016-06-27 23:03:26 +02:00
|
|
|
|
else if (pt.type === 'JNY') {
|
2017-05-16 01:30:43 +02:00
|
|
|
|
result.line = ln[parseInt(pt.jny.prodX)]
|
2016-06-27 23:03:26 +02:00
|
|
|
|
result.direction = pt.jny.dirTxt // todo: parse this
|
2017-04-20 13:56:39 +02:00
|
|
|
|
|
2017-04-25 01:57:09 +02:00
|
|
|
|
if (pt.dep.dPlatfS) result.departurePlatform = pt.dep.dPlatfS
|
|
|
|
|
if (pt.arr.aPlatfS) result.arrivalPlatform = pt.arr.aPlatfS
|
2017-04-20 13:56:39 +02:00
|
|
|
|
|
2017-05-18 13:14:05 +02:00
|
|
|
|
if (pt.jny.stopL) result.passed = pt.jny.stopL.map(stopover(tz, s, ln, r, c))
|
2016-07-18 19:21:31 +02:00
|
|
|
|
if (Array.isArray(pt.jny.remL))
|
2017-05-16 01:30:43 +02:00
|
|
|
|
pt.jny.remL.forEach(applyRemark(s, ln, r, c))
|
2016-06-27 23:03:26 +02:00
|
|
|
|
|
|
|
|
|
if (pt.jny.freq && pt.jny.freq.jnyL)
|
|
|
|
|
result.alternatives = pt.jny.freq.jnyL
|
|
|
|
|
.filter((a) => a.stopL[0].locX === pt.dep.locX)
|
|
|
|
|
.map((a) => ({
|
2017-05-16 01:30:43 +02:00
|
|
|
|
line: ln[parseInt(a.prodX)],
|
2017-05-16 13:11:43 +02:00
|
|
|
|
when: dateTime(tz, c.date, a.stopL[0].dTimeS).format()
|
2016-06-27 23:03:26 +02:00
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// todo: c.sDays
|
|
|
|
|
// todo: c.dep.dProgType, c.arr.dProgType
|
|
|
|
|
// todo: c.conSubscr
|
|
|
|
|
// todo: c.trfRes x vbb-parse-ticket
|
|
|
|
|
// todo: use computed information from part
|
2017-05-16 01:30:43 +02:00
|
|
|
|
// s = stations, ln = lines, r = remarks
|
2017-05-18 13:14:05 +02:00
|
|
|
|
const journey = (tz, s, ln, r) => (c) => {
|
2017-05-16 01:30:43 +02:00
|
|
|
|
const parts = c.secL.map(part(tz, s, ln, r, c))
|
2016-06-27 23:03:26 +02:00
|
|
|
|
return {
|
|
|
|
|
parts
|
2017-05-09 16:39:26 +02:00
|
|
|
|
, origin: parts[0].origin
|
|
|
|
|
, destination: parts[0].destination
|
|
|
|
|
, departure: parts[parts.length - 1].departure
|
|
|
|
|
, arrival: parts[parts.length - 1].arrival
|
2016-06-27 23:03:26 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-26 18:16:10 +02:00
|
|
|
|
|
|
|
|
|
// todo: what is d.jny.dirFlg?
|
|
|
|
|
// todo: d.stbStop.dProgType
|
|
|
|
|
// todo: what is d.stbStop.dTimeR?
|
2017-05-16 01:30:43 +02:00
|
|
|
|
// tz = timezone, s = stations, ln = lines, r = remarks
|
|
|
|
|
const departure = (tz, s, ln, r) => (d) => {
|
2016-06-26 18:16:10 +02:00
|
|
|
|
const result = {
|
|
|
|
|
station: s[parseInt(d.stbStop.locX)]
|
2017-05-16 13:11:43 +02:00
|
|
|
|
, when: dateTime(tz, d.date, d.stbStop.dTimeR || d.stbStop.dTimeS).format()
|
2016-06-27 23:03:26 +02:00
|
|
|
|
, direction: d.dirTxt
|
2017-05-16 01:30:43 +02:00
|
|
|
|
, line: ln[parseInt(d.prodX)]
|
2017-05-16 13:11:43 +02:00
|
|
|
|
, remarks: d.remL ? d.remL.map((rm) => r[parseInt(rm.remX)]) : null
|
|
|
|
|
, trip: +d.jid.split('|')[1]
|
|
|
|
|
}
|
|
|
|
|
if (d.stbStop.dTimeR && d.stbStop.dTimeS) {
|
|
|
|
|
const realtime = dateTime(tz, d.date, d.stbStop.dTimeR)
|
|
|
|
|
const planned = dateTime(tz, d.date, d.stbStop.dTimeS)
|
2017-05-16 17:48:57 +02:00
|
|
|
|
result.delay = Math.round((realtime - planned) / 1000)
|
2016-06-26 18:16:10 +02:00
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
2016-06-27 22:13:52 +02:00
|
|
|
|
|
|
|
|
|
// todo: remarks
|
2017-05-16 01:30:43 +02:00
|
|
|
|
// todo: lines
|
2016-06-27 22:13:52 +02:00
|
|
|
|
// todo: what is s.pCls?
|
|
|
|
|
// todo: what is s.wt?
|
|
|
|
|
// todo: what is s.dur?
|
|
|
|
|
const nearby = (n) => {
|
|
|
|
|
const result = location(n)
|
|
|
|
|
result.distance = n.dist
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-12 03:41:10 +02:00
|
|
|
|
// todo: what is m.dirGeo? maybe the speed?
|
|
|
|
|
// todo: what is m.stopL?
|
|
|
|
|
// todo: what is m.proc? wut?
|
|
|
|
|
// todo: what is m.pos?
|
|
|
|
|
// todo: what is m.ani.dirGeo[n]? maybe the speed?
|
|
|
|
|
// todo: what is m.ani.proc[n]? wut?
|
|
|
|
|
// todo: how does m.ani.poly work?
|
2017-05-16 01:30:43 +02:00
|
|
|
|
// tz = timezone, l = locations, ln = lines, r = remarks
|
|
|
|
|
const movement = (tz, l, ln, r) => (m) => {
|
2016-08-12 03:41:10 +02:00
|
|
|
|
const result = {
|
|
|
|
|
direction: m.dirTxt
|
2017-05-16 01:30:43 +02:00
|
|
|
|
, line: ln[m.prodX]
|
2017-04-25 01:57:09 +02:00
|
|
|
|
, coordinates: m.pos ? {
|
|
|
|
|
latitude: m.pos.y / 1000000,
|
|
|
|
|
longitude: m.pos.x / 1000000
|
|
|
|
|
} : null
|
2016-08-12 03:41:10 +02:00
|
|
|
|
, nextStops: m.stopL.map((s) => ({
|
|
|
|
|
station: l[s.locX]
|
2017-04-25 01:57:09 +02:00
|
|
|
|
, departure: s.dTimeR || s.dTimeS
|
2017-05-16 13:11:43 +02:00
|
|
|
|
? dateTime(tz, m.date, s.dTimeR || s.dTimeS).format()
|
2017-04-25 01:57:09 +02:00
|
|
|
|
: null
|
|
|
|
|
, arrival: s.aTimeR || s.aTimeS
|
2017-05-16 13:11:43 +02:00
|
|
|
|
? dateTime(tz, m.date, s.aTimeR || s.aTimeS).format()
|
2017-04-25 01:57:09 +02:00
|
|
|
|
: null
|
2016-08-12 03:41:10 +02:00
|
|
|
|
}))
|
|
|
|
|
, frames: []
|
|
|
|
|
}
|
|
|
|
|
if (m.ani && Array.isArray(m.ani.mSec))
|
|
|
|
|
for (let i = 0; i < m.ani.mSec.length; i++)
|
|
|
|
|
result.frames.push({
|
2017-05-16 03:44:53 +02:00
|
|
|
|
origin: l[m.ani.fLocX[i]],
|
|
|
|
|
destination: l[m.ani.tLocX[i]],
|
2016-08-12 03:41:10 +02:00
|
|
|
|
t: m.ani.mSec[i]
|
|
|
|
|
})
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-27 22:13:52 +02:00
|
|
|
|
|
|
|
|
|
|
2016-06-22 02:09:02 +02:00
|
|
|
|
module.exports = {
|
|
|
|
|
dateTime,
|
2017-05-16 01:30:43 +02:00
|
|
|
|
location, line, remark, operator,
|
2017-05-18 13:14:05 +02:00
|
|
|
|
stopover, applyRemark, part, journey,
|
2016-06-27 22:13:52 +02:00
|
|
|
|
departure,
|
2016-08-12 03:41:10 +02:00
|
|
|
|
nearby,
|
|
|
|
|
movement
|
2016-06-22 02:09:02 +02:00
|
|
|
|
}
|