forked from eschnou/ardrone-webflight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
162 lines (138 loc) · 4.31 KB
/
app.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
var express = require('express')
, app = express()
, fs = require('fs')
, path = require('path')
, server = require("http").createServer(app)
, io = require('socket.io').listen(server)
, arDrone = require('ar-drone')
, arDroneConstants = require('ar-drone/lib/constants')
;
// Fetch configuration
try {
var config = require('./config');
} catch (err) {
console.log("Missing or corrupted config file. Have a look at config.js.example if you need an example.");
process.exit(-1);
}
// Override the drone ip using an environment variable,
// using the same convention as node-ar-drone
var drone_ip = process.env.DEFAULT_DRONE_IP || '192.168.1.1';
// Keep track of plugins js and css to load them in the view
var scripts = []
, styles = []
;
app.configure(function () {
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs', { pretty: true });
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.use("/components", express.static(path.join(__dirname, 'bower_components')));
});
app.configure('development', function () {
app.use(express.errorHandler());
app.locals.pretty = true;
});
app.get('/', function (req, res) {
res.render('index', {
title: 'WebFlight'
,scripts: scripts
,styles: styles
,options: {
keyboard: config.keyboard
}
});
});
function navdata_option_mask(c) {
return 1 << c;
}
// From the SDK.
var navdata_options = (
navdata_option_mask(arDroneConstants.options.DEMO)
| navdata_option_mask(arDroneConstants.options.VISION_DETECT)
| navdata_option_mask(arDroneConstants.options.MAGNETO)
| navdata_option_mask(arDroneConstants.options.WIFI)
);
// Connect and configure the drone
var client = new arDrone.createClient({timeout:4000});
client.config('general:navdata_demo', 'TRUE');
client.config('video:video_channel', '0');
client.config('general:navdata_options', navdata_options);
// Add a handler on navdata updates
var latestNavData;
client.on('navdata', function (d) {
latestNavData = d;
});
// Signal landed and flying events.
client.on('landing', function () {
console.log('LANDING');
io.sockets.emit('landing');
});
client.on('landed', function () {
console.log('LANDED');
io.sockets.emit('landed');
});
client.on('takeoff', function() {
console.log('TAKEOFF');
io.sockets.emit('takeoff');
});
client.on('hovering', function() {
console.log('HOVERING');
io.sockets.emit('hovering');
});
client.on('flying', function() {
console.log('FLYING');
io.sockets.emit('flying');
});
// Process new websocket connection
io.set('log level', 1);
io.sockets.on('connection', function (socket) {
socket.emit('event', { message: 'Welcome to cockpit :-)' });
});
// Schedule a time to push navdata updates
var pushNavData = function() {
io.sockets.emit('navdata', latestNavData);
};
var navTimer = setInterval(pushNavData, 100);
// Prepare dependency map for plugins
var deps = {
server: server
, app: app
, io: io
, client: client
, config: config
};
// Load the plugins
var dir = path.join(__dirname, 'plugins');
function getFilter(ext) {
return function(filename) {
return filename.match(new RegExp('\\.' + ext + '$', 'i'));
};
}
config.plugins.forEach(function (plugin) {
console.log("Loading " + plugin + " plugin.");
// Load the backend code
require(path.join(dir, plugin))(plugin, deps);
// Add the public assets to a static route
if (fs.existsSync(assets = path.join(dir, plugin, 'public'))) {
app.use("/plugin/" + plugin, express.static(assets));
}
// Add the js to the view
if (fs.existsSync(js = path.join(assets, 'js'))) {
fs.readdirSync(js).filter(getFilter('js')).forEach(function(script) {
scripts.push("/plugin/" + plugin + "/js/" + script);
});
}
// Add the css to the view
if (fs.existsSync(css = path.join(assets, 'css'))) {
fs.readdirSync(css).filter(getFilter('css')).forEach(function(style) {
styles.push("/plugin/" + plugin + "/css/" + style);
});
}
});
// Start the web server
server.listen(app.get('port'), function() {
console.log('AR. Drone WebFlight is listening on port ' + app.get('port'));
});