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/node.d/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)
75 // Grid Current Power Chart
76 if(d['GriPwr'].value !== null) {
77 var id = 'smawebbox_' + 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: 'now', // the family of the chart
87 context: 'smawebbox.grid_power', // the context of the chart
88 type: netdata.chartTypes.area, // the type of the chart
89 priority: webbox.base_priority + 1, // the priority relative to others in the same family
90 update_every: service.update_every, // 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 = 'smawebbox_' + 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: 'today', // the family of the chart
123 context: 'smawebbox.grid_power_today', // the context of the chart
124 type: netdata.chartTypes.area, // the type of the chart
125 priority: webbox.base_priority + 2, // the priority relative to others in the same family
126 update_every: service.update_every, // 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 = 'smawebbox_' + 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: 'total', // the family of the chart
159 context: 'smawebbox.grid_power_total', // the context of the chart
160 type: netdata.chartTypes.area, // the type of the chart
161 priority: webbox.base_priority + 3, // the priority relative to others in the same family
162 update_every: service.update_every, // 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);
193 var service = netdata.service({
195 request: netdata.requestFromURL('http://' + hostname + '/rpc'),
196 update_every: update_every,
199 service.postData = 'RPC={"proc":"GetPlantOverview","format":"JSON","version":"1.0","id":"1"}';
200 service.request.method = 'POST';
201 service.request.headers['Content-Length'] = service.postData.length;
203 service.execute(this.processResponse);
206 configure: function(config) {
209 if(typeof(config.servers) !== 'undefined') {
210 var len = config.servers.length;
212 if(typeof config.servers[len].update_every === 'undefined')
213 config.servers[len].update_every = this.update_every;
215 if(config.servers[len].update_every < 5)
216 config.servers[len].update_every = 5;
218 this.serviceExecute(config.servers[len].name, config.servers[len].hostname, config.servers[len].update_every);
227 // this is called repeatidly to collect data, by calling
228 // netdata.serviceExecute()
229 update: function(service, callback) {
230 service.execute(function(serv, data) {
231 service.module.processResponse(serv, data);
237 module.exports = webbox;