Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6bcc0a1613 | ||
|
|
fa7f0b73ee | ||
|
|
c5ff9f95c9 | ||
|
|
4713fc80eb | ||
|
|
f102d74311 | ||
|
|
172fa4ff52 | ||
|
|
90a0321e9f | ||
|
|
6b595fabb1 | ||
|
|
4a56d253e5 |
@@ -1,14 +1,115 @@
|
|||||||
|
# TODO
|
||||||
|
- [x] Read live telemetry (from a live session or replay)
|
||||||
|
- [x] Read data from a stored `.ibt` file
|
||||||
|
- [x] Allow to export the data to an `.ibt` file
|
||||||
|
- [x] Allow to export the session info data to a `.yaml` file
|
||||||
|
- [ ] Make sure variables with multiple counts are correctly parsed and stored
|
||||||
|
- [ ] Correctly support and implement the bitFields data
|
||||||
|
- [ ] Add the message broadcasting system
|
||||||
|
- [ ] Explore a more convenient API for fetching the data for the SDK user
|
||||||
|
- [ ] Change the pattern in which the data is fetched from the telemetry and
|
||||||
|
how it is exported into `.ibt` files
|
||||||
|
|
||||||
|
|
||||||
# About
|
# About
|
||||||
This project will be able to parse `.ibt` files, read live data from races and
|
This project is a simple Go SDK for the popular iRacing racing simulator.
|
||||||
broadcast messages to the service. Its still not very mature at all, but after
|
It has the capabilites to:
|
||||||
this commit I will turn it into a package instead and give it a stable API.
|
- Read live data (live session or replay)
|
||||||
Will also put some examples then.
|
- Read data from a `.ibt` telemetry file
|
||||||
|
|
||||||
|
It should run on Linux, MacOS and Windows. With the caveat that live sessions
|
||||||
|
only happen on Windows (that I know about), therefore Linux and MacOS can only
|
||||||
|
read data from telemetry files.
|
||||||
|
|
||||||
|
|
||||||
## The SDK
|
## Usage
|
||||||
I used various sources to develop and understand how `iRacing` works, and learn
|
The SDK instance is created by calling `goirsdk.Init(Reader, exportTelem, exportYAML)`
|
||||||
a lot with it. Once I have matured this project a bit I will document its
|
- `Reader` is a variable that implements the interface:
|
||||||
inner workings too.
|
```go
|
||||||
|
type Reader interface {
|
||||||
|
io.Reader
|
||||||
|
io.ReaderAt
|
||||||
|
io.ReadCloser
|
||||||
|
}
|
||||||
|
```
|
||||||
|
To read data from a `.ibt` file, the user should pass the `*os.File` of it, and
|
||||||
|
to read live telemetry the user should pass nil
|
||||||
|
|
||||||
|
- `exportTelem` should be an empty string if the user doesn't want to export
|
||||||
|
the data, otherwise pass a string with the path for the destination telemetry
|
||||||
|
file
|
||||||
|
|
||||||
|
- `exportYAML` is just like the exportTelem but for the session info `yaml` data
|
||||||
|
|
||||||
|
### Example
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/ESilva15/goirsdk"
|
||||||
|
)
|
||||||
|
|
||||||
|
func msToKph(v float32) int {
|
||||||
|
return int((3600 * v) / 1000)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Open the data source file
|
||||||
|
file, err := os.Open("/path/to/ibtFile")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to open IBT file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Instantiate our iRacing SDK instance
|
||||||
|
irsdk, err := goirsdk.Init(file, "", "")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to create iRacing interface: %v", err)
|
||||||
|
}
|
||||||
|
defer irsdk.Close()
|
||||||
|
|
||||||
|
// Set up a loop to iterate our data
|
||||||
|
mainLoopTicker := time.NewTicker(time.Second / 60)
|
||||||
|
defer mainLoopTicker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
// Update the data that the SDK is holding with the next tick
|
||||||
|
_, err := irsdk.Update(100 * time.Millisecond)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("could not update data: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vehicle Movement data gathered from the names we can find on the
|
||||||
|
// telemetry_docs.pdf file
|
||||||
|
// - I wish to make this less verbose if possible
|
||||||
|
if _, ok := irsdk.Vars.Vars["Gear"]; !ok {
|
||||||
|
log.Fatal("Field `Gear` doesn't exist")
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, ok := irsdk.Vars.Vars["RPM"]; !ok {
|
||||||
|
log.Fatal("Field `RPM` doesn't exist")
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, ok := irsdk.Vars.Vars["Speed"]; !ok {
|
||||||
|
log.Fatal("Field `Speed` doesn't exist")
|
||||||
|
}
|
||||||
|
|
||||||
|
gear := int32(irsdk.Vars.Vars["Gear"].Value.(int))
|
||||||
|
rpm := int32(irsdk.Vars.Vars["RPM"].Value.(float32))
|
||||||
|
speed := int32(msToKph(irsdk.Vars.Vars["Speed"].Value.(float32)))
|
||||||
|
|
||||||
|
fmt.Printf("\033[?25l\033[2J\033[H")
|
||||||
|
fmt.Printf("Gear: %d, RPM: %d, Speed: %d", gear, rpm, speed)
|
||||||
|
|
||||||
|
<-mainLoopTicker.C
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
## SharedMem
|
## SharedMem
|
||||||
|
|||||||
+19
-1
@@ -119,7 +119,25 @@ const (
|
|||||||
|
|
||||||
// Some other constants that I need to get trough
|
// Some other constants that I need to get trough
|
||||||
// // bit fields
|
// // bit fields
|
||||||
// enum irsdk_EngineWarnings
|
|
||||||
|
type bitfieldValue struct {
|
||||||
|
Value int
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
// EngineWarnings const
|
||||||
|
var (
|
||||||
|
irsdkWaterTempWarning = bitfieldValue{0x01, "irsdk_waterTempWarning"}
|
||||||
|
irsdkFuelPressureWarning = bitfieldValue{0x02, "irsdk_fueldPressureWarning"}
|
||||||
|
irsdkOilPressureWarning = bitfieldValue{0x04, "irsdk_oilPressureWarning"}
|
||||||
|
irsdkEngineStalled = bitfieldValue{0x08, "irsdk_engineStalled"}
|
||||||
|
irsdkPitSpeedLimiter = bitfieldValue{0x10, "irsdk_pitSpeedLimiter"}
|
||||||
|
irsdkRevLimiterActive = bitfieldValue{0x20, "irsdk_revLimiterActive"}
|
||||||
|
irsdkEngineWarnings = []bitfieldValue{irsdkWaterTempWarning, irsdkFuelPressureWarning,
|
||||||
|
irsdkOilPressureWarning, irsdkEngineStalled, irsdkPitSpeedLimiter, irsdkRevLimiterActive}
|
||||||
|
)
|
||||||
|
|
||||||
|
// enum irsdk_EngineWarnings
|
||||||
// {
|
// {
|
||||||
// irsdk_waterTempWarning = 0x01,
|
// irsdk_waterTempWarning = 0x01,
|
||||||
// irsdk_fuelPressureWarning = 0x02,
|
// irsdk_fuelPressureWarning = 0x02,
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package goirsdk
|
package goirsdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/ESilva15/goirsdk/logger"
|
||||||
|
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -19,6 +21,31 @@ type DiskSubHeader struct {
|
|||||||
RecordCount int32 // RecordCount holds the number of data frames
|
RecordCount int32 // RecordCount holds the number of data frames
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// readSubheader will read the subheader contents out of the telemetry data
|
||||||
|
func (i *IBT) readSubheader() error {
|
||||||
|
log := logger.GetInstance()
|
||||||
|
|
||||||
|
var subheaderRaw [SubHeaderSize]byte
|
||||||
|
_, err := i.File.ReadAt(subheaderRaw[:], HeaderSize)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Failed to read disk subheaders from file: %v", err)
|
||||||
|
}
|
||||||
|
i.SubHeaders, err = parseTelemetrySubHeader(subheaderRaw)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Unable to parse disk subheaders from file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write to the output file - TODO add the check
|
||||||
|
if i.IBTExport != nil {
|
||||||
|
err = i.exportIBT(subheaderRaw[:], HeaderSize)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to export subheaders: %v\n", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// parseTelemetrySubHeader will return a pointer to a DiskSubHeader variable
|
// parseTelemetrySubHeader will return a pointer to a DiskSubHeader variable
|
||||||
// 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
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/ESilva15/goirsdk"
|
||||||
|
)
|
||||||
|
|
||||||
|
func msToKph(v float32) int {
|
||||||
|
return int((3600 * v) / 1000)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Open the data source file
|
||||||
|
file, err := os.Open("/path/to/ibtFile")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to open IBT file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Instantiate our iRacing SDK instance
|
||||||
|
irsdk, err := goirsdk.Init(file, "", "")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to create iRacing interface: %v", err)
|
||||||
|
}
|
||||||
|
defer irsdk.Close()
|
||||||
|
|
||||||
|
// Set up a loop to iterate our data
|
||||||
|
mainLoopTicker := time.NewTicker(time.Second / 60)
|
||||||
|
defer mainLoopTicker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
// Update the data that the SDK is holding with the next tick
|
||||||
|
_, err := irsdk.Update(100 * time.Millisecond)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("could not update data: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vehicle Movement data gathered from the names we can find on the
|
||||||
|
// telemetry_docs.pdf file
|
||||||
|
// - I wish to make this less verbose if possible
|
||||||
|
if _, ok := irsdk.Vars.Vars["Gear"]; !ok {
|
||||||
|
log.Fatal("Field `Gear` doesn't exist")
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, ok := irsdk.Vars.Vars["RPM"]; !ok {
|
||||||
|
log.Fatal("Field `RPM` doesn't exist")
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, ok := irsdk.Vars.Vars["Speed"]; !ok {
|
||||||
|
log.Fatal("Field `Speed` doesn't exist")
|
||||||
|
}
|
||||||
|
|
||||||
|
gear := int32(irsdk.Vars.Vars["Gear"].Value.(int))
|
||||||
|
rpm := int32(irsdk.Vars.Vars["RPM"].Value.(float32))
|
||||||
|
speed := int32(msToKph(irsdk.Vars.Vars["Speed"].Value.(float32)))
|
||||||
|
|
||||||
|
fmt.Printf("\033[?25l\033[2J\033[H")
|
||||||
|
fmt.Printf("Gear: %d, RPM: %d, Speed: %d", gear, rpm, speed)
|
||||||
|
|
||||||
|
<-mainLoopTicker.C
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
module github.com/ESilva15/goirsdk
|
module github.com/ESilva15/goirsdk
|
||||||
|
|
||||||
go 1.23.2
|
go 1.23.2
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/google/go-cmp v0.6.0
|
github.com/google/go-cmp v0.6.0
|
||||||
golang.org/x/sys v0.26.0
|
golang.org/x/sys v0.29.0
|
||||||
golang.org/x/text v0.19.0
|
golang.org/x/text v0.19.0
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
|
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
|
||||||
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
|
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
|
||||||
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
+43
-16
@@ -1,6 +1,8 @@
|
|||||||
package goirsdk
|
package goirsdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/ESilva15/goirsdk/logger"
|
||||||
|
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -39,6 +41,47 @@ type TelemetryHeaders struct {
|
|||||||
BufOffset int32
|
BufOffset int32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// readHeader will read the header out of the telemetry data
|
||||||
|
func (i *IBT) readHeader() error {
|
||||||
|
log := logger.GetInstance()
|
||||||
|
|
||||||
|
var headerRaw [FileHeaderSize]byte
|
||||||
|
_, err := i.File.ReadAt(headerRaw[:], 0)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Failed to read headers from file: %v", err)
|
||||||
|
}
|
||||||
|
i.Headers, err = parseTelemetryHeader(headerRaw)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Unable to read headers from file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write to the output file - TODO: this should only write if necessary
|
||||||
|
if i.IBTExport != nil {
|
||||||
|
err = i.exportIBT(headerRaw[:], 0)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to export headers: %v\n", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseTelemetryHeader will read the IBT file headers from a correctly sized
|
||||||
|
// buffer.
|
||||||
|
// You need to pass a the first FILE_HEADER_SIZE bytes of the buffer
|
||||||
|
func parseTelemetryHeader(buf [FileHeaderSize]byte) (*TelemetryHeaders, error) {
|
||||||
|
// utils.HexDump(buf[:])
|
||||||
|
// fmt.Printf("Len: %d\n", len(buf))
|
||||||
|
|
||||||
|
dst := TelemetryHeaders{}
|
||||||
|
err := binary.Read(bytes.NewBuffer(buf[:]), binary.LittleEndian, &dst)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to unpack data: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &dst, nil
|
||||||
|
}
|
||||||
|
|
||||||
// ToString renders a string showing the values of the struct
|
// ToString renders a string showing the values of the struct
|
||||||
func (th *TelemetryHeaders) ToString() string {
|
func (th *TelemetryHeaders) ToString() string {
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
@@ -61,19 +104,3 @@ func (th *TelemetryHeaders) ToString() string {
|
|||||||
th.NumBuf, th.NumBuf, th.BufLen, th.BufLen, th.BufOffset, th.BufOffset,
|
th.NumBuf, th.NumBuf, th.BufLen, th.BufLen, th.BufOffset, th.BufOffset,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseTelemetryHeader will read the IBT file headers from a correctly sized
|
|
||||||
// buffer.
|
|
||||||
// You need to pass a the first FILE_HEADER_SIZE bytes of the buffer
|
|
||||||
func parseTelemetryHeader(buf [FileHeaderSize]byte) (*TelemetryHeaders, error) {
|
|
||||||
// utils.HexDump(buf[:])
|
|
||||||
// fmt.Printf("Len: %d\n", len(buf))
|
|
||||||
|
|
||||||
dst := TelemetryHeaders{}
|
|
||||||
err := binary.Read(bytes.NewBuffer(buf[:]), binary.LittleEndian, &dst)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("unable to unpack data: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &dst, nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -2,13 +2,12 @@
|
|||||||
package goirsdk
|
package goirsdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/ESilva15/goirsdk/logger"
|
|
||||||
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
|
"github.com/ESilva15/goirsdk/logger"
|
||||||
"github.com/ESilva15/goirsdk/winutils"
|
"github.com/ESilva15/goirsdk/winutils"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
@@ -31,14 +30,16 @@ type Reader interface {
|
|||||||
|
|
||||||
// IBT struct will hold the relevant data for a given IBT file
|
// IBT struct will hold the relevant data for a given IBT file
|
||||||
type IBT struct {
|
type IBT struct {
|
||||||
File Reader // Source of the data
|
File Reader // Source of the data
|
||||||
FileToExport *os.File // If set, it will export the IBT data to the file
|
IBTExport *os.File // If set, it will export the IBT data to the file
|
||||||
YAMLExport *os.File // If set, it will export the session YAML to the file
|
IBTExportPath string // Path for IBT export
|
||||||
Headers *TelemetryHeaders // IBT file Headers
|
YAMLExport *os.File // If set, it will export the session YAML to the file
|
||||||
SubHeaders *DiskSubHeader // IBT file Sub Headers
|
YAMLExportPath string // Path for YAML export
|
||||||
SessionInfo *SessionInfoYAML // IBT file Session Info
|
Headers *TelemetryHeaders // IBT file Headers
|
||||||
Vars *TelemetryVars // Vars will hold the telemetry data
|
SubHeaders *DiskSubHeader // IBT file Sub Headers
|
||||||
winUtils *winutils.IRacingWinUtils // WinUtils gives access to the system utilities
|
SessionInfo *SessionInfoYAML // IBT file Session Info
|
||||||
|
Vars *TelemetryVars // Vars will hold the telemetry data
|
||||||
|
winUtils *winutils.IRacingWinUtils // WinUtils gives access to the system utilities
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IBT) IsConnected() bool {
|
func (i *IBT) IsConnected() bool {
|
||||||
@@ -54,9 +55,12 @@ func (i *IBT) IsConnected() bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IBT) exportYAML(path string) error {
|
func (i *IBT) exportYAML() error {
|
||||||
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
log := logger.GetInstance()
|
||||||
|
|
||||||
|
file, err := os.OpenFile(i.YAMLExportPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Printf("Failed to open file for YAML export: %v\n", err)
|
||||||
return fmt.Errorf("failed to open output file for YAML: %v", err)
|
return fmt.Errorf("failed to open output file for YAML: %v", err)
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
@@ -65,29 +69,53 @@ func (i *IBT) exportYAML(path string) error {
|
|||||||
|
|
||||||
err = enc.Encode(i.SessionInfo)
|
err = enc.Encode(i.SessionInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Printf("Failed to write into file for YAML export: %v\n", err)
|
||||||
return fmt.Errorf("failed to write YAML contents to file: %v", err)
|
return fmt.Errorf("failed to write YAML contents to file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init serves to initialize and get a hold of a IBT struct
|
func (i *IBT) exportIBT(data []byte, offset int64) error {
|
||||||
func Init(f Reader, exportTelem string, exportYAML string) (*IBT, error) {
|
|
||||||
log := logger.GetInstance()
|
log := logger.GetInstance()
|
||||||
|
|
||||||
|
_, err := i.IBTExport.WriteAt(data, offset)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
i.IBTExport.Close()
|
||||||
|
i.IBTExport = nil
|
||||||
|
log.Println("Won't attempt to export anymore")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init serves to initialize and get a hold of a IBT struct
|
||||||
|
// f -> is the source data, pass nil for the SDK to read live data or a
|
||||||
|
// *os.File to read from a file
|
||||||
|
// exportTelem -> is a string with the path to export the telemetry data, pass
|
||||||
|
// an empty string to not export any data
|
||||||
|
// exportTelem -> is a string with the path to export the session info data, pass
|
||||||
|
// an empty string to not export any data
|
||||||
|
func Init(f Reader, exportTelem string, exportYAML string) (*IBT, error) {
|
||||||
|
// log := logger.GetInstance()
|
||||||
|
|
||||||
// Read the header of the file
|
// Read the header of the file
|
||||||
var err error
|
var err error
|
||||||
ibt := IBT{
|
ibt := IBT{
|
||||||
File: f,
|
File: f,
|
||||||
FileToExport: nil,
|
IBTExport: nil,
|
||||||
YAMLExport: nil,
|
IBTExportPath: exportTelem,
|
||||||
Vars: &TelemetryVars{},
|
YAMLExport: nil,
|
||||||
winUtils: nil,
|
YAMLExportPath: exportYAML,
|
||||||
|
Vars: &TelemetryVars{},
|
||||||
|
winUtils: nil,
|
||||||
}
|
}
|
||||||
|
|
||||||
// If requested to output to a telemetry file
|
// If requested to output to a telemetry file
|
||||||
if exportTelem != "" {
|
if exportTelem != "" {
|
||||||
ibt.FileToExport, err = os.OpenFile(exportTelem, os.O_CREATE|os.O_RDWR, 0644)
|
ibt.IBTExport, err = os.OpenFile(exportTelem, os.O_CREATE|os.O_RDWR, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to open ibt export file: %v", err)
|
return nil, fmt.Errorf("failed to open ibt export file: %v", err)
|
||||||
}
|
}
|
||||||
@@ -122,59 +150,21 @@ func Init(f Reader, exportTelem string, exportYAML string) (*IBT, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Read the file headers
|
// Read the file headers
|
||||||
var headerRaw [FileHeaderSize]byte
|
err = ibt.readHeader()
|
||||||
_, err = ibt.File.ReadAt(headerRaw[:], 0)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Failed to read headers from file: %v", err)
|
return nil, err
|
||||||
}
|
|
||||||
ibt.Headers, err = parseTelemetryHeader(headerRaw)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("Unable to read headers from file: %v", err)
|
|
||||||
}
|
|
||||||
// Write to the output file
|
|
||||||
_, err = ibt.FileToExport.WriteAt(headerRaw[:], 0)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the disk sub headers
|
// Read the disk sub headers
|
||||||
var subheaderRaw [SubHeaderSize]byte
|
err = ibt.readSubheader()
|
||||||
_, err = ibt.File.ReadAt(subheaderRaw[:], HeaderSize)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Failed to read disk subheaders from file: %v", err)
|
return nil, err
|
||||||
}
|
|
||||||
ibt.SubHeaders, err = parseTelemetrySubHeader(subheaderRaw)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("Unable to parse disk subheaders from file: %v", err)
|
|
||||||
}
|
|
||||||
// Write to the output file
|
|
||||||
_, err = ibt.FileToExport.WriteAt(subheaderRaw[:], HeaderSize)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read session info string
|
// Read session info string
|
||||||
sessionInfoStringRaw := make([]byte, ibt.Headers.SessionInfoLength)
|
err = ibt.readSessionInfo()
|
||||||
_, err = ibt.File.ReadAt(sessionInfoStringRaw, int64(ibt.Headers.SessionInfoOffset))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Failed to read sessionInfoString from file: %v", err)
|
return nil, err
|
||||||
}
|
|
||||||
// Write to the output file
|
|
||||||
_, err = ibt.FileToExport.WriteAt(sessionInfoStringRaw[:], int64(ibt.Headers.SessionInfoOffset))
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
ibt.SessionInfo, err = parseSessionInfo(sessionInfoStringRaw, ibt.Headers.SessionInfoLength)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("Unable to parse SessionInfoString from file: %v", err)
|
|
||||||
}
|
|
||||||
// Write to YAML output file
|
|
||||||
if exportYAML != "" {
|
|
||||||
err := ibt.exportYAML(exportYAML)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the telemetry vars info
|
// Read the telemetry vars info
|
||||||
@@ -186,6 +176,7 @@ func Init(f Reader, exportTelem string, exportYAML string) (*IBT, error) {
|
|||||||
return &ibt, nil
|
return &ibt, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close cleans up our irsdk instance
|
||||||
func (i *IBT) Close() {
|
func (i *IBT) Close() {
|
||||||
if i.winUtils != nil {
|
if i.winUtils != nil {
|
||||||
// If its not live data, the user is the one with ownership of the handle
|
// If its not live data, the user is the one with ownership of the handle
|
||||||
|
|||||||
+101
@@ -0,0 +1,101 @@
|
|||||||
|
package goirsdk
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"sort"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type StandingsLine struct {
|
||||||
|
CarIdx int
|
||||||
|
LapPct float32
|
||||||
|
Lap int32
|
||||||
|
DriverName string
|
||||||
|
EstTime float32
|
||||||
|
TimeBehind float32
|
||||||
|
}
|
||||||
|
|
||||||
|
func lapTimeRepresentation(t float32) string {
|
||||||
|
if t < 0 {
|
||||||
|
t = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
wholeSeconds := int64(t)
|
||||||
|
lapTime := time.Unix(wholeSeconds, int64((t-float32(wholeSeconds))*1e9))
|
||||||
|
|
||||||
|
return lapTime.Format("04:05.000")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFunctionality(t *testing.T) {
|
||||||
|
input, err := os.Open("../testTelemetry/supercars_race_watkins_glenn.ibt")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("Was unable to prepare telemetry file for testing.")
|
||||||
|
}
|
||||||
|
|
||||||
|
i, _ := Init(input, "", "")
|
||||||
|
defer i.Close()
|
||||||
|
|
||||||
|
// Set up a loop to iterate our data
|
||||||
|
mainLoopTicker := time.NewTicker(time.Second / 60)
|
||||||
|
defer mainLoopTicker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
// Update the data that the SDK is holding with the next tick
|
||||||
|
_, err := i.Update(100 * time.Millisecond)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("could not update data: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vehicle Movement data gathered from the names we can find on the
|
||||||
|
// telemetry_docs.pdf file
|
||||||
|
// - I wish to make this less verbose if possible
|
||||||
|
if _, ok := i.Vars.Vars["CarIdxPosition"]; !ok {
|
||||||
|
log.Fatal("Field `CarIdxPosition` doesn't exist")
|
||||||
|
}
|
||||||
|
driversLapDistPct := i.Vars.Vars["CarIdxLapDistPct"].Value.([]float32)
|
||||||
|
driversEstTime := i.Vars.Vars["CarIdxEstTime"].Value.([]float32)
|
||||||
|
driversLap := i.Vars.Vars["CarIdxLap"].Value.([]int32)
|
||||||
|
// driversBehind := i.Vars.Vars["CarIdxF2Time"].Value.([]float32)
|
||||||
|
|
||||||
|
drivers := i.SessionInfo.DriverInfo.Drivers
|
||||||
|
myIdx := i.SessionInfo.DriverInfo.DriverCarIdx
|
||||||
|
|
||||||
|
standings := make([]StandingsLine, len(drivers))
|
||||||
|
|
||||||
|
fmt.Printf("\033[?25l\033[2J\033[H")
|
||||||
|
for k := range len(drivers) {
|
||||||
|
if drivers[k].CarIsPaceCar == 1 || drivers[k].IsSpectator == 1 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
standings[k] = StandingsLine{
|
||||||
|
CarIdx: k,
|
||||||
|
LapPct: driversLapDistPct[k],
|
||||||
|
DriverName: drivers[k].UserName,
|
||||||
|
EstTime: driversEstTime[k],
|
||||||
|
Lap: driversLap[k],
|
||||||
|
TimeBehind: driversEstTime[myIdx],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Slice(standings, func(i int, j int) bool {
|
||||||
|
if standings[i].Lap > int32(standings[j].Lap) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return standings[i].LapPct >= standings[j].LapPct
|
||||||
|
})
|
||||||
|
|
||||||
|
fmt.Printf("%v\n", driversEstTime)
|
||||||
|
// for p, v := range standings {
|
||||||
|
// fmt.Printf("[%2d] %-30s %13f %13f\n",
|
||||||
|
// p+1, v.DriverName, v.LapPct, driversEstTime[p] - driversEstTime[myIdx])
|
||||||
|
// }
|
||||||
|
|
||||||
|
<-mainLoopTicker.C
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,10 @@
|
|||||||
package goirsdk
|
package goirsdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/ESilva15/goirsdk/logger"
|
||||||
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -307,6 +310,40 @@ type Driver struct {
|
|||||||
TeamIncidentCount int `yaml:"TeamIncidentCount"`
|
TeamIncidentCount int `yaml:"TeamIncidentCount"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// readSessionInfo will read the session info yaml out of the telemetry data
|
||||||
|
func (i *IBT) readSessionInfo() error {
|
||||||
|
log := logger.GetInstance()
|
||||||
|
|
||||||
|
sessionInfoStringRaw := make([]byte, i.Headers.SessionInfoLength)
|
||||||
|
_, err := i.File.ReadAt(sessionInfoStringRaw, int64(i.Headers.SessionInfoOffset))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Failed to read sessionInfoString from file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write to the output file
|
||||||
|
if i.IBTExport != nil {
|
||||||
|
err := i.exportIBT(sessionInfoStringRaw[:], int64(i.Headers.SessionInfoOffset))
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to export offline telemetry data: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
i.SessionInfo, err = parseSessionInfo(sessionInfoStringRaw, i.Headers.SessionInfoLength)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Unable to parse SessionInfoString from file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write to YAML output file
|
||||||
|
if i.YAMLExportPath != "" {
|
||||||
|
err := i.exportYAML()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to export YAML string: %v\n", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// parseSessionInfo will parse the sessionInfo buffer into the SessionInfoYAML
|
// parseSessionInfo will parse the sessionInfo buffer into the SessionInfoYAML
|
||||||
// struct
|
// struct
|
||||||
func parseSessionInfo(buf []byte, len int32) (*SessionInfoYAML, error) {
|
func parseSessionInfo(buf []byte, len int32) (*SessionInfoYAML, error) {
|
||||||
|
|||||||
+161
-15
@@ -7,6 +7,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"math"
|
"math"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -71,6 +72,20 @@ type Var struct {
|
|||||||
Value interface{}
|
Value interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (v *Var) ToString() string {
|
||||||
|
return fmt.Sprintf(
|
||||||
|
"Type: %5d (0x%08x)\n"+
|
||||||
|
"Offset: %5d (0x%08x)\n"+
|
||||||
|
"Count: %5d (0x%08x)\n"+
|
||||||
|
"CountAsTime: %5t\n"+
|
||||||
|
"Name: %s\n"+
|
||||||
|
"Description: %s\n"+
|
||||||
|
"Unit: %s",
|
||||||
|
v.Type, v.Type, v.Offset, v.Offset, v.Count, v.Count,
|
||||||
|
v.CountAsTime, v.Name, v.Description, v.Unit,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
func (v *IBTVar) ToString() string {
|
func (v *IBTVar) ToString() string {
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
"Type: %5d (0x%08x)\n"+
|
"Type: %5d (0x%08x)\n"+
|
||||||
@@ -108,9 +123,12 @@ func (i *IBT) readVariablerHeaders() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = i.FileToExport.WriteAt(rbuf, int64(i.Headers.VarHeaderOffset+k*VarHeaderSize))
|
if i.IBTExport != nil {
|
||||||
if err != nil {
|
err = i.exportIBT(rbuf, int64(i.Headers.VarHeaderOffset+k*VarHeaderSize))
|
||||||
log.Fatal(err)
|
if err != nil {
|
||||||
|
// Don't outright kill it here - maybe nowhere else
|
||||||
|
log.Printf("Failed to export variable contents: %v\n", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var dst IBTVar
|
var dst IBTVar
|
||||||
@@ -136,6 +154,38 @@ func (i *IBT) readVariablerHeaders() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *IBT) parseEngineWarnings() {
|
||||||
|
val, ok := i.Vars.Vars["EngineWarnings"]
|
||||||
|
if !ok {
|
||||||
|
log.Fatal("no engine warnings")
|
||||||
|
}
|
||||||
|
|
||||||
|
bitfield, err := strconv.ParseInt(val.Value.(string), 0, 64)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Unable to get engine warnings: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, ew := range irsdkEngineWarnings {
|
||||||
|
result := (int(bitfield) & ew.Value) != 0
|
||||||
|
i.Vars.Vars[ew.Name] = Var{Value: result}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseBitfieldVariables will parse the variables:
|
||||||
|
// - irsdk_CameraState "CamCameraState"
|
||||||
|
// - irsdk_EngineWarnings "EngineWarnings"
|
||||||
|
// - irsdk_PitSvFlags "PitSvFlags"
|
||||||
|
// - irsdk_Flags "SessionFlags"
|
||||||
|
// - irsdk_SessionState "SessionState"
|
||||||
|
// - irsdk_TrkLoc "CarIdxTrackSurface"
|
||||||
|
//
|
||||||
|
// The approach for now will be to create unique entries in the data map for
|
||||||
|
// the fields in these variables
|
||||||
|
func (i *IBT) parseBitfieldVariables() {
|
||||||
|
// Parse the EngineWarnings variables - its the only one for now
|
||||||
|
i.parseEngineWarnings()
|
||||||
|
}
|
||||||
|
|
||||||
func (i *IBT) readData(buf []byte) error {
|
func (i *IBT) readData(buf []byte) error {
|
||||||
for k, v := range i.Vars.Vars {
|
for k, v := range i.Vars.Vars {
|
||||||
// Slice of the variable value in the buffer
|
// Slice of the variable value in the buffer
|
||||||
@@ -144,26 +194,117 @@ func (i *IBT) readData(buf []byte) error {
|
|||||||
// Read the value
|
// Read the value
|
||||||
switch v.Type {
|
switch v.Type {
|
||||||
case IRSDK_char:
|
case IRSDK_char:
|
||||||
v.Value = string(rbuf[0])
|
if v.Count > 1 {
|
||||||
|
// Array of data
|
||||||
|
data := make([]string, v.Count)
|
||||||
|
for entry := 0; entry < int(v.Count); entry++ {
|
||||||
|
entryOffset := v.Offset + int32(entry)*int32(VarTypes[int(v.Type)].Size)
|
||||||
|
rbuf := buf[entryOffset : entryOffset+int32(VarTypes[int(v.Type)].Size)]
|
||||||
|
|
||||||
|
newValue := string(rbuf[0])
|
||||||
|
data[entry] = newValue
|
||||||
|
}
|
||||||
|
|
||||||
|
v.Value = data
|
||||||
|
} else {
|
||||||
|
// Single value
|
||||||
|
v.Value = string(rbuf[0])
|
||||||
|
}
|
||||||
case IRSDK_bool:
|
case IRSDK_bool:
|
||||||
v.Value = int(rbuf[0]) > 0
|
if v.Count > 1 {
|
||||||
|
// Array of data
|
||||||
|
data := make([]bool, v.Count)
|
||||||
|
for entry := 0; entry < int(v.Count); entry++ {
|
||||||
|
entryOffset := v.Offset + int32(entry)*int32(VarTypes[int(v.Type)].Size)
|
||||||
|
rbuf := buf[entryOffset : entryOffset+int32(VarTypes[int(v.Type)].Size)]
|
||||||
|
|
||||||
|
newValue := int(rbuf[0]) > 0
|
||||||
|
data[entry] = newValue
|
||||||
|
}
|
||||||
|
|
||||||
|
v.Value = data
|
||||||
|
} else {
|
||||||
|
// Single value
|
||||||
|
v.Value = int(rbuf[0]) > 0
|
||||||
|
}
|
||||||
case IRSDK_int:
|
case IRSDK_int:
|
||||||
v.Value = int(binary.LittleEndian.Uint32(rbuf))
|
if v.Count > 1 {
|
||||||
|
// Array of data
|
||||||
|
data := make([]int32, v.Count)
|
||||||
|
for entry := 0; entry < int(v.Count); entry++ {
|
||||||
|
entryOffset := v.Offset + int32(entry)*int32(VarTypes[int(v.Type)].Size)
|
||||||
|
rbuf := buf[entryOffset : entryOffset+int32(VarTypes[int(v.Type)].Size)]
|
||||||
|
newValue := int32(binary.LittleEndian.Uint32(rbuf))
|
||||||
|
data[entry] = newValue
|
||||||
|
}
|
||||||
|
|
||||||
|
v.Value = data
|
||||||
|
} else {
|
||||||
|
// Single value
|
||||||
|
v.Value = int(binary.LittleEndian.Uint32(rbuf))
|
||||||
|
}
|
||||||
case IRSDK_bitField:
|
case IRSDK_bitField:
|
||||||
v.Value = fmt.Sprintf("0x%x", int(binary.LittleEndian.Uint32(rbuf)))
|
if v.Count > 1 {
|
||||||
|
// Array of data
|
||||||
|
data := make([]string, v.Count)
|
||||||
|
for entry := 0; entry < int(v.Count); entry++ {
|
||||||
|
entryOffset := v.Offset + int32(entry)*int32(VarTypes[int(v.Type)].Size)
|
||||||
|
rbuf := buf[entryOffset : entryOffset+int32(VarTypes[int(v.Type)].Size)]
|
||||||
|
newValue := fmt.Sprintf("0x%x", int(binary.LittleEndian.Uint32(rbuf)))
|
||||||
|
data[entry] = newValue
|
||||||
|
}
|
||||||
|
|
||||||
|
v.Value = data
|
||||||
|
} else {
|
||||||
|
// Single value
|
||||||
|
v.Value = fmt.Sprintf("0x%x", int(binary.LittleEndian.Uint32(rbuf)))
|
||||||
|
}
|
||||||
case IRSDK_float:
|
case IRSDK_float:
|
||||||
v.Value = math.Float32frombits(uint32(binary.LittleEndian.Uint32(rbuf)))
|
if v.Count > 1 {
|
||||||
|
// Array of data
|
||||||
|
data := make([]float32, v.Count)
|
||||||
|
for entry := 0; entry < int(v.Count); entry++ {
|
||||||
|
entryOffset := v.Offset + int32(entry)*int32(VarTypes[int(v.Type)].Size)
|
||||||
|
rbuf := buf[entryOffset : entryOffset+int32(VarTypes[int(v.Type)].Size)]
|
||||||
|
newValue := math.Float32frombits(uint32(binary.LittleEndian.Uint32(rbuf)))
|
||||||
|
data[entry] = newValue
|
||||||
|
}
|
||||||
|
|
||||||
|
v.Value = data
|
||||||
|
} else {
|
||||||
|
// Single value
|
||||||
|
v.Value = math.Float32frombits(uint32(binary.LittleEndian.Uint32(rbuf)))
|
||||||
|
}
|
||||||
case IRSDK_double:
|
case IRSDK_double:
|
||||||
v.Value = math.Float64frombits(uint64(binary.LittleEndian.Uint64(rbuf)))
|
if v.Count > 1 {
|
||||||
|
// Array of data
|
||||||
|
data := make([]float64, v.Count)
|
||||||
|
for entry := 0; entry < int(v.Count); entry++ {
|
||||||
|
entryOffset := v.Offset + int32(entry)*int32(VarTypes[int(v.Type)].Size)
|
||||||
|
rbuf := buf[entryOffset : entryOffset+int32(VarTypes[int(v.Type)].Size)]
|
||||||
|
newValue := math.Float64frombits(uint64(binary.LittleEndian.Uint64(rbuf)))
|
||||||
|
data[entry] = newValue
|
||||||
|
}
|
||||||
|
|
||||||
|
v.Value = data
|
||||||
|
} else {
|
||||||
|
// Single value
|
||||||
|
v.Value = math.Float64frombits(uint64(binary.LittleEndian.Uint64(rbuf)))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// --------------
|
// --------------
|
||||||
|
|
||||||
i.Vars.Vars[k] = v
|
i.Vars.Vars[k] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parse the bitfield variables here
|
||||||
|
i.parseBitfieldVariables()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update will read the next data chunk from the telemetry data, works for both the
|
||||||
|
// live and offline data
|
||||||
func (i *IBT) Update(timeout time.Duration) (IRacingState, error) {
|
func (i *IBT) Update(timeout time.Duration) (IRacingState, error) {
|
||||||
if i.winUtils != nil {
|
if i.winUtils != nil {
|
||||||
// Put a way to check if the sim is active here
|
// Put a way to check if the sim is active here
|
||||||
@@ -203,9 +344,11 @@ func (i *IBT) Update(timeout time.Duration) (IRacingState, error) {
|
|||||||
return Failed, err
|
return Failed, err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = i.FileToExport.WriteAt(buf, int64(i.Headers.BufOffset+i.Vars.RecorderTick*i.Headers.BufLen))
|
if i.IBTExport != nil {
|
||||||
if err != nil {
|
err = i.exportIBT(buf, int64(i.Headers.BufOffset+i.Vars.RecorderTick*i.Headers.BufLen))
|
||||||
log.Fatalf("Failed to write to file [2]: %v", err)
|
if err != nil {
|
||||||
|
log.Printf("Failed to export live telemetry data: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = i.readData(buf)
|
err = i.readData(buf)
|
||||||
@@ -217,6 +360,7 @@ func (i *IBT) Update(timeout time.Duration) (IRacingState, error) {
|
|||||||
return Ended, nil
|
return Ended, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Document why this is here, I don't remember the exact words right now
|
||||||
i.Vars.RecorderTick++
|
i.Vars.RecorderTick++
|
||||||
} else {
|
} else {
|
||||||
// This will get the dataframe corresponding to a given tick
|
// This will get the dataframe corresponding to a given tick
|
||||||
@@ -226,9 +370,11 @@ func (i *IBT) Update(timeout time.Duration) (IRacingState, error) {
|
|||||||
|
|
||||||
// Make this happen in a different thread, or have this send to a queue that has a thread
|
// Make this happen in a different thread, or have this send to a queue that has a thread
|
||||||
// writing to a file
|
// writing to a file
|
||||||
_, err = i.FileToExport.WriteAt(buf, int64(start))
|
if i.IBTExport != nil {
|
||||||
if err != nil {
|
err = i.exportIBT(buf, int64(start))
|
||||||
log.Fatalf("Failed to write to file [1]: %v\n", err)
|
if err != nil {
|
||||||
|
log.Printf("Failed to export offline telemetry data: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
|
|||||||
Reference in New Issue
Block a user