-
Notifications
You must be signed in to change notification settings - Fork 3
/
MMM-RNV.js
182 lines (151 loc) · 4.91 KB
/
MMM-RNV.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* global Module */
/* Magic Mirror
* Module: MMM-RNV
*
* By Stefan Krause http://yawns.de
* MIT Licensed.
*/
Module.register('MMM-RNV',{
defaults: {
apiKey: "",
units: config.units,
animationSpeed: 1000,
refreshInterval: 1000 * 60, //refresh every minute
updateInterval: 1000 * 3600, //update every hour
timeFormat: config.timeFormat,
lang: config.language,
initialLoadDelay: 0, // 0 seconds delay
retryDelay: 2500,
apiBase: 'http://rnv.the-agent-factory.de:8080/easygo2/api',
requestURL: '/regions/rnv/modules/stationmonitor/element',
stationID: '',
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'];
},
start: function() {
Log.info('Starting module: ' + this.name);
this.loaded = false;
this.sendSocketNotification('CONFIG', this.config);
},
getDom: function() {
var wrapper = document.createElement("div");
if (this.config.apiKey === "") {
wrapper.innerHTML = "No RNV <i>apiKey</i> set in config file.";
wrapper.className = "dimmed light small";
return wrapper;
}
if (this.config.stationID === "") {
wrapper.innerHTML = "No RNV <i>stationID</i> set in config file.";
wrapper.className = "dimmed light small";
return wrapper;
}
if (!this.loaded) {
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");
timeHeader.innerHTML = "Abfahrt";
timeHeader.className = "rnvheader";
row.appendChild(timeHeader);
var lineHeader = document.createElement("th");
lineHeader.innerHTML = "Linie";
lineHeader.className = "rnvheader";
lineHeader.colSpan = 2;
row.appendChild(lineHeader);
var destinationHeader = document.createElement("th");
destinationHeader.innerHTML = "Fahrtrichtung";
destinationHeader.className = "rnvheader";
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);
}
row.appendChild(cellDeparture);
var cellTransport = document.createElement("td");
cellTransport.className = "timeinfo";
var symbolTransportation = document.createElement("span");
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);
}
wrapper.appendChild(table);
if (this.ticker) {
var marqueeTicker = document.createElement("marquee");
marqueeTicker.innerHTML = this.ticker;
marqueeTicker.className = "small thin light";
marqueeTicker.width = document.getElementsByClassName("module MMM-RNV MMM-RNV")[0].offsetWidth;
wrapper.appendChild(marqueeTicker);
}
return wrapper;
},
processDepartures: function(data) {
if (!data.listOfDepartures) {
return;
}
this.departures = [];
this.ticker = data.ticker;
for (var i in data.listOfDepartures) {
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);
}
this.departures.push({
time: (t.time).substring(0,5),
delay: (((t.time).indexOf('+') > 0) ? (t.time).substring(6,(t.time).length) : 0),
lineLabel: t.lineLabel,
direction: t.direction,
status: t.status,
statusNote: t.statusNote,
transportation: t.transportation,
});
}
return;
},
socketNotificationReceived: function(notification, payload) {
if (notification === "STARTED") {
this.updateDom();
}
else if (notification === "DATA") {
this.loaded = true;
this.processDepartures(JSON.parse(payload));
this.updateDom();
}
}
});