-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
69 lines (50 loc) · 1.85 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
exports.init = function(drone) {
var express = require('express'),
http = require('http'),
path = require('path'),
util = require('util');
var app = express(),
srv = http.createServer(app),
io = require('socket.io').listen(srv);
io.set('log level', 0);
app.configure(function() {
app.set('port', process.env.PORT || 3001);
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
drone.config('general:navdata_demo', 'TRUE');
// send required parts of navdata to the client
drone.on('navdata', function(data) {
if(!data.demo) { return; }
var clientData = {
droneState: { flying: data.droneState.flying }
};
var navdataClientKeys = ['controlState', 'flyState', 'batteryPercentage', 'altitudeMeters'];
for(var i=0, n=navdataClientKeys.length; i<n; i++) {
var k = navdataClientKeys[i];
clientData[k] = data.demo[k];
}
clientData.rotation = {
leftRight: data.demo.rotation.leftRight,
frontBack: data.demo.rotation.frontBack,
clockwise: data.demo.rotation.clockwise
};
io.sockets.emit('navdata', clientData);
//console.log(util.inspect(clientData, {colors:true}));
});
srv.listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
// notify client and serve images
var pngstream = drone.createPngStream(),
currentImg, i = 0;
pngstream.on("data", function (frame) {
currentImg = frame;
io.sockets.emit("image", "/image/" + i++);
});
app.get('/image/:id', function (req, res) {
res.writeHead(200, { "Content-Type": "image/png" });
res.end(currentImg, "binary");
});
return app;
};