210 fix merge files andrew :)
This commit is contained in:
parent
97e9cd0997
commit
933c5d2660
172
lib/cylon.js
172
lib/cylon.js
|
@ -6,82 +6,66 @@
|
|||
* Licensed under the Apache 2.0 license.
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
(function() {
|
||||
'use strict';
|
||||
var Cylon,
|
||||
__slice = [].slice,
|
||||
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
||||
|
||||
require('./config');
|
||||
|
||||
require('./utils');
|
||||
|
||||
require('./logger');
|
||||
|
||||
require('./driver');
|
||||
|
||||
require('./adaptor');
|
||||
|
||||
Logger.setup();
|
||||
|
||||
// Cylon is the global namespace for the project, and also in charge of
|
||||
// maintaining the Master singleton that controls all the robots.
|
||||
Cylon = (function() {
|
||||
var Master, instance;
|
||||
|
||||
var Cylon = (function() {
|
||||
function Cylon() {}
|
||||
|
||||
instance = null;
|
||||
var instance = null;
|
||||
|
||||
|
||||
// Public: Fetches singleton instance of Master, or creates a new one if it
|
||||
// doesn't already exist
|
||||
//
|
||||
// Returns a Master instance
|
||||
Cylon.getInstance = function() {
|
||||
var args;
|
||||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
||||
return instance != null ? instance : instance = (function(func, args, ctor) {
|
||||
ctor.prototype = func.prototype;
|
||||
var child = new ctor, result = func.apply(child, args);
|
||||
return Object(result) === result ? result : child;
|
||||
})(Master, args, function(){});
|
||||
return new Master();
|
||||
};
|
||||
|
||||
var bind = function(fn, me) {
|
||||
return function() { return fn.apply(me, arguments); }
|
||||
}
|
||||
|
||||
// The Master class is our puppeteer that manages all the robots, as well as
|
||||
// starting them and the API.
|
||||
Master = (function() {
|
||||
var api, api_config, robots;
|
||||
|
||||
robots = [];
|
||||
|
||||
api = null;
|
||||
|
||||
api_config = {
|
||||
host: '127.0.0.1',
|
||||
port: '3000'
|
||||
};
|
||||
var Master = (function() {
|
||||
var robots = [],
|
||||
api = null,
|
||||
api_config = { host: '127.0.0.1', port: '3000' };
|
||||
|
||||
// Public: Creates a new Master
|
||||
//
|
||||
// Returns a Master instance
|
||||
function Master() {
|
||||
this.robot = __bind(this.robot, this);
|
||||
var readLine, rl;
|
||||
this.robot = bind(this.robot, this);
|
||||
|
||||
this.self = this;
|
||||
|
||||
if (process.platform === "win32") {
|
||||
readLine = require("readline");
|
||||
rl = readLine.createInterface({
|
||||
var readline = require("readline");
|
||||
|
||||
var rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
});
|
||||
rl.on("SIGINT", function() {
|
||||
return process.emit("SIGINT");
|
||||
});
|
||||
|
||||
rl.on("SIGINT", function() { return process.emit("SIGINT"); });
|
||||
}
|
||||
|
||||
process.on("SIGINT", function() {
|
||||
Cylon.getInstance().stop();
|
||||
return process.kill();
|
||||
process.kill();
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -98,10 +82,11 @@
|
|||
// work: (me) ->
|
||||
// me.led.toggle()
|
||||
Master.prototype.robot = function(opts) {
|
||||
var Robot, robot;
|
||||
Robot = require("./robot");
|
||||
var Robot = require("./robot");
|
||||
opts.master = this;
|
||||
robot = new Robot(opts);
|
||||
|
||||
var robot = new Robot(opts);
|
||||
|
||||
robots.push(robot);
|
||||
return robot;
|
||||
};
|
||||
|
@ -121,11 +106,11 @@
|
|||
//
|
||||
// Returns the API configuration
|
||||
Master.prototype.api = function(opts) {
|
||||
if (opts == null) {
|
||||
opts = {};
|
||||
}
|
||||
if (opts == null) { opts = {}; }
|
||||
|
||||
api_config.host = opts.host || "127.0.0.1";
|
||||
api_config.port = opts.port || "3000";
|
||||
|
||||
return api_config;
|
||||
};
|
||||
|
||||
|
@ -136,24 +121,19 @@
|
|||
//
|
||||
// Returns the found Robot or result of the callback if it's supplied
|
||||
Master.prototype.findRobot = function(name, callback) {
|
||||
var bot, error, robot, _i, _len;
|
||||
var error,
|
||||
robot = null;
|
||||
for (_i = 0, _len = robots.length; _i < _len; _i++) {
|
||||
bot = robots[_i];
|
||||
if (bot.name === name) {
|
||||
robot = bot;
|
||||
}
|
||||
|
||||
for (var i = 0; i < robots.length; i++) {
|
||||
var bot = robots[i];
|
||||
if (bot.name === name) { robot = bot; }
|
||||
}
|
||||
|
||||
if (robot == null) {
|
||||
error = {
|
||||
error: "No Robot found with the name " + name
|
||||
};
|
||||
}
|
||||
if (callback) {
|
||||
return callback(error, robot);
|
||||
} else {
|
||||
return robot;
|
||||
error = { error: "No Robot found with the name " + name };
|
||||
}
|
||||
|
||||
return callback ? callback(error, robot) : robot;
|
||||
};
|
||||
|
||||
// Public: Finds a particular Robot's device by name
|
||||
|
@ -165,24 +145,18 @@
|
|||
// Returns the found Device or result of the callback if it's supplied
|
||||
Master.prototype.findRobotDevice = function(robotid, deviceid, callback) {
|
||||
return this.findRobot(robotid, function(err, robot) {
|
||||
var device, error;
|
||||
if (err) {
|
||||
callback(err, robot);
|
||||
}
|
||||
var error,
|
||||
device = null;
|
||||
if (robot.devices[deviceid]) {
|
||||
device = robot.devices[deviceid];
|
||||
}
|
||||
|
||||
if (err) { return callback(err, robot); }
|
||||
|
||||
if (robot.devices[deviceid]) { device = robot.devices[deviceid]; }
|
||||
|
||||
if (device == null) {
|
||||
error = {
|
||||
error: "No device found with the name " + deviceid + "."
|
||||
};
|
||||
}
|
||||
if (callback) {
|
||||
return callback(error, device);
|
||||
} else {
|
||||
return device;
|
||||
error = { error: "No device found with the name " + deviceid + "." };
|
||||
}
|
||||
|
||||
return callback ? callback(error, device) : device;
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -195,24 +169,18 @@
|
|||
// Returns the found Connection or result of the callback if it's supplied
|
||||
Master.prototype.findRobotConnection = function(robotid, connid, callback) {
|
||||
return this.findRobot(robotid, function(err, robot) {
|
||||
var connection, error;
|
||||
if (err) {
|
||||
callback(err, robot);
|
||||
}
|
||||
var error,
|
||||
connection = null;
|
||||
if (robot.connections[connid]) {
|
||||
connection = robot.connections[connid];
|
||||
}
|
||||
|
||||
if (err) { return callback(err, robot); }
|
||||
|
||||
if (robot.connections[connid]) { connection = robot.connections[connid]; }
|
||||
|
||||
if (connection == null) {
|
||||
error = {
|
||||
error: "No connection found with the name " + connid + "."
|
||||
};
|
||||
}
|
||||
if (callback) {
|
||||
return callback(error, connection);
|
||||
} else {
|
||||
return connection;
|
||||
error = { error: "No connection found with the name " + connid + "." };
|
||||
}
|
||||
|
||||
return callback ? callback(error, connection) : connection;
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -220,27 +188,22 @@
|
|||
//
|
||||
// Returns nothing
|
||||
Master.prototype.start = function() {
|
||||
var robot, _i, _len, _results;
|
||||
this.startAPI();
|
||||
_results = [];
|
||||
for (_i = 0, _len = robots.length; _i < _len; _i++) {
|
||||
robot = robots[_i];
|
||||
_results.push(robot.start());
|
||||
|
||||
for (var i = 0; i < robots.length; i++) {
|
||||
var robot = robots[i];
|
||||
robot.start();
|
||||
}
|
||||
return _results;
|
||||
};
|
||||
|
||||
// Public: Stops the API and the robots
|
||||
//
|
||||
// Returns nothing
|
||||
Master.prototype.stop = function() {
|
||||
var robot, _i, _len, _results;
|
||||
_results = [];
|
||||
for (_i = 0, _len = robots.length; _i < _len; _i++) {
|
||||
robot = robots[_i];
|
||||
_results.push(robot.stop());
|
||||
for (var i = 0; i < robots.length; i++) {
|
||||
var robot = robots[i];
|
||||
robot.stop();
|
||||
}
|
||||
return _results;
|
||||
};
|
||||
|
||||
// Creates a new instance of the Cylon API server, or returns the
|
||||
|
@ -248,10 +211,9 @@
|
|||
//
|
||||
// Returns an Cylon.ApiServer instance
|
||||
Master.prototype.startAPI = function() {
|
||||
var server;
|
||||
server = require('./api');
|
||||
var Server = require('./api');
|
||||
api_config.master = this.self;
|
||||
return api != null ? api : api = new server(api_config);
|
||||
return api != null ? api : api = new Server(api_config);
|
||||
};
|
||||
|
||||
return Master;
|
||||
|
@ -263,5 +225,3 @@
|
|||
}).call(this);
|
||||
|
||||
module.exports = Cylon.getInstance();
|
||||
|
||||
}).call(this);
|
||||
|
|
112
lib/logger.js
112
lib/logger.js
|
@ -6,10 +6,11 @@
|
|||
* Licensed under the Apache 2.0 license.
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
(function() {
|
||||
var BasicLogger, NullLogger,
|
||||
__slice = [].slice;
|
||||
var getArgs = function(args) {
|
||||
return args.length >= 1 ? [].slice.call(args, 0) : [];
|
||||
}
|
||||
|
||||
// The Logger is a global object to facilitate logging stuff to the console (or
|
||||
// other output) easily and consistently. It's available anywhere in Cylon, as
|
||||
|
@ -23,76 +24,89 @@
|
|||
//
|
||||
// Returns the new logger instance
|
||||
setup: function(logger) {
|
||||
if (logger == null) {
|
||||
logger = new BasicLogger;
|
||||
if (logger == null) { logger = new BasicLogger; }
|
||||
|
||||
if (logger === false) {
|
||||
this.logger = new NullLogger();
|
||||
} else {
|
||||
this.logger = logger;
|
||||
}
|
||||
return this.logger = logger === false ? new NullLogger : logger;
|
||||
},
|
||||
toString: function() {
|
||||
return this.logger.toString();
|
||||
|
||||
return this.logger
|
||||
},
|
||||
|
||||
toString: function() { return this.logger.toString(); },
|
||||
|
||||
debug: function() {
|
||||
var args, _ref;
|
||||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
||||
return (_ref = this.logger).debug.apply(_ref, args);
|
||||
var args = getArgs(arguments);
|
||||
return this.logger.debug.apply(this.logger, args);
|
||||
},
|
||||
|
||||
info: function() {
|
||||
var args, _ref;
|
||||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
||||
return (_ref = this.logger).info.apply(_ref, args);
|
||||
var args = getArgs(arguments);
|
||||
return this.logger.info.apply(this.logger, args);
|
||||
},
|
||||
|
||||
warn: function() {
|
||||
var args, _ref;
|
||||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
||||
return (_ref = this.logger).warn.apply(_ref, args);
|
||||
var args = getArgs(arguments);
|
||||
return this.logger.warn.apply(this.logger, args);
|
||||
},
|
||||
|
||||
error: function() {
|
||||
var args, _ref;
|
||||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
||||
return (_ref = this.logger).error.apply(_ref, args);
|
||||
var args = getArgs(arguments);
|
||||
return this.logger.error.apply(this.logger, args);
|
||||
},
|
||||
|
||||
fatal: function() {
|
||||
var args, _ref;
|
||||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
||||
return (_ref = this.logger).fatal.apply(_ref, args);
|
||||
var args = getArgs(arguments);
|
||||
return this.logger.fatal.apply(this.logger, args);
|
||||
}
|
||||
};
|
||||
|
||||
// The BasicLogger pushes stuff to console.log. Nothing more, nothing less.
|
||||
BasicLogger = (function() {
|
||||
var BasicLogger = (function() {
|
||||
function BasicLogger() {}
|
||||
|
||||
BasicLogger.prototype.toString = function() {
|
||||
return "BasicLogger";
|
||||
};
|
||||
BasicLogger.prototype.toString = function() { return "BasicLogger"; };
|
||||
|
||||
BasicLogger.prototype.debug = function() {
|
||||
var args;
|
||||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
||||
return console.log.apply(console, ["D, [" + (new Date().toISOString()) + "] DEBUG -- :"].concat(__slice.call(args)));
|
||||
var args = getArgs(arguments),
|
||||
string = ["D, [" + (new Date().toISOString()) + "] DEBUG -- :"],
|
||||
data = string.concat(args.slice());
|
||||
|
||||
return console.log.apply(console, data);
|
||||
};
|
||||
|
||||
BasicLogger.prototype.info = function() {
|
||||
var args;
|
||||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
||||
return console.log.apply(console, ["I, [" + (new Date().toISOString()) + "] INFO -- :"].concat(__slice.call(args)));
|
||||
var args = getArgs(arguments),
|
||||
string = ["I, [" + (new Date().toISOString()) + "] INFO -- :"],
|
||||
data = string.concat(args.slice());
|
||||
|
||||
return console.log.apply(console, data);
|
||||
};
|
||||
|
||||
BasicLogger.prototype.warn = function() {
|
||||
var args;
|
||||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
||||
return console.log.apply(console, ["W, [" + (new Date().toISOString()) + "] WARN -- :"].concat(__slice.call(args)));
|
||||
var args = getArgs(arguments),
|
||||
string = ["W, [" + (new Date().toISOString()) + "] WARN -- :"],
|
||||
data = string.concat(args.slice());
|
||||
|
||||
return console.log.apply(console, data);
|
||||
};
|
||||
|
||||
BasicLogger.prototype.error = function() {
|
||||
var args;
|
||||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
||||
return console.log.apply(console, ["E, [" + (new Date().toISOString()) + "] ERROR -- :"].concat(__slice.call(args)));
|
||||
var args = getArgs(arguments),
|
||||
string = ["E, [" + (new Date().toISOString()) + "] ERROR -- :"],
|
||||
data = string.concat(args.slice());
|
||||
|
||||
return console.log.apply(console, data);
|
||||
};
|
||||
|
||||
BasicLogger.prototype.fatal = function() {
|
||||
var args;
|
||||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
||||
return console.log.apply(console, ["F, [" + (new Date().toISOString()) + "] FATAL -- :"].concat(__slice.call(args)));
|
||||
var args = getArgs(arguments),
|
||||
string = ["F, [" + (new Date().toISOString()) + "] FATAL -- :"],
|
||||
data = string.concat(args.slice());
|
||||
|
||||
return console.log.apply(console, data);
|
||||
};
|
||||
|
||||
return BasicLogger;
|
||||
|
@ -101,25 +115,17 @@
|
|||
|
||||
// The NullLogger is designed for cases where you want absolutely nothing to
|
||||
// print to anywhere. Every proxied method from the Logger returns a noop.
|
||||
NullLogger = (function() {
|
||||
var NullLogger = (function() {
|
||||
function NullLogger() {}
|
||||
|
||||
NullLogger.prototype.toString = function() {
|
||||
return "NullLogger";
|
||||
};
|
||||
NullLogger.prototype.toString = function() { return "NullLogger"; };
|
||||
|
||||
NullLogger.prototype.debug = function() {};
|
||||
|
||||
NullLogger.prototype.info = function() {};
|
||||
|
||||
NullLogger.prototype.warn = function() {};
|
||||
|
||||
NullLogger.prototype.error = function() {};
|
||||
|
||||
NullLogger.prototype.fatal = function() {};
|
||||
|
||||
return NullLogger;
|
||||
|
||||
})();
|
||||
|
||||
}).call(this);
|
||||
|
|
89
lib/utils.js
89
lib/utils.js
|
@ -6,10 +6,6 @@
|
|||
* Licensed under the Apache 2.0 license.
|
||||
*/
|
||||
|
||||
|
||||
(function() {
|
||||
var __slice = [].slice;
|
||||
|
||||
// Public: Alias to setInterval, combined with Number monkeypatches below to
|
||||
// create an artoo-like syntax.
|
||||
//
|
||||
|
@ -58,17 +54,15 @@
|
|||
// Examples:
|
||||
// sleep 1.second()
|
||||
global.sleep = function(ms) {
|
||||
var i, start, _results;
|
||||
start = Date.now();
|
||||
_results = [];
|
||||
var start = Date.now();
|
||||
|
||||
while(Date.now() < start + ms) {
|
||||
_results.push(i = 1);
|
||||
var i = 0;
|
||||
}
|
||||
return _results;
|
||||
};
|
||||
|
||||
// Copies
|
||||
global.slice = [].slice;
|
||||
|
||||
global.hasProp = {}.hasOwnProperty;
|
||||
|
||||
// Public: Function to use for class inheritance. Copy of a CoffeeScript helper
|
||||
|
@ -84,15 +78,12 @@
|
|||
//
|
||||
// Returns subclass
|
||||
global.subclass = function(child, parent) {
|
||||
var ctor, key;
|
||||
ctor = function() {
|
||||
this.constructor = child;
|
||||
};
|
||||
for (key in parent) {
|
||||
if (hasProp.call(parent, key)) {
|
||||
child[key] = parent[key];
|
||||
}
|
||||
var ctor = function() { this.constructor = child; };
|
||||
|
||||
for (var key in parent) {
|
||||
if (hasProp.call(parent, key)) { child[key] = parent[key]; }
|
||||
}
|
||||
|
||||
ctor.prototype = parent.prototype;
|
||||
child.prototype = new ctor();
|
||||
child.__super__ = parent.prototype;
|
||||
|
@ -110,28 +101,23 @@
|
|||
//
|
||||
// Returns base
|
||||
global.proxyFunctionsToObject = function(methods, target, base, force) {
|
||||
var method, _fn, _i, _len;
|
||||
if (base == null) {
|
||||
base = this;
|
||||
}
|
||||
if (force == null) {
|
||||
force = false;
|
||||
}
|
||||
_fn = function(method) {
|
||||
if (base == null) { base = this; }
|
||||
if (force == null) { force = false; }
|
||||
|
||||
var fn = function(method) {
|
||||
return base[method] = function() {
|
||||
var args;
|
||||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
||||
var args = arguments.length >= 1 ? [].slice.call(arguments, 0) : [];
|
||||
return target[method].apply(target, args);
|
||||
};
|
||||
};
|
||||
for (_i = 0, _len = methods.length; _i < _len; _i++) {
|
||||
method = methods[_i];
|
||||
|
||||
for (var i = 0; i < methods.length; i++) {
|
||||
var method = methods[i];
|
||||
if (!force) {
|
||||
if (typeof base[method] === 'function') {
|
||||
continue;
|
||||
if (typeof base[method] === 'function') { continue; }
|
||||
}
|
||||
}
|
||||
_fn(method);
|
||||
|
||||
fn(method);
|
||||
}
|
||||
return base;
|
||||
};
|
||||
|
@ -144,36 +130,32 @@
|
|||
//
|
||||
// Returns base
|
||||
global.proxyTestStubs = function(methods, base) {
|
||||
var method, _i, _len;
|
||||
if (base == null) {
|
||||
base = this;
|
||||
}
|
||||
for (_i = 0, _len = methods.length; _i < _len; _i++) {
|
||||
method = methods[_i];
|
||||
base[method] = function() {
|
||||
var args;
|
||||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
||||
return true;
|
||||
};
|
||||
if (base == null) { base = this; }
|
||||
|
||||
for (var i = 0; i < methods.length; i++) {
|
||||
var method = methods[i];
|
||||
|
||||
base[method] = function() { return true; };
|
||||
base.commandList.push(method);
|
||||
}
|
||||
|
||||
return base;
|
||||
};
|
||||
|
||||
// Public: Monkey-patches Number to have Rails-like #seconds() function. Warning,
|
||||
// Public: Monkey-patches Number to have Rails-like //seconds() function. Warning,
|
||||
// due to the way the Javascript parser works, applying functions on numbers is
|
||||
// kind of weird. See examples for details.
|
||||
//
|
||||
// Examples
|
||||
//
|
||||
// 2.seconds()
|
||||
// #=> SyntaxError: Unexpected token ILLEGAL
|
||||
// //=> SyntaxError: Unexpected token ILLEGAL
|
||||
//
|
||||
// 10..seconds()
|
||||
// #=> 10000
|
||||
// //=> 10000
|
||||
//
|
||||
// (5).seconds()
|
||||
// #=> 5000
|
||||
// //=> 5000
|
||||
//
|
||||
// Returns an integer representing time in milliseconds
|
||||
Number.prototype.seconds = function() {
|
||||
|
@ -185,7 +167,7 @@
|
|||
// Examples
|
||||
//
|
||||
// 1.second()
|
||||
// #=> 1000
|
||||
// //=> 1000
|
||||
//
|
||||
// Returns an integer representing time in milliseconds
|
||||
Number.prototype.second = function() {
|
||||
|
@ -200,7 +182,7 @@
|
|||
// Examples
|
||||
//
|
||||
// 5..fromScale(0, 10)
|
||||
// #=> 0.5
|
||||
// //=> 0.5
|
||||
//
|
||||
// Returns an integer representing the scaled value
|
||||
Number.prototype.fromScale = function(start, end) {
|
||||
|
@ -215,11 +197,10 @@
|
|||
// Examples
|
||||
//
|
||||
// 0.5.toScale(0, 10)
|
||||
// #=> 5
|
||||
// //=> 5
|
||||
//
|
||||
// Returns an integer representing the scaled value
|
||||
Number.prototype.toScale = function(start, end) {
|
||||
|
||||
return Math.ceil(this * (Math.max(start, end) - Math.min(start, end)) + Math.min(start, end));
|
||||
};
|
||||
|
||||
}).call(this);
|
||||
|
|
Loading…
Reference in New Issue