-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
91 lines (77 loc) · 1.75 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
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
/* Magic Mirror
* Node Helper: MMM-PVoutput
*
* By Martin van Es
* MIT Licensed.
*/
var NodeHelper = require("node_helper");
var request = require('request');
module.exports = NodeHelper.create({
// Override start method.
start: function() {
console.log("Starting node helper for: " + this.name);
return;
},
// Override socketNotificationReceived method.
socketNotificationReceived: function(notification, payload) {
var self = this;
this.sid = payload.sid;
this.apiKey = payload.apiKey;
this.url = "http://pvoutput.org/service/r2/getstatus.jsp";
// Do some stuff with API url
setInterval(function() {
self.getData(self)
}, payload.updateInterval);
this.getData(this);
return;
},
getData: function(self) {
var options = {
url: self.url,
headers: {
'X-Pvoutput-SystemId': self.sid,
'X-Pvoutput-Apikey': self.apiKey,
},
qs: {
h: 1,
asc: 1,
limit: 1000,
},
}
request(options, function(error, response, body) {
self.processData(error, response, body, self);
});
},
processData: function(error, response, body, self) {
if (error) {
self.sendSocketNotification("ERROR", {
error: "Error",
});
return;
}
if (response.statusCode != 200) {
self.sendSocketNotification("ERROR", {
error: body,
});
return;
}
var Gp = [];
var Cp = [];
var curGp = 0;
var curGe = 0;
var lines = body.split(';');
for(var i = 0;i < lines.length;i++){
var values = lines[i].split(',');
Gp.push(values[4]=="NaN"?0:values[4]);
Cp.push(values[8]=="NaN"?0:values[8]);
curGp = values[4]=="NaN"?0:values[4];
curGe = values[2]=="NaN"?0:values[2];
}
self.sendSocketNotification("NEW_DATA", {
Gp: Gp,
Cp: Cp,
curGp: curGp,
curGe: curGe,
});
},
});