Slightly refactor constructor

This commit is contained in:
Andrew Stewart 2014-06-09 13:39:16 -07:00
parent dbde3d5b7d
commit 4e43483e9d
1 changed files with 25 additions and 21 deletions

View File

@ -17,10 +17,6 @@ var Connection = require("./connection"),
var Async = require("async"),
EventEmitter = require('events').EventEmitter;
// A Robot is the primary interface for interacting with a collection of physical
// computing capabilities.
var Robot;
// Public: Creates a new Robot
//
// opts - object containing Robot options
@ -43,11 +39,13 @@ var Robot;
// work: (me) ->
// Utils.every 1.second(), ->
// me.sphero.roll 60, Math.floor(Math.random() * 360//
module.exports = Robot = function Robot(opts) {
var Robot = module.exports = function Robot(opts) {
if (opts == null) {
opts = {};
}
var self = this;
var methods = [
"toString",
"registerDriver",
@ -62,43 +60,49 @@ module.exports = Robot = function Robot(opts) {
"initConnections"
];
for (var i = 0; i < methods.length ; i++) {
var method = methods[i];
this[method] = Utils.bind(this[method], this);
}
methods.forEach(function(method) {
self[method] = Utils.bind(self[method], self);
});
this.name = opts.name || this.constructor.randomName();
this.connections = {};
this.devices = {};
this.adaptors = {};
this.drivers = {};
this.commands = [];
this.running = false;
this.registerAdaptor("./test/loopback", "loopback");
this.registerAdaptor("./test/test-adaptor", "test");
this.registerDriver("./test/ping", "ping");
this.registerDriver("./test/test-driver", "test");
this.initConnections(opts.connection || opts.connections);
this.initDevices(opts.device || opts.devices);
this.work = opts.work || opts.play;
this.work || (this.work = function() { Logger.info("No work yet"); });
this.registerDefaults();
this.initConnections(opts.connection || opts.connections);
this.initDevices(opts.device || opts.devices);
for (var n in opts) {
var func = opts[n],
reserved = ['connection', 'connections', 'device', 'devices', 'work'];
if (reserved.indexOf(n) < 0) { this[n] = func; }
if (reserved.indexOf(n) < 0) {
this[n] = func;
}
}
};
Utils.subclass(Robot, EventEmitter);
// Public: Registers the default Drivers and Adaptors with Cylon.
//
// Returns nothing.
Robot.prototype.registerDefaults = function registerDefaults() {
this.registerAdaptor("./test/loopback", "loopback");
this.registerAdaptor("./test/test-adaptor", "test");
this.registerDriver("./test/ping", "ping");
this.registerDriver("./test/test-driver", "test");
};
// Public: Generates a random name for a Robot.
//
// Returns a string name