rooi / homebridge-lightwaverf

LightwaveRF plugin for HomeBridge
9 stars 7 forks source link

Listener not invoked due to inconsistent parsing of status code #42

Closed jshiell closed 4 years ago

jshiell commented 4 years ago

This is an oddity that popped up when I was trying to get a Raspberry Pi to behave like a LF WiFi hub. It's actually in the node-lightwaverf library, but since you don't have an active issue tracker on there - and this is the actual interface I'm using for the library - I've added it here instead. Hope that's okay.

There's an inconsistency in the parsing code for the responses between when the listener is added, where it's parsed as an int and then converted to a string, and the listener lookup where it's just taken from the message literal.

The result of this is that if a message returns a padding ID in the response then the listener isn't invoked.

e.g.

002,!R1D5FdP32|
2,OK 

will result in the listener being invoked, whereas

002,!R1D5FdP32|
002,OK 

will not invoke the listener, as 002 doesn't match the key 2 in responseListeners.

Parsing the lookup as an int before converting it to a string works as you'd expect, although you need to be careful to check the response isn't a Lightwave JSON response first.

Thanks mutely!

rooi commented 4 years ago

Hi,

Thanks for catching this!

It's been a long time since I've looked at this.

You're suggesting to change this: var responseListenerData = this.responseListeners[code.toString()];

Into this, right? var responseListenerData = this.responseListeners[parseInt(code, 10).toString()];

What do you mean by being careful to check for a JSON response? I wasn't aware that that there could be a json response from the ICS-1000

jshiell commented 4 years ago

Hey,

Yes, dead on. The extra catch is the aforementioned JSON responses: e.g. from https://api.lightwaverf.com/introduction_basic_comms.html#json-outputs-link

S[255.255.255.255:9760]: 111,!R1D1F1 
R[192.168.1.103:9761]: 111,OK 
R[192.168.1.103:9760]: *!{"trans":1,"mac":"03:34:BC","time":1456495650,"pkt":"433T","fn":"on","room":1,"dev":1}

Apparently newer firmwares for the Link will return a JSON response after the traditional OK. They're prefix with *! however, so I'd suggest just skipping anything that starts with that.

rooi commented 4 years ago

Finally got to it. It should be fixed, please reopen if you see something strange

jshiell commented 4 years ago

Thank you, much appreciated!