diff --git a/dist/api/api.js b/dist/api/api.js new file mode 100644 index 0000000..9dc1c15 --- /dev/null +++ b/dist/api/api.js @@ -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); diff --git a/dist/cylon.js b/dist/cylon.js index 5a83cba..d8f85a5 100644 --- a/dist/cylon.js +++ b/dist/cylon.js @@ -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; })(); diff --git a/src/api/api.coffee b/src/api/api.coffee index 6c8cf0f..08030a0 100644 --- a/src/api/api.coffee +++ b/src/api/api.coffee @@ -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()) diff --git a/src/cylon.coffee b/src/cylon.coffee index 1d21177..275ca03 100644 --- a/src/cylon.coffee +++ b/src/cylon.coffee @@ -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()