cylon/lib/api/routes.js

121 lines
3.3 KiB
JavaScript

/*
* Cylon API - Route Definitions
* cylonjs.com
*
* Copyright (c) 2013-2014 The Hybrid Group
* Licensed under the Apache 2.0 license.
*/
var Cylon = require('../cylon');
var router = module.exports = require('express').Router();
// Loads up the appropriate Robot/Device/Connection instances, if they are
// present in the route params.
var load = function load(req, res, next) {
var robot = req.params.robot,
device = req.params.device,
connection = req.params.connection;
if (robot) {
req.robot = Cylon.robots[robot];
if (!req.robot) {
return res.json(404, { error: "No Robot found with the name " + robot });
}
}
if (device) {
req.device = req.robot.devices[device];
if (!req.device) {
return res.json(404, { error: "No device found with the name " + device });
}
}
if (connection) {
req.connection = req.robot.connections[connection];
if (!req.connection) {
return res.json(404, { error: "No connection found with the name " + connection });
}
}
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) {
res.json({ robots: Cylon.toJSON().robots });
});
router.get("/robots/:robot", load, function(req, res) {
res.json({ robot: req.robot });
});
router.get("/robots/:robot/commands", load, function(req, res) {
res.json({ commands: Object.keys(req.robot.commands) });
});
router.all("/robots/:robot/commands/:command", load, function(req, res) {
var command = req.robot.commands[req.params.command];
var result = command.apply(req.robot, req.commandParams);
res.json({ result: result });
});
router.get("/robots/:robot/devices", load, function(req, res) {
res.json({ devices: req.robot.toJSON().devices });
});
router.get("/robots/:robot/devices/:device", load, function(req, res) {
res.json({ device: req.device });
});
router.get("/robots/:robot/devices/:device/events/:event", load, function(req, res) {
var event = req.params.event;
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Connection': 'keep-alive',
'Cache-Control': 'no-cache'
});
var writeData = function(data) {
res.write("data: " + JSON.stringify(data) + "\n\n");
};
req.device.on(event, writeData);
res.on('close', function() {
req.device.removeListener(event, writeData);
});
});
router.get("/robots/:robot/devices/:device/commands", load, function(req, res) {
res.json({ commands: Object.keys(req.device.driver.commands) });
});
router.all("/robots/:robot/devices/:device/commands/:command", load, function(req, res) {
var command = req.device.driver.commands[req.params.command];
var result = command.apply(req.device.driver, req.commandParams);
res.json({ result: result });
});
router.get("/robots/:robot/connections", load, function(req, res) {
res.json({ connections: req.robot.toJSON().connections });
});
router.get("/robots/:robot/connections/:connection", load, function(req, res) {
res.json({ connection: req.connection });
});