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();
|
|
|
|
|
2014-06-07 04:52:28 +08:00
|
|
|
// 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 = [];
|
|
|
|
|
2014-06-07 04:52:28 +08:00
|
|
|
for (var bot in Cylon.robots) {
|
2014-06-07 05:11:35 +08:00
|
|
|
data.push(Cylon.robots[bot]);
|
2014-06-05 04:39:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
res.json(data);
|
|
|
|
});
|
|
|
|
|
2014-06-07 04:52:28 +08:00
|
|
|
router.get("/robots/:robot", load, function(req, res) {
|
2014-06-07 05:11:35 +08:00
|
|
|
res.json(req.robot);
|
2014-06-05 04:39:30 +08:00
|
|
|
});
|
|
|
|
|
2014-06-07 04:52:28 +08:00
|
|
|
router.get("/robots/:robot/commands", load, function(req, res) {
|
2014-06-07 05:11:35 +08:00
|
|
|
res.json(req.robot.toJSON().commands);
|
2014-06-05 04:39:30 +08:00
|
|
|
});
|
|
|
|
|
2014-06-07 04:52:28 +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
|
|
|
|
2014-06-07 04:52:28 +08:00
|
|
|
var result = req.robot[command].apply(req.robot, req.commandParams);
|
|
|
|
res.json({ result: result });
|
2014-06-05 04:39:30 +08:00
|
|
|
});
|
|
|
|
|
2014-06-07 04:52:28 +08:00
|
|
|
router.get("/robots/:robot/devices", load, function(req, res) {
|
2014-06-07 05:11:35 +08:00
|
|
|
res.json(req.robot.toJSON().devices);
|
2014-06-05 04:39:30 +08:00
|
|
|
});
|
|
|
|
|
2014-06-07 04:52:28 +08:00
|
|
|
router.get("/robots/:robot/devices/:device", load, function(req, res) {
|
2014-06-07 05:11:35 +08:00
|
|
|
res.json(req.device);
|
2014-06-05 04:39:30 +08:00
|
|
|
});
|
|
|
|
|
2014-06-07 04:52:28 +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
|
|
|
|
2014-06-07 04:52:28 +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
|
|
|
|
2014-06-07 04:52:28 +08:00
|
|
|
var writeData = function(data) {
|
2014-06-11 09:45:08 +08:00
|
|
|
res.write("data: " + JSON.stringify(data) + "\n\n");
|
2014-06-07 04:52:28 +08:00
|
|
|
};
|
2014-06-05 04:39:30 +08:00
|
|
|
|
2014-06-07 04:52:28 +08:00
|
|
|
req.device.on(event, writeData);
|
2014-06-05 04:39:30 +08:00
|
|
|
|
2014-06-07 04:52:28 +08:00
|
|
|
res.on('close', function() {
|
|
|
|
req.device.removeListener(event, writeData);
|
2014-06-05 04:39:30 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-06-07 04:52:28 +08:00
|
|
|
router.get("/robots/:robot/devices/:device/commands", load, function(req, res) {
|
2014-06-07 05:11:35 +08:00
|
|
|
res.json(req.device.toJSON().commands);
|
2014-06-05 04:39:30 +08:00
|
|
|
});
|
|
|
|
|
2014-06-07 04:52:28 +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
|
|
|
|
2014-06-07 04:52:28 +08:00
|
|
|
var result = req.device[command].apply(req.device, req.commandParams);
|
|
|
|
res.json({ result: result });
|
2014-06-05 04:39:30 +08:00
|
|
|
});
|
|
|
|
|
2014-06-07 04:52:28 +08:00
|
|
|
router.get("/robots/:robot/connections", load, function(req, res) {
|
2014-06-07 05:11:35 +08:00
|
|
|
res.json(req.robot.toJSON().connections);
|
2014-06-05 04:39:30 +08:00
|
|
|
});
|
|
|
|
|
2014-06-07 04:52:28 +08:00
|
|
|
router.get("/robots/:robot/connections/:connection", load, function(req, res) {
|
2014-06-07 05:11:35 +08:00
|
|
|
res.json(req.connection);
|
2014-06-05 04:39:30 +08:00
|
|
|
});
|