2016-06-22 02:09:02 +02:00
|
|
|
|
'use strict'
|
|
|
|
|
|
2016-06-25 20:54:33 +02:00
|
|
|
|
const moment = require('moment-timezone')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
.valueOf()
|
|
|
|
|
}
|
|
|
|
|
|
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 = {
|
|
|
|
|
type, name: l.name
|
|
|
|
|
, latitude: l.crd ? l.crd.y / 1000000 : null
|
|
|
|
|
, longitude: l.crd ? l.crd.x / 1000000 : null
|
|
|
|
|
}
|
|
|
|
|
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.cls?
|
|
|
|
|
// todo: what is p.oprX?
|
|
|
|
|
const product = (p) => {
|
|
|
|
|
if (!p.prodCtx) return null
|
|
|
|
|
return {
|
|
|
|
|
name: p.name, nr: +p.number, class: p.cls,
|
|
|
|
|
productCode: +p.prodCtx.catCode, productName: p.prodCtx.catOutS
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const remark = (r) => null // todo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const agency = (a) => a.name
|
2016-06-26 18:16:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-06-27 23:03:26 +02:00
|
|
|
|
// s = stations, p = products, r = remarks, c = connection
|
|
|
|
|
const stop = (tz, s, p, r, c) => (st) => {
|
|
|
|
|
const result = {station: s[parseInt(st.locX)]}
|
|
|
|
|
if (st.aTimeR || st.aTimeS) result.arrival =
|
|
|
|
|
new Date(dateTime(tz, c.date, st.aTimeR || st.aTimeS))
|
|
|
|
|
if (st.dTimeR || st.dTimeS) result.departure =
|
|
|
|
|
new Date(dateTime(tz, c.date, st.dTimeR || st.dTimeS))
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// todo: finish parseRemark first
|
|
|
|
|
// s = stations, p = products, r = remarks, c = connection
|
|
|
|
|
const applyRemark = (s, p, r, c) => (rm) => null
|
|
|
|
|
|
|
|
|
|
// todo: pt.sDays
|
|
|
|
|
// todo: pt.dep.dProgType, pt.arr.dProgType
|
|
|
|
|
// todo: what is pt.jny.dirFlg?
|
|
|
|
|
// todo: how does pt.freq work?
|
|
|
|
|
// s = stations, p = products, r = remarks, c = connection
|
|
|
|
|
const part = (tz, s, p, r, c) => (pt) => {
|
|
|
|
|
const result = {
|
|
|
|
|
from: s[parseInt(pt.dep.locX)]
|
|
|
|
|
, to: s[parseInt(pt.arr.locX)]
|
|
|
|
|
, start: new Date(dateTime(tz, c.date, pt.dep.dTimeR || pt.dep.dTimeS))
|
|
|
|
|
, end: new Date(dateTime(tz, c.date, pt.dep.aTimeR || pt.arr.aTimeS))
|
|
|
|
|
}
|
|
|
|
|
if (pt.type === 'WALK') result.type = 'walking'
|
|
|
|
|
else if (pt.type === 'JNY') {
|
|
|
|
|
result.product = p[parseInt(pt.jny.prodX)]
|
|
|
|
|
result.direction = pt.jny.dirTxt // todo: parse this
|
|
|
|
|
if (pt.jny.stopL) result.passed = pt.jny.stopL.map(stop(tz, s, p, r, c))
|
|
|
|
|
pt.jny.remL.forEach(applyRemark(s, p, r, c))
|
|
|
|
|
|
|
|
|
|
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) => ({
|
|
|
|
|
product: p[parseInt(a.prodX)],
|
|
|
|
|
when: new Date(dateTime(tz, c.date, a.stopL[0].dTimeS))
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
// s = stations, p = products, r = remarks
|
|
|
|
|
const route = (tz, s, p, r) => (c) => {
|
|
|
|
|
const parts = c.secL.map(part(tz, s, p, r, c))
|
|
|
|
|
return {
|
|
|
|
|
parts
|
|
|
|
|
, from: parts[0].from
|
|
|
|
|
, start: parts[0].start
|
|
|
|
|
, to: parts[parts.length - 1].to
|
|
|
|
|
, end: parts[parts.length - 1].end
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-26 18:16:10 +02:00
|
|
|
|
|
|
|
|
|
// todo: what is d.jny.dirFlg?
|
|
|
|
|
// todo: d.stbStop.dProgType
|
|
|
|
|
// todo: what is d.stbStop.dTimeR?
|
|
|
|
|
// tz = timezone, s = stations, p = products, r = remarks
|
|
|
|
|
const departure = (tz, s, p, r) => (d) => {
|
|
|
|
|
const result = {
|
|
|
|
|
station: s[parseInt(d.stbStop.locX)]
|
|
|
|
|
, when: new Date(dateTime(tz, d.date, d.stbStop.dTimeR || d.stbStop.dTimeS))
|
2016-06-27 23:03:26 +02:00
|
|
|
|
, direction: d.dirTxt
|
2016-06-26 18:16:10 +02:00
|
|
|
|
, product: p[parseInt(d.prodX)]
|
|
|
|
|
, remarks: d.remL ? d.remL.map((rm) => r[parseInt(rm.remX)]) : null
|
|
|
|
|
, trip: +d.jid.split('|')[1]
|
|
|
|
|
}
|
|
|
|
|
if (d.stbStop.dTimeR && d.stbStop.dTimeS) result.delay =
|
|
|
|
|
dateTime(tz, d.date, d.stbStop.dTimeR) - dateTime(tz, d.date, d.stbStop.dTimeS)
|
|
|
|
|
return result
|
|
|
|
|
}
|
2016-06-27 22:13:52 +02:00
|
|
|
|
|
|
|
|
|
// todo: remarks
|
|
|
|
|
// todo: products
|
|
|
|
|
// 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-06-22 02:09:02 +02:00
|
|
|
|
module.exports = {
|
|
|
|
|
dateTime,
|
2016-06-26 18:16:10 +02:00
|
|
|
|
location, product, remark, agency,
|
2016-06-27 23:03:26 +02:00
|
|
|
|
stop, applyRemark, part, route,
|
2016-06-27 22:13:52 +02:00
|
|
|
|
departure,
|
|
|
|
|
nearby
|
2016-06-22 02:09:02 +02:00
|
|
|
|
}
|