Add method to run a command for a particular device

This commit is contained in:
Andrew Stewart 2013-11-01 11:13:03 -07:00
parent b9560b159c
commit e9a4f25ccd
2 changed files with 43 additions and 0 deletions

29
dist/api/api.js vendored
View File

@ -34,11 +34,15 @@
name: "Cylon API Server" name: "Cylon API Server"
}); });
this.io = socketio.listen(this.server); this.io = socketio.listen(this.server);
this.server.use(restify.bodyParser({
mapParams: false
}));
this.server.get("/robots", this.getRobots); this.server.get("/robots", this.getRobots);
this.server.get("/robots/:robotid", this.getRobotByName); this.server.get("/robots/:robotid", this.getRobotByName);
this.server.get("/robots/:robotid/devices", this.getDevices); this.server.get("/robots/:robotid/devices", this.getDevices);
this.server.get("/robots/:robotid/devices/:deviceid", this.getDeviceByName); this.server.get("/robots/:robotid/devices/:deviceid", this.getDeviceByName);
this.server.get("/robots/:robotid/devices/:deviceid/commands", this.getDeviceCommands); this.server.get("/robots/:robotid/devices/:deviceid/commands", this.getDeviceCommands);
this.server.post("/robots/:robotid/devices/:deviceid/commands/:commandid", this.runDeviceCommand);
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);
}); });
@ -88,6 +92,31 @@
}); });
}; };
Server.prototype.runDeviceCommand = function(req, res, next) {
var commandid, deviceid, key, params, robotid, value, _ref;
robotid = req.params.robotid;
deviceid = req.params.deviceid;
commandid = req.params.commandid;
params = [];
if (typeof req.body === 'object') {
_ref = req.body;
for (key in _ref) {
value = _ref[key];
params.push(value);
}
}
return master.findRobotDevice(robotid, deviceid, function(err, device) {
var result;
if (err) {
return res.send(err);
}
result = device[commandid].apply(device, params);
return res.send({
result: result
});
});
};
return Server; return Server;
})(); })();

View File

@ -25,6 +25,8 @@ 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.use restify.bodyParser(mapParams: false)
@server.get "/robots", @getRobots @server.get "/robots", @getRobots
@server.get "/robots/:robotid", @getRobotByName @server.get "/robots/:robotid", @getRobotByName
@server.get "/robots/:robotid/devices", @getDevices @server.get "/robots/:robotid/devices", @getDevices
@ -61,3 +63,15 @@ namespace "Api", ->
res.send if err then err else device.data().commands res.send if err then err else device.data().commands
runDeviceCommand: (req, res, next) -> runDeviceCommand: (req, res, next) ->
robotid = req.params.robotid
deviceid = req.params.deviceid
commandid = req.params.commandid
params = []
if typeof req.body is 'object'
params.push(value) for key, value of req.body
master.findRobotDevice robotid, deviceid, (err, device) ->
if err then return res.send err
result = device[commandid](params...)
res.send result: result