From 1ee35f148722b3173e15d8c2e5648836d4527c5e Mon Sep 17 00:00:00 2001 From: Andrew Stewart Date: Fri, 28 Feb 2014 12:13:19 -0800 Subject: [PATCH] Clean up constructor --- lib/robot.js | 58 +++++++++++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/lib/robot.js b/lib/robot.js index 1da9e90..76d6aca 100644 --- a/lib/robot.js +++ b/lib/robot.js @@ -8,9 +8,6 @@ '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('./basestar'); require("./connection"); @@ -53,45 +50,54 @@ namespace("Cylon", function() { // every 1.second(), -> // me.sphero.roll 60, Math.floor(Math.random() * 360// 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.name = opts.name || this.constructor.randomName(); this.master = opts.master; + 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 || function() { - return Logger.info("No work yet"); - }; - for (n in opts) { - func = opts[n]; - reserved = ['connection', 'connections', 'device', 'devices', 'work']; - if (__indexOf.call(reserved, n) < 0) { - this.robot[n] = func; - } + + this.work = opts.work || function() { Logger.info("No work yet"); }; + + for (var n in opts) { + var func = opts[n], + reserved = ['connection', 'connections', 'device', 'devices', 'work']; + + if (reserved.indexOf(n) < 0) { this.robot[n] = func; } } }