Merge pull request #213 from hybridgroup/refactor/threepio

Threepio Support
This commit is contained in:
Ron Evans 2014-07-19 01:08:19 +02:00
commit 8762820d0c
10 changed files with 78 additions and 49 deletions

View File

@ -43,26 +43,17 @@ var API = module.exports = function API(opts) {
// extracts command params from request // extracts command params from request
this.express.use(function(req, res, next) { this.express.use(function(req, res, next) {
var method = req.method.toLowerCase(),
container = {};
req.commandParams = []; req.commandParams = [];
if (method === 'get' || Object.keys(req.query).length > 0) { for (var p in req.body) {
container = req.query; req.commandParams.push(req.body[p]);
} else if (typeof(req.body) === 'object') {
container = req.body;
}
for (var p in container) {
req.commandParams.push(container[p]);
} }
return next(); return next();
}); });
// load route definitions // load route definitions
this.express.use('/', require('./api/routes')); this.express.use('/api', require('./api/routes'));
}; };
API.prototype.defaults = { API.prototype.defaults = {

View File

@ -41,37 +41,44 @@ var load = function load(req, res, next) {
next(); next();
}; };
router.get("/", function(req, res) {
res.json({ MCP: Cylon });
});
router.get("/commands", function(req, res) {
res.json({ commands: Object.keys(Cylon.commands) });
});
router.post("/commands/:command", function(req, res) {
var command = req.params.command;
var result = Cylon.commands[command].apply(Cylon, req.commandParams);
res.json({ result: result });
});
router.get("/robots", function(req, res) { router.get("/robots", function(req, res) {
var data = []; res.json({ robots: Cylon.toJSON().robots });
for (var bot in Cylon.robots) {
data.push(Cylon.robots[bot]);
}
res.json(data);
}); });
router.get("/robots/:robot", load, function(req, res) { router.get("/robots/:robot", load, function(req, res) {
res.json(req.robot); res.json({ robot: req.robot });
}); });
router.get("/robots/:robot/commands", load, function(req, res) { router.get("/robots/:robot/commands", load, function(req, res) {
res.json(req.robot.toJSON().commands); res.json({ commands: req.robot.commands });
}); });
router.all("/robots/:robot/commands/:command", load, function(req, res) { router.all("/robots/:robot/commands/:command", load, function(req, res) {
var command = req.params.command; var command = req.params.command;
var result = req.robot[command].apply(req.robot, req.commandParams); var result = req.robot[command].apply(req.robot, req.commandParams);
res.json({ result: result }); res.json({ result: result });
}); });
router.get("/robots/:robot/devices", load, function(req, res) { router.get("/robots/:robot/devices", load, function(req, res) {
res.json(req.robot.toJSON().devices); res.json({ devices: req.robot.toJSON().devices });
}); });
router.get("/robots/:robot/devices/:device", load, function(req, res) { router.get("/robots/:robot/devices/:device", load, function(req, res) {
res.json(req.device); res.json({ device: req.device });
}); });
router.get("/robots/:robot/devices/:device/events/:event", load, function(req, res) { router.get("/robots/:robot/devices/:device/events/:event", load, function(req, res) {
@ -95,7 +102,7 @@ router.get("/robots/:robot/devices/:device/events/:event", load, function(req, r
}); });
router.get("/robots/:robot/devices/:device/commands", load, function(req, res) { router.get("/robots/:robot/devices/:device/commands", load, function(req, res) {
res.json(req.device.toJSON().commands); res.json({ commands: req.device.toJSON().commands });
}); });
router.all("/robots/:robot/devices/:device/commands/:command", load, function(req, res) { router.all("/robots/:robot/devices/:device/commands/:command", load, function(req, res) {
@ -106,9 +113,9 @@ router.all("/robots/:robot/devices/:device/commands/:command", load, function(re
}); });
router.get("/robots/:robot/connections", load, function(req, res) { router.get("/robots/:robot/connections", load, function(req, res) {
res.json(req.robot.toJSON().connections); res.json({ connections: req.robot.toJSON().connections });
}); });
router.get("/robots/:robot/connections/:connection", load, function(req, res) { router.get("/robots/:robot/connections/:connection", load, function(req, res) {
res.json(req.connection); res.json({ connection: req.connection });
}); });

View File

@ -16,7 +16,6 @@ var Logger = require('./logger'),
// Public: Creates a new Connection // Public: Creates a new Connection
// //
// opts - hash of acceptable params: // opts - hash of acceptable params:
// id - string ID for the connection
// robot - Robot the Connection belongs to // robot - Robot the Connection belongs to
// name - name for the connection // name - name for the connection
// adaptor - string module name of the adaptor to be set up // adaptor - string module name of the adaptor to be set up
@ -26,13 +25,10 @@ var Logger = require('./logger'),
var Connection = module.exports = function Connection(opts) { var Connection = module.exports = function Connection(opts) {
opts = opts || {}; opts = opts || {};
opts.id = opts.id || Math.floor(Math.random() * 10000);
this.connect = this.connect.bind(this); this.connect = this.connect.bind(this);
this.robot = opts.robot; this.robot = opts.robot;
this.name = opts.name; this.name = opts.name;
this.connection_id = opts.id;
this.port = opts.port; this.port = opts.port;
this.adaptor = this.initAdaptor(opts); this.adaptor = this.initAdaptor(opts);
@ -49,7 +45,6 @@ Connection.prototype.toJSON = function() {
name: this.name, name: this.name,
port: this.port, port: this.port,
adaptor: this.adaptor.constructor.name || this.adaptor.name, adaptor: this.adaptor.constructor.name || this.adaptor.name,
connection_id: this.connection_id
}; };
}; };

View File

@ -29,7 +29,8 @@ var Cylon = module.exports = {
api_instance: null, api_instance: null,
robots: {} robots: {},
commands: {}
}; };
// Public: Creates a new Robot // Public: Creates a new Robot
@ -94,6 +95,19 @@ Cylon.halt = function halt(callback) {
Async.parallel(fns, callback); Async.parallel(fns, callback);
}; };
Cylon.toJSON = function() {
var robots = [];
for (var bot in this.robots) {
robots.push(this.robots[bot]);
}
return {
robots: robots,
commands: Object.keys(this.commands)
}
};
if (process.platform === "win32") { if (process.platform === "win32") {
var readline = require("readline"), var readline = require("readline"),
io = { input: process.stdin, output: process.stdout }; io = { input: process.stdin, output: process.stdout };

View File

@ -78,7 +78,7 @@ Device.prototype.toJSON = function() {
name: this.name, name: this.name,
driver: this.driver.constructor.name || this.driver.name, driver: this.driver.constructor.name || this.driver.name,
pin: this.pin, pin: this.pin,
connection: this.connection.toJSON(), connection: this.connection.name,
commands: this.driver.commands commands: this.driver.commands
}; };
}; };

View File

@ -403,7 +403,7 @@ Robot.prototype.requireDriver = function(driverName) {
// moduleName - name of the Node module to require // moduleName - name of the Node module to require
// driverName - name of the driver to register the moduleName under // driverName - name of the driver to register the moduleName under
// //
// Returns the registered module nam// // Returns the registered module name
Robot.prototype.registerDriver = function(moduleName, driverName) { Robot.prototype.registerDriver = function(moduleName, driverName) {
if (this.drivers[driverName] == null) { if (this.drivers[driverName] == null) {
try { try {

View File

@ -21,6 +21,7 @@ module.exports = Ping = function Ping() {
Utils.subclass(Ping, Driver); Utils.subclass(Ping, Driver);
Ping.prototype.ping = function() { Ping.prototype.ping = function() {
this.device.emit('ping', 'ping');
return "pong"; return "pong";
}; };

View File

@ -44,11 +44,6 @@ describe("Connection", function() {
it("contains the connection's adaptor name", function() { it("contains the connection's adaptor name", function() {
expect(json.adaptor).to.be.eql("Loopback"); expect(json.adaptor).to.be.eql("Loopback");
}); });
it("contains the connection's ID", function() {
var id = connection.connection_id;
expect(json.connection_id).to.be.eql(id);
});
}); });
describe("#connect", function() { describe("#connect", function() {

View File

@ -21,7 +21,6 @@ describe("Cylon", function() {
it("sets Driver to the Driver module", function() { it("sets Driver to the Driver module", function() {
expect(Cylon.Driver).to.be.eql(Driver); expect(Cylon.Driver).to.be.eql(Driver);
}); });
});
it("sets @api_instance to null by default", function() { it("sets @api_instance to null by default", function() {
expect(Cylon.api_instance).to.be.eql(null); expect(Cylon.api_instance).to.be.eql(null);
@ -31,6 +30,11 @@ describe("Cylon", function() {
expect(Cylon.robots).to.be.eql({}); expect(Cylon.robots).to.be.eql({});
}); });
it("sets @robots to an empty object by default", function() {
expect(Cylon.commands).to.be.eql({});
});
});
describe("#robot", function() { describe("#robot", function() {
afterEach(function() { afterEach(function() {
Cylon.robots = {}; Cylon.robots = {};
@ -98,4 +102,26 @@ describe("Cylon", function() {
expect(bot2.halt).to.be.called; expect(bot2.halt).to.be.called;
}); });
}); });
describe("#toJSON", function() {
var json, bot1, bot2, echo;
beforeEach(function() {
bot1 = {};
bot2 = {};
Cylon.robots = { 'bot1': bot1, 'bot2': bot2 };
Cylon.commands.echo = echo = function(arg) { return arg; };
json = Cylon.toJSON();
});
it("contains all robots the MCP knows about", function() {
expect(json.robots).to.be.eql([bot1, bot2]);
});
it("contains an array of MCP commands", function() {
expect(json.commands).to.be.eql(['echo']);
})
});
}); });

View File

@ -41,7 +41,7 @@ describe("Device", function() {
expect(device.pin).to.be.eql(13); expect(device.pin).to.be.eql(13);
}); });
it("sets @connection to the specified connection on the Robot", function() { it("sets @connection to the name of the specified connection on the Robot", function() {
expect(device.connection).to.be.eql(connection); expect(device.connection).to.be.eql(connection);
}); });
@ -125,8 +125,8 @@ describe("Device", function() {
expect(json.driver).to.be.eql('Ping'); expect(json.driver).to.be.eql('Ping');
}); });
it("contains the device's connection json", function() { it("contains the device's connection name", function() {
expect(json.connection).to.be.eql(device.connection.toJSON()); expect(json.connection).to.be.eql('loopback');
}); });
it("contains the device's driver commands", function() { it("contains the device's driver commands", function() {