Much along the way to adaptor/driver registration and loading

This commit is contained in:
deadprogram 2013-10-22 23:23:52 -07:00
parent 42cd14cf14
commit 132282d4c2
3 changed files with 31 additions and 4 deletions

11
examples/hello.js Normal file
View File

@ -0,0 +1,11 @@
var cylon = require('..');
// Initialize the robot
var robot = cylon.robot({
work: function() {
every((1).second(), function() { Logger.info("hello, human!"); });
}
});
// start working
robot.start();

View File

@ -13,8 +13,6 @@ Device = require("./device")
module.exports = class Robot module.exports = class Robot
self = this self = this
@connections = {}
@devices = {}
constructor: (opts = {}) -> constructor: (opts = {}) ->
@name = opts.name or @constructor.randomName() @name = opts.name or @constructor.randomName()
@ -66,5 +64,20 @@ module.exports = class Robot
device.start() device.start()
self[device.name] = device self[device.name] = device
requireAdaptor: (adaptorName, connection) ->
require("cylon-#{@adaptors[adaptorName]}").register(self) unless @adaptors[adaptorName]?
require(@adaptors[adaptorName]).adaptor(adaptorName)(connection: connection)
registerAdaptor: (moduleName, adaptorName) ->
@adaptors ?= {}
return if @adaptors[adaptorName]?
@adaptors[adaptorName] = moduleName
requireDriver: (driverName, device) -> requireDriver: (driverName, device) ->
console.log "load the driver" require("cylon-#{@drivers[driverName]}").register(self) unless @drivers[driverName]?
require(@drivers[driverName]).driver(driverName)(device: device)
registerDriver: (moduleName, driverName) ->
@drivers ?= {}
return if @drivers[driverName]?
@drivers[driverName] = moduleName

View File

@ -5,6 +5,7 @@ Robot = source("robot")
describe "Device", -> describe "Device", ->
robot = new Robot(name: 'me') robot = new Robot(name: 'me')
requireDriver = sinon.stub(robot, 'requireDriver')
device = new Device(name: "devisive", driver: 'driving', robot: robot) device = new Device(name: "devisive", driver: 'driving', robot: robot)
it "should belong to a robot", -> it "should belong to a robot", ->
@ -15,4 +16,6 @@ describe "Device", ->
it "should use default connection if none specified" it "should use default connection if none specified"
it "should use connection if one is specified" it "should use connection if one is specified"
it "should have an driver"
it "should require a driver", ->
requireDriver.should.be.called