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

1
dist/driver.js vendored
View File

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

4
dist/robot.js vendored
View File

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

View File

@ -29,24 +29,15 @@
}; };
namespace('Cylon.Adaptors', function() { namespace('Cylon.Adaptors', function() {
var _ref;
return this.Loopback = (function(_super) { return this.Loopback = (function(_super) {
__extends(Loopback, _super); __extends(Loopback, _super);
function Loopback(opts) { function Loopback() {
this.self = this; _ref = Loopback.__super__.constructor.apply(this, arguments);
this.name = opts.name; 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() { Loopback.prototype.commands = function() {
return ['ping']; return ['ping'];
}; };

View File

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

View File

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

View File

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

View File

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

View File

@ -16,17 +16,5 @@ module.exports =
namespace 'Cylon.Adaptors', -> namespace 'Cylon.Adaptors', ->
class @Loopback extends @Adaptor 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: -> commands: ->
['ping'] ['ping']

View File

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

View File

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

View File

@ -6,7 +6,7 @@
source("driver"); source("driver");
source("ping"); source("test/ping");
describe("Device", function() { describe("Device", function() {
var device, driver, initDriver, robot; var device, driver, initDriver, robot;
@ -22,17 +22,29 @@
driver: 'driving', driver: 'driving',
robot: robot robot: robot
}); });
it("should belong to a robot", function() { it("belongs to a robot", function() {
return device.robot.name.should.be.equal('me'); 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'); return device.name.should.be.equal('devisive');
}); });
it("should use default connection if none specified"); it("can init a driver", function() {
it("should use connection if one is specified");
return it("should init a driver", function() {
return initDriver.should.be.called; 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); }).call(this);

View File

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

View File

@ -3,7 +3,7 @@
source "device" source "device"
source "robot" source "robot"
source "driver" source "driver"
source "ping" source "test/ping"
describe "Device", -> describe "Device", ->
robot = new Cylon.Robot(name: 'me') robot = new Cylon.Robot(name: 'me')
@ -11,14 +11,24 @@ describe "Device", ->
initDriver = sinon.stub(robot, 'initDriver').returns(driver) initDriver = sinon.stub(robot, 'initDriver').returns(driver)
device = new Cylon.Device(name: "devisive", driver: 'driving', robot: robot) 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' device.robot.name.should.be.equal 'me'
it "should have a name", -> it "has a name", ->
device.name.should.be.equal 'devisive' 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 default connection if none specified"
it "should use connection if one is specified" it "should use connection if one is specified"
it "should init a driver", ->
initDriver.should.be.called