Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
drucknase opened this issue Mar 27, 2020 · 4 comments

Comments

@drucknase
Copy link

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
Copy link
Owner

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

@drucknase
Copy link
Author

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
Copy link
Author

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

@drucknase
Copy link
Author

drucknase commented Mar 29, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants