Add /robots/:robotid and /robots/:robotid/devices

This commit is contained in:
Andrew Stewart 2013-10-30 17:41:16 -07:00
parent 2a91066864
commit 65a2e20959
4 changed files with 49 additions and 2 deletions

22
dist/api/api.js vendored
View File

@ -34,7 +34,9 @@
name: "Cylon API Server" name: "Cylon API Server"
}); });
this.io = socketio.listen(this.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() { this.server.listen(this.port, this.host, function() {
return Logger.info("" + _this.server.name + " is listening at " + _this.server.url); 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; return Server;
})(); })();

10
dist/cylon.js vendored
View File

@ -64,6 +64,16 @@
return robots; 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() { Master.prototype.start = function() {
var robot, _i, _len, _results; var robot, _i, _len, _results;
this.startAPI(); this.startAPI();

View File

@ -25,10 +25,23 @@ namespace "Api", ->
@server = restify.createServer(name: "Cylon API Server") @server = restify.createServer(name: "Cylon API Server")
@io = socketio.listen @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, => @server.listen @port, @host, =>
Logger.info "#{@server.name} is listening at #{@server.url}" Logger.info "#{@server.name} is listening at #{@server.url}"
getRobots: (req, res, next) -> getRobots: (req, res, next) ->
res.send (robot.data() for robot in master.robots()) 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

View File

@ -38,6 +38,10 @@ class Cylon
robots: -> robots robots: -> robots
findRobot: (name) ->
for robot in robots
return robot if robot.name is name
start: -> start: ->
do @startAPI do @startAPI
robot.start() for robot in robots robot.start() for robot in robots