Skip to content

Instantly share code, notes, and snippets.

@BrianAdams
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrianAdams/e0ffdf91b6ead102715b to your computer and use it in GitHub Desktop.
Save BrianAdams/e0ffdf91b6ead102715b to your computer and use it in GitHub Desktop.
Initial example of adding other connectivity protocols to OpenROV
function curlAPI(name, deps) {
console.log('This is where curlAPI plugin code would execute in the node process.');
var sockets = [];
deps.cockpit.onAny(function(value){
var event = this.event;
if (event !== 'newListener') {
sockets.forEach(function(socket) {
socket.write(JSON.stringify([event,value]));
});
}
});
//Add some routes for express
deps.app.get('/curl-api/stream', function (req, res) {
sockets.push(res);
req.on("close", function() {
// request closed unexpectedly
var i = sockets.indexOf(res);
delete sockets[i];
});
req.on("end", function() {
// request ended normally
var i = sockets.indexOf(res);
delete sockets[i];
});
});
//To send command:
//Tilt: curl -X POST -d ".1" http://192.168.254.1:8080/curl-api/c/lights -w "%{http_code}\n"
//JSON: curl -X POST http://192.168.254.1:8080/curl-api/c/plugin.jsonapi.cmd -w "%{http_code}\r\n" -d 'data={"cat":"test", "car":"bob"}'
/*
rov@OpenROV:~$ tail -f /var/log/openrov.log | grep tilt
Sending command to arduino: tilt(1550);
tilt(1550);
cmd: tilt(1550)
Sending command to arduino: tilt(1450);
tilt(1450);
cmd: tilt(1450)
*/
deps.app.post('/curl-api/c/:cmd', function (req, res) {
console.log("[CURL-API]: " + req.params.cmd);
var paramater;
try {
paramater=JSON.parse(req.body.data);
} catch (e) {
paramater=req.body.data;
}
console.log("[CURL-API]- " + JSON.stringify(paramater));
deps.cockpit.emit(req.params.cmd,paramater);
res.status(202).end(); //Accepted
});
};
module.exports = curlAPI;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment