cylon/lib/api.js

242 lines
6.3 KiB
JavaScript
Raw Normal View History

/*
* Cylon API
* cylonjs.com
*
* Copyright (c) 2013-2014 The Hybrid Group
* Licensed under the Apache 2.0 license.
*/
"use strict";
2014-06-05 02:37:15 +08:00
var fs = require('fs'),
path = require('path');
2014-06-05 02:06:02 +08:00
var express = require('express'),
bodyParser = require('body-parser');
2014-04-04 03:28:27 +08:00
var Logger = require('./logger');
2014-05-08 06:19:23 +08:00
var API = module.exports = function API(opts) {
var self = this;
2014-05-08 06:19:23 +08:00
if (opts == null) {
opts = {};
}
2014-04-04 03:28:27 +08:00
2014-06-05 02:37:15 +08:00
for (var d in this.defaults) {
this[d] = opts.hasOwnProperty(d) ? opts[d] : this.defaults[d];
}
2014-05-08 06:19:23 +08:00
this.server = express();
2014-04-04 03:28:27 +08:00
2014-05-08 06:19:23 +08:00
//configure ssl if requested
if (this.ssl && typeof(this.ssl) === 'object') {
var https = require('https');
2014-04-04 03:28:27 +08:00
2014-05-08 06:19:23 +08:00
var options = {
2014-06-05 02:37:15 +08:00
key: fs.readFileSync(this.ssl.key),
cert: fs.readFileSync(this.ssl.cert)
};
2014-05-08 06:19:23 +08:00
this.server.node = https.createServer(options, this.server);
} else {
Logger.warn("API using insecure connection. We recommend using an SSL certificate with Cylon.")
this.server.node = this.server;
}
var authfn = this.setupAuth();
this.server.use(authfn);
2014-05-08 06:19:23 +08:00
this.server.set('title', 'Cylon API Server');
2014-05-08 06:19:23 +08:00
this.server.use(express["static"](__dirname + "/../node_modules/robeaux/"));
this.configureRoutes();
2014-05-08 06:19:23 +08:00
};
2014-06-05 02:37:15 +08:00
API.prototype.defaults = {
host: '127.0.0.1',
port: '3000',
auth: false,
CORS: '',
ssl: {
2014-06-05 02:58:54 +08:00
key: path.normalize(__dirname + "/api/ssl/server.key"),
cert: path.normalize(__dirname + "/api/ssl/server.crt")
2014-06-05 02:37:15 +08:00
}
};
API.prototype.setupAuth = function setupAuth() {
var authfn = function auth(req, res, next) { next(); };
if (typeof(this.auth) === 'object' && this.auth.type) {
var type = this.auth.type,
module = "./api/auth/" + type,
filename = path.normalize(__dirname + "/" + module + ".js"),
exists = fs.existsSync(filename);
if (exists) {
authfn = require(filename)(this.auth);
}
};
return authfn;
};
2014-05-08 06:19:23 +08:00
API.prototype.listen = function() {
var self = this;
this.server.node.listen(this.port, this.host, null, function() {
var title = self.server.get('title');
var protocol = self.ssl ? "https" : "http";
Logger.info(title + " is now online.");
Logger.info("Listening at " + protocol + "://" + self.host + ":" + self.port);
});
};
// Parses req to extract params to be used for commands.
//
// Returns an array of params
API.prototype.parseCommandParams = function(req) {
var param_container = {},
params = [];
if (req.method === 'GET' || Object.keys(req.query).length > 0) {
param_container = req.query;
} else if (typeof req.body === 'object') {
param_container = req.body;
}
for (var p in param_container) { params.push(param_container[p]); }
return params;
};
// Sets all routes for the server
API.prototype.configureRoutes = function() {
var Cylon = require('./cylon');
2014-05-08 06:19:23 +08:00
var self = this;
this.server.all("/*", function(req, res, next) {
res.set("Access-Control-Allow-Origin", self.CORS || "*");
2014-05-08 06:19:23 +08:00
res.set("Access-Control-Allow-Headers", "Content-Type");
res.set('Content-Type', 'application/json');
return next();
});
this.server.get("/robots", function(req, res) {
var data = [];
2014-06-05 02:37:15 +08:00
for (var i = 0; i < Cylon.robots.length; i++) {
var robot = Cylon.robots[i];
2014-05-08 06:19:23 +08:00
data.push(robot.data());
}
res.json(data);
});
this.server.get("/robots/:robot", function(req, res) {
2014-06-05 02:37:15 +08:00
Cylon.findRobot(req.params.robot, function(err, robot) {
2014-05-08 06:19:23 +08:00
res.json(err ? err : robot.data());
});
});
this.server.get("/robots/:robot/commands", function(req, res) {
2014-06-05 02:37:15 +08:00
Cylon.findRobot(req.params.robot, function(err, robot) {
2014-05-08 06:19:23 +08:00
res.json(err ? err : robot.data().commands);
});
});
this.server.all("/robots/:robot/commands/:command", function(req, res) {
var params = self.parseCommandParams(req);
2014-06-05 02:37:15 +08:00
Cylon.findRobot(req.params.robot, function(err, robot) {
2014-05-08 06:19:23 +08:00
if (err) { return res.json(err); }
var result = robot[req.params.command].apply(robot, params);
res.json({ result: result });
});
});
this.server.get("/robots/:robot/devices", function(req, res) {
2014-06-05 02:37:15 +08:00
Cylon.findRobot(req.params.robot, function(err, robot) {
2014-05-08 06:19:23 +08:00
res.json(err ? err : robot.data().devices);
});
});
this.server.get("/robots/:robot/devices/:device", function(req, res) {
var robot = req.params.robot,
device = req.params.device;
2014-06-05 02:37:15 +08:00
Cylon.findRobotDevice(robot, device, function(err, device) {
2014-05-08 06:19:23 +08:00
res.json(err ? err : device.data());
});
});
this.server.get("/robots/:robot/devices/:device/events/:event", function(req, res) {
var robot = req.params.robot,
device = req.params.device,
event = req.params.event;
2014-06-05 02:37:15 +08:00
Cylon.findRobotDevice(robot, device, function(err, device) {
2014-05-08 06:19:23 +08:00
if (err) { res.json(err); }
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Connection': 'keep-alive',
'Cache-Control': 'no-cache'
});
2014-02-28 06:14:33 +08:00
2014-05-08 06:19:23 +08:00
var writeData = function(data) {
res.write("data: " + JSON.stringify(data) + "\n\n")
};
2014-05-08 06:19:23 +08:00
device.on(event, writeData);
2014-05-08 06:19:23 +08:00
res.on('close', function() {
device.removeListener(event, writeData);
});
2014-05-08 06:19:23 +08:00
});
});
this.server.get("/robots/:robot/devices/:device/commands", function(req, res) {
var robot = req.params.robot,
device = req.params.device;
2014-06-05 02:37:15 +08:00
Cylon.findRobotDevice(robot, device, function(err, device) {
2014-05-08 06:19:23 +08:00
res.json(err ? err : device.data().commands);
});
});
this.server.all("/robots/:robot/devices/:device/commands/:command", function(req, res) {
var robot = req.params.robot,
device = req.params.device,
command = req.params.command;
var params = self.parseCommandParams(req);
2014-06-05 02:37:15 +08:00
Cylon.findRobotDevice(robot, device, function(err, device) {
2014-05-08 06:19:23 +08:00
if (err) { return res.json(err); }
var result = device[command].apply(device, params);
res.json({ result: result });
});
});
this.server.get("/robots/:robot/connections", function(req, res) {
2014-06-05 02:37:15 +08:00
Cylon.findRobot(req.params.robot, function(err, robot) {
2014-05-08 06:19:23 +08:00
res.json(err ? err : robot.data().connections);
});
});
this.server.get("/robots/:robot/connections/:connection", function(req, res) {
var robot = req.params.robot,
connection = req.params.connection;
2014-06-05 02:37:15 +08:00
Cylon.findRobotConnection(robot, connection, function(err, connection) {
2014-05-08 06:19:23 +08:00
res.json(err ? err : connection.data());
});
});
};