Remove node-namespace from Device

This commit is contained in:
Andrew Stewart 2014-05-06 19:18:13 -07:00
parent d46de63379
commit f8e546a6e0
4 changed files with 105 additions and 120 deletions

View File

@ -8,121 +8,113 @@
'use strict';
require('./cylon');
require('./driver');
require('./utils');
var namespace = require('node-namespace'),
EventEmitter = require('events').EventEmitter;
var 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
namespace("Cylon", function() {
this.Device = (function(klass) {
subclass(Device, klass);
var 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
function Device(opts) {
if (opts == null) { opts = {}; }
// 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
module.exports = Device = function Device(opts) {
if (opts == null) {
opts = {};
}
this.halt = bind(this.halt, this);
this.start = bind(this.start, this);
this.halt = bind(this.halt, this);
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.initDriver(opts);
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.initDriver(opts);
proxyFunctionsToObject(this.driver.commands(), this.driver, this.self);
}
proxyFunctionsToObject(this.driver.commands(), this.driver, this.self);
};
// Public: Starts the device driver
//
// callback - callback function to be executed by the driver start
//
// Returns result of supplied callback
Device.prototype.start = function(callback) {
var msg = "Starting device " + this.name;
subclass(Device, EventEmitter);
if (this.pin != null) {
msg += " on pin " + this.pin;
}
// Public: Starts the device driver
//
// callback - callback function to be executed by the driver start
//
// Returns result of supplied callback
Device.prototype.start = function(callback) {
var msg = "Starting device " + this.name;
Logger.info(msg);
return this.driver.start(callback);
};
if (this.pin != null) {
msg += " on pin " + this.pin;
}
// Public: Halt the device driver
//
// Returns result of supplied callback
Device.prototype.halt = function() {
Logger.info("Halting device " + this.name);
return this.driver.halt();
};
Logger.info(msg);
return this.driver.start(callback);
};
// Public: Exports basic data for the Connection
//
// Returns an Object containing Connection data
Device.prototype.data = function() {
return {
name: this.name,
driver: this.driver.constructor.name || this.driver.name,
pin: this.pin,
connection: this.connection.data(),
commands: this.driver.commands()
};
};
// Public: Halt the device driver
//
// Returns result of supplied callback
Device.prototype.halt = function() {
Logger.info("Halting device " + this.name);
return this.driver.halt();
};
// Public: Retrieves the connections from the parent Robot instances
//
// c - name of the connection to fetch
//
// Returns a Connection instance
Device.prototype.determineConnection = function(c) {
if (c) { return this.robot.connections[c]; }
};
// Public: Exports basic data for the Connection
//
// Returns an Object containing Connection data
Device.prototype.data = function() {
return {
name: this.name,
driver: this.driver.constructor.name || this.driver.name,
pin: this.pin,
connection: this.connection.data(),
commands: this.driver.commands()
};
};
// Public: Returns a default Connection to use
//
// Returns a Connection instance
Device.prototype.defaultConnection = function() {
var first = 0;
// Public: Retrieves the connections from the parent Robot instances
//
// c - name of the connection to fetch
//
// Returns a Connection instance
Device.prototype.determineConnection = function(c) {
if (c) { return this.robot.connections[c]; }
};
for (var c in this.robot.connections) {
var connection = this.robot.connections[c];
first || (first = connection);
}
// Public: Returns a default Connection to use
//
// Returns a Connection instance
Device.prototype.defaultConnection = function() {
var first = 0;
return first;
};
for (var c in this.robot.connections) {
var connection = this.robot.connections[c];
first || (first = connection);
}
// Public: sets up driver with @robot
//
// opts - object containing options when initializing driver
// driver - name of the driver to intt()
//
// Returns the set-up driver
Device.prototype.initDriver = function(opts) {
if (opts == null) { opts = {}; }
Logger.debug("Loading driver '" + opts.driver + "'");
return this.robot.initDriver(opts.driver, this.self, opts);
};
return first;
};
return Device;
})(EventEmitter);
});
module.exports = Cylon.Device;
// Public: sets up driver with @robot
//
// opts - object containing options when initializing driver
// driver - name of the driver to intt()
//
// Returns the set-up driver
Device.prototype.initDriver = function(opts) {
if (opts == null) { opts = {}; }
Logger.debug("Loading driver '" + opts.driver + "'");
return this.robot.initDriver(opts.driver, this.self, opts);
};

View File

@ -12,7 +12,7 @@ require('./cylon');
require('./basestar');
require("./connection");
require("./adaptor");
require("./device");
var Device = require("./device");
require("./driver");
require('./digital-pin');
@ -174,12 +174,16 @@ namespace("Cylon", function() {
var device = devices[i];
Logger.info("Initializing device '" + device.name + "'...");
device['robot'] = this;
this.devices[device.name] = new Cylon.Device(device);
this.devices[device.name] = this._createDevice(device);
}
return this.devices;
};
Robot.prototype._createDevice = function(device) {
return new Device(device);
};
// Public: Starts the Robot working.
//
// Starts the connections, devices, and work.

View File

@ -2,7 +2,7 @@
var Ping = source('test/ping');
source("device");
var Device = source("device");
source("robot");
describe("Cylon.Device", function() {
@ -20,7 +20,7 @@ describe("Cylon.Device", function() {
var initDriver = stub(robot, 'initDriver').returns(driver);
var device = new Cylon.Device({
var device = new Device({
robot: robot,
name: "ping",
pin: 13,

View File

@ -1,5 +1,7 @@
"use strict";
var Device = source('device');
source("robot");
describe("Cylon.Robot", function() {
@ -155,36 +157,23 @@ describe("Cylon.Robot", function() {
});
context("when passed a connection object", function() {
before(function() {
stub(Cylon, 'Device').returns("new device")
});
afterEach(function() { robot.devices = {}; });
after(function() {
Cylon.Device.restore();
});
it("instantiates a new connection with the provided object", function() {
it("instantiates a new device with the provided object", function() {
var device = { name: 'ping', driver: 'ping' };
robot.initDevices(device);
expect(Cylon.Device).to.be.calledWith(device);
expect(Cylon.Device).to.be.calledWithNew;
expect(robot.devices['ping']).to.be.a('object');
});
});
context("when passed an array of device objects", function() {
before(function() {
stub(Cylon, 'Device').returns("new device")
});
after(function() {
Cylon.Device.restore();
});
afterEach(function() { robot.devices = {}; });
it("instantiates a new device with each of the provided objects", function() {
var devices = [{ name: 'ping', driver: 'ping' }]
robot.initDevices(devices);
expect(Cylon.Device).to.be.calledWith(devices[0]);
expect(Cylon.Device).to.be.calledWithNew;
expect(robot.devices['ping']).to.be.a('object');
});
});
});