Merge pull request #258 from hybridgroup/climate-improvements

Code Climate improvements
This commit is contained in:
Andrew Stewart 2015-01-17 23:33:07 -08:00
commit ad94b81e87
1 changed files with 38 additions and 46 deletions

View File

@ -27,19 +27,6 @@ var Async = require("async"),
// work - work to be performed when the Robot is started
//
// Returns a new Robot
// Example (CoffeeScript):
// Cylon.robot
// name: "Spherobot!"
//
// connection:
// name: "sphero", adaptor: "sphero", port: "/dev/rfcomm0"
//
// device:
// name: "sphero", driver: "sphero"
//
// work: (me) ->
// Utils.every 1.second(), ->
// me.sphero.roll 60, Math.floor(Math.random() * 360//
var Robot = module.exports = function Robot(opts) {
opts = opts || {};
@ -49,25 +36,14 @@ var Robot = module.exports = function Robot(opts) {
"startDevices",
"startConnections",
"start",
"initRobot",
"initDevices",
"initConnections"
];
_.bindAll(this, methods);
this.name = opts.name || Robot.randomName();
this.connections = {};
this.devices = {};
this.adaptors = {};
this.drivers = {};
this.commands = {};
this.running = false;
this.work = opts.work || opts.play;
if (!this.work) {
this.work = function() { Logger.debug("No work yet."); };
}
this.initRobot(opts);
this.initConnections(opts);
this.initDevices(opts);
@ -145,6 +121,26 @@ Robot.prototype.connection = function(name, conn) {
return this;
};
// Public: Initializes all vars for robot
//
// opts - options array passed to constructor
//
// Returns null
Robot.prototype.initRobot = function(opts) {
this.name = opts.name || Robot.randomName();
this.connections = {};
this.devices = {};
this.adaptors = {};
this.drivers = {};
this.commands = {};
this.running = false;
this.work = opts.work || opts.play;
if (!this.work) {
this.work = function() { Logger.debug("No work yet."); };
}
};
// Public: Initializes all connections for the robot
//
// opts - options array passed to constructor
@ -171,16 +167,7 @@ Robot.prototype.initConnections = function(opts) {
if (_.isObject(opts.connections)) {
if (_.isArray(opts.connections)) {
str = "Specifying connections as an array is deprecated. ";
str += "It will be removed in 1.0.0.";
Logger.warn(str);
_.forEach(opts.connections, function(conn, key) {
var name = _.isString(key) ? key : conn.name;
this.connection(name, conn);
}, this);
this.performArraySetup(opts.connections, "connection", "connections");
return this.connections;
}
@ -268,16 +255,7 @@ Robot.prototype.initDevices = function(opts) {
if (_.isObject(opts.devices)) {
if (_.isArray(opts.devices)) {
str = "Specifying devices as an array is deprecated. ";
str += "It will be removed in 1.0.0.";
Logger.warn(str);
_.forEach(opts.devices, function(device, key) {
var name = _.isString(key) ? key : device.name;
this.device(name, device);
}, this);
this.performArraySetup(opts.devices, "device", "devices");
return this.devices;
}
@ -425,3 +403,17 @@ Robot.prototype.halt = function(callback) {
Robot.prototype.toString = function() {
return "[Robot name='" + this.name + "']";
};
Robot.prototype.performArraySetup = function(things, typeOfThing, arrayName) {
var str = "Specifying ";
str += arrayName;
str += " as an array is deprecated. ";
str += "It will be removed in 1.0.0.";
Logger.warn(str);
_.forEach(things, function(t, key) {
var name = _.isString(key) ? key : t.name;
this[typeOfThing](name, t);
}, this);
};