Merge pull request #234 from hybridgroup/workMode

Work mode configuration
This commit is contained in:
Ron Evans 2014-10-28 17:37:24 +00:00
commit 3ceb6cda85
2 changed files with 35 additions and 13 deletions

View File

@ -75,9 +75,18 @@ Cylon.api = function api() {
//
// Returns nothing
Cylon.start = function start() {
var starters = [];
for (var bot in this.robots) {
this.robots[bot].start();
starters.push(this.robots[bot].start);
}
Async.parallel(starters, function(err, results) {
var mode = Utils.fetch(Config, 'workMode', 'async');
if (mode === 'sync') {
for (var bot in this.robots) {
this.robots[bot].startWork();
}
}
}.bind(this));
};
// Public: Sets the internal configuration, based on passed options

View File

@ -248,26 +248,23 @@ Robot.prototype.initDevices = function(devices) {
// Starts the connections, devices, and work.
//
// Returns the result of the work
Robot.prototype.start = function() {
Robot.prototype.start = function(callback) {
if (this.running) {
return this;
}
var begin = function(callback) {
Logger.info('Working.');
this.emit('ready', this);
this.work.call(this, this);
this.running = true;
callback(null, true);
}.bind(this);
var mode = Utils.fetch(Config, 'workMode', 'async');
Async.series([
this.startConnections,
this.startDevices,
begin
], function(err) {
function(callback) {
if (mode === 'async') {
this.startWork();
}
callback(null, true);
}.bind(this)
], function(err, results) {
if (!!err) {
Logger.fatal("An error occured while trying to start the robot:");
Logger.fatal(err);
@ -276,11 +273,27 @@ Robot.prototype.start = function() {
}
this.emit('error', err);
}
if (typeof(callback) === 'function') {
callback(err, results);
}
}.bind(this));
return this;
};
// Public: Starts the Robot's work and triggers a callback
//
// callback - callback function to be triggered
//
// Returns nothing
Robot.prototype.startWork = function() {
Logger.info('Working.');
this.emit('ready', this);
this.work.call(this, this);
this.running = true;
};
// Public: Starts the Robot's connections and triggers a callback
//
// callback - callback function to be triggered