fronius.node.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. "use strict";
  2. // SPDX-License-Identifier: GPL-3.0-or-later
  3. // This program will connect to one or more Fronius Symo Inverters.
  4. // to get the Solar Power Generated (current, today).
  5. // example configuration in netdata/conf.d/node.d/fronius.conf.md
  6. require("url");
  7. require("http");
  8. var netdata = require("netdata");
  9. netdata.debug("loaded " + __filename + " plugin");
  10. var fronius = {
  11. name: "Fronius",
  12. enable_autodetect: false,
  13. update_every: 5,
  14. base_priority: 60000,
  15. charts: {},
  16. powerGridId: "p_grid",
  17. powerPvId: "p_pv",
  18. powerAccuId: "p_akku", // not my typo! Using the ID from the AP
  19. consumptionLoadId: "p_load",
  20. autonomyId: "rel_autonomy",
  21. consumptionSelfId: "rel_selfconsumption",
  22. solarConsumptionId: "solar_consumption",
  23. energyTodayId: "e_day",
  24. energyYearId: "e_year",
  25. createBasicDimension: function (id, name, divisor) {
  26. return {
  27. id: id, // the unique id of the dimension
  28. name: name, // the name of the dimension
  29. algorithm: netdata.chartAlgorithms.absolute,// the id of the netdata algorithm
  30. multiplier: 1, // the multiplier
  31. divisor: divisor, // the divisor
  32. hidden: false // is hidden (boolean)
  33. };
  34. },
  35. // Gets the site power chart. Will be created if not existing.
  36. getSitePowerChart: function (service, suffix) {
  37. var id = this.getChartId(service, suffix);
  38. var chart = fronius.charts[id];
  39. if (fronius.isDefined(chart)) return chart;
  40. var dim = {};
  41. dim[fronius.powerGridId] = this.createBasicDimension(fronius.powerGridId, "grid", 1);
  42. dim[fronius.powerPvId] = this.createBasicDimension(fronius.powerPvId, "photovoltaics", 1);
  43. dim[fronius.powerAccuId] = this.createBasicDimension(fronius.powerAccuId, "accumulator", 1);
  44. chart = {
  45. id: id, // the unique id of the chart
  46. name: "", // the unique name of the chart
  47. title: service.name + " Current Site Power", // the title of the chart
  48. units: "W", // the units of the chart dimensions
  49. family: "power", // the family of the chart
  50. context: "fronius.power", // the context of the chart
  51. type: netdata.chartTypes.area, // the type of the chart
  52. priority: fronius.base_priority + 1, // the priority relative to others in the same family
  53. update_every: service.update_every, // the expected update frequency of the chart
  54. dimensions: dim
  55. };
  56. chart = service.chart(id, chart);
  57. fronius.charts[id] = chart;
  58. return chart;
  59. },
  60. // Gets the site consumption chart. Will be created if not existing.
  61. getSiteConsumptionChart: function (service, suffix) {
  62. var id = this.getChartId(service, suffix);
  63. var chart = fronius.charts[id];
  64. if (fronius.isDefined(chart)) return chart;
  65. var dim = {};
  66. dim[fronius.consumptionLoadId] = this.createBasicDimension(fronius.consumptionLoadId, "load", 1);
  67. chart = {
  68. id: id, // the unique id of the chart
  69. name: "", // the unique name of the chart
  70. title: service.name + " Current Load", // the title of the chart
  71. units: "W", // the units of the chart dimensions
  72. family: "consumption", // the family of the chart
  73. context: "fronius.consumption", // the context of the chart
  74. type: netdata.chartTypes.area, // the type of the chart
  75. priority: fronius.base_priority + 2, // the priority relative to others in the same family
  76. update_every: service.update_every, // the expected update frequency of the chart
  77. dimensions: dim
  78. };
  79. chart = service.chart(id, chart);
  80. fronius.charts[id] = chart;
  81. return chart;
  82. },
  83. // Gets the site consumption chart. Will be created if not existing.
  84. getSiteAutonomyChart: function (service, suffix) {
  85. var id = this.getChartId(service, suffix);
  86. var chart = fronius.charts[id];
  87. if (fronius.isDefined(chart)) return chart;
  88. var dim = {};
  89. dim[fronius.autonomyId] = this.createBasicDimension(fronius.autonomyId, "autonomy", 1);
  90. dim[fronius.consumptionSelfId] = this.createBasicDimension(fronius.consumptionSelfId, "self_consumption", 1);
  91. dim[fronius.solarConsumptionId] = this.createBasicDimension(fronius.solarConsumptionId, "solar_consumption", 1);
  92. chart = {
  93. id: id, // the unique id of the chart
  94. name: "", // the unique name of the chart
  95. title: service.name + " Current Autonomy", // the title of the chart
  96. units: "%", // the units of the chart dimensions
  97. family: "autonomy", // the family of the chart
  98. context: "fronius.autonomy", // the context of the chart
  99. type: netdata.chartTypes.area, // the type of the chart
  100. priority: fronius.base_priority + 3, // the priority relative to others in the same family
  101. update_every: service.update_every, // the expected update frequency of the chart
  102. dimensions: dim
  103. };
  104. chart = service.chart(id, chart);
  105. fronius.charts[id] = chart;
  106. return chart;
  107. },
  108. // Gets the site energy chart for today. Will be created if not existing.
  109. getSiteEnergyTodayChart: function (service, suffix) {
  110. var chartId = this.getChartId(service, suffix);
  111. var chart = fronius.charts[chartId];
  112. if (fronius.isDefined(chart)) return chart;
  113. var dim = {};
  114. dim[fronius.energyTodayId] = this.createBasicDimension(fronius.energyTodayId, "today", 1000);
  115. chart = {
  116. id: chartId, // the unique id of the chart
  117. name: "", // the unique name of the chart
  118. title: service.name + " Energy production for today",// the title of the chart
  119. units: "kWh", // the units of the chart dimensions
  120. family: "energy", // the family of the chart
  121. context: "fronius.energy.today", // the context of the chart
  122. type: netdata.chartTypes.area, // the type of the chart
  123. priority: fronius.base_priority + 4, // the priority relative to others in the same family
  124. update_every: service.update_every, // the expected update frequency of the chart
  125. dimensions: dim
  126. };
  127. chart = service.chart(chartId, chart);
  128. fronius.charts[chartId] = chart;
  129. return chart;
  130. },
  131. // Gets the site energy chart for today. Will be created if not existing.
  132. getSiteEnergyYearChart: function (service, suffix) {
  133. var chartId = this.getChartId(service, suffix);
  134. var chart = fronius.charts[chartId];
  135. if (fronius.isDefined(chart)) return chart;
  136. var dim = {};
  137. dim[fronius.energyYearId] = this.createBasicDimension(fronius.energyYearId, "year", 1000);
  138. chart = {
  139. id: chartId, // the unique id of the chart
  140. name: "", // the unique name of the chart
  141. title: service.name + " Energy production for this year",// the title of the chart
  142. units: "kWh", // the units of the chart dimensions
  143. family: "energy", // the family of the chart
  144. context: "fronius.energy.year", // the context of the chart
  145. type: netdata.chartTypes.area, // the type of the chart
  146. priority: fronius.base_priority + 5, // the priority relative to others in the same family
  147. update_every: service.update_every, // the expected update frequency of the chart
  148. dimensions: dim
  149. };
  150. chart = service.chart(chartId, chart);
  151. fronius.charts[chartId] = chart;
  152. return chart;
  153. },
  154. // Gets the inverter power chart. Will be created if not existing.
  155. // Needs the array of inverters in order to create a chart with all inverters as dimensions
  156. getInverterPowerChart: function (service, suffix, inverters) {
  157. var chartId = this.getChartId(service, suffix);
  158. var chart = fronius.charts[chartId];
  159. if (fronius.isDefined(chart)) return chart;
  160. var dim = {};
  161. for (var key in inverters) {
  162. if (inverters.hasOwnProperty(key)) {
  163. var name = key;
  164. if (!isNaN(key)) name = "inverter_" + key;
  165. dim[key] = this.createBasicDimension("inverter_" + key, name, 1);
  166. }
  167. }
  168. chart = {
  169. id: chartId, // the unique id of the chart
  170. name: "", // the unique name of the chart
  171. title: service.name + " Current Inverter Output",// the title of the chart
  172. units: "W", // the units of the chart dimensions
  173. family: "inverters", // the family of the chart
  174. context: "fronius.inverter.output", // the context of the chart
  175. type: netdata.chartTypes.stacked, // the type of the chart
  176. priority: fronius.base_priority + 6, // the priority relative to others in the same family
  177. update_every: service.update_every, // the expected update frequency of the chart
  178. dimensions: dim
  179. };
  180. chart = service.chart(chartId, chart);
  181. fronius.charts[chartId] = chart;
  182. return chart;
  183. },
  184. processResponse: function (service, content) {
  185. var json = fronius.convertToJson(content);
  186. if (json === null) return;
  187. // add the service
  188. service.commit();
  189. var chartDefinitions = fronius.parseCharts(service, json);
  190. var chartCount = chartDefinitions.length;
  191. while (chartCount--) {
  192. var chartObj = chartDefinitions[chartCount];
  193. service.begin(chartObj.chart);
  194. var dimCount = chartObj.dimensions.length;
  195. while (dimCount--) {
  196. var dim = chartObj.dimensions[dimCount];
  197. service.set(dim.name, dim.value);
  198. }
  199. service.end();
  200. }
  201. },
  202. parseCharts: function (service, json) {
  203. var site = json.Body.Data.Site;
  204. return [
  205. this.parsePowerChart(service, site),
  206. this.parseConsumptionChart(service, site),
  207. this.parseAutonomyChart(service, site),
  208. this.parseEnergyTodayChart(service, site),
  209. this.parseEnergyYearChart(service, site),
  210. this.parseInverterChart(service, json.Body.Data.Inverters)
  211. ];
  212. },
  213. parsePowerChart: function (service, site) {
  214. return this.getChart(this.getSitePowerChart(service, "power"),
  215. [
  216. this.getDimension(this.powerGridId, Math.round(site.P_Grid)),
  217. this.getDimension(this.powerPvId, Math.round(Math.max(site.P_PV, 0))),
  218. this.getDimension(this.powerAccuId, Math.round(site.P_Akku))
  219. ]
  220. );
  221. },
  222. parseConsumptionChart: function (service, site) {
  223. return this.getChart(this.getSiteConsumptionChart(service, "consumption"),
  224. [this.getDimension(this.consumptionLoadId, Math.round(Math.abs(site.P_Load)))]
  225. );
  226. },
  227. parseAutonomyChart: function (service, site) {
  228. var selfConsumption = site.rel_SelfConsumption;
  229. var solarConsumption = 0;
  230. var load = Math.abs(site.P_Load);
  231. var power = Math.max(site.P_PV, 0);
  232. if (power <= 0) solarConsumption = 0;
  233. else if (load >= power) solarConsumption = 100;
  234. else solarConsumption = 100 / power * load;
  235. return this.getChart(this.getSiteAutonomyChart(service, "autonomy"),
  236. [
  237. this.getDimension(this.autonomyId, Math.round(site.rel_Autonomy)),
  238. this.getDimension(this.consumptionSelfId, Math.round(selfConsumption === null ? 100 : selfConsumption)),
  239. this.getDimension(this.solarConsumptionId, Math.round(solarConsumption))
  240. ]
  241. );
  242. },
  243. parseEnergyTodayChart: function (service, site) {
  244. return this.getChart(this.getSiteEnergyTodayChart(service, "energy.today"),
  245. [this.getDimension(this.energyTodayId, Math.round(Math.max(site.E_Day, 0)))]
  246. );
  247. },
  248. parseEnergyYearChart: function (service, site) {
  249. return this.getChart(this.getSiteEnergyYearChart(service, "energy.year"),
  250. [this.getDimension(this.energyYearId, Math.round(Math.max(site.E_Year, 0)))]
  251. );
  252. },
  253. parseInverterChart: function (service, inverters) {
  254. var dimensions = [];
  255. for (var key in inverters) {
  256. if (inverters.hasOwnProperty(key)) {
  257. dimensions.push(this.getDimension(key, Math.round(inverters[key].P)));
  258. }
  259. }
  260. return this.getChart(this.getInverterPowerChart(service, "inverters.output", inverters), dimensions);
  261. },
  262. getDimension: function (name, value) {
  263. return {
  264. name: name,
  265. value: value
  266. };
  267. },
  268. getChart: function (chart, dimensions) {
  269. return {
  270. chart: chart,
  271. dimensions: dimensions
  272. };
  273. },
  274. getChartId: function (service, suffix) {
  275. return "fronius_" + service.name + "." + suffix;
  276. },
  277. convertToJson: function (httpBody) {
  278. if (httpBody === null) return null;
  279. var json = httpBody;
  280. // can't parse if it's already a json object,
  281. // the check enables easier testing if the httpBody is already valid JSON.
  282. if (typeof httpBody !== "object") {
  283. try {
  284. json = JSON.parse(httpBody);
  285. } catch (error) {
  286. netdata.error("fronius: Got a response, but it is not valid JSON. Ignoring. Error: " + error.message);
  287. return null;
  288. }
  289. }
  290. return this.isResponseValid(json) ? json : null;
  291. },
  292. // some basic validation
  293. isResponseValid: function (json) {
  294. if (this.isUndefined(json.Body)) return false;
  295. if (this.isUndefined(json.Body.Data)) return false;
  296. if (this.isUndefined(json.Body.Data.Site)) return false;
  297. return this.isDefined(json.Body.Data.Inverters);
  298. },
  299. // module.serviceExecute()
  300. // this function is called only from this module
  301. // its purpose is to prepare the request and call
  302. // netdata.serviceExecute()
  303. serviceExecute: function (name, uri, update_every) {
  304. netdata.debug(this.name + ": " + name + ": url: " + uri + ", update_every: " + update_every);
  305. var service = netdata.service({
  306. name: name,
  307. request: netdata.requestFromURL("http://" + uri),
  308. update_every: update_every,
  309. module: this
  310. });
  311. service.execute(this.processResponse);
  312. },
  313. configure: function (config) {
  314. if (fronius.isUndefined(config.servers)) return 0;
  315. var added = 0;
  316. var len = config.servers.length;
  317. while (len--) {
  318. var server = config.servers[len];
  319. if (fronius.isUndefined(server.update_every)) server.update_every = this.update_every;
  320. if (fronius.areUndefined([server.name, server.hostname, server.api_path])) continue;
  321. var url = server.hostname + server.api_path;
  322. this.serviceExecute(server.name, url, server.update_every);
  323. added++;
  324. }
  325. return added;
  326. },
  327. // module.update()
  328. // this is called repeatedly to collect data, by calling
  329. // netdata.serviceExecute()
  330. update: function (service, callback) {
  331. service.execute(function (serv, data) {
  332. service.module.processResponse(serv, data);
  333. callback();
  334. });
  335. },
  336. isUndefined: function (value) {
  337. return typeof value === "undefined";
  338. },
  339. areUndefined: function (valueArray) {
  340. var i = 0;
  341. for (i; i < valueArray.length; i++) {
  342. if (this.isUndefined(valueArray[i])) return true;
  343. }
  344. return false;
  345. },
  346. isDefined: function (value) {
  347. return typeof value !== "undefined";
  348. }
  349. };
  350. module.exports = fronius;