forked from YR/MMM-YrNow
-
Notifications
You must be signed in to change notification settings - Fork 14
/
node_helper.js
59 lines (53 loc) · 2.44 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
// Reminder to self: tail -f ~/.pm2/logs/MagicMirror-out-0.log
var NodeHelper = require('node_helper');
var request = require('request');
var d = new Date();
var n = d.toLocaleDateString() + " " + d.toLocaleTimeString();
// console.log("MMM-YrThen " + n + ": Starting MMM-YrThen in node_helper");
module.exports = NodeHelper.create({
start: function() {
var d = new Date();
var n = d.toLocaleDateString() + " " + d.toLocaleTimeString();
console.log("MMM-YrThen " + n + ": Starting helper for MMM-YrThen");
this.config = null;
this.forecastUrl = '';
},
socketNotificationReceived: function(notification, payload) {
var self = this;
var d = new Date();
var n = d.toLocaleDateString() + " " + d.toLocaleTimeString();
// console.log("MMM-YrThen " + n + ": Received notification");
if(notification === 'GET_YRTHEN_FORECAST') {
var d = new Date();
var n = d.toLocaleDateString() + " " + d.toLocaleTimeString();
// console.log("MMM-YrThen " + n + ": Received GET_YRTHEN_FORECAST notification");
self.config = payload.config;
self.forecastUrl = payload.forecastUrl;
this.getForecastFromYrThen();
}
},
getForecastFromYrThen: function() {
var d = new Date();
var n = d.toLocaleDateString() + " " + d.toLocaleTimeString();
// console.log("MMM-YrThen " + n + ": Getting forecast");
var self = this;
var locationData = {};
request({url: self.forecastUrl, method: 'GET'}, function(error, response, message) {
var d = new Date();
var n = d.toLocaleDateString() + " " + d.toLocaleTimeString();
// console.log("MMM-YrThen " + n + ": Requesting forecast from" + self.forecastUrl);
if (!error && (response.statusCode == 200 || response.statusCode == 304)) {
locationData.forecast = JSON.parse(message);
var d = new Date();
var n = d.toLocaleDateString() + " " + d.toLocaleTimeString();
// console.log("MMM-YrThen " + n + ": Returning forecast");
self.sendSocketNotification('YRTHEN_FORECAST_DATA', locationData);
}
else{
var d = new Date();
var n = d.toLocaleDateString() + " " + d.toLocaleTimeString();
// console.log("MMM-YrThen " + n + ": Error!");
}
});
}
});