cylon/lib/api/routes.js

115 lines
2.8 KiB
JavaScript
Raw Normal View History

2014-06-05 04:39:30 +08:00
/*
* 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({ error: "No Robot found with the name " + robot });
}
}
if (device) {
req.device = req.robot.devices[device];
if (!req.device) {
return res.json({ error: "No device found with the name " + device });
}
}
if (connection) {
req.connection = req.robot.connections[connection];
if (!req.connection) {
return res.json({ error: "No connection found with the name " + connection });
}
}
next();
};
2014-06-05 04:39:30 +08:00
router.get("/robots", function(req, res) {
var data = [];
for (var bot in Cylon.robots) {
data.push(Cylon.robots[bot]);
2014-06-05 04:39:30 +08:00
}
res.json(data);
});
router.get("/robots/:robot", load, function(req, res) {
res.json(req.robot);
2014-06-05 04:39:30 +08:00
});
router.get("/robots/:robot/commands", load, function(req, res) {
res.json(req.robot.toJSON().commands);
2014-06-05 04:39:30 +08:00
});
router.all("/robots/:robot/commands/:command", load, function(req, res) {
var command = req.params.command;
2014-06-05 04:39:30 +08:00
var result = req.robot[command].apply(req.robot, req.commandParams);
res.json({ result: result });
2014-06-05 04:39:30 +08:00
});
router.get("/robots/:robot/devices", load, function(req, res) {
res.json(req.robot.toJSON().devices);
2014-06-05 04:39:30 +08:00
});
router.get("/robots/:robot/devices/:device", load, function(req, res) {
res.json(req.device);
2014-06-05 04:39:30 +08:00
});
router.get("/robots/:robot/devices/:device/events/:event", load, function(req, res) {
var event = req.params.event;
2014-06-05 04:39:30 +08:00
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Connection': 'keep-alive',
'Cache-Control': 'no-cache'
});
2014-06-05 04:39:30 +08:00
var writeData = function(data) {
res.write("data: " + JSON.stringify(data) + "\n\n");
};
2014-06-05 04:39:30 +08:00
req.device.on(event, writeData);
2014-06-05 04:39:30 +08:00
res.on('close', function() {
req.device.removeListener(event, writeData);
2014-06-05 04:39:30 +08:00
});
});
router.get("/robots/:robot/devices/:device/commands", load, function(req, res) {
res.json(req.device.toJSON().commands);
2014-06-05 04:39:30 +08:00
});
router.all("/robots/:robot/devices/:device/commands/:command", load, function(req, res) {
var command = req.params.command;
2014-06-05 04:39:30 +08:00
var result = req.device[command].apply(req.device, req.commandParams);
res.json({ result: result });
2014-06-05 04:39:30 +08:00
});
router.get("/robots/:robot/connections", load, function(req, res) {
res.json(req.robot.toJSON().connections);
2014-06-05 04:39:30 +08:00
});
router.get("/robots/:robot/connections/:connection", load, function(req, res) {
res.json(req.connection);
2014-06-05 04:39:30 +08:00
});