node-djiparsetxt
Version:
command-line application that reads a DJI '.txt' file and outputs a json.
272 lines • 12.6 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
}
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.get_jpegs = exports.get_kml = exports.get_header = exports.get_details = exports.parse_file = void 0;
var ejs_1 = __importDefault(require("ejs"));
var fs_1 = __importDefault(require("fs"));
var path_1 = __importDefault(require("path"));
var DEFAULT_IMAGE = "https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png";
var commands_1 = require("./commands");
var CliArguments_1 = require("./common/CliArguments");
var ServiceManager_1 = require("./common/ServiceManager");
var RecordTypes_1 = require("./services/RecordTypes");
function execute_cli(args) {
var argv = new CliArguments_1.CliArguments(args);
// assert cli args
if (argv.assert_args()) {
return;
}
// create managers
var serviceMan = new ServiceManager_1.ServiceManager();
var command;
var output;
// read files from arguments
var files = new commands_1.ReadFileCommand(serviceMan).exec(argv.file_paths);
for (var _i = 0, files_1 = files; _i < files_1.length; _i++) {
var file = files_1[_i];
if (argv.print_header || argv.print_records || argv.details || argv.distrib) {
command = new commands_1.PrintInfoCommand(serviceMan);
output = command.exec({
file: file,
printHeader: argv.print_header,
printRecords: argv.print_records,
printDetails: argv.details,
printDistribution: argv.distrib,
});
command = new commands_1.OutputCommand(serviceMan);
command.exec({ file: file.path, buffer: output, output: argv.output });
continue;
}
command = new commands_1.ParseRecordsCommand(serviceMan);
var records = command.exec({ file: file });
if (records.isEmpty) {
continue;
}
if (argv.unscramble) {
command = new commands_1.UnscrambleCommand(serviceMan);
command.exec({ records: records });
command = new commands_1.SerializeRecordsCommand(serviceMan);
var buffer = command.exec({ file: file, records: records });
command = new commands_1.OutputCommand(serviceMan);
output = argv.output === undefined ? path_1.default.dirname(file.path) : argv.output;
command.exec({ file: file.path + ".unscrambled", buffer: buffer, output: output });
continue;
}
if (argv.show_record != null) {
var type = argv.show_record;
command = new commands_1.ShowTypeCommand(serviceMan);
var buffer = command.exec({ type: type, records: records, file: file.path, output: argv.output });
command = new commands_1.OutputCommand(serviceMan);
output = argv.output;
command.exec({ file: file.path, buffer: buffer, output: output });
continue;
}
if (argv.jpeg) {
command = new commands_1.JpegExtractCommand(serviceMan);
command.exec({ records: records });
continue;
}
var outputString = void 0;
if (argv.csv) {
command = new commands_1.Records2CsvCommand(serviceMan);
outputString = command.exec({ records: records, output: argv.output });
}
else {
command = new commands_1.Records2JsonCommand(serviceMan);
outputString = command.exec({
records: records,
output: argv.output,
prettyPrint: argv.pretty_print,
});
}
command = new commands_1.OutputCommand(serviceMan);
output = argv.output ? argv.output : null;
command.exec({ file: file.path, buffer: outputString, output: output });
}
}
// this is what runs when called as a tool
if (require.main === module) {
try {
var args = process.argv.slice(2);
execute_cli(args);
}
catch (e) {
var processName = "node-djiparsetxt";
console.log(processName + ": " + e);
console.log(e.stack);
}
}
//#region Public API
/**
* Parse the record from the given file.
* @param buf File buffer of a log.
* @param filter Function to use as a filter for the rows, only IRecord's that return true are returned.
* @returns Array of rows with each row being
* an object where the keys are the record type.
*/
function parse_file(buf, filter) {
var serviceMan = new ServiceManager_1.ServiceManager();
var fileParsingService = serviceMan.get_service(ServiceManager_1.ServiceTypes.FileParsing);
var cacheTransService = serviceMan.get_service(ServiceManager_1.ServiceTypes.CacheTransform);
var recordsCache = fileParsingService.parse_records(buf);
cacheTransService.unscramble(recordsCache);
var unscrambledRows = cacheTransService.cache_as_rows(recordsCache);
var parsedRows = cacheTransService.rows_to_json(unscrambledRows);
// if a filter is given, apply it to the rows
if (filter !== undefined && filter !== null) {
parsedRows = parsedRows.filter(function (val) { return filter(val); });
}
return parsedRows;
}
exports.parse_file = parse_file;
/**
* Get details section of the given file.
* @param buf File buffer of a log.
* @returns Object with the parsed details section.
*/
function get_details(buf) {
var serviceMan = new ServiceManager_1.ServiceManager();
var fileInfoService = serviceMan.get_service(ServiceManager_1.ServiceTypes.FileInfo);
return fileInfoService.get_details(buf);
}
exports.get_details = get_details;
/**
* Get header section of the given file.
* @param buf File buffer of a log.
* @returns Object with the parsed header section.
*/
function get_header(buf) {
var serviceMan = new ServiceManager_1.ServiceManager();
var fileInfoService = serviceMan.get_service(ServiceManager_1.ServiceTypes.FileInfo);
return fileInfoService.get_header_info(buf);
}
exports.get_header = get_header;
/**
* Returns a KML string.
* @param buf File buffer of a log
* @param image Optional image param for the kml
* @returns Array of jpeg buffers.
*/
function get_kml(buf, image, removeNoSignalRecords) {
if (removeNoSignalRecords === void 0) { removeNoSignalRecords = false; }
return __awaiter(this, void 0, void 0, function () {
var filter, parsedRows, results, homeCoordinates, imageURL, index, rec, long, lat, location_1, templateFilePath, kmlTemplate, kml;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
filter = function (row) { return true; };
// if removeNoSignalRecords is set, we remove the frames with OSD.gps_level on zero.
if (removeNoSignalRecords) {
filter = function (row) { return row.OSD.gps_level !== 0; };
}
parsedRows = parse_file(buf, filter);
results = "";
homeCoordinates = "";
imageURL = "";
if (!image) {
imageURL = DEFAULT_IMAGE;
}
else {
imageURL = image;
}
for (index = 0; index < parsedRows.length; index += 1) {
rec = parsedRows[index];
long = rec.OSD.longitude;
lat = rec.OSD.latitude;
location_1 = long + "," + lat + " ";
results += location_1;
// if first coord, set home to it
if (index === 0) {
homeCoordinates = location_1;
}
}
templateFilePath = path_1.default.join(__dirname, "/template/kml-template.ejs");
kmlTemplate = fs_1.default.readFileSync(templateFilePath, "utf8");
return [4 /*yield*/, ejs_1.default.render(kmlTemplate, {
imageurl: imageURL,
homeCoordinates: homeCoordinates,
coordinates: results,
})];
case 1:
kml = _a.sent();
return [2 /*return*/, kml];
}
});
});
}
exports.get_kml = get_kml;
/**
* Get the jpegs in file.
* @param buf File buffer of a log
* @returns Array of jpeg buffers.
*/
function get_jpegs(buf) {
var jpegs = [];
var serviceMan = new ServiceManager_1.ServiceManager();
var fileParsingService = serviceMan.get_service(ServiceManager_1.ServiceTypes.FileParsing);
var cacheTransService = serviceMan.get_service(ServiceManager_1.ServiceTypes.CacheTransform);
var cache = fileParsingService.parse_records(buf);
cacheTransService.unscramble(cache);
var jpegRecords = cache.records.filter(function (rec) { return rec.type === RecordTypes_1.RecordTypes.JPEG; });
for (var _i = 0, jpegRecords_1 = jpegRecords; _i < jpegRecords_1.length; _i++) {
var record = jpegRecords_1[_i];
// ignore zero bytes images
if (record.data[0].length === 0)
continue;
jpegs = jpegs.concat(record.data);
}
return jpegs;
}
exports.get_jpegs = get_jpegs;
__exportStar(require("./shared/interfaces"), exports);
//#endregion
//# sourceMappingURL=node-djiparsetxt.js.map