210 need fix merge

This commit is contained in:
xixebombilla 2014-02-27 16:45:14 -06:00
commit 642c5c8b1e
19 changed files with 917 additions and 586 deletions

1
.rvmrc
View File

@ -1 +0,0 @@
rvm use 2.1.0@cylonjs.com --create

View File

@ -2,7 +2,7 @@
var Program = require('commander'),
Pkg = require('../package.json'),
CliCommands = require('../dist/cli/cli-commands'),
CliCommands = require('../lib/cli/cli-commands'),
fs = require('fs');
Program
@ -12,7 +12,7 @@ Program
Program
.command("generate <name>")
.description("Generates a new adaptor")
.action(function(name) { require('../src/generators/adaptor')(name) });
.action(function(name) { require('../lib/generators/adaptor')(name) });
Program
.command("scan <type>")
@ -36,7 +36,7 @@ Program
break;
case 'connect':
option = (option === null || option === undefined) ? '/dev/rfcomm0' : option;
CliCommands.connectToSerial(option, address);
CliCommands.bluetooth.connect(option, address);
break;
case 'scan':
CliCommands.bluetooth.scan();

View File

@ -24,7 +24,7 @@ namespace("Cylon", function() {
// name - name of the Adaptor, used when printing to console
// connection - Connection the adaptor will use to proxy commands/events
//
// Returns a new Adaptor
// Returns a new Adaptor
function Adaptor(opts) {
if (opts == null) {
opts = {};

View File

@ -77,19 +77,19 @@ namespace("Cylon", function() {
return _results;
})());
});
this.server.get("/robots/:robotname", function(req, res) {
return master.findRobot(req.params.robotname, function(err, robot) {
return res.json(err ? err : robot.data());
});
});
this.server.get("/robots/:robotname/commands", function(req, res) {
return master.findRobot(req.params.robotname, function(err, robot) {
return res.json(err ? err : robot.data().commands);
});
});
this.server.all("/robots/:robotname/commands/:commandname", function(req, res) {
var params;
params = _this.parseCommandParams(req);
@ -104,13 +104,13 @@ namespace("Cylon", function() {
});
});
});
this.server.get("/robots/:robotname/devices", function(req, res) {
return master.findRobot(req.params.robotname, function(err, robot) {
return res.json(err ? err : robot.data().devices);
});
});
this.server.get("/robots/:robotname/devices/:devicename", function(req, res) {
var devicename, robotname, _ref;
_ref = [req.params.robotname, req.params.devicename], robotname = _ref[0], devicename = _ref[1];
@ -118,7 +118,7 @@ namespace("Cylon", function() {
return res.json(err ? err : device.data());
});
});
this.server.get("/robots/:robotname/devices/:devicename/commands", function(req, res) {
var devicename, robotname, _ref;
_ref = [req.params.robotname, req.params.devicename], robotname = _ref[0], devicename = _ref[1];
@ -126,7 +126,7 @@ namespace("Cylon", function() {
return res.json(err ? err : device.data().commands);
});
});
this.server.all("/robots/:robot/devices/:device/commands/:commandname", function(req, res) {
var commandname, devicename, params, robotname;
params = [req.params.robot, req.params.device, req.params.commandname];
@ -143,13 +143,13 @@ namespace("Cylon", function() {
});
});
});
this.server.get("/robots/:robotname/connections", function(req, res) {
return master.findRobot(req.params.robotname, function(err, robot) {
return res.json(err ? err : robot.data().connections);
});
});
this.server.get("/robots/:robot/connections/:connection", function(req, res) {
var connectionname, robotname, _ref;
_ref = [req.params.robot, req.params.connection], robotname = _ref[0], connectionname = _ref[1];
@ -157,11 +157,11 @@ namespace("Cylon", function() {
return res.json(err ? err : connection.data());
});
});
this.server.get("/robots/:robotname/devices/:devicename/events", function(req, res) {
return req.io.route('events');
});
return this.server.io.route('events', function(req) {
var devicename, robotname, _ref;
_ref = [req.params.robotname, req.params.devicename], robotname = _ref[0], devicename = _ref[1];

View File

@ -1,44 +1,61 @@
(function() {
var bluetooth, os, scan;
require("./process");
require("./process");
var bluetooth,
scan = require("./scan"),
os = require('os');
scan = require("./scan");
bluetooth = {
pair: function(hciX, address) {
var process = new Cylon.Process,
platform = os.platform();
os = require('os');
bluetooth = {
pair: function(hciX, address) {
var platform, process;
process = new Cylon.Process;
platform = os.platform();
switch (platform) {
case 'linux':
return process.spawn('bluez-simple-agent', [hciX, address]);
case 'darwin':
return console.log("OS X manages Bluetooth pairing itself.");
default:
return console.log("OS not yet supported.");
}
},
unpair: function(hciX, address) {
var platform, process;
process = new Cylon.Process;
platform = os.platform();
switch (platform) {
case 'linux':
return process.spawn('bluez-simple-agent', [hciX, address, 'remove']);
case 'darwin':
return console.log("OS X manages Bluetooth unpairing itself.");
default:
return console.log("OS not yet supported.");
}
},
scan: function() {
return scan('bluetooth');
switch (platform) {
case 'linux':
process.spawn('bluez-simple-agent', [hciX, address]);
break;
case 'darwin':
console.log("OS X manages Bluetooth pairing itself.");
break;
default:
console.log("OS not yet supported.");
}
};
},
module.exports = bluetooth;
unpair: function(hciX, address) {
var process = new Cylon.Process,
platform = os.platform();
}).call(this);
switch (platform) {
case 'linux':
process.spawn('bluez-simple-agent', [hciX, address, 'remove']);
break;
case 'darwin':
console.log("OS X manages Bluetooth unpairing itself.");
break;
default:
console.log("OS not yet supported.");
}
},
connect: function(dev, address) {
var process = new Cylon.Process,
platform = os.platform();
switch (platform) {
case 'linux':
process.spawn('sudo', ['rfcomm', 'connect', dev, address, '1']);
break;
case 'darwin':
console.log("OS X manages binding itself.");
break;
default:
console.log("OS not yet supported");
}
},
scan: function() {
scan('bluetooth');
}
};
module.exports = bluetooth;

View File

@ -1,22 +1,17 @@
(function() {
var bluetooth, cliCommands, connectToSerial, scan;
var bluetooth, cliCommands, connectToSerial, scan;
scan = require('./scan');
var cliCommands,
scan = require('./scan'),
bluetooth = require('./bluetooth'),
connectToSerial = require('./connect-to-serial');
cliCommands = {
scan: scan,
bluetooth: {
pair: bluetooth.pair,
unpair: bluetooth.unpair,
scan: bluetooth.scan,
connect: bluetooth.connect
}
};
bluetooth = require('./bluetooth');
cliCommands = {
scan: scan,
connectToSerial: connectToSerial,
bluetooth: {
pair: bluetooth.pair,
unpair: bluetooth.unpair,
scan: bluetooth.scan
}
};
module.exports = cliCommands;
}).call(this);
module.exports = cliCommands;

View File

@ -1,24 +0,0 @@
(function() {
var connectToSerial, os;
require("./process");
os = require('os');
connectToSerial = function(dev, address) {
var platform, process;
process = new Cylon.Process;
platform = os.platform();
switch (platform) {
case 'linux':
return process.spawn('sudo', ['rfcomm', 'connect', dev, address, '1']);
case 'darwin':
return console.log("OS X manages binding itself.");
default:
return console.log("OS not yet supported");
}
};
module.exports = connectToSerial;
}).call(this);

View File

@ -1,53 +1,48 @@
(function() {
var exec, namespace, spawn;
var spawn = require('child_process').spawn,
exec = require('child_process').exec,
namespace = require('node-namespace');
spawn = require('child_process').spawn;
namespace("Cylon", function() {
this.Process = (function() {
function Process() {}
exec = require('child_process').exec;
Process.prototype.exec = function(command) {
exec(command, function(err, stdout, stderr) {
if (stdout != null) {
console.log(stdout);
}
if (stderr != null) {
console.log(stderr);
}
if (err != null) {
console.log(err);
}
});
};
namespace = require('node-namespace');
Process.prototype.spawn = function(command, args) {
var cmd;
namespace("Cylon", function() {
return this.Process = (function() {
function Process() {}
cmd = spawn(command, args, {
stdio: 'inherit'
});
Process.prototype.exec = function(command) {
exec(command, function(err, stdout, stderr) {
if (stdout != null) {
console.log(stdout);
}
if (stderr != null) {
console.log(stderr);
}
if (err != null) {
return console.log(err);
}
});
return true;
};
cmd.on('close', function(code) {
if (code !== 0) {
return console.log("ps process exited with code " + code);
}
});
Process.prototype.spawn = function(command, args) {
var cmd;
cmd = spawn(command, args, {
stdio: 'inherit'
});
cmd.on('close', function(code) {
if (code !== 0) {
return console.log("ps process exited with code " + code);
}
});
return cmd.on('exit', function(code) {
if (code !== 0) {
return console.log("ps process exited with code " + code);
}
});
};
cmd.on('exit', function(code) {
if (code !== 0) {
return console.log("ps process exited with code " + code);
}
});
};
return Process;
return Process;
})();
});
})();
});
module.exports = Cylon.Process;
}).call(this);
module.exports = Cylon.Process;

View File

@ -1,34 +1,35 @@
(function() {
var os, scan;
require("./process");
require("./process");
var scan,
os = require('os');
os = require('os');
scan = function(type) {
var process = new Cylon.Process,
platform = os.platform();
scan = function(type) {
var platform, process;
process = new Cylon.Process;
platform = os.platform();
switch (platform) {
case 'linux':
switch (type) {
case 'serial':
return process.exec("dmesg | grep tty");
case 'bluetooth':
return process.exec("hcitool scan");
case 'usb':
return process.exec("lsusb");
default:
return console.log("Device type not yet supported.");
}
break;
case 'darwin':
return process.exec("ls /dev/{tty,cu}.*");
default:
return console.log("OS not yet supported.");
}
};
switch (platform) {
case 'linux':
switch (type) {
case 'serial':
process.exec("dmesg | grep tty");
break;
case 'bluetooth':
process.exec("hcitool scan");
break;
case 'usb':
process.exec("lsusb");
break;
default:
console.log("Device type not yet supported.");
break;
}
break;
case 'darwin':
process.exec("ls /dev/{tty,cu}.*");
break;
default:
console.log("OS not yet supported.");
}
};
module.exports = scan;
}).call(this);
module.exports = scan;

View File

@ -6,35 +6,67 @@
* Licensed under the Apache 2.0 license.
*/
"use strict";
<<<<<<< HEAD
(function() {
'use strict';
var Cylon,
__slice = [].slice,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
=======
>>>>>>> c39f448d14c27cdd9a1524a11c57a5c01a345b3c
require('./config');
require('./config');
require('./utils');
require('./logger');
require('./driver');
require('./adaptor');
require('./utils');
Logger.setup();
require('./logger');
// Cylon is the global namespace for the project, and also in charge of
// maintaining the Master singleton that controls all the robots.
var Cylon = (function() {
function Cylon() {}
require('./driver');
var instance = null;
require('./adaptor');
Logger.setup();
// Public: Fetches singleton instance of Master, or creates a new one if it
// doesn't already exist
//
// Returns a Master instance
Cylon.getInstance = function() {
return new Master();
};
<<<<<<< HEAD
// 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 bind = function(fn, me) {
return function() { return fn.apply(me, arguments); }
}
>>>>>>> c39f448d14c27cdd9a1524a11c57a5c01a345b3c
function Cylon() {}
// The Master class is our puppeteer that manages all the robots, as well as
// starting them and the API.
var Master = (function() {
var robots = [],
api = null,
api_config = { host: '127.0.0.1', port: '3000' };
instance = null;
// Public: Creates a new Master
//
// Returns a Master instance
function Master() {
this.robot = bind(this.robot, this);
<<<<<<< HEAD
// Public: Fetches singleton instance of Master, or creates a new one if it
// doesn't already exist
//
@ -83,9 +115,22 @@
process.on("SIGINT", function() {
Cylon.getInstance().stop();
return process.kill();
=======
this.self = this;
if (process.platform === "win32") {
var readline = require("readline");
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
>>>>>>> c39f448d14c27cdd9a1524a11c57a5c01a345b3c
});
rl.on("SIGINT", function() { return process.emit("SIGINT"); });
}
<<<<<<< HEAD
// Public: Creates a new Robot
//
// opts - hash of Robot attributes
@ -240,10 +285,126 @@
for (_i = 0, _len = robots.length; _i < _len; _i++) {
robot = robots[_i];
_results.push(robot.stop());
}
return _results;
};
=======
process.on("SIGINT", function() {
Cylon.getInstance().stop();
process.kill();
});
}
// Public: Creates a new Robot
//
// opts - hash of Robot attributes
//
// Returns a shiny new Robot
// Examples:
// Cylon.robot
// connection: { name: 'arduino', adaptor: 'firmata' }
// device: { name: 'led', driver: 'led', pin: 13 }
//
// work: (me) ->
// me.led.toggle()
Master.prototype.robot = function(opts) {
var Robot = require("./robot");
opts.master = this;
var robot = new Robot(opts);
robots.push(robot);
return robot;
};
// Public: Returns all Robots the Master knows about
//
// Returns an array of all Robot instances
Master.prototype.robots = function() {
return robots;
};
// Public: Configures the API host and port based on passed options
//
// opts - object containing API options
// host - host address API should serve from
// port - port API should listen for requests on
//
// Returns the API configuration
Master.prototype.api = function(opts) {
if (opts == null) { opts = {}; }
api_config.host = opts.host || "127.0.0.1";
api_config.port = opts.port || "3000";
return api_config;
};
// Public: Finds a particular robot by name
//
// name - name of the robot to find
// callback - optional callback to be executed
//
// Returns the found Robot or result of the callback if it's supplied
Master.prototype.findRobot = function(name, callback) {
var error,
robot = null;
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 };
}
return callback ? callback(error, robot) : robot;
};
// Public: Finds a particular Robot's device by name
//
// robotid - name of the robot to find
// deviceid - name of the device to find
// callback - optional callback to be executed
//
// 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 error,
device = null;
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 + "." };
}
return callback ? callback(error, device) : device;
});
};
// Public: Finds a particular Robot's connection by name
//
// robotid - name of the robot to find
// connid - name of the device to find
// callback - optional callback to be executed
//
// 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 error,
connection = null;
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 + "." };
>>>>>>> c39f448d14c27cdd9a1524a11c57a5c01a345b3c
}
<<<<<<< HEAD
// Creates a new instance of the Cylon API server, or returns the
// already-existing one.
//
@ -254,15 +415,50 @@
api_config.master = this.self;
return api != null ? api : api = new server(api_config);
};
=======
return callback ? callback(error, connection) : connection;
});
};
return Master;
// Public: Starts up the API and the robots
//
// Returns nothing
Master.prototype.start = function() {
this.startAPI();
})();
for (var i = 0; i < robots.length; i++) {
var robot = robots[i];
robot.start();
}
};
>>>>>>> c39f448d14c27cdd9a1524a11c57a5c01a345b3c
return Cylon;
// Public: Stops the API and the robots
//
// Returns nothing
Master.prototype.stop = function() {
for (var i = 0; i < robots.length; i++) {
var robot = robots[i];
robot.stop();
}
};
}).call(this);
// Creates a new instance of the Cylon API server, or returns the
// already-existing one.
//
// Returns an Cylon.ApiServer instance
Master.prototype.startAPI = function() {
var Server = require('./api');
api_config.master = this.self;
return api != null ? api : api = new Server(api_config);
};
module.exports = Cylon.getInstance();
return Master;
})();
return Cylon;
}).call(this);
module.exports = Cylon.getInstance();

