Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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
|
||||
This project will be able to parse `.ibt` files, read live data from races and
|
||||
broadcast messages to the service. Its still not very mature at all, but after
|
||||
this commit I will turn it into a package instead and give it a stable API.
|
||||
Will also put some examples then.
|
||||
This project is a simple Go SDK for the popular iRacing racing simulator.
|
||||
It has the capabilites to:
|
||||
- Read live data (live session or replay)
|
||||
- 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
|
||||
I used various sources to develop and understand how `iRacing` works, and learn
|
||||
a lot with it. Once I have matured this project a bit I will document its
|
||||
inner workings too.
|
||||
## Usage
|
||||
The SDK instance is created by calling `goirsdk.Init(Reader, exportTelem, exportYAML)`
|
||||
- `Reader` is a variable that implements the interface:
|
||||
```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
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package goirsdk
|
||||
|
||||
import (
|
||||
"github.com/ESilva15/goirsdk/logger"
|
||||
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
@@ -19,6 +21,31 @@ type DiskSubHeader struct {
|
||||
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
|
||||
// or nil if an error occurs. In which case the error return value is more
|
||||
// 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
|
||||
|
||||
go 1.23.2
|
||||
|
||||
require (
|
||||
github.com/google/go-cmp v0.6.0
|
||||
golang.org/x/sys v0.26.0
|
||||
golang.org/x/text v0.19.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
module github.com/ESilva15/goirsdk
|
||||
|
||||
go 1.23.2
|
||||
|
||||
require (
|
||||
github.com/google/go-cmp v0.6.0
|
||||
golang.org/x/sys v0.29.0
|
||||
golang.org/x/text v0.19.0
|
||||
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/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
|
||||
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
|
||||
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/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
|
||||
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/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/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
+43
-16
@@ -1,6 +1,8 @@
|
||||
package goirsdk
|
||||
|
||||
import (
|
||||
"github.com/ESilva15/goirsdk/logger"
|
||||
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
@@ -39,6 +41,47 @@ type TelemetryHeaders struct {
|
||||
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
|
||||
func (th *TelemetryHeaders) ToString() string {
|
||||
return fmt.Sprintf(
|
||||
@@ -61,19 +104,3 @@ func (th *TelemetryHeaders) ToString() string {
|
||||
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
|
||||
|
||||
import (
|
||||
"github.com/ESilva15/goirsdk/logger"
|
||||
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"io"
|
||||
|
||||
"github.com/ESilva15/goirsdk/logger"
|
||||
"github.com/ESilva15/goirsdk/winutils"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
@@ -31,14 +30,16 @@ type Reader interface {
|
||||
|
||||
// IBT struct will hold the relevant data for a given IBT file
|
||||
type IBT struct {
|
||||
File Reader // Source of the data
|
||||
FileToExport *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
|
||||
Headers *TelemetryHeaders // IBT file Headers
|
||||
SubHeaders *DiskSubHeader // IBT file Sub Headers
|
||||
SessionInfo *SessionInfoYAML // IBT file Session Info
|
||||
Vars *TelemetryVars // Vars will hold the telemetry data
|
||||
winUtils *winutils.IRacingWinUtils // WinUtils gives access to the system utilities
|
||||
File Reader // Source of the data
|
||||
IBTExport *os.File // If set, it will export the IBT data to the file
|
||||
IBTExportPath string // Path for IBT export
|
||||
YAMLExport *os.File // If set, it will export the session YAML to the file
|
||||
YAMLExportPath string // Path for YAML export
|
||||
Headers *TelemetryHeaders // IBT file Headers
|
||||
SubHeaders *DiskSubHeader // IBT file Sub Headers
|
||||
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 {
|
||||
@@ -54,9 +55,12 @@ func (i *IBT) IsConnected() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (i *IBT) exportYAML(path string) error {
|
||||
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
func (i *IBT) exportYAML() error {
|
||||
log := logger.GetInstance()
|
||||
|
||||
file, err := os.OpenFile(i.YAMLExportPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
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)
|
||||
}
|
||||
defer file.Close()
|
||||
@@ -65,29 +69,53 @@ func (i *IBT) exportYAML(path string) error {
|
||||
|
||||
err = enc.Encode(i.SessionInfo)
|
||||
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 nil
|
||||
}
|
||||
|
||||
// Init serves to initialize and get a hold of a IBT struct
|
||||
func Init(f Reader, exportTelem string, exportYAML string) (*IBT, error) {
|
||||
func (i *IBT) exportIBT(data []byte, offset int64) error {
|
||||
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
|
||||
var err error
|
||||
ibt := IBT{
|
||||
File: f,
|
||||
FileToExport: nil,
|
||||
YAMLExport: nil,
|
||||
Vars: &TelemetryVars{},
|
||||
winUtils: nil,
|
||||
File: f,
|
||||
IBTExport: nil,
|
||||
IBTExportPath: exportTelem,
|
||||
YAMLExport: nil,
|
||||
YAMLExportPath: exportYAML,
|
||||
Vars: &TelemetryVars{},
|
||||
winUtils: nil,
|
||||
}
|
||||
|
||||
// If requested to output to a telemetry file
|
||||
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 {
|
||||
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
|
||||
var headerRaw [FileHeaderSize]byte
|
||||
_, err = ibt.File.ReadAt(headerRaw[:], 0)
|
||||
err = ibt.readHeader()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to read headers from file: %v", 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)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Read the disk sub headers
|
||||
var subheaderRaw [SubHeaderSize]byte
|
||||
_, err = ibt.File.ReadAt(subheaderRaw[:], HeaderSize)
|
||||
err = ibt.readSubheader()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to read disk subheaders from file: %v", 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)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Read session info string
|
||||
sessionInfoStringRaw := make([]byte, ibt.Headers.SessionInfoLength)
|
||||
_, err = ibt.File.ReadAt(sessionInfoStringRaw, int64(ibt.Headers.SessionInfoOffset))
|
||||
err = ibt.readSessionInfo()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to read sessionInfoString from file: %v", 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
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Read the telemetry vars info
|
||||
@@ -186,6 +176,7 @@ func Init(f Reader, exportTelem string, exportYAML string) (*IBT, error) {
|
||||
return &ibt, nil
|
||||
}
|
||||
|
||||
// Close cleans up our irsdk instance
|
||||
func (i *IBT) Close() {
|
||||
if i.winUtils != nil {
|
||||
// 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
|
||||
|
||||
import (
|
||||
"github.com/ESilva15/goirsdk/logger"
|
||||
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
@@ -307,6 +310,40 @@ type Driver struct {
|
||||
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
|
||||
// struct
|
||||
func parseSessionInfo(buf []byte, len int32) (*SessionInfoYAML, error) {
|
||||
|
||||
+125
-15
@@ -71,6 +71,20 @@ type Var struct {
|
||||
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 {
|
||||
return fmt.Sprintf(
|
||||
"Type: %5d (0x%08x)\n"+
|
||||
@@ -108,9 +122,12 @@ func (i *IBT) readVariablerHeaders() error {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = i.FileToExport.WriteAt(rbuf, int64(i.Headers.VarHeaderOffset+k*VarHeaderSize))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
if i.IBTExport != nil {
|
||||
err = i.exportIBT(rbuf, int64(i.Headers.VarHeaderOffset+k*VarHeaderSize))
|
||||
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
|
||||
@@ -144,17 +161,103 @@ func (i *IBT) readData(buf []byte) error {
|
||||
// Read the value
|
||||
switch v.Type {
|
||||
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:
|
||||
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:
|
||||
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:
|
||||
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:
|
||||
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:
|
||||
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)))
|
||||
}
|
||||
}
|
||||
// --------------
|
||||
|
||||
@@ -164,6 +267,8 @@ func (i *IBT) readData(buf []byte) error {
|
||||
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) {
|
||||
if i.winUtils != nil {
|
||||
// Put a way to check if the sim is active here
|
||||
@@ -203,9 +308,11 @@ func (i *IBT) Update(timeout time.Duration) (IRacingState, error) {
|
||||
return Failed, err
|
||||
}
|
||||
|
||||
_, err = i.FileToExport.WriteAt(buf, int64(i.Headers.BufOffset+i.Vars.RecorderTick*i.Headers.BufLen))
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to write to file [2]: %v", err)
|
||||
if i.IBTExport != nil {
|
||||
err = i.exportIBT(buf, int64(i.Headers.BufOffset+i.Vars.RecorderTick*i.Headers.BufLen))
|
||||
if err != nil {
|
||||
log.Printf("Failed to export live telemetry data: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
err = i.readData(buf)
|
||||
@@ -217,6 +324,7 @@ func (i *IBT) Update(timeout time.Duration) (IRacingState, error) {
|
||||
return Ended, nil
|
||||
}
|
||||
|
||||
// Document why this is here, I don't remember the exact words right now
|
||||
i.Vars.RecorderTick++
|
||||
} else {
|
||||
// This will get the dataframe corresponding to a given tick
|
||||
@@ -226,9 +334,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
|
||||
// writing to a file
|
||||
_, err = i.FileToExport.WriteAt(buf, int64(start))
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to write to file [1]: %v\n", err)
|
||||
if i.IBTExport != nil {
|
||||
err = i.exportIBT(buf, int64(start))
|
||||
if err != nil {
|
||||
log.Printf("Failed to export offline telemetry data: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err == io.EOF {
|
||||
|
||||
Reference in New Issue
Block a user