-
Notifications
You must be signed in to change notification settings - Fork 2
/
drone.js
275 lines (227 loc) · 6.42 KB
/
drone.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
const logger = require('morgan');
const bodyParser = require('body-parser');
const path = require('path');
const { spawn } = require('child_process');
const dgram = require('dgram');
const express = require('express');
const app = express();
const throttle = require('lodash/throttle');
const ws = require('ws');
const axios = require('axios');
const TELLO_COMMAND_PORT = 8889;
const TELLO_STATE_PORT = 8890;
const TELLO_VIDEO_PORT = 11111;
const TELLO_HOST = '192.168.10.1';
const SERVER_PORT = 6767;
const SERVER_HOST = 'localhost';
/**
* ============================
* Drone Config
* ============================
*/
// Init drone connection
const drone = dgram.createSocket('udp4');
drone.bind(TELLO_COMMAND_PORT);
// Init drone SDK.
drone.send('command', 0, 'command'.length, TELLO_COMMAND_PORT, TELLO_HOST, handleError);
// Command response
drone.on('message', message => {
console.log(`🤖 : ${message}`);
});
// Listen drone state messages
// e.g temp or angles
const droneState = dgram.createSocket('udp4');
droneState.bind(TELLO_STATE_PORT);
droneState.on(
'message',
throttle(state => {
const formattedState = parseState(state.toString());
// uncomment to see the data in the terminal
// console.log('drone messages', formattedState)
}, 100)
);
function parseState(state) {
return state
.split(';')
.map(x => x.split(':'))
.reduce((data, [key, value]) => {
data[key] = value;
return data;
}, {});
}
function handleError(err) {
if (err) {
console.log('ERROR');
console.log(err);
}
}
function sendCommand(command) {
drone.send(command, 0, command.length, TELLO_COMMAND_PORT, TELLO_HOST, handleError);
}
/**
* ============================
* Server/Express Config
* ============================
*/
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Enable static assets for FE
app.use(express.static(path.join(__dirname, 'public')))
// APIs to trigger commands from Node-Red
app.post('/command', ({ body }, res) => {
const command = body.command;
console.log(`Command: ${command}`);
sendCommand(command)
res.sendStatus(200);
});
app.post(`/streamon`, (req, res) => {
console.log('Starting stream.')
const command = 'streamon';
sendCommand(command);
res.end()
})
app.post(`/streamoff`, (req, res) => {
console.log('Stopping stream.')
const command = 'streamoff';
sendCommand(command);
res.end()
})
app.post(`/testing`, (req, res) => {
console.log(req.body)
res.end()
})
// Server Web page to stream video
app.get('/', (req, res, next) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'))
}); // takeoff, land, flip f
app.post(`/tellostream`, (req, res) => {
res.connection.setTimeout(0)
console.log(
`Stream Connected: ${req.socket.remoteAddress}:${req.socket.remotePort}`
)
req.on('data', function (data) {
wsServer.broadcast(data)
})
req.on('end', function () {
console.log(
`Stream Disconnected: ${req.socket.remoteAddress}:${req.socket.remotePort}`
)
})
})
const server = app.listen(SERVER_PORT, SERVER_HOST, () => {
const host = server.address().address
const port = server.address().port
console.log(`Server started at http://${host}:${port}/`)
})
/**
* ============================
* Web Hook Relay Socket Config
* ============================
*/
const webRelayHookURL = 'wss://my.webhookrelay.com/v1/socket';
const reconnectInterval = 1000 * 3;
let webRelaySocket;
const apiKey = process.env.RELAY_KEY;
const apiSecret = process.env.RELAY_SECRET;
const connect = function () {
webRelaySocket = new ws(webRelayHookURL);
webRelaySocket.on('open', function () {
console.log('Connected to Web Relay, sending authentication request');
webRelaySocket.send(JSON.stringify({ action: 'auth', key: apiKey, secret: apiSecret }));
});
webRelaySocket.on('message', async function incoming(data) {
const { type, status, body } = JSON.parse(data);
if (type === 'status' && status === 'authenticated') {
webRelaySocket.send(JSON.stringify({ action: 'subscribe', buckets: ['gactions'] }));
}
// trigger command from Google Assistant -> Web Relay Hook -> Drone
ifttAction(body);
});
webRelaySocket.on('error', function () {
console.log('socket error');
});
webRelaySocket.on('close', function () {
console.log('socket closed, reconnecting');
setTimeout(connect, reconnectInterval);
});
};
connect();
async function ifttAction(body) {
if (body) {
const { action } = JSON.parse(body);
if (action) {
console.log('Action coming from IFTTT', action);
if (action === 'streamon') {
await axios.post('http://localhost:6767/command', { command: 'streamon' })
return;
}
sendCommand(action);
}
}
}
/**
* ============================
* Video Streaming Config
* ============================
*/
const wsServer = new ws.Server({ server: server })
wsServer.on('connection', function (socket, upgradeReq) {
const remoteAddress = (upgradeReq || socket.upgradeReq).socket.remoteAddress
console.log(
`WebSocket Connected: ${remoteAddress} (${wsServer.clients.size} total)`
)
socket.on('close', function (code, message) {
console.log(
`WebSocket Disonnected: ${remoteAddress} (${wsServer.clients.size} total)`
)
})
})
wsServer.broadcast = function (data) {
wsServer.clients.forEach(function each(client) {
if (client.readyState === ws.OPEN) {
client.send(data)
}
})
}
const ffmpeg = spawn('ffmpeg', [
'-hide_banner',
'-i',
`udp://${TELLO_HOST}:${TELLO_VIDEO_PORT}`,
'-f',
'mpegts',
'-codec:v',
'mpeg1video',
'-s',
'640x480',
'-b:v',
'800k',
'-bf',
'0',
'-r',
'20',
`http://${SERVER_HOST}:${SERVER_PORT}/tellostream`
])
ffmpeg.stderr.on('data', data => {
console.log(`stderr: ${data}`)
})
ffmpeg.on('close', code => {
console.log(`child process exited with code ${code}`)
})
// Safely fill ffmpeg
const exitHandler = options => {
if (options.cleanup) {
ffmpeg.stderr.pause()
ffmpeg.stdout.pause()
ffmpeg.stdin.pause()
ffmpeg.kill()
}
if (options.exit) {
process.exit()
}
}
process.on('exit', exitHandler.bind(null, { cleanup: true }))
process.on('SIGINT', exitHandler.bind(null, { exit: true }))
process.on('SIGUSR1', exitHandler.bind(null, { exit: true }))
process.on('SIGUSR2', exitHandler.bind(null, { exit: true }))
process.on('uncaughtException', exitHandler.bind(null, { exit: true }))