diff --git a/examples/sphero.coffee b/examples/sphero.coffee index eba3f3c..cc41841 100644 --- a/examples/sphero.coffee +++ b/examples/sphero.coffee @@ -2,9 +2,13 @@ Cylon = require('..') Cylon.robot connection: - name: 'Sphero', adaptor: 'sphero', port: '/dev/Sphero-RPB' + name: 'sphero', adaptor: 'sphero', port: '/dev/rfcomm0' - work: -> - every 2.seconds(), -> Logger.info "Required cylon-sphero adaptor!" + device: + name: 'sphero', driver: 'sphero' + + work: (me) -> + #every 1.second(), -> Logger.info 'hi' + every 1.second(), -> me.sphero.roll(60, Math.floor(Math.random() * 360), 1) .start() diff --git a/src/adaptor.coffee b/src/adaptor.coffee index 537bcee..f1a2923 100644 --- a/src/adaptor.coffee +++ b/src/adaptor.coffee @@ -12,5 +12,5 @@ module.exports = class Adaptor constructor: (opts) -> @name = opts.name - addProxy: (object, method) -> - this[method] = (args...) -> object[method](args...) + commands: -> + ['smile', 'laugh', 'help'] diff --git a/src/connection.coffee b/src/connection.coffee index 73ff401..67911ab 100644 --- a/src/connection.coffee +++ b/src/connection.coffee @@ -18,6 +18,7 @@ module.exports = class Connection @robot = opts.robot @name = opts.name @adaptor = @requireAdaptor(opts.adaptor) # or 'loopback') + @addCommands(@adaptor) @port = new Port(opts.port) connect: -> @@ -31,3 +32,9 @@ module.exports = class Connection requireAdaptor: (adaptorName) -> Logger.info "dynamic load adaptor" @robot.requireAdaptor(adaptorName, @self) + + addCommands: (object) -> + @addProxy(object, method) for method in object.commands() + + addProxy: (object, method) -> + this[method] = (args...) -> object[method](args...) diff --git a/test/src/specs/adaptor.spec.coffee b/test/src/specs/adaptor.spec.coffee index f32eb81..81b4309 100644 --- a/test/src/specs/adaptor.spec.coffee +++ b/test/src/specs/adaptor.spec.coffee @@ -11,15 +11,3 @@ describe "Adaptor", -> it "should be able to connect" it "should be able to disconnect" - - it 'can alias methods with addProxy()', -> - proxyObject = { toString: -> "[object ProxyObject]" } - adaptor.addProxy(proxyObject, 'toString') - assert typeof adaptor.toString is 'function' - adaptor.toString().should.be.equal "[object ProxyObject]" - - it 'can alias methods with arguments with addProxy()', -> - proxyObject = { returnString: (string) -> string } - adaptor.addProxy(proxyObject, 'returnString') - assert typeof adaptor.returnString is 'function' - adaptor.returnString("testString").should.be.equal "testString" diff --git a/test/src/specs/connection.spec.coffee b/test/src/specs/connection.spec.coffee index 75f2d79..57c242b 100644 --- a/test/src/specs/connection.spec.coffee +++ b/test/src/specs/connection.spec.coffee @@ -1,10 +1,13 @@ 'use strict'; Connection = source("connection") +Adaptor = source("adaptor") Robot = source("robot") describe "Connection", -> robot = new Robot(name: 'me') + adaptor = new Adaptor(name: 'loopback') + requireAdaptor = sinon.stub(robot, 'requireAdaptor').returns(adaptor) connection = new Connection(name: "connective", adaptor: "loopback", robot: robot) it "should belong to a robot", -> @@ -19,3 +22,15 @@ describe "Connection", -> it "should be able to require an external adaptor module" it "should be able to connect" it "should be able to disconnect" + + it 'can alias methods with addProxy()', -> + proxyObject = { toString: -> "[object ProxyObject]" } + connection.addProxy(proxyObject, 'toString') + assert typeof connection.toString is 'function' + connection.toString().should.be.equal "[object ProxyObject]" + + it 'can alias methods with arguments with addProxy()', -> + proxyObject = { returnString: (string) -> string } + connection.addProxy(proxyObject, 'returnString') + assert typeof connection.returnString is 'function' + connection.returnString("testString").should.be.equal "testString"