diff --git a/package.json b/package.json index ad3c8ad..09e4669 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,8 @@ "express": "3.0.x", "ejs": "~0.8.3", "socket.io": "~0.9.4", - "dronestream": "git+ssh://git@github.com:eschnou/node-dronestream.git#master" + "dronestream": "git+ssh://git@github.com:eschnou/node-dronestream.git#master", + "dateformat": "~1.0.6-1.2.3" }, "author": "Laurent Eschenauer ", "contributors": [ diff --git a/plugins/blackbox/index.js b/plugins/blackbox/index.js new file mode 100644 index 0000000..d3e014b --- /dev/null +++ b/plugins/blackbox/index.js @@ -0,0 +1,83 @@ +var fs = require('fs'); +var path = require('path'); +var df = require('dateformat'); + +var client + , io + , config + , navStream + , motionStream + , recording = false + ; + +function blackbox(name, deps) { + + client = deps.client; + io = deps.io; + config = deps.config; + + deps.io.sockets.on('connection', function (socket) { + socket.on('/blackbox/start', function (cmd) { + _start(); + }); + socket.on('/blackbox/stop', function (cmd) { + _stop(); + }); + }); + + deps.client.on('navdata', function(data) { + _record(data); + }); + +}; + +function _start() { + if (recording) return; + console.log("Start recording navigation data"); + if (config && config.blackbox && config.blackbox.path) { + var root = config.blackbox.path; + } else { + var root = "."; + } + var folder = df(new Date(), "yyyy-mm-dd_hh-MM-ss"); + fs.mkdir(path.join(root, folder), function() { + navStream = fs.createWriteStream(path.join(root, folder, 'navdata.txt')); + motionStream = fs.createWriteStream(path.join(root, folder, 'motion.txt')); + motionStream.write("seq,pitch,roll,yaw,xVelocity,yVelocity,zVelocity,altitude\n", function() { + recording = true; + io.sockets.emit('/message', "Blackbox started recording NavData."); + }); + }); +} + +function _stop() { + if (!recording) return; + console.log("Stopped recording navigation data"); + recording = false; + navStream.end(); + motionStream.end(); + io.sockets.emit('/message', "Blackbox stopped recording NavData."); +} + +function _record(data) { + if (!recording) return; + + navStream.write(JSON.stringify(data) + "\n"); + + var seq = data.sequenceNumber + , pitch = data.demo.rotation.pitch + , roll = data.demo.rotation.roll + , yaw = data.demo.rotation.yaw + , vx = data.demo.velocity.x + , vy = data.demo.velocity.y + , vz = data.demo.velocity.z + , z = data.demo.altitude + ; + + motionStream.write(seq + "," + pitch + "," + roll + "," + yaw + "," + vx + "," + vy + "," + vz + "," + z + "\n"); +} + +function _writeHeader(stream, cb) { +} + +module.exports = blackbox; diff --git a/plugins/blackbox/public/js/blackbox.js b/plugins/blackbox/public/js/blackbox.js new file mode 100644 index 0000000..60e19e6 --- /dev/null +++ b/plugins/blackbox/public/js/blackbox.js @@ -0,0 +1,42 @@ +(function(window, document) { + 'use strict'; + + /* + * Constructuor + */ + var Blackbox = function Blackbox(cockpit) { + console.log("Loading Blackbox plugin."); + this.cockpit = cockpit; + this.recording = false; + + // Register the various event handlers + this.listen(); + }; + + /* + * Register keyboard event listener + */ + Blackbox.prototype.listen = function listen() { + var self = this; + $(document).keydown(function(ev) { + self.keyDown(ev); + }); + }; + + /* + * Process onkeydown. + */ + Blackbox.prototype.keyDown = function keyDown(ev) { + if ([ev.keyCode] != 82) { + return; + } + ev.preventDefault(); + + this.recording = !this.recording; + var cmd = this.recording ? "stop" : "start"; + this.cockpit.socket.emit("/blackbox/" + cmd, {}); + }; + + window.Cockpit.plugins.push(Blackbox); + +}(window, document));