Add /robots/:robotid and /robots/:robotid/devices
This commit is contained in:
parent
2a91066864
commit
65a2e20959
|
@ -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;
|
||||
|
||||
})();
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue