Clean up API implementation using middleware
This commit is contained in:
parent
5690d723fb
commit
af4bbd963b
|
@ -10,112 +10,105 @@ 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();
|
||||
};
|
||||
|
||||
router.get("/robots", function(req, res) {
|
||||
var data = [];
|
||||
|
||||
for (var i = 0; i < Cylon.robots.length; i++) {
|
||||
var robot = Cylon.robots[i];
|
||||
data.push(robot.data());
|
||||
for (var bot in Cylon.robots) {
|
||||
data.push(Cylon.robots[bot].data());
|
||||
}
|
||||
|
||||
res.json(data);
|
||||
});
|
||||
|
||||
router.get("/robots/:robot", function(req, res) {
|
||||
Cylon.findRobot(req.params.robot, function(err, robot) {
|
||||
res.json(err ? err : robot.data());
|
||||
router.get("/robots/:robot", load, function(req, res) {
|
||||
res.json(req.robot.data());
|
||||
});
|
||||
|
||||
router.get("/robots/:robot/commands", load, function(req, res) {
|
||||
res.json(req.robot.data().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.data().devices);
|
||||
});
|
||||
|
||||
router.get("/robots/:robot/devices/:device", load, function(req, res) {
|
||||
res.json(req.device.data());
|
||||
});
|
||||
|
||||
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/commands", function(req, res) {
|
||||
Cylon.findRobot(req.params.robot, function(err, robot) {
|
||||
res.json(err ? err : robot.data().commands);
|
||||
});
|
||||
router.get("/robots/:robot/devices/:device/commands", load, function(req, res) {
|
||||
res.json(req.device.data().commands);
|
||||
});
|
||||
|
||||
router.all("/robots/:robot/commands/:command", function(req, res) {
|
||||
Cylon.findRobot(req.params.robot, function(err, robot) {
|
||||
if (err) { return res.json(err); }
|
||||
router.all("/robots/:robot/devices/:device/commands/:command", load, function(req, res) {
|
||||
var command = req.params.command;
|
||||
|
||||
var result = robot[req.params.command].apply(robot, req.commandParams);
|
||||
res.json({ result: result });
|
||||
});
|
||||
var result = req.device[command].apply(req.device, req.commandParams);
|
||||
res.json({ result: result });
|
||||
});
|
||||
|
||||
router.get("/robots/:robot/devices", function(req, res) {
|
||||
Cylon.findRobot(req.params.robot, function(err, robot) {
|
||||
res.json(err ? err : robot.data().devices);
|
||||
});
|
||||
router.get("/robots/:robot/connections", load, function(req, res) {
|
||||
res.json(req.robot.data().connections);
|
||||
});
|
||||
|
||||
router.get("/robots/:robot/devices/:device", function(req, res) {
|
||||
var robot = req.params.robot,
|
||||
device = req.params.device;
|
||||
|
||||
Cylon.findRobotDevice(robot, device, function(err, device) {
|
||||
res.json(err ? err : device.data());
|
||||
});
|
||||
});
|
||||
|
||||
router.get("/robots/:robot/devices/:device/events/:event", function(req, res) {
|
||||
var robot = req.params.robot,
|
||||
device = req.params.device,
|
||||
event = req.params.event;
|
||||
|
||||
Cylon.findRobotDevice(robot, device, function(err, device) {
|
||||
if (err) { res.json(err); }
|
||||
|
||||
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")
|
||||
};
|
||||
|
||||
device.on(event, writeData);
|
||||
|
||||
res.on('close', function() {
|
||||
device.removeListener(event, writeData);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
router.get("/robots/:robot/devices/:device/commands", function(req, res) {
|
||||
var robot = req.params.robot,
|
||||
device = req.params.device;
|
||||
|
||||
Cylon.findRobotDevice(robot, device, function(err, device) {
|
||||
res.json(err ? err : device.data().commands);
|
||||
});
|
||||
});
|
||||
|
||||
router.all("/robots/:robot/devices/:device/commands/:command", function(req, res) {
|
||||
var robot = req.params.robot,
|
||||
device = req.params.device,
|
||||
command = req.params.command;
|
||||
|
||||
Cylon.findRobotDevice(robot, device, function(err, device) {
|
||||
if (err) { return res.json(err); }
|
||||
|
||||
var result = device[command].apply(device, req.commandParams);
|
||||
res.json({ result: result });
|
||||
});
|
||||
});
|
||||
|
||||
router.get("/robots/:robot/connections", function(req, res) {
|
||||
Cylon.findRobot(req.params.robot, function(err, robot) {
|
||||
res.json(err ? err : robot.data().connections);
|
||||
});
|
||||
});
|
||||
|
||||
router.get("/robots/:robot/connections/:connection", function(req, res) {
|
||||
var robot = req.params.robot,
|
||||
connection = req.params.connection;
|
||||
|
||||
Cylon.findRobotConnection(robot, connection, function(err, connection) {
|
||||
res.json(err ? err : connection.data());
|
||||
});
|
||||
router.get("/robots/:robot/connections/:connection", load, function(req, res) {
|
||||
res.json(req.connection.data());
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue