WIP on dynamic loading of adaptor and driver modules

This commit is contained in:
deadprogram 2013-10-23 08:38:00 -07:00
parent 68b0c1dc09
commit 8252d38a5e
5 changed files with 45 additions and 10 deletions

View File

@ -1,6 +1,8 @@
var Cylon = require('..');
Cylon.robot({
connection: { name: 'looped', adaptor: 'loopback'},
work: function() {
every((1).second(), function() { Logger.info("Hello, human!"); });
after((10).seconds(), function() { Logger.info("Impressive."); });

View File

@ -14,11 +14,18 @@ module.exports = class Connection
constructor: (opts = {}) ->
@robot = opts.robot
@name = opts.name
@adaptor = opts.adaptor
@adaptor = @requireAdaptor(opts.adaptor) # or 'loopback')
@port = new Port(opts.port)
connect: ->
Logger.info "Connecting to '#{@name}' on port '#{@port.toString()}'..."
@adaptor.connect
disconnect: ->
Logger.info "Disconnecting from '#{@name}' on port '#{@port.toString()}'..."
@adaptor.disconnect
requireAdaptor: (adaptorName) ->
self = this
console.log "dynamic load adaptor"
@robot.requireAdaptor(adaptorName, self)

22
src/loopback.coffee Normal file
View File

@ -0,0 +1,22 @@
###
* adaptor
* cylonjs.com
*
* Copyright (c) 2013 The Hybrid Group
* Licensed under the Apache 2.0 license.
###
'use strict';
exports.adaptor = (opts = {}) ->
new Adaptor.Loopback(opts)
module.exports = class Adaptor.Loopback
constructor: (opts) ->
@name = opts.name
connect: ->
console.log "Connecting to adaptor '#{@name}'..."
disconnect: ->
console.log "Disconnecting from adaptor '#{@name}'..."

View File

@ -20,30 +20,32 @@ module.exports = class Robot
@connections = {}
@devices = {}
initConnections(opts.connection or opts.connections)
initDevices(opts.device or opts.devices)
@registerAdaptor "./loopback", "loopback"
@initConnections(opts.connection or opts.connections)
@initDevices(opts.device or opts.devices)
@work = opts.work or -> (Logger.info "No work yet")
@randomName: ->
"Robot #{ Math.floor(Math.random() * 100000) }"
initConnections = (connections) ->
initConnections: (connections) ->
Logger.info "Initializing connections..."
return unless connections?
connections = [].concat connections
for connection in connections
Logger.info "Initializing connection '#{ connection.name }'..."
connection['robot'] = self
self.connections[connection.name] = new Connection(connection)
@connections[connection.name] = new Connection(connection)
initDevices = (devices) ->
initDevices: (devices) ->
Logger.info "Initializing devices..."
return unless devices?
devices = [].concat devices
for device in devices
Logger.info "Initializing device '#{ device.name }'..."
device['robot'] = self
self.devices[device.name] = new Device(device)
@devices[device.name] = new Device(device)
start: ->
@startConnections()

View File

@ -1,18 +1,20 @@
'use strict';
Connection = source("connection")
Robot = source("robot")
describe "Connection", ->
connection = new Connection(name: "connective", adaptor: "adaptive", robot: 'me')
robot = new Robot(name: 'me')
connection = new Connection(name: "connective", adaptor: "loopback", robot: robot)
it "should belong to a robot", ->
connection.robot.should.be.equal 'me'
connection.robot.name.should.be.equal 'me'
it "should have a name", ->
connection.name.should.be.equal 'connective'
it "should have an adaptor", ->
connection.adaptor.should.be.equal 'adaptive'
connection.adaptor.name.should.be.equal 'loopback'
it "should be able to require an external adaptor module"
it "should be able to connect"