EricSmekens / node-bluetooth-obd

Node package for communication with Bluetooth OBD connectors.
Other
267 stars 71 forks source link

Messages failing after disconnect #33

Open Reiss-Cashmore opened 5 years ago

Reiss-Cashmore commented 5 years ago

Hi, I am creating a graphing app based off your library. The first time I connect to a device it works perfectly (Other than I had to remove the "replies" part of the string that gets added to the Queue. It seems like it always appends a 1 to every PID which would just make my ELM Simulator return nothing. Removing this makes it work.

However back to the issue, once I disconnect and reconnect it seems there are really weird concatenated messages being sent to the BT device.

Seems like something weird goes on after a disconnect that doesn't properly clean up. I have a hunch it is something to do with the intervalWriter which may go deeper into the way the btSerial lib is handling the buffers. But am struggling to debug that. I've tried just adding all the functions I could find that detach listeners, detach pollers, clear queue,etc..etc. None of it helped. If you disconnect again and reconnect the problem only compounds. See my 3rd screenshot.

Screenshot of Good Calls on first connect on OBD Device

Screenshot of Data receieved back in front end from OBD Device

Screenshot of calls being sent after disconnect and reconnect.

Screenshot of Data receieved back in front end from OBD Device after disconnect and reconnect.

After second disconnect and reconnect

You can find my project here which should demonstrate the issue. https://github.com/Reiss-Cashmore/obdii-grapher

I am using Mac OSX if that helps.

EricSmekens commented 5 years ago

Kudo's on the amount of information you provided in this report. Still don't have a lot of time/focus on this project planned, so I think this will not be picked up by me.

How are you running your simulator, do you have a piece of hardware?

Reiss-Cashmore commented 5 years ago

Thanks, there is definitely a bug with instances of the buffers and the listeners that get attached. I couldn't track it down.

A work around I found is to just to completely reinstantiate your library after every disconnect (The OBDReader() object) i.e

var OBDReader = require('../lib/obd.js');
var btOBDReader = new OBDReader();

as well as reinstantiating the btSerial object after every disconnect. i.e

var btSerial = new (require('bluetooth-serial-port')).BluetoothSerialPort();

It's a pretty nuclear workaround but it is good enough for now. However I feel that the risk of a memory leak here is enormous. I'm fairly sure there are listeners still attached to the buffers somewhere. So although we "overwrite" the previous instance of your lib. Garbage collection will likely not dispose of it. However, I'll either have to monitor resource usage of my app over an extended period or refresh myself on the specifics of Garbage Collection to be sure this is the case. I don't yet know if the listeners/callbacks are considered the javascript equivalent of strong or weak reference.

EricSmekens commented 5 years ago

Yes, I can agree that several parts are very far from optimal. I alwasy have this wish to rewrite a large part of this module, but the lack of good simulation always refrains me from doing so.

Maybe I should write my mock, to mock the bluetooth + obd so I can work on this on my local computer without all the bluetooth hassle.