// Extract the required data from the payload
const data = msg.payload.data.map(item => {
const temperature = Number(hexTo8p8(item.adv_data.substr(32, 4)));
const humidity = Number(hexTo8p8(item.adv_data.substr(36, 4)));
return {
adv_data: item.adv_data,
rssi: item.rssi,
mac: item.mac.toUpperCase(),
timestamp: item.timestamp,
};
});
var structure = [
{ name: “AdvFlags”, offset: 0, length: 3 },
{ name: “ID”, offset: 4, length: 3 },
{ name: “AdvType”, offset: 8, length: 1 },
{ name: “UUID”, offset: 9, length: 2 },
{ name: “FrameType”, offset: 11, length: 1 },
{ name: “VersionTag”, offset: 12, length: 1 },
{ name: “SensorMask”, offset: 13, length: 1 },
{ name: “Voltage”, offset: 14, length: 2 },
{ name: “Temperature”, offset: 16, length: 2 },
{ name: “Humidity”, offset: 18, length: 2 },
{ name: “AccX”, offset: 20, length: 2 },
{ name: “AccY”, offset: 22, length: 2 },
{ name: “AccZ”, offset: 24, length: 2 },
{ name: “CutoffFlag”, offset: 26, length: 1 },
{ name: “PIRFlag”, offset: 27, length: 1 }
];
// Extract values based on the provided structure
// Initialize an array to store data points for bulk insert
var influxBulkInsert = ;
// Format the data for InfluxDB
const influxData = data.map(item => {
var sensorMask = parseInt(item.adv_data.substr(13 * 2, 2), 16);
var dataPoint = {
measurement: 'mem',
fields: {
rssi: item.rssi,
device_mac: item.mac.toUpperCase(),
sensor_mask: parseInt(item.adv_data.substr(13 * 2, 2), 16),
Voltage: parseInt(item.adv_data.substr(14 * 2, 4), 16),
raw: item.adv_data
},
tags: {
mac: item.mac.toUpperCase(),
device_mac: item.mac.toUpperCase(),
sensor_mask: parseInt(item.adv_data.substr(13 * 2, 2), 16),
},
timestamp: item.timestamp
};
// Check if CutoffBit is true (Bit 4)
if (sensorMask & 0x10) {
dataPoint.fields.cutoff = parseInt(item.adv_data.substr(13 * 2, 2), 16);
}
// Check each bit in the sensor mask
if (sensorMask & 0x01) {
// Bit 0: Voltage
dataPoint.fields.voltage = parseInt(item.adv_data.substr(14 * 2, 4), 16);
}
if (sensorMask & 0x02) {
// Bit 1: Temperature
dataPoint.fields.temperature = Number(hexTo8p8(item.adv_data.substr(32, 4)));
}
if (sensorMask & 0x04) {
// Bit 2: Humidity
dataPoint.fields.humidity = Number(hexTo8p8(item.adv_data.substr(36, 4)));
}
if (sensorMask & 0x08) {
// Bit 3: Accelerometer
dataPoint.fields.AccX = parseInt(item.adv_data.substr(20 * 2, 4), 16);
dataPoint.fields.AccY = parseInt(item.adv_data.substr(22 * 2, 4), 16);
dataPoint.fields.AccZ = parseInt(item.adv_data.substr(24 * 2, 4), 16);
}
if (sensorMask & 0x10) {
// Bit 4: Cutoff
dataPoint.fields.cutoff = parseInt(item.adv_data.substr(18 * 2, 2), 16);
}
if (sensorMask & 0x20) {
// Bit 5: PIR Indication
// Include PIR indication field if the bit is set
dataPoint.fields.PIRIndication = parseInt(item.adv_data.substr(14 * 2, 2), 16);
}
influxBulkInsert.push(dataPoint);
});
// Set the formatted data as the new payload
msg.payload = influxBulkInsert;
return msg;
function hexTo8p8(/** @type {string} */ value) {
let intValue = parseInt(value, 16);
let signed = (intValue & 0x8000) > 0 ? -1 / 10 : 1;
return signed * intValue / Math.pow(2, 8);
}
// Convert Temperature and Humidity from hex to 8.8 fixed-point format
[“Temperature”, “Humidity”].forEach(function (field_name) {
extractedValues[field_name] = parseInt(extractedValues[field_name], 16) / 256.0;
});
// Extract and interpret the Sensor Mask bits
var sensor_mask = parseInt(extractedValues.SensorMask, 16);
extractedValues.VoltageBit = !!(sensor_mask & 0x01);
extractedValues.TempBit = !!(sensor_mask & 0x02);
extractedValues.HumidityBit = !!(sensor_mask & 0x04);
extractedValues.AccBit = !!(sensor_mask & 0x08);
extractedValues.CutoffBit = !!(sensor_mask & 0x10);
extractedValues.PIRIndicationBit = !!(sensor_mask & 0x20);
function hexTo8p8(/** @type {string} */ value) {
let intValue = parseInt(value, 16);
let signed = (intValue & 0x8000) > 0 ? -1 / 10 : 1;
return signed * intValue / Math.pow(2, 8);
}
Need some support to convert this function for Telefraf for converitng raw adv data