diff --git a/dist/api/api.js b/dist/api/api.js index 674e892..19cc176 100644 --- a/dist/api/api.js +++ b/dist/api/api.js @@ -34,11 +34,15 @@ name: "Cylon API Server" }); this.io = socketio.listen(this.server); + this.server.use(restify.bodyParser({ + mapParams: false + })); this.server.get("/robots", this.getRobots); this.server.get("/robots/:robotid", this.getRobotByName); this.server.get("/robots/:robotid/devices", this.getDevices); this.server.get("/robots/:robotid/devices/:deviceid", this.getDeviceByName); 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() { 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; })(); diff --git a/src/api/api.coffee b/src/api/api.coffee index 14f5a4e..43ad4db 100644 --- a/src/api/api.coffee +++ b/src/api/api.coffee @@ -25,6 +25,8 @@ namespace "Api", -> @server = restify.createServer(name: "Cylon API Server") @io = socketio.listen @server + @server.use restify.bodyParser(mapParams: false) + @server.get "/robots", @getRobots @server.get "/robots/:robotid", @getRobotByName @server.get "/robots/:robotid/devices", @getDevices @@ -61,3 +63,15 @@ namespace "Api", -> res.send if err then err else device.data().commands 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