-
Notifications
You must be signed in to change notification settings - Fork 13
/
MMM-vvsDeparture.js
executable file
·209 lines (176 loc) · 5.48 KB
/
MMM-vvsDeparture.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* global Module */
/* Magic Mirror
* Module: MMM-vvsDeparture
*
* By niklaskappler
* MIT Licensed.
*/
Module.register("MMM-vvsDeparture", {
defaults: {
station_id: 'de:08111:6112',
station_name: "",
maximumEntries: 6,
reloadInterval: 1 * 60 * 1000, // every minute
colorDelay: true,
colorNoDelay: true,
number: undefined,
direction: undefined,
offset: undefined
},
requiresVersion: "2.1.0", // Required version of MagicMirror
// Define required scripts.
getStyles: function () {
return ["MMM-vvsDeparture.css"];
},
// Define required scripts.
getScripts: function () {
return ["moment.js"];
},
// Load translations files
getTranslations: function() {
return {
en: "translations/en.json",
de: "translations/de.json"
};
},
// Overrides start function.
start: function () {
var self = this;
Log.log("Starting module: " + self.name + "as" + self.identifier);
self.departure = [];
self.sendSocketNotification("GET_DEPARTURES",
{
"config": self.config,
"identifier": this.identifier,
});
},
// socketNotificationReceived from helper
socketNotificationReceived: function (notification, payload) {
var self = this;
if (notification === this.identifier+"_NEW_DEPARTURES") {
self.departure = payload.stopEvents;
self.station_name = self.config.station_name ? self.config.station_name : payload.locations[0].disassembledName;
self.updateDom();
}
},
// Override dom generator.
getDom: function () {
var self = this;
var wrapper = document.createElement("div");
wrapper.appendChild(self.getHeaderDom());
var tableWrapper = document.createElement("table");
tableWrapper.className = "departure";
var added = 0;
for (var i in self.departure) {
// If the maximum number of entries is reached
// stop adding and attach table
if (added >= self.config.maximumEntries) {
wrapper.appendChild(tableWrapper);
return wrapper;
}
var currentValue = self.departure[i];
// Skip if the configuration should hide this
if(!self.showNumber(currentValue.transportation.number) || !self.showDirection(currentValue.transportation.destination.name)) {
continue;
}
// Row
var trWrapper = document.createElement("tr");
// Clock
var clockWrapper = document.createElement("td");
clockWrapper.className = "time";
var date = new Date(currentValue.departureTimePlanned);
clockWrapper.innerHTML = moment(date.getHours() + ":" + date.getMinutes(), "HH:mm")
.subtract(currentValue.delay, "m")
.format("HH:mm");
trWrapper.appendChild(clockWrapper);
// Delay
var delayWrapper = document.createElement("td");
if("isRealtimeControlled" in currentValue && currentValue.isRealtimeControlled == true){
var delay = this.calculateDelay(currentValue.departureTimePlanned, currentValue.departureTimeEstimated);
}
if (delay.getMinutes() != 0) {
delayWrapper.className = "delay";
if (self.config.colorDelay) {
delayWrapper.className += " color";
}
} else {
delayWrapper.className = "nodelay";
if (self.config.colorNoDelay) {
delayWrapper.className += " color";
}
}
if(isNaN(delay.getMinutes())){
delayWrapper.innerHTML = self.translate("CANCELED");
} else {
delayWrapper.innerHTML = "+" +delay.getMinutes();
}
trWrapper.appendChild(delayWrapper);
// Lane
var laneWrapper = document.createElement("td");
laneWrapper.className = "number";
laneWrapper.innerHTML = currentValue.transportation.number;
trWrapper.appendChild(laneWrapper);
// Direction
var directionWrapper = document.createElement("td");
directionWrapper.className = "direction";
directionWrapper.innerHTML = currentValue.transportation.destination.name;
trWrapper.appendChild(directionWrapper);
trWrapper.className = "small dimmed";
tableWrapper.appendChild(trWrapper);
added++;
}
wrapper.appendChild(tableWrapper);
return wrapper;
},
stringTemplateParser: function (expression, valueObj) {
const templateMatcher = /{{\s?([^{}\s]*)\s?}}/g;
let text = expression.replace(templateMatcher, (substring, value, index) => {
value = valueObj[value];
return value;
});
return text
},
getHeaderDom: function () {
var self = this;
var headerWrappper = document.createElement("header");
if(self.config.offset && self.config.offset >= 0){
headerWrappper.innerHTML = self.stringTemplateParser(
self.translate("DIRECTIONS_FROM_WITH_OFFSET"),
{
STATION: self.station_name,
OFFSET: self.config.offset,
});
}else {
headerWrappper.innerHTML = self.stringTemplateParser(
self.translate("DIRECTIONS_FROM"),
{STATION: self.station_name});
}
return headerWrappper;
},
calculateDelay(departureTimePlanned, departureTimeEstimated){
timePlanned = new Date(departureTimePlanned);
timeEstimated = new Date(departureTimeEstimated);
var timeDiff = new Date(timeEstimated.getTime() - timePlanned.getTime());
return timeDiff
},
showNumber : function(number) {
var self = this;
return self.isValue(number, self.config.number);
},
showDirection : function(direction) {
var self = this;
return self.isValue(direction, self.config.direction);
},
isValue : function(input, value) {
if(!value || !input) {
return true;
} else if(value instanceof Array) {
return value.indexOf(input) >= 0;
} else if (typeof value === "string" || value instanceof String) {
return value === input;
} else if(typeof value === "function" || value instanceof Function) {
return value(input);
}
return false;
}
});