Merge pull request #234 from hybridgroup/workMode
Work mode configuration
This commit is contained in:
commit
3ceb6cda85
11
lib/cylon.js
11
lib/cylon.js
|
@ -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
|
||||
|
|
37
lib/robot.js
37
lib/robot.js
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue