-
Notifications
You must be signed in to change notification settings - Fork 2
/
MMM-MarineWeather.js
445 lines (381 loc) · 13.7 KB
/
MMM-MarineWeather.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/* Magic Mirror
* Module: MMM-MarineWeather
*
* Magic Mirror By Michael Teeuw https://magicmirror.builders
* MIT Licensed.
*
* Module MMM-MarineWeather By Grena https://github.com/grenagit
* MIT Licensed.
*/
Module.register("MMM-MarineWeather",{
// Default module config
defaults: {
latitude: 0,
longitude: 0,
appid: "",
units: config.units,
updateInterval: 60 * 60 * 1000, // every 1 hour
animationSpeed: 1000, // 1 second
showDirectionAsArrow: false,
showGustAsWind: false,
useBeaufort: false,
useKMPH: false,
roundTemp: false,
initialLoadDelay: 0, // 0 second delay
retryDelay: 2500, // 2,5 seconds
apiBase: "https://api.stormglass.io/",
MWEndpoint: "v2/weather/point",
params: ["airTemperature", "waterTemperature", "pressure", "cloudCover", "windSpeed", "windDirection", "waveHeight", "waveDirection"],
dataSource: "sg"
},
// Define required scripts
getStyles: function() {
return ["MMM-MarineWeather.css", "font-awesome.css"];
},
// Define start sequence
start: function() {
Log.info("Starting module: " + this.name);
this.airTemperature = null;
this.waterTemperature = null;
this.temperatureUnit = null;
this.pressure = null;
this.pressureUnit = null;
this.cloudCover = null;
this.visibility = null;
this.visibilityUnit = null;
this.seaLevel = null;
this.seaLevelUnit = null;
this.windSpeed = null;
this.windSpeedUnit = null;
this.windDeg = null;
this.windDirection = null;
this.waveHeight = null;
this.waveHeightUnit = null;
this.waveDeg = null;
this.waveDirection = null;
this.wavePeriod = null;
this.loaded = false;
this.scheduleUpdate(this.config.initialLoadDelay);
},
// Override dom generator
getDom: function() {
var wrapper = document.createElement("div");
if (this.config.appid === "") {
wrapper.innerHTML = "Please set the correct Strom Glass <i>appid</i> in the config for module: " + this.name + ".";
wrapper.className = "dimmed light small";
return wrapper;
}
if (this.config.latitude === 0 && this.config.longitude === 0) {
wrapper.innerHTML = "Please set the weather <i>location</i> in the config for module: " + this.name + ".";
wrapper.className = "dimmed light small";
return wrapper;
}
if (!this.loaded) {
wrapper.innerHTML = this.translate("LOADING");
wrapper.className = "dimmed light small";
return wrapper;
}
var medium = document.createElement("div");
medium.className = "normal medium";
if(this.windSpeed !== null || this.windDirection !== null) {
var windIcon = document.createElement("span");
windIcon.className = "fas fa-wind dimmed";
medium.appendChild(windIcon);
}
if(this.windSpeed !== null) {
var windSpeed = document.createElement("span");
windSpeed.innerHTML = " " + this.windSpeed + "<span class=\"xsmall\">" + this.windSpeedUnit + "</span>";
medium.appendChild(windSpeed);
}
if(this.windDirection !== null) {
var windDirection = document.createElement("sup");
if (this.config.showDirectionAsArrow) {
if(this.windDeg !== null) {
windDirection.innerHTML = " <i class=\"fas fa-long-arrow-alt-down\" style=\"transform:rotate(" + this.windDeg + "deg);\"></i> ";
}
} else {
windDirection.innerHTML = " " + this.translate(this.windDirection);
}
medium.appendChild(windDirection);
}
if(this.waveHeight !== null || this.waveDirection !== null || this.wavePeriod !== null) {
var spacer = document.createElement("span");
spacer.innerHTML = " ";
medium.appendChild(spacer);
var waveIcon = document.createElement("span");
waveIcon.className = "fas fa-water dimmed";
medium.appendChild(waveIcon);
}
if(this.waveHeight !== null) {
var waveHeight = document.createElement("span");
waveHeight.innerHTML = " " + this.waveHeight + "<span class=\"xsmall\">" + this.waveHeightUnit + "</span>";
medium.appendChild(waveHeight);
}
if(this.waveDirection !== null) {
var waveDirection = document.createElement("sup");
if (this.config.showDirectionAsArrow) {
if(this.waveDeg !== null) {
waveDirection.innerHTML = " <i class=\"fas fa-long-arrow-alt-down\" style=\"transform:rotate(" + this.waveDeg + "deg);\"></i> ";
}
} else {
waveDirection.innerHTML = " " + this.translate(this.waveDirection);
}
medium.appendChild(waveDirection);
}
if (this.wavePeriod !== null) {
var wavePeriod = document.createElement("span");
wavePeriod.innerHTML = " " + this.wavePeriod + "<span class=\"xsmall\">s</span>";
medium.appendChild(wavePeriod);
}
if (this.seaLevel !== null) {
var spacer = document.createElement("span");
spacer.innerHTML = " ";
medium.appendChild(spacer);
var rulerIcon = document.createElement("span");
rulerIcon.className = "fas fa-ruler-vertical dimmed";
medium.appendChild(rulerIcon);
var seaLevel = document.createElement("span");
seaLevel.innerHTML = " " + this.seaLevel + "<span class=\"xsmall\">" + this.seaLevelUnit + "</span>";
medium.appendChild(seaLevel);
}
wrapper.appendChild(medium);
var large = document.createElement("div");
large.className = "large light";
var waterIcon = document.createElement("span");
waterIcon.className = "fas fa-tint watericon";
large.appendChild(waterIcon);
var waterTemperature = document.createElement("span");
waterTemperature.innerHTML = " " + this.waterTemperature + this.temperatureUnit;
large.appendChild(waterTemperature);
wrapper.appendChild(large);
var small = document.createElement("div");
small.className = "dimmed small";
var airIcon = document.createElement("span");
airIcon.className = "fas fa-umbrella-beach";
small.appendChild(airIcon);
var airTemperature = document.createElement("span");
airTemperature.innerHTML = " " + this.airTemperature + this.temperatureUnit;
small.appendChild(airTemperature);
if (this.pressure !== null) {
var spacer = document.createElement("span");
spacer.innerHTML = " ";
small.appendChild(spacer);
var compassIcon = document.createElement("span");
compassIcon.className = "fas fa-compass";
small.appendChild(compassIcon);
var pressure = document.createElement("span");
pressure.innerHTML = " " + this.pressure + "<span class=\"xsmall\">" + this.pressureUnit + "</span>";
small.appendChild(pressure);
}
if (this.cloudCover !== null) {
var spacer = document.createElement("span");
spacer.innerHTML = " ";
small.appendChild(spacer);
var cloudIcon = document.createElement("span");
cloudIcon.className = "fas fa-cloud";
small.appendChild(cloudIcon);
var cloudCover = document.createElement("span");
cloudCover.innerHTML = " " + this.cloudCover + "%";
small.appendChild(cloudCover);
}
if (this.visibility !== null) {
var spacer = document.createElement("span");
spacer.innerHTML = " ";
small.appendChild(spacer);
var cloudIcon = document.createElement("span");
cloudIcon.className = "fas fa-binoculars";
small.appendChild(cloudIcon);
var visibility = document.createElement("span");
visibility.innerHTML = " " + this.visibility + "<span class=\"xsmall\">" + this.visibilityUnit + "</span>";
small.appendChild(visibility);
}
wrapper.appendChild(small);
return wrapper;
},
// Request new data from api.stormglass.io with node_helper
socketNotificationReceived: function(notification, payload) {
if (notification === "STARTED") {
this.updateDom(this.config.animationSpeed);
} else if (notification === "ERROR") {
Log.error(this.name + ": Do not access to data (" + payload + " HTTP error).");
} else if (notification === "DATA") {
this.processMW(payload);
}
},
// Use the received data to set the various values before update DOM
processMW: function(data) {
if (!data || !data.hours || typeof data.hours[0].time === "undefined") {
Log.error(this.name + ": Do not receive usable data.");
return;
}
switch(this.config.units) {
case "metric":
this.waterTemperature = this.roundValue(data.hours[0].waterTemperature[this.config.dataSource]);
this.airTemperature = this.roundValue(data.hours[0].airTemperature[this.config.dataSource]);
this.temperatureUnit = "°C";
break;
case "imperial":
this.waterTemperature = this.roundValue((data.hours[0].waterTemperature[this.config.dataSource] * 1.8) + 32);
this.airTemperature = this.roundValue((data.hours[0].airTemperature[this.config.dataSource] * 1.8) + 32);
this.temperatureUnit = "°F";
break;
}
if (this.checkData(data, "pressure")) {
switch(this.config.units) {
case "metric":
this.pressure = parseFloat(data.hours[0].pressure[this.config.dataSource]).toFixed(0);
this.pressureUnit = "hPa";
break;
case "imperial":
this.pressure = parseFloat(data.hours[0].pressure[this.config.dataSource] / 68.94).toFixed(0);
this.pressureUnit = "psi";
break;
}
}
if (this.checkData(data, "cloudCover")) {
this.cloudCover = parseFloat(data.hours[0].cloudCover[this.config.dataSource]).toFixed(0);
}
if (this.checkData(data, "visibility")) {
switch(this.config.units) {
case "metric":
this.visibility = parseFloat(data.hours[0].visibility[this.config.dataSource]).toFixed(1);
this.visibilityUnit = "km";
break;
case "imperial":
this.visibility = parseFloat(data.hours[0].visibility[this.config.dataSource] * 0.6214).toFixed(1);
this.visibilityUnit = "mi";
break;
}
}
if (this.checkData(data, "seaLevel")) {
switch(this.config.units) {
case "metric":
this.seaLevel = parseFloat(data.hours[0].seaLevel[this.config.dataSource]).toFixed(1);
this.seaLevelUnit = "m";
break;
case "imperial":
this.seaLevel = parseFloat(data.hours[0].seaLevel[this.config.dataSource] / 0.3048).toFixed(1);
this.seaLevelUnit = "ft";
break;
}
}
if (this.checkData(data, "windSpeed")) {
if (this.config.useBeaufort){
this.windSpeed = this.ms2Beaufort(this.roundValue(data.hours[0].windSpeed[this.config.dataSource]));
this.windSpeedUnit = "bf";
} else if (this.config.useKMPH) {
this.windSpeed = parseFloat((data.hours[0].windSpeed[this.config.dataSource] * 60 * 60) / 1000).toFixed(0);
this.windSpeedUnit = "km/h";
} else if(this.config.units === "imperial") {
this.windSpeed = parseFloat((data.hours[0].windSpeed[this.config.dataSource] * 60 * 60) / 1609).toFixed(0);
this.windSpeedUnit = "mph";
} else {
this.windSpeed = parseFloat(data.hours[0].windSpeed[this.config.dataSource]).toFixed(0);
this.windSpeedUnit = "m/s";
}
}
if (this.checkData(data, "windDirection")) {
this.windDeg = data.hours[0].windDirection[this.config.dataSource];
this.windDirection = this.deg2Cardinal(data.hours[0].windDirection[this.config.dataSource]);
}
if (this.checkData(data, "waveHeight")) {
switch(this.config.units) {
case "metric":
this.waveHeight = parseFloat(data.hours[0].waveHeight[this.config.dataSource]).toFixed(1);
this.waveHeightUnit = "m";
break;
case "imperial":
this.waveHeight = parseFloat(data.hours[0].waveHeight[this.config.dataSource] / 0.3048).toFixed(1);
this.waveHeightUnit = "ft";
break;
}
}
if (this.checkData(data, "waveDirection")) {
this.waveDeg = data.hours[0].waveDirection[this.config.dataSource];
this.waveDirection = this.deg2Cardinal(data.hours[0].waveDirection[this.config.dataSource]);
}
if (this.checkData(data, "wavePeriod")) {
this.wavePeriod = parseFloat(data.hours[0].wavePeriod[this.config.dataSource]).toFixed(1);
}
this.loaded = true;
this.updateDom(this.config.animationSpeed);
this.scheduleUpdate();
},
// Schedule next update
scheduleUpdate: function(delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
var self = this;
setTimeout(function() {
self.sendSocketNotification('CONFIG', self.config);
}, nextLoad);
},
// Converts m/s to beaufort
ms2Beaufort: function(ms) {
var kmh = ms * 60 * 60 / 1000;
var speeds = [1, 5, 11, 19, 28, 38, 49, 61, 74, 88, 102, 117, 1000];
for (var beaufort in speeds) {
var speed = speeds[beaufort];
if (speed > kmh) {
return beaufort;
}
}
return 12;
},
// Convert degree to cardinal direction
deg2Cardinal: function(deg) {
if (deg>11.25 && deg<=33.75){
return "NNE";
} else if (deg > 33.75 && deg <= 56.25) {
return "NE";
} else if (deg > 56.25 && deg <= 78.75) {
return "ENE";
} else if (deg > 78.75 && deg <= 101.25) {
return "E";
} else if (deg > 101.25 && deg <= 123.75) {
return "ESE";
} else if (deg > 123.75 && deg <= 146.25) {
return "SE";
} else if (deg > 146.25 && deg <= 168.75) {
return "SSE";
} else if (deg > 168.75 && deg <= 191.25) {
return "S";
} else if (deg > 191.25 && deg <= 213.75) {
return "SSW";
} else if (deg > 213.75 && deg <= 236.25) {
return "SW";
} else if (deg > 236.25 && deg <= 258.75) {
return "WSW";
} else if (deg > 258.75 && deg <= 281.25) {
return "W";
} else if (deg > 281.25 && deg <= 303.75) {
return "WNW";
} else if (deg > 303.75 && deg <= 326.25) {
return "NW";
} else if (deg > 326.25 && deg <= 348.75) {
return "NNW";
} else {
return "N";
}
},
// Round a temperature to 1 decimal or integer (depending on config.roundTemp)
roundValue: function(temperature) {
var decimals = this.config.roundTemp ? 0 : 1;
return parseFloat(temperature).toFixed(decimals);
},
// Check if data is usable
checkData: function(data, param) {
if(this.config.params.indexOf(param) !== -1) {
if (typeof data.hours[0][param] !== "undefined") {
return true;
} else {
Log.error(this.name + ": Do not receive usable data for " + param + " (this information will be hidden).");
return false;
}
} else {
return false;
}
}
});