diff --git a/MMM-RNV.css b/MMM-RNV.css index e756b2b..ade7d75 100644 --- a/MMM-RNV.css +++ b/MMM-RNV.css @@ -2,6 +2,10 @@ color: #FF0000; } +.nodelay{ + color: #11aa41; +} + .rnvheader { text-align: left; } diff --git a/MMM-RNV.js b/MMM-RNV.js index 5da7379..132a529 100644 --- a/MMM-RNV.js +++ b/MMM-RNV.js @@ -8,7 +8,7 @@ */ Module.register('MMM-RNV',{ - + defaults: { apiKey: "", units: config.units, @@ -21,21 +21,25 @@ Module.register('MMM-RNV',{ initialLoadDelay: 0, // 0 seconds delay retryDelay: 2500, - apiBase: 'http://rnv.the-agent-factory.de:8080/easygo2/api', + apiBase: 'http://rnv.the-agent-factory.de:8080/easygo2/api', requestURL: '/regions/rnv/modules/stationmonitor/element', stationID: '', - + poleIDs: '', + walkingTimeOffset: 0, + numberOfShownDepartures: 10, + showZeroDelay: false, + iconTable: { "KOM": "fa fa-bus", "STRAB": "fa fa-subway" }, }, - + // Define required scripts. getScripts: function() { return ["moment.js", "font-awesome.css"]; }, - + getStyles: function() { return ['MMM-RNV.css']; }, @@ -65,18 +69,18 @@ Module.register('MMM-RNV',{ wrapper.innerHTML = this.translate('LOADING'); wrapper.className = "dimmed light small"; return wrapper; - } - + } + if (!this.departures.length) { wrapper.innerHTML = "No data"; wrapper.className = "dimmed light small"; return wrapper; } - + var table = document.createElement("table"); table.id = "rnvtable"; table.className = "small thin light"; - + var row = document.createElement("tr"); var timeHeader = document.createElement("th"); @@ -91,22 +95,28 @@ Module.register('MMM-RNV',{ var destinationHeader = document.createElement("th"); destinationHeader.innerHTML = "Fahrtrichtung"; destinationHeader.className = "rnvheader"; - row.appendChild(destinationHeader); + row.appendChild(destinationHeader); table.appendChild(row); - + for (var i in this.departures) { var currentDeparture = this.departures[i]; var row = document.createElement("tr"); table.appendChild(row); - + var cellDeparture = document.createElement("td"); cellDeparture.innerHTML = currentDeparture.time; cellDeparture.className = "timeinfo"; + if (currentDeparture.delay > 0) { var spanDelay = document.createElement("span"); spanDelay.innerHTML = ' +' + currentDeparture.delay; spanDelay.className = "small delay"; cellDeparture.appendChild(spanDelay); + } else if(currentDeparture.connectionIsLive && this.config.showZeroDelay) { + var spanDelay = document.createElement("span"); + spanDelay.innerHTML = ' +' + currentDeparture.delay; + spanDelay.className = "small nodelay"; + cellDeparture.appendChild(spanDelay); } row.appendChild(cellDeparture); @@ -116,19 +126,19 @@ Module.register('MMM-RNV',{ symbolTransportation.className = this.config.iconTable[currentDeparture.transportation]; cellTransport.appendChild(symbolTransportation); row.appendChild(cellTransport); - + var cellLine = document.createElement("td"); cellLine.innerHTML = currentDeparture.lineLabel; cellLine.className = "lineinfo"; row.appendChild(cellLine); - + var cellDirection = document.createElement("td"); cellDirection.innerHTML = currentDeparture.direction; cellDirection.className = "destinationinfo"; - row.appendChild(cellDirection); + row.appendChild(cellDirection); } wrapper.appendChild(table); - + if (this.ticker) { var marqueeTicker = document.createElement("marquee"); marqueeTicker.innerHTML = this.ticker; @@ -144,30 +154,50 @@ Module.register('MMM-RNV',{ if (!data.listOfDepartures) { return; } - + this.departures = []; this.ticker = data.ticker; + var iterations = 0; for (var i in data.listOfDepartures) { + if(iterations == this.config.numberOfShownDepartures && this.config.numberOfShownDepartures > 0 && this.config.numberOfShownDepartures <= 10 ){ + break; + } var t = data.listOfDepartures[i]; - if ((t.time).indexOf(' ') > 0) { // time contains a date because it is not today - t.time = (t.time).substring((t.time).indexOf(' ')+1, (t.time).length); + var delay = 0; // delay of the trasportation + var departure = 0; // departure time of the transportation + var connectionIsLive = false; + var now = moment(); + + if((t.time).includes('+')) { // if connection already started (showing delay, could be 0 as well) + connectionIsLive = true; + delay = (t.time).substring((t.time).indexOf('+') + 1, (t.time).length); // parse delay + t.time = (t.time).substring(0, (t.time).indexOf('+')); // cut off the delay } + + departure = moment(t.time, ["HH:mm", "DD.MM.YYYY HH:mm"]); + d1 = departure.clone(); // workaround because if-condition changes var departure (adds the delay) for unknown reason + if(moment.duration(d1.add(delay, 'm').diff(now)).as('minutes') <= this.config.walkingTimeOffset) { + continue; // skip this entry if transport is not reachable in time: (departure + delay - now) <= walkingTimeOffset) + } + this.departures.push({ - time: (t.time).substring(0,5), - delay: (((t.time).indexOf('+') > 0) ? (t.time).substring(6,(t.time).length) : 0), + time: departure.format('HH:mm'), + delay: delay, lineLabel: t.lineLabel, direction: t.direction, status: t.status, statusNote: t.statusNote, transportation: t.transportation, + connectionIsLive: connectionIsLive, }); - + + iterations++; } - + return; }, - + socketNotificationReceived: function(notification, payload) { if (notification === "STARTED") { this.updateDom(); @@ -177,6 +207,6 @@ Module.register('MMM-RNV',{ this.processDepartures(JSON.parse(payload)); this.updateDom(); } - } + } }); diff --git a/README.md b/README.md index 5d5153b..522e2af 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,6 @@ It monitors a given station in the RNV traffic network and shows the 10 upcoming Open a terminal session, navigate to your MagicMirror's `modules` folder and execute `git clone https://github.com/yawnsde/MMM-RNV.git`, a new folder called MMM-RNV will be created. Activate the module by adding it to the config.js file as shown below. Of course the position is up to you and the header is optional/customizable. -To find the id of your station take a look here: https://opendata.rnv-online.de/sites/default/files/Haltestellen_16.xml ## Using the module ````javascript @@ -22,4 +21,18 @@ modules: [ stationID: 'ENTER YOUR STATION ID HERE', } }, + ] ```` + +## Configuration options + +The following properties can be configured: + +| Option | Type | Description | Format | +| --- | --- | --- | --- | +| `apiKey` | String | Your personal API Key | 'abcdefghi123456' | +| `stationID` | String | The ID of your station. To find the ID of your station take a look here: https://opendata.rnv-online.de/sites/default/files/Haltestellen_16.xml | '1234' | +| `poleIDs` | String | The platform your transport leaves from. This can influence the direction shown. Get the different poles by executing `curl -H "RNV_API_TOKEN:" http://rnv.the-agent-factory.de:8080/easygo2/api/regions/rnv/modules/stations/detail?stationId=`. To get the connections leaving from this pole, fire `curl -H "RNV_API_TOKEN:" http://rnv.the-agent-factory.de:8080/easygo2/api/regions/rnv/modules/stationmonitor/element?hafasID=&time=null&poles=`| '1', for multiple poles: '1;5;6' | +| `walkingTimeOffset` | int | Time it takes you to reach your station in minutes. | 0 | +| `numberOfShownDepartures` | int | Number of shown departures. Has to be between 1 and 10. | 10 | +| `showZeroDelay` | bool | Shows delay for any connection which has already started even if 0. | false | diff --git a/node_helper.js b/node_helper.js index 0ecf4ea..b289c7e 100644 --- a/node_helper.js +++ b/node_helper.js @@ -21,9 +21,12 @@ module.exports = NodeHelper.create({ getData: function() { var self = this; - var currentDate = moment().format('YYYY-MM-DD+hh:mm:ss'); + var currentDate = moment().add(this.config.walkingTimeOffset, 'm').format('YYYY-MM-DD+HH:mm:ss'); var myUrl = this.config.apiBase + this.config.requestURL + '?hafasID=' + this.config.stationID + '&time=' + currentDate; - + if(this.config.poleIDs != '') { + myUrl += '&poles=' + this.config.poleIDs; + } + request({ url: myUrl, method: 'GET',