-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
48 lines (40 loc) · 1.41 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
/* Magic Mirror
*
* Node Helper for MMM-EnphaseEnvoy module
*
* Earle Lowe [email protected]
* 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}`);
},
getEnphaseData: function() {
var url = "http://" + this.config.hostname + "/production";
http.get(url, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
var self=this;
var re = new RegExp('<td>Currently</td>.*<td>(.*?) (.?)W</td></tr>.*<td>Today</td>.*<td>(.*?) (.?)Wh</td></tr>.*<td>Past Week</td>.*<td>(.*?) (.?)Wh</td></tr>.*<td>Since Installation</td>.*<td>(.*?) (.?)Wh</td></tr>');
m = re.exec(data);
var myjson = JSON.stringify(m);
self.sendSocketNotification("DATA", myjson) ;
});
}).on('error', (err) => {
Log.error(`Got error: ${err.message}`);
});
},
socketNotificationReceived: function(notification, payload) {
if (notification === "CONFIG") {
this.config = payload;
} else if (notification === "GET_DATA") {
this.getEnphaseData();
}
}
});