Devices will only be started up once, and can be added dynamically

This commit is contained in:
deadprogram 2015-09-01 18:12:39 -07:00
parent 9cdbdd1d37
commit 4ec19961f8
2 changed files with 25 additions and 0 deletions

View File

@ -319,6 +319,10 @@ Robot.prototype.startDevices = function(callback) {
this[name] = device;
return function(cb) {
if (device.started === true) {
return cb.call(device);
}
var str = "Starting device '" + name + "'";
if (device.pin) {
@ -326,6 +330,8 @@ Robot.prototype.startDevices = function(callback) {
}
log(str + ".");
device.started = true;
return device.start.call(device, cb);
};
}, this);

View File

@ -442,6 +442,25 @@ describe("Robot", function() {
expect(bot.devices.alpha.start).to.be.called;
expect(bot.devices.bravo.start).to.be.called;
});
it("runs #start on each device only once", function() {
bot.startDevices();
bot.startDevices();
expect(bot.devices.alpha.start).to.be.called.once;
expect(bot.devices.bravo.start).to.be.called.once;
});
it("runs #start on a newly added device", function() {
bot.startDevices();
bot.device("charlie", { driver: "ping" });
stub(bot.devices.charlie, "start").returns(true);
bot.startDevices();
expect(bot.devices.alpha.start).to.be.called.once;
expect(bot.devices.bravo.start).to.be.called.once;
expect(bot.devices.charlie.start).to.be.called.once;
});
});
describe("#halt", function() {