View File

@ -7,13 +7,22 @@
*/
(function() {
var BasicLogger, NullLogger,
__slice = [].slice;
"use strict";
var getArgs = function(args) {
return args.length >= 1 ? [].slice.call(args, 0) : [];
}
>>>>>>> c39f448d14c27cdd9a1524a11c57a5c01a345b3c
// 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
// well as in external modules that are loaded into Cylon
<<<<<<< HEAD
global.Logger = {
// Public: Creates a Logger instance and assigns it to @logger
@ -55,73 +64,133 @@
var args, _ref;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return (_ref = this.logger).fatal.apply(_ref, args);
}
};
=======
global.Logger = {
// Public: Creates a Logger instance and assigns it to @logger
//
// logger - logger object to use. Defaults to a BasicLogger, or a NullLogger if
// false is supplied
//
// Returns the new logger instance
setup: function(logger) {
if (logger == null) { logger = new BasicLogger; }
if (logger === false) {
this.logger = new NullLogger();
} else {
this.logger = logger;
>>>>>>> c39f448d14c27cdd9a1524a11c57a5c01a345b3c
}
<<<<<<< HEAD
// The BasicLogger pushes stuff to console.log. Nothing more, nothing less.
BasicLogger = (function() {
function BasicLogger() {}
=======
return this.logger
},
BasicLogger.prototype.toString = function() {
return "BasicLogger";
};
toString: function() { return this.logger.toString(); },
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)));
};
debug: function() {
var args = getArgs(arguments);
return this.logger.debug.apply(this.logger, args);
},
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)));
};
info: function() {
var args = getArgs(arguments);
return this.logger.info.apply(this.logger, args);
},
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)));
};
warn: function() {
var args = getArgs(arguments);
return this.logger.warn.apply(this.logger, args);
},
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)));
};
error: function() {
var args = getArgs(arguments);
return this.logger.error.apply(this.logger, args);
},
>>>>>>> c39f448d14c27cdd9a1524a11c57a5c01a345b3c
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)));
};
fatal: function() {
var args = getArgs(arguments);
return this.logger.fatal.apply(this.logger, args);
}
};
return BasicLogger;
// The BasicLogger pushes stuff to console.log. Nothing more, nothing less.
var BasicLogger = (function() {
function BasicLogger() {}
})();
BasicLogger.prototype.toString = function() { return "BasicLogger"; };
BasicLogger.prototype.debug = function() {
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 = 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 = getArgs(arguments),
string = ["W, [" + (new Date().toISOString()) + "] WARN -- :"],
data = string.concat(args.slice());
<<<<<<< HEAD
// The NullLogger is designed for cases where you want absolutely nothing to
// print to anywhere. Every proxied method from the Logger returns a noo
NullLogger = (function() {
function NullLogger() {}
=======
return console.log.apply(console, data);
};
>>>>>>> c39f448d14c27cdd9a1524a11c57a5c01a345b3c
NullLogger.prototype.toString = function() {
return "NullLogger";
};
BasicLogger.prototype.error = function() {
var args = getArgs(arguments),
string = ["E, [" + (new Date().toISOString()) + "] ERROR -- :"],
data = string.concat(args.slice());
NullLogger.prototype.debug = function() {};
return console.log.apply(console, data);
};
NullLogger.prototype.info = function() {};
BasicLogger.prototype.fatal = function() {
var args = getArgs(arguments),
string = ["F, [" + (new Date().toISOString()) + "] FATAL -- :"],
data = string.concat(args.slice());
NullLogger.prototype.warn = function() {};
return console.log.apply(console, data);
};
NullLogger.prototype.error = function() {};
return BasicLogger;
NullLogger.prototype.fatal = function() {};
})();
return NullLogger;
// The NullLogger is designed for cases where you want absolutely nothing to
// print to anywhere. Every proxied method from the Logger returns a noop.
var NullLogger = (function() {
function NullLogger() {}
})();
NullLogger.prototype.toString = function() { return "NullLogger"; };
}).call(this);
NullLogger.prototype.debug = function() {};
NullLogger.prototype.info = function() {};
NullLogger.prototype.warn = function() {};
NullLogger.prototype.error = function() {};
NullLogger.prototype.fatal = function() {};
return NullLogger;
})();

