-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
46 lines (41 loc) · 1.29 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
/* global module, require */
/* jshint node: true */
/* Magic Mirror
* Node Helper: MMM-Clash-Royale
*
* By Ian Perrin http://ianperrin.com
* MIT Licensed.
*/
var request = require('request');
var NodeHelper = require("node_helper");
module.exports = NodeHelper.create({
// Subclass start method.
start: function() {
console.log("Starting module: " + this.name);
},
// Subclass socketNotificationReceived received.
socketNotificationReceived: function(notification, payload) {
if (notification === "GET_DATA") {
var options = {
url: payload.apiBase + "/api/random-deck"
};
this.getData(options, "RANDOM_DECK");
}
},
/**
* getData
* Request data from the supplied URL and broadcast it to the MagicMirror module if it's received.
*/
getData: function(options, name) {
console.log("Get " + name + " data for url " + options.url);
var self = this;
request(options, function(error, response, body) {
if (response.statusCode === 200) {
self.sendSocketNotification(name, JSON.parse(body));
} else {
self.sendSocketNotification(name);
console.log("Error getting " + name + " " + response.statusCode);
}
});
}
});