Refactoring Adaptor and Driver into proper base classes
This commit is contained in:
parent
1833c304cd
commit
0e874c52d5
|
@ -13,14 +13,23 @@
|
|||
|
||||
namespace = require('node-namespace');
|
||||
|
||||
namespace('Cylon', function() {
|
||||
namespace('Cylon.Adaptors', function() {
|
||||
return this.Adaptor = (function() {
|
||||
function Adaptor(opts) {
|
||||
this.name = opts.name;
|
||||
}
|
||||
|
||||
Adaptor.prototype.commands = function() {
|
||||
return ['smile', 'laugh', 'help'];
|
||||
return [];
|
||||
};
|
||||
|
||||
Adaptor.prototype.connect = function(callback) {
|
||||
Logger.info("Connecting to adaptor '" + this.name + "'...");
|
||||
return callback(null);
|
||||
};
|
||||
|
||||
Adaptor.prototype.disconnect = function() {
|
||||
return Logger.info("Disconnecting from adaptor '" + this.name + "'...");
|
||||
};
|
||||
|
||||
return Adaptor;
|
||||
|
@ -28,6 +37,6 @@
|
|||
})();
|
||||
});
|
||||
|
||||
module.exports = Cylon.Adaptor;
|
||||
module.exports = Cylon.Adaptors.Adaptor;
|
||||
|
||||
}).call(this);
|
|
@ -18,6 +18,8 @@
|
|||
|
||||
require("./port");
|
||||
|
||||
require("./adaptor");
|
||||
|
||||
namespace = require('node-namespace');
|
||||
|
||||
EventEmitter = require('events').EventEmitter;
|
||||
|
|
|
@ -16,6 +16,8 @@
|
|||
|
||||
require('./cylon');
|
||||
|
||||
require('./driver');
|
||||
|
||||
namespace = require('node-namespace');
|
||||
|
||||
EventEmitter = require('events').EventEmitter;
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
|
||||
namespace = require('node-namespace');
|
||||
|
||||
namespace('Cylon', function() {
|
||||
namespace('Cylon.Drivers', function() {
|
||||
return this.Driver = (function() {
|
||||
function Driver(opts) {
|
||||
this.self = this;
|
||||
|
@ -21,11 +21,15 @@
|
|||
}
|
||||
|
||||
Driver.prototype.start = function() {
|
||||
return Logger.info("started");
|
||||
return Logger.info("Driver " + this.name + " started");
|
||||
};
|
||||
|
||||
Driver.prototype.stop = function() {
|
||||
return Logger.info("Driver " + this.name + " stopped");
|
||||
};
|
||||
|
||||
Driver.prototype.commands = function() {
|
||||
return ['smile', 'laugh', 'help'];
|
||||
return [];
|
||||
};
|
||||
|
||||
return Driver;
|
||||
|
@ -33,6 +37,6 @@
|
|||
})();
|
||||
});
|
||||
|
||||
module.exports = Cylon.Driver;
|
||||
module.exports = Cylon.Drivers.Driver;
|
||||
|
||||
}).call(this);
|
|
@ -9,8 +9,12 @@
|
|||
|
||||
(function() {
|
||||
'use strict';
|
||||
var Adaptor, Loopback,
|
||||
__slice = [].slice;
|
||||
var namespace,
|
||||
__slice = [].slice,
|
||||
__hasProp = {}.hasOwnProperty,
|
||||
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
|
||||
|
||||
namespace = require('node-namespace');
|
||||
|
||||
module.exports = {
|
||||
adaptor: function() {
|
||||
|
@ -20,12 +24,14 @@
|
|||
ctor.prototype = func.prototype;
|
||||
var child = new ctor, result = func.apply(child, args);
|
||||
return Object(result) === result ? result : child;
|
||||
})(Adaptor.Loopback, args, function(){});
|
||||
})(Cylon.Adaptors.Loopback, args, function(){});
|
||||
}
|
||||
};
|
||||
|
||||
Adaptor = {
|
||||
Loopback: Loopback = (function() {
|
||||
namespace('Cylon.Adaptors', function() {
|
||||
return this.Loopback = (function(_super) {
|
||||
__extends(Loopback, _super);
|
||||
|
||||
function Loopback(opts) {
|
||||
this.self = this;
|
||||
this.name = opts.name;
|
||||
|
@ -33,7 +39,8 @@
|
|||
|
||||
Loopback.prototype.connect = function(callback) {
|
||||
Logger.info("Connecting to adaptor '" + this.name + "'...");
|
||||
return callback(null);
|
||||
callback(null);
|
||||
return this.connection.emit('connect');
|
||||
};
|
||||
|
||||
Loopback.prototype.disconnect = function() {
|
||||
|
@ -46,7 +53,7 @@
|
|||
|
||||
return Loopback;
|
||||
|
||||
})()
|
||||
};
|
||||
})(this.Adaptor);
|
||||
});
|
||||
|
||||
}).call(this);
|
||||
|
|
|
@ -9,8 +9,12 @@
|
|||
|
||||
(function() {
|
||||
'use strict';
|
||||
var Driver, Ping,
|
||||
__slice = [].slice;
|
||||
var namespace,
|
||||
__slice = [].slice,
|
||||
__hasProp = {}.hasOwnProperty,
|
||||
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
|
||||
|
||||
namespace = require('node-namespace');
|
||||
|
||||
module.exports = {
|
||||
driver: function() {
|
||||
|
@ -20,12 +24,14 @@
|
|||
ctor.prototype = func.prototype;
|
||||
var child = new ctor, result = func.apply(child, args);
|
||||
return Object(result) === result ? result : child;
|
||||
})(Driver.Ping, args, function(){});
|
||||
})(Cylon.Drivers.Ping, args, function(){});
|
||||
}
|
||||
};
|
||||
|
||||
Driver = {
|
||||
Ping: Ping = (function() {
|
||||
namespace('Cylon.Drivers', function() {
|
||||
return this.Ping = (function(_super) {
|
||||
__extends(Ping, _super);
|
||||
|
||||
function Ping(opts) {
|
||||
this.self = this;
|
||||
this.name = opts.name;
|
||||
|
@ -46,7 +52,7 @@
|
|||
|
||||
return Ping;
|
||||
|
||||
})()
|
||||
};
|
||||
})(this.Driver);
|
||||
});
|
||||
|
||||
}).call(this);
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
###
|
||||
* adaptor
|
||||
* cylonjs.com
|
||||
*
|
||||
* Copyright (c) 2013 The Hybrid Group
|
||||
* Licensed under the Apache 2.0 license.
|
||||
###
|
||||
|
||||
'use strict'
|
||||
|
||||
namespace = require 'node-namespace'
|
||||
|
||||
namespace 'Cylon.Adaptors', ->
|
||||
class @Adaptor
|
||||
constructor: (opts) ->
|
||||
@name = opts.name
|
||||
|
||||
commands: ->
|
||||
[]
|
||||
|
||||
connect: (callback) ->
|
||||
Logger.info "Connecting to adaptor '#{@name}'..."
|
||||
(callback)(null)
|
||||
|
||||
disconnect: ->
|
||||
Logger.info "Disconnecting from adaptor '#{@name}'..."
|
||||
|
||||
module.exports = Cylon.Adaptors.Adaptor
|
|
@ -10,6 +10,7 @@
|
|||
|
||||
require "./robot"
|
||||
require "./port"
|
||||
require "./adaptor"
|
||||
|
||||
namespace = require 'node-namespace'
|
||||
EventEmitter = require('events').EventEmitter
|
||||
|
|
|
@ -9,6 +9,8 @@
|
|||
'use strict'
|
||||
|
||||
require './cylon'
|
||||
require './driver'
|
||||
|
||||
namespace = require 'node-namespace'
|
||||
EventEmitter = require('events').EventEmitter
|
||||
|
||||
|
|
|
@ -10,17 +10,19 @@
|
|||
|
||||
namespace = require 'node-namespace'
|
||||
|
||||
# A test class, used to create dummy Drivers
|
||||
namespace 'Cylon', ->
|
||||
namespace 'Cylon.Drivers', ->
|
||||
class @Driver
|
||||
constructor: (opts) ->
|
||||
@self = this
|
||||
@name = opts.name
|
||||
|
||||
start: ->
|
||||
Logger.info "started"
|
||||
Logger.info "Driver #{@name} started"
|
||||
|
||||
stop: ->
|
||||
Logger.info "Driver #{@name} stopped"
|
||||
|
||||
commands: ->
|
||||
['smile', 'laugh', 'help']
|
||||
[]
|
||||
|
||||
module.exports = Cylon.Driver
|
||||
module.exports = Cylon.Drivers.Driver
|
|
@ -8,12 +8,14 @@
|
|||
|
||||
'use strict'
|
||||
|
||||
namespace = require 'node-namespace'
|
||||
|
||||
module.exports =
|
||||
adaptor: (args...) ->
|
||||
new Adaptor.Loopback(args...)
|
||||
new Cylon.Adaptors.Loopback(args...)
|
||||
|
||||
Adaptor =
|
||||
Loopback: class Loopback
|
||||
namespace 'Cylon.Adaptors', ->
|
||||
class @Loopback extends @Adaptor
|
||||
constructor: (opts) ->
|
||||
@self = this
|
||||
@name = opts.name
|
||||
|
@ -21,6 +23,7 @@ Adaptor =
|
|||
connect: (callback) ->
|
||||
Logger.info "Connecting to adaptor '#{@name}'..."
|
||||
(callback)(null)
|
||||
@connection.emit 'connect'
|
||||
|
||||
disconnect: ->
|
||||
Logger.info "Disconnecting from adaptor '#{@name}'..."
|
||||
|
|
|
@ -8,12 +8,14 @@
|
|||
|
||||
'use strict'
|
||||
|
||||
namespace = require 'node-namespace'
|
||||
|
||||
module.exports =
|
||||
driver: (args...) ->
|
||||
new Driver.Ping(args...)
|
||||
new Cylon.Drivers.Ping(args...)
|
||||
|
||||
Driver =
|
||||
Ping: class Ping
|
||||
namespace 'Cylon.Drivers', ->
|
||||
class @Ping extends @Driver
|
||||
constructor: (opts) ->
|
||||
@self = this
|
||||
@name = opts.name
|
||||
|
|
|
@ -1,21 +0,0 @@
|
|||
###
|
||||
* adaptor
|
||||
* cylonjs.com
|
||||
*
|
||||
* Copyright (c) 2013 The Hybrid Group
|
||||
* Licensed under the Apache 2.0 license.
|
||||
###
|
||||
|
||||
'use strict'
|
||||
|
||||
namespace = require 'node-namespace'
|
||||
|
||||
namespace 'Cylon', ->
|
||||
class @Adaptor
|
||||
constructor: (opts) ->
|
||||
@name = opts.name
|
||||
|
||||
commands: ->
|
||||
['smile', 'laugh', 'help']
|
||||
|
||||
module.exports = Cylon.Adaptor
|
|
@ -2,17 +2,19 @@
|
|||
'use strict';
|
||||
source("connection");
|
||||
|
||||
source("test/adaptor");
|
||||
source("adaptor");
|
||||
|
||||
source("robot");
|
||||
|
||||
source("loopback");
|
||||
|
||||
describe("Connection", function() {
|
||||
var adaptor, connection, initAdaptor, robot;
|
||||
robot = new Cylon.Robot({
|
||||
name: 'me'
|
||||
});
|
||||
adaptor = new Cylon.Adaptor({
|
||||
name: 'loopback'
|
||||
adaptor = new Cylon.Adaptors.Loopback({
|
||||
name: 'loopy'
|
||||
});
|
||||
initAdaptor = sinon.stub(robot, 'initAdaptor').returns(adaptor);
|
||||
connection = new Cylon.Connection({
|
||||
|
@ -20,18 +22,30 @@
|
|||
adaptor: "loopback",
|
||||
robot: robot
|
||||
});
|
||||
it("should belong to a robot", function() {
|
||||
it("belongs to a robot", function() {
|
||||
return connection.robot.name.should.be.equal('me');
|
||||
});
|
||||
it("should have a name", function() {
|
||||
it("has a name", function() {
|
||||
return connection.name.should.be.equal('connective');
|
||||
});
|
||||
it("should have an adaptor", function() {
|
||||
return connection.adaptor.name.should.be.equal('loopback');
|
||||
it("has an adaptor", function() {
|
||||
return connection.adaptor.name.should.be.equal('loopy');
|
||||
});
|
||||
it("can init an external adaptor module", function() {
|
||||
return initAdaptor.should.be.called;
|
||||
});
|
||||
it("can connect to adaptor", function() {
|
||||
var adaptorConnect;
|
||||
adaptorConnect = sinon.stub(adaptor, 'connect').returns(true);
|
||||
connection.connect();
|
||||
return adaptorConnect.should.be.called;
|
||||
});
|
||||
return it("can disconnect from adaptor", function() {
|
||||
var adaptorDisconnect;
|
||||
adaptorDisconnect = sinon.stub(adaptor, 'disconnect').returns(true);
|
||||
connection.disconnect();
|
||||
return adaptorDisconnect.should.be.called;
|
||||
});
|
||||
it("should be able to require an external adaptor module");
|
||||
it("should be able to connect");
|
||||
return it("should be able to disconnect");
|
||||
});
|
||||
|
||||
}).call(this);
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
Device = source('device');
|
||||
|
||||
Driver = source('test/driver');
|
||||
Driver = source('driver');
|
||||
|
||||
Connection = source('connection');
|
||||
|
||||
|
|
|
@ -4,14 +4,16 @@
|
|||
|
||||
source("robot");
|
||||
|
||||
source("test/driver");
|
||||
source("driver");
|
||||
|
||||
source("ping");
|
||||
|
||||
describe("Device", function() {
|
||||
var device, driver, initDriver, robot;
|
||||
robot = new Cylon.Robot({
|
||||
name: 'me'
|
||||
});
|
||||
driver = new Cylon.Driver({
|
||||
driver = new Cylon.Drivers.Ping({
|
||||
name: 'driving'
|
||||
});
|
||||
initDriver = sinon.stub(robot, 'initDriver').returns(driver);
|
||||
|
|
|
@ -1,27 +1,37 @@
|
|||
'use strict';
|
||||
|
||||
source "connection"
|
||||
source "test/adaptor"
|
||||
source "adaptor"
|
||||
source "robot"
|
||||
source "loopback"
|
||||
|
||||
describe "Connection", ->
|
||||
robot = new Cylon.Robot(name: 'me')
|
||||
adaptor = new Cylon.Adaptor(name: 'loopback')
|
||||
adaptor = new Cylon.Adaptors.Loopback(name: 'loopy')
|
||||
initAdaptor = sinon.stub(robot, 'initAdaptor').returns(adaptor)
|
||||
connection = new Cylon.Connection
|
||||
name: "connective"
|
||||
adaptor: "loopback"
|
||||
robot: robot
|
||||
|
||||
it "should belong to a robot", ->
|
||||
it "belongs to a robot", ->
|
||||
connection.robot.name.should.be.equal 'me'
|
||||
|
||||
it "should have a name", ->
|
||||
it "has a name", ->
|
||||
connection.name.should.be.equal 'connective'
|
||||
|
||||
it "should have an adaptor", ->
|
||||
connection.adaptor.name.should.be.equal 'loopback'
|
||||
it "has an adaptor", ->
|
||||
connection.adaptor.name.should.be.equal 'loopy'
|
||||
|
||||
it "should be able to require an external adaptor module"
|
||||
it "should be able to connect"
|
||||
it "should be able to disconnect"
|
||||
it "can init an external adaptor module", ->
|
||||
initAdaptor.should.be.called
|
||||
|
||||
it "can connect to adaptor", ->
|
||||
adaptorConnect = sinon.stub(adaptor, 'connect').returns(true)
|
||||
connection.connect()
|
||||
adaptorConnect.should.be.called
|
||||
|
||||
it "can disconnect from adaptor", ->
|
||||
adaptorDisconnect = sinon.stub(adaptor, 'disconnect').returns(true)
|
||||
connection.disconnect()
|
||||
adaptorDisconnect.should.be.called
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
Cylon = source "cylon"
|
||||
Robot = source 'robot'
|
||||
Device = source 'device'
|
||||
Driver = source 'test/driver'
|
||||
Driver = source 'driver'
|
||||
Connection = source 'connection'
|
||||
|
||||
describe "Cylon", ->
|
||||
|
|
|
@ -2,11 +2,12 @@
|
|||
|
||||
source "device"
|
||||
source "robot"
|
||||
source "test/driver"
|
||||
source "driver"
|
||||
source "ping"
|
||||
|
||||
describe "Device", ->
|
||||
robot = new Cylon.Robot(name: 'me')
|
||||
driver = new Cylon.Driver(name: 'driving')
|
||||
driver = new Cylon.Drivers.Ping(name: 'driving')
|
||||
initDriver = sinon.stub(robot, 'initDriver').returns(driver)
|
||||
device = new Cylon.Device(name: "devisive", driver: 'driving', robot: robot)
|
||||
|
||||
|
|
Loading…
Reference in New Issue