Namespace Device class

This commit is contained in:
Andrew Stewart 2013-11-04 12:16:42 -08:00
parent ae666f67dd
commit eda6f9db76
4 changed files with 142 additions and 132 deletions

120
dist/device.js vendored
View File

@ -9,79 +9,85 @@
(function() {
'use strict';
var Device, EventEmitter,
var EventEmitter, namespace,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__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; };
require('./cylon');
namespace = require('node-namespace');
EventEmitter = require('events').EventEmitter;
module.exports = Device = (function(_super) {
__extends(Device, _super);
namespace('Cylon', function() {
return this.Device = (function(_super) {
__extends(Device, _super);
function Device(opts) {
if (opts == null) {
opts = {};
function Device(opts) {
if (opts == null) {
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) {
var msg;
msg = "Starting device '" + this.name + "'";
if (this.pin != null) {
msg += " on pin " + this.pin;
}
Logger.info(msg);
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.start = function(callback) {
var msg;
msg = "Starting device '" + this.name + "'";
if (this.pin != null) {
msg += " on pin " + this.pin;
}
Logger.info(msg);
return this.driver.start(callback);
};
};
Device.prototype.determineConnection = function(c) {
if (c) {
return this.robot.connections[c];
}
};
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.defaultConnection = function() {
var first, k, v, _ref;
first = 0;
_ref = this.robot.connections;
for (k in _ref) {
v = _ref[k];
first || (first = v);
}
return first;
};
Device.prototype.determineConnection = function(c) {
if (c) {
return this.robot.connections[c];
}
};
Device.prototype.requireDriver = function(opts) {
if (opts == null) {
opts = {};
}
Logger.debug("Loading driver '" + opts.driver + "'");
return this.robot.requireDriver(opts.driver, this.self, opts);
};
Device.prototype.defaultConnection = function() {
var first, k, v, _ref;
first = 0;
_ref = this.robot.connections;
for (k in _ref) {
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);

View File

@ -8,79 +8,83 @@
'use strict';
require('./cylon')
require './cylon'
namespace = require 'node-namespace'
EventEmitter = require('events').EventEmitter
# The Artoo::Device class represents the interface to
# a specific individual hardware devices. Examples would be a digital
# 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
#
# opts - object containing Device params
# name - string name of the device
# pin - string pin of the device
# robot - parent Robot to the device
# connection - connection to the device
# driver - string name of the module the device driver logic lives in
#
# Returns a new Device
constructor: (opts = {}) ->
@self = this
@robot = opts.robot
@name = opts.name
@pin = opts.pin
@connection = @determineConnection(opts.connection) or @defaultConnection()
@driver = @requireDriver(opts)
proxyFunctionsToObject @driver.commands(), @driver, Device
# Public: Creates a new Device
#
# opts - object containing Device params
# name - string name of the device
# pin - string pin of the device
# robot - parent Robot to the device
# connection - connection to the device
# driver - string name of the module the device driver logic lives in
#
# Returns a new Device
constructor: (opts = {}) ->
@self = this
@robot = opts.robot
@name = opts.name
@pin = opts.pin
@connection = @determineConnection(opts.connection) or @defaultConnection()
@driver = @requireDriver(opts)
proxyFunctionsToObject @driver.commands(), @driver, Device
# Public: Starts the device driver
#
# callback - callback function to be executed by the driver start
#
# Returns result of supplied callback
start: (callback) =>
msg = "Starting device '#{ @name }'"
msg += " on pin #{@pin}" if @pin?
Logger.info msg
@driver.start(callback)
# Public: Starts the device driver
#
# callback - callback function to be executed by the driver start
#
# Returns result of supplied callback
start: (callback) =>
msg = "Starting device '#{ @name }'"
msg += " on pin #{@pin}" if @pin?
Logger.info msg
@driver.start(callback)
# Public: Exports basic data for the Connection
#
# Returns an Object containing Connection data
data: ->
{
name: @name
driver: @driver.constructor.name || @driver.name
pin: if @pin? then @pin.toString else null
connection: @connection.data()
commands: @driver.commands()
}
# Public: Exports basic data for the Connection
#
# Returns an Object containing Connection data
data: ->
{
name: @name
driver: @driver.constructor.name || @driver.name
pin: if @pin? then @pin.toString else null
connection: @connection.data()
commands: @driver.commands()
}
# Public: Retrieves the connections from the parent Robot instances
#
# c - name of the connection to fetch
#
# Returns a Connection instance
determineConnection: (c) ->
@robot.connections[c] if c
# Public: Retrieves the connections from the parent Robot instances
#
# c - name of the connection to fetch
#
# Returns a Connection instance
determineConnection: (c) ->
@robot.connections[c] if c
# Public: Returns a default Connection to use
#
# Returns a Connection instance
defaultConnection: ->
first = 0
for k, v of @robot.connections
first or= v
first
# Public: Returns a default Connection to use
#
# Returns a Connection instance
defaultConnection: ->
first = 0
for k, v of @robot.connections
first or= v
first
# Public: sets up driver with @robot
#
# opts - object containing options when requiring driver
# driver - name of the module to require()
#
# Returns the set-up driver
requireDriver: (opts = {}) ->
Logger.debug "Loading driver '#{ opts.driver }'"
@robot.requireDriver(opts.driver, @self, opts)
# Public: sets up driver with @robot
#
# opts - object containing options when requiring driver
# driver - name of the module to require()
#
# Returns the set-up driver
requireDriver: (opts = {}) ->
Logger.debug "Loading driver '#{ opts.driver }'"
@robot.requireDriver(opts.driver, @self, opts)
module.exports = Cylon.Device

View File

@ -1,23 +1,23 @@
(function() {
'use strict';
var Device, Driver, Robot;
var Driver;
Device = source("device");
source("device");
source("robot");
Driver = source("driver");
Robot = source("robot");
describe("Device", function() {
var device, driver, requireDriver, robot;
robot = new Robot({
robot = new Cylon.Robot({
name: 'me'
});
driver = new Driver({
name: 'driving'
});
requireDriver = sinon.stub(robot, 'requireDriver').returns(driver);
device = new Device({
device = new Cylon.Device({
name: "devisive",
driver: 'driving',
robot: robot

View File

@ -1,14 +1,14 @@
'use strict';
Device = source("device")
source("device")
source("robot")
Driver = source("driver")
Robot = source("robot")
describe "Device", ->
robot = new Robot(name: 'me')
robot = new Cylon.Robot(name: 'me')
driver = new Driver(name: 'driving')
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", ->
device.robot.name.should.be.equal 'me'