-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
516 lines (450 loc) · 13.3 KB
/
main.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
const tello = require('tello-drone');
const express = require('express');
const { spawn } = require('child_process');
const path = require('path');
const ws = require('ws');
const fs = require('fs');
const os = require('os');
const { outputFile, remove } = require('fs-extra');
const { ipcMain, app, dialog, shell, BrowserWindow, Menu } = require('electron')
const log = require('electron-log');
const { autoUpdater } = require("electron-updater");
let winMarkerConfig, aboutBrowserWindow;
autoUpdater.logger = log;
autoUpdater.logger.transports.file.level = 'info';
log.info('App starting...');
const version = app.getVersion()
log.info('Hermes version', version);
const droneState = {
vgx: 0,
vgy: 0,
vgz: 0,
bat: 0,
time: 0,
h: 0,
temph: 0,
templ: 0,
pitch: 0,
yaw: 0,
baro: 0,
agx: 0,
agy: 0,
agz: 0,
tof: 0
}
// Normalize platform for path to ffmpeg binary
let platform = os.platform()
if (platform == "darwin") {
platform = "mac";
} else if (platform == "win32") {
platform = "win";
}
// Get path to resources directory if app is built
var getResourcesPath = function () {
var paths = Array.from(arguments)
if (platform === 'mac') {
if (/[\\/]Electron\.app[\\/]/.test(process.execPath)) {
paths.unshift(path.join(process.cwd(), 'resources'));
} else {
// In builds, the resources directory is located in 'Contents/Resources'
paths.unshift(process.resourcesPath)
}
} else {
// win
paths.unshift(path.join(process.cwd(), 'resources'))
}
return path.join.apply(null, paths)
}
// TODO: Support Windows with ffmpeg.exe binary
const platformPath = getResourcesPath(platform)
const ffmpegpath = path.join(platformPath, 'bin', 'ffmpeg');
const exp = express()
const drone = tello.connect();
let win;
let flying;
const TELLO_VIDEO_PORT = 11111
const TELLO_HOST = '192.168.10.1'
const HOST = 'localhost';
const PORT = 3000;
// Store photos and videos in the default directories
const imageDir = app.getPath('pictures') + "/Hermes/";
const videoDir = app.getPath('videos') + "/Hermes/";
// Create the directories if they doesn't already exist
mkDir(imageDir);
mkDir(videoDir);
function mkDir(path) {
fs.opendir(path, (err, dir) => {
if (err) {
fs.mkdir(path, (err) => {
if (err) throw error;
})
}
dir.close();
});
}
ipcMain.on('tryConnect', (event, arg) => {
drone.send('command');
// console.log('Tried Connecting to Drone');
});
ipcMain.on('greenflag', (event, code) => {
eval(code);
});
ipcMain.on('takeoff', (event, arg) => {
drone.send('takeoff');
flying = true;
console.log('flying: ', flying);
win.webContents.send('flying', true);
});
ipcMain.on('land', (event, arg) => {
drone.send('land');
flying = false;
console.log('flying: ', flying);
win.webContents.send('flying', false);
});
ipcMain.on('emergency', (event, arg) => {
drone.send('emergency');
flying = false;
win.webContents.send('flying', false);
});
ipcMain.on('rc', (event, arg) => {
drone.send("rc", { a: arg.leftRight, b: arg.forBack, c: arg.upDown, d: arg.yaw });
});
function takePhoto() {
win.webContents.send('takePhoto');
}
ipcMain.on('save_photo', (event, fileName, img) => {
const filePath = imageDir + fileName;
outputFile(filePath, img, (err) => {
if (err) {
event.sender.send('save_file_error', err.message);
} else {
event.reply('saved_file', filePath);
}
})
})
function startVideo() {
win.webContents.send('startVideo');
}
function stopVideo() {
win.webContents.send('stopVideo');
}
ipcMain.on('save_video', (event, fileName, buffer) => {
const webmFilename = videoDir + fileName + '.webm';
const mp4Filename = videoDir + fileName + '.mp4';
outputFile(webmFilename, buffer, (err) => {
if (err) {
event.sender.send('save_file_error', err.message);
} else {
const ffmpeg = spawn(ffmpegpath, [
'-y',
'-i',
webmFilename,
'-c:v',
'mpeg4',
'-strict', 'experimental',
mp4Filename
])
ffmpeg.stderr.on('data', data => {
console.log(`stderr: ${data}`)
})
ffmpeg.on('close', code => {
console.log(`child process exited with code ${code}`);
remove(webmFilename);
event.reply('saved_file', mp4Filename);
})
}
})
})
exp.use(express.static(path.join(__dirname, 'public')))
exp.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}`
)
})
});
drone.on("state", state => {
// console.log("Received State > ", state);
if (win.webContents) {
win.webContents.send('dronestate', state);
Object.assign(droneState, state);
}
});
drone.on("send", (err, length) => {
if (err) console.log(err);
// console.log(`Sent command is ${length} long`);
});
drone.on("message", message => {
console.log("Received Message > ", message);
});
drone.on("connection", async () => {
try {
await drone.send("battery?");
await drone.send("streamon");
console.log("Connected to drone");
} catch (error) {
console.log(error)
}
});
function startVideoStream() {
console.log('starting ffmpeg')
// ffmpeg -i udp://192.168.10.1:11111 -f mpegts -codec:v mpeg1video -s 640x480 -b:v 800k -r 20 -bf 0 http://127.0.0.1:8081/tellostream
const ffmpeg = spawn(ffmpegpath, [
'-hide_banner',
'-i',
`udp://${TELLO_HOST}:${TELLO_VIDEO_PORT}`,
'-f',
'mpegts',
'-codec:v',
'mpeg1video',
'-s',
'1280x720',
'-b:v',
'1000k',
'-bf',
'0',
'-r',
'30',
`http://${HOST}:${PORT}/tellostream`
])
ffmpeg.stderr.on('data', data => {
console.log(`stderr: ${data}`)
})
ffmpeg.on('close', code => {
console.log(`child process exited with code ${code}`)
})
}
startVideoStream();
const server = exp.listen(PORT, HOST, () => {
const host = server.address().address
const port = server.address().port
console.log(`Server started at http://${host}:${port}/`)
});
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)
}
})
}
function configureMarkers() {
if (!winMarkerConfig) {
winMarkerConfig = new BrowserWindow({
width: 472,
height: 600,
resizable: false,
webPreferences: {
nodeIntegration: true
}
});
winMarkerConfig.on('closed', () => {
winMarkerConfig = null
})
winMarkerConfig.loadFile('public/configMarkers.html');
}
}
function aboutWindow() {
if (!aboutBrowserWindow) {
aboutBrowserWindow = new BrowserWindow({
width: 480,
height: 540,
resizable: false,
webPreferences: {
nodeIntegration: true
}
});
aboutBrowserWindow.on('closed', () => {
aboutBrowserWindow = null
})
aboutBrowserWindow.webContents.on('new-window', function(e, url) {
e.preventDefault();
shell.openExternal(url);
});
aboutBrowserWindow.setMenu(null);
aboutBrowserWindow.loadFile('public/aboutBrowserWindow.html');
aboutBrowserWindow.webContents.on('did-finish-load', () => {
aboutBrowserWindow.webContents.send('version', version);
})
}
}
function createWindow() {
// Create the browser window.
win = new BrowserWindow({
width: 1280,
height: 800,
webPreferences: {
nodeIntegration: true
}
})
win.loadFile('public/index.html');
win.on('closed', () => {
win = null;
if (wsServer) wsServer.close();
});
const template = [
{
label: 'Hermes',
submenu: [
{
label: 'About',
click() {
aboutWindow();
}
},
{
label: 'Quit',
click() {
app.quit()
},
accelerator: 'CmdOrCtrl+Q'
}
]
},
{
label: 'File',
submenu: [
{
label: 'Save',
click() {
// Send save message
win.webContents.send('file', 'save');
},
accelerator: 'CmdOrCtrl+S'
},
{
label: 'Open',
click() {
// Send open message
win.webContents.send('file', 'open');
},
accelerator: 'CmdOrCtrl+O'
}
]
},
{
label: 'View',
submenu: [
{ type: 'separator' },
{ role: 'reload' },
{ role: 'forcereload' },
{ type: 'separator' },
{ role: 'resetzoom' },
{ role: 'zoomin' },
{ role: 'zoomout' },
{ type: 'separator' },
{ role: 'togglefullscreen' }
]
},
{
label: 'Developer',
submenu: [
{ role: 'toggledevtools' },
{ type: 'separator' },
{
label: 'Display JavaScript',
type: 'checkbox',
checked: false,
click: e => {
win.webContents.send('displayJS', e.checked);
}
},
{
label: 'Configure markers',
click() {
configureMarkers();
},
},
{
label: 'Use local camera',
type: 'checkbox',
checked: false,
click: e => {
win.webContents.send('useLocalCamera', e.checked);
}
}
]
},
{ role: 'windowMenu' },
{
label: 'Help',
submenu: [
{
label: 'Open Documentation',
click: e => {
shell.openExternal('https://hermes.orange.haus');
}
},
{
label: 'Submit Bug Report',
click: e => {
shell.openExternal('https://github.com/tgb20/Hermes/issues');
}
}
]
}
];
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
}
/* Auto-update */
autoUpdater.on('checking-for-update', () => {
// It's quiet here.
});
autoUpdater.on('update-available', (info) => {
// Download automatically
});
autoUpdater.on('update-not-available', (info) => {
// It's quiet here.
});
autoUpdater.on('error', (err) => {
log.error(err);
})
autoUpdater.on('download-progress', (progressObj) => {
let log_message = "Download speed: " + progressObj.bytesPerSecond;
log_message = log_message + ' - Downloaded ' + progressObj.percent + '%';
log_message = log_message + ' (' + progressObj.transferred + "/" + progressObj.total + ')';
log.info(log_message);
})
autoUpdater.on('update-downloaded', (info) => {
options = {
title: 'Hermes Updates',
message: 'An update is ready and will be installed the next time you run Hermes.',
buttons: ["Restart now", "Keep working"],
defaultId: 0,
cancelId: 1
};
dialog.showMessageBox(win, options).then( (res, checked) => {
if (res.response === 0) {
const isSilent = false;
const isForceRunAfter = true;
autoUpdater.quitAndInstall(isSilent, isForceRunAfter);
}
});
});
app.on('ready', function() {
autoUpdater.checkForUpdatesAndNotify();
});
app.whenReady().then(createWindow);
const updateMarkers = () => {
win.webContents.send('updateMarkers');
}
module.exports.updateMarkers = updateMarkers;