From 65a2e20959df792ab98fb1093196b52a0db6784f Mon Sep 17 00:00:00 2001 From: Andrew Stewart Date: Wed, 30 Oct 2013 17:41:16 -0700 Subject: [PATCH] Add /robots/:robotid and /robots/:robotid/devices --- dist/api/api.js | 22 +++++++++++++++++++++- dist/cylon.js | 10 ++++++++++ src/api/api.coffee | 15 ++++++++++++++- src/cylon.coffee | 4 ++++ 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/dist/api/api.js b/dist/api/api.js index 9dc1c15..5104ffc 100644 --- a/dist/api/api.js +++ b/dist/api/api.js @@ -34,7 +34,9 @@ name: "Cylon API Server" }); this.io = socketio.listen(this.server); - this.server.get("/", this.getRobots); + this.server.get("/robots", this.getRobots); + this.server.get("/robots/:robotid", this.getRobotByName); + this.server.get("/robots/:robotid/devices", this.getRobotDevices); this.server.listen(this.port, this.host, function() { return Logger.info("" + _this.server.name + " is listening at " + _this.server.url); }); @@ -54,6 +56,24 @@ })()); }; + Server.prototype.getRobotByName = function(req, res, next) { + var robot; + robot = master.findRobot(req.params.robotid); + if (robot) { + return res.send(robot.data()); + } else { + return res.send({ + error: "No robot with that name exists." + }); + } + }; + + Server.prototype.getRobotDevices = function(req, res, next) { + var robot; + robot = master.findRobot(req.params.robotid); + return res.send(robot.data().devices); + }; + return Server; })(); diff --git a/dist/cylon.js b/dist/cylon.js index d8f85a5..ee6f720 100644 --- a/dist/cylon.js +++ b/dist/cylon.js @@ -64,6 +64,16 @@ return robots; }; + Master.prototype.findRobot = function(name) { + var robot, _i, _len; + for (_i = 0, _len = robots.length; _i < _len; _i++) { + robot = robots[_i]; + if (robot.name === name) { + return robot; + } + } + }; + Master.prototype.start = function() { var robot, _i, _len, _results; this.startAPI(); diff --git a/src/api/api.coffee b/src/api/api.coffee index 08030a0..b820a16 100644 --- a/src/api/api.coffee +++ b/src/api/api.coffee @@ -25,10 +25,23 @@ namespace "Api", -> @server = restify.createServer(name: "Cylon API Server") @io = socketio.listen @server - @server.get "/", @getRobots + @server.get "/robots", @getRobots + @server.get "/robots/:robotid", @getRobotByName + @server.get "/robots/:robotid/devices", @getRobotDevices @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()) + + getRobotByName: (req, res, next) -> + robot = master.findRobot(req.params.robotid) + if robot + res.send robot.data() + else + res.send {error: "No robot with that name exists."} + + getRobotDevices: (req, res, next) -> + robot = master.findRobot(req.params.robotid) + res.send robot.data().devices diff --git a/src/cylon.coffee b/src/cylon.coffee index 275ca03..2b417d3 100644 --- a/src/cylon.coffee +++ b/src/cylon.coffee @@ -38,6 +38,10 @@ class Cylon robots: -> robots + findRobot: (name) -> + for robot in robots + return robot if robot.name is name + start: -> do @startAPI robot.start() for robot in robots