Skip to content

Commit

Permalink
Improved errir for handling for parsed expression values, issue #2
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mwittig committed Feb 22, 2016
1 parent b4f6a30 commit a1eb3bd
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 30 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Expand Down Expand Up @@ -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"
}
}
6 changes: 6 additions & 0 deletions device-config-schema.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
72 changes: 44 additions & 28 deletions filter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) =>
Expand All @@ -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
Expand Down Expand Up @@ -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) =>
Expand All @@ -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
Expand Down

0 comments on commit a1eb3bd

Please sign in to comment.