-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMMM-WienerLinien.js
213 lines (187 loc) · 5.83 KB
/
MMM-WienerLinien.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
/**
* @file MMM-WienerLinien.js
*
* @author fewieden
* @license MIT
*
* @see https://github.com/fewieden/MMM-WienerLinien
*/
/* global Module Log moment config */
/**
* @external Module
* @see https://github.com/MichMich/MagicMirror/blob/master/js/module.js
*/
/**
* @external Log
* @see https://github.com/MichMich/MagicMirror/blob/master/js/logger.js
*/
/**
* @external moment
* @see https://www.npmjs.com/package/moment
*/
/**
* @module MMM-WienerLinien
* @description Frontend for the module to display data.
*
* @requires external:Module
* @requires external:Log
* @requires external:moment
*/
Module.register('MMM-WienerLinien', {
/** @member {number} index - Is used to determine which station gets rendered. */
index: 0,
/** @member {Object} types - Mapping of transportation types to icons. */
types: {
ptBusCity: 'fa-bus',
ptTram: 'fa-train',
ptTramWLB: 'fa-train',
ptMetro: 'fa-subway'
},
/**
* @member {Object} defaults - Defines the default config values.
* @property {int} max - Amount of departure times to display.
* @property {boolean|number} shortenStation - Maximum characters for station name.
* @property {boolean|number} shortenDestination - Maximum characters for destination name.
* @property {int} rotateInterval - Speed of rotation.
* @property {int} updateInterval - Speed of update.
* @property {string[]} elevatorStations - Station IDs that should be checked for elevator incidents.
* @property {string[]} incidentLines - Lines that should be checked for incidents.
* @property {boolean} incidentShort - Short or long incident description.
*/
defaults: {
max: 5,
shortenStation: false,
shortenDestination: false,
rotateInterval: 20 * 1000,
updateInterval: 5 * 60 * 1000,
elevatorStations: [],
incidentLines: [],
incidentShort: false
},
/**
* @function getTranslations
* @description Translations for this module.
* @override
*
* @returns {Object.<string, string>} Available translations for this module (key: language code, value: filepath).
*/
getTranslations() {
return {
en: 'translations/en.json',
de: 'translations/de.json'
};
},
/**
* @function getScripts
* @description Script dependencies for this module.
* @override
*
* @returns {string[]} List of the script dependency filepaths.
*/
getScripts() {
return ['moment.js'];
},
/**
* @function getStyles
* @description Style dependencies for this module.
* @override
*
* @returns {string[]} List of the style dependency filepaths.
*/
getStyles() {
return ['font-awesome.css', 'MMM-WienerLinien.css'];
},
/**
* @function getTemplate
* @description Nunjuck template.
* @override
*
* @returns {string} Path to nunjuck template.
*/
getTemplate() {
return 'templates/MMM-WienerLinien.njk';
},
/**
* @function getTemplateData
* @description Dynamic data that gets rendered in the nunjuck template.
* @override
*
* @returns {object} Data for the nunjuck template.
*/
getTemplateData() {
if (!this.stations) {
return {};
}
const keys = Object.keys(this.stations);
this.maxIndex = keys.length;
if (this.index >= this.maxIndex) {
this.index = 0;
}
const station = this.stations[keys[this.index]];
const { name, departures: allDepartures } = station;
const departures = allDepartures.slice(0, Math.min(allDepartures.length, this.config.max));
return {
departures,
name,
config: this.config,
elevators: this.elevators,
incidents: this.incidents
};
},
/**
* @function start
* @description Sets nunjuck filters and starts station rotation interval.
* @override
*
* @returns {void}
*/
start() {
Log.info(`Starting module: ${this.name}`);
moment.locale(config.language);
this.maxIndex = this.config.stations.length;
setInterval(() => {
this.updateDom(300);
this.index += 1;
if (this.index >= this.maxIndex) {
this.index = 0;
}
}, this.config.rotateInterval);
this.sendSocketNotification('CONFIG', this.config);
this.addFilters();
},
/**
* @function socketNotificationReceived
* @description Handles incoming messages from node_helper.
* @override
*
* @param {string} notification - Notification name
* @param {*} payload - Detailed payload of the notification.
*/
socketNotificationReceived(notification, payload) {
if (notification === 'STATIONS') {
this.stations = payload;
} else if (notification === 'ELEVATORS') {
this.elevators = payload;
} else if (notification === 'INCIDENTS') {
this.incidents = payload;
}
this.updateDom(300);
},
/**
* @function addFilters
* @description Adds custom filters used by the nunjuck template.
*
* @returns {void}
*/
addFilters() {
this.nunjucksEnvironment().addFilter('timeUntil', time => moment().to(time));
this.nunjucksEnvironment().addFilter('icon', type => this.types[type] || 'fa-question');
this.nunjucksEnvironment().addFilter('isEmpty', array => !array || array.length < 1);
this.nunjucksEnvironment().addFilter('shortenText', (text, maxLength) => {
if (!maxLength || text.length < maxLength) {
return text;
}
return `${text.slice(0, maxLength)}…`;
});
}
});