-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_helper.js
More file actions
97 lines (78 loc) · 2.59 KB
/
node_helper.js
File metadata and controls
97 lines (78 loc) · 2.59 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
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
91
92
93
94
95
var NodeHelper = require('node_helper');
const http = require('http');
const https = require('https');
//const textToImage = require('text-to-image');
module.exports = NodeHelper.create({
start: function () {
console.log('MMM-SolarPowerFrontend helper started...');
},
//Subclass socketNotificationReceived received.
socketNotificationReceived: function (notification, payload) {
console.log(this.name + ' Received ' + notification);
console.log(payload)
if (notification === "MMM-SolarPowerFrontend_SET_CONFIG") {
this.config = payload;
} else if (notification === "MMM-SolarPowerFrontend_GET_24H") {
this.fetch24h();
} else if (notification === "MMM-SolarPowerFrontend_GET_30D") {
this.fetch30d();
} else if (notification === "MMM-SolarPowerFrontend_GET_12MO") {
this.fetch12mo();
}
},
fetch24h: function() {
var self=this;
var url=this.config.power_24h_url;
this.fetchURL(url, json =>{
self.data24h = json;
self.sendSocketNotification('MMM-SolarPowerFrontend_DAY_RESULT', json);
});
},
fetch30d: function() {
var self=this;
var url=this.config.power_30d_url;
this.fetchURL(url, json =>{
self.data30d = json;
self.sendSocketNotification('MMM-SolarPowerFrontend_MONTH_RESULT', json);
});
},
fetch12mo: function() {
var self=this;
var url=this.config.power_1yr_url;
this.fetchURL(url, json =>{
self.data12mo = json;
self.sendSocketNotification('MMM-SolarPowerFrontend_MONTHLY_HOURLY_RESULT', json);
});
},
fetchURL: function(url,callback) {
var self=this;
const cacheurl = new URL(url);
const protocol = cacheurl.protocol.startsWith("https://") ? https : http ;
protocol.get(url, res => {
console.log("https callback handler for " + url);
let data = [];
const headerDate = res.headers && res.headers.date ? res.headers.date : 'no response date';
res.on('data', chunk => {
data.push(chunk);
});
res.on('end', () => {
var html = ""+data.join('');
//console.log('Response ended: ' + html.substring(0,100));
console.log('Response ended: ' + html);
console.log('Response ended: ' + html.replace("\n","").replace("\r",""));
//console.log(html.indexOf('"status" : "OK"'));
if(html != "{\"data\": \"\"}") {
var o=JSON.parse(html.replace("\n","").replace("\r",""));
if(typeof o=="array")
callback(o);
else if('data' in o && Array.isArray(o.data))
callback(o.data);
else
console.error(self.name + " did not receive expected value from:" + url);
}
});
}).on('error', err => {
console.log('Error: ', err.message);
});
},
});