-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolaredge-power.js
191 lines (147 loc) · 5.86 KB
/
solaredge-power.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
var https = require('https'),
urllib = require("url"),
EventEmitter = require('events').EventEmitter;
function dateFormat (date, fstr, utc) {
utc = utc ? 'getUTC' : 'get';
return fstr.replace (/%[YmdHMS]/g, function (m) {
switch (m) {
case '%Y': return date[utc + 'FullYear'] (); // no leading zeros required
case '%m': m = 1 + date[utc + 'Month'] (); break;
case '%d': m = date[utc + 'Date'] (); break;
case '%H': m = date[utc + 'Hours'] (); break;
case '%M': m = date[utc + 'Minutes'] (); break;
case '%S': m = date[utc + 'Seconds'] (); break;
default: return m.slice (1); // unknown code, remove %
}
// add leading zero if required
return ('0' + m).slice (-2);
});
}
module.exports = function(RED) {
var API_BASE = "https://monitoringapi.solaredge.com";
function SolarEdgeSite(n) {
RED.nodes.createNode(this, n);
this.siteid = n.siteid;
this.apikey = n.apikey;
}
RED.nodes.registerType("solaredge-power-site", SolarEdgeSite);
function SolarEdgeNode(n) {
RED.nodes.createNode(this, n);
var node = this;
node.site = RED.nodes.getNode(n.site);
node.interval = n.interval;
node.command = n.command;
node.timer = {};
function generateURL(command, options) {
var query = "";
for (var key in options) {
query += encodeURIComponent(key) + "=" + encodeURIComponent(options[key]);
}
var prefix_url = ""
switch (command) {
case 'list':
case 'sensors':
prefix_url = "/equipment/" + node.site.siteid + "/" + command + ".json" ;
break;
case 'details':
case 'overview':
case 'currentPowerFlow':
case 'inventory':
case 'dataPeriod':
case 'envBenefits':
prefix_url = "/site/" + node.site.siteid + "/" + command + ".json" ;
break;
case 'inverterTechnicalData':
prefix_url = "/site/" + node.site.siteid + "/" + "inventory" + ".json" ;
break;
default:
prefix_url = "/site/" + node.site.siteid + "/" + command + ".json";
}
return API_BASE + prefix_url + "?" + query;
// return API_BASE + "/site/" + node.site.siteid + "/" + command + ".json" + "?" + query;
}
function fetchData2nd(node, msg) {
date5minutesAgo = new Date ();
date5minutesAgo.setMinutes(date5minutesAgo.getMinutes() -5);
var options = { api_key: node.site.apikey,
endTime: dateFormat(new Date (), "%Y-%m-%d %H:%M:%S", true),
startTime: dateFormat(date5minutesAgo, "%Y-%m-%d %H:%M:%S", true)
};
var query="";
for (var key in options) {
query += "&" + encodeURIComponent(key) + "=" + encodeURIComponent(options[key]);
}
// console.log(Date());
var SN = msg.payload.Inventory.inverters[0].SN;
newurl = API_BASE + "/equipment/" + node.site.siteid + "/" + SN + "/data?a=1" + query;
msg.newnurl = newurl;
// console.log(newurl);
var opts = urllib.parse(newurl);
opts.method = "GET";
var request = https.request(opts, function(res) {
res.setEncoding('utf8');
var msg = {};
msg.statusCode = res.statusCode;
msg.payload = "";
res.on("data", function(chunk) {
msg.payload += chunk;
node.log(chunk);
});
res.on("end",function() {
try { msg.payload = JSON.parse(msg.payload); }
catch(e) { node.warn("Invalid JSON returned"); }
// console.log("youhou");
node.send(msg);
node.status({});
});
});
request.on("error", function(err) {
node.error(err);
node.status({ fill: "red", shape: "dot", text: "error" });
});
request.end();
}
function fetchData() {
node.timer = setTimeout(fetchData, node.interval * 1000);
var options = { api_key: node.site.apikey };
var url = generateURL(node.command, options);
var opts = urllib.parse(url);
opts.method = "GET";
var request = https.request(opts, function(res) {
res.setEncoding('utf8');
var msg = {};
msg.statusCode = res.statusCode;
msg.payload = "";
res.on("data", function(chunk) {
msg.payload += chunk;
node.log(chunk);
});
res.on("end",function() {
try { msg.payload = JSON.parse(msg.payload); }
catch(e) { node.warn("Invalid JSON returned"); }
switch (node.command) {
case 'inverterTechnicalData':
fetchData2nd(node, msg);
break;
default:
node.send(msg);
node.status({});
break;
}
});
});
request.on("error", function(err) {
node.error(err);
node.status({ fill: "red", shape: "dot", text: "error" });
});
request.end();
}
node.on("close", function(){
if (node.timer) {
clearTimeout(node.timer);
}
});
fetchData();
}
RED.nodes.registerType("solaredge-power", SolarEdgeNode);
};