View File

@ -7,6 +7,7 @@
*/
(function() {
var __slice = [].slice;
@ -123,19 +124,114 @@
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return target[method].apply(target, args);
};
=======
// Public: Alias to setInterval, combined with Number monkeypatches below to
// create an artoo-like syntax.
//
// interval - interval to run action on
// action - action to perform at interval
//
// Examples
//
// every 5.seconds(), -> console.log("hello world (and again in 5 seconds)!")
//
// Returns an interval
global.every = function(interval, action) {
return setInterval(action, interval);
};
// Public: Alias to setTimeout, combined with Number monkeypatches below to
// create an artoo-like syntax.
//
// interval - interval to run action on
// action - action to perform at interval
//
// Examples
//
// after 10.seconds(), -> console.log("hello world from ten seconds ago!")
//
// Returns an interval
global.after = function(delay, action) {
return setTimeout(action, delay);
};
// Public: Alias to the `every` function, but passing 0
// Examples
//
// constantly -> console.log("hello world (and again and again)!")
//
// Returns an interval
global.constantly = function(action) {
return every(0, action);
};
// Public: Sleep - do nothing for some duration of time.
//
// ms - number of ms to sleep for
//
// Returns a function
// Examples:
// sleep 1.second()
global.sleep = function(ms) {
var start = Date.now();
while(Date.now() < start + ms) {
var i = 0;
}
};
// Copies
global.slice = [].slice;
global.hasProp = {}.hasOwnProperty;
// Public: Function to use for class inheritance. Copy of a CoffeeScript helper
// function.
//
// Example
//
// var Sphero = (function(klass) {
// subclass(Sphero, klass);
// // Sphero is now a subclass of Parent, and can access it's methods through
// // Sphero.__super__
// })(Parent);
//
// Returns subclass
global.subclass = function(child, parent) {
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;
return child;
};
// Public: Proxies a list of methods from one object to another. It will not
// overwrite existing methods unless told to.
//
// methods - array of functions to proxy
// target - object to proxy the functions to
// base - (optional) object that proxied functions will be declared on. Defaults
// to this
// force - (optional) boolean - whether or not to force method assignment
//
// Returns base
global.proxyFunctionsToObject = function(methods, target, base, force) {
if (base == null) { base = this; }
if (force == null) { force = false; }
var fn = function(method) {
return base[method] = function() {
var args = arguments.length >= 1 ? [].slice.call(arguments, 0) : [];
return target[method].apply(target, args);
>>>>>>> c39f448d14c27cdd9a1524a11c57a5c01a345b3c
};
for (_i = 0, _len = methods.length; _i < _len; _i++) {
method = methods[_i];
if (!force) {
if (typeof base[method] === 'function') {
continue;
}
}
_fn(method);
}
return base;
};
<<<<<<< HEAD
// Public: Proxies a list of methods for test stubbing.
//
// methods - array of functions to proxy
@ -221,5 +317,98 @@
Number.prototype.toScale = function(start, end) {
return Math.ceil(this * (Math.max(start, end) - Math.min(start, end)) + Math.min(start, end));
};
=======
for (var i = 0; i < methods.length; i++) {
var method = methods[i];
if (!force) {
if (typeof base[method] === 'function') { continue; }
}
>>>>>>> c39f448d14c27cdd9a1524a11c57a5c01a345b3c
}).call(this);
fn(method);
}
return base;
};
// Public: Proxies a list of methods for test stubbing.
//
// methods - array of functions to proxy
// base - (optional) object that proxied functions will be declared on. Defaults
// to this
//
// Returns base
global.proxyTestStubs = function(methods, base) {
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,
// 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
//
// 10..seconds()
// //=> 10000
//
// (5).seconds()
// //=> 5000
//
// Returns an integer representing time in milliseconds
Number.prototype.seconds = function() {
return this * 1000;
};
// Public: Alias for Number::seconds, see comments for that method
//
// Examples
//
// 1.second()
// //=> 1000
//
// Returns an integer representing time in milliseconds
Number.prototype.second = function() {
return this.seconds(this);
};
// Public: Convert value from old scale (start, end) to (0..1) scale
//
// start - low point of scale to convert value from
// end - high point of scale to convert value from
//
// Examples
//
// 5..fromScale(0, 10)
// //=> 0.5
//
// Returns an integer representing the scaled value
Number.prototype.fromScale = function(start, end) {
return (this - Math.min(start, end)) / (Math.max(start, end) - Math.min(start, end));
};
// Public: Convert value from (0..1) scale to new (start, end) scale
//
// start - low point of scale to convert value to
// end - high point of scale to convert value to
//
// Examples
//
// 0.5.toScale(0, 10)
// //=> 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));
};

