Payload Parse HEX

Hello,

I am new to studying TAGOIO and would like some support in setting up the Parse Payload for the following variables:

Payload:

HEX

7b2245223a2d302e322c2253223a2d302e322c2256223a302e3030302c2254223a302e3030302c224d223a3139353234372c2223223a36387d

ASCII

{“E”:-0.2,“S”:-0.2,“V”:0.000,“T”:0.000,“M”:195247,“#”:68}

Variables: E, S, V, T, M and #;

Could you help me with this reasoning? Do you have any example of how I can do it?

Thanks.

Hello Anderson Portela,

Below is an example of a Payload Parser along with an explanation:

Steps to Decode the Payload:

  1. Convert the Hexadecimal String to a Buffer: This will allow us to decode the string into its original format.
  2. Parse the Buffer as a JSON Object: The buffer should be converted to a string and then parsed as JSON.
  3. Extract the Relevant Data: Extract the variables from the JSON object and format them into the TagoIO data object format.

TagoIO Data Object:

We will map each key in the JSON object to a TagoIO data object with appropriate variable names and values.

Payload sent to device:

[{"variable": "payload","value": "7b2245223a2d302e322c2253223a2d302e322c2256223a302e3030302c2254223a302e3030302c224d223a3139353234372c2223223a36387d"}]

Decoder Implementation:

Here is the JavaScript code to decode the payload and format it into the TagoIO data object format

/*** Parses the payload and returns an array of data objects.** @param {string} hexPayload - The payload in hex to be parsed.* @returns {Array} An array of data objects extracted from the payload.*/function parsePayload(hexPayload) {const buffer = Buffer.from(hexPayload, "hex");const jsonString = buffer.toString("utf8");const jsonObject = JSON.parse(jsonString);const time = new Date().toISOString();const group = `${new Date().getTime()}-${Math.random().toString(36).substring(2, 5)}`;const data = [{ variable: "E", value: jsonObject.E, group, time },{ variable: "S", value: jsonObject.S, group, time },{ variable: "V", value: jsonObject.V, group, time },{ variable: "T", value: jsonObject.T, group, time },{ variable: "M", value: jsonObject.M, group, time },{ variable: "hash", value: jsonObject["#"], group, time }];return data;}// Handle Received Dataconst hexPayload = payload.find((x) => x.variable === "payload").value;if (hexPayload) {try {const parsedData = parsePayload(hexPayload);payload = parsedData;} catch (error) {// Print the error to the Live Inspector.// console.error(error);// Return the variable parse_error for debugging.payload = [{ variable: "parse_error", value: error.message }];}}

Explanation:

  1. Buffer Conversion: The payload value is converted from a hexadecimal string to a buffer.
  2. JSON Parsing: The buffer is converted to a UTF-8 string and then parsed as a JSON object.
  3. Data Extraction: Each key-value pair in the JSON object is mapped to a TagoIO data object with a variable name, value, group, and time.
  4. Payload Handling: The parsed data is assigned back to the payload variable.