morzzz007 / node-tradfri

Node API to control IKEA Tradfri Lights
MIT License
49 stars 17 forks source link

command response parsing error #12

Closed Juravenator closed 6 years ago

Juravenator commented 6 years ago

On my machine, when executing any command, I get the following error:

Invalid response: SyntaxError: Unexpected token u in JSON at position 0

The soap-client used from upstream seems to be working, for example, executing tradfri.getDevices in the background runs /usr/bin/coap-client -m get -u "xxx" -k "yyy" "coaps://some-ip:5684/15001" and returns

stdOut: '[65536,65537,...]\n'

The line producing the error is in CoapClient.makeRequest(): resolve(JSON.parse(stdOut.split('\n')[3]));
When I change this to

let split = stdOut.split('\n');
resolve(JSON.parse(split[3] || split[0]));

Everything works again.

My main question is where this magic number 3 comes from. Is this a misconfigured libcoap on my end? Or is the 3 variable across environments (and would warrant a PR)?

My system:

morzzz007 commented 6 years ago

Hi, sorry for the late response! I assume libcoap clients can differ (I only tested it on osx and raspibian), and trying to parse the first element of the array on failure won't make things any worse. I just merged an another PR, so i'll include this change in the new release. Thanks!