summaryrefslogtreecommitdiffstats
path: root/node.d
diff options
context:
space:
mode:
authorBrainDoctor <github.account@chrigel.net>2017-07-18 16:16:11 +0200
committerBrainDoctor <github.account@chrigel.net>2017-07-18 21:14:52 +0200
commit1b54db67ba71dd2c65dd31d49cb8798ac4cc4d89 (patch)
treec6febfe64b0b26aec440a160fe2712666cd979d2 /node.d
parent64f6191801edbdd2cf3d15178a8fdef879cd574c (diff)
more unit tests for fronius plugin
Diffstat (limited to 'node.d')
-rw-r--r--node.d/fronius.node.js29
1 files changed, 22 insertions, 7 deletions
diff --git a/node.d/fronius.node.js b/node.d/fronius.node.js
index f771f6c3d0..54bae761da 100644
--- a/node.d/fronius.node.js
+++ b/node.d/fronius.node.js
@@ -205,9 +205,8 @@ var fronius = {
},
processResponse: function (service, content) {
- if (content === null) return;
- var json = JSON.parse(content);
- if (!fronius.isResponseValid(json)) return;
+ var json = fronius.parseResponse(content);
+ if (json === null) return;
// add the service
service.commit();
@@ -255,12 +254,19 @@ var fronius = {
}
},
+ parseResponse: function (httpBody) {
+ if (httpBody === null) return null;
+ var json = httpBody;
+ if (typeof httpBody !== "object") json = JSON.parse(httpBody);
+ return this.isResponseValid(json) ? json : null;
+ },
+
// some basic validation
isResponseValid: function (json) {
- if (fronius.isUndefined(json.Body)) return false;
- if (fronius.isUndefined(json.Body.Data)) return false;
- if (fronius.isUndefined(json.Body.Data.Site)) return false;
- return fronius.isDefined(json.Body.Data.Inverters);
+ if (this.isUndefined(json.Body)) return false;
+ if (this.isUndefined(json.Body.Data)) return false;
+ if (this.isUndefined(json.Body.Data.Site)) return false;
+ return this.isDefined(json.Body.Data.Inverters);
},
// module.serviceExecute()
@@ -287,6 +293,7 @@ var fronius = {
while (len--) {
var server = config.servers[len];
if (fronius.isUndefined(server.update_every)) server.update_every = this.update_every;
+ if (fronius.areUndefined([server.name, server.hostname, server.api_path])) continue;
var url = server.hostname + server.api_path;
this.serviceExecute(server.name, url, server.update_every);
@@ -309,6 +316,14 @@ var fronius = {
return typeof value === 'undefined';
},
+ areUndefined: function (valueArray) {
+ var i = 0;
+ for (i; i < valueArray.length; i++) {
+ if (this.isUndefined(valueArray[i])) return true;
+ }
+ return false;
+ },
+
isDefined: function (value) {
return typeof value !== 'undefined';
}