Grayda / node-orvibo

A node.js package to control Orvibo products
78 stars 18 forks source link

Only send setState event/callback on confirmation of state change #2

Closed dotsam closed 8 years ago

dotsam commented 8 years ago

Protocol documentation shows that the socket will send a confirmation when it changes state (Message 6463) but currently nothing is done with that message besides a debug log. It would be more useful if calling setState wouldn't fire/callback until this message was received, so that code could continue to call it until it's confirmed as successful.

At the very least, firing another event when 6463 is received would be nice. I can write the pull request for that, but wanted to see if you had any thoughts on the best way to have setState wait for this message as well.

Grayda commented 8 years ago

Hi Sam, that's a great idea. I had half a thought of implementing something like that a while ago, but I haven't touched the code for a little bit (I've got another project I'm working on).

I'm actually planning to do a rewrite at some point in the future, using promises, so then you could call something like:

orvibo[0].setState(true).then(function(state) {
    console.log("State actually changed to " + state + ". I promise!")
}).error(function(err) {
    console.log("Error setting state. Error was: " + err)
})

And know for sure that the state was really, truly set on the socket, because the promise wouldn't resolve until the response came back from the socket. But that might be a little ways off, given I'm neck-deep in another project. So feel free to do a PR. If it helps people in the interim, I'm all for it!

Grayda commented 8 years ago

Closing, as I believe the issue has been resolved by the PR. Thanks for taking the time to help out :)