View File

@ -1,37 +0,0 @@
require "./process"
scan = require "./scan"
os = require('os')
bluetooth =
pair: (hciX, address) ->
process = new Cylon.Process
platform = os.platform()
switch platform
when 'linux'
process.spawn 'bluez-simple-agent', [hciX, address]
when 'darwin'
console.log "OS X manages Bluetooth pairing itself."
else
console.log "OS not yet supported."
unpair: (hciX, address) ->
process = new Cylon.Process
platform = os.platform()
switch platform
when 'linux'
process.spawn 'bluez-simple-agent', [hciX, address, 'remove']
when 'darwin'
console.log "OS X manages Bluetooth unpairing itself."
else
console.log "OS not yet supported."
scan: () ->
scan('bluetooth')
module.exports = bluetooth

View File

@ -1,13 +0,0 @@
scan = require('./scan')
connectToSerial = require('./connect-to-serial')
bluetooth = require('./bluetooth')
cliCommands =
scan: scan
connectToSerial: connectToSerial
bluetooth:
pair: bluetooth.pair
unpair: bluetooth.unpair
scan: bluetooth.scan
module.exports = cliCommands

View File

@ -1,18 +0,0 @@
require "./process"
os = require 'os'
connectToSerial = (dev, address) ->
process = new Cylon.Process
platform = os.platform()
switch platform
when 'linux'
process.spawn 'sudo', ['rfcomm', 'connect', dev, address, '1']
when 'darwin'
console.log "OS X manages binding itself."
else
console.log "OS not yet supported"
module.exports = connectToSerial

