3 // This program will connect to one or more SMA Sunny Webboxes
4 // to get the Solar Power Generated (current, today, total).
6 // example configuration in /etc/netdata/sma_webbox.conf
9 "enable_autodetect": false,
14 "hostname": "10.0.1.1",
19 "hostname": "10.0.2.1",
26 var url = require('url');
27 var http = require('http');
28 var netdata = require('netdata');
30 if(netdata.options.DEBUG === true) netdata.debug('loaded ' + __filename + ' plugin');
34 enable_autodetect: true,
39 processResponse: function(service, data) {
41 var r = JSON.parse(data);
58 // parse the webbox response
59 // and put it in our d object
61 var len = r.result.overview.length;
63 var e = r.result.overview[len];
64 if(typeof(d[e.meta]) !== 'undefined') {
66 d[e.meta].value = e.value;
67 d[e.meta].unit = e.unit;
72 if(found > 0 && service.added !== true)
73 netdata.serviceAdd(service);
75 // Grid Current Power Chart
76 if(d['GriPwr'].value !== null) {
77 var id = 'sma_webbox_' + service.name + '.current';
78 var chart = webbox.charts[id];
80 if(typeof chart === 'undefined') {
82 id: id, // the unique id of the chart
83 name: '', // the unique name of the chart
84 title: service.name + ' Current Grid Power', // the title of the chart
85 units: d['GriPwr'].unit, // the units of the chart dimensions
86 family: 'sma_webbox_' + service.name, // the family of the chart
87 category: 'sma_webbox_' + service.name, // the category of the chart
88 type: netdata.chartTypes.area, // the type of the chart
89 priority: 1000, // the priority relative to others in the same family and category
90 update_every: Math.round(service.update_every / 1000), // the expected update frequency of the chart
93 id: 'GriPwr', // the unique id of the dimension
94 name: 'power', // the name of the dimension
95 algorithm: netdata.chartAlgorithms.absolute,// the id of the netdata algorithm
96 multiplier: 1, // the multiplier
97 divisor: 1, // the divisor
98 hidden: false // is hidden (boolean)
103 chart = service.chart(id, chart);
104 webbox.charts[id] = chart;
107 service.begin(chart);
108 service.set('GriPwr', Math.round(d['GriPwr'].value));
112 if(d['GriEgyTdy'].value !== null) {
113 var id = 'sma_webbox_' + service.name + '.today';
114 var chart = webbox.charts[id];
116 if(typeof chart === 'undefined') {
118 id: id, // the unique id of the chart
119 name: '', // the unique name of the chart
120 title: service.name + ' Today Grid Power', // the title of the chart
121 units: d['GriEgyTdy'].unit, // the units of the chart dimensions
122 family: 'sma_webbox_' + service.name, // the family of the chart
123 category: 'sma_webbox_' + service.name, // the category of the chart
124 type: netdata.chartTypes.area, // the type of the chart
125 priority: 1000, // the priority relative to others in the same family and category
126 update_every: Math.round(service.update_every / 1000), // the expected update frequency of the chart
129 id: 'GriEgyTdy', // the unique id of the dimension
130 name: 'power', // the name of the dimension
131 algorithm: netdata.chartAlgorithms.absolute,// the id of the netdata algorithm
132 multiplier: 1, // the multiplier
133 divisor: 1000, // the divisor
134 hidden: false // is hidden (boolean)
139 chart = service.chart(id, chart);
140 webbox.charts[id] = chart;
143 service.begin(chart);
144 service.set('GriEgyTdy', Math.round(d['GriEgyTdy'].value * 1000));
148 if(d['GriEgyTot'].value !== null) {
149 var id = 'sma_webbox_' + service.name + '.total';
150 var chart = webbox.charts[id];
152 if(typeof chart === 'undefined') {
154 id: id, // the unique id of the chart
155 name: '', // the unique name of the chart
156 title: service.name + ' Total Grid Power', // the title of the chart
157 units: d['GriEgyTot'].unit, // the units of the chart dimensions
158 family: 'sma_webbox_' + service.name, // the family of the chart
159 category: 'sma_webbox_' + service.name, // the category of the chart
160 type: netdata.chartTypes.area, // the type of the chart
161 priority: 1000, // the priority relative to others in the same family and category
162 update_every: Math.round(service.update_every / 1000), // the expected update frequency of the chart
165 id: 'GriEgyTot', // the unique id of the dimension
166 name: 'power', // the name of the dimension
167 algorithm: netdata.chartAlgorithms.absolute,// the id of the netdata algorithm
168 multiplier: 1, // the multiplier
169 divisor: 1000, // the divisor
170 hidden: false // is hidden (boolean)
175 chart = service.chart(id, chart);
176 webbox.charts[id] = chart;
179 service.begin(chart);
180 service.set('GriEgyTot', Math.round(d['GriEgyTot'].value * 1000));
186 // module.serviceExecute()
187 // this function is called only from this module
188 // its purpose is to prepare the request and call
189 // netdata.serviceExecute()
190 serviceExecute: function(name, hostname, update_every) {
191 if(netdata.options.DEBUG === true) netdata.debug(this.name + ': ' + name + ': hostname: ' + hostname + ', update_every: ' + update_every);
195 request: netdata.requestFromURL('http://' + hostname + '/rpc'),
196 update_every: update_every,
201 service.postData = 'RPC={"proc":"GetPlantOverview","format":"JSON","version":"1.0","id":"1"}';
202 service.request.method = 'POST';
203 service.request.headers['Content-Length'] = service.postData.length;
205 netdata.serviceExecute(service, this.processResponse);
208 configure: function(config) {
211 if(typeof(config.servers) !== 'undefined') {
212 var len = config.servers.length;
214 if(typeof config.servers[len].update_every === 'undefined')
215 config.servers[len].update_every = this.update_every;
217 config.servers[len].update_every = config.servers[len].update_every * 1000;
219 if(config.servers[len].update_every < 5000)
220 config.servers[len].update_every = 5000;
222 this.serviceExecute(config.servers[len].name, config.servers[len].hostname, config.servers[len].update_every);
231 // this is called repeatidly to collect data, by calling
232 // netdata.serviceExecute()
233 update: function(service, callback) {
234 netdata.serviceExecute(service, function(serv, data) {
235 service.module.processResponse(serv, data);
241 module.exports = webbox;