Merge pull request #198 from hybridgroup/refactor/robot

Robot Refactors
This commit is contained in:
Ron Evans 2014-06-10 13:52:00 -04:00
commit 17c1d4609f
2 changed files with 86 additions and 63 deletions

View File

@ -17,10 +17,6 @@ var Connection = require("./connection"),
var Async = require("async"),
EventEmitter = require('events').EventEmitter;
// A Robot is the primary interface for interacting with a collection of physical
// computing capabilities.
var Robot;
// Public: Creates a new Robot
//
// opts - object containing Robot options
@ -43,11 +39,13 @@ var Robot;
// work: (me) ->
// Utils.every 1.second(), ->
// me.sphero.roll 60, Math.floor(Math.random() * 360//
module.exports = Robot = function Robot(opts) {
var Robot = module.exports = function Robot(opts) {
if (opts == null) {
opts = {};
}
var self = this;
var methods = [
"toString",
"registerDriver",
@ -62,44 +60,49 @@ module.exports = Robot = function Robot(opts) {
"initConnections"
];
for (var i = 0; i < methods.length ; i++) {
var method = methods[i];
this[method] = Utils.bind(this[method], this);
}
methods.forEach(function(method) {
self[method] = Utils.bind(self[method], self);
});
this.robot = this;
this.name = opts.name || this.constructor.randomName();
this.connections = {};
this.devices = {};
this.adaptors = {};
this.drivers = {};
this.commands = [];
this.running = false;
this.registerAdaptor("./test/loopback", "loopback");
this.registerAdaptor("./test/test-adaptor", "test");
this.registerDriver("./test/ping", "ping");
this.registerDriver("./test/test-driver", "test");
this.initConnections(opts.connection || opts.connections);
this.initDevices(opts.device || opts.devices);
this.work = opts.work || opts.play;
this.work || (this.work = function() { Logger.info("No work yet"); });
this.registerDefaults();
this.initConnections(opts.connection || opts.connections);
this.initDevices(opts.device || opts.devices);
for (var n in opts) {
var func = opts[n],
reserved = ['connection', 'connections', 'device', 'devices', 'work'];
if (reserved.indexOf(n) < 0) { this.robot[n] = func; }
if (reserved.indexOf(n) < 0) {
this[n] = func;
}
}
};
Utils.subclass(Robot, EventEmitter);
// Public: Registers the default Drivers and Adaptors with Cylon.
//
// Returns nothing.
Robot.prototype.registerDefaults = function registerDefaults() {
this.registerAdaptor("./test/loopback", "loopback");
this.registerAdaptor("./test/test-adaptor", "test");
this.registerDriver("./test/ping", "ping");
this.registerDriver("./test/test-driver", "test");
};
// Public: Generates a random name for a Robot.
//
// Returns a string name
@ -143,14 +146,17 @@ Robot.prototype.toJSON = function() {
//
// Returns initialized connections
Robot.prototype.initConnections = function(connections) {
Logger.info("Initializing connections...");
if (connections == null) { return; }
Logger.info("Initializing connections.");
if (connections == null) {
return;
}
connections = [].concat(connections);
for (var i = 0; i < connections.length; i++) {
var connection = connections[i];
Logger.info("Initializing connection '" + connection.name + "'...");
Logger.info("Initializing connection '" + connection.name + "'.");
connection['robot'] = this;
this.connections[connection.name] = new Connection(connection);
}
@ -164,14 +170,17 @@ Robot.prototype.initConnections = function(connections) {
//
// Returns initialized devices
Robot.prototype.initDevices = function(devices) {
Logger.info("Initializing devices...");
if (devices == null) { return; }
Logger.info("Initializing devices.");
if (devices == null) {
return;
}
devices = [].concat(devices);
for (var i = 0; i < devices.length; i++) {
var device = devices[i];
Logger.info("Initializing device '" + device.name + "'...");
Logger.info("Initializing device '" + device.name + "'.");
device['robot'] = this;
this.devices[device.name] = this._createDevice(device);
}
@ -190,17 +199,26 @@ Robot.prototype._createDevice = function(device) {
// Returns the result of the work
Robot.prototype.start = function() {
var self = this;
return this.startConnections(function() {
return self.robot.startDevices(function(err) {
if (err) {
throw err;
}else{
self.robot.work.call(self.robot, self.robot);
self.running = true;
Logger.info("Working...");
self.robot.emit('working');
}
});
var begin = function(callback) {
self.work.call(self, self);
self.running = true;
self.emit('working');
Logger.info('Working.');
callback(null, true);
};
Async.series([
self.startConnections,
self.startDevices,
begin
], function(err, results) {
if (!!err) {
Logger.fatal("An error occured while trying to start the robot:");
Logger.fatal(err);
}
});
};
@ -212,11 +230,11 @@ Robot.prototype.start = function() {
Robot.prototype.startConnections = function(callback) {
var starters = {};
Logger.info("Starting connections...");
Logger.info("Starting connections.");
for (var n in this.connections) {
var connection = this.connections[n];
this.robot[n] = connection;
this[n] = connection;
starters[n] = connection.connect;
}
@ -231,11 +249,11 @@ Robot.prototype.startConnections = function(callback) {
Robot.prototype.startDevices = function(callback) {
var starters = {};
Logger.info("Starting devices...");
Logger.info("Starting devices.");
for (var n in this.devices) {
var device = this.devices[n];
this.robot[n] = device;
this[n] = device;
starters[n] = device.start;
}
@ -248,8 +266,13 @@ Robot.prototype.startDevices = function(callback) {
//
// Returns nothing
Robot.prototype.halt = function() {
for (var d in this.devices) { this.devices[d].halt(); }
for (var c in this.connections) { this.connections[c].halt(); }
for (var d in this.devices) {
this.devices[d].halt();
}
for (var c in this.connections) {
this.connections[c].halt();
}
};
// Public: Initialize an adaptor and adds it to @robot.adaptors
@ -259,16 +282,18 @@ Robot.prototype.halt = function() {
//
// Returns the adaptor
Robot.prototype.initAdaptor = function(adaptorName, connection, opts) {
if (opts == null) { opts = {}; }
if (opts == null) {
opts = {};
}
var adaptor = this.robot.requireAdaptor(adaptorName, opts).adaptor({
var adaptor = this.requireAdaptor(adaptorName, opts).adaptor({
name: adaptorName,
connection: connection,
extraParams: opts
});
if (config.testing_mode) {
var testAdaptor = this.robot.requireAdaptor('test').adaptor({
var testAdaptor = this.requireAdaptor('test').adaptor({
name: adaptorName,
connection: connection,
extraParams: opts
@ -285,12 +310,12 @@ Robot.prototype.initAdaptor = function(adaptorName, connection, opts) {
//
// Returns the module for the adaptor
Robot.prototype.requireAdaptor = function(adaptorName, opts) {
if (this.robot.adaptors[adaptorName] == null) {
if (this.adaptors[adaptorName] == null) {
var moduleName = opts.module || adaptorName;
this.robot.registerAdaptor("cylon-" + moduleName, adaptorName);
this.robot.adaptors[adaptorName].register(this);
this.registerAdaptor("cylon-" + moduleName, adaptorName);
this.adaptors[adaptorName].register(this);
}
return this.robot.adaptors[adaptorName];
return this.adaptors[adaptorName];
};
// Public: Registers an Adaptor with the Robot
@ -321,16 +346,18 @@ Robot.prototype.registerAdaptor = function(moduleName, adaptorName) {
//
// Returns the new driver
Robot.prototype.initDriver = function(driverName, device, opts) {
if (opts == null) { opts = {}; }
if (opts == null) {
opts = {};
}
var driver = this.robot.requireDriver(driverName).driver({
var driver = this.requireDriver(driverName).driver({
name: driverName,
device: device,
extraParams: opts
});
if (config.testing_mode) {
var testDriver = this.robot.requireDriver('test').driver({
var testDriver = this.requireDriver('test').driver({
name: driverName,
device: device,
extraParams: opts
@ -348,11 +375,11 @@ Robot.prototype.initDriver = function(driverName, device, opts) {
//
// Returns the module for driver
Robot.prototype.requireDriver = function(driverName) {
if (this.robot.drivers[driverName] == null) {
this.robot.registerDriver("cylon-" + driverName, driverName);
this.robot.drivers[driverName].register(this);
if (this.drivers[driverName] == null) {
this.registerDriver("cylon-" + driverName, driverName);
this.drivers[driverName].register(this);
}
return this.robot.drivers[driverName];
return this.drivers[driverName];
};
// Public: Registers an Driver with the Robot

View File

@ -20,10 +20,6 @@ describe("Robot", function() {
});
describe("constructor", function() {
it("sets a @robot variable as a circular reference to the robot", function() {
expect(robot.robot).to.be.eql(robot);
});
describe("name", function() {
context("if provided", function() {
it("is set to the passed value", function() {