Start to API and fetching of all Robots

This commit is contained in:
Andrew Stewart 2013-10-30 16:42:50 -07:00
parent 05c8ad3bdc
commit 2a91066864
4 changed files with 108 additions and 9 deletions

62
dist/api/api.js vendored Normal file
View File

@ -0,0 +1,62 @@
/*
* api
* cylonjs.com
*
* Copyright (c) 2013 The Hybrid Group
* Licensed under the Apache 2.0 license.
*/
(function() {
var namespace, restify, socketio;
restify = require('restify');
socketio = require('socket.io');
namespace = require('node-namespace');
namespace("Api", function() {
return this.Server = (function() {
var master;
master = null;
function Server(opts) {
var _this = this;
if (opts == null) {
opts = {};
}
this.host = opts.host || "127.0.0.1";
this.port = opts.port || "3000";
master = opts.master;
this.server = restify.createServer({
name: "Cylon API Server"
});
this.io = socketio.listen(this.server);
this.server.get("/", this.getRobots);
this.server.listen(this.port, this.host, function() {
return Logger.info("" + _this.server.name + " is listening at " + _this.server.url);
});
}
Server.prototype.getRobots = function(req, res, next) {
var robot;
return res.send((function() {
var _i, _len, _ref, _results;
_ref = master.robots();
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
robot = _ref[_i];
_results.push(robot.data());
}
return _results;
})());
};
return Server;
})();
});
}).call(this);

22
dist/cylon.js vendored
View File

@ -19,6 +19,8 @@
require('./logger');
require('./api/api');
Logger.setup();
Cylon = (function() {
@ -39,14 +41,17 @@
};
Master = (function() {
var robots;
var api, robots;
robots = [];
api = null;
function Master() {
this.robot = __bind(this.robot, this);
this.self = this;
}
robots = [];
Master.prototype.robot = function(opts) {
var robot;
opts.master = this;
@ -55,8 +60,13 @@
return robot;
};
Master.prototype.robots = function() {
return robots;
};
Master.prototype.start = function() {
var robot, _i, _len, _results;
this.startAPI();
_results = [];
for (_i = 0, _len = robots.length; _i < _len; _i++) {
robot = robots[_i];
@ -65,6 +75,12 @@
return _results;
};
Master.prototype.startAPI = function() {
return api != null ? api : api = new Api.Server({
master: this.self
});
};
return Master;
})();

View File

@ -10,16 +10,25 @@ restify = require 'restify'
socketio = require 'socket.io'
namespace = require 'node-namespace'
namespace "Cylon.API", ->
namespace "Api", ->
# The Cylon API Server provides an interface to communicate with master class
# and retrieve information about the robots being controlled.
class @Server
master = null
constructor: (opts = {}) ->
host = opts.host || "127.0.0.1"
port = opts.port || "3000"
@host = opts.host || "127.0.0.1"
@port = opts.port || "3000"
master = opts.master
@self = this
@server = restify.createServer(name: "Cylon API Server")
@io = socketio.listen server
@io = socketio.listen @server
master: -> Cylon.getInstance()
@server.get "/", @getRobots
@server.listen @port, @host, =>
Logger.info "#{@server.name} is listening at #{@server.url}"
getRobots: (req, res, next) ->
res.send (robot.data() for robot in master.robots())

View File

@ -13,6 +13,8 @@ Robot = require("./robot")
require('./utils')
require('./logger')
require('./api/api')
Logger.setup()
class Cylon
@ -23,6 +25,10 @@ class Cylon
class Master
robots = []
api = null
constructor: ->
@self = this
robot: (opts) =>
opts.master = this
@ -30,7 +36,13 @@ class Cylon
robots.push robot
robot
robots: -> robots
start: ->
do @startAPI
robot.start() for robot in robots
startAPI: ->
api ?= new Api.Server(master: @self)
module.exports = Cylon.getInstance()