-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGPWrapper.js
534 lines (447 loc) · 13.4 KB
/
GPWrapper.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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
const fs = require('fs')
const rd = require('readline')
const path = require('path')
const http = require('http')
const https = require('https')
const url = require('url')
const wifi = require('node-wifi')
const GoPro = require('goproh4')
const chp = require('child_process')
const { recognizeFaces, trainModel, cropFaces } = require('./faceDetection')
const { getDataPath, getAppdataPath, ensureAppdataDirExists } = require('./commons')
const ifaceName = 'wlan0' //network interface, set it to NULL for random
const gpConfigFile = `gp_nw.json`
//Function that gets the config file
const getConfig = function(){
const gpFilePath = path.resolve(getAppdataPath(), gpConfigFile)
let config = false
//Make sure the config file exists
if (fs.existsSync(gpFilePath)) {
config = require(gpFilePath)
}
return config
};
//Function that saves the config file
const saveConfig = function(config){
const gpFilePath = path.resolve(getAppdataPath(), gpConfigFile)
fs.writeFileSync(gpFilePath, JSON.stringify(config))
};
//Function that sets up a new GoPro
const setupGoPro = function(){
//Initialize wifi interface
wifi.init({
ilface: ifaceName
})
//Utility function that scans networks
const startScan = function(){
wifi.scan((err, networks) => {
if (err){
console.log("There was an error while scanning for the WiFi:\n" + err + "\n Make sure your WiFi is enabled and try again.")
}
else {
const ssidList = networks.map( nw => nw.ssid )
//Let the user choose the GoPro WiFi
console.log('Available WiFi networks:')
ssidList.forEach( (name, i) => {
console.log( (i+1) + '. ' + name)
});
console.log("")
// Ask the user for the person's name
let rl = rd.createInterface( process.stdin, process.stdout )
rl.setPrompt("Type in your ssid or choose your GoPro's from the list [1-"+ ssidList.length + "] (Hit enter to refresh the list) > " )
rl.prompt()
//Once the name was received
rl.on('line', (line) => {
rl.close()
//If we need to refresh
if (line == "") {
startScan()
}
else {
//Connect to the selected Network
if (typeof ssidList[ parseInt(line -1) ] != "undefined"){
connectToSSID(networks[ parseInt(line -1) ].ssid)
}
//If the ssid was provided
else {
connectToSSID( line )
}
}
})
}
})
};
//Utility function that connects to a GoPro
const connectToSSID = function( network ){
console.log("Connecting to: " + network)
// Ask the user for the password
let rl = rd.createInterface( process.stdin, process.stdout )
rl.setPrompt("Enter your GoPro's WiFi password > " )
rl.prompt()
//Once the name was received
rl.on('line', (line) => {
rl.close()
//We try to connect to the network
wifi.connect( { ssid: network, password: line }, (err) => {
if (err){
console.log(err)
return false;
}
//We store the GoPro's details
ensureAppdataDirExists()
let config = { ssid: network, password: line }
saveConfig(config)
firstTimeConn(config)
})
})
};
//Manage first time connection
const firstTimeConn = function(config){
// Provide the user with further instructions
let rl = rd.createInterface( process.stdin, process.stdout )
rl.setPrompt("Now turn andd keep the GoPro on by pressing its Menu button (Hit enter when the GoPro's screen is on) > " )
rl.prompt()
//Once enter was hit
rl.on('line', (line) => {
rl.close()
//We get the rest of the needed details from the GoPro
http.get('http://10.5.5.9/gp/gpControl', res => {
let body = "";
res.on("data", data => {
body += data
});
res.on("end", () => {
body = JSON.parse(body)
console.log("The GoPro has been successfully added!")
config.ap_mac = body.info.ap_mac.replace(/([0-9a-zA-Z]{2})(?=.)/g, "$1:")
config.config = body
//We save the camera's details
saveConfig(config)
})
})
});
};
//We start scanning for WiFi networks
startScan()
}
//Function that checks if there's an active connection
const verifyConnection = function (callback) {
console.log("Verifying Connection") //debug
//Initialize wifi interface
wifi.init({
ilface: ifaceName
});
// Get the the config file
let config = getConfig()
let connected = false
//Make sure the config file exists
if (!config) {
console.log("No devices have been configured. Try setting up your device first")
// Execute the callback
if (typeof callback == "function")
callback(connected)
}
else {
console.log("Getting current wifi connections") //debug
//We check for current connections
wifi.getCurrentConnections( (err, currentConnections) => {
if (err){
console.log(err)
}
//We check if we are currently connected to the GoPro
currentConnections.forEach( conn => {
connected = connected || conn.ssid == config.ssid
})
// Execute the callback with the connections
if (typeof callback == "function")
callback(connected)
})
}
};
//Function that disconnects from the GoPro
const disconnectFromGoPro = function(){
wifi.disconnect( (err) => {
if (err){
console.log(err)
}
console.log("Disconnected")
});
};
//Function that turns off the camera
const poweroff = function(){
// Get the the config file
let config = getConfig();
//We instantiate the camera
cam = new GoPro.Camera({
mac: config.ap_mac
});
cam.ready().then(function () {
// Turn the camera off
return cam.powerOff();
});
}
//Function that powers on the camera
const powerOn = function(usrCallback, confirmConnection){
// Get the the config file
let config = getConfig();
let maximumTries = 50;
let internalInterval
let cam
//The Hero 4 Session takes a little bit to register clients connected.
//When needed the connection will be confirmed with the camera (Streaming)
confirmConnection = typeof confirmConnection == "undefined" ? false : confirmConnection
confirmConnection = confirmConnection && ( config.config.info.firmware_version.indexOf("HX") >= 0 )
//Since the poweron function does not return a promise we creat a utility function that waits for the camera to be ready
const whenCamReady = function(callback){
cam.status().then(function (status) {
//We check for the connection
if (confirmConnection){
if (status.status[31] > 0) {
console.log("Status received") //debug
callback(cam, status)
}
else if (maximumTries > 0){
maximumTries--
setTimeout( function(){ whenCamReady( callback ) }, 100 );
}
}
else {
console.log("Status received") //debug
callback(cam, status)
}
}, function(e){
if (maximumTries > 0){
maximumTries--
setTimeout( function(){ whenCamReady( callback ) }, 100 );
}
else {
console.log("ERROR");
}
console.log("Error ") //debug
});
};
//If there's no config available
if (!config) {
console.log("No devices have been configured. Try setting up your device first")
// Execute the callback
if (typeof usrCallback == "function")
usrCallback(false)
}
else {
console.log("Instantiating GoPro: " + config.ap_mac) //debug
//We instantiate the camera
cam = new GoPro.Camera({
mac: config.ap_mac
});
cam.ready().then(function () {
console.log("GoPro ready, powering on") //debug
cam.powerOn()
console.log("Getting cam status:" ) //debug
whenCamReady((cam, status)=>{
usrCallback(cam, status);
})
});
}
};
//Function that takes a picture
const takePicture = function(callback){
//We verify there's a connection to the GoPro
verifyConnection( (isConnected) => {
// If there's an active connection
if (isConnected){
//We wake up the GoPro
powerOn( (cam) => {
// Set camera mode
cam.mode(GoPro.Settings.Modes.Photo, GoPro.Settings.Submodes.Photo.Single).then(function () {
// Execute the callback
if (typeof callback == "function")
callback(cam.start())
else
return cam.start()
})
});
}
})
};
//Function that lists the GoPro contents
const listContents = function() {
//We verify there's a connection to the GoPro
verifyConnection( (isConnected) => {
// If there's an active connection
if (isConnected){
//We wake up the GoPro
powerOn( (cam) => {
cam.listMedia().then(function (result) {
//For each directory the camera has
result.media.forEach(function (directory) {
console.log('[directory] =', directory.d);
// For each file in this directory
directory.fs.forEach(function (file) {
var dateTaken = new Date(file.mod * 1000); /* x1000 timestamp unix > timestamp ms */
var size = file.s / 1000000; /* byte to mb */
var name = file.n; /* filename */
if (file.g !== undefined) { // burst
var burstId = file.g;
var startingId = file.b;
var endId = file.l;
var numberOfPics = (endId - startingId) + 1;
}
console.log('[url] = ', 'http://' + cam._ip + '/videos/DCIM/' + directory.d + '/' + file.n);
});
});
});
});
}
})
};
//Function that retrieves the GoPro media
const getLatestContent = function(callback){
//We verify there's a connection to the GoPro
verifyConnection( (isConnected) => {
// If there's an active connection
if (isConnected){
//We wake up the GoPro
powerOn( (cam) => {
cam.listMedia().then(function (result) {
var lastDirectory = result.media[result.media.length - 1];
var lastFile = lastDirectory.fs[lastDirectory.fs.length - 1];
// get last media
cam.getMedia(lastDirectory.d, lastFile.n, getDataPath() + '/photos/' + lastFile.n).then(function (filename) {
// Execute the callback
if (typeof callback == "function")
callback(getDataPath() + '/photos/' + lastFile.n)
});
});
});
}
})
};
//Function to delete the latest media in the GoPro
const deleteLatestContent = function(callback){
//We verify there's a connection to the GoPro
verifyConnection( (isConnected) => {
// If there's an active connection
if (isConnected){
//We wake up the GoPro
powerOn( (cam) => {
//The latest media gets deleted
cam.deleteLast().then(function () {
console.log('Last media deleted'); //debug
// Execute the callback
if (typeof callback == "function")
callback()
});
});
}
})
};
//Function that deletes all the media in the GoPro
const deleteAllContent = function(callback){
//We verify there's a connection to the GoPro
verifyConnection( (isConnected) => {
// If there's an active connection
if (isConnected){
//We wake up the GoPro
powerOn( (cam) => {
//The latest media gets deleted
cam.deleteAll().then(function () {
console.log('Storage cleared'); //debug
// Execute the callback
if (typeof callback == "function")
callback()
});
});
}
})
};
//Function that initializes the video streaming
const startStreaming = function( config ){
//We verify there's a connection to the GoPro
verifyConnection( (isConnected) => {
// If there's an active connection
if (isConnected){
//We wake up the GoPro
powerOn( (cam) => {
//The media stream gets started
cam.restartStream().then(function () {
console.log('Stream initiated'); //debug
var spawn_process = function () {
var ffmpeg = chp.spawn("ffmpeg", config.options);
var helpBuffer = Buffer(0)
ffmpeg.stdout.pipe(process.stdout);
ffmpeg.stderr.pipe(process.stdout);
ffmpeg.on('exit', function () {
spawn_process();
});
//If there's a callback on data stdout
if (typeof config.onStdoutData == "function")
ffmpeg.stdout.on('data', function (data) { config.onStdoutData( data, helpBuffer ) })
//If there's a callback on close
if (typeof config.onClose == "function")
ffmpeg.on('close', function (code) { config.onClose(imageBuffer) });
//If there's a callback on ready
if (typeof config.onReady == "function")
onReady( ffmpeg )
};
spawn_process();
});
}, true);
}
});
};
//The functions are exported
exports.setupGoPro = setupGoPro
exports.takePicture = takePicture
exports.listContents = listContents
exports.poweroff = poweroff
exports.getLatestContent = getLatestContent
exports.deleteLatestContent = deleteLatestContent
exports.deleteAllContent = deleteAllContent
exports.startStreaming = startStreaming
//###### USAGE ######
//Use this when initial setup is needed
//setupGoPro()
//Use this to take pictures
//takePicture(function(res){
// console.log(res)
//})
//Use this function to list media
//listContents()
//Use this function to turnoff the GoPro
//poweroff()
//Use this function to download media into the dest folder
//getLatestContent((filePath) => {
// console.log(filePath)
//})
//Use this function to delete the latest photo or video
//deleteLatestContent(() => {
// console.log("Last Item Deleted")
//})
//Use this function to delete all media in the GoPro
//deleteAllContent(() => {
// console.log("Every Item Deleted")
//})
//Use this function to stream the GoPro
/*startStreaming(
{ "options": [
"-i",
"udp://:8554", // Stream input coming from the GoPro
"-probesize", //probing size to lower latency
"8192",
"-f",
"mpeg1video",
"-b",
"800k",
"-r",
"30",
("http://127.0.0.1:8082/publish")
]}
)*/
//verifyConnection( (isConnected) => {
// If there's an active connection
// if (isConnected){
// //We wake up the GoPro
// powerOn( (cam) => {
// })
// }
//})