]> arthur.barton.de Git - netdata.git/blob - node.d/sma_webbox.node.js
converted from dos to unix
[netdata.git] / node.d / sma_webbox.node.js
1 'use strict';
2
3 // This program will connect to one or more SMA Sunny Webboxes
4 // to get the Solar Power Generated (current, today, total).
5
6 // example configuration in /etc/netdata/sma_webbox.conf
7 /*
8 {
9         "enable_autodetect": false,
10         "update_every": 5,
11         "servers": [
12                 {
13                         "name": "plant1",
14                         "hostname": "10.0.1.1",
15                         "update_every": 10
16                 },
17                 {
18                         "name": "plant2",
19                         "hostname": "10.0.2.1",
20                         "update_every": 15
21                 }
22         ]
23 }
24 */
25
26 var url = require('url');
27 var http = require('http');
28 var netdata = require('netdata');
29
30 if(netdata.options.DEBUG === true) netdata.debug('loaded ' + __filename + ' plugin');
31
32 var webbox = {
33         name: __filename,
34         enable_autodetect: true,
35         update_every: 1,
36
37         charts: {},
38
39         processResponse: function(service, data) {
40                 if(data !== null) {
41                         var r = JSON.parse(data);
42
43                         var d = {
44                                 'GriPwr': {
45                                         unit: null,
46                                         value: null
47                                 },
48                                 'GriEgyTdy': {
49                                         unit: null,
50                                         value: null
51                                 },
52                                 'GriEgyTot': {
53                                         unit: null,
54                                         value: null
55                                 }
56                         };
57
58                         // parse the webbox response
59                         // and put it in our d object
60                         var found = 0;
61                         var len = r.result.overview.length;
62                         while(len--) {
63                                 var e = r.result.overview[len];
64                                 if(typeof(d[e.meta]) !== 'undefined') {
65                                         found++;
66                                         d[e.meta].value = e.value;
67                                         d[e.meta].unit = e.unit;
68                                 }
69                         }
70
71                         // add the service
72                         if(found > 0 && service.added !== true)
73                                 service.commit();
74
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];
79
80                                 if(typeof chart === 'undefined') {
81                                         chart = {
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: service.update_every,                             // the expected update frequency of the chart
91                                                 dimensions: {
92                                                         'GriPwr': {
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)
99                                                         }
100                                                 }
101                                         };
102
103                                         chart = service.chart(id, chart);
104                                         webbox.charts[id] = chart;
105                                 }
106
107                                 service.begin(chart);
108                                 service.set('GriPwr', Math.round(d['GriPwr'].value));
109                                 service.end();
110                         }
111
112                         if(d['GriEgyTdy'].value !== null) {
113                                 var id = 'sma_webbox_' + service.name + '.today';
114                                 var chart = webbox.charts[id];
115
116                                 if(typeof chart === 'undefined') {
117                                         chart = {
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: service.update_every,                             // the expected update frequency of the chart
127                                                 dimensions: {
128                                                         'GriEgyTdy': {
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)
135                                                         }
136                                                 }
137                                         };
138
139                                         chart = service.chart(id, chart);
140                                         webbox.charts[id] = chart;
141                                 }
142
143                                 service.begin(chart);
144                                 service.set('GriEgyTdy', Math.round(d['GriEgyTdy'].value * 1000));
145                                 service.end();
146                         }
147
148                         if(d['GriEgyTot'].value !== null) {
149                                 var id = 'sma_webbox_' + service.name + '.total';
150                                 var chart = webbox.charts[id];
151
152                                 if(typeof chart === 'undefined') {
153                                         chart = {
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: service.update_every,                             // the expected update frequency of the chart
163                                                 dimensions: {
164                                                         'GriEgyTot': {
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)
171                                                         }
172                                                 }
173                                         };
174
175                                         chart = service.chart(id, chart);
176                                         webbox.charts[id] = chart;
177                                 }
178
179                                 service.begin(chart);
180                                 service.set('GriEgyTot', Math.round(d['GriEgyTot'].value * 1000));
181                                 service.end();
182                         }
183                 }
184         },
185
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);
192
193                 var service = netdata.service({
194                         name: name,
195                         request: netdata.requestFromURL('http://' + hostname + '/rpc'),
196                         update_every: update_every,
197                         module: this
198                 });
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;
202
203                 service.execute(this.processResponse);
204         },
205
206         configure: function(config) {
207                 var added = 0;
208
209                 if(typeof(config.servers) !== 'undefined') {
210                         var len = config.servers.length;
211                         while(len--) {
212                                 if(typeof config.servers[len].update_every === 'undefined')
213                                         config.servers[len].update_every = this.update_every;
214
215                                 if(config.servers[len].update_every < 5)
216                                         config.servers[len].update_every = 5;
217
218                                 this.serviceExecute(config.servers[len].name, config.servers[len].hostname, config.servers[len].update_every);
219                                 added++;
220                         }
221                 }
222
223                 return added;
224         },
225
226         // module.update()
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);
232                         callback();
233                 });
234         },
235 };
236
237 module.exports = webbox;