node-temper1
node-temper1 copied to clipboard
readTemperature creates a new HID device each time
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.
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:
@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.
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);
}
});
})
Thank you @mKeRix , I rolled back the change