Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to stop discovery when predefined lights are found (base branch es2015) #61

Open
wants to merge 5 commits into
base: es2015
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,9 @@ client.init({
debug: false, // logs all messages in console if turned on
address: '0.0.0.0', // the IPv4 address to bind the udp connection to
broadcast: '255.255.255.255', // set's the IPv4 broadcast address which is addressed to discover bulbs
lights: [] // Can be used provide a list of known light IPv4 ip addresses if broadcast packets in network are not allowed
// For example: ['192.168.0.112', '192.168.0.114'], this will then be addressed directly
lights: [], // Can be used provide a list of known light IPv4 ip addresses if broadcast packets in network are not allowed
// For example: ['192.168.0.112', '192.168.0.114'], this will then be addressed directly
stopAfterDiscovery: false // stops discovery process after discovering all known lights (requires list
// of addresses provided with "lights" setting)
});
```
93 changes: 93 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"lolex": "^1.6.0",
"mkdirp": "^0.5.1",
"mocha": "^3.5.3",
"rimraf": "^2.6.2"
"rimraf": "^2.6.2",
"sinon": "^4.0.2"
},
"directories": {
"test": "test",
Expand Down
48 changes: 47 additions & 1 deletion src/lifx/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
const util = require('util');
const dgram = require('dgram');
const EventEmitter = require('eventemitter3');
const {defaults, isArray, result, find, bind, forEach} = require('lodash');
const {
defaults, isArray, isBoolean, isString, result, find, bind, forEach, keys, every, includes, filter
} = require('lodash');
const Packet = require('../lifx').packet;
const {Light, constants, utils} = require('../lifx');

Expand Down Expand Up @@ -40,6 +42,9 @@ function Client() {
this.resendMaxTimes = 5;
this.source = utils.getRandomHexString(8);
this.broadcastAddress = '255.255.255.255';
this.lightAddresses = [];
this.stopAfterDiscovery = false;
this.discoveryCompleted = false;
}
util.inherits(Client, EventEmitter);

Expand All @@ -58,6 +63,7 @@ util.inherits(Client, EventEmitter);
* @param {String} [options.source] The source to send to light, must be 8 chars lowercase or digit
* @param {Boolean} [options.startDiscovery] Weather to start discovery after initialization or not
* @param {Array} [options.lights] Pre set list of ip addresses of known addressable lights
* @param {Boolean} [options.stopAfterDiscovery] Stop discovery after discovering known addressable lights defined with options.light
* @param {String} [options.broadcast] The broadcast address to use for light discovery
* @param {Number} [options.sendPort] The port to send messages to
* @param {Function} [callback] Called after initialation
Expand All @@ -72,6 +78,7 @@ Client.prototype.init = function(options, callback) {
source: '',
startDiscovery: true,
lights: [],
stopAfterDiscovery: false,
broadcast: '255.255.255.255',
sendPort: constants.LIFX_DEFAULT_PORT,
resendPacketDelay: 150,
Expand Down Expand Up @@ -134,6 +141,13 @@ Client.prototype.init = function(options, callback) {
throw new TypeError('LIFX Client lights option array element \'' + light + '\' is not expected IPv4 format');
}
});
this.lightAddresses = opts.lights;

if (!isBoolean(opts.stopAfterDiscovery)) {
throw new TypeError('LIFX Client stopAfterDiscovery must be a boolean');
} else {
this.stopAfterDiscovery = opts.stopAfterDiscovery;
}
}

if (opts.source !== '') {
Expand Down Expand Up @@ -334,6 +348,14 @@ Client.prototype.startDiscovery = function(lights) {
sendDiscoveryPacket();
};

/**
* Checks if light discovery is in progress
* @return {Boolean} is discovery in progress
*/
Client.prototype.isDiscovering = function() {
return this.discoveryTimer !== null;
};

/**
* Checks all registered message handlers if they request the given message
* @param {Object} msg message to check handler for
Expand Down Expand Up @@ -418,6 +440,15 @@ Client.prototype.processDiscoveryPacket = function(err, msg, rinfo) {
this.devices[msg.target].address = rinfo.address;
this.devices[msg.target].seenOnDiscovery = this.discoveryPacketSequence;
}

// Check if discovery should be stopped
if (this.stopAfterDiscovery && !this.discoveryCompleted) {
if (this.predefinedDiscoveredAndOnline()) {
this.emit('discovery-completed');
this.stopDiscovery();
this.discoveryCompleted = true;
}
}
}
};

Expand All @@ -444,6 +475,21 @@ Client.prototype.stopDiscovery = function() {
this.discoveryTimer = null;
};

/**
* Checks if all predefined lights are discovered and online
* @return {Boolean} are lights discovered and online
*/
Client.prototype.predefinedDiscoveredAndOnline = function() {
const predefinedDevices = filter(this.devices, (device) => includes(this.lightAddresses, device.address));

const numDiscovered = keys(this.devices).length;
const allDiscovered = numDiscovered >= this.lightAddresses.length;
const allOnline = every(predefinedDevices, (device) => device.status === 'on');
const labelsReceived = every(predefinedDevices, (device) => isString(device.label));

return allDiscovered && allOnline && labelsReceived;
};

/**
* Send a LIFX message objects over the network
* @param {Object} msg A message object or multiple with data to send
Expand Down
Loading