Last active
August 29, 2015 14:22
-
-
Save calderonroberto/2fc3f3eb5c30f3d1ac9c to your computer and use it in GitHub Desktop.
A function node for Node-RED that assembles data from WoTKit to create an HTTP request.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Return an error if data is empty. | |
if (msg.payload.length === 0){ | |
msg.payload = {"error":"no data found"}; | |
return msg; | |
} | |
// Get the values as an array | |
var values = [] | |
var sensors = [] | |
msg.payload.map(function(element){ | |
values.push(element.value) | |
if (sensors.indexOf(element.sensor_name) == -1 && element.sensor_name !== null) | |
sensors.push(element.sensor_name) | |
}); | |
// Compute the average | |
var average = values.reduce(function(previous, current, index, array) { | |
return previous + current; | |
}) / values.length; | |
// Assemble response and return | |
msg.payload = {'average_value': average, 'sensors_parsed':sensors, 'data_points' : values.length } | |
return msg; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment