From 8921e902ebb8273f9a87d8a1cbd2d11cf19a328e Mon Sep 17 00:00:00 2001 From: deadprogram Date: Mon, 7 Sep 2015 08:34:01 -0700 Subject: [PATCH] Add function to dynamically start devices --- lib/robot.js | 49 +++++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/lib/robot.js b/lib/robot.js index 20fd764..ffcf0ff 100644 --- a/lib/robot.js +++ b/lib/robot.js @@ -315,30 +315,43 @@ Robot.prototype.startDevices = function(callback) { log("Starting devices."); - var starters = _.map(this.devices, function(device, name) { - this[name] = device; - + var starters = _.map(this.devices, function(device) { return function(cb) { - if (device.started === true) { - return cb.call(device); - } - - var str = "Starting device '" + name + "'"; - - if (device.pin) { - str += " on pin " + device.pin; - } - - log(str + "."); - device.started = true; - - return device.start.call(device, cb); - }; + return this.startDevice(device, cb); + }.bind(this); }, this); return _.parallel(starters, callback); }; +/** + * Starts a single device on Robot + * + * @param {Object} device to start + * @param {Function} callback function to be triggered after the device is + * started + * @return {void} + */ +Robot.prototype.startDevice = function(device, callback) { + if (device.started === true) { + return callback.call(device); + } + + var log = this.log; + var str = "Starting device '" + device.name + "'"; + + if (device.pin) { + str += " on pin " + device.pin; + } + + log(str + "."); + this[device.name] = device; + device.start.call(device, callback); + device.started = true; + + return device.started; +}; + /** * Halts the Robot, attempting to gracefully stop devices and connections. *