tello-custom-ip
Version:
Tello drone client with custom IP address support, forked from @0x77/tellots
96 lines • 3.48 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.exchanger = void 0;
var constants_1 = __importDefault(require("./constants"));
var config_1 = require("./config");
var dgram_1 = __importDefault(require("dgram"));
var types_1 = require("./types");
var parsers_1 = require("./parsers");
var client = dgram_1.default.createSocket('udp4'), _local = {
state: "idle"
};
client.on('message', function (msg) {
_local.state = msg.toString();
});
client.bind(constants_1.default.ports.response);
var bindStateManagement = function (resolve, reject, parser) {
var timeoutId = setTimeout(function () {
_local.state = "error";
}, 10000);
var intervalId = setInterval(function () {
if (isIdle())
return;
if (isError())
reject(_local.state);
else {
var rawResponse = _local.state;
if (parser) {
// Parse the response using the provided parser
try {
var parsedResponse = parser(rawResponse);
resolve(parsedResponse);
}
catch (error) {
reject(error);
}
}
else {
// Return raw response if no parser is provided
resolve(rawResponse);
}
}
clearInterval(intervalId);
clearTimeout(timeoutId);
_local.state = "idle";
}, 100);
};
var isIdle = function () { return _local.state === "idle"; };
var isError = function () { return _local.state === "error"; };
var transmit = function (command) {
var message = Buffer.from(command);
client.send(message, 0, message.length, constants_1.default.ports.command, (0, config_1.getTelloIP)(), function (error) {
if (error)
_local.state = "error";
});
};
/**
* Send a command to the Tello drone
* @param command - The command to send
* @param parser - Optional parser function to convert the response to a specific type
* @param type - The type of command (used for determining default parser)
* @returns Promise resolving to the parsed response
*/
var send = function (command, parser, type) {
if (type === void 0) { type = types_1.CommandType.READ; }
return new Promise(function (resolve, reject) {
if (!isIdle())
reject("error");
// Use provided parser or default based on command type
var responseParser = parser || getDefaultParser(type);
bindStateManagement(resolve, reject, responseParser);
transmit(command);
});
};
/**
* Get the default parser based on command type
*/
var getDefaultParser = function (type) {
switch (type) {
case types_1.CommandType.CONTROL:
case types_1.CommandType.SET:
// For control and set commands, parse "ok" as true, anything else as false
return (function (response) {
return (0, parsers_1.parseCommandResponse)(response);
});
case types_1.CommandType.READ:
default:
// For read commands, return the raw string
return (function (response) { return response; });
}
};
exports.exchanger = { send: send, _local: _local };
exports.default = exports.exchanger;
//# sourceMappingURL=exchanger.js.map