forked from MichMich/MMM-Toon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
57 lines (49 loc) · 1.55 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
/* MMM-Toon
* Node Helper
*
* By Michael Teeuw http://michaelteeuw.nl
* MIT Licensed.
*/
var ToonAPI = require("./ToonAPI.js");
module.exports = NodeHelper.create({
// Subclass start method.
start: function() {
console.log("Starting module: " + this.name);
this.config = {};
this.fetcherRunning = false;
this.status = false;
},
// Subclass socketNotificationReceived received.
socketNotificationReceived: function(notification, payload) {
if (notification === "CONFIG") {
console.log("Toon config received!");
this.config = payload;
if (this.config.apiKey && this.config.apiSecret && this.config.accessToken) {
ToonAPI.setApiKeySecret(this.config.apiKey, this.config.apiSecret, this.config.accessToken);
if (!this.fetcherRunning) {
this.fetchStatus();
}
}
if (this.status) {
this.sendSocketNotification('STATUS', this.status);
}
}
},
/**
* fetchStatus
* Request new status drom the Toon API and broadcast it to the MagicMirror module if it's received.
*/
fetchStatus: function() {
var self = this;
this.fetcherRunning = true;
ToonAPI.getStatus(function(status) {
if (status && status.updated) {
self.status = status;
self.sendSocketNotification('STATUS', status);
}
setTimeout(function() {
self.fetchStatus();
}, self.config.updateInterval);
});
}
});