Namespace Device class
This commit is contained in:
parent
ae666f67dd
commit
eda6f9db76
|
@ -9,79 +9,85 @@
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
'use strict';
|
'use strict';
|
||||||
var Device, EventEmitter,
|
var EventEmitter, namespace,
|
||||||
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
|
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
|
||||||
__hasProp = {}.hasOwnProperty,
|
__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; };
|
__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; };
|
||||||
|
|
||||||
require('./cylon');
|
require('./cylon');
|
||||||
|
|
||||||
|
namespace = require('node-namespace');
|
||||||
|
|
||||||
EventEmitter = require('events').EventEmitter;
|
EventEmitter = require('events').EventEmitter;
|
||||||
|
|
||||||
module.exports = Device = (function(_super) {
|
namespace('Cylon', function() {
|
||||||
__extends(Device, _super);
|
return this.Device = (function(_super) {
|
||||||
|
__extends(Device, _super);
|
||||||
|
|
||||||
function Device(opts) {
|
function Device(opts) {
|
||||||
if (opts == null) {
|
if (opts == null) {
|
||||||
opts = {};
|
opts = {};
|
||||||
|
}
|
||||||
|
this.start = __bind(this.start, this);
|
||||||
|
this.self = this;
|
||||||
|
this.robot = opts.robot;
|
||||||
|
this.name = opts.name;
|
||||||
|
this.pin = opts.pin;
|
||||||
|
this.connection = this.determineConnection(opts.connection) || this.defaultConnection();
|
||||||
|
this.driver = this.requireDriver(opts);
|
||||||
|
proxyFunctionsToObject(this.driver.commands(), this.driver, Device);
|
||||||
}
|
}
|
||||||
this.start = __bind(this.start, this);
|
|
||||||
this.self = this;
|
|
||||||
this.robot = opts.robot;
|
|
||||||
this.name = opts.name;
|
|
||||||
this.pin = opts.pin;
|
|
||||||
this.connection = this.determineConnection(opts.connection) || this.defaultConnection();
|
|
||||||
this.driver = this.requireDriver(opts);
|
|
||||||
proxyFunctionsToObject(this.driver.commands(), this.driver, Device);
|
|
||||||
}
|
|
||||||
|
|
||||||
Device.prototype.start = function(callback) {
|
Device.prototype.start = function(callback) {
|
||||||
var msg;
|
var msg;
|
||||||
msg = "Starting device '" + this.name + "'";
|
msg = "Starting device '" + this.name + "'";
|
||||||
if (this.pin != null) {
|
if (this.pin != null) {
|
||||||
msg += " on pin " + this.pin;
|
msg += " on pin " + this.pin;
|
||||||
}
|
}
|
||||||
Logger.info(msg);
|
Logger.info(msg);
|
||||||
return this.driver.start(callback);
|
return this.driver.start(callback);
|
||||||
};
|
|
||||||
|
|
||||||
Device.prototype.data = function() {
|
|
||||||
return {
|
|
||||||
name: this.name,
|
|
||||||
driver: this.driver.constructor.name || this.driver.name,
|
|
||||||
pin: this.pin != null ? this.pin.toString : null,
|
|
||||||
connection: this.connection.data(),
|
|
||||||
commands: this.driver.commands()
|
|
||||||
};
|
};
|
||||||
};
|
|
||||||
|
|
||||||
Device.prototype.determineConnection = function(c) {
|
Device.prototype.data = function() {
|
||||||
if (c) {
|
return {
|
||||||
return this.robot.connections[c];
|
name: this.name,
|
||||||
}
|
driver: this.driver.constructor.name || this.driver.name,
|
||||||
};
|
pin: this.pin != null ? this.pin.toString : null,
|
||||||
|
connection: this.connection.data(),
|
||||||
|
commands: this.driver.commands()
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
Device.prototype.defaultConnection = function() {
|
Device.prototype.determineConnection = function(c) {
|
||||||
var first, k, v, _ref;
|
if (c) {
|
||||||
first = 0;
|
return this.robot.connections[c];
|
||||||
_ref = this.robot.connections;
|
}
|
||||||
for (k in _ref) {
|
};
|
||||||
v = _ref[k];
|
|
||||||
first || (first = v);
|
|
||||||
}
|
|
||||||
return first;
|
|
||||||
};
|
|
||||||
|
|
||||||
Device.prototype.requireDriver = function(opts) {
|
Device.prototype.defaultConnection = function() {
|
||||||
if (opts == null) {
|
var first, k, v, _ref;
|
||||||
opts = {};
|
first = 0;
|
||||||
}
|
_ref = this.robot.connections;
|
||||||
Logger.debug("Loading driver '" + opts.driver + "'");
|
for (k in _ref) {
|
||||||
return this.robot.requireDriver(opts.driver, this.self, opts);
|
v = _ref[k];
|
||||||
};
|
first || (first = v);
|
||||||
|
}
|
||||||
|
return first;
|
||||||
|
};
|
||||||
|
|
||||||
return Device;
|
Device.prototype.requireDriver = function(opts) {
|
||||||
|
if (opts == null) {
|
||||||
|
opts = {};
|
||||||
|
}
|
||||||
|
Logger.debug("Loading driver '" + opts.driver + "'");
|
||||||
|
return this.robot.requireDriver(opts.driver, this.self, opts);
|
||||||
|
};
|
||||||
|
|
||||||
})(EventEmitter);
|
return Device;
|
||||||
|
|
||||||
|
})(EventEmitter);
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = Cylon.Device;
|
||||||
|
|
||||||
}).call(this);
|
}).call(this);
|
||||||
|
|
|
@ -8,79 +8,83 @@
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
require('./cylon')
|
require './cylon'
|
||||||
|
namespace = require 'node-namespace'
|
||||||
EventEmitter = require('events').EventEmitter
|
EventEmitter = require('events').EventEmitter
|
||||||
|
|
||||||
# The Artoo::Device class represents the interface to
|
# The Artoo::Device class represents the interface to
|
||||||
# a specific individual hardware devices. Examples would be a digital
|
# a specific individual hardware devices. Examples would be a digital
|
||||||
# thermometer connected to an Arduino, or a Sphero's accelerometer.
|
# thermometer connected to an Arduino, or a Sphero's accelerometer.
|
||||||
module.exports = class Device extends EventEmitter
|
namespace 'Cylon', ->
|
||||||
|
class @Device extends EventEmitter
|
||||||
|
|
||||||
# Public: Creates a new Device
|
# Public: Creates a new Device
|
||||||
#
|
#
|
||||||
# opts - object containing Device params
|
# opts - object containing Device params
|
||||||
# name - string name of the device
|
# name - string name of the device
|
||||||
# pin - string pin of the device
|
# pin - string pin of the device
|
||||||
# robot - parent Robot to the device
|
# robot - parent Robot to the device
|
||||||
# connection - connection to the device
|
# connection - connection to the device
|
||||||
# driver - string name of the module the device driver logic lives in
|
# driver - string name of the module the device driver logic lives in
|
||||||
#
|
#
|
||||||
# Returns a new Device
|
# Returns a new Device
|
||||||
constructor: (opts = {}) ->
|
constructor: (opts = {}) ->
|
||||||
@self = this
|
@self = this
|
||||||
@robot = opts.robot
|
@robot = opts.robot
|
||||||
@name = opts.name
|
@name = opts.name
|
||||||
@pin = opts.pin
|
@pin = opts.pin
|
||||||
@connection = @determineConnection(opts.connection) or @defaultConnection()
|
@connection = @determineConnection(opts.connection) or @defaultConnection()
|
||||||
@driver = @requireDriver(opts)
|
@driver = @requireDriver(opts)
|
||||||
proxyFunctionsToObject @driver.commands(), @driver, Device
|
proxyFunctionsToObject @driver.commands(), @driver, Device
|
||||||
|
|
||||||
# Public: Starts the device driver
|
# Public: Starts the device driver
|
||||||
#
|
#
|
||||||
# callback - callback function to be executed by the driver start
|
# callback - callback function to be executed by the driver start
|
||||||
#
|
#
|
||||||
# Returns result of supplied callback
|
# Returns result of supplied callback
|
||||||
start: (callback) =>
|
start: (callback) =>
|
||||||
msg = "Starting device '#{ @name }'"
|
msg = "Starting device '#{ @name }'"
|
||||||
msg += " on pin #{@pin}" if @pin?
|
msg += " on pin #{@pin}" if @pin?
|
||||||
Logger.info msg
|
Logger.info msg
|
||||||
@driver.start(callback)
|
@driver.start(callback)
|
||||||
|
|
||||||
# Public: Exports basic data for the Connection
|
# Public: Exports basic data for the Connection
|
||||||
#
|
#
|
||||||
# Returns an Object containing Connection data
|
# Returns an Object containing Connection data
|
||||||
data: ->
|
data: ->
|
||||||
{
|
{
|
||||||
name: @name
|
name: @name
|
||||||
driver: @driver.constructor.name || @driver.name
|
driver: @driver.constructor.name || @driver.name
|
||||||
pin: if @pin? then @pin.toString else null
|
pin: if @pin? then @pin.toString else null
|
||||||
connection: @connection.data()
|
connection: @connection.data()
|
||||||
commands: @driver.commands()
|
commands: @driver.commands()
|
||||||
}
|
}
|
||||||
|
|
||||||
# Public: Retrieves the connections from the parent Robot instances
|
# Public: Retrieves the connections from the parent Robot instances
|
||||||
#
|
#
|
||||||
# c - name of the connection to fetch
|
# c - name of the connection to fetch
|
||||||
#
|
#
|
||||||
# Returns a Connection instance
|
# Returns a Connection instance
|
||||||
determineConnection: (c) ->
|
determineConnection: (c) ->
|
||||||
@robot.connections[c] if c
|
@robot.connections[c] if c
|
||||||
|
|
||||||
# Public: Returns a default Connection to use
|
# Public: Returns a default Connection to use
|
||||||
#
|
#
|
||||||
# Returns a Connection instance
|
# Returns a Connection instance
|
||||||
defaultConnection: ->
|
defaultConnection: ->
|
||||||
first = 0
|
first = 0
|
||||||
for k, v of @robot.connections
|
for k, v of @robot.connections
|
||||||
first or= v
|
first or= v
|
||||||
first
|
first
|
||||||
|
|
||||||
# Public: sets up driver with @robot
|
# Public: sets up driver with @robot
|
||||||
#
|
#
|
||||||
# opts - object containing options when requiring driver
|
# opts - object containing options when requiring driver
|
||||||
# driver - name of the module to require()
|
# driver - name of the module to require()
|
||||||
#
|
#
|
||||||
# Returns the set-up driver
|
# Returns the set-up driver
|
||||||
requireDriver: (opts = {}) ->
|
requireDriver: (opts = {}) ->
|
||||||
Logger.debug "Loading driver '#{ opts.driver }'"
|
Logger.debug "Loading driver '#{ opts.driver }'"
|
||||||
@robot.requireDriver(opts.driver, @self, opts)
|
@robot.requireDriver(opts.driver, @self, opts)
|
||||||
|
|
||||||
|
module.exports = Cylon.Device
|
||||||
|
|
|
@ -1,23 +1,23 @@
|
||||||
(function() {
|
(function() {
|
||||||
'use strict';
|
'use strict';
|
||||||
var Device, Driver, Robot;
|
var Driver;
|
||||||
|
|
||||||
Device = source("device");
|
source("device");
|
||||||
|
|
||||||
|
source("robot");
|
||||||
|
|
||||||
Driver = source("driver");
|
Driver = source("driver");
|
||||||
|
|
||||||
Robot = source("robot");
|
|
||||||
|
|
||||||
describe("Device", function() {
|
describe("Device", function() {
|
||||||
var device, driver, requireDriver, robot;
|
var device, driver, requireDriver, robot;
|
||||||
robot = new Robot({
|
robot = new Cylon.Robot({
|
||||||
name: 'me'
|
name: 'me'
|
||||||
});
|
});
|
||||||
driver = new Driver({
|
driver = new Driver({
|
||||||
name: 'driving'
|
name: 'driving'
|
||||||
});
|
});
|
||||||
requireDriver = sinon.stub(robot, 'requireDriver').returns(driver);
|
requireDriver = sinon.stub(robot, 'requireDriver').returns(driver);
|
||||||
device = new Device({
|
device = new Cylon.Device({
|
||||||
name: "devisive",
|
name: "devisive",
|
||||||
driver: 'driving',
|
driver: 'driving',
|
||||||
robot: robot
|
robot: robot
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
Device = source("device")
|
source("device")
|
||||||
|
source("robot")
|
||||||
Driver = source("driver")
|
Driver = source("driver")
|
||||||
Robot = source("robot")
|
|
||||||
|
|
||||||
describe "Device", ->
|
describe "Device", ->
|
||||||
robot = new Robot(name: 'me')
|
robot = new Cylon.Robot(name: 'me')
|
||||||
driver = new Driver(name: 'driving')
|
driver = new Driver(name: 'driving')
|
||||||
requireDriver = sinon.stub(robot, 'requireDriver').returns(driver)
|
requireDriver = sinon.stub(robot, 'requireDriver').returns(driver)
|
||||||
device = new Device(name: "devisive", driver: 'driving', robot: robot)
|
device = new Cylon.Device(name: "devisive", driver: 'driving', robot: robot)
|
||||||
|
|
||||||
it "should belong to a robot", ->
|
it "should belong to a robot", ->
|
||||||
device.robot.name.should.be.equal 'me'
|
device.robot.name.should.be.equal 'me'
|
||||||
|
|
Loading…
Reference in New Issue