Comment on page
Accessing Measurements in Decoders
The measurements object, which holds the measurement data of all database fields, contains an additional level in the dictionary for API and MQTT Decoder, namely that of the device ID. The dictionary looks like this here.
measurements["26ab17ac-5ce4-41a1-b873-b2dff76a8f38"].TEMPERATURE.value
However, if you do not have the Datacake UUID available within these decoders, then you can use the following helper function in the decoder to convert a user serial to the respective Datacake UUID of your device in order to be able to identify the device in the measurements object.
var data = JSON.parse(payload.body)
var serial = data.serial // as an example
// Now request the Datacake UUID for given user-serial
var datacakeUUID = deviceSerialToId[serial]
var temperatureInDB = measurements[datacakeUUID]["TEMPERATURE"].value;
function Decoder(payload) {
var data = JSON.parse(payload.body);
var serial = data.serial
// lets assume you have a database field called MESSAGE_COUNT
// and you want to increment this +1 everytime this decoder runs
// first we need to load previous state (actually current state before new data will be ingested)
// of the database field into the decoder
var message_count = measurements[deviceSerialToId[serial]]["MESSAGE_COUNT"].value;
/*
Info
Datacake API decoder do have a helper function to get the ID from custom
serial numbers provided by the user.
-> deviceSerialToId["ABC123123"]
*/
// Now increment
message_count += 1
// Return back to Datacake
return [
{
field: "MESSAGE_COUNT",
value: message_count,
device: serial
}
]
}
Last modified 6mo ago