node-temper1 icon indicating copy to clipboard operation
node-temper1 copied to clipboard

readTemperature creates a new HID device each time

Open rektide opened this issue 8 years ago • 4 comments

https://github.com/asmuelle/node-temper1/blob/4f44eabd10f2cb8d08478e09625d7235745275b4/index.js#L28 shows that each readTemperature line creates a new HID object. This is a not-insignificant object creation (https://github.com/node-hid/node-hid/blob/265341e25bc2f04c983f2edd13ccc890309d0f84/nodehid.js#L12).

It would be sweet to have a persistent object that can be probed for the temperature.

rektide avatar Aug 10 '16 01:08 rektide

This issue is quite timely!

I suspect that adopting this object oriented API would be helpful in preventing the nondeterministic error I noted in #9. Basically, I think what is happening is that since macOS does not have interface numbers on USB devices, the new HID.HID(path) call might be randomly returning either one of the two interfaces at path, only one of which is actually openable by node-hid.

However, if the interface were changed to return an encapsulation of the device/interface instead of the nonspecific path string, then temper1 could ensure that it always returns a working device/interface.

All that said, I am still very new to USB internals, so my diagnosis of the issue I am experiencing on macOS may be way off! But in any case, I would also very much like to see an object oriented API for temper1. :smile:

limulus avatar Aug 10 '16 16:08 limulus

@rektide, thank you. As a first simple solution I just cache the HID objects by path. what do you think? I am not very much into object oriented javascript programming, but I will try to come up with a OO solution later.

and also thanx to you @limulus. I tested on OSX 10.11.6 and invoked readTemperature() in a loop. This switched the USB device into keyboard mode so I think the command I send to the micro controller ([0x01, 0x80, 0x33, 0x09, 0x00, 0x00, 0x00, 0x00]) is not quite right. I will try to find a solution.

asmuelle avatar Aug 10 '16 17:08 asmuelle

Your latest commit (version 0.0.8) makes my Node application crash on the second read of a device. 0.0.7 works fine. Since it doesn't give me an error at all I couldn't debug it sadly. Here's my code being called in an interval of 15 seconds for reference:

var that = this;
var devices = temper.getDevices();

devices.forEach(function (device, index) {
    temper.readTemperature(device, function(err, value) {
        if (err) {
            console.error(err);
        }
        else {
            var payload = {
                id: index,
                temperature: value,
                unit: 'C'
            };

            that.callback(channel, payload);
        }
    });
})

mKeRix avatar Aug 18 '16 19:08 mKeRix

Thank you @mKeRix , I rolled back the change

asmuelle avatar Aug 25 '16 10:08 asmuelle