-
Notifications
You must be signed in to change notification settings - Fork 8
/
node_helper.js
90 lines (76 loc) · 2.33 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* Magic Mirror
* Module: MMM-DynamicWeather
*
* By Scott Lewis - https://github.com/scottcl88/MMM-DynamicWeather
* MIT Licensed.
*
* Extension helper module to call external resources
*/
const NodeHelper = require("node_helper");
const https = require('https');
module.exports = NodeHelper.create({
start: function () { },
callApi: function (payload) {
let that = this;
this.url = payload;
let success = false;
console.info("[MMM-DynamicWeather] Getting Weather API data");
https.get(this.url, (res) => {
let body = '';
res.on('data', (chunk) => {
body += chunk;
});
res.on('end', () => {
let result = JSON.parse(body);
if (res.statusCode !== 200) {
console.error("[MMM-DynamicWeather] Failed getting api: ", res.statusCode);
} else {
console.info("[MMM-DynamicWeather] Received successful Weather API data");
success = true;
}
that.sendSocketNotification("API-Received", {
url: that.url,
result: result,
success: success,
});
});
}).on('error', (error) => {
console.error("[MMM-DynamicWeather] Failed getting api: ", error);
});
},
callHoliday: function () {
let that = this;
let success = false;
console.info("[MMM-DynamicWeather] Getting Holiday data");
https.get("https://www.timeanddate.com/holidays/us/?hol=43122559", (res) => {
let body = '';
res.on('data', (chunk) => {
body += chunk;
});
res.on('end', () => {
if (res.statusCode !== 200) {
console.error("[MMM-DynamicWeather] Failed getting holidays: ", res.statusCode);
} else {
console.info("[MMM-DynamicWeather] Received successful Holiday data");
success = true;
}
let result = { holidayBody: body };
that.sendSocketNotification("Holiday-Received", {
url: that.url,
result: result,
success: success,
});
});
}).on('error', (error) => {
console.error("[MMM-DynamicWeather] Failed getting holidays: ", error);
});
},
socketNotificationReceived: function (notification, payload) {
if (notification === "API-Fetch") {
this.callApi(payload);
}
if (notification === "Holiday-Fetch") {
this.callHoliday();
}
},
});