diff --git a/lib/monitor.js b/lib/monitor.js index 750a10e..fff29d8 100644 --- a/lib/monitor.js +++ b/lib/monitor.js @@ -3,37 +3,121 @@ var requestP = require('request-promise'); var utils = require('./utils'); -var monitor = function monitor(stop, offset, amount, callback) { +function pointFinder(name, callback) { + // remove whitespace, vvo-online.de handles all kinds of requests pretty nicely - stop = utils.stripSpaces(stop); + var stopName = utils.stripSpaces(name); - // hst = name of bus- or tramstop, e.g. 'Helmholtzstraße' - // vz = time offset from now; trims the beginning of the array - // lim = limit the amount of results, max seems to be 31 - // var url = 'http://widgets.vvo-online.de/abfahrtsmonitor/Abfahrten.do?hst=' + stop + '&vz=' + offset + '&lim=' + amount; var options = { - url: 'http://widgets.vvo-online.de/abfahrtsmonitor/Abfahrten.do', - qs: { - hst: stop, - vz: offset, - lim: amount + url: 'https://webapi.vvo-online.de/tr/pointfinder?format=json', + qs: { + limit: 0, + query: stopName, + stopsOnly: true, + dvb: true, + assignedStops: true + } + } + + return requestP(options).then(function(response) { + var data = JSON.parse(response); + + // check status + if(data.Status !== 'undefined' && + data.Status.Code === 'Ok'&& + data.Points !== 'undefined') { + + for(var i=0; i < data.Points.length; i++) { + var p = data.Points[i].split('|'); + + // only and empty city string points to + // DVB (Dresden) stops + if(p[2].length === 0 || p[2] === 'Dresden') { + var stopid = p[0]; + + if(typeof(callback) === 'function') + callback(stopid); + + break; + } } - }; + } + }); +} + +function depatureMonitor(stopid, offset, amount, callback) { var now = new Date(); + var time = now; + if(offset !== 'undefined' && offset !== 0) + time = new Date(now.getTime() + (offset * 60 * 1000)); + + var options = { + url: 'https://webapi.vvo-online.de/dm?format=json', + qs: { + stopid: stopid, + time: time.toISOString(), + isarrival: false, + limit: amount, + shorttermchanges: true, + mentzonly: false + } + } + + return requestP(options).then(function(response) { + var data = JSON.parse(response); + var departures = []; + + // check status + if(data.Status !== 'undefined' && + data.Status.Code === 'Ok'&& + data.Departures !== 'undefined') { + + for(var i=0; i < data.Departures.length; i++) { + var d = data.Departures[i]; + + var arrivalTime; + if(d.RealTime) + arrivalTime = new Date(parseInt(d.RealTime.substr(6))); + else + arrivalTime = new Date(parseInt(d.ScheduledTime.substr(6))); + + var arrivalTimeRelative = Math.round((arrivalTime - now) / 1000 / 60); + + var scheduledTime = new Date(parseInt(d.ScheduledTime.substr(6))); + var scheduledTimeRelative = Math.round((scheduledTime - now) / 1000 / 60); + + var platform; + if(d.Platform) + platform = { name: d.Platform.Name, type: d.Platform.Type }; + + departures.push({ + line: d.LineName, + direction: d.Direction, + platform: platform, + arrivalTime: arrivalTime, + arrivalTimeRelative: arrivalTimeRelative, + scheduledTime: scheduledTime, + scheduledTimeRelative: scheduledTimeRelative, + delayTime: Math.round((arrivalTime - scheduledTime) / 1000 / 60), + state: d.State ? d.State : 'Unknown', + mode: utils.parseMode(d.Mot) + }); + } + + return departures; + } + }).nodeify(callback); +} + +var monitor = function monitor(stop, offset, amount, callback) { - return requestP(options) - .then(JSON.parse) - .map(function (transport) { - var arrivalTimeRelative = parseInt(transport[2]) || 0; - return { - line: transport[0], - direction: transport[1], - arrivalTimeRelative: arrivalTimeRelative, - arrivalTime: new Date(now.getTime() + 60000 * arrivalTimeRelative), - mode: utils.parseMode(transport[0]) - }; - }).nodeify(callback); + if(typeof(stop) === 'string') { + pointFinder(utils.stripSpaces(stop), function(stopid) { + return depatureMonitor(stopid, offset, amount, callback); + }); + } else + return depatureMonitor(stop, offset, amount, callback); }; module.exports = monitor; diff --git a/lib/utils.js b/lib/utils.js index ccea5d1..eb5c602 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -61,28 +61,8 @@ utils.parsePin = function parsePin(dataAsString, pinType) { }; }; -utils.parseMode = function parseMode(id) { - if (parseInt(id) != undefined) { - var intID = parseInt(id); - if (between(intID, 0, 59)) return utils.modes['tram']; - if (between(intID, 60, 99)) return utils.modes['citybus']; - if (between(intID, 100, 1000)) return utils.modes['regiobus']; - } - if (id === 'SWB' || id === 'STB') return utils.modes['lift']; - - if (matches(id, /^E\d+/)) { - var match = id.match(/^E(\d+)/); - if (match[1] <= 59) return utils.modes['tram']; - else return utils.modes['citybus']; - } - - if (matches(id, /^\D$|^\D\/\D$/)) return utils.modes['regiobus']; - if (matches(id, /^F/)) return utils.modes['ferry']; - if (matches(id, /^RE|^IC|^TL|^RB|^SB|^SE|^U\d/)) return utils.modes['train']; - if (matches(id, /^S/)) return utils.modes['metropolitan']; - if (matches(id, /alita/)) return utils.modes['ast']; - - return null; +utils.parseMode = function parseMode(name) { + return utils.modes[name.toLowerCase()]; } utils.modes = { @@ -96,19 +76,24 @@ utils.modes = { name: "citybus", icon_url: "https://www.dvb.de/assets/img/trans-icon/transport-bus.svg" }, - 'regiobus': { + 'intercitybus': { title: "Regionalbus", - name: "regiobus", + name: "intercitybus", icon_url: "https://www.dvb.de/assets/img/trans-icon/transport-bus.svg" }, - 'metropolitan': { + 'suburbanrailway': { title: "S-Bahn", - name: "metropolitan", + name: "SuburbanRailway", icon_url: "https://www.dvb.de/assets/img/trans-icon/transport-metropolitan.svg" }, - 'lift': { + 'train': { + title: "Zug", + name: "train", + icon_url: "https://www.dvb.de/assets/img/trans-icon/transport-train.svg" + }, + 'cableway': { title: "Seil-/Schwebebahn", - name: "lift", + name: "cableway", icon_url: "https://www.dvb.de/assets/img/trans-icon/transport-lift.svg" }, 'ferry': { @@ -116,15 +101,10 @@ utils.modes = { name: "ferry", icon_url: "https://www.dvb.de/assets/img/trans-icon/transport-ferry.svg" }, - 'ast': { + 'hailedsharedtaxi': { title: "Anrufsammeltaxi (AST)/ Rufbus", - name: "ast", + name: "hailedsharedtaxi", icon_url: "https://www.dvb.de/assets/img/trans-icon/transport-alita.svg" - }, - 'train': { - title: "Zug", - name: "train", - icon_url: "https://www.dvb.de/assets/img/trans-icon/transport-train.svg" } }; diff --git a/package.json b/package.json index 1e36bd9..7b77df9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dvbjs", - "version": "4.2.2", + "version": "4.3.0", "description": "query Dresden's public transport system for current bus- and tramstop data", "main": "index", "scripts": {