forked from raywo/MMM-PublicTransportHafas
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMMM-PublicTransportHafas.js
executable file
·314 lines (259 loc) · 12 KB
/
MMM-PublicTransportHafas.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* global dayjs PtDomBuilder Module Log config */
/*
* UserPresence Management (PIR sensor)
* (The variable UserPresence must currently still be declared with var, as several modules use this
* variable. If someone wants to change this, they would have to adapt the other modules as well.)
*/
// eslint-disable-next-line no-var
var UserPresence = true; // true by default, so no impact for user without a PIR sensor
Module.register("MMM-PublicTransportHafas", {
defaults: {
// Module misc
name: "MMM-PublicTransportHafas",
hafasProfile: "db", // Which profile should be used?
hidden: false,
updatesEvery: 120, // How often should the table be updated in s?
timeFormat: config.timeFormat, // Since we don't use moment.js, we need to handle the time format ourselves. This is the default time format of the mirror.
// Header
headerPrefix: "",
headerAppendix: "",
// Display last update time
displayLastUpdate: true, // Add line after the tasks with the last server update time
displayLastUpdateFormat: "dd - HH:mm:ss", // Format to display the last update. See dayjs.js documentation for all display possibilities
// Departures options
direction: "", // Show only departures heading to this station. (A station ID.)
ignoredLines: [], // Which lines should be ignored? (comma-separated list of line names)
ignoreRelatedStations: false, // For some stations there are related stations. By default, their departures are also displayed.
excludedTransportationTypes: [], // Which transportation types should not be shown on the mirror? (comma-separated list of types) possible values: "tram", "bus", "suburban", "subway", "regional" and "national"
timeToStation: 10, // How long do you need to walk to the Station? (in minutes)
timeInFuture: 40, // Show departures for the next *timeInFuture* minutes.
// Look and Feel
marqueeLongDirections: true, // Use Marquee effect for long station names?
replaceInDirections: {}, // key-value pairs which are used to replace `key` by `value` in the displayed directions
showColoredLineSymbols: true, // Want colored line symbols?
useColorForRealtimeInfo: true, // Want colored real time information (timeToStation, early)?
showAbsoluteTime: true, // How should the departure time be displayed? "15:10" (absolute) or "in 5 minutes" (relative)
noRealtimeDelayString: "+?", // Only relevant if 'showAbsoluteTime: true'. The string that is displayed as delay if no real-time departure time data is available.
showRelativeTimeOnlyUnder: 10 * 60 * 1000, // Display the time only relatively if the departure takes place in less than 10 minutes (600000 milliseconds). The value is only relevant if showAbsoluteTime: false.
showTableHeaders: true, // Show table headers?
showTableHeadersAsSymbols: true, // Table Headers as symbols or written?
showWarningRemarks: true, // Show warning remarks?
tableHeaderOrder: ["time", "line", "direction", "platform"], // In which order should the table headers appear?
maxUnreachableDepartures: 0, // How many unreachable departures should be shown?
maxReachableDepartures: 7, // How many reachable departures should be shown?
fadeUnreachableDepartures: true,
fadeReachableDepartures: true,
fadePointForReachableDepartures: 0.25,
customLineStyles: "", // Prefix for the name of the custom css file. ex: Leipzig-lines.css (case sensitive)
showOnlyLineNumbers: false, // Display only the line number instead of the complete name, i. e. "11" instead of "STR 11"
animationSpeed: 1_500 // Refresh animation speed in milliseconds
},
start () {
Log.info(`Starting module: ${this.name} with identifier: ${this.identifier}`);
this.ModulePublicTransportHafasHidden = false; // By default we display the module (if no carousel or other module)
this.updatesIntervalID = 0; // To stop and start auto update for each module instance
this.lastUpdate = 0; // Timestamp of the last module update. set at 0 at start-up
this.departures = [];
this.initialized = false;
this.error = {};
this.errorCount = 0;
this.sanitizeConfig();
if (!this.config.stationID) {
this.error.message = this.translate("NO_STATION_ID_SET");
return;
}
const fetcherOptions = {
identifier: this.identifier,
hafasProfile: this.config.hafasProfile,
stationID: this.config.stationID,
timeToStation: this.config.timeToStation,
timeInFuture: this.config.timeInFuture,
direction: this.config.direction,
ignoredLines: this.config.ignoredLines,
ignoreRelatedStations: this.config.ignoreRelatedStations,
excludedTransportationTypes: this.config.excludedTransportationTypes,
maxReachableDepartures: this.config.maxReachableDepartures,
maxUnreachableDepartures: this.config.maxUnreachableDepartures
};
this.sendSocketNotification("CREATE_FETCHER", fetcherOptions);
// Set locale
dayjs.locale(config.language);
},
suspend () {
// Core function called when the module is hidden
this.ModulePublicTransportHafasHidden = true; // Module hidden
Log.debug(`[MMM-PublicTransportHafas] Function suspend - Module is hidden ${this.config.stationName}`);
this.gestionUpdateIntervalHafas(); // Call the function which manages all the cases
},
resume () {
// Core function called when the module is displayed
this.ModulePublicTransportHafasHidden = false;
Log.debug(`[MMM-PublicTransportHafas] Function working - Module is displayed ${this.config.stationName}`);
this.gestionUpdateIntervalHafas();
},
notificationReceived (notification, payload) {
if (notification === "USER_PRESENCE") {
// Notification sent by the MMM-PIR-Sensor module. See its doc.
Log.debug(`[MMM-PublicTransportHafas] NotificationReceived USER_PRESENCE = ${payload}`);
UserPresence = payload;
this.gestionUpdateIntervalHafas();
}
},
gestionUpdateIntervalHafas () {
if (
UserPresence === true && this.ModulePublicTransportHafasHidden === false
) {
// Make sure to have a user present in front of the screen (PIR sensor) and that the module is displayed
Log.debug(`[MMM-PublicTransportHafas] ${this.config.stationName} is displayed and user present! Update it`);
// Update now and start again the update timer
this.startFetchingLoop(this.config.updatesEvery);
} else {
// (UserPresence = false OU ModulePublicTransportHafasHidden = true)
Log.debug(`[MMM-PublicTransportHafas] No one is watching: Stop the update!${this.config.stationName}`);
clearInterval(this.updatesIntervalID); // Stop the current update interval
this.updatesIntervalID = 0; // Reset the variable
}
},
getDom () {
const domBuilder = new PtDomBuilder(this.config);
// Error handling
if (this.hasErrors()) {
Log.error("[MMM-PublicTransportHafas]", this.error);
let errorMessage;
switch (this.error.code) {
case "ENOTFOUND":
// HAFAS endpoint not available
errorMessage = this.translate("ERROR_ENOTFOUND");
break;
case "NOT_FOUNDS":
// Station not found
errorMessage = this.translate("NOT_FOUND");
break;
default:
errorMessage = this.error.hafasMessage || this.error.code || this.error.message;
break;
}
Log.error("[MMM-PublicTransportHafas]", errorMessage.replace(/<br>/gu, " "));
errorMessage = `${this.translate("LOADING")}<br><br><small>⚠️ ${errorMessage}</small>`;
return domBuilder.getSimpleDom(errorMessage);
}
this.errorCount = 0;
if (!this.initialized) {
return domBuilder.getSimpleDom(this.translate("LOADING"));
}
const headings = {
time: this.translate("PTH_DEPARTURE_TIME"),
line: this.translate("PTH_LINE"),
direction: this.translate("PTH_TO"),
platform: this.translate("PTH_PLATFORM")
};
const noDeparturesMessage = this.translate("PTH_NO_DEPARTURES");
const wrapper = domBuilder.getDom(
this.departures,
headings,
noDeparturesMessage
);
// Display the update time at the end, if defined so by the user config
if (this.config.displayLastUpdate) {
const updateInfo = document.createElement("div");
updateInfo.className = "xsmall light align-left";
updateInfo.textContent = `Update: ${dayjs
.unix(this.lastUpdate)
.format(this.config.displayLastUpdateFormat)}`;
wrapper.appendChild(updateInfo);
}
return wrapper;
},
getStyles () {
const styles = [this.file("css/styles.css"), "font-awesome.css"];
if (this.config.customLineStyles !== "") {
const customStyle = `css/${this.config.customLineStyles}-lines.css`;
styles.push(this.file(customStyle));
}
return styles;
},
getScripts () {
return [
this.file("node_modules/dayjs/dayjs.min.js"),
this.file("node_modules/dayjs/plugin/localizedFormat.js"),
this.file("node_modules/dayjs/plugin/relativeTime.js"),
this.file(`node_modules/dayjs/locale/${config.language}.js`),
this.file("core/PtDomBuilder.js"),
this.file("core/PtTableBodyBuilder.js")
];
},
getTranslations () {
return {
en: "translations/en.json",
de: "translations/de.json"
};
},
socketNotificationReceived (notification, payload) {
if (this.isForThisStation(payload)) {
switch (notification) {
case "FETCHER_INITIALIZED":
this.initialized = true;
this.startFetchingLoop(this.config.updatesEvery);
break;
case "DEPARTURES_FETCHED":
if (this.config.displayLastUpdate) {
this.lastUpdate = Date.now() / 1_000; // Save the timestamp of the last update to be able to display it
}
Log.log(`[MMM-PublicTransportHafas] Update OK, station : ${
this.config.stationName
} at : ${Number(dayjs
.unix(this.lastUpdate)
.format(this.config.displayLastUpdateFormat))}`);
// Reset error object
this.error = {};
this.departures = payload.departures;
this.updateDom(this.config.animationSpeed);
this.sendNotification("TRANSPORT_HAFAS", payload.departures);
break;
case "FETCH_ERROR":
this.error = payload.error;
this.errorCount += 1;
this.departures = [];
// Only show the error message if it occurs 2 times in a row.
if (this.errorCount > 1) {
this.updateDom(this.config.animationSpeed);
}
break;
}
}
},
isForThisStation (payload) {
return payload.identifier === this.identifier;
},
sanitizeConfig () {
if (this.config.updatesEvery < 30) {
this.config.updatesEvery = 30;
}
if (this.config.timeToStation < 0) {
this.config.timeToStation = 0;
}
if (this.config.timeInFuture < this.config.timeToStation + 30) {
this.config.timeInFuture = this.config.timeToStation + 30;
}
if (this.config.maxUnreachableDepartures < 0) {
this.config.maxUnreachableDepartures = 0;
}
if (this.config.maxReachableDepartures < 0) {
this.config.maxReachableDepartures = this.defaults.maxReachableDepartures;
}
},
startFetchingLoop (interval) {
// Start immediately ...
this.sendSocketNotification("FETCH_DEPARTURES", this.identifier);
// ... and then repeat in the given interval
if (this.updatesIntervalID === 0) {
// If this instance as no auto update defined, then we create one. Otherwise : nothing.
this.updatesIntervalID = setInterval(() => {
this.sendSocketNotification("FETCH_DEPARTURES", this.identifier);
}, interval * 1_000);
}
},
hasErrors () {
return Object.keys(this.error).length > 0;
}
});