More cleanup, seemingly reading timestamp headers now

main
Jacob Windle 2023-04-27 11:08:54 -04:00
parent dd3dc2525d
commit 33e1092030
1 changed files with 25 additions and 9 deletions

View File

@ -33,6 +33,7 @@ Contains the header byte for a record row
mutable struct RecordHeader
type::RecordType
local_mesg_type::Int64
time_offset::Union{Nothing, Int64}
end
struct FieldDefinition
@ -48,6 +49,16 @@ mutable struct DefinitionBody
field_definitions::Vector{FieldDefinition}
end
"""
LocalMesgTypeMapping
For use in mapping local message type to a struct containing useful info.
"""
struct LocalMesgTypeMapping
row_sz::Int64
global_mesg_type::AbstractString
end
mutable struct DataBody
end
@ -67,7 +78,7 @@ mutable struct FITFileReader
bytes_read::Int64
header::Union{FITHeader, Nothing}
body::Union{Vector{DataRecord}, Nothing}
mesg_map::Dict{Real, AbstractString}
mesg_map::Dict{Real, LocalMesgTypeMapping}
next_row_sz::Int64
end
@ -128,16 +139,22 @@ end
Decode the header byte, determine if the record is a definition, data, or timestamp.
"""
function decode_header(v::Vector{UInt8})::RecordHeader
type = if v[7] == 1
timestamp
elseif v[6] == 1
type = if v[8] == 1
timestamp
elseif v[7] == 1
definition
else
data
end
local_mesg_type = sum_bits(v[1:4])
RecordHeader(type, local_mesg_type)
if type == timestamp
local_mesg_type = sum_bits(v[6:7])
time_offset = sum_bits(v[1:5])
RecordHeader(type, local_mesg_type, time_offset)
else
local_mesg_type = sum_bits(v[1:4])
RecordHeader(type, local_mesg_type, nothing)
end
end
"""
@ -165,7 +182,7 @@ Using f, read the appropriate message body given the hdr (which tells us if this
"""
function read_record_content!(f::FITFileReader, hdr::RecordHeader)::Union{DefinitionBody, DataBody}
# Read the definition record, return the body
if hdr.definition
if hdr.type == definition
# Read all bytes first
header_bytes = read_bytes!(f, 5)
header_field_definitions = read_bytes!(f, header_bytes[end] * 3) # sz * field size
@ -185,8 +202,7 @@ function read_record_content!(f::FITFileReader, hdr::RecordHeader)::Union{Defini
row_sz = sum(get_row_size.([field_definitions, developer_field_definitions]))
# Associate the global message type with this local message type.
f.mesg_map[hdr.local_mesg_type] = global_mesg_type
f.next_row_sz = row_sz
f.mesg_map[hdr.local_mesg_type] = LocalMesgTypeMapping(row_sz, global_mesg_type)
DefinitionBody(
header_bytes[2] == 0 ? "littleendian" : "bigendian",