Clean up #start to use Async.series
This commit is contained in:
parent
4e43483e9d
commit
dd553a55f2
43
lib/robot.js
43
lib/robot.js
|
@ -146,14 +146,14 @@ Robot.prototype.toJSON = function() {
|
||||||
//
|
//
|
||||||
// Returns initialized connections
|
// Returns initialized connections
|
||||||
Robot.prototype.initConnections = function(connections) {
|
Robot.prototype.initConnections = function(connections) {
|
||||||
Logger.info("Initializing connections...");
|
Logger.info("Initializing connections.");
|
||||||
if (connections == null) { return; }
|
if (connections == null) { return; }
|
||||||
|
|
||||||
connections = [].concat(connections);
|
connections = [].concat(connections);
|
||||||
|
|
||||||
for (var i = 0; i < connections.length; i++) {
|
for (var i = 0; i < connections.length; i++) {
|
||||||
var connection = connections[i];
|
var connection = connections[i];
|
||||||
Logger.info("Initializing connection '" + connection.name + "'...");
|
Logger.info("Initializing connection '" + connection.name + "'.");
|
||||||
connection['robot'] = this;
|
connection['robot'] = this;
|
||||||
this.connections[connection.name] = new Connection(connection);
|
this.connections[connection.name] = new Connection(connection);
|
||||||
}
|
}
|
||||||
|
@ -167,14 +167,14 @@ Robot.prototype.initConnections = function(connections) {
|
||||||
//
|
//
|
||||||
// Returns initialized devices
|
// Returns initialized devices
|
||||||
Robot.prototype.initDevices = function(devices) {
|
Robot.prototype.initDevices = function(devices) {
|
||||||
Logger.info("Initializing devices...");
|
Logger.info("Initializing devices.");
|
||||||
if (devices == null) { return; }
|
if (devices == null) { return; }
|
||||||
|
|
||||||
devices = [].concat(devices);
|
devices = [].concat(devices);
|
||||||
|
|
||||||
for (var i = 0; i < devices.length; i++) {
|
for (var i = 0; i < devices.length; i++) {
|
||||||
var device = devices[i];
|
var device = devices[i];
|
||||||
Logger.info("Initializing device '" + device.name + "'...");
|
Logger.info("Initializing device '" + device.name + "'.");
|
||||||
device['robot'] = this;
|
device['robot'] = this;
|
||||||
this.devices[device.name] = this._createDevice(device);
|
this.devices[device.name] = this._createDevice(device);
|
||||||
}
|
}
|
||||||
|
@ -193,17 +193,26 @@ Robot.prototype._createDevice = function(device) {
|
||||||
// Returns the result of the work
|
// Returns the result of the work
|
||||||
Robot.prototype.start = function() {
|
Robot.prototype.start = function() {
|
||||||
var self = this;
|
var self = this;
|
||||||
return this.startConnections(function() {
|
|
||||||
return self.startDevices(function(err) {
|
var begin = function(callback) {
|
||||||
if (err) {
|
self.work.call(self, self);
|
||||||
throw err;
|
self.running = true;
|
||||||
}else{
|
self.emit('working');
|
||||||
self.work.call(self, self);
|
|
||||||
self.running = true;
|
Logger.info('Working.');
|
||||||
Logger.info("Working...");
|
|
||||||
self.emit('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) {
|
Robot.prototype.startConnections = function(callback) {
|
||||||
var starters = {};
|
var starters = {};
|
||||||
|
|
||||||
Logger.info("Starting connections...");
|
Logger.info("Starting connections.");
|
||||||
|
|
||||||
for (var n in this.connections) {
|
for (var n in this.connections) {
|
||||||
var connection = this.connections[n];
|
var connection = this.connections[n];
|
||||||
|
@ -234,7 +243,7 @@ Robot.prototype.startConnections = function(callback) {
|
||||||
Robot.prototype.startDevices = function(callback) {
|
Robot.prototype.startDevices = function(callback) {
|
||||||
var starters = {};
|
var starters = {};
|
||||||
|
|
||||||
Logger.info("Starting devices...");
|
Logger.info("Starting devices.");
|
||||||
|
|
||||||
for (var n in this.devices) {
|
for (var n in this.devices) {
|
||||||
var device = this.devices[n];
|
var device = this.devices[n];
|
||||||
|
|
Loading…
Reference in New Issue