Add support for nested device details inside conn
e.g. connections: { loopback: { adaptor: "loopback", devices: { ping: { driver: "ping", pin: 1 } } } } In the event of a name collision between the `devices` hash and a nested device, the nested device will 'win'.
This commit is contained in:
parent
fe4c899f7f
commit
8065644b4e
32
lib/robot.js
32
lib/robot.js
|
@ -132,6 +132,7 @@ Robot.prototype.connection = function(name, conn) {
|
||||||
if (this.connections[conn.name]) {
|
if (this.connections[conn.name]) {
|
||||||
var original = conn.name,
|
var original = conn.name,
|
||||||
str;
|
str;
|
||||||
|
|
||||||
conn.name = Utils.makeUnique(original, Object.keys(this.connections));
|
conn.name = Utils.makeUnique(original, Object.keys(this.connections));
|
||||||
|
|
||||||
str = "Connection names must be unique.";
|
str = "Connection names must be unique.";
|
||||||
|
@ -174,12 +175,32 @@ Robot.prototype.initConnections = function(opts) {
|
||||||
str += "It will be removed in 1.0.0.";
|
str += "It will be removed in 1.0.0.";
|
||||||
|
|
||||||
Logger.warn(str);
|
Logger.warn(str);
|
||||||
}
|
|
||||||
|
|
||||||
_.forEach(opts.connections, function(conn, key) {
|
_.forEach(opts.connections, function(conn, key) {
|
||||||
var name = _.isString(key) ? key : conn.name;
|
var name = _.isString(key) ? key : conn.name;
|
||||||
this.connection(name, conn);
|
this.connection(name, conn);
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
|
return this.connections;
|
||||||
|
}
|
||||||
|
|
||||||
|
_.forIn(opts.connections, function(conn, key) {
|
||||||
|
var name = _.isString(key) ? key : conn.name;
|
||||||
|
|
||||||
|
if (conn.devices) {
|
||||||
|
_.forIn(conn.devices, function(device, deviceName) {
|
||||||
|
opts.devices = opts.devices || {};
|
||||||
|
|
||||||
|
device.connection = name;
|
||||||
|
|
||||||
|
opts.devices[deviceName] = device;
|
||||||
|
});
|
||||||
|
|
||||||
|
delete conn.devices;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.connection(name, conn);
|
||||||
|
}, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.connections;
|
return this.connections;
|
||||||
|
@ -251,12 +272,19 @@ Robot.prototype.initDevices = function(opts) {
|
||||||
str += "It will be removed in 1.0.0.";
|
str += "It will be removed in 1.0.0.";
|
||||||
|
|
||||||
Logger.warn(str);
|
Logger.warn(str);
|
||||||
}
|
|
||||||
|
|
||||||
_.forEach(opts.devices, function(device, key) {
|
_.forEach(opts.devices, function(device, key) {
|
||||||
var name = _.isString(key) ? key : device.name;
|
var name = _.isString(key) ? key : device.name;
|
||||||
this.device(name, device);
|
this.device(name, device);
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
|
return this.devices;
|
||||||
|
}
|
||||||
|
|
||||||
|
_.forIn(opts.devices, function(device, key) {
|
||||||
|
var name = _.isString(key) ? key : device.name;
|
||||||
|
this.device(name, device);
|
||||||
|
}, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.devices;
|
return this.devices;
|
||||||
|
|
|
@ -310,6 +310,35 @@ describe("Robot", function() {
|
||||||
bot.initConnections({ connections: connections });
|
bot.initConnections({ connections: connections });
|
||||||
expect(bot.connections.loopback).to.be.instanceOf(Adaptor);
|
expect(bot.connections.loopback).to.be.instanceOf(Adaptor);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
context("when the object contains device details", function() {
|
||||||
|
var opts;
|
||||||
|
|
||||||
|
beforeEach(function() {
|
||||||
|
opts = {
|
||||||
|
connections: {
|
||||||
|
loopback: {
|
||||||
|
adaptor: "loopback",
|
||||||
|
devices: {
|
||||||
|
ping: { driver: "ping", pin: 1 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
bot.initConnections(opts);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("adds the devices to opts.devices", function() {
|
||||||
|
expect(opts.devices).to.be.eql({
|
||||||
|
ping: { driver: "ping", pin: 1, connection: "loopback" }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("removes the device details from optsconnections", function() {
|
||||||
|
expect(opts.connections.devices).to.be.eql(undefined);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
context("when passed an array of connection objects", function() {
|
context("when passed an array of connection objects", function() {
|
||||||
|
|
Loading…
Reference in New Issue