-
Notifications
You must be signed in to change notification settings - Fork 427
/
Copy pathClient.js
222 lines (186 loc) · 5.73 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
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._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._ref = {};
this._pcmd = {};
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));
return this;
};
Client.prototype._handleNavdata = function(navdata) {
if (navdata.droneState && navdata.droneState.emergencyLanding && this._disableEmergency) {
this._ref.emergency = true;
} else {
this._ref.emergency = false;
this._disableEmergency = false;
}
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(this._ref);
this._udpControl.pcmd(this._pcmd);
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;
return this;
};
Client.prototype.takeoff = function() {
this._ref.fly = true;
return this;
};
Client.prototype.land = function() {
this._ref.fly = false;
return this;
};
Client.prototype.stop = function() {
this._pcmd = {};
return this;
};
Client.prototype.config = function(key, value) {
if (typeof key === "object") {
for (var k in key) {
this.config(k, key[k]);
}
return this;
}
// @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);
});
return this;
};
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);
});
return this;
};
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);
});
return this;
};
Client.prototype._repeat = function(times, fn) {
this._repeaters.push({times: times, method: fn});
};
var pcmdOptions = [
['up', 'down'],
['left', 'right'],
['front', 'back'],
['clockwise', 'counterClockwise'],
]
pcmdOptions.forEach(function(pair) {
Client.prototype[pair[0]] = function(speed) {
speed = parseFloat(speed);
this._pcmd[pair[0]] = speed;
delete this._pcmd[pair[1]];
return this;
};
Client.prototype[pair[1]] = function(speed) {
speed = parseFloat(speed);
this._pcmd[pair[1]] = speed;
delete this._pcmd[pair[0]];
return this;
};
});