Clean up constructor

This commit is contained in:
Andrew Stewart 2014-02-28 12:13:19 -08:00
parent f307aad0cd
commit 1ee35f1487
1 changed files with 32 additions and 26 deletions

View File

@ -8,9 +8,6 @@
'use strict'; 'use strict';
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }
var __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
require('./cylon'); require('./cylon');
require('./basestar'); require('./basestar');
require("./connection"); require("./connection");
@ -53,45 +50,54 @@ namespace("Cylon", function() {
// every 1.second(), -> // every 1.second(), ->
// me.sphero.roll 60, Math.floor(Math.random() * 360// // me.sphero.roll 60, Math.floor(Math.random() * 360//
function Robot(opts) { function Robot(opts) {
var func, n, reserved; if (opts == null) { opts = {}; }
if (opts == null) {
opts = {}; var methods = [
"toString",
"registerDriver",
"requireDriver",
"registerAdaptor",
"requireAdaptor",
"stop",
"startDevices",
"startConnections",
"start",
"initDevices",
"initConnections"
];
for (var i = 0; i < methods.length ; i++) {
var method = methods[i];
this[method] = bind(this[method], this);
} }
this.toString = __bind(this.toString, this);
this.registerDriver = __bind(this.registerDriver, this);
this.requireDriver = __bind(this.requireDriver, this);
this.registerAdaptor = __bind(this.registerAdaptor, this);
this.requireAdaptor = __bind(this.requireAdaptor, this);
this.stop = __bind(this.stop, this);
this.startDevices = __bind(this.startDevices, this);
this.startConnections = __bind(this.startConnections, this);
this.start = __bind(this.start, this);
this.initDevices = __bind(this.initDevices, this);
this.initConnections = __bind(this.initConnections, this);
this.robot = this; this.robot = this;
this.name = opts.name || this.constructor.randomName(); this.name = opts.name || this.constructor.randomName();
this.master = opts.master; this.master = opts.master;
this.connections = {}; this.connections = {};
this.devices = {}; this.devices = {};
this.adaptors = {}; this.adaptors = {};
this.drivers = {}; this.drivers = {};
this.commands = []; this.commands = [];
this.running = false; this.running = false;
this.registerAdaptor("./test/loopback", "loopback"); this.registerAdaptor("./test/loopback", "loopback");
this.registerAdaptor("./test/test-adaptor", "test"); this.registerAdaptor("./test/test-adaptor", "test");
this.registerDriver("./test/ping", "ping"); this.registerDriver("./test/ping", "ping");
this.registerDriver("./test/test-driver", "test"); this.registerDriver("./test/test-driver", "test");
this.initConnections(opts.connection || opts.connections); this.initConnections(opts.connection || opts.connections);
this.initDevices(opts.device || opts.devices); this.initDevices(opts.device || opts.devices);
this.work = opts.work || function() {
return Logger.info("No work yet"); this.work = opts.work || function() { Logger.info("No work yet"); };
};
for (n in opts) { for (var n in opts) {
func = opts[n]; var func = opts[n],
reserved = ['connection', 'connections', 'device', 'devices', 'work']; reserved = ['connection', 'connections', 'device', 'devices', 'work'];
if (__indexOf.call(reserved, n) < 0) {
this.robot[n] = func; if (reserved.indexOf(n) < 0) { this.robot[n] = func; }
}
} }
} }