flotron-decoder
v5.0.0
Published
This function, decode, serves as a payload decoder for logger data, specifically for a combination of battery, analog, and digital data types. It extracts and transforms the payload data based on the logger type and specified scaling parameters.
Downloads
33
Readme
Payload Decoder Documentation
This function, decode, serves as a payload decoder for logger data, specifically for a combination of battery, analog, and digital data types. It extracts and transforms the payload data based on the logger type and specified scaling parameters.
Function Signature
function decode(loggerType, payload, analogMin, analogMax, batteryMin = 0, batteryMax = 3600)
Parameters
loggerType
(string): A string indicating the type of logger data, specifying the presence of battery ("B"), analog ("A"), and digital ("D") data types.payload
(string): The raw payload data to be decoded.analogMin
(number): The minimum value for analog data scaling.analogMax
(number): The maximum value for analog data scaling.batteryMin
(number, optional): The minimum value for battery data scaling. Defaults to 0.batteryMax
(number, optional): The maximum value for battery data scaling. Defaults to 3600.
Return Value
An object containing three arrays:
Battery
: Array containing decoded and scaled battery data.Analog
: Array containing decoded and scaled analog data.Digital
: Array containing decoded and averaged digital data.
Implementation
Initialization
- Variables are declared to track the counts and arrays for battery, analog, and digital data.
- Trackers are initialized to manage the iteration through the payload.
Data Extraction and Simplification
- The function iterates through the payload based on the logger type and extracts data into respective arrays.
- Each array is then simplified by calculating the average of subarrays.
Scaling
Battery Data
- The battery data is scaled from the raw values to a specified range using a linear scaling formula.
- The scaled battery data is constrained within the specified range.
Analog Data
- Analog data is scaled similarly to the battery data, using a linear scaling formula.
- The scaled analog data is constrained within the specified range.
Digital Data
Digital data is averaged within each subarray.
Example Usage
const loggerType = "BAAADD";
const payload = "0A0F00B01400C82100D0ABCDEF";
const analogMin = 0;
const analogMax = 100;
const batteryMin = 0;
const batteryMax = 3600;
const decodedData = decode(loggerType, payload, analogMin, analogMax, batteryMin, batteryMax);
console.log(decodedData);
This function is designed to handle decoding payloads from a logger device and can be customized by adjusting scaling parameters according to the specific characteristics of the data.
Attribution
Created by Hanno Vorster, 2024