Clean up #start to use Async.series

This commit is contained in:
Andrew Stewart 2014-06-09 13:55:44 -07:00
parent 4e43483e9d
commit dd553a55f2
1 changed files with 26 additions and 17 deletions

View File

@ -146,14 +146,14 @@ Robot.prototype.toJSON = function() {
//
// Returns initialized connections
Robot.prototype.initConnections = function(connections) {
Logger.info("Initializing connections...");
Logger.info("Initializing connections.");
if (connections == null) { return; }
connections = [].concat(connections);
for (var i = 0; i < connections.length; i++) {
var connection = connections[i];
Logger.info("Initializing connection '" + connection.name + "'...");
Logger.info("Initializing connection '" + connection.name + "'.");
connection['robot'] = this;
this.connections[connection.name] = new Connection(connection);
}
@ -167,14 +167,14 @@ Robot.prototype.initConnections = function(connections) {
//
// Returns initialized devices
Robot.prototype.initDevices = function(devices) {
Logger.info("Initializing devices...");
Logger.info("Initializing devices.");
if (devices == null) { return; }
devices = [].concat(devices);
for (var i = 0; i < devices.length; i++) {
var device = devices[i];
Logger.info("Initializing device '" + device.name + "'...");
Logger.info("Initializing device '" + device.name + "'.");
device['robot'] = this;
this.devices[device.name] = this._createDevice(device);
}
@ -193,17 +193,26 @@ Robot.prototype._createDevice = function(device) {
// Returns the result of the work
Robot.prototype.start = function() {
var self = this;
return this.startConnections(function() {
return self.startDevices(function(err) {
if (err) {
throw err;
}else{
self.work.call(self, self);
self.running = true;
Logger.info("Working...");
self.emit('working');
}
});
var begin = function(callback) {
self.work.call(self, self);
self.running = true;
self.emit('working');
Logger.info('Working.');
callback(null, true);
};
Async.series([
self.startConnections,
self.startDevices,
begin
], function(err, results) {
if (!!err) {
Logger.fatal("An error occured while trying to start the robot:");
Logger.fatal(err);
}
});
};
@ -215,7 +224,7 @@ Robot.prototype.start = function() {
Robot.prototype.startConnections = function(callback) {
var starters = {};
Logger.info("Starting connections...");
Logger.info("Starting connections.");
for (var n in this.connections) {
var connection = this.connections[n];
@ -234,7 +243,7 @@ Robot.prototype.startConnections = function(callback) {
Robot.prototype.startDevices = function(callback) {
var starters = {};
Logger.info("Starting devices...");
Logger.info("Starting devices.");
for (var n in this.devices) {
var device = this.devices[n];