Last active
April 4, 2021 17:31
-
-
Save LosantGists/ec1468d824122e91a4979a4f797bbcb5 to your computer and use it in GitHub Desktop.
Node-RED function node to combine SensorTag messages into single payload.
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
// This function node combines all of the async values | |
// reported by the sensor tag node into a single | |
// payload. The flow will only continue when all values | |
// have been received. | |
var payload = context.get('payload') || { data: {} }; | |
// Number of total attributes to report. | |
// When we collect all attributes from the | |
// sensor tag, the flow will continue. | |
// If you change what is read in the sensor tag config, | |
// this number will also have to change. | |
var NUM_ATTRIBUTES = 13; | |
switch(msg.topic) { | |
case 'sensorTag/temperature': | |
payload.data.temperatureObject = msg.payload.object; | |
payload.data.temperatureAmbient = msg.payload.ambient; | |
break; | |
case 'sensorTag/humidity': | |
payload.data.humidity = msg.payload.humidity; | |
break; | |
case 'sensorTag/pressure': | |
payload.data.pressure = msg.payload.pressure; | |
break; | |
case 'sensorTag/accelerometer': | |
payload.data.accelerometerX = msg.payload.x; | |
payload.data.accelerometerY = msg.payload.y; | |
payload.data.accelerometerZ = msg.payload.z; | |
break; | |
case 'sensorTag/gyroscope': | |
payload.data.gyroscopeX = msg.payload.x; | |
payload.data.gyroscopeY = msg.payload.y; | |
payload.data.gyroscopeZ = msg.payload.z; | |
break; | |
case 'sensorTag/magnetometer': | |
payload.data.magnetometerX = msg.payload.x; | |
payload.data.magnetometerY = msg.payload.y; | |
payload.data.magnetometerZ = msg.payload.z; | |
break; | |
} | |
context.set('payload', payload); | |
// We've got all values. Return the new payload and | |
// clear the payload stored in context. | |
if(Object.keys(payload.data).length === NUM_ATTRIBUTES) { | |
context.set('payload', null); | |
msg.payload = payload; | |
return msg; | |
} | |
return null; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment