Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 3 new features #1

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions MMM-RNV.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
color: #FF0000;
}

.nodelay{
color: #11aa41;
}

.rnvheader {
text-align: left;
}
Expand Down
80 changes: 55 additions & 25 deletions MMM-RNV.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

Module.register('MMM-RNV',{

defaults: {
apiKey: "",
units: config.units,
Expand All @@ -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'];
},
Expand Down Expand Up @@ -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");
Expand All @@ -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);

Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -177,6 +207,6 @@ Module.register('MMM-RNV',{
this.processDepartures(JSON.parse(payload));
this.updateDom();
}
}
}

});
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:<apiKey>" http://rnv.the-agent-factory.de:8080/easygo2/api/regions/rnv/modules/stations/detail?stationId=<stationID>`. To get the connections leaving from this pole, fire `curl -H "RNV_API_TOKEN:<apiKey>" http://rnv.the-agent-factory.de:8080/easygo2/api/regions/rnv/modules/stationmonitor/element?hafasID=<stationID>&time=null&poles=<poleIDs>`| '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 |
7 changes: 5 additions & 2 deletions node_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down