sean9keenan / BigAssFansAPI

Unofficial API for Big Ass Fans!
MIT License
55 stars 12 forks source link

Receiving bind EADDRINUSE 0.0.0.0:31415 when invoking via Node-Express #6

Open LupoLoopy opened 8 years ago

LupoLoopy commented 8 years ago

events.js:160 throw er; // Unhandled 'error' event ^

Error: bind EADDRINUSE 0.0.0.0:31415 at Object.exports._errnoException (util.js:1036:11) at exports._exceptionWithHostPort (util.js:1059:20) at dgram.js:221:18 at _combinedTickCallback (internal/process/next_tick.js:77:11) at process._tickCallback (internal/process/next_tick.js:98:9)

Now - I could handle the error, but I'm interested to understand why repeatedly hitting the API is causing this response?

sean9keenan commented 8 years ago

In what way are you repeatedly hitting the endpoint? That error happens if multiple "fan masters" are trying to listen to fans. The solution (in general) is to have all requests go through the same fan master - which in turn is the only object that binds to port 31415 - which is the port that the fans communicate over

wolispace commented 6 years ago

Hi Sean, great API, thanks!

I had this problem too and my solution is to run myMaster.onFanFullyUpdated and stuff my fans into a globally accessible object.. then the http checks for thet before trying to interact with things.

I have a long way to go to get it the way I want but here is some basic functional code:

var bigAssApi = require("../BigAssApi");
var http = require('http');

var myMaster = new bigAssApi.FanMaster(3); // Expect 3 fans in my setup
var myFans = {}; // global holds all fans when they have been found

myMaster.onFanFullyUpdated = function(myBigAss){
  console.log(myBigAss.name);
  myFans[myBigAss.name] = myBigAss; // put each fan in the global
}

//create a server object:
http.createServer(function (req, res) {
  var content = 'Waiting for fans - try again...';
  var fanName = 'Living Room Fan';
  var oneFan = myFans[fanName]; // read a named fan from the global
  if (req.url !== '/favicon.ico') { // ignore this request
    console.log('Controlling ' + oneFan['name'] + ' ' + req.url);
    if (oneFan) {
      content = fanName + ' found. bright was [' + oneFan.light.brightness +']';
      var params = req.url.split('/');
      var state = params.length > 0 ? params[1] : 'off';
      if (state === 'on') {
        oneFan.light.brightness = 9;
        console.log('ON');
      } else {
        oneFan.light.brightness = 0;
        console.log('OFF');
      }

      content = content + ' state= ' + state + '. Now bright=[' +  oneFan.light.brightness + ']';
    }
    content = "<a href='/on'>On</a> | <a href='/off'>Off</a>";

    var contentHeader = '<html><head><meta name="viewport" content="width=device-width, initial-scale=1"><head><body>';
    var contentFooter = '</body></html>';
    res.write(contentHeader + content + contentFooter); //write a response to the client
    res.end(); //end the response
  }

}).listen(8080); //the server object listens on port 8080