-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-NL-rain-intensity.js
113 lines (101 loc) · 4.08 KB
/
MMM-NL-rain-intensity.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
//MMM-NL-rain-intensity.js:
Module.register("MMM-NL-rain-intensity",{
// Default module config.
defaults: {
api: "https://gps.buienradar.nl/getrr.php",
lat: 52.15,
lon: 4.49,
refreshInterval: 75000,
},
// Define required translations.
getTranslations: function() {
return {
en: "translations/en.json",
nl: "translations/nl.json",
};
},
// Define start sequence.
start: function() {
Log.info("Starting module: " + this.name);
this.nodatacount = 0;
this.sendSocketNotification('CONFIG', this.config);
},
// Override dom generator.
getDom: function() {
var wrapper = document.getElementById('rainintensity');
if (!wrapper) {
wrapper = document.createElement("div");
wrapper.id = "rainintensity";
wrapper.className = "xsmall";
wrapper.innerHTML = this.translate("STARTING");
}
return wrapper;
},
getHeader: function() {
return this.translate("UMBRELLA");
},
/* socketNotificationReceive(notification)
* used to get communication from the nodehelper
*
* argument notification object - status label from nodehelper.
* argument payload object - Weather information received via buienradar.nl.
*/
socketNotificationReceived: function(notification, payload) {
rainintensitytable = document.getElementById('rainintensity');
// configured succesfully
if (notification === "STARTED") {
Log.info(this.name + ": configured");
rainintensitytable.innerHTML = this.translate("STARTED");
} else
// error received from node_helper.js
if (notification === "ERROR") {
Log.error(this.name + ": rain forecast error: " + payload);
rainintensitytable.innerHTML = this.translate("ERROR");
} else
// data received
if (notification === "DATA") {
// data received from node_helper.js but empty payload (api calls return empty string sometimes)
if (!payload || payload ==="") {
this.nodatacount++;
nodata = this.translate("NODATA");
Log.warn(this.name + ": " + nodata);
rainintensitytable.innerHTML = nodata;
} else
// data received and payload to parse
{
this.nodatacount=0;
this.totalrain = 0;
this.rains = [];
this.times = [];
var lines = payload.split("\n");
var numLines = lines.length-1;
//console.log(payload)
for (i=0;i<numLines; i++) {
var line = lines[i];
var pipeIndex = line.indexOf('|');
r = parseInt(line.substring(0, pipeIndex));
t = line.substring(pipeIndex+1, line.length);
this.totalrain += r;
this.rains.push(r /2.55);
this.times.push(t);
}
if (this.totalrain == 0) {
Log.info(this.name + ": no rain expected");
noRainText = this.translate("NORAIN") + ' '
+ this.times[this.times.length-1] ;
rainintensitytable.innerHTML = noRainText;
} else {
Log.info(this.name + ": rain expected");
rainintensitytable.innerHTML = "";
rainintensitytable.innerHTML += this.times[0] +" ";
for (j=0;j<12;j++){
rainintensitytable.innerHTML += "<span style='background-color:hsl(0, 0%," + this.rains[j] + "%);'> </span>";
}
rainintensitytable.innerHTML += " " + this.times[11];
Log.info(rainintensitytable.innerHTML);
}
}
}
this.updateDom();
}
});