Spaces:
Paused
Paused
File size: 2,347 Bytes
d9d886e | 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 | const { MavLinkTLogPacketSplitter, MavLinkPacketParser, minimal } = require('node-mavlink')
const { vehicleTypeEnumMap, autopilotEnumMap } = require('../serialcomm_mavpyredis/SerialcommMavPyRedis.js')
const fs = require('fs')
const { Readable } = require('stream')
const { parsePBTlog, extractPBMeta } = require('../tlogParsers/pbTlogParser.js')
const extractMavMeta = (file, cb) => {
const splitter = new MavLinkTLogPacketSplitter()
const parser = new MavLinkPacketParser()
const reader = file.pipe(splitter).pipe(parser)
const REGISTRY = {
...minimal.REGISTRY
}
let startTime
let endTime
let vehicleType, autopilotType
return new Promise((resolve, reject) => {
reader.on('data', packet => {
const cls = REGISTRY[packet.header.msgid]
if (cls) {
const data = packet.protocol.data(packet.payload, cls)
if (packet.header.timestamp) {
startTime = startTime || new Date(Number(packet.header.timestamp))
endTime = new Date(Number(packet.header.timestamp))
if (data instanceof minimal.Heartbeat) {
vehicleType = vehicleTypeEnumMap[data.type]
autopilotType = autopilotEnumMap[data.autopilot]
}
} else {
console.log(data)
}
}
})
file.on('close', () => {
const differenceInMillis = Math.abs(endTime - startTime)
const differenceInSeconds = Math.floor(differenceInMillis / 1000)
const hours = Math.floor(differenceInSeconds / 3600)
const minutes = Math.floor((differenceInSeconds % 3600) / 60)
const seconds = differenceInSeconds % 60
const extractedData = {
duration: [hours, minutes, seconds],
flightDateTime: new Date(startTime).toISOString(),
vehicleType,
autopilotType
}
resolve(extractedData)
})
})
}
const extractLogMeta = (fileName, cb) => {
if (String(fileName).includes('pb-')) {
// parsePBTlog(fileName, cb)
extractPBMeta(fileName, cb)
// console.log(data)
return
}
fs.readFile(`../Adhyasta-MavPyRedis/logs/${fileName}.tlog`, async (err, file) => {
// if (err) {
// parsePBTlog(fileName, responseCallback)
// return
// }
const _file = Readable.from(file)
const data = await extractMavMeta(_file)
cb(data)
})
}
module.exports = extractLogMeta
|