diff --git a/src/discover.js b/src/discover.js index 7c52c1e..90c0235 100644 --- a/src/discover.js +++ b/src/discover.js @@ -23,6 +23,7 @@ module.exports = function (options) { return new Promise(function (resolve, reject) { var discoverer = dgram.createSocket('udp4'); discoverer.bind(function () { + debug("Milight: Discovery socket openend"); discoverer.setBroadcast(true); }); @@ -41,6 +42,7 @@ module.exports = function (options) { timeoutId = setTimeout(function() { try { discoverer.close(); + debug("Milight: Discovery socket closed"); } catch (ex) {/*ignore*/} resolve(discoResults); }, timeout) @@ -71,6 +73,7 @@ module.exports = function (options) { debug(error); try { discoverer.close(); + debug("Milight: Discovery socket closed"); } catch (ex) {/*ignore*/} reject(error); }); diff --git a/src/milight.js b/src/milight.js index d15fbd6..9deb17b 100644 --- a/src/milight.js +++ b/src/milight.js @@ -78,22 +78,20 @@ MilightController.prototype._createSocket = function () { return resolve(); } else { - debug("Initializing Socket"); + debug("Milight: Initializing Socket"); var socket = dgram.createSocket('udp4'); - if (self._broadcastMode) { - socket.bind(function () { - socket.setBroadcast(true); - self.clientSocket = socket; - debug("Milight: Initializing Socket (broadcast mode) completed"); - return resolve(); - }); - } - else { + socket.bind(function () { self.clientSocket = socket; - debug("Milight: Initializing Socket done"); + if (self._broadcastMode) { + socket.setBroadcast(true); + debug("Milight: Initializing Socket (broadcast mode) done"); + } + else { + debug("Milight: Initializing Socket done"); + } return resolve(); - } + }) } }); }); @@ -203,6 +201,7 @@ MilightController.prototype.close = function () { if (self.clientSocket) { self.clientSocket.close(); delete self.clientSocket; + debug("Milight: Socket closed"); } return Promise.resolve(); }) diff --git a/test/index.js b/test/index.js index 421bf87..f66a3a1 100644 --- a/test/index.js +++ b/test/index.js @@ -118,7 +118,7 @@ describe("Testing transmission of control sequences", function () { }); it("shall receive the command rgbw on in broadcast mode", function (done) { - myLight = new Milight({ + var myLight = new Milight({ commandRepeat: 1 }); var calls = [ @@ -150,7 +150,7 @@ describe("Testing transmission of control sequences", function () { commands2.rgbw.brightness2(100) ]; var test = function(total, command) { - return myLight.sendCommands(command) + return light.sendCommands(command) .then(function () { expect(bytesReceived.length).toBe(command.length); expect(JSON.stringify(bytesReceived)).toEqual(JSON.stringify(command)); @@ -176,7 +176,7 @@ describe("Testing transmission of control sequences", function () { commands2.rgbw.hue.apply(commands2.rgbw, args) ]; var innerTest = function(total, command) { - return myLight.sendCommands(command) + return light.sendCommands(command) .then(function () { expect(bytesReceived.length).toBe(command.length); expect(JSON.stringify(bytesReceived)).toEqual(JSON.stringify(command)); @@ -201,7 +201,7 @@ describe("Testing transmission of control sequences", function () { commands2.rgbw.rgb255(255, 255, 255) ]; var test = function(total, command) { - return myLight.sendCommands(command) + return light.sendCommands(command) .then(function () { expect(bytesReceived.length).toBe(flattenDeep(command).length); expect(JSON.stringify(bytesReceived)).toEqual(JSON.stringify(flattenDeep(command))); @@ -212,7 +212,6 @@ describe("Testing transmission of control sequences", function () { Promise.reduce( calls, test, 0 ).finally(function () { - myLight.close(); done(); }) }); @@ -232,7 +231,7 @@ describe("Testing transmission of control sequences", function () { flattenDeep(commands2.rgbw.rgb255.apply(commands2.rgbw, args)) ]; var innerTest = function(total, command) { - return myLight.sendCommands(command) + return light.sendCommands(command) .then(function () { expect(bytesReceived.length).toBe(command.length); expect(JSON.stringify(bytesReceived)).toEqual(JSON.stringify(command)); @@ -258,7 +257,7 @@ describe("Testing transmission of control sequences", function () { commands2.rgbw[commandName](1) ]; var innerTest = function(total, command) { - return myLight.sendCommands(command) + return light.sendCommands(command) .then(function () { expect(bytesReceived.length).toBe(command.length); expect(JSON.stringify(bytesReceived)).toEqual(JSON.stringify(command)); @@ -284,7 +283,7 @@ describe("Testing transmission of control sequences", function () { commands2.rgbw[commandName]() ]; var innerTest = function(total, command) { - return myLight.sendCommands(command) + return light.sendCommands(command) .then(function () { expect(bytesReceived.length).toBe(command.length); expect(JSON.stringify(bytesReceived)).toEqual(JSON.stringify(command)); @@ -310,7 +309,7 @@ describe("Testing transmission of control sequences", function () { commands2.white[commandName](1) ]; var innerTest = function(total, command) { - return myLight.sendCommands(command) + return light.sendCommands(command) .then(function () { expect(bytesReceived.length).toBe(command.length); expect(JSON.stringify(bytesReceived)).toEqual(JSON.stringify(command)); @@ -336,7 +335,7 @@ describe("Testing transmission of control sequences", function () { commands2.white[commandName]() ]; var innerTest = function(total, command) { - return myLight.sendCommands(command) + return light.sendCommands(command) .then(function () { expect(bytesReceived.length).toBe(command.length); expect(JSON.stringify(bytesReceived)).toEqual(JSON.stringify(command)); @@ -362,7 +361,7 @@ describe("Testing transmission of control sequences", function () { commands2.rgb[commandName]() ]; var innerTest = function(total, command) { - return myLight.sendCommands(command) + return light.sendCommands(command) .then(function () { expect(bytesReceived.length).toBe(command.length); expect(JSON.stringify(bytesReceived)).toEqual(JSON.stringify(command)); @@ -393,7 +392,7 @@ describe("Testing transmission of control sequences", function () { commands2.rgb.hue.apply(commands.rgb, args) ]; var innerTest = function(total, command) { - return myLight.sendCommands(command) + return light.sendCommands(command) .then(function () { expect(bytesReceived.length).toBe(command.length); expect(JSON.stringify(bytesReceived)).toEqual(JSON.stringify(command));