From 8252d38a5e4a061ccd126bdc4b8bf8fd5631bd88 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Wed, 23 Oct 2013 08:38:00 -0700 Subject: [PATCH] WIP on dynamic loading of adaptor and driver modules --- examples/hello.js | 2 ++ src/connection.coffee | 9 ++++++++- src/loopback.coffee | 22 ++++++++++++++++++++++ src/robot.coffee | 14 ++++++++------ test/src/specs/connection.spec.coffee | 8 +++++--- 5 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 src/loopback.coffee diff --git a/examples/hello.js b/examples/hello.js index f4951ec..69c4c55 100644 --- a/examples/hello.js +++ b/examples/hello.js @@ -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."); }); diff --git a/src/connection.coffee b/src/connection.coffee index ac9fb3c..b8e3d2f 100644 --- a/src/connection.coffee +++ b/src/connection.coffee @@ -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) diff --git a/src/loopback.coffee b/src/loopback.coffee new file mode 100644 index 0000000..b22ab8d --- /dev/null +++ b/src/loopback.coffee @@ -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}'..." diff --git a/src/robot.coffee b/src/robot.coffee index 1d5cf88..038252c 100644 --- a/src/robot.coffee +++ b/src/robot.coffee @@ -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() diff --git a/test/src/specs/connection.spec.coffee b/test/src/specs/connection.spec.coffee index ac7e773..75f2d79 100644 --- a/test/src/specs/connection.spec.coffee +++ b/test/src/specs/connection.spec.coffee @@ -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"