UNPKG

@rflafla/motec-ld-reader

Version:

A Node.js library for reading MoTeC .ld telemetry files

106 lines (105 loc) 3.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LdData = void 0; const utils_js_1 = require("./utils.js"); const ldHead_js_1 = require("./ldHead.js"); const ldChan_js_1 = require("./ldChan.js"); /** * Container for parsed data of an ld file. * * Allows reading and accessing channel data. */ class LdData { constructor(head, channels) { this.head = head; this.channels = channels; } /** * Parse data from an ld file */ static fromFile(filePath) { const buffer = (0, utils_js_1.readFileBuffer)(filePath); return this.fromBuffer(buffer); } /** * Parse data from an ld file */ static fromBuffer(buffer) { const reader = new utils_js_1.BinaryReader(buffer); // Read header const head = ldHead_js_1.LdHead.fromFile(reader); // Read channels const channels = (0, ldChan_js_1.readChannels)(reader, head.metaPtr); return new LdData(head, channels); } /** * Get a channel by name or index */ getChannel(nameOrIndex) { if (typeof nameOrIndex === 'number') { if (nameOrIndex < 0 || nameOrIndex >= this.channels.length) { throw new Error(`Channel index ${nameOrIndex} out of range`); } return this.channels[nameOrIndex]; } const matchingChannels = this.channels.filter(ch => ch.name === nameOrIndex); if (matchingChannels.length === 0) { throw new Error(`Channel '${nameOrIndex}' not found`); } if (matchingChannels.length > 1) { throw new Error(`Multiple channels with name '${nameOrIndex}' found`); } return matchingChannels[0]; } /** * Get all channel names */ getChannelNames() { return this.channels.map(ch => ch.name); } /** * Get number of channels */ get channelCount() { return this.channels.length; } /** * Convert to a simple object structure * Useful for JSON serialization or data export */ toObject() { return { metadata: { driver: this.head.driver, vehicleId: this.head.vehicleId, venue: this.head.venue, datetime: this.head.datetime.toISOString(), shortComment: this.head.shortComment, event: this.head.event?.name, session: this.head.event?.session }, channels: this.channels.map(ch => ({ name: ch.name, shortName: ch.shortName, unit: ch.unit, freq: ch.freq, data: ch.data })) }; } /** * Get channel data as a map of channel names to data arrays */ toDataMap() { const result = {}; for (const channel of this.channels) { result[channel.name] = channel.data; } return result; } toString() { const channelList = this.channels.map((ch, idx) => ` [${idx}] ${ch.toString()}`).join('\n'); return `${this.head.toString()}\n\nChannels (${this.channels.length}):\n${channelList}`; } } exports.LdData = LdData;