View File

@ -1,24 +0,0 @@
spawn = require('child_process').spawn
exec = require('child_process').exec
namespace = require 'node-namespace'
namespace "Cylon", ->
class @Process
exec: (command) ->
exec command, (err, stdout, stderr) ->
console.log(stdout) if stdout?
console.log(stderr) if stderr?
console.log(err) if err?
true
spawn: (command, args) ->
cmd = spawn command, args, { stdio: 'inherit' }
cmd.on 'close', (code) ->
console.log "ps process exited with code #{code}" if code isnt 0
cmd.on 'exit', (code) ->
console.log "ps process exited with code #{code}" if code isnt 0
module.exports = Cylon.Process

View File

@ -1,29 +0,0 @@
require "./process"
os = require('os')
scan = (type) ->
process = new Cylon.Process
platform = os.platform()
switch platform
when 'linux'
switch type
when 'serial'
process.exec "dmesg | grep tty"
when 'bluetooth'
process.exec "hcitool scan"
when 'usb'
process.exec "lsusb"
else
console.log "Device type not yet supported."
when 'darwin'
process.exec "ls /dev/{tty,cu}.*"
else
console.log "OS not yet supported."
module.exports = scan

View File

@ -1,51 +1,56 @@
(function() {
'use strict';
source("connection");
'use strict';
source("adaptor");
source("connection");
source("adaptor");
source("robot");
source("test/loopback");
source("robot");
describe("Connection", function() {
var adaptor, connection, initAdaptor, robot;
source("test/loopback");
describe("Connection", function() {
var adaptor, connection, initAdaptor, robot;
robot = new Cylon.Robot({
name: 'me'
});
adaptor = new Cylon.Adaptors.Loopback({
name: 'loopy'
});
initAdaptor = sinon.stub(robot, 'initAdaptor').returns(adaptor);
connection = new Cylon.Connection({
name: "connective",
adaptor: "loopback",
robot: robot
});
it("belongs to a robot", function() {
return connection.robot.name.should.be.equal('me');
});
it("has a name", function() {
return connection.name.should.be.equal('connective');
});
it("has an adaptor", function() {
return connection.adaptor.name.should.be.equal('loopy');
});
it("can init an external adaptor module", function() {
return initAdaptor.should.be.called;
});
it("can connect to adaptor", function() {
var adaptorConnect;
adaptorConnect = sinon.stub(adaptor, 'connect').returns(true);
connection.connect();
return adaptorConnect.should.be.called;
});
return it("can disconnect from adaptor", function() {
var adaptorDisconnect;
adaptorDisconnect = sinon.stub(adaptor, 'disconnect').returns(true);
connection.disconnect();
return adaptorDisconnect.should.be.called;
});
robot = new Cylon.Robot({
name: 'me'
});
}).call(this);
adaptor = new Cylon.Adaptors.Loopback({
name: 'loopy'
});
initAdaptor = sinon.stub(robot, 'initAdaptor').returns(adaptor);
connection = new Cylon.Connection({
name: "connective",
adaptor: "loopback",
robot: robot
});
it("belongs to a robot", function() {
connection.robot.name.should.be.equal('me');
});
it("has a name", function() {
connection.name.should.be.equal('connective');
});
it("has an adaptor", function() {
connection.adaptor.name.should.be.equal('loopy');
});
it("can init an external adaptor module", function() {
initAdaptor.should.be.called;
});
it("can connect to adaptor", function() {
var adaptorConnect;
adaptorConnect = sinon.stub(adaptor, 'connect').returns(true);
connection.connect();
adaptorConnect.should.be.called;
});
it("can disconnect from adaptor", function() {
var adaptorDisconnect;
adaptorDisconnect = sinon.stub(adaptor, 'disconnect').returns(true);
connection.disconnect();
adaptorDisconnect.should.be.called;
});
});

View File

@ -1,196 +1,206 @@
(function() {
'use strict';
var Connection, Cylon, Device, Driver, Robot,
__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; };
'use strict';
var Connection, Cylon, Device, Driver, Robot,
__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; };
Cylon = source("cylon");
Cylon = source("cylon");
Robot = source('robot');
Device = source('device');
Driver = source('driver');
Connection = source('connection');
Robot = source('robot');
Device = source('device');
Driver = source('driver');
Connection = source('connection');
describe("Cylon", function() {
it("should create a robot", function() {
var robot;
assert(__indexOf.call(Object.keys(Cylon), 'robot') >= 0);
robot = Cylon.robot({
name: 'caprica six'
});
return robot.name.should.be.eql('caprica six');
describe("Cylon", function() {
it("should create a robot", function() {
var robot;
assert(__indexOf.call(Object.keys(Cylon), 'robot') >= 0);
robot = Cylon.robot({
name: 'caprica six'
});
describe('#api', function() {
describe('without arguments', function() {
return it("returns the current API configuration", function() {
var api_config;
api_config = Cylon.api();
assert(__indexOf.call(Object.keys(api_config), 'host') >= 0);
return assert(__indexOf.call(Object.keys(api_config), 'port') >= 0);
robot.name.should.be.eql('caprica six');
});
describe('#api', function() {
describe('without arguments', function() {
it("returns the current API configuration", function() {
var api_config;
api_config = Cylon.api();
assert(__indexOf.call(Object.keys(api_config), 'host') >= 0);
assert(__indexOf.call(Object.keys(api_config), 'port') >= 0);
});
});
describe('with a host and port', function() {
it('sets the API configuration to what was specified', function() {
var api_config;
api_config = Cylon.api({
host: '0.0.0.0',
port: '8888'
});
api_config.host.should.be.eql("0.0.0.0");
api_config.port.should.be.eql("8888");
});
});
});
describe("#robots", function() {
it("returns an array of all robots", function() {
var robot, robots, _i, _len, _results;
robots = Cylon.robots();
assert(robots instanceof Array);
_results = [];
for (_i = 0, _len = robots.length; _i < _len; _i++) {
robot = robots[_i];
_results.push(assert(robot instanceof Robot));
}
return _results;
});
});
describe("#findRobot", function() {
describe("synchronous", function() {
describe("with a valid robot name", function() {
it("returns the robot", function() {
var robot;
robot = Cylon.findRobot("caprica six");
assert(robot instanceof Robot);
robot.name.should.be.equal("caprica six");
});
});
return describe('with a host and port', function() {
return it('sets the API configuration to what was specified', function() {
var api_config;
api_config = Cylon.api({
host: '0.0.0.0',
port: '8888'
});
api_config.host.should.be.eql("0.0.0.0");
return api_config.port.should.be.eql("8888");
describe("with an invalid robot name", function() {
it("returns null", function() {
var robot;
robot = Cylon.findRobot("Tom Servo");
assert(robot === null);
});
});
});
describe("#robots", function() {
return it("returns an array of all robots", function() {
var robot, robots, _i, _len, _results;
robots = Cylon.robots();
assert(robots instanceof Array);
_results = [];
for (_i = 0, _len = robots.length; _i < _len; _i++) {
robot = robots[_i];
_results.push(assert(robot instanceof Robot));
}
return _results;
});
});
describe("#findRobot", function() {
describe("synchronous", function() {
describe("with a valid robot name", function() {
return it("returns the robot", function() {
var robot;
robot = Cylon.findRobot("caprica six");
describe("async", function() {
describe("with a valid robot name", function() {
it("passes the robot and an empty error to the callback", function() {
return Cylon.findRobot("caprica six", function(error, robot) {
assert(error === void 0);
assert(robot instanceof Robot);
return robot.name.should.be.equal("caprica six");
});
});
return describe("with an invalid robot name", function() {
return it("returns null", function() {
var robot;
robot = Cylon.findRobot("Tom Servo");
return assert(robot === null);
robot.name.should.be.equal("caprica six");
});
});
});
return describe("async", function() {
describe("with a valid robot name", function() {
return it("passes the robot and an empty error to the callback", function() {
return Cylon.findRobot("caprica six", function(error, robot) {
assert(error === void 0);
assert(robot instanceof Robot);
return robot.name.should.be.equal("caprica six");
});
});
});
return describe("with an invalid robot name", function() {
return it("passes no robot and an error message to the callback", function() {
return Cylon.findRobot("Tom Servo", function(error, robot) {
assert(robot === null);
assert(typeof error === 'object');
return error.error.should.be.eql("No Robot found with the name Tom Servo");
});
});
});
});
});
describe("#findRobotDevice", function() {
var crow;
crow = Cylon.robot({
name: "Crow",
device: {
name: 'testDevice',
driver: 'ping'
}
});
describe("synchronous", function() {
describe("with a valid robot and device name", function() {
return it("returns the device", function() {
var device;
device = Cylon.findRobotDevice("Crow", "testDevice");
assert(device instanceof Device);
return device.name.should.be.equal("testDevice");
});
});
return describe("with an invalid device name", function() {
return it("returns null", function() {
var device;
device = Cylon.findRobotDevice("Crow", "madethisup");
return assert(device === null);
});
});
});
return describe("async", function() {
describe("with a valid robot and device name", function() {
return it("passes the device and an empty error to the callback", function() {
return Cylon.findRobotDevice("Crow", "testDevice", function(error, device) {
assert(error === void 0);
assert(device instanceof Device);
return device.name.should.be.equal("testDevice");
});
});
});
return describe("with an invalid device name", function() {
return it("passes no device and an error message to the callback", function() {
return Cylon.findRobotDevice("Crow", "madethisup", function(err, device) {
assert(device === null);
assert(typeof err === 'object');
return err.error.should.be.eql("No device found with the name madethisup.");
});
});
});
});
});
return describe("#findRobotConnection", function() {
var ultron;
ultron = Cylon.robot({
name: "Ultron",
connection: {
name: 'loopback',
adaptor: 'loopback'
}
});
describe("synchronous", function() {
describe("with a valid robot and connection name", function() {
return it("returns the connection", function() {
var connection;
connection = Cylon.findRobotConnection("Ultron", "loopback");
assert(connection instanceof Connection);
return connection.name.should.be.equal("loopback");
});
});
return describe("with an invalid connection name", function() {
return it("returns null", function() {
var connection;
connection = Cylon.findRobotConnection("Ultron", "madethisup");
return assert(connection === null);
});
});
});
return describe("async", function() {
describe("with a valid robot and connection name", function() {
return it("passes the connection and an empty error to the callback", function() {
return Cylon.findRobotConnection("Ultron", "loopback", function(error, conn) {
assert(error === void 0);
assert(conn instanceof Connection);
return conn.name.should.be.equal("loopback");
});
});
});
return describe("with an invalid connection name", function() {
return it("passes no connection and an error message to the callback", function() {
return Cylon.findRobotConnection("Ultron", "madethisup", function(err, conn) {
var message;
assert(conn === null);
assert(typeof err === 'object');
message = "No connection found with the name madethisup.";
return err.error.should.be.eql(message);
});
describe("with an invalid robot name", function() {
it("passes no robot and an error message to the callback", function() {
return Cylon.findRobot("Tom Servo", function(error, robot) {
assert(robot === null);
assert(typeof error === 'object');
error.error.should.be.eql("No Robot found with the name Tom Servo");
});
});
});
});
});
}).call(this);
describe("#findRobotDevice", function() {
var crow;
crow = Cylon.robot({
name: "Crow",
device: {
name: 'testDevice',
driver: 'ping'
}
});
describe("synchronous", function() {
describe("with a valid robot and device name", function() {
it("returns the device", function() {
var device;
device = Cylon.findRobotDevice("Crow", "testDevice");
assert(device instanceof Device);
device.name.should.be.equal("testDevice");
});
});
describe("with an invalid device name", function() {
it("returns null", function() {
var device;
device = Cylon.findRobotDevice("Crow", "madethisup");
assert(device === null);
});
});
});
describe("async", function() {
describe("with a valid robot and device name", function() {
it("passes the device and an empty error to the callback", function() {
return Cylon.findRobotDevice("Crow", "testDevice", function(error, device) {
assert(error === void 0);
assert(device instanceof Device);
device.name.should.be.equal("testDevice");
});
});
});
describe("with an invalid device name", function() {
it("passes no device and an error message to the callback", function() {
return Cylon.findRobotDevice("Crow", "madethisup", function(err, device) {
assert(device === null);
assert(typeof err === 'object');
err.error.should.be.eql("No device found with the name madethisup.");
});
});
});
});
});
describe("#findRobotConnection", function() {
var ultron;
ultron = Cylon.robot({
name: "Ultron",
connection: {
name: 'loopback',
adaptor: 'loopback'
}
});
describe("synchronous", function() {
describe("with a valid robot and connection name", function() {
it("returns the connection", function() {
var connection;
connection = Cylon.findRobotConnection("Ultron", "loopback");
assert(connection instanceof Connection);
connection.name.should.be.equal("loopback");
});
});
describe("with an invalid connection name", function() {
it("returns null", function() {
var connection;
connection = Cylon.findRobotConnection("Ultron", "madethisup");
assert(connection === null);
});
});
});
describe("async", function() {
describe("with a valid robot and connection name", function() {
it("passes the connection and an empty error to the callback", function() {
return Cylon.findRobotConnection("Ultron", "loopback", function(error, conn) {
assert(error === void 0);
assert(conn instanceof Connection);
conn.name.should.be.equal("loopback");
});
});
});
describe("with an invalid connection name", function() {
it("passes no connection and an error message to the callback", function() {
return Cylon.findRobotConnection("Ultron", "madethisup", function(err, conn) {
var message;
assert(conn === null);
assert(typeof err === 'object');
message = "No connection found with the name madethisup.";
err.error.should.be.eql(message);
});
});
});
});
});
});