Skip to content

Instantly share code, notes, and snippets.

@seriousManual
Last active August 29, 2015 14:27
Show Gist options
  • Save seriousManual/556facea58792779b960 to your computer and use it in GitHub Desktop.
Save seriousManual/556facea58792779b960 to your computer and use it in GitHub Desktop.
wrapper the altitude state
var Emitter = require('events').EventEmitter;
var util = require('util');
function StateWrapper(client) {
Emitter.call(this);
this._state = {
altitude: 0
};
this._handle = null;
this._client = client;
this._watch();
}
util.inherits(StateWrapper, Emitter);
StateWrapper.prototype._watch = function() {
var that = this;
this._client.on('navdata', function(data) {
if (!data) return;
if (!data.demo) return;
that._state.altitude = data.demo.altitude;
that.emit('altitude', data.demo.altitude);
});
};
StateWrapper.prototype.gotoAltitude = function(altitude, callback) {
var that = this;
var direction = null;
if (altitude < this._state.altitude) {
this._client.down(0.1);
direction = 'down';
} else {
this._client.up(0.1);
direction = 'up';
}
var handle = setInterval(function() {
if ((direction == 'up' && that._state.altitude > altitude) || (direction == 'down' && that._state.altitude < altitude)) {
that._client.stop();
clearInterval(handle);
callback();
}
}, 100);
};
module.exports = StateWrapper;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment