Merge pull request #258 from hybridgroup/climate-improvements
Code Climate improvements
This commit is contained in:
commit
ad94b81e87
84
lib/robot.js
84
lib/robot.js
|
@ -27,19 +27,6 @@ var Async = require("async"),
|
||||||
// work - work to be performed when the Robot is started
|
// work - work to be performed when the Robot is started
|
||||||
//
|
//
|
||||||
// Returns a new Robot
|
// 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) {
|
var Robot = module.exports = function Robot(opts) {
|
||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
|
|
||||||
|
@ -49,25 +36,14 @@ var Robot = module.exports = function Robot(opts) {
|
||||||
"startDevices",
|
"startDevices",
|
||||||
"startConnections",
|
"startConnections",
|
||||||
"start",
|
"start",
|
||||||
|
"initRobot",
|
||||||
"initDevices",
|
"initDevices",
|
||||||
"initConnections"
|
"initConnections"
|
||||||
];
|
];
|
||||||
|
|
||||||
_.bindAll(this, methods);
|
_.bindAll(this, methods);
|
||||||
|
|
||||||
this.name = opts.name || Robot.randomName();
|
this.initRobot(opts);
|
||||||
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.initConnections(opts);
|
this.initConnections(opts);
|
||||||
this.initDevices(opts);
|
this.initDevices(opts);
|
||||||
|
|
||||||
|
@ -145,6 +121,26 @@ Robot.prototype.connection = function(name, conn) {
|
||||||
return this;
|
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
|
// Public: Initializes all connections for the robot
|
||||||
//
|
//
|
||||||
// opts - options array passed to constructor
|
// opts - options array passed to constructor
|
||||||
|
@ -171,16 +167,7 @@ Robot.prototype.initConnections = function(opts) {
|
||||||
|
|
||||||
if (_.isObject(opts.connections)) {
|
if (_.isObject(opts.connections)) {
|
||||||
if (_.isArray(opts.connections)) {
|
if (_.isArray(opts.connections)) {
|
||||||
str = "Specifying connections as an array is deprecated. ";
|
this.performArraySetup(opts.connections, "connection", "connections");
|
||||||
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);
|
|
||||||
|
|
||||||
return this.connections;
|
return this.connections;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -268,16 +255,7 @@ Robot.prototype.initDevices = function(opts) {
|
||||||
|
|
||||||
if (_.isObject(opts.devices)) {
|
if (_.isObject(opts.devices)) {
|
||||||
if (_.isArray(opts.devices)) {
|
if (_.isArray(opts.devices)) {
|
||||||
str = "Specifying devices as an array is deprecated. ";
|
this.performArraySetup(opts.devices, "device", "devices");
|
||||||
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);
|
|
||||||
|
|
||||||
return this.devices;
|
return this.devices;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -425,3 +403,17 @@ Robot.prototype.halt = function(callback) {
|
||||||
Robot.prototype.toString = function() {
|
Robot.prototype.toString = function() {
|
||||||
return "[Robot name='" + this.name + "']";
|
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);
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in New Issue