@cryptotaxi247 / netdata-1 / commits / 77bf63432

Shorthand method definition according to EcmaScript 6

BrainDoctor committed Jul 5, 2017 at 16:49 UTC 77bf634326e382ddf06be3fc3169f76f893d26fd
2 files changed +327 -12
node.d/fronius.node.js
+12 -12
@@ -25,7 +25,7 @@ var fronius = {
25 base_priority: 60000,
26 charts: {},
27
28 - createBasicDimension: function (id, name) {
28 + createBasicDimension(id, name) {
29 return {
30 id: id, // the unique id of the dimension
31 name: name, // the name of the dimension
@@ -37,7 +37,7 @@ var fronius = {
37 },
38
39 // Gets the site power chart. Will be created if not existing.
40 - getSitePowerChart: function (service, id) {
40 + getSitePowerChart(service, id) {
41
42 var chart = fronius.charts[id];
43 if (fronius.isDefined(chart)) return chart;
@@ -66,7 +66,7 @@ var fronius = {
66 },
67
68 // Gets the site consumption chart. Will be created if not existing.
69 - getSiteConsumptionChart: function (service, id) {
69 + getSiteConsumptionChart(service, id) {
70
71 var chart = fronius.charts[id];
72 if (fronius.isDefined(chart)) return chart;
@@ -93,7 +93,7 @@ var fronius = {
93
94
95 // Gets the site consumption chart. Will be created if not existing.
96 - getSiteAutonomyChart: function (service, id) {
96 + getSiteAutonomyChart(service, id) {
97 var chart = fronius.charts[id];
98 if (fronius.isDefined(chart)) return chart;
99 var dim = {};
@@ -120,7 +120,7 @@ var fronius = {
120
121 // Gets the inverter power chart. Will be created if not existing.
122 // Needs the array of inverters in order to create a chart with all inverters as dimensions
123 - getInverterPowerChart: function (service, chartId, inverters) {
123 + getInverterPowerChart(service, chartId, inverters) {
124
125 var chart = fronius.charts[chartId];
126 if (fronius.isDefined(chart)) return chart;
@@ -159,7 +159,7 @@ var fronius = {
159
160 // Gets the inverter energy production chart for today. Will be created if not existing.
161 // Needs the array of inverters in order to create a chart with all inverters as dimensions
162 - getInverterEnergyTodayChart: function (service, chartId, inverters) {
162 + getInverterEnergyTodayChart(service, chartId, inverters) {
163
164 var chart = fronius.charts[chartId];
165 if (fronius.isDefined(chart)) return chart;
@@ -204,7 +204,7 @@ var fronius = {
204 },
205
206
207 - processResponse: function (service, content) {
207 + processResponse(service, content) {
208 if (content === null) return;
209 var json = JSON.parse(content);
210 // validating response
@@ -262,7 +262,7 @@ var fronius = {
262 // this function is called only from this module
263 // its purpose is to prepare the request and call
264 // netdata.serviceExecute()
265 - serviceExecute: function (name, uri, update_every) {
265 + serviceExecute(name, uri, update_every) {
266 netdata.debug(this.name + ': ' + name + ': url: ' + uri + ', update_every: ' + update_every);
267
268 var service = netdata.service({
@@ -276,7 +276,7 @@ var fronius = {
276 },
277
278
279 - configure: function (config) {
279 + configure(config) {
280 if (fronius.isUndefined(config.servers)) return 0;
281 var added = 0;
282 var len = config.servers.length;
@@ -294,18 +294,18 @@ var fronius = {
294 // module.update()
295 // this is called repeatedly to collect data, by calling
296 // netdata.serviceExecute()
297 - update: function (service, callback) {
297 + update(service, callback) {
298 service.execute(function (serv, data) {
299 service.module.processResponse(serv, data);
300 callback();
301 });
302 },
303
304 - isUndefined: function (value) {
304 + isUndefined(value) {
305 return typeof value === 'undefined';
306 },
307
308 - isDefined: function (value) {
308 + isDefined(value) {
309 return typeof value !== 'undefined';
310 }
311
node.d/stiebeleltron.node.js new
+315
@@ -0,0 +1,315 @@
1 +'use strict';
2 +
3 +// This program will connect to one or more Fronius Symo Inverters.
4 +// to get the Solar Power Generated (current, today).
5 +
6 +// example configuration in netdata/conf.d/node.d/fronius.conf.md
7 +
8 +var url = require('url');
9 +var http = require('http');
10 +var netdata = require('netdata');
11 +
12 +netdata.debug('loaded ' + __filename + ' plugin');
13 +
14 +const power_grid_id = 'p_grid';
15 +const power_pv_id = 'p_pv';
16 +const power_accu_id = 'p_akku'; // not my typo! Using the ID from the API
17 +const consumption_load_id = 'p_load';
18 +const autonomy_id = 'rel_autonomy';
19 +const consumption_self_id = 'rel_selfconsumption';
20 +
21 +var fronius = {
22 + name: "Fronius",
23 + enable_autodetect: false,
24 + update_every: 5,
25 + base_priority: 60000,
26 + charts: {},
27 +
28 + createBasicDimension: function (id, name) {
29 + return {
30 + id: id, // the unique id of the dimension
31 + name: name, // the name of the dimension
32 + algorithm: netdata.chartAlgorithms.absolute,// the id of the netdata algorithm
33 + multiplier: 1, // the multiplier
34 + divisor: 1, // the divisor
35 + hidden: false // is hidden (boolean)
36 + }
37 + },
38 +
39 + // Gets the site power chart. Will be created if not existing.
40 + getSitePowerChart: function (service, id) {
41 +
42 + var chart = fronius.charts[id];
43 + if (fronius.isDefined(chart)) return chart;
44 +
45 + var dim = {};
46 + dim[power_grid_id] = this.createBasicDimension(power_grid_id, "Grid");
47 + dim[power_pv_id] = this.createBasicDimension(power_pv_id, "Photovoltaics");
48 + dim[power_accu_id] = this.createBasicDimension(power_accu_id, "Accumulator");
49 +
50 + chart = {
51 + id: id, // the unique id of the chart
52 + name: '', // the unique name of the chart
53 + title: service.name + ' Current Site Power', // the title of the chart
54 + units: 'W', // the units of the chart dimensions
55 + family: 'Power', // the family of the chart
56 + context: 'fronius.power', // the context of the chart
57 + type: netdata.chartTypes.area, // the type of the chart
58 + priority: fronius.base_priority + 1, // the priority relative to others in the same family
59 + update_every: service.update_every, // the expected update frequency of the chart
60 + dimensions: dim
61 + };
62 + chart = service.chart(id, chart);
63 + fronius.charts[id] = chart;
64 +
65 + return chart;
66 + },
67 +
68 + // Gets the site consumption chart. Will be created if not existing.
69 + getSiteConsumptionChart: function (service, id) {
70 +
71 + var chart = fronius.charts[id];
72 + if (fronius.isDefined(chart)) return chart;
73 + var dim = {};
74 + dim[consumption_load_id] = this.createBasicDimension(consumption_load_id, "Load");
75 +
76 + chart = {
77 + id: id, // the unique id of the chart
78 + name: '', // the unique name of the chart
79 + title: service.name + ' Current Load', // the title of the chart
80 + units: 'W', // the units of the chart dimensions
81 + family: 'Consumption', // the family of the chart
82 + context: 'fronius.consumption', // the context of the chart
83 + type: netdata.chartTypes.area, // the type of the chart
84 + priority: fronius.base_priority + 2, // the priority relative to others in the same family
85 + update_every: service.update_every, // the expected update frequency of the chart
86 + dimensions: dim
87 + };
88 + chart = service.chart(id, chart);
89 + fronius.charts[id] = chart;
90 +
91 + return chart;
92 + },
93 +
94 +
95 + // Gets the site consumption chart. Will be created if not existing.
96 + getSiteAutonomyChart: function (service, id) {
97 + var chart = fronius.charts[id];
98 + if (fronius.isDefined(chart)) return chart;
99 + var dim = {};
100 + dim[autonomy_id] = this.createBasicDimension(autonomy_id, "Autonomy");
101 + dim[consumption_self_id] = this.createBasicDimension(consumption_self_id, "Self Consumption");
102 +
103 + chart = {
104 + id: id, // the unique id of the chart
105 + name: '', // the unique name of the chart
106 + title: service.name + ' Current Autonomy', // the title of the chart
107 + units: '%', // the units of the chart dimensions
108 + family: 'Autonomy', // the family of the chart
109 + context: 'fronius.autonomy', // the context of the chart
110 + type: netdata.chartTypes.area, // the type of the chart
111 + priority: fronius.base_priority + 3, // the priority relative to others in the same family
112 + update_every: service.update_every, // the expected update frequency of the chart
113 + dimensions: dim
114 + };
115 + chart = service.chart(id, chart);
116 + fronius.charts[id] = chart;
117 +
118 + return chart;
119 + },
120 +
121 + // Gets the inverter power chart. Will be created if not existing.
122 + // Needs the array of inverters in order to create a chart with all inverters as dimensions
123 + getInverterPowerChart: function (service, chartId, inverters) {
124 +
125 + var chart = fronius.charts[chartId];
126 + if (fronius.isDefined(chart)) return chart;
127 +
128 + var dim = {};
129 +
130 + var inverter_count = Object.keys(inverters).length;
131 + var inverter = inverters[inverter_count.toString()];
132 + var i = 1;
133 + for (i; i <= inverter_count; i++) {
134 + if (fronius.isUndefined(inverter)) {
135 + netdata.error("Expected an Inverter with a numerical name! " +
136 + "Have a look at your JSON output to verify.");
137 + continue;
138 + }
139 + dim[i.toString()] = this.createBasicDimension("inverter_" + i, "Inverter " + i);
140 + }
141 +
142 + chart = {
143 + id: chartId, // the unique id of the chart
144 + name: '', // the unique name of the chart
145 + title: service.name + ' Current Inverter Output', // the title of the chart
146 + units: 'W', // the units of the chart dimensions
147 + family: 'Inverters', // the family of the chart
148 + context: 'fronius.inverter', // the context of the chart
149 + type: netdata.chartTypes.stacked, // the type of the chart
150 + priority: fronius.base_priority + 4, // the priority relative to others in the same family
151 + update_every: service.update_every, // the expected update frequency of the chart
152 + dimensions: dim
153 + };
154 + chart = service.chart(chartId, chart);
155 + fronius.charts[chartId] = chart;
156 +
157 + return chart;
158 + },
159 +
160 + // Gets the inverter energy production chart for today. Will be created if not existing.
161 + // Needs the array of inverters in order to create a chart with all inverters as dimensions
162 + getInverterEnergyTodayChart: function (service, chartId, inverters) {
163 +
164 + var chart = fronius.charts[chartId];
165 + if (fronius.isDefined(chart)) return chart;
166 +
167 + var dim = {};
168 +
169 + var inverter_count = Object.keys(inverters).length;
170 + var inverter = inverters[inverter_count.toString()];
171 + var i = 1;
172 + for (i; i <= inverter_count; i++) {
173 + if (fronius.isUndefined(inverter)) {
174 + netdata.error("Expected an Inverter with a numerical name! " +
175 + "Have a look at your JSON output to verify.");
176 + continue;
177 + }
178 + dim[i.toString()] = {
179 + id: 'inverter_' + i, // the unique id of the dimension
180 + name: 'Inverter ' + i, // the name of the dimension
181 + algorithm: netdata.chartAlgorithms.absolute,// the id of the netdata algorithm
182 + multiplier: 1, // the multiplier
183 + divisor: 1000, // the divisor
184 + hidden: false // is hidden (boolean)
185 + };
186 + }
187 +
188 + chart = {
189 + id: chartId, // the unique id of the chart
190 + name: '', // the unique name of the chart
191 + title: service.name + ' Inverter Energy production for today', // the title of the chart
192 + units: 'kWh', // the units of the chart dimensions
193 + family: 'Inverters', // the family of the chart
194 + context: 'fronius.inverter', // the context of the chart
195 + type: netdata.chartTypes.stacked, // the type of the chart
196 + priority: fronius.base_priority + 5, // the priority relative to others in the same family
197 + update_every: service.update_every, // the expected update frequency of the chart
198 + dimensions: dim
199 + };
200 + chart = service.chart(chartId, chart);
201 + fronius.charts[chartId] = chart;
202 +
203 + return chart;
204 + },
205 +
206 +
207 + processResponse: function (service, content) {
208 + if (content === null) return;
209 + var json = JSON.parse(content);
210 + // validating response
211 + if (fronius.isUndefined(json.Body)) return;
212 + if (fronius.isUndefined(json.Body.Data)) return;
213 + if (fronius.isUndefined(json.Body.Data.Site)) return;
214 + if (fronius.isUndefined(json.Body.Data.Inverters)) return;
215 +
216 + // add the service
217 + if (service.added !== true) service.commit();
218 +
219 + var site = json.Body.Data.Site;
220 +
221 + // Site Current Power Chart
222 + service.begin(fronius.getSitePowerChart(service, 'fronius_' + service.name + '.power'));
223 + service.set(power_grid_id, Math.round(site.P_Grid));
224 + service.set(power_pv_id, Math.round(site.P_PV));
225 + service.set(power_accu_id, Math.round(site.P_Akku));
226 + service.end();
227 +
228 + // Site Consumption Chart
229 + var consumption = site.P_Load;
230 + if (consumption === null) consumption = 0;
231 + consumption *= -1;
232 +
233 + service.begin(fronius.getSiteConsumptionChart(service, 'fronius_' + service.name + '.consumption'));
234 + service.set(consumption_load_id, Math.round(consumption));
235 + service.end();
236 +
237 + // Site Autonomy Chart
238 + service.begin(fronius.getSiteAutonomyChart(service, 'fronius_' + service.name + '.autonomy'));
239 + service.set(autonomy_id, Math.round(site.rel_Autonomy));
240 + service.set(consumption_self_id, Math.round(site.rel_SelfConsumption));
241 + service.end();
242 +
243 + // Inverters
244 + var inverters = json.Body.Data.Inverters;
245 + var inverter_count = Object.keys(inverters).length;
246 + if (inverter_count <= 0) return;
247 + var i = 1;
248 + for (i; i <= inverter_count; i++) {
249 + var inverter = inverters[i];
250 + if (fronius.isUndefined(inverter)) continue;
251 + netdata.debug("Setting values");
252 + service.begin(fronius.getInverterPowerChart(service, 'fronius_' + service.name + '.inverters.output', inverters));
253 + service.set(i.toString(), Math.round(inverter.P));
254 + service.end();
255 + service.begin(fronius.getInverterEnergyTodayChart(service, 'fronius_' + service.name + '.inverters.today', inverters));
256 + service.set(i.toString(), Math.round(inverter.E_Day));
257 + service.end();
258 + }
259 + },
260 +
261 + // module.serviceExecute()
262 + // this function is called only from this module
263 + // its purpose is to prepare the request and call
264 + // netdata.serviceExecute()
265 + serviceExecute: function (name, uri, update_every) {
266 + netdata.debug(this.name + ': ' + name + ': url: ' + uri + ', update_every: ' + update_every);
267 +
268 + var service = netdata.service({
269 + name: name,
270 + request: netdata.requestFromURL('http://' + uri),
271 + update_every: update_every,
272 + module: this
273 + });
274 + service.request.method = 'GET';
275 + service.execute(this.processResponse);
276 + },
277 +
278 +
279 + configure: function (config) {
280 + if (fronius.isUndefined(config.servers)) return 0;
281 + var added = 0;
282 + var len = config.servers.length;
283 + while (len--) {
284 + var server = config.servers[len];
285 + if (fronius.isUndefined(server.update_every)) server.update_every = this.update_every;
286 +
287 + var url = server.hostname + server.api_path;
288 + this.serviceExecute(server.name, url, server.update_every);
289 + added++;
290 + }
291 + return added;
292 + },
293 +
294 + // module.update()
295 + // this is called repeatedly to collect data, by calling
296 + // netdata.serviceExecute()
297 + update: function (service, callback) {
298 + service.execute(function (serv, data) {
299 + service.module.processResponse(serv, data);
300 + callback();
301 + });
302 + },
303 +
304 + isUndefined: function (value) {
305 + return typeof value === 'undefined';
306 + },
307 +
308 + isDefined: function (value) {
309 + return typeof value !== 'undefined';
310 + }
311 +
312 +
313 +};
314 +
315 +module.exports = fronius;