Remaining moves and refactoring for Adaptor/Driver base classes

This commit is contained in:
deadprogram 2013-12-14 12:12:18 -08:00
parent 0e874c52d5
commit 8f40826a34
14 changed files with 57 additions and 60 deletions

5
dist/adaptor.js vendored
View File

@ -16,7 +16,9 @@
namespace('Cylon.Adaptors', function() {
return this.Adaptor = (function() {
function Adaptor(opts) {
this.self = this;
this.name = opts.name;
this.connection = opts.connection;
}
Adaptor.prototype.commands = function() {
@ -25,7 +27,8 @@
Adaptor.prototype.connect = function(callback) {
Logger.info("Connecting to adaptor '" + this.name + "'...");
return callback(null);
callback(null);
return this.connection.emit('connect');
};
Adaptor.prototype.disconnect = function() {

1
dist/driver.js vendored
View File

@ -18,6 +18,7 @@
function Driver(opts) {
this.self = this;
this.name = opts.name;
this.device = opts.device;
}
Driver.prototype.start = function() {

4
dist/robot.js vendored
View File

@ -57,8 +57,8 @@
this.adaptors = {};
this.drivers = {};
this.commands = [];
this.registerAdaptor("./loopback", "loopback");
this.registerDriver("./ping", "ping");
this.registerAdaptor("./test/loopback", "loopback");
this.registerDriver("./test/ping", "ping");
this.initConnections(opts.connection || opts.connections);
this.initDevices(opts.device || opts.devices);
this.work = opts.work || function() {

View File

@ -29,24 +29,15 @@
};
namespace('Cylon.Adaptors', function() {
var _ref;
return this.Loopback = (function(_super) {
__extends(Loopback, _super);
function Loopback(opts) {
this.self = this;
this.name = opts.name;
function Loopback() {
_ref = Loopback.__super__.constructor.apply(this, arguments);
return _ref;
}
Loopback.prototype.connect = function(callback) {
Logger.info("Connecting to adaptor '" + this.name + "'...");
callback(null);
return this.connection.emit('connect');
};
Loopback.prototype.disconnect = function() {
return Logger.info("Disconnecting from adaptor '" + this.name + "'...");
};
Loopback.prototype.commands = function() {
return ['ping'];
};

View File

@ -29,23 +29,19 @@
};
namespace('Cylon.Drivers', function() {
var _ref;
return this.Ping = (function(_super) {
__extends(Ping, _super);
function Ping(opts) {
this.self = this;
this.name = opts.name;
function Ping() {
_ref = Ping.__super__.constructor.apply(this, arguments);
return _ref;
}
Ping.prototype.commands = function() {
return ['ping'];
};
Ping.prototype.start = function(callback) {
Logger.info("Starting driver '" + this.name + "'...");
return callback(null);
};
Ping.prototype.ping = function() {
return "pong";
};

View File

@ -13,7 +13,9 @@ namespace = require 'node-namespace'
namespace 'Cylon.Adaptors', ->
class @Adaptor
constructor: (opts) ->
@self = this
@name = opts.name
@connection = opts.connection
commands: ->
[]
@ -21,6 +23,7 @@ namespace 'Cylon.Adaptors', ->
connect: (callback) ->
Logger.info "Connecting to adaptor '#{@name}'..."
(callback)(null)
@connection.emit 'connect'
disconnect: ->
Logger.info "Disconnecting from adaptor '#{@name}'..."

View File

@ -15,6 +15,7 @@ namespace 'Cylon.Drivers', ->
constructor: (opts) ->
@self = this
@name = opts.name
@device = opts.device
start: ->
Logger.info "Driver #{@name} started"

View File

@ -57,8 +57,8 @@ namespace 'Cylon', ->
@drivers = {}
@commands = []
@registerAdaptor "./loopback", "loopback"
@registerDriver "./ping", "ping"
@registerAdaptor "./test/loopback", "loopback"
@registerDriver "./test/ping", "ping"
@initConnections(opts.connection or opts.connections)
@initDevices(opts.device or opts.devices)

View File

@ -16,17 +16,5 @@ module.exports =
namespace 'Cylon.Adaptors', ->
class @Loopback extends @Adaptor
constructor: (opts) ->
@self = this
@name = opts.name
connect: (callback) ->
Logger.info "Connecting to adaptor '#{@name}'..."
(callback)(null)
@connection.emit 'connect'
disconnect: ->
Logger.info "Disconnecting from adaptor '#{@name}'..."
commands: ->
['ping']

View File

@ -16,16 +16,8 @@ module.exports =
namespace 'Cylon.Drivers', ->
class @Ping extends @Driver
constructor: (opts) ->
@self = this
@name = opts.name
commands: ->
['ping']
start: (callback) ->
Logger.info "Starting driver '#{@name}'..."
(callback)(null)
ping: ->
"pong"

View File

@ -6,7 +6,7 @@
source("robot");
source("loopback");
source("test/loopback");
describe("Connection", function() {
var adaptor, connection, initAdaptor, robot;

View File

@ -6,7 +6,7 @@
source("driver");
source("ping");
source("test/ping");
describe("Device", function() {
var device, driver, initDriver, robot;
@ -22,17 +22,29 @@
driver: 'driving',
robot: robot
});
it("should belong to a robot", function() {
it("belongs to a robot", function() {
return device.robot.name.should.be.equal('me');
});
it("should have a name", function() {
it("has a name", function() {
return device.name.should.be.equal('devisive');
});
it("should use default connection if none specified");
it("should use connection if one is specified");
return it("should init a driver", function() {
it("can init a driver", function() {
return initDriver.should.be.called;
});
it("can start a driver", function() {
var driverStart;
driverStart = sinon.stub(driver, 'start').returns(true);
device.start();
return driverStart.should.be.called;
});
it("can stop a driver", function() {
var driverStop;
driverStop = sinon.stub(driver, 'stop').returns(true);
device.stop();
return driverStop.should.be.called;
});
it("should use default connection if none specified");
return it("should use connection if one is specified");
});
}).call(this);

View File

@ -3,7 +3,7 @@
source "connection"
source "adaptor"
source "robot"
source "loopback"
source "test/loopback"
describe "Connection", ->
robot = new Cylon.Robot(name: 'me')

View File

@ -3,7 +3,7 @@
source "device"
source "robot"
source "driver"
source "ping"
source "test/ping"
describe "Device", ->
robot = new Cylon.Robot(name: 'me')
@ -11,14 +11,24 @@ describe "Device", ->
initDriver = sinon.stub(robot, 'initDriver').returns(driver)
device = new Cylon.Device(name: "devisive", driver: 'driving', robot: robot)
it "should belong to a robot", ->
it "belongs to a robot", ->
device.robot.name.should.be.equal 'me'
it "should have a name", ->
it "has a name", ->
device.name.should.be.equal 'devisive'
it "can init a driver", ->
initDriver.should.be.called
it "can start a driver", ->
driverStart = sinon.stub(driver, 'start').returns(true)
device.start()
driverStart.should.be.called
it "can stop a driver", ->
driverStop = sinon.stub(driver, 'stop').returns(true)
device.stop()
driverStop.should.be.called
it "should use default connection if none specified"
it "should use connection if one is specified"
it "should init a driver", ->
initDriver.should.be.called