-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMMM-RBB-Weather.js
346 lines (286 loc) · 10 KB
/
MMM-RBB-Weather.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/* Magic Mirror
* Module: MMM-RBB-Weather
*
* By Nikolai Keist (github.com/nkl-kst)
* MIT Licensed.
*/
Module.register('MMM-RBB-Weather', {
defaults: {
// Data
id: '10381', // Berlin-Steglitz
days: 4, // Including current, 0 = only current data
// Times
animationSpeed: 1, // 1 sec.
updateInterval: 600, // 10 min.
// Show / hide flags
showCurrentText: true,
showCurrentWindspeed: true,
showRainProbability: true,
showUpdateTime: false,
showWindspeed: false,
// Styling
animateCurrentIcon: true,
animateForecastIcon: false,
dayFormat: 'ddd',
splitCurrentTextGreater: 30,
tableClass: 'small',
whiteIcons: true,
// Trigger other modules
triggers: [
// eg. { day: 1, field: 'prr', value: 50, module: 'clock', hide: true }
]
},
// Requires Nunjucks support added in Magic Mirror 2.2.0
requiresVersion: '2.2.0',
// Instancevariable
weatherData: null,
updatedAt: null,
getScripts: function() {
return [
'moment.js',
'IconMapper.js'
];
},
getStyles: function() {
return [
'font-awesome.css',
'font-awesome5.css', // Only available in MagicMirror > 2.5.0
'weather-icons.css',
'weather-icons-wind.css',
'MMM-RBB-Weather.css'
];
},
getTranslations: function() {
return {
de: 'translations/de.json',
en: 'translations/en.json'
};
},
start: function() {
Log.info(`Starting module: ${this.name} ...`);
// Set locale
moment.locale(this.config.language);
},
notificationReceived: function(notification, payload, sender) {
// DOM ready
if (notification === 'MODULE_DOM_CREATED') {
// Initial data load
this.loadData();
}
},
socketNotificationReceived: function(notification, payload) {
// Data loaded with node helper
if (notification === 'DATA_LOADED') {
this.weatherData = payload.data;
this.updatedAt = payload.time;
// Update module
this.updateDom(this.config.animationSpeed * 1000);
// Trigger other modules
this.triggerModules();
}
},
/**
* getTemplate - Return the Nunjucks template that should be rendered. If there is no weather
* data available yet, the 'nodata' template is returned.
*
* @return {String} Template to render
*/
getTemplate: function() {
// No data available
if (this.weatherData === null || this.weatherData.length === 0) {
return 'templates/nodata.njk';
}
// Default module template
return 'templates/module.njk';
},
/**
* getTemplateData - Return the data that is included in the rendered Nunjucks remplate. The
* whole module instance is returned here, because several functions are called in the template.
*
* @return {Object} Data to put into templates
*/
getTemplateData: function() {
return { module: this };
},
/**
* getCurrentText - Return the formatted current weather text. This is
* split with a line break if the text contains a colon and length is
* greater then config.splitCurrentTextGreater.
*
* @param {String} text Current weather text
* @return {String} Formatted text
*/
getCurrentText: function(text) {
const splitValue = this.config.splitCurrentTextGreater;
// No text available
if (!text) {
return undefined;
}
// Check if text and flag are given
if (splitValue === 0) {
return text;
}
// Check if text is long enough to split
if (text.length <= splitValue) {
return text;
}
// Split and break at colons
return text.replace(/,\s*/, '<br/>');
},
/**
* getDayText - Return the formatted day text for the given day index. This is used for forecast
* days, so index 1 represents today, 2 represents tomorrow and so on.
*
* @param {Number} dayIndex Day index
* @return {String} Formatted day text
*/
getForecastDayText: function(dayIndex) {
const day = moment().add(dayIndex - 1, 'days');
return day.format(this.config.dayFormat);
},
/**
* getIconUrl - Return URL to the animated or static icon mapped to the given RBB icon.
*
* @param {Boolean} animate Use animated icons
* @param {String} rbbIcon RBB icon to be mapped
* @return {String} URL to mapped icon
*/
getIconUrl: function(animate, rbbIcon) {
// No current icon available
if (!rbbIcon || isNaN(rbbIcon)) {
return undefined;
}
// Icon path
const iconFolder = animate ? 'animated' : 'static';
const iconPath = IconMapper.getIconPath(rbbIcon, iconFolder);
// Fallback to RBB icons if no mapping was found
if (!iconPath) {
return `https://www.rbb24.de/basis/grafik/icons/wetter/svg/${rbbIcon}.svg`;
}
// Return icon url
return this.file(iconPath);
},
/**
* getTempIcon - Get thermometer icon depending on temperature value.
*
* @param {Number} temp Temperature value
* @return {String} Icon name
*/
getTempIcon: function(temp) {
if (temp >= 35) return 'fa-umbrella-beach';
if (temp >= 28) return 'fa-thermometer-full';
if (temp >= 21) return 'fa-thermometer-three-quarters';
if (temp >= 14) return 'fa-thermometer-half';
if (temp >= 7) return 'fa-thermometer-quarter';
if (temp >= 0) return 'fa-thermometer-empty';
return 'fa-snowflake';
},
/**
* getRainProbabilityIcon - Get rain icon depending on rain probability. Standard icon is
* "fa-tint", this is dimmed if rain probability is <= 15% and the "fa-umbrella" icon is used
* when probability is >= 70%.
*
* @param {Number} prob Rain probability
* @return {String} Icon name
*/
getRainProbabilityIcon: function(prob) {
if (prob <= 15) {
return 'fa-tint-slash';
}
if (prob >= 70) {
return 'fa-umbrella';
}
return 'fa-tint';
},
/**
* getWindDirKey - Get wind direction short key based on wind direction degrees.
*
* @param {String} deg Wind direction in degrees
* @return {String} Wind direction short text
*/
getWindDirKey: function(deg) {
// No wind direction available
if (!deg || isNaN(deg)) {
return undefined;
}
if (deg <= 22) return 'N';
if (deg <= 67) return 'NE';
if (deg <= 112) return 'E';
if (deg <= 157) return 'SE';
if (deg <= 202) return 'S';
if (deg <= 247) return 'SW';
if (deg <= 292) return 'W';
if (deg <= 337) return 'NW';
return 'N';
},
/**
* getFormattedUpdatedTime - Get the human readable update time.
*
* @return {String} Formatted update time
*/
getFormattedUpdateTime: function() {
return moment(this.updatedAt).format('DD.MM. HH:mm');
},
/**
* loadData - Load weather data via node_helper. This functions sends a socket notification with
* LOAD_DATA as notification name and the place id (config.id) and day count (config.days) as
* payload and schedules the next refresh.
*/
loadData: function() {
Log.info('Send socket notification to load data in node_helper ...');
// Load data via node helper
const dataConfig = { id: this.config.id, days: this.config.days };
this.sendSocketNotification('LOAD_DATA', dataConfig);
// Schedule next refresh
this.scheduleRefresh();
},
/**
* scheduleRefresh - Schedules refresh timer depending on config.updateInterval. This function
* calls loadData().
*/
scheduleRefresh: function() {
setTimeout(() => {
this.loadData();
}, this.config.updateInterval * 1000);
},
/**
* triggerModules - Trigger other modules based on weather data values.
* Hides the defined module(s) if the data field is lower or equal than the
* defined trigger value, otherwise shows the module. This behaviour is
* switched if the hide-Property is used in triggers.
*/
triggerModules: function() {
// Do nothing if no data is available
if (this.weatherData === null) return;
// Iterate over defined triggers
for (const trigger of this.config.triggers) {
// Check if weather data is available for needed day
const data = this.weatherData[trigger.day];
if (!data) continue;
// Get module(s) by class
const modules = MM.getModules().withClass(trigger.module);
for (const modul of modules) {
Log.info(`Trigger module '${modul.name}' ...`);
// Show/hide options
const lockOptions = { lockString: this.name };
const speed = this.config.animationSpeed * 1000;
// Get weather data to check
let dataValue = data[trigger.field];
if (trigger.field === 'maxtemp') {
dataValue = data.temp.split(';')[0];
}
if (trigger.field === 'mintemp') {
dataValue = data.temp.split(';')[1];
}
// Check trigger condition: Hide module if data value is lower
// or equal than trigger value or if the trigger uses hide mode,
// but not both (xor)
if ((dataValue <= trigger.value) !== (trigger.hide === true)) {
modul.hide(speed, lockOptions);
} else {
modul.show(speed, lockOptions);
}
}
}
}
});