From 4e43483e9d6c3d2336a720aef38fc7394a61bdb6 Mon Sep 17 00:00:00 2001 From: Andrew Stewart Date: Mon, 9 Jun 2014 13:39:16 -0700 Subject: [PATCH] Slightly refactor constructor --- lib/robot.js | 46 +++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/lib/robot.js b/lib/robot.js index 80e4940..9d58cfc 100644 --- a/lib/robot.js +++ b/lib/robot.js @@ -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