Add function to dynamically start devices
This commit is contained in:
parent
4ec19961f8
commit
8921e902eb
49
lib/robot.js
49
lib/robot.js
|
@ -315,30 +315,43 @@ Robot.prototype.startDevices = function(callback) {
|
||||||
|
|
||||||
log("Starting devices.");
|
log("Starting devices.");
|
||||||
|
|
||||||
var starters = _.map(this.devices, function(device, name) {
|
var starters = _.map(this.devices, function(device) {
|
||||||
this[name] = device;
|
|
||||||
|
|
||||||
return function(cb) {
|
return function(cb) {
|
||||||
if (device.started === true) {
|
return this.startDevice(device, cb);
|
||||||
return cb.call(device);
|
}.bind(this);
|
||||||
}
|
|
||||||
|
|
||||||
var str = "Starting device '" + name + "'";
|
|
||||||
|
|
||||||
if (device.pin) {
|
|
||||||
str += " on pin " + device.pin;
|
|
||||||
}
|
|
||||||
|
|
||||||
log(str + ".");
|
|
||||||
device.started = true;
|
|
||||||
|
|
||||||
return device.start.call(device, cb);
|
|
||||||
};
|
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
return _.parallel(starters, callback);
|
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.
|
* Halts the Robot, attempting to gracefully stop devices and connections.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue