tiefpunkt / lorawan-gateway-traffic-analysis

A Node-Red flow to analyze the LoRaWAN packets your gateway(s) receive.
6 stars 0 forks source link

Syntax Error Message on node Remove Binary Prefix and parse DevAddrfunction #1

Open drucknase opened 4 years ago

drucknase commented 4 years ago

I get the following error message on node

27.3.2020, 12:46:58node: Remove Binary Prefix and parse DevAddrfunction : (error) "SyntaxError: Unexpected end of JSON input"

tiefpunkt commented 4 years ago

I've had that issue as well before. Are you on the latest version that's using Buffers from the UDP in?

drucknase commented 4 years ago

yes, I'm using 'buffer' in the UDP in node and I did a git fetch on friday so I should be on the newest version

drucknase commented 4 years ago

I use the multi protocol packet forwarder (mp_pkt_fwd). May the behaviour of the flow depend on the used packet forwarder?

drucknase commented 4 years ago

I believe the problem are UDP packets send by the GW without any json content. This happens in my configuration about every 5sec. This packest just include

Bytes Function
0 protocol version = 2
1-2 random token
3 PUSH_DATA identifier 0x00
4-11 Gateway unique identifier (MAC address)

A JSONparse of such a message will lead to the error message. So I changed the JS code of the node to the following content and than the error message will disapear: `// https://github.com/Lora-net/packet_forwarder/blob/master/PROTOCOL.TXT

function reverse (src) { var buffer = Buffer.allocUnsafe(src.length) for (var i = 0, j = src.length - 1; i <= j; ++i, --j) { buffer[i] = src[j] buffer[j] = src[i] }

return buffer }

inputBuf = msg.payload; //fullMessage = inputBuf.toString("ascii",12); fullMessage = inputBuf.toString("utf-8",12); if (fullMessage) { message = JSON.parse(fullMessage);

//head_raw=msg.payload.substring(0,12);
//head = Buffer.from(head_raw,'binary').toString("hex");

gwmac_raw=msg.payload.slice(4,12);
gwmac = gwmac_raw.toString("hex");

if (message.rxpk && message.rxpk.length > 0 && message.rxpk[0].data) {
    var payloadBase64 = message.rxpk[0].data;
    var bufferDevice = Buffer.from(payloadBase64, 'base64').slice(1,5);
    bufferDevice = reverse(bufferDevice);
    message.rxpk[0].DevAddr = bufferDevice.toString('hex').toUpperCase();

    var frameCounter = Buffer.from(payloadBase64, 'base64').slice(6,8);
    frameCounter = reverse(frameCounter);
    message.rxpk[0].FrameCounter = parseInt(frameCounter.toString('hex'), 16);
}

msg.payload = message;
msg.payload.gateway = "eui-" + gwmac;
//msg.payload.head = head;
return msg;

}`

So you have to test for empty string before parsing. Because I'm no JS expert, maybe there are better solutions. Up to now it works for me. Thanks.