Formatting and added errors to the var header parser

This commit is contained in:
2024-10-27 15:29:48 +00:00
parent eeea06942a
commit fdc53f80e7
5 changed files with 61 additions and 60 deletions
+2 -2
View File
@@ -7,7 +7,7 @@ import (
) )
const ( const (
SubHeaderSize = 32 // SubHeaderSize is the size of the subheader SubHeaderSize = 32 // SubHeaderSize is the size of the subheader
) )
// DiskSubHeader represents the IBT sub headers // DiskSubHeader represents the IBT sub headers
@@ -23,7 +23,7 @@ type DiskSubHeader struct {
// or nil if an error occurs. In which case the error return value is more // or nil if an error occurs. In which case the error return value is more
// valuable // valuable
func parseTelemetrySubHeader(buf [SubHeaderSize]byte) (*DiskSubHeader, error) { func parseTelemetrySubHeader(buf [SubHeaderSize]byte) (*DiskSubHeader, error) {
dst := DiskSubHeader{} dst := DiskSubHeader{}
err := binary.Read(bytes.NewBuffer(buf[:]), binary.LittleEndian, &dst) err := binary.Read(bytes.NewBuffer(buf[:]), binary.LittleEndian, &dst)
if err != nil { if err != nil {
return nil, err return nil, err
+18 -18
View File
@@ -13,30 +13,30 @@ const (
// TelemetryHeaders struct to hold an IBT file's headers // TelemetryHeaders struct to hold an IBT file's headers
type TelemetryHeaders struct { type TelemetryHeaders struct {
Version int32 Version int32
// Status of 1 indicates a completed session and status of 0 a live session // Status of 1 indicates a completed session and status of 0 a live session
Status int32 Status int32
// TickRate indicates the frequency of writes (usually 60) // TickRate indicates the frequency of writes (usually 60)
TickRate int32 TickRate int32
// SessionInfoUpdate indicates the number of times the SessionInfo was // SessionInfoUpdate indicates the number of times the SessionInfo was
// updated. 0 for finished sessions and >1 for active sessions // updated. 0 for finished sessions and >1 for active sessions
SessionInfoUpdate int32 SessionInfoUpdate int32
// SessionInfoLength is the length of the session info buffer // SessionInfoLength is the length of the session info buffer
SessionInfoLength int32 SessionInfoLength int32
// SessionInfoOffset is the offset of the session info in the buffer // SessionInfoOffset is the offset of the session info in the buffer
SessionInfoOffset int32 SessionInfoOffset int32
// NumVars is the number of variables in each input // NumVars is the number of variables in each input
NumVars int32 NumVars int32
// VarHeaderOffset is the offset of the VarHeader // VarHeaderOffset is the offset of the VarHeader
VarHeaderOffset int32 VarHeaderOffset int32
// NumBuf will be 1 for static files and 3 for live telemetry files // NumBuf will be 1 for static files and 3 for live telemetry files
NumBuf int32 NumBuf int32
// BufLen is the length for parsing VarHeader values // BufLen is the length for parsing VarHeader values
BufLen int32 BufLen int32
// Padding // Padding
Padding [12]byte Padding [12]byte
// I still don't know what this is: // I still don't know what this is:
BufOffset int32 BufOffset int32
} }
// ToString renders a string showing the values of the struct // ToString renders a string showing the values of the struct
+19 -17
View File
@@ -11,7 +11,6 @@ import (
const ( const (
ibtFile = "./telemetryFiles/mx5_2016Okayama_full_2024_10_19_22_02_12.ibt" ibtFile = "./telemetryFiles/mx5_2016Okayama_full_2024_10_19_22_02_12.ibt"
// ibtFile = "./telemetryFiles/sample.ibt"
) )
// Reader is an interface to represent the readable data that can be either // Reader is an interface to represent the readable data that can be either
@@ -45,7 +44,7 @@ func Init(f Reader) (*IBT, error) {
var headerRaw [FileHeaderSize]byte var headerRaw [FileHeaderSize]byte
_, err = ibt.File.ReadAt(headerRaw[:], 0) _, err = ibt.File.ReadAt(headerRaw[:], 0)
if err != nil { if err != nil {
return nil, fmt.Errorf("Failed to read from file: %v", err) return nil, fmt.Errorf("Failed to read headers from file: %v", err)
} }
ibt.Headers, err = parseTelemetryHeader(headerRaw) ibt.Headers, err = parseTelemetryHeader(headerRaw)
if err != nil { if err != nil {
@@ -56,11 +55,11 @@ func Init(f Reader) (*IBT, error) {
var subheaderRaw [SubHeaderSize]byte var subheaderRaw [SubHeaderSize]byte
_, err = ibt.File.ReadAt(subheaderRaw[:], 112) _, err = ibt.File.ReadAt(subheaderRaw[:], 112)
if err != nil { if err != nil {
return nil, fmt.Errorf("Failed to read subheader from file: %v", err) return nil, fmt.Errorf("Failed to read disk subheaders from file: %v", err)
} }
ibt.SubHeaders, err = parseTelemetrySubHeader(subheaderRaw) ibt.SubHeaders, err = parseTelemetrySubHeader(subheaderRaw)
if err != nil { if err != nil {
return nil, fmt.Errorf("Unable to parse subheaders from file: %v", err) return nil, fmt.Errorf("Unable to parse disk subheaders from file: %v", err)
} }
// Read session info string // Read session info string
@@ -71,11 +70,14 @@ func Init(f Reader) (*IBT, error) {
} }
ibt.SessionInfo, err = parseSessionInfo(sessionInfoStringRaw) ibt.SessionInfo, err = parseSessionInfo(sessionInfoStringRaw)
if err != nil { if err != nil {
return nil, fmt.Errorf("Unable to parse Session Info from file: %v", err) return nil, fmt.Errorf("Unable to parse SessionInfoString from file: %v", err)
} }
// Read the telemetry vars info // Read the telemetry vars info
ibt.readVariablerHeaders() err = ibt.readVariablerHeaders()
if err != nil {
return nil, fmt.Errorf("Unable to parser variable headers from file: %v", err)
}
return &ibt, nil return &ibt, nil
} }
@@ -96,19 +98,19 @@ func main() {
fmt.Printf("%s\n", ibt.Headers.ToString()) fmt.Printf("%s\n", ibt.Headers.ToString())
fmt.Printf("%s\n", ibt.SubHeaders.ToString()) fmt.Printf("%s\n", ibt.SubHeaders.ToString())
// Display the human readable start date // Display the human readable start date
unixStartDate := time.Unix(ibt.SubHeaders.StartDate, 0) unixStartDate := time.Unix(ibt.SubHeaders.StartDate, 0)
startDate := unixStartDate.Format("2006/01/02 15:04:05 -0700 MST") startDate := unixStartDate.Format("2006/01/02 15:04:05 -0700 MST")
fmt.Println("StartDate:", startDate) fmt.Println("StartDate:", startDate)
// Display the human readable version of start time // Display the human readable version of start time
unixStartTime := time.Unix(ibt.SubHeaders.StartDate + int64(ibt.SubHeaders.StartTime), 0) unixStartTime := time.Unix(ibt.SubHeaders.StartDate+int64(ibt.SubHeaders.StartTime), 0)
startTime := unixStartTime.Format("2006/01/02 15:04:05 -0700 MST") startTime := unixStartTime.Format("2006/01/02 15:04:05 -0700 MST")
fmt.Println("StartTime:", startTime) fmt.Println("StartTime:", startTime)
// Display the human readable version of end time // Display the human readable version of end time
unixEndTime := time.Unix(ibt.SubHeaders.StartDate + int64(ibt.SubHeaders.EndTime), 0) unixEndTime := time.Unix(ibt.SubHeaders.StartDate+int64(ibt.SubHeaders.EndTime), 0)
endTime := unixEndTime.Format("2006/01/02 15:04:05 -0700 MST") endTime := unixEndTime.Format("2006/01/02 15:04:05 -0700 MST")
fmt.Println("EndTime: ", endTime) fmt.Println("EndTime: ", endTime)
last := time.Now().UnixMilli() last := time.Now().UnixMilli()
@@ -133,5 +135,5 @@ func main() {
} }
} }
fmt.Printf("%d\n", ibt.Tick) fmt.Printf("%d\n", ibt.Tick)
} }
+8 -9
View File
@@ -307,18 +307,17 @@ type Driver struct {
// parseSessionInfo will parse the sessionInfo buffer into the SessionInfoYAML // parseSessionInfo will parse the sessionInfo buffer into the SessionInfoYAML
// struct // struct
func parseSessionInfo(data []byte) (*SessionInfoYAML, error) { func parseSessionInfo(data []byte) (*SessionInfoYAML, error) {
var sessionInfo SessionInfoYAML var sessionInfo SessionInfoYAML
err := yaml.Unmarshal(data, &sessionInfo) err := yaml.Unmarshal(data, &sessionInfo)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &sessionInfo, nil return &sessionInfo, nil
} }
// ToString will return a readable string of the struct // ToString will return a readable string of the struct
func (s *SessionInfoYAML) ToString() string { func (s *SessionInfoYAML) ToString() string {
stringified, _ := json.MarshalIndent(s, "", " ") stringified, _ := json.MarshalIndent(s, "", " ")
return string(stringified) return string(stringified)
} }
+14 -14
View File
@@ -4,14 +4,14 @@ import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"io"
"log" "log"
"math"
"strings" "strings"
"math"
"io"
) )
const ( const (
VarSize = 144 VarHeaderSize = 144
IRSDK_char = 0 IRSDK_char = 0
IRSDK_bool = 1 IRSDK_bool = 1
IRSDK_int = 2 IRSDK_int = 2
@@ -88,26 +88,24 @@ type TelemetryVars struct {
Vars map[string]Var Vars map[string]Var
} }
func (i *IBT) readVariablerHeaders() { func (i *IBT) readVariablerHeaders() error {
i.Vars = &TelemetryVars{Vars: make(map[string]Var, i.Headers.NumVars)} i.Vars = &TelemetryVars{Vars: make(map[string]Var, i.Headers.NumVars)}
var k int32 var k int32
for k = 0; k < i.Headers.NumVars; k++ { for k = 0; k < i.Headers.NumVars; k++ {
rbuf := make([]byte, VarSize) rbuf := make([]byte, VarHeaderSize)
_, err := i.File.ReadAt(rbuf, int64(i.Headers.VarHeaderOffset+k*VarSize)) _, err := i.File.ReadAt(rbuf, int64(i.Headers.VarHeaderOffset+k*VarHeaderSize))
if err != nil { if err != nil {
log.Fatal(err) return err
} }
var dst IBTVar var dst IBTVar
err = binary.Read(bytes.NewBuffer(rbuf[:]), binary.LittleEndian, &dst) err = binary.Read(bytes.NewBuffer(rbuf[:]), binary.LittleEndian, &dst)
if err != nil { if err != nil {
log.Fatal(err) return err
} }
fmt.Println(dst.Name)
v := Var{ v := Var{
Type: dst.Type, Type: dst.Type,
Offset: dst.Offset, Offset: dst.Offset,
@@ -121,13 +119,15 @@ func (i *IBT) readVariablerHeaders() {
i.Vars.Vars[v.Name] = v i.Vars.Vars[v.Name] = v
} }
return nil
} }
func (i *IBT) readData() error { func (i *IBT) readData() error {
// I think that we can add one extra check or verification here // I think that we can add one extra check or verification here
// The file headers tells us how many data frames there are, we can probably // The file headers tells us how many data frames there are, we can probably
// cap it at that instead of waiting for the read to fail // cap it at that instead of waiting for the read to fail
// Probably wouldn't work on live data tho // Probably wouldn't work on live data tho
start := i.Headers.BufOffset + i.Tick*i.Headers.BufLen start := i.Headers.BufOffset + i.Tick*i.Headers.BufLen
buf := make([]byte, i.Headers.BufLen) buf := make([]byte, i.Headers.BufLen)
_, err := i.File.ReadAt(buf, int64(start)) _, err := i.File.ReadAt(buf, int64(start))