shokai / node-philips-hue

Node.js library for Philips Hue bridge API
http://npmjs.org/package/philips-hue
17 stars 7 forks source link

Connection reset #14

Open mAPBhlJ opened 7 years ago

mAPBhlJ commented 7 years ago

I get the error:

(node:3769) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: connect ECONNRESET 192.168.1.16:80

When I try to run:

var Hue = require('philips-hue');
var hue = new Hue();
var sleep = require('sleep');

var hue = new Hue;
hue.bridge = "192.168.1.16";  // from hue.getBridges
hue.username = "myid"; // from hue.auth

for(var i = 1; i<5;i++){
        hue.light(i).on();
        sleep.sleep(2);
}
sleep.sleep(1);
for(var i = 1; i<5;i++){
        hue.light(i).off();
        sleep.sleep(2);
}

As far as I know this should be working? I've obfuscated the username, but it's actually working for the first few commands.

Is it a network issue or an issue with my hue bridge? Even if so, I think the library should have some kind of safeguarding or reconnect retry?

shokai commented 7 years ago

Does it work if you do not use sleep?

mAPBhlJ commented 7 years ago

I'm sorry to bother you with this, using setInterval it seems to work. Also, sending too many requests about 10+ a sec will cause the Hue to drop the connection (this isn't caused by your library, rather a limitation of Hue).

var Hue = require('philips-hue');
var hue = new Hue();
var sleep = require('sleep');

var hue = new Hue;
hue.bridge = "192.168.1.16";  // from hue.getBridges
hue.username = "z39w3w8bcDNPitCKg1X47g-PjLr7JNOwtv3dnUUy"; // from hue.auth

var current = 1;

const intervalObj = setInterval(() => {
        hue.light(current).off();
        current++;
        if(current>5){current=1;}
        hue.light(current).on();
}, 5000);

However, what if the connection is ever reset? Does the library autoconnect (or is it stateless, as in, every time a new connection is made)?

mAPBhlJ commented 7 years ago

Okay I've made a testprogram that runs through a set of hue lights And after 3 seconds does a small spam.

var Hue = require('philips-hue');
var hue = new Hue();

hue.bridge = "192.168.1.16";  // from hue.getBridges
hue.username = "key"; // from hue.auth

var current = 1;

const intervalObj = setInterval(() => {
        hue.light(current).off();
        current++;
        if(current>10){current=1;}
        hue.light(current).on();
}, 1000);

setTimeout( ()=>{
        for(var i = 0; i<6; i++){
                hue.light(1).off();
        }
},3000);

I find that it does throw: (node:3905) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: read ECONNRESET

But actually continues gracefully after that. I feel that that the commands in the spam are lost and when the connection is unstable it may not always toggle your lights succesfully.

I could probably fill in the promise reject with a notification to the user or a retry after a timeout, is that the way to go?