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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
"use strict";
// This program will connect to one or more Fronius Symo Inverters.
// to get the Solar Power Generated (current, today).
// example configuration in netdata/conf.d/node.d/fronius.conf.md
var url = require("url");
var http = require("http");
var netdata = require("netdata");
netdata.debug("loaded " + __filename + " plugin");
var fronius = {
name: "Fronius",
enable_autodetect: false,
update_every: 5,
base_priority: 60000,
charts: {},
powerGridId: "p_grid",
powerPvId: "p_pv",
powerAccuId: "p_akku", // not my typo! Using the ID from the AP
consumptionLoadId: "p_load",
autonomyId: "rel_autonomy",
consumptionSelfId: "rel_selfconsumption",
solarConsumptionId: "solar_consumption",
energyTodayId: "e_day",
energyYearId: "e_year",
createBasicDimension: function (id, name, divisor) {
return {
id: id, // the unique id of the dimension
name: name, // the name of the dimension
algorithm: netdata.chartAlgorithms.absolute,// the id of the netdata algorithm
multiplier: 1, // the multiplier
divisor: divisor, // the divisor
hidden: false // is hidden (boolean)
};
},
// Gets the site power chart. Will be created if not existing.
getSitePowerChart: function (service, suffix) {
var id = this.getChartId(service, suffix);
var chart = fronius.charts[id];
if (fronius.isDefined(chart)) return chart;
var dim = {};
dim[fronius.powerGridId] = this.createBasicDimension(fronius.powerGridId, "grid", 1);
dim[fronius.powerPvId] = this.createBasicDimension(fronius.powerPvId, "photovoltaics", 1);
dim[fronius.powerAccuId] = this.createBasicDimension(fronius.powerAccuId, "accumulator", 1);
chart = {
id: id, // the unique id of the chart
name: "", // the unique name of the chart
title: service.name + " Current Site Power", // the title of the chart
units: "W", // the units of the chart dimensions
family: "power", // the family of the chart
context: "fronius.power", // the context of the chart
type: netdata.chartTypes.area, // the type of the chart
priority: fronius.base_priority + 1, // the priority relative to others in the same family
update_every: service.update_every, // the expected update frequency of the chart
dimensions: dim
};
chart = service.chart(id, chart);
fronius.charts[id] = chart;
return chart;
},
// Gets the site consumption chart. Will be created if not existing.
getSiteConsumptionChart: function (service, suffix) {
var id = this.getChartId(service, suffix);
var chart = fronius.charts[id];
if (fronius.isDefined(chart)) return chart;
var dim = {};
dim[fronius.consumptionLoadId] = this.createBasicDimension(fronius.consumptionLoadId, "load", 1);
chart = {
id: id, // the unique id of the chart
name: "", // the unique name of the chart
title: service.name + " Current Load", // the title of the chart
units: "W", // the units of the chart dimensions
family: "consumption", // the family of the chart
context: "fronius.consumption", // the context of the chart
type: netdata.chartTypes.area, // the type of the chart
priority: fronius.base_priority + 2, // the priority relative to others in the same family
update_every: service.update_every, // the expected update frequency of the chart
dimensions: dim
};
chart = service.chart(id, chart);
fronius.charts[id] = chart;
return chart;
},
// Gets the site consumption chart. Will be created if not existing.
getSiteAutonomyChart: function (service, suffix) {
var id = this.getChartId(service, suffix);
var chart = fronius.charts[id];
if (fronius.isDefined(chart)) return chart;
var dim = {};
dim[fronius.autonomyId] = this.createBasicDimension(fronius.autonomyId, "autonomy", 1);
dim[fronius.consumptionSelfId] = this.createBasicDimension(fronius.consumptionSelfId, "self_consumption", 1);
dim[fronius.solarConsumptionId] = this.createBasicDimension(fronius.solarConsumptionId, "solar_consumption", 1);
chart = {
id: id, // the unique id of the chart
name: "", // the unique name of the chart
title: service.name + " Current Autonomy", // the title of the chart
units: "%", // the units of the chart dimensions
family: "autonomy", // the family of the chart
context: "fronius.autonomy", // the context of the chart
type: netdata.chartTypes.area, // the type of the chart
priority: fronius.base_priority + 3, // the priority relative to others in the same family
update_every: service.update_every, // the expected update frequency of the chart
dimensions: dim
};
chart = service.chart(id, chart);
fronius.charts[id] = chart;
return chart;
},
// Gets the site energy chart for today. Will be created if not existing.
getSiteEnergyTodayChart: function (service, suffix) {
var chartId = this.getChartId(service, suffix);
var chart = fronius.charts[chartId];
if (fronius.isDefined(chart)) return chart;
var dim = {};
dim[fronius.energyTodayId] = this.createBasicDimension(fronius.energyTodayId, "today", 1000);
chart = {
id: chartId, // the unique id of the chart
name: "", // the unique name of the chart
title: service.name + " Energy production for today",// the title of the chart
units: "kWh", // the units of the chart dimensions
family: "energy", // the family of the chart
context: "fronius.energy.today", // the context of the chart
type: netdata.chartTypes.area, // the type of the chart
priority: fronius.base_priority + 4, // the priority relative to others in the same family
update_every: service.update_every, // the expected update frequency of the chart
dimensions: dim
};
chart = service.chart(chartId, chart);
fronius.charts[chartId] = chart;
return chart;
},
// Gets the site energy chart for today. Will be created if not existing.
getSiteEnergyYearChart: function (service, suffix) {
var chartId = this.getChartId(service, suffix);
var chart = fronius.charts[chartId];
if (fronius.isDefined(chart)) return chart;
var dim = {};
dim[fronius.energyYearId] = this.createBasicDimension(fronius.energyYearId, "year", 1000);
chart = {
id: chartId, // the unique id of the chart
name: "", // the unique name of the chart
title: service.name + " Energy production for this year",// the title of the chart
units: "kWh", // the units of the chart dimensions
family: "energy", // the family of the chart
context: "fronius.energy.year", // the context of the chart
type: netdata.chartTypes.area, // the type of the chart
priority: fronius.base_priority + 5, // the priority relative to others in the same family
update_every: service.update_every, // the expected update frequency of the chart
dimensions: dim
};
chart = service.chart(chartId, chart);
fronius.charts[chartId] = chart;
return chart;
},
// Gets the inverter power chart. Will be created if not existing.
// Needs the array of inverters in order to create a chart with all inverters as dimensions
getInverterPowerChart: function (service, suffix, inverters) {
var chartId = this.getChartId(service, suffix);
var chart = fronius.charts[chartId];
if (fronius.isDefined(chart)) return chart;
var dim = {};
for (var key in inverters) {
if (inverters.hasOwnProperty(key)) {
var name = key;
if (!isNaN(key)) name = "inverter_" + key;
dim[key] = this.createBasicDimension("inverter_" + key, name, 1);
}
}
chart = {
id: chartId, // the unique id of the chart
name: "", // the unique name of the chart
title: service.name + " Current Inverter Output",// the title of the chart
units: "W", // the units of the chart dimensions
family: "inverters", // the family of the chart
context: "fronius.inverter.output", // the context of the chart
type: netdata.chartTypes.stacked, // the type of the chart
priority: fronius.base_priority + 6, // the priority relative to others in the same family
update_every: service.update_every, // the expected update frequency of the chart
dimensions: dim
};
chart = service.chart(chartId, chart);
fronius.charts[chartId] = chart;
return chart;
},
processResponse: function (service, content) {
var json = fronius.convertToJson(content);
if (json === null) return;
// add the service
service.commit();
var chartDefinitions = fronius.parseCharts(service, json);
var chartCount = chartDefinitions.length;
while (chartCount--) {
var chartObj = chartDefinitions[chartCount];
service.begin(chartObj.chart);
var dimCount = chartObj.dimensions.length;
while (dimCount--) {
var dim = chartObj.dimensions[dimCount];
service.set(dim.name, dim.value);
}
service.end();
}
},
parseCharts: function (service, json) {
var site = json.Body.Data.Site;
return [
this.parsePowerChart(service, site),
this.parseConsumptionChart(service, site),
this.parseAutonomyChart(service, site),
this.parseEnergyTodayChart(service, site),
this.parseEnergyYearChart(service, site),
this.parseInverterChart(service, json.Body.Data.Inverters)
];
},
parsePowerChart: function (service, site) {
return this.getChart(this.getSitePowerChart(service, "power"),
[
this.getDimension(this.powerGridId, Math.round(site.P_Grid)),
this.getDimension(this.powerPvId, Math.round(Math.max(site.P_PV, 0))),
this.getDimension(this.powerAccuId, Math.round(site.P_Akku))
]
);
},
parseConsumptionChart: function (service, site) {
return this.getChart(this.getSiteConsumptionChart(service, "consumption"),
[this.getDimension(this.consumptionLoadId, Math.round(Math.abs(site.P_Load)))]
);
},
parseAutonomyChart: function (service, site) {
var selfConsumption = site.rel_SelfConsumption;
var solarConsumption = 0;
var load = Math.abs(site.P_Load);
var power = Math.max(site.P_PV, 0);
if (power <= 0) solarConsumption = 0;
else if (load >= power) solarConsumption = 100;
else solarConsumption = 100 / power * load;
return this.getChart(this.getSiteAutonomyChart(service, "autonomy"),
[
this.getDimension(this.autonomyId, Math.round(site.rel_Autonomy)),
this.getDimension(this.consumptionSelfId, Math.round(selfConsumption === null ? 100 : selfConsumption)),
this.getDimension(this.solarConsumptionId, Math.round(solarConsumption))
]
);
},
parseEnergyTodayChart: function (service, site) {
return this.getChart(this.getSiteEnergyTodayChart(service, "energy.today"),
[this.getDimension(this.energyTodayId, Math.round(Math.max(site.E_Day, 0)))]
);
},
parseEnergyYearChart: function (service, site) {
return this.getChart(this.getSiteEnergyYearChart(service, "energy.year"),
[this.getDimension(this.energyYearId, Math.round(Math.max(site.E_Year, 0)))]
);
},
parseInverterChart: function (service, inverters) {
var dimensions = [];
for (var key in inverters) {
if (inverters.hasOwnProperty(key)) {
dimensions.push(this.getDimension(key, Math.round(inverters[key].P)));
}
}
return this.getChart(this.getInverterPowerChart(service, "inverters.output", inverters), dimensions);
},
getDimension: function (name, value) {
return {
name: name,
value: value
};
},
getChart: function (chart, dimensions) {
return {
chart: chart,
dimensions: dimensions
};
},
getChartId: function (service, suffix) {
return "fronius_" + service.name + "." + suffix;
},
convertToJson: function (httpBody) {
if (httpBody === null) return null;
var json = httpBody;
// can't parse if it's already a json object,
// the check enables easier testing if the httpBody is already valid JSON.
if (typeof httpBody !== "object") {
try {
json = JSON.parse(httpBody);
} catch (error) {
netdata.error("fronius: Got a response, but it is not valid JSON. Ignoring. Error: " + error.message);
return null;
}
}
return this.isResponseValid(json) ? json : null;
},
// some basic validation
isResponseValid: function (json) {
if (this.isUndefined(json.Body)) return false;
if (this.isUndefined(json.Body.Data)) return false;
if (this.isUndefined(json.Body.Data.Site)) return false;
return this.isDefined(json.Body.Data.Inverters);
},
// module.serviceExecute()
// this function is called only from this module
// its purpose is to prepare the request and call
// netdata.serviceExecute()
serviceExecute: function (name, uri, update_every) {
netdata.debug(this.name + ": " + name + ": url: " + uri + ", update_every: " + update_every);
var service = netdata.service({
name: name,
request: netdata.requestFromURL("http://" + uri),
update_every: update_every,
module: this
});
service.execute(this.processResponse);
},
configure: function (config) {
if (fronius.isUndefined(config.servers)) return 0;
var added = 0;
var len = config.servers.length;
while (len--) {
var server = config.servers[len];
if (fronius.isUndefined(server.update_every)) server.update_every = this.update_every;
if (fronius.areUndefined([server.name, server.hostname, server.api_path])) continue;
var url = server.hostname + server.api_path;
this.serviceExecute(server.name, url, server.update_every);
added++;
}
return added;
},
// module.update()
// this is called repeatedly to collect data, by calling
// netdata.serviceExecute()
update: function (service, callback) {
service.execute(function (serv, data) {
service.module.processResponse(serv, data);
callback();
});
},
isUndefined: function (value) {
return typeof value === "undefined";
},
areUndefined: function (valueArray) {
var i = 0;
for (i; i < valueArray.length; i++) {
if (this.isUndefined(valueArray[i])) return true;
}
return false;
},
isDefined: function (value) {
return typeof value !== "undefined";
}
};
module.exports = fronius;
|