From a1eb3bdda4d1e15e808673ac8208b225ab7349de Mon Sep 17 00:00:00 2001 From: marcus Date: Mon, 22 Feb 2016 21:51:04 +0100 Subject: [PATCH] Improved errir for handling for parsed expression values, issue #2 Corrected device-config-schema: "unit", "label", and "discrete" are now optional as described in README, issue #2 Removed "type" property from example in README, issue #2 --- README.md | 2 -- device-config-schema.coffee | 6 ++++ filter.coffee | 72 ++++++++++++++++++++++--------------- 3 files changed, 50 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 08b83a0..239ed62 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,6 @@ Note, however, the resulting value must be a number to be processed by the filte "label": "Temperature", "expression": "$unipi-2.temperature", "acronym": "T", - "type": "number", "unit": "°C" } } @@ -82,7 +81,6 @@ Note, however, the resulting value must be a number to be processed by the filte "name": "Temperature", "expression": "$unipi-2.temperature", "acronym": "T", - "type": "number", "unit": "°C" } } diff --git a/device-config-schema.coffee b/device-config-schema.coffee index 65daccc..f0ceee6 100644 --- a/device-config-schema.coffee +++ b/device-config-schema.coffee @@ -25,14 +25,17 @@ module.exports = { unit: description: "The unit of the variable. Only works if type is a number." type: "string" + required: false label: description: "A custom label to use in the frontend." type: "string" + required: false discrete: description: " Should be set to true if the value does not change continuously over time. " type: "boolean" + required: false acronym: description: "Acronym to show as value label in the frontend" type: "string" @@ -63,14 +66,17 @@ module.exports = { unit: description: "The unit of the variable. Only works if type is a number." type: "string" + required: false label: description: "A custom label to use in the frontend." type: "string" + required: false discrete: description: " Should be set to true if the value does not change continuously over time. " type: "boolean" + required: false acronym: description: "Acronym to show as value label in the frontend" type: "string" diff --git a/filter.coffee b/filter.coffee index 4a9e9a9..7bf1108 100644 --- a/filter.coffee +++ b/filter.coffee @@ -33,7 +33,7 @@ module.exports = (env) -> @sum = 0.0 @mean = 0.0 - @varManager = plugin.framework.variableManager #so you get the variableManager + @varManager = plugin.framework.variableManager @_exprChangeListeners = [] name = @output.name @@ -74,18 +74,15 @@ module.exports = (env) -> else assert false ).then((val) => - if val? - val = Number(val) - @filterValues.push val - @sum = @sum + val - if @filterValues.length > @size - @sum = @sum - @filterValues.shift() - @mean = @sum / @filterValues.length - - env.logger.debug @mean, @filterValues - @_setAttribute name, @mean - else - env.logger.error "Error on device #{@config.id}: Input value is null or undefined" + val = @_getNumber(val) + @filterValues.push val + @sum = @sum + val + if @filterValues.length > @size + @sum = @sum - @filterValues.shift() + @mean = @sum / @filterValues.length + + env.logger.debug @mean, @filterValues + @_setAttribute name, @mean return @attributeValue ).catch((error) => @@ -94,6 +91,17 @@ module.exports = (env) -> ) evaluate() + _getNumber: (value) -> + if value? + numValue = Number value + unless isNaN numValue + return numValue + else + errorMessage = "Input value is not a number: #{value}" + else + errorMessage = "Input value is null or undefined" + throw new Error errorMessage + _setAttribute: (attributeName, value) -> @attributeValue = value @emit attributeName, value @@ -150,24 +158,21 @@ module.exports = (env) -> else assert false ).then((val) => - if val? - val = Number(val) - @filterValues.push val - if @filterValues.length > @size - @filterValues.shift() + val = @_getNumber(val) + @filterValues.push val + if @filterValues.length > @size + @filterValues.shift() - processedValues = _.clone(@filterValues) - if processedValues.length > 2 - processedValues.sort() - processedValues.shift() - processedValues.pop() + processedValues = _.clone(@filterValues) + if processedValues.length > 2 + processedValues.sort() + processedValues.shift() + processedValues.pop() - @mean = processedValues.reduce(((a, b) => return a + b), 0) / processedValues.length + @mean = processedValues.reduce(((a, b) => return a + b), 0) / processedValues.length - env.logger.debug @mean, @filterValues, processedValues - @_setAttribute name, @mean - else - env.logger.error "Error on device #{@config.id}: Input value is null or undefined" + env.logger.debug @mean, @filterValues, processedValues + @_setAttribute name, @mean return @attributeValue ).catch((error) => @@ -176,6 +181,17 @@ module.exports = (env) -> ) evaluate() + _getNumber: (value) -> + if value? + numValue = Number value + unless isNaN numValue + return numValue + else + errorMessage = "Input value is not a number: #{value}" + else + errorMessage = "Input value is null or undefined" + throw new Error errorMessage + _setAttribute: (attributeName, value) -> @attributeValue = value @emit attributeName, value