sma_webbox.node.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. 'use strict';
  2. // SPDX-License-Identifier: GPL-3.0-or-later
  3. // This program will connect to one or more SMA Sunny Webboxes
  4. // to get the Solar Power Generated (current, today, total).
  5. // example configuration in /etc/netdata/node.d/sma_webbox.conf
  6. /*
  7. {
  8. "enable_autodetect": false,
  9. "update_every": 5,
  10. "servers": [
  11. {
  12. "name": "plant1",
  13. "hostname": "10.0.1.1",
  14. "update_every": 10
  15. },
  16. {
  17. "name": "plant2",
  18. "hostname": "10.0.2.1",
  19. "update_every": 15
  20. }
  21. ]
  22. }
  23. */
  24. require('url');
  25. require('http');
  26. var netdata = require('netdata');
  27. if(netdata.options.DEBUG === true) netdata.debug('loaded ' + __filename + ' plugin');
  28. var webbox = {
  29. name: __filename,
  30. enable_autodetect: true,
  31. update_every: 1,
  32. base_priority: 60000,
  33. charts: {},
  34. processResponse: function(service, data) {
  35. if(data !== null) {
  36. var r = JSON.parse(data);
  37. var d = {
  38. 'GriPwr': {
  39. unit: null,
  40. value: null
  41. },
  42. 'GriEgyTdy': {
  43. unit: null,
  44. value: null
  45. },
  46. 'GriEgyTot': {
  47. unit: null,
  48. value: null
  49. }
  50. };
  51. // parse the webbox response
  52. // and put it in our d object
  53. var found = 0;
  54. var len = r.result.overview.length;
  55. while(len--) {
  56. var e = r.result.overview[len];
  57. if(typeof(d[e.meta]) !== 'undefined') {
  58. found++;
  59. d[e.meta].value = e.value;
  60. d[e.meta].unit = e.unit;
  61. }
  62. }
  63. // add the service
  64. if(found > 0 && service.added !== true)
  65. service.commit();
  66. // Grid Current Power Chart
  67. if(d['GriPwr'].value !== null) {
  68. const id = 'smawebbox_' + service.name + '.current';
  69. let chart = webbox.charts[id];
  70. if(typeof chart === 'undefined') {
  71. chart = {
  72. id: id, // the unique id of the chart
  73. name: '', // the unique name of the chart
  74. title: service.name + ' Current Grid Power', // the title of the chart
  75. units: d['GriPwr'].unit, // the units of the chart dimensions
  76. family: 'now', // the family of the chart
  77. context: 'smawebbox.grid_power', // the context of the chart
  78. type: netdata.chartTypes.area, // the type of the chart
  79. priority: webbox.base_priority + 1, // the priority relative to others in the same family
  80. update_every: service.update_every, // the expected update frequency of the chart
  81. dimensions: {
  82. 'GriPwr': {
  83. id: 'GriPwr', // the unique id of the dimension
  84. name: 'power', // the name of the dimension
  85. algorithm: netdata.chartAlgorithms.absolute,// the id of the netdata algorithm
  86. multiplier: 1, // the multiplier
  87. divisor: 1, // the divisor
  88. hidden: false // is hidden (boolean)
  89. }
  90. }
  91. };
  92. chart = service.chart(id, chart);
  93. webbox.charts[id] = chart;
  94. }
  95. service.begin(chart);
  96. service.set('GriPwr', Math.round(d['GriPwr'].value));
  97. service.end();
  98. }
  99. if(d['GriEgyTdy'].value !== null) {
  100. const id = 'smawebbox_' + service.name + '.today';
  101. let chart = webbox.charts[id];
  102. if(typeof chart === 'undefined') {
  103. chart = {
  104. id: id, // the unique id of the chart
  105. name: '', // the unique name of the chart
  106. title: service.name + ' Today Grid Power', // the title of the chart
  107. units: d['GriEgyTdy'].unit, // the units of the chart dimensions
  108. family: 'today', // the family of the chart
  109. context: 'smawebbox.grid_power_today', // the context of the chart
  110. type: netdata.chartTypes.area, // the type of the chart
  111. priority: webbox.base_priority + 2, // the priority relative to others in the same family
  112. update_every: service.update_every, // the expected update frequency of the chart
  113. dimensions: {
  114. 'GriEgyTdy': {
  115. id: 'GriEgyTdy', // the unique id of the dimension
  116. name: 'power', // the name of the dimension
  117. algorithm: netdata.chartAlgorithms.absolute,// the id of the netdata algorithm
  118. multiplier: 1, // the multiplier
  119. divisor: 1000, // the divisor
  120. hidden: false // is hidden (boolean)
  121. }
  122. }
  123. };
  124. chart = service.chart(id, chart);
  125. webbox.charts[id] = chart;
  126. }
  127. service.begin(chart);
  128. service.set('GriEgyTdy', Math.round(d['GriEgyTdy'].value * 1000));
  129. service.end();
  130. }
  131. if(d['GriEgyTot'].value !== null) {
  132. const id = 'smawebbox_' + service.name + '.total';
  133. let chart = webbox.charts[id];
  134. if(typeof chart === 'undefined') {
  135. chart = {
  136. id: id, // the unique id of the chart
  137. name: '', // the unique name of the chart
  138. title: service.name + ' Total Grid Power', // the title of the chart
  139. units: d['GriEgyTot'].unit, // the units of the chart dimensions
  140. family: 'total', // the family of the chart
  141. context: 'smawebbox.grid_power_total', // the context of the chart
  142. type: netdata.chartTypes.area, // the type of the chart
  143. priority: webbox.base_priority + 3, // the priority relative to others in the same family
  144. update_every: service.update_every, // the expected update frequency of the chart
  145. dimensions: {
  146. 'GriEgyTot': {
  147. id: 'GriEgyTot', // the unique id of the dimension
  148. name: 'power', // the name of the dimension
  149. algorithm: netdata.chartAlgorithms.absolute,// the id of the netdata algorithm
  150. multiplier: 1, // the multiplier
  151. divisor: 1000, // the divisor
  152. hidden: false // is hidden (boolean)
  153. }
  154. }
  155. };
  156. chart = service.chart(id, chart);
  157. webbox.charts[id] = chart;
  158. }
  159. service.begin(chart);
  160. service.set('GriEgyTot', Math.round(d['GriEgyTot'].value * 1000));
  161. service.end();
  162. }
  163. }
  164. },
  165. // module.serviceExecute()
  166. // this function is called only from this module
  167. // its purpose is to prepare the request and call
  168. // netdata.serviceExecute()
  169. serviceExecute: function(name, hostname, update_every) {
  170. if(netdata.options.DEBUG === true) netdata.debug(this.name + ': ' + name + ': hostname: ' + hostname + ', update_every: ' + update_every);
  171. var service = netdata.service({
  172. name: name,
  173. request: netdata.requestFromURL('http://' + hostname + '/rpc'),
  174. update_every: update_every,
  175. module: this
  176. });
  177. service.postData = 'RPC={"proc":"GetPlantOverview","format":"JSON","version":"1.0","id":"1"}';
  178. service.request.method = 'POST';
  179. service.request.headers['Content-Length'] = service.postData.length;
  180. service.execute(this.processResponse);
  181. },
  182. configure: function(config) {
  183. var added = 0;
  184. if(typeof(config.servers) !== 'undefined') {
  185. var len = config.servers.length;
  186. while(len--) {
  187. if(typeof config.servers[len].update_every === 'undefined')
  188. config.servers[len].update_every = this.update_every;
  189. if(config.servers[len].update_every < 5)
  190. config.servers[len].update_every = 5;
  191. this.serviceExecute(config.servers[len].name, config.servers[len].hostname, config.servers[len].update_every);
  192. added++;
  193. }
  194. }
  195. return added;
  196. },
  197. // module.update()
  198. // this is called repeatidly to collect data, by calling
  199. // netdata.serviceExecute()
  200. update: function(service, callback) {
  201. service.execute(function(serv, data) {
  202. service.module.processResponse(serv, data);
  203. callback();
  204. });
  205. },
  206. };
  207. module.exports = webbox;