Address bugs w/ connections
This commit is contained in:
parent
44075a1a30
commit
1c922f02c1
|
@ -37,7 +37,7 @@ var Device = module.exports = function Device(opts) {
|
|||
this.robot = opts.robot;
|
||||
this.name = opts.name;
|
||||
this.pin = opts.pin;
|
||||
this.connection = this.determineConnection(opts.connection) || this.defaultConnection();
|
||||
this.connection = opts.connection;
|
||||
this.driver = this.initDriver(opts);
|
||||
|
||||
this.details = {};
|
||||
|
@ -96,29 +96,6 @@ Device.prototype.toJSON = function() {
|
|||
};
|
||||
};
|
||||
|
||||
// Public: Retrieves the connections from the parent Robot instances
|
||||
//
|
||||
// conn - name of the connection to fetch
|
||||
//
|
||||
// Returns a Connection instance
|
||||
Device.prototype.determineConnection = function(conn) {
|
||||
return this.robot.connections[conn];
|
||||
};
|
||||
|
||||
// Public: Returns a default Connection to use
|
||||
//
|
||||
// Returns a Connection instance
|
||||
Device.prototype.defaultConnection = function() {
|
||||
var first = 0;
|
||||
|
||||
for (var c in this.robot.connections) {
|
||||
var connection = this.robot.connections[c];
|
||||
first = first || connection;
|
||||
}
|
||||
|
||||
return first;
|
||||
};
|
||||
|
||||
// Public: sets up driver with @robot
|
||||
//
|
||||
// opts - object containing options when initializing driver
|
||||
|
|
|
@ -25,7 +25,7 @@ var Driver = module.exports = function Driver(opts) {
|
|||
|
||||
this.name = opts.name;
|
||||
this.device = opts.device;
|
||||
this.connection = this.device.connection;
|
||||
this.connection = opts.connection;
|
||||
this.adaptor = this.connection.adaptor;
|
||||
this.interval = extraParams.interval || 10;
|
||||
|
||||
|
|
29
lib/robot.js
29
lib/robot.js
|
@ -88,13 +88,6 @@ var Robot = module.exports = function Robot(opts) {
|
|||
this.initConnections(opts.connection || opts.connections);
|
||||
this.initDevices(opts.device || opts.devices);
|
||||
|
||||
var hasDevices = !!Object.keys(this.devices).length,
|
||||
hasConnections = !!Object.keys(this.connections).length;
|
||||
|
||||
if (hasDevices && !hasConnections) {
|
||||
throw new Error("No connections specified");
|
||||
}
|
||||
|
||||
for (var n in opts) {
|
||||
var opt = opts[n];
|
||||
|
||||
|
@ -211,6 +204,11 @@ Robot.prototype.initDevices = function(devices) {
|
|||
return;
|
||||
}
|
||||
|
||||
// check that there are connections to use
|
||||
if (!Object.keys(this.connections).length) {
|
||||
throw new Error("No connections specified")
|
||||
}
|
||||
|
||||
devices = [].concat(devices);
|
||||
|
||||
devices.forEach(function(device) {
|
||||
|
@ -222,6 +220,23 @@ Robot.prototype.initDevices = function(devices) {
|
|||
Logger.warn("Device names must be unique. Renaming '" + original + "' to '" + device.name + "'");
|
||||
}
|
||||
|
||||
if (typeof device.connection === 'string') {
|
||||
if (this.connections[device.connection] == null) {
|
||||
var str = "No connection found with the name " + device.connection + ".\n";
|
||||
Logger.fatal(str);
|
||||
process.emit('SIGINT');
|
||||
}
|
||||
|
||||
device.connection = this.connections[device.connection];
|
||||
} else {
|
||||
for (var conn in this.connections) {
|
||||
device.connection = this.connections[conn];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
device.adaptor = device.connection.adaptor;
|
||||
|
||||
Logger.info("Initializing device '" + device.name + "'.");
|
||||
this.devices[device.name] = new Device(device);
|
||||
}.bind(this));
|
||||
|
|
|
@ -19,7 +19,8 @@ describe("Device", function() {
|
|||
|
||||
driver = new Ping({
|
||||
name: 'driver',
|
||||
device: { connection: connection, port: 13 }
|
||||
device: { connection: connection, pin: 13 },
|
||||
connection: connection
|
||||
});
|
||||
|
||||
driver.cmd = spy();
|
||||
|
@ -32,7 +33,7 @@ describe("Device", function() {
|
|||
robot: robot,
|
||||
name: "ping",
|
||||
pin: 13,
|
||||
connection: 'loopback'
|
||||
connection: connection
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -161,16 +162,4 @@ describe("Device", function() {
|
|||
expect(json.commands).to.be.eql(Object.keys(driver.commands));
|
||||
});
|
||||
});
|
||||
|
||||
describe("#determineConnection", function() {
|
||||
it("returns the connection with the given name from the Robot", function() {
|
||||
expect(device.determineConnection("loopback")).to.be.eql(connection);
|
||||
});
|
||||
});
|
||||
|
||||
describe("#defaultConnection", function() {
|
||||
it("returns the first connection found on the robot", function() {
|
||||
expect(device.defaultConnection()).to.be.eql(connection);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -7,17 +7,22 @@ var Driver = source("driver"),
|
|||
Utils = source('utils');
|
||||
|
||||
describe("Driver", function() {
|
||||
var device, driver;
|
||||
var connection, device, driver;
|
||||
|
||||
beforeEach(function() {
|
||||
connection = {
|
||||
adaptor: 'adaptor'
|
||||
};
|
||||
|
||||
device = {
|
||||
connection: {},
|
||||
connection: connection,
|
||||
emit: spy()
|
||||
};
|
||||
|
||||
driver = new Driver({
|
||||
name: 'driver',
|
||||
device: device
|
||||
device: device,
|
||||
connection: connection
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -44,13 +49,14 @@ describe("Driver", function() {
|
|||
|
||||
it("sets @interval to 10ms by default, or the provided value", function() {
|
||||
expect(driver.interval).to.be.eql(10);
|
||||
|
||||
driver = new Driver({
|
||||
name: 'driver',
|
||||
device: device,
|
||||
extraParams: {
|
||||
interval: 2000
|
||||
}
|
||||
extraParams: { interval: 2000 },
|
||||
connection: { }
|
||||
});
|
||||
|
||||
expect(driver.interval).to.be.eql(2000);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -269,7 +269,9 @@ describe("Robot", function() {
|
|||
var bot;
|
||||
|
||||
beforeEach(function() {
|
||||
bot = new Robot();
|
||||
bot = new Robot({
|
||||
connection: { name: 'loopback', adaptor: 'loopback' }
|
||||
});
|
||||
});
|
||||
|
||||
context("when not passed anything", function() {
|
||||
|
|
Loading…
Reference in New Issue