diff --git a/lib/api.js b/lib/api.js index b92b854..71c4341 100644 --- a/lib/api.js +++ b/lib/api.js @@ -145,6 +145,32 @@ namespace("Cylon", function() { }); }); + this.server.get("/robots/:robotname/devices/:devicename/events/:eventname", function(req, res) { + var robotname = req.params.robotname, + devicename = req.params.devicename, + eventname = req.params.eventname; + + master.findRobotDevice(robotname, devicename, function(err, device) { + if (err) { res.json(err); } + + res.writeHead(200, { + 'Content-Type': 'text/event-stream', + 'Connection': 'keep-alive', + 'Cache-Control': 'no-cache' + }); + + var writeData = function(data) { + res.write("data: " + JSON.stringify(data) + "\n\n") + }; + + device.on(eventname, writeData); + + res.on('close', function() { + device.removeListener(eventname, writeData); + }); + }); + }); + this.server.get("/robots/:robotname/devices/:devicename/commands", function(req, res) { var robotname = req.params.robotname, devicename = req.params.devicename;