forked from nigel-daniels/MMM-3Day-Forecast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
73 lines (59 loc) · 2.41 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
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
/* Magic Mirror Module: MMM-3Day-Forecast helper
* Version: 1.0.0
*
* By Nigel Daniels https://github.com/nigel-daniels/
* MIT Licensed.
*/
var NodeHelper = require('node_helper');
var request = require('request');
module.exports = NodeHelper.create({
start: function () {
console.log('MMM-3Day-Forecast helper, started...');
},
getWeatherData: function(payload) {
var _this = this;
this.url = payload;
request({url: this.url, method: 'GET'}, function(error, response, body) {
// Lets convert the body into JSON
var result = JSON.parse(body);
// Check to see if we are error free and got an OK response
if (!error && response.statusCode == 200) {
var forecast = []; // Clear the array
for (var i=0; i < 3; i++) {
var day = {
icon: result.data[i].weather.icon,
conditions: result.data[i].weather.description,
high: result.data[i].max_temp,
pop: result.data[i].pop,
humid: result.data[i].rh,
wspd: result.data[i].wind_spd,
wdir: result.data[i].wind_cdir
};
forecast.push(day);
}
} else {
// In all other cases it's some other error
for (var i=0; i<3; i++) {
var day = {
icon: 'blank',
conditions: 'No weather data',
high: '--',
pop: '--',
humid: '--',
wspd: '--',
wdir: '--'
};
forecast.push(day);
}
}
// We have the response figured out so lets fire off the notifiction
_this.sendSocketNotification('GOT-3DAY-FORECAST', {'url': _this.url, 'forecast': forecast});
});
},
socketNotificationReceived: function(notification, payload) {
// Check this is for us and if it is let's get the weather data
if (notification === 'GET-3DAY-FORECAST') {
this.getWeatherData(payload);
}
}
});