-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
48 lines (42 loc) · 1.45 KB
/
node_helper.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
/* Magic Mirror
* Module: MMM-RainForecast
*
* By jupadin
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
const { XMLHttpRequest } = require('xmlhttprequest');
module.exports = NodeHelper.create({
start: function() {
this.config = null;
},
socketNotificationReceived: function(notification, payload) {
if (notification == "SET_CONFIG") {
this.config = payload;
}
// Fetch new data from RainForecast-Server
this.getData();
},
getData: function() {
console.info(this.name + ": Fetching data from RainForecast-Server...");
this.getTimestamps().then(fetchedTimestamps => {
this.sendSocketNotification("DATA", fetchedTimestamps)
});
// Set timeout to continuiusly fetch new data from RainForecast-Server
setTimeout(this.getData.bind(this), (this.config.updateInterval));
},
getTimestamps: function() {
return new Promise((resolve, reject) => {
const apiRequest = new XMLHttpRequest();
apiRequest.open("GET", "https://api.rainviewer.com/public/weather-maps.json");
apiRequest.onload = () => {
if (apiRequest.status >= 200 && apiRequest.status <= 300) {
resolve(apiRequest.responseText);
} else {
reject(apiRequest.statusText);
}
};
apiRequest.send();
});
}
})