Clean up robot initialization

This commit is contained in:
Andrew Stewart 2014-07-12 10:48:54 -07:00
parent ee366739d3
commit 1c8e84cfb2
1 changed files with 16 additions and 22 deletions

View File

@ -49,9 +49,7 @@ var missingModuleError = function(module) {
// Utils.every 1.second(), ->
// me.sphero.roll 60, Math.floor(Math.random() * 360//
var Robot = module.exports = function Robot(opts) {
if (opts == null) {
opts = {};
}
opts = opts || {};
var methods = [
"toString",
@ -71,7 +69,7 @@ var Robot = module.exports = function Robot(opts) {
this[method] = this[method].bind(this);
}.bind(this));
this.name = opts.name || this.constructor.randomName();
this.name = opts.name || Robot.randomName();
this.connections = {};
this.devices = {};
this.adaptors = {};
@ -125,19 +123,21 @@ Robot.randomName = function() {
Robot.prototype.toJSON = function() {
var connections = (function() {
var results = [];
for (var n in this.connections) {
var conn = this.connections[n];
results.push(conn.toJSON());
results.push(this.connections[n].toJSON());
}
return results;
}).call(this);
var devices = (function() {
var results = [];
for (var n in this.devices) {
var device = this.devices[n];
results.push(device.toJSON());
results.push(this.devices[n].toJSON());
}
return results;
}).call(this);
@ -163,12 +163,11 @@ Robot.prototype.initConnections = function(connections) {
connections = [].concat(connections);
for (var i = 0; i < connections.length; i++) {
var connection = connections[i];
Logger.info("Initializing connection '" + connection.name + "'.");
connection['robot'] = this;
this.connections[connection.name] = new Connection(connection);
}
connections.forEach(function(conn) {
Logger.info("Initializing connection '" + conn.name + "'.");
conn['robot'] = this;
this.connections[conn.name] = new Connection(conn);
}.bind(this));
return this.connections;
};
@ -187,20 +186,15 @@ Robot.prototype.initDevices = function(devices) {
devices = [].concat(devices);
for (var i = 0; i < devices.length; i++) {
var device = devices[i];
devices.forEach(function(device) {
Logger.info("Initializing device '" + device.name + "'.");
device['robot'] = this;
this.devices[device.name] = this._createDevice(device);
}
this.devices[device.name] = new Device(device);
}.bind(this));
return this.devices;
};
Robot.prototype._createDevice = function(device) {
return new Device(device);
};
// Public: Starts the Robot working.
//
// Starts the connections, devices, and work.