WIP on dynamic loading of adaptor and driver modules
This commit is contained in:
parent
68b0c1dc09
commit
8252d38a5e
|
@ -1,6 +1,8 @@
|
||||||
var Cylon = require('..');
|
var Cylon = require('..');
|
||||||
|
|
||||||
Cylon.robot({
|
Cylon.robot({
|
||||||
|
connection: { name: 'looped', adaptor: 'loopback'},
|
||||||
|
|
||||||
work: function() {
|
work: function() {
|
||||||
every((1).second(), function() { Logger.info("Hello, human!"); });
|
every((1).second(), function() { Logger.info("Hello, human!"); });
|
||||||
after((10).seconds(), function() { Logger.info("Impressive."); });
|
after((10).seconds(), function() { Logger.info("Impressive."); });
|
||||||
|
|
|
@ -14,11 +14,18 @@ module.exports = class Connection
|
||||||
constructor: (opts = {}) ->
|
constructor: (opts = {}) ->
|
||||||
@robot = opts.robot
|
@robot = opts.robot
|
||||||
@name = opts.name
|
@name = opts.name
|
||||||
@adaptor = opts.adaptor
|
@adaptor = @requireAdaptor(opts.adaptor) # or 'loopback')
|
||||||
@port = new Port(opts.port)
|
@port = new Port(opts.port)
|
||||||
|
|
||||||
connect: ->
|
connect: ->
|
||||||
Logger.info "Connecting to '#{@name}' on port '#{@port.toString()}'..."
|
Logger.info "Connecting to '#{@name}' on port '#{@port.toString()}'..."
|
||||||
|
@adaptor.connect
|
||||||
|
|
||||||
disconnect: ->
|
disconnect: ->
|
||||||
Logger.info "Disconnecting from '#{@name}' on port '#{@port.toString()}'..."
|
Logger.info "Disconnecting from '#{@name}' on port '#{@port.toString()}'..."
|
||||||
|
@adaptor.disconnect
|
||||||
|
|
||||||
|
requireAdaptor: (adaptorName) ->
|
||||||
|
self = this
|
||||||
|
console.log "dynamic load adaptor"
|
||||||
|
@robot.requireAdaptor(adaptorName, self)
|
||||||
|
|
|
@ -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}'..."
|
|
@ -20,30 +20,32 @@ module.exports = class Robot
|
||||||
@connections = {}
|
@connections = {}
|
||||||
@devices = {}
|
@devices = {}
|
||||||
|
|
||||||
initConnections(opts.connection or opts.connections)
|
@registerAdaptor "./loopback", "loopback"
|
||||||
initDevices(opts.device or opts.devices)
|
|
||||||
|
@initConnections(opts.connection or opts.connections)
|
||||||
|
@initDevices(opts.device or opts.devices)
|
||||||
@work = opts.work or -> (Logger.info "No work yet")
|
@work = opts.work or -> (Logger.info "No work yet")
|
||||||
|
|
||||||
@randomName: ->
|
@randomName: ->
|
||||||
"Robot #{ Math.floor(Math.random() * 100000) }"
|
"Robot #{ Math.floor(Math.random() * 100000) }"
|
||||||
|
|
||||||
initConnections = (connections) ->
|
initConnections: (connections) ->
|
||||||
Logger.info "Initializing connections..."
|
Logger.info "Initializing connections..."
|
||||||
return unless connections?
|
return unless connections?
|
||||||
connections = [].concat connections
|
connections = [].concat connections
|
||||||
for connection in connections
|
for connection in connections
|
||||||
Logger.info "Initializing connection '#{ connection.name }'..."
|
Logger.info "Initializing connection '#{ connection.name }'..."
|
||||||
connection['robot'] = self
|
connection['robot'] = self
|
||||||
self.connections[connection.name] = new Connection(connection)
|
@connections[connection.name] = new Connection(connection)
|
||||||
|
|
||||||
initDevices = (devices) ->
|
initDevices: (devices) ->
|
||||||
Logger.info "Initializing devices..."
|
Logger.info "Initializing devices..."
|
||||||
return unless devices?
|
return unless devices?
|
||||||
devices = [].concat devices
|
devices = [].concat devices
|
||||||
for device in devices
|
for device in devices
|
||||||
Logger.info "Initializing device '#{ device.name }'..."
|
Logger.info "Initializing device '#{ device.name }'..."
|
||||||
device['robot'] = self
|
device['robot'] = self
|
||||||
self.devices[device.name] = new Device(device)
|
@devices[device.name] = new Device(device)
|
||||||
|
|
||||||
start: ->
|
start: ->
|
||||||
@startConnections()
|
@startConnections()
|
||||||
|
|
|
@ -1,18 +1,20 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
Connection = source("connection")
|
Connection = source("connection")
|
||||||
|
Robot = source("robot")
|
||||||
|
|
||||||
describe "Connection", ->
|
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", ->
|
it "should belong to a robot", ->
|
||||||
connection.robot.should.be.equal 'me'
|
connection.robot.name.should.be.equal 'me'
|
||||||
|
|
||||||
it "should have a name", ->
|
it "should have a name", ->
|
||||||
connection.name.should.be.equal 'connective'
|
connection.name.should.be.equal 'connective'
|
||||||
|
|
||||||
it "should have an adaptor", ->
|
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 require an external adaptor module"
|
||||||
it "should be able to connect"
|
it "should be able to connect"
|
||||||
|
|
Loading…
Reference in New Issue