forked from qistoph/MMM-nstreinen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nstreinen.js
204 lines (175 loc) · 5.39 KB
/
nstreinen.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
/* global Module, Log, moment */
Module.register("nstreinen", {
defaults: {
destination: null,
maxEntries: 5,
reloadInterval: 5 * 60 * 1000,
departureOffset: 0,
displaySymbol: true,
symbolMapping: {
"Intercity": "train",
"Intercity direct": "forward",
"Sprinter": "stop-circle",
"Stopbus i.p.v. trein": "bus",
"Snelbus i.p.v. trein": "bus",
"default": "train"
},
fade: true,
fadePoint: 0.25,
},
init: function() {
this.trains = undefined;
},
start: function() {
Log.info("Starting module: " + this.name);
var self = this;
setInterval(function() {
self.updateDom();
}, this.config.reloadInterval);
if (this.config.destination) {
this.addTrip(this.config.station, this.config.destination, this.config.user, this.config.pass, this.config.departureOffset, this.config.maxEntries, this.config.reloadInterval);
} else {
this.addStation(this.config.station, this.config.user, this.config.pass, this.config.reloadInterval);
}
},
getStyles: function() {
return ["nstreinen.css"];
},
getScripts: function() {
return ["moment.js"];
},
// Override socket notification handler.
socketNotificationReceived: function (notification, payload) {
//console.log(payload);
if (notification === "STATION_EVENTS") {
if (this.hasStation(payload.station) && this.hasDestination(null)) {
this.trains = payload.trains;
this.loaded = true;
this.error = null;
}
} else if (notification === "TRIP_EVENTS") {
if (this.hasStation(payload.station) && this.hasDestination(payload.destination)) {
this.trains = payload.trains;
this.loaded = true;
this.error = null;
}
} else if (notification === "FETCH_ERROR") {
Log.error("NSTreinen Error. Could not fetch api: " + payload.error);
this.error = payload.error;
} else if (notification === "INCORRECT_URL") {
Log.error("NSTreinen Error. Incorrect url: " + payload.url);
this.error = "Invalid URL";
} else {
Log.log("NSTreinen received an unknown socket notification: " + notification);
}
this.updateDom();
},
getDom: function() {
var wrapper;
if (this.error) {
wrapper = document.createElement("div");
wrapper.className = "small";
wrapper.innerText = this.error;
return wrapper;
}
var trains = this.createTrainsList();
wrapper = document.createElement("table");
wrapper.className = "small";
if (trains.length === 0) {
wrapper.innerHTML = (this.loaded) ? "No information" : "Loading...";
wrapper.className = "small dimmed";
return wrapper;
}
for (var t in trains) {
var train = trains[t];
var trainWrapper = document.createElement("tr");
trainWrapper.className = "normal";
if (this.config.displaySymbol) {
var symbolWrapper = document.createElement("td");
symbolWrapper.className = "symbol";
var symbol = document.createElement("span");
var symbolName = train.trainKind in this.config.symbolMapping ? this.config.symbolMapping[train.trainKind] : this.config.symbolMapping["default"];
symbol.className = "fa fa-"+symbolName;
symbolWrapper.appendChild(symbol);
trainWrapper.appendChild(symbolWrapper);
}
var titleWrapper = document.createElement("td");
titleWrapper.innerHTML = train.destination;
titleWrapper.className = "title";
trainWrapper.appendChild(titleWrapper);
if (train.destinationChanged) {
trainWrapper.className = "bright";
}
var timeWrapper = document.createElement("td");
timeWrapper.innerHTML = moment(train.departureTime).format("HH:mm");
if (train.departureDelay != 0) {
timeWrapper.innerHTML += "+" + train.departureDelay;
}
timeWrapper.className = "bright align-left";
trainWrapper.appendChild(timeWrapper);
var trackWrapper = document.createElement("td");
trackWrapper.innerHTML = train.track || "";
trackWrapper.className = "track";
if (train.trackChanged) {
trackWrapper.className = "bright";
}
if (train.cancelled) {
trainWrapper.style.textDecoration = "line-through";
}
trainWrapper.appendChild(trackWrapper);
wrapper.appendChild(trainWrapper);
// Create fade effect.
if (this.config.fade && this.config.fadePoint < 1) {
if (this.config.fadePoint < 0) {
this.config.fadePoint = 0;
}
var startingPoint = trains.length * this.config.fadePoint;
var steps = trains.length - startingPoint;
if (t >= startingPoint) {
var currentStep = t - startingPoint;
trainWrapper.style.opacity = 1 - (1 / steps * currentStep);
}
}
}
return wrapper;
},
addStation: function(station, user, pass, reloadInterval) {
this.sendSocketNotification("ADD_STATION", {
station: station,
user: user,
pass: pass,
reloadInterval: reloadInterval,
});
},
addTrip: function(station, destination, user, pass, departureOffset, maxEntries, reloadInterval) {
this.sendSocketNotification("ADD_TRIP", {
station: station,
destination: destination,
user: user,
pass: pass,
departureOffset: departureOffset,
maxEntries: maxEntries,
reloadInterval: reloadInterval
});
},
hasStation: function(station) {
if(this.config.station === station) {
return true;
}
return false;
},
hasDestination: function(destination) {
if (this.config.destination === destination) {
return true;
}
return false;
},
createTrainsList: function() {
var trains = this.trains;
if (trains === undefined) {
return [];
}
trains = trains.slice(0, this.config.maxEntries);
return trains;
}
});