Can proxy commands from connection to associated adaptor

This commit is contained in:
deadprogram 2013-10-23 23:18:19 -07:00
parent b8371232d1
commit a6d29ecbdd
5 changed files with 31 additions and 17 deletions

View File

@ -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()

View File

@ -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']

View File

@ -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...)

View File

@ -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"

View File

@ -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"