I need some help composing a new Base64 string from a msg.payload. I am using a function node to do this. I am using the Buffer. function. I have managed to construct all needed parts of the new msg.payload, however I can not properly concatenate them into a correct singe array Base64 string.
This is my flow:
[
{
"id": "6b6e467a198f9bae",
"type": "debug",
"z": "ba4a4f26f3906e7b",
"name": "Format User Message",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 780,
"y": 500,
"wires": []
},
{
"id": "876e9abdce1b7887",
"type": "inject",
"z": "ba4a4f26f3906e7b",
"name": "ABCD - ER Message",
"props": [
{
"p": "payload"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "{\"device_ids\":[\"0016c001f0028af9\"],\"created_at\":\"2022-04-17T19:25:58.441417+00:00\",\"text\":\"ABCDE\"}",
"payloadType": "json",
"x": 300,
"y": 500,
"wires": [
[
"8af452e5fc8956f9"
]
]
},
{
"id": "8af452e5fc8956f9",
"type": "function",
"z": "ba4a4f26f3906e7b",
"name": "",
"func": "device_eui = msg.payload.device_ids[0];\n\nport = [28];\nid = 0x90;\nlen = 0;\nmsg_len = 0;\ntext = \"\";\nseq = 1;\nretry = 1;\nnew_msg = \"\";\n\ntext = Buffer.from(msg.payload.text, 'ascii'),\nmsg_len = text.length,\n//hex_string = [0x1C, 0x90, 0x08, 0x05, 0x41, 0x42, 0x43, 0x44, 0x45, 0x01, 0x01];\nlen = (msg_len + 3),\nnew_msg = [port, id, len, msg_len, text, seq, retry],\n//new_msg_concat = Buffer.concat(port,id,len,msg_len,text,seq,retry),\n\nmsg.payload = {\n \"confirmed\": false,\n \"fPort\": 28,\n \"data\": Buffer(new_msg, 'hex').toString('base64'),\n};\n\nmsg.topic = device_eui;\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 500,
"y": 500,
"wires": [
[
"6b6e467a198f9bae"
]
]
}
]
The problem is that the text Buffer is also an array and therefore not properly added as separate bytes. The intended output should be:
[0x1C, 0x90, 0x08, 0x05, 0x41, 0x42, 0x43, 0x44, 0x45, 0x01, 0x01]
Not sure hot to approach this better.