Merge pull request #213 from hybridgroup/refactor/threepio
Threepio Support
This commit is contained in:
commit
8762820d0c
15
lib/api.js
15
lib/api.js
|
@ -43,26 +43,17 @@ var API = module.exports = function API(opts) {
|
|||
|
||||
// extracts command params from request
|
||||
this.express.use(function(req, res, next) {
|
||||
var method = req.method.toLowerCase(),
|
||||
container = {};
|
||||
|
||||
req.commandParams = [];
|
||||
|
||||
if (method === 'get' || Object.keys(req.query).length > 0) {
|
||||
container = req.query;
|
||||
} else if (typeof(req.body) === 'object') {
|
||||
container = req.body;
|
||||
}
|
||||
|
||||
for (var p in container) {
|
||||
req.commandParams.push(container[p]);
|
||||
for (var p in req.body) {
|
||||
req.commandParams.push(req.body[p]);
|
||||
}
|
||||
|
||||
return next();
|
||||
});
|
||||
|
||||
// load route definitions
|
||||
this.express.use('/', require('./api/routes'));
|
||||
this.express.use('/api', require('./api/routes'));
|
||||
};
|
||||
|
||||
API.prototype.defaults = {
|
||||
|
|
|
@ -41,37 +41,44 @@ var load = function load(req, res, 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) {
|
||||
var data = [];
|
||||
|
||||
for (var bot in Cylon.robots) {
|
||||
data.push(Cylon.robots[bot]);
|
||||
}
|
||||
|
||||
res.json(data);
|
||||
res.json({ robots: Cylon.toJSON().robots });
|
||||
});
|
||||
|
||||
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) {
|
||||
res.json(req.robot.toJSON().commands);
|
||||
res.json({ commands: req.robot.commands });
|
||||
});
|
||||
|
||||
router.all("/robots/:robot/commands/:command", load, function(req, res) {
|
||||
var command = req.params.command;
|
||||
|
||||
var result = req.robot[command].apply(req.robot, req.commandParams);
|
||||
res.json({ result: result });
|
||||
});
|
||||
|
||||
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) {
|
||||
res.json(req.device);
|
||||
res.json({ device: req.device });
|
||||
});
|
||||
|
||||
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) {
|
||||
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) {
|
||||
|
@ -106,9 +113,9 @@ router.all("/robots/:robot/devices/:device/commands/:command", load, function(re
|
|||
});
|
||||
|
||||
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) {
|
||||
res.json(req.connection);
|
||||
res.json({ connection: req.connection });
|
||||
});
|
||||
|
|
|
@ -16,7 +16,6 @@ var Logger = require('./logger'),
|
|||
// Public: Creates a new Connection
|
||||
//
|
||||
// opts - hash of acceptable params:
|
||||
// id - string ID for the connection
|
||||
// robot - Robot the Connection belongs to
|
||||
// name - name for the connection
|
||||
// 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) {
|
||||
opts = opts || {};
|
||||
|
||||
opts.id = opts.id || Math.floor(Math.random() * 10000);
|
||||
|
||||
this.connect = this.connect.bind(this);
|
||||
|
||||
this.robot = opts.robot;
|
||||
this.name = opts.name;
|
||||
this.connection_id = opts.id;
|
||||
this.port = opts.port;
|
||||
this.adaptor = this.initAdaptor(opts);
|
||||
|
||||
|
@ -49,7 +45,6 @@ Connection.prototype.toJSON = function() {
|
|||
name: this.name,
|
||||
port: this.port,
|
||||
adaptor: this.adaptor.constructor.name || this.adaptor.name,
|
||||
connection_id: this.connection_id
|
||||
};
|
||||
};
|
||||
|
||||
|
|
16
lib/cylon.js
16
lib/cylon.js
|
@ -29,7 +29,8 @@ var Cylon = module.exports = {
|
|||
|
||||
api_instance: null,
|
||||
|
||||
robots: {}
|
||||
robots: {},
|
||||
commands: {}
|
||||
};
|
||||
|
||||
// Public: Creates a new Robot
|
||||
|
@ -94,6 +95,19 @@ Cylon.halt = function halt(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") {
|
||||
var readline = require("readline"),
|
||||
io = { input: process.stdin, output: process.stdout };
|
||||
|
|
|
@ -78,7 +78,7 @@ Device.prototype.toJSON = function() {
|
|||
name: this.name,
|
||||
driver: this.driver.constructor.name || this.driver.name,
|
||||
pin: this.pin,
|
||||
connection: this.connection.toJSON(),
|
||||
connection: this.connection.name,
|
||||
commands: this.driver.commands
|
||||
};
|
||||
};
|
||||
|
|
|
@ -403,7 +403,7 @@ Robot.prototype.requireDriver = function(driverName) {
|
|||
// moduleName - name of the Node module to require
|
||||
// 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) {
|
||||
if (this.drivers[driverName] == null) {
|
||||
try {
|
||||
|
|
|
@ -21,6 +21,7 @@ module.exports = Ping = function Ping() {
|
|||
Utils.subclass(Ping, Driver);
|
||||
|
||||
Ping.prototype.ping = function() {
|
||||
this.device.emit('ping', 'ping');
|
||||
return "pong";
|
||||
};
|
||||
|
||||
|
|
|
@ -44,11 +44,6 @@ describe("Connection", function() {
|
|||
it("contains the connection's adaptor name", function() {
|
||||
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() {
|
||||
|
|
|
@ -21,14 +21,18 @@ describe("Cylon", function() {
|
|||
it("sets Driver to the Driver module", function() {
|
||||
expect(Cylon.Driver).to.be.eql(Driver);
|
||||
});
|
||||
});
|
||||
|
||||
it("sets @api_instance to null by default", function() {
|
||||
expect(Cylon.api_instance).to.be.eql(null);
|
||||
});
|
||||
it("sets @api_instance to null by default", function() {
|
||||
expect(Cylon.api_instance).to.be.eql(null);
|
||||
});
|
||||
|
||||
it("sets @robots to an empty object by default", function() {
|
||||
expect(Cylon.robots).to.be.eql({});
|
||||
it("sets @robots to an empty object by default", function() {
|
||||
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() {
|
||||
|
@ -98,4 +102,26 @@ describe("Cylon", function() {
|
|||
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']);
|
||||
})
|
||||
});
|
||||
});
|
||||
|
|
|
@ -41,7 +41,7 @@ describe("Device", function() {
|
|||
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);
|
||||
});
|
||||
|
||||
|
@ -125,8 +125,8 @@ describe("Device", function() {
|
|||
expect(json.driver).to.be.eql('Ping');
|
||||
});
|
||||
|
||||
it("contains the device's connection json", function() {
|
||||
expect(json.connection).to.be.eql(device.connection.toJSON());
|
||||
it("contains the device's connection name", function() {
|
||||
expect(json.connection).to.be.eql('loopback');
|
||||
});
|
||||
|
||||
it("contains the device's driver commands", function() {
|
||||
|
|
Loading…
Reference in New Issue