From 372dde00783f4db0b5168dc2fcf71fbd7a8a6bdc Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sat, 23 Nov 2013 17:13:20 -0800 Subject: [PATCH 1/2] Refactoring adaptor and driver initialization to simplify, and fix init overwrite error --- dist/connection.js | 6 +- dist/device.js | 6 +- dist/robot.js | 69 ++++++++++------------ src/connection.coffee | 6 +- src/device.coffee | 10 ++-- src/robot.coffee | 83 ++++++++++++--------------- test/dist/specs/connection.spec.js | 4 +- test/dist/specs/device.spec.js | 8 +-- test/src/specs/connection.spec.coffee | 2 +- test/src/specs/device.spec.coffee | 6 +- 10 files changed, 92 insertions(+), 108 deletions(-) diff --git a/dist/connection.js b/dist/connection.js index 285eff7..931ca8a 100644 --- a/dist/connection.js +++ b/dist/connection.js @@ -38,7 +38,7 @@ this.robot = opts.robot; this.name = opts.name; this.connection_id = opts.id; - this.adaptor = this.requireAdaptor(opts); + this.adaptor = this.initAdaptor(opts); this.port = new Cylon.Port(opts.port); proxyFunctionsToObject(this.adaptor.commands(), this.adaptor, this.self); } @@ -72,9 +72,9 @@ return this.adaptor.disconnect(); }; - Connection.prototype.requireAdaptor = function(opts) { + Connection.prototype.initAdaptor = function(opts) { Logger.debug("Loading adaptor '" + opts.adaptor + "'"); - return this.robot.requireAdaptor(opts.adaptor, this.self, opts); + return this.robot.initAdaptor(opts.adaptor, this.self, opts); }; return Connection; diff --git a/dist/device.js b/dist/device.js index 947e500..51a15d8 100644 --- a/dist/device.js +++ b/dist/device.js @@ -35,7 +35,7 @@ this.name = opts.name; this.pin = opts.pin; this.connection = this.determineConnection(opts.connection) || this.defaultConnection(); - this.driver = this.requireDriver(opts); + this.driver = this.initDriver(opts); proxyFunctionsToObject(this.driver.commands(), this.driver, this.self); } @@ -81,12 +81,12 @@ return first; }; - Device.prototype.requireDriver = function(opts) { + Device.prototype.initDriver = function(opts) { if (opts == null) { opts = {}; } Logger.debug("Loading driver '" + opts.driver + "'"); - return this.robot.requireDriver(opts.driver, this.self, opts); + return this.robot.initDriver(opts.driver, this.self, opts); }; return Device; diff --git a/dist/robot.js b/dist/robot.js index 39bf10c..5cd1d10 100644 --- a/dist/robot.js +++ b/dist/robot.js @@ -39,6 +39,9 @@ opts = {}; } 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); @@ -182,64 +185,54 @@ return _results; }; - Robot.prototype.requireAdaptor = function(adaptorName, connection, opts) { + Robot.prototype.initAdaptor = function(adaptorName, connection, opts) { if (opts == null) { opts = {}; } - if (this.robot.adaptors[adaptorName] != null) { - if (typeof this.robot.adaptors[adaptorName] === 'string') { - this.robot.adaptors[adaptorName] = require(this.robot.adaptors[adaptorName]).adaptor({ - name: adaptorName, - connection: connection, - extraParams: opts - }); - } - } else { - require("cylon-" + adaptorName).register(this); - this.robot.adaptors[adaptorName] = require("cylon-" + adaptorName).adaptor({ - name: adaptorName, - connection: connection, - extraParams: opts - }); + return this.robot.requireAdaptor(adaptorName).adaptor({ + name: adaptorName, + connection: connection, + extraParams: opts + }); + }; + + Robot.prototype.requireAdaptor = function(adaptorName) { + if (this.robot.adaptors[adaptorName] == null) { + this.robot.registerAdaptor("cylon-" + adaptorName, adaptorName); + this.robot.adaptors[adaptorName].register(this); } return this.robot.adaptors[adaptorName]; }; Robot.prototype.registerAdaptor = function(moduleName, adaptorName) { - if (this.adaptors[adaptorName] != null) { - return; + if (this.adaptors[adaptorName] == null) { + return this.adaptors[adaptorName] = require(moduleName); } - return this.adaptors[adaptorName] = moduleName; }; - Robot.prototype.requireDriver = function(driverName, device, opts) { + Robot.prototype.initDriver = function(driverName, device, opts) { if (opts == null) { opts = {}; } - if (this.robot.drivers[driverName] != null) { - if (typeof this.robot.drivers[driverName] === 'string') { - this.robot.drivers[driverName] = require(this.robot.drivers[driverName]).driver({ - name: driverName, - device: device, - extraParams: opts - }); - } - } else { - require("cylon-" + driverName).register(this); - this.robot.drivers[driverName] = require("cylon-" + driverName).driver({ - name: driverName, - device: device, - extraParams: opts - }); + return this.robot.requireDriver(driverName).driver({ + name: driverName, + device: device, + extraParams: opts + }); + }; + + Robot.prototype.requireDriver = function(driverName) { + if (this.robot.drivers[driverName] == null) { + this.robot.registerDriver("cylon-" + driverName, driverName); + this.robot.drivers[driverName].register(this); } return this.robot.drivers[driverName]; }; Robot.prototype.registerDriver = function(moduleName, driverName) { - if (this.drivers[driverName] != null) { - return; + if (this.drivers[driverName] == null) { + return this.drivers[driverName] = require(moduleName); } - return this.drivers[driverName] = moduleName; }; return Robot; diff --git a/src/connection.coffee b/src/connection.coffee index 6d8e5c2..49e3c4f 100644 --- a/src/connection.coffee +++ b/src/connection.coffee @@ -35,7 +35,7 @@ namespace 'Cylon', -> @robot = opts.robot @name = opts.name @connection_id = opts.id - @adaptor = @requireAdaptor opts + @adaptor = @initAdaptor opts @port = new Cylon.Port opts.port proxyFunctionsToObject @adaptor.commands(), @adaptor, @self @@ -75,8 +75,8 @@ namespace 'Cylon', -> # adaptorName - module name of adaptor to require # # Returns the set-up adaptor - requireAdaptor: (opts) -> + initAdaptor: (opts) -> Logger.debug "Loading adaptor '#{opts.adaptor}'" - @robot.requireAdaptor(opts.adaptor, @self, opts) + @robot.initAdaptor(opts.adaptor, @self, opts) module.exports = Cylon.Connection diff --git a/src/device.coffee b/src/device.coffee index ff5d482..6acbf01 100644 --- a/src/device.coffee +++ b/src/device.coffee @@ -34,7 +34,7 @@ namespace 'Cylon', -> @name = opts.name @pin = opts.pin @connection = @determineConnection(opts.connection) or @defaultConnection() - @driver = @requireDriver(opts) + @driver = @initDriver(opts) proxyFunctionsToObject @driver.commands(), @driver, @self # Public: Starts the device driver @@ -86,12 +86,12 @@ namespace 'Cylon', -> # Public: sets up driver with @robot # - # opts - object containing options when requiring driver - # driver - name of the module to require() + # opts - object containing options when initializing driver + # driver - name of the driver to intt() # # Returns the set-up driver - requireDriver: (opts = {}) -> + initDriver: (opts = {}) -> Logger.debug "Loading driver '#{ opts.driver }'" - @robot.requireDriver(opts.driver, @self, opts) + @robot.initDriver(opts.driver, @self, opts) module.exports = Cylon.Device diff --git a/src/robot.coffee b/src/robot.coffee index 1b4fb8e..8237dce 100644 --- a/src/robot.coffee +++ b/src/robot.coffee @@ -162,31 +162,28 @@ namespace 'Cylon', -> for n, connection of @connections connection.disconnect() + # Public: Initialize an adaptor and adds it to @robot.adaptors + # + # adaptorName - module name of adaptor to require + # connection - the Connection that requested the adaptor be required + # + # Returns the adaptor + initAdaptor: (adaptorName, connection, opts = {}) -> + @robot.requireAdaptor(adaptorName).adaptor + name: adaptorName, + connection: connection, + extraParams: opts + # Public: Requires a hardware adaptor and adds it to @robot.adaptors # # adaptorName - module name of adaptor to require # connection - the Connection that requested the adaptor be required # # Returns the set-up adaptor - requireAdaptor: (adaptorName, connection, opts = {}) -> - if @robot.adaptors[adaptorName]? - if typeof @robot.adaptors[adaptorName] is 'string' - @robot.adaptors[adaptorName] = require( - @robot.adaptors[adaptorName] - ).adaptor( - name: adaptorName, - connection: connection, - extraParams: opts - ) - else - require("cylon-#{adaptorName}").register(this) - @robot.adaptors[adaptorName] = require( - "cylon-#{adaptorName}" - ).adaptor( - name: adaptorName, - connection: connection, - extraParams: opts - ) + requireAdaptor: (adaptorName) => + unless @robot.adaptors[adaptorName]? + @robot.registerAdaptor "cylon-#{adaptorName}", adaptorName + @robot.adaptors[adaptorName].register this return @robot.adaptors[adaptorName] @@ -196,35 +193,30 @@ namespace 'Cylon', -> # adaptorName - name of the adaptor to register the moduleName under # # Returns the registered module name - registerAdaptor: (moduleName, adaptorName) -> - return if @adaptors[adaptorName]? - @adaptors[adaptorName] = moduleName + registerAdaptor: (moduleName, adaptorName) => + @adaptors[adaptorName] = require(moduleName) unless @adaptors[adaptorName]? - # Public: Requires a hardware driver and adds it to @robot.drivers + # Public: Init a hardware driver + # + # driverName - driver name + # device - the Device that requested the driver be initialized + # + # Returns the new driver + initDriver: (driverName, device, opts = {}) -> + @robot.requireDriver(driverName).driver + name: driverName, + device: device, + extraParams: opts + + # Public: Requires module for a driver and adds it to @robot.drivers # # driverName - module name of driver to require - # connection - the Connection that requested the driver be required # - # Returns the set-up driver - requireDriver: (driverName, device, opts = {}) -> - if @robot.drivers[driverName]? - if typeof @robot.drivers[driverName] is 'string' - @robot.drivers[driverName] = require( - @robot.drivers[driverName] - ).driver( - name: driverName, - device: device, - extraParams: opts - ) - else - require("cylon-#{driverName}").register(this) - @robot.drivers[driverName] = require( - "cylon-#{driverName}" - ).driver( - name: driverName, - device: device, - extraParams: opts - ) + # Returns the module for driver + requireDriver: (driverName) => + unless @robot.drivers[driverName]? + @robot.registerDriver "cylon-#{driverName}", driverName + @robot.drivers[driverName].register this return @robot.drivers[driverName] @@ -235,7 +227,6 @@ namespace 'Cylon', -> # # Returns the registered module name registerDriver: (moduleName, driverName) => - return if @drivers[driverName]? - @drivers[driverName] = moduleName + @drivers[driverName] = require(moduleName) unless @drivers[driverName]? module.exports = Cylon.Robot diff --git a/test/dist/specs/connection.spec.js b/test/dist/specs/connection.spec.js index 313a040..fa7b6c0 100644 --- a/test/dist/specs/connection.spec.js +++ b/test/dist/specs/connection.spec.js @@ -7,14 +7,14 @@ source("robot"); describe("Connection", function() { - var adaptor, connection, requireAdaptor, robot; + var adaptor, connection, initAdaptor, robot; robot = new Cylon.Robot({ name: 'me' }); adaptor = new Cylon.Adaptor({ name: 'loopback' }); - requireAdaptor = sinon.stub(robot, 'requireAdaptor').returns(adaptor); + initAdaptor = sinon.stub(robot, 'initAdaptor').returns(adaptor); connection = new Cylon.Connection({ name: "connective", adaptor: "loopback", diff --git a/test/dist/specs/device.spec.js b/test/dist/specs/device.spec.js index 6f7d2e1..6342a4f 100644 --- a/test/dist/specs/device.spec.js +++ b/test/dist/specs/device.spec.js @@ -7,14 +7,14 @@ source("test/driver"); describe("Device", function() { - var device, driver, requireDriver, robot; + var device, driver, initDriver, robot; robot = new Cylon.Robot({ name: 'me' }); driver = new Cylon.Driver({ name: 'driving' }); - requireDriver = sinon.stub(robot, 'requireDriver').returns(driver); + initDriver = sinon.stub(robot, 'initDriver').returns(driver); device = new Cylon.Device({ name: "devisive", driver: 'driving', @@ -28,8 +28,8 @@ }); it("should use default connection if none specified"); it("should use connection if one is specified"); - return it("should require a driver", function() { - return requireDriver.should.be.called; + return it("should init a driver", function() { + return initDriver.should.be.called; }); }); diff --git a/test/src/specs/connection.spec.coffee b/test/src/specs/connection.spec.coffee index 3fb1694..94d26af 100644 --- a/test/src/specs/connection.spec.coffee +++ b/test/src/specs/connection.spec.coffee @@ -7,7 +7,7 @@ source "robot" describe "Connection", -> robot = new Cylon.Robot(name: 'me') adaptor = new Cylon.Adaptor(name: 'loopback') - requireAdaptor = sinon.stub(robot, 'requireAdaptor').returns(adaptor) + initAdaptor = sinon.stub(robot, 'initAdaptor').returns(adaptor) connection = new Cylon.Connection name: "connective" adaptor: "loopback" diff --git a/test/src/specs/device.spec.coffee b/test/src/specs/device.spec.coffee index 93c7815..bac2186 100644 --- a/test/src/specs/device.spec.coffee +++ b/test/src/specs/device.spec.coffee @@ -7,7 +7,7 @@ source "test/driver" describe "Device", -> robot = new Cylon.Robot(name: 'me') driver = new Cylon.Driver(name: 'driving') - requireDriver = sinon.stub(robot, 'requireDriver').returns(driver) + initDriver = sinon.stub(robot, 'initDriver').returns(driver) device = new Cylon.Device(name: "devisive", driver: 'driving', robot: robot) it "should belong to a robot", -> @@ -19,5 +19,5 @@ describe "Device", -> it "should use default connection if none specified" it "should use connection if one is specified" - it "should require a driver", -> - requireDriver.should.be.called + it "should init a driver", -> + initDriver.should.be.called From c90372001efce6be84a9ef2779bbe71281e04d94 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sat, 23 Nov 2013 21:18:09 -0800 Subject: [PATCH 2/2] Docs corrections --- src/connection.coffee | 7 ++++--- src/robot.coffee | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/connection.coffee b/src/connection.coffee index 49e3c4f..377b62e 100644 --- a/src/connection.coffee +++ b/src/connection.coffee @@ -50,7 +50,7 @@ namespace 'Cylon', -> connection_id: @connection_id } - # Public: Creates the adaptor connection + # Public: Connect the adaptor's connection # # callback - callback function to run when the adaptor is connected # @@ -61,7 +61,7 @@ namespace 'Cylon', -> Logger.info msg @adaptor.connect(callback) - # Public: Closes the adaptor connection + # Public: Disconnect the adaptor's connection # # Returns nothing disconnect: -> @@ -72,7 +72,8 @@ namespace 'Cylon', -> # Public: sets up adaptor with @robot # - # adaptorName - module name of adaptor to require + # opts - options for adaptor being initialized + # adaptor - name of the adaptor # # Returns the set-up adaptor initAdaptor: (opts) -> diff --git a/src/robot.coffee b/src/robot.coffee index 8237dce..8041b74 100644 --- a/src/robot.coffee +++ b/src/robot.coffee @@ -177,9 +177,8 @@ namespace 'Cylon', -> # Public: Requires a hardware adaptor and adds it to @robot.adaptors # # adaptorName - module name of adaptor to require - # connection - the Connection that requested the adaptor be required # - # Returns the set-up adaptor + # Returns the module for the adaptor requireAdaptor: (adaptorName) => unless @robot.adaptors[adaptorName]? @robot.registerAdaptor "cylon-#{adaptorName}", adaptorName @@ -200,6 +199,7 @@ namespace 'Cylon', -> # # driverName - driver name # device - the Device that requested the driver be initialized + # opts - object containing options when initializing driver # # Returns the new driver initDriver: (driverName, device, opts = {}) ->