Merge pull request #126 from hybridgroup/robot-refactors

Refactors and Tests for Robot class
This commit is contained in:
Ron Evans 2014-02-28 21:49:43 -08:00
commit e6f6049e1e
2 changed files with 131 additions and 106 deletions

View File

@ -8,9 +8,6 @@
'use strict'; 'use strict';
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }
var __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
require('./cylon'); require('./cylon');
require('./basestar'); require('./basestar');
require("./connection"); require("./connection");
@ -53,45 +50,54 @@ namespace("Cylon", function() {
// every 1.second(), -> // every 1.second(), ->
// me.sphero.roll 60, Math.floor(Math.random() * 360// // me.sphero.roll 60, Math.floor(Math.random() * 360//
function Robot(opts) { function Robot(opts) {
var func, n, reserved; if (opts == null) { opts = {}; }
if (opts == null) {
opts = {}; var methods = [
"toString",
"registerDriver",
"requireDriver",
"registerAdaptor",
"requireAdaptor",
"stop",
"startDevices",
"startConnections",
"start",
"initDevices",
"initConnections"
];
for (var i = 0; i < methods.length ; i++) {
var method = methods[i];
this[method] = bind(this[method], this);
} }
this.toString = __bind(this.toString, this);
this.registerDriver = __bind(this.registerDriver, this);
this.requireDriver = __bind(this.requireDriver, this);
this.registerAdaptor = __bind(this.registerAdaptor, this);
this.requireAdaptor = __bind(this.requireAdaptor, this);
this.stop = __bind(this.stop, this);
this.startDevices = __bind(this.startDevices, this);
this.startConnections = __bind(this.startConnections, this);
this.start = __bind(this.start, this);
this.initDevices = __bind(this.initDevices, this);
this.initConnections = __bind(this.initConnections, this);
this.robot = this; this.robot = this;
this.name = opts.name || this.constructor.randomName(); this.name = opts.name || this.constructor.randomName();
this.master = opts.master; this.master = opts.master;
this.connections = {}; this.connections = {};
this.devices = {}; this.devices = {};
this.adaptors = {}; this.adaptors = {};
this.drivers = {}; this.drivers = {};
this.commands = []; this.commands = [];
this.running = false; this.running = false;
this.registerAdaptor("./test/loopback", "loopback"); this.registerAdaptor("./test/loopback", "loopback");
this.registerAdaptor("./test/test-adaptor", "test"); this.registerAdaptor("./test/test-adaptor", "test");
this.registerDriver("./test/ping", "ping"); this.registerDriver("./test/ping", "ping");
this.registerDriver("./test/test-driver", "test"); this.registerDriver("./test/test-driver", "test");
this.initConnections(opts.connection || opts.connections); this.initConnections(opts.connection || opts.connections);
this.initDevices(opts.device || opts.devices); this.initDevices(opts.device || opts.devices);
this.work = opts.work || function() {
return Logger.info("No work yet"); this.work = opts.work || function() { Logger.info("No work yet"); };
};
for (n in opts) { for (var n in opts) {
func = opts[n]; var func = opts[n],
reserved = ['connection', 'connections', 'device', 'devices', 'work']; reserved = ['connection', 'connections', 'device', 'devices', 'work'];
if (__indexOf.call(reserved, n) < 0) {
this.robot[n] = func; if (reserved.indexOf(n) < 0) { this.robot[n] = func; }
}
} }
} }
@ -106,29 +112,28 @@ namespace("Cylon", function() {
// //
// Returns an Object containing Robot data // Returns an Object containing Robot data
Robot.prototype.data = function() { Robot.prototype.data = function() {
var connection, device, n; var connections = (function() {
var results = [];
for (var n in this.connections) {
var conn = this.connections[n];
results.push(conn.data());
}
return results;
}).call(this);
var devices = (function() {
var results = [];
for (var n in this.devices) {
var device = this.devices[n];
results.push(device.data());
}
return results;
}).call(this);
return { return {
name: this.name, name: this.name,
connections: (function() { connections: connections,
var _ref, _results; devices: devices,
_ref = this.connections;
_results = [];
for (n in _ref) {
connection = _ref[n];
_results.push(connection.data());
}
return _results;
}).call(this),
devices: (function() {
var _ref, _results;
_ref = this.devices;
_results = [];
for (n in _ref) {
device = _ref[n];
_results.push(device.data());
}
return _results;
}).call(this),
commands: this.commands commands: this.commands
}; };
}; };
@ -139,18 +144,18 @@ namespace("Cylon", function() {
// //
// Returns initialized connections // Returns initialized connections
Robot.prototype.initConnections = function(connections) { Robot.prototype.initConnections = function(connections) {
var connection, _i, _len;
Logger.info("Initializing connections..."); Logger.info("Initializing connections...");
if (connections == null) { if (connections == null) { return; }
return;
}
connections = [].concat(connections); connections = [].concat(connections);
for (_i = 0, _len = connections.length; _i < _len; _i++) {
connection = connections[_i]; 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; connection['robot'] = this;
this.connections[connection.name] = new Cylon.Connection(connection); this.connections[connection.name] = new Cylon.Connection(connection);
} }
return this.connections; return this.connections;
}; };
@ -160,20 +165,19 @@ namespace("Cylon", function() {
// //
// Returns initialized devices // Returns initialized devices
Robot.prototype.initDevices = function(devices) { Robot.prototype.initDevices = function(devices) {
var device, _i, _len, _results;
Logger.info("Initializing devices..."); Logger.info("Initializing devices...");
if (devices == null) { if (devices == null) { return; }
return;
}
devices = [].concat(devices); devices = [].concat(devices);
_results = [];
for (_i = 0, _len = devices.length; _i < _len; _i++) { for (var i = 0; i < devices.length; i++) {
device = devices[_i]; var device = devices[i];
Logger.info("Initializing device '" + device.name + "'..."); Logger.info("Initializing device '" + device.name + "'...");
device['robot'] = this; device['robot'] = this;
_results.push(this.devices[device.name] = new Cylon.Device(device)); this.devices[device.name] = new Cylon.Device(device);
} }
return _results;
return this.devices;
}; };
// Public: Starts the Robot working. // Public: Starts the Robot working.
@ -182,13 +186,13 @@ namespace("Cylon", function() {
// //
// Returns the result of the work // Returns the result of the work
Robot.prototype.start = function() { Robot.prototype.start = function() {
var _this = this; var self = this;
return this.startConnections(function() { return this.startConnections(function() {
return _this.robot.startDevices(function() { return self.robot.startDevices(function() {
_this.robot.work.call(_this.robot, _this.robot); self.robot.work.call(self.robot, self.robot);
_this.running = true; self.running = true;
Logger.info("Working..."); Logger.info("Working...");
return _this.robot.emit('working'); return self.robot.emit('working');
}); });
}); });
}; };
@ -199,15 +203,16 @@ namespace("Cylon", function() {
// //
// Returns nothing // Returns nothing
Robot.prototype.startConnections = function(callback) { Robot.prototype.startConnections = function(callback) {
var connection, n, starters, _ref; var starters = {}
Logger.info("Starting connections..."); Logger.info("Starting connections...");
starters = {};
_ref = this.connections; for (var n in this.connections) {
for (n in _ref) { var connection = this.connections[n];
connection = _ref[n];
this.robot[n] = connection; this.robot[n] = connection;
starters[n] = connection.connect; starters[n] = connection.connect;
} }
return Async.parallel(starters, callback); return Async.parallel(starters, callback);
}; };
@ -217,15 +222,16 @@ namespace("Cylon", function() {
// //
// Returns nothing // Returns nothing
Robot.prototype.startDevices = function(callback) { Robot.prototype.startDevices = function(callback) {
var device, n, starters, _ref; var starters = {}
Logger.info("Starting devices..."); Logger.info("Starting devices...");
starters = {};
_ref = this.devices; for (var n in this.devices) {
for (n in _ref) { var device = this.devices[n];
device = _ref[n];
this.robot[n] = device; this.robot[n] = device;
starters[n] = device.start; starters[n] = device.start;
} }
return Async.parallel(starters, callback); return Async.parallel(starters, callback);
}; };
@ -235,19 +241,8 @@ namespace("Cylon", function() {
// //
// Returns nothing // Returns nothing
Robot.prototype.stop = function() { Robot.prototype.stop = function() {
var connection, device, n, _ref, _ref1, _results; for (var d in this.devices) { this.devices[d].stop(); }
_ref = this.devices; for (var c in this.connections) { this.connections[c].stop(); }
for (n in _ref) {
device = _ref[n];
device.stop();
}
_ref1 = this.connections;
_results = [];
for (n in _ref1) {
connection = _ref1[n];
_results.push(connection.disconnect());
}
return _results;
}; };
// Public: Initialize an adaptor and adds it to @robot.adaptors // Public: Initialize an adaptor and adds it to @robot.adaptors
@ -257,24 +252,23 @@ namespace("Cylon", function() {
// //
// Returns the adaptor // Returns the adaptor
Robot.prototype.initAdaptor = function(adaptorName, connection, opts) { Robot.prototype.initAdaptor = function(adaptorName, connection, opts) {
var realAdaptor, testAdaptor; if (opts == null) { opts = {}; }
if (opts == null) {
opts = {}; var adaptor = this.robot.requireAdaptor(adaptorName).adaptor({
}
realAdaptor = this.robot.requireAdaptor(adaptorName).adaptor({
name: adaptorName, name: adaptorName,
connection: connection, connection: connection,
extraParams: opts extraParams: opts
}); });
if (CylonConfig.testing_mode) { if (CylonConfig.testing_mode) {
testAdaptor = this.robot.requireAdaptor('test').adaptor({ var testAdaptor = this.robot.requireAdaptor('test').adaptor({
name: adaptorName, name: adaptorName,
connection: connection, connection: connection,
extraParams: opts extraParams: opts
}); });
return proxyTestStubs(realAdaptor.commands(), testAdaptor); return proxyTestStubs(adaptor.commands(), testAdaptor);
} else { } else {
return realAdaptor; return adaptor;
} }
}; };
@ -311,24 +305,24 @@ namespace("Cylon", function() {
// //
// Returns the new driver // Returns the new driver
Robot.prototype.initDriver = function(driverName, device, opts) { Robot.prototype.initDriver = function(driverName, device, opts) {
var realDriver, testDriver; if (opts == null) { opts = {}; }
if (opts == null) {
opts = {}; var driver = this.robot.requireDriver(driverName).driver({
}
realDriver = this.robot.requireDriver(driverName).driver({
name: driverName, name: driverName,
device: device, device: device,
extraParams: opts extraParams: opts
}); });
if (CylonConfig.testing_mode) { if (CylonConfig.testing_mode) {
testDriver = this.robot.requireDriver('test').driver({ var testDriver = this.robot.requireDriver('test').driver({
name: driverName, name: driverName,
device: device, device: device,
extraParams: opts extraParams: opts
}); });
return proxyTestStubs(realDriver.commands(), testDriver);
return proxyTestStubs(driver.commands(), testDriver);
} else { } else {
return realDriver; return driver;
} }
}; };

View File

@ -57,4 +57,35 @@ describe("Robot", function() {
robot.toString().should.be.equal("[Robot name='irobot']"); robot.toString().should.be.equal("[Robot name='irobot']");
}); });
}); });
describe("#data", function() {
var dataBot = new Cylon.Robot({
connection: { name: 'loopback', adaptor: 'loopback' },
device: { name: 'ping', driver: 'ping' }
});
var data = dataBot.data();
it("returns an object", function() {
expect(data).to.be.a('object');
});
it("contains the robot's name", function() {
expect(data.name).to.eql(dataBot.name);
});
it("contains the robot's commands", function() {
expect(data.commands).to.eql(dataBot.commands);
});
it("contains the robot's devices", function() {
var deviceData = dataBot.devices.ping.data();
expect(data.devices).to.eql([deviceData]);
});
it("contains the robot's connections", function() {
var connectionData = dataBot.connections.loopback.data();
expect(data.connections).to.eql([connectionData]);
});
});
}); });