Repository navigation
Expand file tree
/
Copy pathnode_helper.js
More file actions
57 lines (48 loc) · 1.63 KB
/
Copy pathnode_helper.js
File metadata and controls
57 lines (48 loc) · 1.63 KB
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
/* Magic Mirror
*
* Node Helper for MMM-nodejs-pool module
*
* Earle Lowe elowe@elowe.com
* Apache License
*/
const NodeHelper = require('node_helper');
const Log = require("logger");
const http = require('http');
module.exports = NodeHelper.create({
start: function() {
Log.log(`Starting node helper for: ${this.name}`);
},
getAllData: function() {
var url = "http://" + this.config.hostname + "/state/all";
http.get(url, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
var self=this;
self.sendSocketNotification("POOL_DATA", data) ;
});
}).on('error', (err) => {
Log.error(`Got error: ${err.message}`);
this.sendSocketNotification("POOL_ERROR", err.message);
});
},
getData: function() {
this.getAllData();
},
socketNotificationReceived: function(notification, payload) {
// Config is sent with CONFIG and with every GET_DATA, so the helper
// never depends on CONFIG having arrived first.
if (payload && payload.hostname) {
this.config = payload;
}
if (notification === "GET_DATA") {
if (!this.config || !this.config.hostname) {
Log.error("MMM-nodejs-pool: no hostname configured yet");
return;
}
this.getData();
}
}
});