Merge pull request #228 from hybridgroup/add/imperative-work
Add support for work to be triggered via an event
This commit is contained in:
commit
e91bc57de0
|
@ -0,0 +1,15 @@
|
|||
var Cylon = require('cylon');
|
||||
|
||||
var robot = new Cylon.Robot({
|
||||
connection: { name: 'loopback', adaptor: 'loopback' },
|
||||
device: { name: 'ping', driver: 'ping' },
|
||||
});
|
||||
|
||||
setInterval(function() {
|
||||
console.log("Hello, human!")
|
||||
console.log(robot.ping.ping());
|
||||
}, 1000);
|
||||
|
||||
setTimeout(function() {
|
||||
console.log("I've been at your command for 5 seconds now.")
|
||||
}, 5000);
|
|
@ -21,6 +21,7 @@ var Cylon = module.exports = {
|
|||
Logger: Logger,
|
||||
Driver: require('./driver'),
|
||||
Adaptor: require('./adaptor'),
|
||||
Robot: Robot,
|
||||
Utils: Utils,
|
||||
|
||||
IO: {
|
||||
|
|
15
lib/robot.js
15
lib/robot.js
|
@ -79,7 +79,7 @@ var Robot = module.exports = function Robot(opts) {
|
|||
this.work = opts.work || opts.play;
|
||||
|
||||
if (!this.work) {
|
||||
this.work = function() { console.log("No work yet."); }
|
||||
this.work = function() { Logger.debug("No work yet."); }
|
||||
}
|
||||
|
||||
this.registerDefaults();
|
||||
|
@ -120,6 +120,12 @@ var Robot = module.exports = function Robot(opts) {
|
|||
this.commands = opts.commands;
|
||||
}
|
||||
|
||||
var mode = Utils.fetch(Config, 'mode', 'auto');
|
||||
|
||||
if (mode === 'auto') {
|
||||
// run on the next tick, to allow for 'work' event handlers to be set up
|
||||
setTimeout(this.start, 0);
|
||||
}
|
||||
};
|
||||
|
||||
Utils.subclass(Robot, EventEmitter);
|
||||
|
@ -230,11 +236,11 @@ Robot.prototype.initDevices = function(devices) {
|
|||
// Returns the result of the work
|
||||
Robot.prototype.start = function() {
|
||||
var begin = function(callback) {
|
||||
Logger.info('Working.');
|
||||
|
||||
this.emit('work', this);
|
||||
this.work.call(this, this);
|
||||
this.running = true;
|
||||
this.emit('working');
|
||||
|
||||
Logger.info('Working.');
|
||||
|
||||
callback(null, true);
|
||||
}.bind(this);
|
||||
|
@ -249,6 +255,7 @@ Robot.prototype.start = function() {
|
|||
Logger.fatal(err);
|
||||
}
|
||||
});
|
||||
return this;
|
||||
};
|
||||
|
||||
// Public: Starts the Robot's connections and triggers a callback
|
||||
|
|
|
@ -316,8 +316,12 @@ describe("Robot", function() {
|
|||
expect(robot.work).to.be.called;
|
||||
});
|
||||
|
||||
it("emits the 'working' event", function() {
|
||||
expect(robot.emit).to.be.calledWith("working")
|
||||
it("emits the 'work' event", function() {
|
||||
expect(robot.emit).to.be.calledWith("work")
|
||||
});
|
||||
|
||||
it("returns the robot", function() {
|
||||
expect(robot.start()).to.be.eql(robot);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -27,4 +27,9 @@ global.source = function(module) {
|
|||
};
|
||||
|
||||
var Cylon = source('cylon');
|
||||
|
||||
Cylon.config({
|
||||
mode: "manual"
|
||||
});
|
||||
|
||||
Cylon.Logger.setup(false);
|
||||
|
|
Loading…
Reference in New Issue