Compose Base64 string from different input values

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.

A more "low-code" approach would be to use a buffer maker node from node-red-contrib-buffer-parser package. It is designed for this type of thing.

[{"id":"876e9abdce1b7887","type":"inject","z":"7978bfa8bc6b354e","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":604,"y":528,"wires":[["353a3a03fdbbd4d5","4a54ee7398240f67"]]},{"id":"353a3a03fdbbd4d5","type":"buffer-maker","z":"7978bfa8bc6b354e","name":"","specification":"spec","specificationType":"ui","items":[{"name":"port","type":"uint8","length":1,"dataType":"num","data":"28"},{"name":"id","type":"uint8","length":1,"dataType":"num","data":"0x90"},{"name":"msg_len","type":"uint8","length":1,"dataType":"jsonata","data":"3 + $length(payload.text)"},{"name":"len","type":"uint8","length":1,"dataType":"jsonata","data":"$length(payload.text)"},{"name":"text","type":"ascii","length":-1,"dataType":"msg","data":"payload.text"},{"name":"seq","type":"uint8","length":1,"dataType":"num","data":"1"},{"name":"retry","type":"uint8","length":1,"dataType":"num","data":"1"}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"buffer","msgPropertyType":"str","x":826,"y":528,"wires":[["6c0ee1b369ee8d40","79df7d1313e153dd"]]},{"id":"6c0ee1b369ee8d40","type":"debug","z":"7978bfa8bc6b354e","name":"After maker","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"buffer","targetType":"msg","statusVal":"buffer","statusType":"auto","x":842,"y":592,"wires":[]},{"id":"4a54ee7398240f67","type":"debug","z":"7978bfa8bc6b354e","name":"Original","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":576,"y":592,"wires":[]},{"id":"480eb22a913e7a30","type":"debug","z":"7978bfa8bc6b354e","name":"Result","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1186,"y":592,"wires":[]},{"id":"79df7d1313e153dd","type":"change","z":"7978bfa8bc6b354e","name":"make payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"{}","tot":"json"},{"t":"set","p":"payload.data","pt":"msg","to":"buffer","tot":"msg","dc":true},{"t":"set","p":"payload.confirmed","pt":"msg","to":"false","tot":"bool"},{"t":"set","p":"payload.fPort","pt":"msg","to":"buffer[0]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1024,"y":528,"wires":[["d94a0b06aa410fcf"]]},{"id":"d94a0b06aa410fcf","type":"base64","z":"7978bfa8bc6b354e","name":"","action":"","property":"payload.data","x":1188,"y":528,"wires":[["480eb22a913e7a30"]]}]
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.