Yes, I know this is not unusual (and maybe that is why I am having a hard time finding a solution) but I am 100% sure this is what I need to accomplish.
Currently I'm translating the buffer to base64, but it's not correct for the person I'm working with.
He absolutely wants the payload to be in hexadecimal and also in JSON (and buffers are not recognized in json).
So about the solution, I think I'll try to do loop which append each term of the buffer to a string. I don't know if it's will work, let's see !
Here is msg.payload: [10,0,255]
the equivalent buffer in hex is: 0xa 0x0 0xff
What I'm trying to do is to have a JSON like this:
{
"deviceid": "ABC0000BCA",
"value": "0A 00 FF", // A concatened string of the hex buffer
"devicetype": "HomeDetection"
}
So to achieve this, I used a function to transform [0xa 0x0 0xff] in a string like this: 0A 00 FF:
// Creation of a function to get the Hex Value from a message
function* hexFormatValues(buffer) {
for (let x of buffer) {
const hex = x.toString(16)
yield hex.padStart(2, '0')
}
}
// Variable declaration for my loop
var string = ""
const buf = Buffer.from(msg.payload)
// Concatenation of the buffer in a string
for (let hex of hexFormatValues(buf)) {
string = string + hex + " " // You can remove the " " if you don't want spaces between the bytes
}
msg.payload = string;
return msg;
And in the JSON template I put the following:
{
"deviceid": "ABC0000BCA",
"value": {{payload}}, // A concatened string of the hex buffer
"devicetype": "HomeDetection"
}
I'm not sure this will ever be of use to anyone but I am sharing the solution just in case.