cylon/lib/api.js

186 lines
5.5 KiB
JavaScript
Raw Normal View History

/*
* api
* cylonjs.com
*
* Copyright (c) 2013-2014 The Hybrid Group
* Licensed under the Apache 2.0 license.
*/
"use strict";
var express = require('express.io');
var namespace = require('node-namespace');
namespace("Cylon", function() {
2014-02-28 06:42:33 +08:00
// The Cylon API Server provides an interface to communicate with master class
// and retrieve information about the robots being controlled.
this.ApiServer = (function() {
var master;
master = null;
function ApiServer(opts) {
2014-04-04 03:28:27 +08:00
var self = this;
if (opts == null) { opts = {}; }
this.host = opts.host || "127.0.0.1";
this.port = opts.port || "3000";
2014-04-04 03:28:27 +08:00
master = opts.master;
2014-04-04 03:28:27 +08:00
this.server = express().http().io();
2014-04-04 03:28:27 +08:00
this.server.set('title', 'Cylon API Server');
2014-04-04 03:28:27 +08:00
this.server.use(express.json());
this.server.use(express.urlencoded());
this.server.use(express["static"](__dirname + "/../node_modules/robeaux/"));
2014-04-04 03:28:27 +08:00
this.server.get("/*", function(req, res, next) {
res.set('Content-Type', 'application/json');
return next();
});
2014-04-04 03:28:27 +08:00
this.configureRoutes();
2014-04-04 03:28:27 +08:00
this.server.listen(this.port, this.host, function() {
2014-04-04 03:28:27 +08:00
var title = self.server.get('title');
Logger.info(title + " is listening on " + self.host + ":" + self.port);
});
}
2014-02-28 06:42:33 +08:00
// Parses req to extract params to be used for commands.
//
// Returns an array of params
ApiServer.prototype.parseCommandParams = function(req) {
2014-04-04 03:28:27 +08:00
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;
}
2014-04-04 03:28:27 +08:00
for (var p in param_container) { params.push(param_container[p]); }
return params;
};
ApiServer.prototype.configureRoutes = function() {
2014-04-04 03:28:27 +08:00
var self = this;
2014-01-17 06:56:40 +08:00
this.server.get("/robots", function(req, res) {
2014-04-04 03:28:27 +08:00
var data = [];
for (var i = 0; i < master.robots.length; i++) {
var robot = master.robots[i];
data.push(robot.data());
}
res.json(data);
});
2014-02-28 06:14:33 +08:00
this.server.get("/robots/:robotname", function(req, res) {
2014-04-04 03:28:27 +08:00
master.findRobot(req.params.robotname, function(err, robot) {
res.json(err ? err : robot.data());
});
});
2014-02-28 06:14:33 +08:00
this.server.get("/robots/:robotname/commands", function(req, res) {
2014-04-04 03:28:27 +08:00
master.findRobot(req.params.robotname, function(err, robot) {
res.json(err ? err : robot.data().commands);
2013-11-01 04:36:28 +08:00
});
});
2014-02-28 06:14:33 +08:00
this.server.all("/robots/:robotname/commands/:commandname", function(req, res) {
2014-04-04 03:28:27 +08:00
var params = self.parseCommandParams(req);
master.findRobot(req.params.robotname, function(err, robot) {
if (err) { return res.json(err); }
var result = robot[req.params.commandname].apply(robot, params);
res.json({ result: result });
2013-11-26 06:28:13 +08:00
});
});
2014-02-28 06:14:33 +08:00
this.server.get("/robots/:robotname/devices", function(req, res) {
2014-04-04 03:28:27 +08:00
master.findRobot(req.params.robotname, function(err, robot) {
res.json(err ? err : robot.data().devices);
2013-11-26 06:28:13 +08:00
});
});
2014-02-28 06:14:33 +08:00
this.server.get("/robots/:robotname/devices/:devicename", function(req, res) {
2014-04-04 03:28:27 +08:00
var robotname = req.params.robotname,
devicename = req.params.devicename;
master.findRobotDevice(robotname, devicename, function(err, device) {
res.json(err ? err : device.data());
2013-11-01 04:36:28 +08:00
});
});
2014-02-28 06:14:33 +08:00
this.server.get("/robots/:robotname/devices/:devicename/commands", function(req, res) {
2014-04-04 03:28:27 +08:00
var robotname = req.params.robotname,
devicename = req.params.devicename;
master.findRobotDevice(robotname, devicename, function(err, device) {
res.json(err ? err : device.data().commands);
});
});
2014-02-28 06:14:33 +08:00
this.server.all("/robots/:robot/devices/:device/commands/:commandname", function(req, res) {
2014-04-04 03:28:27 +08:00
var robotname = req.params.robot,
devicename = req.params.device,
commandname = req.params.commandname;
var params = self.parseCommandParams(req);
master.findRobotDevice(robotname, devicename, function(err, device) {
if (err) { return res.json(err); }
var result = device[commandname].apply(device, params);
res.json({ result: result });
});
});
2014-02-28 06:14:33 +08:00
this.server.get("/robots/:robotname/connections", function(req, res) {
2014-04-04 03:28:27 +08:00
master.findRobot(req.params.robotname, function(err, robot) {
res.json(err ? err : robot.data().connections);
});
});
2014-02-28 06:14:33 +08:00
this.server.get("/robots/:robot/connections/:connection", function(req, res) {
2014-04-04 03:28:27 +08:00
var robotname = req.params.robot,
connectionname = req.params.connection;
master.findRobotConnection(robotname, connectionname, function(err, connection) {
res.json(err ? err : connection.data());
});
});
2014-02-28 06:14:33 +08:00
this.server.get("/robots/:robotname/devices/:devicename/events", function(req, res) {
2014-04-04 03:28:27 +08:00
req.io.route('events');
});
2014-02-28 06:14:33 +08:00
2014-04-04 03:28:27 +08:00
this.server.io.route('events', function(req) {
var robotname = req.params.robotname,
devicename = req.params.devicename
master.findRobotDevice(robotname, devicename, function(err, device) {
if (err) { return req.io.respond(err); }
device.on('update', function(data) {
req.io.emit('update', { data: data });
});
});
});
};
return ApiServer;
})();
});
2014-01-11 03:48:39 +08:00
module.exports = Cylon.ApiServer;