-
Notifications
You must be signed in to change notification settings - Fork 427
/
Copy pathClient.js
257 lines (213 loc) · 6.51 KB
/
Client.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
var EventEmitter = require('events').EventEmitter;
var util = require('util');
Client.UdpControl = require('./control/UdpControl');
Client.Repl = require('./Repl');
Client.UdpNavdataStream = require('./navdata/UdpNavdataStream');
Client.PngStream = require('./video/PngStream');
module.exports = Client;
util.inherits(Client, EventEmitter);
function Client(options) {
EventEmitter.call(this);
options = options || {};
this.flyBit = false;
this.emergencyBit = false;
this.leftRight = 0;
this.frontBack = 0;
this.upDown = 0;
this.clockSpin = 0;
this._udpControl = options.udpControl || new Client.UdpControl(options);
this._udpNavdatasStream = options.udpNavdataStream || new Client.UdpNavdataStream(options);
this._pngStream = new Client.PngStream(options);
this._interval = null;
this._repeaters = [];
this._afterOffset = 0;
this._disableEmergency = false;
this._lastState = 'CTRL_LANDED';
this._lastBattery = 100;
this._lastAltitude = 0;
}
Client.prototype.after = function(duration, fn) {
setTimeout(fn.bind(this), this._afterOffset + duration);
this._afterOffset += duration;
return this;
};
Client.prototype.createRepl = function() {
var repl = new Client.Repl(this);
repl.resume();
return repl;
};
Client.prototype.createPngStream = function() {
this._pngStream.resume();
return this._pngStream;
};
Client.prototype.resume = function() {
this.disableEmergency();
this._setInterval(30);
this._udpNavdatasStream.removeAllListeners();
this._udpNavdatasStream.resume();
this._udpNavdatasStream
.on('error', this._maybeEmitError.bind(this))
.on('data', this._handleNavdata.bind(this));
};
Client.prototype._handleNavdata = function(navdata) {
if (navdata.droneState && navdata.droneState.emergencyLanding && this._disableEmergency) {
this.emergencyBit = true;
} else {
this.emergencyBit = false;
this._disableEmergency = false;
}
// request demo navdata if 'demo' option is not included
// also: don't emit navdata if 'demo' option is not present
// @TODO: Make this behavior configurable in the constructor
if (!navdata.demo) {
this._udpControl.config('general:navdata_demo', 'TRUE');
} else {
this.emit('navdata', navdata);
}
this._processNavdata(navdata);
};
Client.prototype._processNavdata = function(navdata) {
if (navdata.droneState && navdata.demo) {
// controlState events
var cstate = navdata.demo.controlState;
var emitState = (function(e, state) {
if (cstate === state && this._lastState !== state) {
return this.emit(e);
}
}).bind(this);
emitState('landing', 'CTRL_TRANS_LANDING');
emitState('landed', 'CTRL_LANDED');
emitState('takeoff', 'CTRL_TRANS_TAKEOFF');
emitState('hovering', 'CTRL_HOVERING');
emitState('flying', 'CTRL_FLYING');
this._lastState = cstate;
// battery events
var battery = navdata.demo.batteryPercentage;
if (navdata.droneState.lowBattery === 1) {
this.emit('lowBattery', battery);
}
if (navdata.demo.batteryPercentage !== this._lastBattery) {
this.emit('batteryChange', battery);
this._lastBattery = battery;
}
// altitude events
var altitude = navdata.demo.altitudeMeters;
if (altitude !== this._lastAltitude) {
this.emit('altitudeChange', altitude);
this._lastAltitude = altitude;
}
}
};
// emits an 'error' event, but only if somebody is listening. This avoids
// making node's EventEmitter throwing an exception for non-critical errors
Client.prototype._maybeEmitError = function(err) {
if (this.listeners('error').length > 0) {
this.emit('error', err);
}
};
Client.prototype._setInterval = function(duration) {
clearInterval(this._interval);
this._interval = setInterval(this._sendCommands.bind(this), duration);
};
Client.prototype._sendCommands = function() {
this._udpControl.ref({
flyBit : this.flyBit,
emergencyBit : this.emergencyBit
});
this._udpControl.pcmd({
leftRight : this.leftRight,
frontBack : this.frontBack,
upDown : this.upDown,
clockSpin : this.clockSpin,
});
this._udpControl.flush();
this._repeaters
.forEach(function(repeat) {
repeat.times--;
repeat.method();
});
this._repeaters = this._repeaters.filter(function(repeat) {
return repeat.times > 0;
});
};
Client.prototype.disableEmergency = function() {
this._disableEmergency = true;
};
Client.prototype.takeoff = function() {
this.flyBit = true;
};
Client.prototype.land = function() {
this.flyBit = false;
};
Client.prototype.stop = function() {
this.leftRight = 0;
this.frontBack = 0;
this.upDown = 0;
this.clockSpin = 0;
};
Client.prototype.config = function(key, value) {
// @TODO Figure out if we can get a ACK for this, so we don't need to
// repeat it blindly like this
var self = this;
this._repeat(10, function() {
self._udpControl.config(key, value);
});
};
Client.prototype.animate = function(animation, duration) {
// @TODO Figure out if we can get a ACK for this, so we don't need to
// repeat it blindly like this
var self = this;
this._repeat(10, function() {
self._udpControl.animate(animation, duration);
});
};
Client.prototype.animateLeds = function(animation, hz, duration) {
// @TODO Figure out if we can get a ACK for this, so we don't need to
// repeat it blindly like this
var self = this;
this._repeat(10, function() {
self._udpControl.animateLeds(animation, hz, duration);
});
};
Client.prototype._repeat = function(times, fn) {
this._repeaters.push({times: times, method: fn});
};
Client.prototype.left = function(speed) {
this.leftRight = speed;
};
Client.prototype.right = function(speed) {
this.leftRight = -speed;
};
Client.prototype.front = function(speed) {
this.frontBack = speed;
};
Client.prototype.back = function(speed) {
this.frontBack = -speed;
};
Client.prototype.up = function(speed) {
this.upDown = speed;
};
Client.prototype.down = function(speed) {
this.upDown = -speed;
};
Client.prototype.clockwise = function(speed) {
this.clockSpin = speed;
};
Client.prototype.counterClockwise = function(speed) {
this.clockSpin = -speed;
};
Client.prototype.toJSON = function() {
var json = {};
for (var key in this) {
var isPrivate = (key.substr(0, 1) === '_');
if (isPrivate) {
continue;
}
var value = this[key];
if (typeof value === 'function') {
continue;
}
json[key] = value;
}
return json;
};