-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
99 lines (70 loc) · 2.29 KB
/
server.js
File metadata and controls
99 lines (70 loc) · 2.29 KB
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
"use strict";
/*
node-dji
License in LICENSE file
*/
const config = {
// This key should be equal to one in mobile app settings
ioAuthKey: 'abcd1234',
// Server port to listen to
ioServerPort: 8005
};
const io_server = require('socket.io')(config.ioServerPort);
const NodeDJI = require('./node-dji/node-dji');
// Server connection handler
io_server.on('connection', socket => {
// Check if AuthKey is OK
let authKey_from_app = socket.handshake.headers.authkey;
if( authKey_from_app !== config.ioAuthKey ){
console.log("Connection with wrong auth key", authKey_from_app, "Aborting...");
socket.disconnect(true);
return;
}
console.log('Drone connected!');
//
// Create new NodeDJI instance
let drone = new NodeDJI(socket);
//
// Get drone info
//
console.log("Drone's name:", drone.getName() );
console.log("Drone's SN:", drone.getSerialNumber() );
//
// Listeners for streaming data
//
// Print out app state values
drone.on('appState', values => console.log(values) );
// Print out common telemetry values
drone.on('commonTelemetry', values => console.log(values) );
// Print out app attitude telemetry values
drone.on('attitudeTelemetry', values => console.log(values) );
// Drone disconnected
drone.on('disconnect', reason => console.log('Drone disconnected, reason', reason) );
//
// Perform actions
//
drone.takeOff()
.then( response => {
// Command executed on the app
console.log("Takeoff command succeeded with", response);
})
.catch( error_message => {
console.log('Failed to takeoff', error_message);
});
drone.land()
.then( response => {
// Command executed on the app
console.log("Land command succeeded with", response);
})
.catch( error_message => {
console.log('Failed to land', error_message);
});
drone.returnToHome()
.then( response => {
// Command executed on the app
console.log("RTH command succeeded with", response);
})
.catch( error_message => {
console.log('Failed to RTH', error_message);
});
});