Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4762d854cd | ||
|
|
a82caa05a2 | ||
|
|
eed8c562b2 | ||
|
|
052f4582c9 | ||
|
|
7584b0ffcf |
+71
-291
@@ -2,7 +2,6 @@ package goirsdk
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Msg struct {
|
||||
@@ -13,13 +12,15 @@ type Msg struct {
|
||||
}
|
||||
|
||||
const (
|
||||
DATAVALIDEVENTNAME string = "IRSDKDataValidEvent"
|
||||
MEMMAPFILENAME = "IRSDKMemMapFileName"
|
||||
SimStatusUrl string = "http://127.0.0.1:32034/get_sim_status?object=simStatus"
|
||||
IRSDK_DATAVALIDEVENTNAME string = "Local\\IRSDKDataValidEvent"
|
||||
IRSDK_MEMMAPFILENAME string = "Local\\" + MEMMAPFILENAME
|
||||
IRSDK_BROADCASTMSGNAME string = "IRSDK_BROADCASTMSG"
|
||||
fileMapSize uint32 = 1164 * 1024
|
||||
connTimeout int64 = 30
|
||||
IRSDK_DATAVALIDEVENTNAME string = "Local\\" + DATAVALIDEVENTNAME
|
||||
// IRSDK_DATAVALIDEVENTNAME string = DATAVALIDEVENTNAME
|
||||
IRSDK_MEMMAPFILENAME string = "Local\\" + MEMMAPFILENAME
|
||||
IRSDK_BROADCASTMSGNAME string = "IRSDK_BROADCASTMSG"
|
||||
fileMapSize uint32 = 1164 * 1024
|
||||
connTimeout int64 = 30
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -141,124 +142,55 @@ type bitfieldValue struct {
|
||||
// EngineWarnings - Start
|
||||
// TODO: wtf
|
||||
var (
|
||||
irsdk_WaterTempWarning int64 = 0x00000001
|
||||
irsdk_FuelPressureWarning int64 = 0x00000002
|
||||
irsdk_OilPressureWarning int64 = 0x00000004
|
||||
irsdk_EngineStalled int64 = 0x00000008
|
||||
irsdk_PitSpeedLimiter int64 = 0x00000010
|
||||
irsdk_RevLimiterActive int64 = 0x00000020
|
||||
irsdk_AbsActive int64 = 0x00000100
|
||||
// DEPRECATE THESE ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
|
||||
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"}
|
||||
irsdkAbsActive = bitfieldValue{0x100, "irsdk_absActive"}
|
||||
irsdkEngineWarnings = []bitfieldValue{
|
||||
irsdkWaterTempWarning, irsdkFuelPressureWarning,
|
||||
irsdkOilPressureWarning, irsdkEngineStalled, irsdkPitSpeedLimiter, irsdkRevLimiterActive,
|
||||
irsdkAbsActive,
|
||||
}
|
||||
irsdk_WaterTempWarning = 0x00000001
|
||||
irsdk_FuelPressureWarning = 0x00000002
|
||||
irsdk_OilPressureWarning = 0x00000004
|
||||
irsdk_EngineStalled = 0x00000008
|
||||
irsdk_PitSpeedLimiter = 0x00000010
|
||||
irsdk_RevLimiterActive = 0x00000020
|
||||
irsdk_AbsActive = 0x00000100
|
||||
)
|
||||
|
||||
func (i *IBT) WaterTempWarning() bool {
|
||||
func (i *IBT) checkEngineWarningsBitfield(field int) bool {
|
||||
val, ok := i.Vars.Vars["EngineWarnings"]
|
||||
if !ok {
|
||||
log.Fatal("no EngineWarnings")
|
||||
}
|
||||
|
||||
bitfield, err := strconv.ParseInt(val.Value.(string), 0, 64)
|
||||
if err != nil {
|
||||
log.Fatal("unable to get EngineWarnings: " + err.Error())
|
||||
bitfield, ok := val.Value.(uint32)
|
||||
if !ok {
|
||||
log.Fatalf("unable to typecast EngineWarnings: %+v", val)
|
||||
}
|
||||
|
||||
return bitfield&irsdk_WaterTempWarning != 0
|
||||
return bitfield&uint32(field) != 0
|
||||
}
|
||||
|
||||
func (i *IBT) WaterTempWarning() bool {
|
||||
return i.checkEngineWarningsBitfield(irsdk_WaterTempWarning)
|
||||
}
|
||||
|
||||
func (i *IBT) FuelPressureWarning() bool {
|
||||
val, ok := i.Vars.Vars["EngineWarnings"]
|
||||
if !ok {
|
||||
log.Fatal("no EngineWarnings")
|
||||
}
|
||||
|
||||
bitfield, err := strconv.ParseInt(val.Value.(string), 0, 64)
|
||||
if err != nil {
|
||||
log.Fatal("unable to get EngineWarnings: " + err.Error())
|
||||
}
|
||||
|
||||
return bitfield&irsdk_FuelPressureWarning != 0
|
||||
return i.checkEngineWarningsBitfield(irsdk_FuelPressureWarning)
|
||||
}
|
||||
|
||||
func (i *IBT) OilPressureWarning() bool {
|
||||
val, ok := i.Vars.Vars["EngineWarnings"]
|
||||
if !ok {
|
||||
log.Fatal("no EngineWarnings")
|
||||
}
|
||||
|
||||
bitfield, err := strconv.ParseInt(val.Value.(string), 0, 64)
|
||||
if err != nil {
|
||||
log.Fatal("unable to get EngineWarnings: " + err.Error())
|
||||
}
|
||||
|
||||
return bitfield&irsdk_OilPressureWarning != 0
|
||||
return i.checkEngineWarningsBitfield(irsdk_OilPressureWarning)
|
||||
}
|
||||
|
||||
func (i *IBT) EngineStalled() bool {
|
||||
val, ok := i.Vars.Vars["EngineWarnings"]
|
||||
if !ok {
|
||||
log.Fatal("no EngineWarnings")
|
||||
}
|
||||
|
||||
bitfield, err := strconv.ParseInt(val.Value.(string), 0, 64)
|
||||
if err != nil {
|
||||
log.Fatal("unable to get EngineWarnings: " + err.Error())
|
||||
}
|
||||
|
||||
return bitfield&irsdk_EngineStalled != 0
|
||||
return i.checkEngineWarningsBitfield(irsdk_EngineStalled)
|
||||
}
|
||||
|
||||
func (i *IBT) PitSpeedLimiter() bool {
|
||||
val, ok := i.Vars.Vars["EngineWarnings"]
|
||||
if !ok {
|
||||
log.Fatal("no EngineWarnings")
|
||||
}
|
||||
|
||||
bitfield, err := strconv.ParseInt(val.Value.(string), 0, 64)
|
||||
if err != nil {
|
||||
log.Fatal("unable to get EngineWarnings: " + err.Error())
|
||||
}
|
||||
|
||||
return bitfield&irsdk_PitSpeedLimiter != 0
|
||||
return i.checkEngineWarningsBitfield(irsdk_PitSpeedLimiter)
|
||||
}
|
||||
|
||||
func (i *IBT) RevLimiterActive() bool {
|
||||
val, ok := i.Vars.Vars["EngineWarnings"]
|
||||
if !ok {
|
||||
log.Fatal("no EngineWarnings")
|
||||
}
|
||||
|
||||
bitfield, err := strconv.ParseInt(val.Value.(string), 0, 64)
|
||||
if err != nil {
|
||||
log.Fatal("unable to get EngineWarnings: " + err.Error())
|
||||
}
|
||||
|
||||
return bitfield&irsdk_RevLimiterActive != 0
|
||||
return i.checkEngineWarningsBitfield(irsdk_RevLimiterActive)
|
||||
}
|
||||
|
||||
func (i *IBT) AbsActive() bool {
|
||||
val, ok := i.Vars.Vars["EngineWarnings"]
|
||||
if !ok {
|
||||
log.Fatal("no EngineWarnings")
|
||||
}
|
||||
|
||||
bitfield, err := strconv.ParseInt(val.Value.(string), 0, 64)
|
||||
if err != nil {
|
||||
log.Fatal("unable to get EngineWarnings: " + err.Error())
|
||||
}
|
||||
|
||||
return bitfield&irsdk_AbsActive != 0
|
||||
return i.checkEngineWarningsBitfield(irsdk_AbsActive)
|
||||
}
|
||||
|
||||
// EngineWarnings - END
|
||||
@@ -274,7 +206,7 @@ const (
|
||||
irsdk_StateCoolDown = 0x06
|
||||
)
|
||||
|
||||
func (i *IBT) SessionStateInvalid() bool {
|
||||
func (i *IBT) checkSessionStateField(field int) bool {
|
||||
val, ok := i.Vars.Vars["SessionState"]
|
||||
if !ok {
|
||||
return true
|
||||
@@ -285,92 +217,36 @@ func (i *IBT) SessionStateInvalid() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
return value == irsdk_StateInvalid
|
||||
return value == field
|
||||
}
|
||||
|
||||
func (i *IBT) SessionStateInvalid() bool {
|
||||
return i.checkSessionStateField(irsdk_StateInvalid)
|
||||
}
|
||||
|
||||
// BUG: Need to fix all of these functions, they are all wrong
|
||||
func (i *IBT) SessionStateGetInCar() bool {
|
||||
val, ok := i.Vars.Vars["SessionState"]
|
||||
if !ok {
|
||||
log.Fatal("no SessionState")
|
||||
}
|
||||
|
||||
bitfield, err := strconv.ParseInt(val.Value.(string), 0, 64)
|
||||
if err != nil {
|
||||
log.Fatal("unable to get SessionState: " + err.Error())
|
||||
}
|
||||
|
||||
return bitfield == irsdk_StateGetInCar
|
||||
return i.checkSessionStateField(irsdk_StateGetInCar)
|
||||
}
|
||||
|
||||
func (i *IBT) SessionStateWarmup() bool {
|
||||
val, ok := i.Vars.Vars["SessionState"]
|
||||
if !ok {
|
||||
log.Fatal("no SessionState")
|
||||
}
|
||||
|
||||
bitfield, err := strconv.ParseInt(val.Value.(string), 0, 64)
|
||||
if err != nil {
|
||||
log.Fatal("unable to get SessionState: " + err.Error())
|
||||
}
|
||||
|
||||
return bitfield == irsdk_StateWarmup
|
||||
return i.checkSessionStateField(irsdk_StateWarmup)
|
||||
}
|
||||
|
||||
func (i *IBT) SessionStateParadeLaps() bool {
|
||||
val, ok := i.Vars.Vars["SessionState"]
|
||||
if !ok {
|
||||
log.Fatal("no SessionState")
|
||||
}
|
||||
|
||||
bitfield, err := strconv.ParseInt(val.Value.(string), 0, 64)
|
||||
if err != nil {
|
||||
log.Fatal("unable to get SessionState: " + err.Error())
|
||||
}
|
||||
|
||||
return bitfield == irsdk_StateParadeLaps
|
||||
return i.checkSessionStateField(irsdk_StateParadeLaps)
|
||||
}
|
||||
|
||||
func (i *IBT) SessionStateRacing() bool {
|
||||
val, ok := i.Vars.Vars["SessionState"]
|
||||
if !ok {
|
||||
log.Fatal("no SessionState")
|
||||
}
|
||||
|
||||
bitfield, err := strconv.ParseInt(val.Value.(string), 0, 64)
|
||||
if err != nil {
|
||||
log.Fatal("unable to get SessionState: " + err.Error())
|
||||
}
|
||||
|
||||
return bitfield == irsdk_StateRacing
|
||||
return i.checkSessionStateField(irsdk_StateRacing)
|
||||
}
|
||||
|
||||
func (i *IBT) SessionStateCheckered() bool {
|
||||
val, ok := i.Vars.Vars["SessionState"]
|
||||
if !ok {
|
||||
log.Fatal("no SessionState")
|
||||
}
|
||||
|
||||
bitfield, err := strconv.ParseInt(val.Value.(string), 0, 64)
|
||||
if err != nil {
|
||||
log.Fatal("unable to get SessionState: " + err.Error())
|
||||
}
|
||||
|
||||
return bitfield == irsdk_StateCheckered
|
||||
return i.checkSessionStateField(irsdk_StateCheckered)
|
||||
}
|
||||
|
||||
func (i *IBT) SessionStateCoolDown() bool {
|
||||
val, ok := i.Vars.Vars["SessionState"]
|
||||
if !ok {
|
||||
log.Fatal("no SessionState")
|
||||
}
|
||||
|
||||
bitfield, err := strconv.ParseInt(val.Value.(string), 0, 64)
|
||||
if err != nil {
|
||||
log.Fatal("unable to get SessionState: " + err.Error())
|
||||
}
|
||||
|
||||
return bitfield == irsdk_StateCoolDown
|
||||
return i.checkSessionStateField(irsdk_StateCoolDown)
|
||||
}
|
||||
|
||||
func SessionStateToString(state int) string {
|
||||
@@ -652,175 +528,79 @@ func CameraStateToString(state int) string {
|
||||
// PitSvFlags -- Start
|
||||
const (
|
||||
// Tires
|
||||
irsdk_LFTireChange = 0x00000001
|
||||
irsdk_RFTireChange = 0x00000002
|
||||
irsdk_LRTireChange = 0x00000004
|
||||
irsdk_RRTireChange = 0x00000008
|
||||
irsdk_LFTireChange uint32 = 0x00000001
|
||||
irsdk_RFTireChange uint32 = 0x00000002
|
||||
irsdk_LRTireChange uint32 = 0x00000004
|
||||
irsdk_RRTireChange uint32 = 0x00000008
|
||||
// Fuel
|
||||
irsdk_FuelFill = 0x00000010
|
||||
irsdk_FuelFill uint32 = 0x00000010
|
||||
|
||||
irsdk_WindshieldTearoff = 0x00000020
|
||||
irsdk_FastRepair = 0x00000040
|
||||
irsdk_WindshieldTearoff uint32 = 0x00000020
|
||||
irsdk_FastRepair uint32 = 0x00000040
|
||||
|
||||
// Other pit service request flags
|
||||
irsdk_ClearTires = 0x00000080 // Uncheck tire change
|
||||
irsdk_ClearWS = 0x00000100 // Uncheck windshield tearoff
|
||||
irsdk_ClearFR = 0x00000200 // Uncheck FastRepair
|
||||
irsdk_ClearFuel = 0x00000400 // Uncheck refuelling
|
||||
irsdk_ClearTires uint32 = 0x00000080 // Uncheck tire change
|
||||
irsdk_ClearWS uint32 = 0x00000100 // Uncheck windshield tearoff
|
||||
irsdk_ClearFR uint32 = 0x00000200 // Uncheck FastRepair
|
||||
irsdk_ClearFuel uint32 = 0x00000400 // Uncheck refuelling
|
||||
)
|
||||
|
||||
func (i *IBT) LFTireChange() bool {
|
||||
func (i *IBT) checkPitSvFlags(field uint32) bool {
|
||||
val, ok := i.Vars.Vars["PitSvFlags"]
|
||||
if !ok {
|
||||
log.Fatal("no PitSvFlags")
|
||||
}
|
||||
|
||||
bitfield, err := strconv.ParseInt(val.Value.(string), 0, 64)
|
||||
if err != nil {
|
||||
log.Fatal("unable to get PitSvFlags: " + err.Error())
|
||||
bitfield, ok := val.Value.(uint32)
|
||||
if !ok {
|
||||
log.Fatal("unable to get PitSvFlags: %+v", val)
|
||||
}
|
||||
|
||||
return bitfield&irsdk_LFTireChange != 0
|
||||
return bitfield&field != 0
|
||||
}
|
||||
|
||||
func (i *IBT) LFTireChange() bool {
|
||||
return i.checkPitSvFlags(irsdk_LFTireChange)
|
||||
}
|
||||
|
||||
func (i *IBT) RFTireChange() bool {
|
||||
val, ok := i.Vars.Vars["PitSvFlags"]
|
||||
if !ok {
|
||||
log.Fatal("no PitSvFlags")
|
||||
}
|
||||
|
||||
bitfield, err := strconv.ParseInt(val.Value.(string), 0, 64)
|
||||
if err != nil {
|
||||
log.Fatal("unable to get PitSvFlags: " + err.Error())
|
||||
}
|
||||
|
||||
return bitfield&irsdk_RFTireChange != 0
|
||||
return i.checkPitSvFlags(irsdk_RFTireChange)
|
||||
}
|
||||
|
||||
func (i *IBT) LRTireChange() bool {
|
||||
val, ok := i.Vars.Vars["PitSvFlags"]
|
||||
if !ok {
|
||||
log.Fatal("no PitSvFlags")
|
||||
}
|
||||
|
||||
bitfield, err := strconv.ParseInt(val.Value.(string), 0, 64)
|
||||
if err != nil {
|
||||
log.Fatal("unable to get PitSvFlags: " + err.Error())
|
||||
}
|
||||
|
||||
return bitfield&irsdk_LRTireChange != 0
|
||||
return i.checkPitSvFlags(irsdk_LRTireChange)
|
||||
}
|
||||
|
||||
func (i *IBT) RRTireChange() bool {
|
||||
val, ok := i.Vars.Vars["PitSvFlags"]
|
||||
if !ok {
|
||||
log.Fatal("no PitSvFlags")
|
||||
}
|
||||
|
||||
bitfield, err := strconv.ParseInt(val.Value.(string), 0, 64)
|
||||
if err != nil {
|
||||
log.Fatal("unable to get PitSvFlags: " + err.Error())
|
||||
}
|
||||
|
||||
return bitfield&irsdk_RRTireChange != 0
|
||||
return i.checkPitSvFlags(irsdk_RRTireChange)
|
||||
}
|
||||
|
||||
func (i *IBT) FuelFill() bool {
|
||||
val, ok := i.Vars.Vars["PitSvFlags"]
|
||||
if !ok {
|
||||
log.Fatal("no PitSvFlags")
|
||||
}
|
||||
|
||||
bitfield, err := strconv.ParseInt(val.Value.(string), 0, 64)
|
||||
if err != nil {
|
||||
log.Fatal("unable to get PitSvFlags: " + err.Error())
|
||||
}
|
||||
|
||||
return bitfield&irsdk_FuelFill != 0
|
||||
return i.checkPitSvFlags(irsdk_FuelFill)
|
||||
}
|
||||
|
||||
func (i *IBT) WindshieldTearoff() bool {
|
||||
val, ok := i.Vars.Vars["PitSvFlags"]
|
||||
if !ok {
|
||||
log.Fatal("no PitSvFlags")
|
||||
}
|
||||
|
||||
bitfield, err := strconv.ParseInt(val.Value.(string), 0, 64)
|
||||
if err != nil {
|
||||
log.Fatal("unable to get PitSvFlags: " + err.Error())
|
||||
}
|
||||
|
||||
return bitfield&irsdk_WindshieldTearoff != 0
|
||||
return i.checkPitSvFlags(irsdk_WindshieldTearoff)
|
||||
}
|
||||
|
||||
func (i *IBT) FastRepair() bool {
|
||||
val, ok := i.Vars.Vars["PitSvFlags"]
|
||||
if !ok {
|
||||
log.Fatal("no PitSvFlags")
|
||||
}
|
||||
|
||||
bitfield, err := strconv.ParseInt(val.Value.(string), 0, 64)
|
||||
if err != nil {
|
||||
log.Fatal("unable to get PitSvFlags: " + err.Error())
|
||||
}
|
||||
|
||||
return bitfield&irsdk_FastRepair != 0
|
||||
return i.checkPitSvFlags(irsdk_FastRepair)
|
||||
}
|
||||
|
||||
func (i *IBT) ClearTires() bool {
|
||||
val, ok := i.Vars.Vars["PitSvFlags"]
|
||||
if !ok {
|
||||
log.Fatal("no PitSvFlags")
|
||||
}
|
||||
|
||||
bitfield, err := strconv.ParseInt(val.Value.(string), 0, 64)
|
||||
if err != nil {
|
||||
log.Fatal("unable to get PitSvFlags: " + err.Error())
|
||||
}
|
||||
|
||||
return bitfield&irsdk_ClearTires != 0
|
||||
return i.checkPitSvFlags(irsdk_ClearTires)
|
||||
}
|
||||
|
||||
func (i *IBT) ClearWS() bool {
|
||||
val, ok := i.Vars.Vars["PitSvFlags"]
|
||||
if !ok {
|
||||
log.Fatal("no PitSvFlags")
|
||||
}
|
||||
|
||||
bitfield, err := strconv.ParseInt(val.Value.(string), 0, 64)
|
||||
if err != nil {
|
||||
log.Fatal("unable to get PitSvFlags: " + err.Error())
|
||||
}
|
||||
|
||||
return bitfield&irsdk_ClearWS != 0
|
||||
return i.checkPitSvFlags(irsdk_ClearWS)
|
||||
}
|
||||
|
||||
func (i *IBT) ClearFR() bool {
|
||||
val, ok := i.Vars.Vars["PitSvFlags"]
|
||||
if !ok {
|
||||
log.Fatal("no PitSvFlags")
|
||||
}
|
||||
|
||||
bitfield, err := strconv.ParseInt(val.Value.(string), 0, 64)
|
||||
if err != nil {
|
||||
log.Fatal("unable to get PitSvFlags: " + err.Error())
|
||||
}
|
||||
|
||||
return bitfield&irsdk_ClearFR != 0
|
||||
return i.checkPitSvFlags(irsdk_ClearFR)
|
||||
}
|
||||
|
||||
func (i *IBT) ClearFuel() bool {
|
||||
val, ok := i.Vars.Vars["PitSvFlags"]
|
||||
if !ok {
|
||||
log.Fatal("no PitSvFlags")
|
||||
}
|
||||
|
||||
bitfield, err := strconv.ParseInt(val.Value.(string), 0, 64)
|
||||
if err != nil {
|
||||
log.Fatal("unable to get PitSvFlags: " + err.Error())
|
||||
}
|
||||
|
||||
return bitfield&irsdk_ClearFuel != 0
|
||||
return i.checkPitSvFlags(irsdk_ClearFuel)
|
||||
}
|
||||
|
||||
// PitSvFlags -- End
|
||||
|
||||
+1
-7
@@ -4,8 +4,6 @@ import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
|
||||
"github.com/ESilva15/goirsdk/logger"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -23,8 +21,6 @@ type DiskSubHeader struct {
|
||||
|
||||
// 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 {
|
||||
@@ -39,7 +35,7 @@ func (i *IBT) readSubheader() error {
|
||||
if i.Opts.IBTExport {
|
||||
err = i.exportIBT(subheaderRaw[:], HeaderSize)
|
||||
if err != nil {
|
||||
log.Printf("failed to export subheaders: %v\n", err)
|
||||
i.Opts.Logger.Debug("failed to export disksubheader", "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,8 +46,6 @@ func (i *IBT) readSubheader() error {
|
||||
// or nil if an error occurs. In which case the error return value is more
|
||||
// valuable
|
||||
func parseTelemetrySubHeader(buf [SubHeaderSize]byte) (*DiskSubHeader, error) {
|
||||
// utils.HexDump(buf[:])
|
||||
|
||||
dst := DiskSubHeader{}
|
||||
err := binary.Read(bytes.NewBuffer(buf[:]), binary.LittleEndian, &dst)
|
||||
if err != nil {
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
// I should rename winutils to something else but what this package does
|
||||
// is interface some windows stuff that we need for the:
|
||||
// - Broadcast Channel
|
||||
// - Valid Data Event windows thing
|
||||
package mmaputils
|
||||
|
||||
import (
|
||||
"io"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Reader interface {
|
||||
io.Reader
|
||||
io.ReaderAt
|
||||
io.ReadCloser
|
||||
}
|
||||
|
||||
type EventUtils struct {
|
||||
Utils *utils
|
||||
}
|
||||
|
||||
func Init() (*EventUtils, error) {
|
||||
u, err := newUtils()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &EventUtils{u}, nil
|
||||
}
|
||||
|
||||
func (u *EventUtils) Close() {
|
||||
if u.Utils == nil {
|
||||
return
|
||||
}
|
||||
|
||||
u.Utils.Close()
|
||||
}
|
||||
|
||||
// OpenEvent will open the named windows event
|
||||
func (u *EventUtils) OpenEvent(name string) error {
|
||||
return u.Utils.OpenEvent(name)
|
||||
}
|
||||
|
||||
// OpenBroadcastChannel will open the broadcast channel
|
||||
func (u *EventUtils) OpenBroadcastChannel(name string) error {
|
||||
return u.Utils.OpenBroadcastChannel(name)
|
||||
}
|
||||
|
||||
// CheckValidDataEvent checks if our windows even is telling us we are good to go
|
||||
func (u *EventUtils) CheckValidDataEvent(timeout time.Duration) bool {
|
||||
return u.Utils.CheckValidDataEvent(timeout)
|
||||
}
|
||||
|
||||
// SignalEvent triggers a frame pulse (used by mock servers and tests)
|
||||
func (u *EventUtils) SignalEvent() error {
|
||||
return u.Utils.SignalEvent()
|
||||
}
|
||||
|
||||
// SignalEvent triggers a frame pulse (used by mock servers and tests)
|
||||
func SignalEvent(name string) {
|
||||
signalEvent(name)
|
||||
}
|
||||
|
||||
// CleanupEvent cleans up event resources (used by mock servers and tests)
|
||||
func CleanupEvent(name string) {
|
||||
cleanupEvent(name)
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
//go:build linux && cgo
|
||||
|
||||
package mmaputils
|
||||
|
||||
/*
|
||||
#include <semaphore.h>
|
||||
#include <fcntl.h>
|
||||
#include <time.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static inline void* open_posix_semaphore(const char* name) {
|
||||
sem_t* sem = sem_open(name, O_CREAT, 0666, 0);
|
||||
if (sem == SEM_FAILED) {
|
||||
return NULL;
|
||||
}
|
||||
return (void*)sem;
|
||||
}
|
||||
|
||||
static inline void close_posix_semaphore(void* sem_ptr) {
|
||||
if (sem_ptr != NULL) {
|
||||
sem_close((sem_t*)sem_ptr);
|
||||
}
|
||||
}
|
||||
|
||||
static inline int timed_wait_posix_semaphore(void* sem_ptr, long timeout_ms) {
|
||||
if (sem_ptr == NULL) {
|
||||
return -1;
|
||||
}
|
||||
sem_t* sem = (sem_t*)sem_ptr;
|
||||
|
||||
// Drain the semaphore
|
||||
while (sem_trywait(sem) == 0) {}
|
||||
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
|
||||
ts.tv_sec += timeout_ms / 1000;
|
||||
ts.tv_nsec += (timeout_ms % 1000) * 1000000;
|
||||
|
||||
if (ts.tv_nsec >= 1000000000) {
|
||||
ts.tv_sec += ts.tv_nsec / 1000000000;
|
||||
ts.tv_nsec %= 1000000000;
|
||||
}
|
||||
|
||||
int res;
|
||||
do {
|
||||
res = sem_timedwait(sem, &ts);
|
||||
} while (res == -1 && errno == EINTR); // Retry if interrupted by Go GC/scheduler
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline int signal_posix_semaphore(const char* name) {
|
||||
if (name == NULL) {
|
||||
return -1;
|
||||
}
|
||||
sem_t* sem = sem_open(name, 0);
|
||||
if (sem == SEM_FAILED) {
|
||||
return -1;
|
||||
}
|
||||
int res = sem_post(sem);
|
||||
sem_close(sem);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline int post_posix_semaphore(void* sem_ptr) {
|
||||
if (sem_ptr != NULL) {
|
||||
return sem_post((sem_t*)sem_ptr);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static inline int unlink_posix_semaphore(const char* name) {
|
||||
if (name == NULL) {
|
||||
return -1;
|
||||
}
|
||||
return sem_unlink(name);
|
||||
}
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"github.com/ESilva15/goirsdk/sharedMem"
|
||||
)
|
||||
|
||||
type utils struct {
|
||||
semName string
|
||||
sem unsafe.Pointer
|
||||
}
|
||||
|
||||
func newUtils() (*utils, error) {
|
||||
return &utils{}, nil
|
||||
}
|
||||
|
||||
func (u *utils) Close() {
|
||||
if u.sem != nil {
|
||||
C.close_posix_semaphore(u.sem)
|
||||
u.sem = nil
|
||||
}
|
||||
}
|
||||
|
||||
// OpenMemMap returns a Reader interface that can be used to read the data
|
||||
// No need to encapsulate it
|
||||
func OpenMemMap(name string, size uint32) (Reader, error) {
|
||||
file, err := sharedMem.Open(name, size)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open `%s` with err: %+v", name, err)
|
||||
}
|
||||
|
||||
return file, nil
|
||||
}
|
||||
|
||||
// OpenEvent creates or connects to a Unix socket for event signaling on Linux
|
||||
func (u *utils) OpenEvent(eventName string) error {
|
||||
u.semName = "/" + strings.TrimPrefix(eventName, "/")
|
||||
|
||||
cName := C.CString(u.semName)
|
||||
defer C.free(unsafe.Pointer(cName))
|
||||
|
||||
sem := C.open_posix_semaphore(cName)
|
||||
if sem == nil {
|
||||
return fmt.Errorf("failed to open POSIX semaphore: %s", u.semName)
|
||||
}
|
||||
|
||||
u.sem = sem
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *utils) OpenBroadcastChannel(name string) error {
|
||||
// No-op or log stub on Linux
|
||||
return nil
|
||||
}
|
||||
|
||||
// CheckValidDataEvent waits for a pulse byte sent over the socket
|
||||
func (u *utils) CheckValidDataEvent(timeout time.Duration) bool {
|
||||
if u.sem == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
ms := C.long(timeout.Milliseconds())
|
||||
res := C.timed_wait_posix_semaphore(u.sem, ms)
|
||||
|
||||
return res == 0
|
||||
}
|
||||
|
||||
func (u *utils) SendBroadcastMessage(id, p1, p2 uintptr) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *utils) SignalEvent() error {
|
||||
if u.sem == nil {
|
||||
return fmt.Errorf("u.sem is not set")
|
||||
}
|
||||
|
||||
C.post_posix_semaphore(u.sem)
|
||||
return nil
|
||||
}
|
||||
|
||||
func signalEvent(name string) {
|
||||
semName := "/" + strings.TrimPrefix(name, "/")
|
||||
cName := C.CString(semName)
|
||||
defer C.free(unsafe.Pointer(cName))
|
||||
C.signal_posix_semaphore(cName)
|
||||
}
|
||||
|
||||
func cleanupEvent(name string) {
|
||||
semName := "/" + strings.TrimPrefix(name, "/")
|
||||
cName := C.CString(semName)
|
||||
defer C.free(unsafe.Pointer(cName))
|
||||
C.unlink_posix_semaphore(cName)
|
||||
}
|
||||
@@ -0,0 +1,202 @@
|
||||
package mmaputils_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
eventutils "github.com/ESilva15/goirsdk/eventutils"
|
||||
)
|
||||
|
||||
const testEventName = "test_iRSDKDataValidEvent"
|
||||
|
||||
// Tests initialization and resource cleanup
|
||||
func TestInitAndClose(t *testing.T) {
|
||||
sdkUtils, err := eventutils.Init()
|
||||
if err != nil {
|
||||
t.Fatalf("Init() failed: %v", err)
|
||||
}
|
||||
if sdkUtils == nil {
|
||||
t.Fatalf("Init() returned nil struct")
|
||||
}
|
||||
|
||||
sdkUtils.Close()
|
||||
}
|
||||
|
||||
// Tests timeout behavior when telemetry stalls or stops
|
||||
func TestCheckValidDataEvent_Timeout(t *testing.T) {
|
||||
defer eventutils.CleanupEvent(testEventName)
|
||||
|
||||
sdkUtils, err := eventutils.Init()
|
||||
if err != nil {
|
||||
t.Fatalf("Init() failed: %v", err)
|
||||
}
|
||||
defer sdkUtils.Close()
|
||||
|
||||
if err := sdkUtils.OpenEvent(testEventName); err != nil {
|
||||
t.Fatalf("OpenEvent() failed: %v", err)
|
||||
}
|
||||
|
||||
timeout := 40 * time.Millisecond
|
||||
start := time.Now()
|
||||
|
||||
// Should return false because no producer signaled the event
|
||||
got := sdkUtils.CheckValidDataEvent(timeout)
|
||||
elapsed := time.Since(start)
|
||||
|
||||
if got != false {
|
||||
t.Errorf("expected CheckValidDataEvent to return false on timeout, got true")
|
||||
}
|
||||
|
||||
if elapsed < timeout {
|
||||
t.Errorf("expected timeout to wait at least %v, returned early after %v", timeout, elapsed)
|
||||
}
|
||||
}
|
||||
|
||||
// Tests successful unblocking when a frame signal arrives
|
||||
func TestCheckValidDataEvent_Signaled(t *testing.T) {
|
||||
defer eventutils.CleanupEvent(testEventName)
|
||||
|
||||
sdkUtils, err := eventutils.Init()
|
||||
if err != nil {
|
||||
t.Fatalf("Init() failed: %v", err)
|
||||
}
|
||||
defer sdkUtils.Close()
|
||||
|
||||
if err := sdkUtils.OpenEvent(testEventName); err != nil {
|
||||
t.Fatalf("OpenEvent() failed: %v", err)
|
||||
}
|
||||
|
||||
// Simulate mock producer sending a frame signal after 10ms
|
||||
go func() {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
eventutils.SignalEvent(testEventName)
|
||||
}()
|
||||
|
||||
start := time.Now()
|
||||
got := sdkUtils.CheckValidDataEvent(200 * time.Millisecond)
|
||||
elapsed := time.Since(start)
|
||||
|
||||
if got != true {
|
||||
t.Errorf("expected CheckValidDataEvent to return true on signal, got false")
|
||||
}
|
||||
|
||||
if elapsed >= 150*time.Millisecond {
|
||||
t.Errorf("CheckValidDataEvent took too long (%v), failed to wake up immediately", elapsed)
|
||||
}
|
||||
}
|
||||
|
||||
// Tests multi-frame streaming loop (simulating steady 60Hz feed)
|
||||
func TestCheckValidDataEvent_MultipleTicks(t *testing.T) {
|
||||
defer eventutils.CleanupEvent(testEventName)
|
||||
|
||||
sdkUtils, err := eventutils.Init()
|
||||
if err != nil {
|
||||
t.Fatalf("Init() failed: %v", err)
|
||||
}
|
||||
defer sdkUtils.Close()
|
||||
|
||||
if err := sdkUtils.OpenEvent(testEventName); err != nil {
|
||||
t.Fatalf("OpenEvent() failed: %v", err)
|
||||
}
|
||||
|
||||
frameCount := 5
|
||||
go func() {
|
||||
for i := 0; i < frameCount; i++ {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
eventutils.SignalEvent(testEventName)
|
||||
}
|
||||
}()
|
||||
|
||||
for i := 0; i < frameCount; i++ {
|
||||
ok := sdkUtils.CheckValidDataEvent(100 * time.Millisecond)
|
||||
if !ok {
|
||||
t.Fatalf("failed to receive pulse for tick %d", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Test60FPS60SecondsSemaphores(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping 60-second endurance test in short mode")
|
||||
}
|
||||
|
||||
const eventName = "test_60fps_event"
|
||||
const targetFPS = 60
|
||||
const durationSeconds = 60
|
||||
const totalFrames = targetFPS * durationSeconds // 3,600 frames
|
||||
const frameInterval = time.Second / targetFPS // ~16.666ms
|
||||
|
||||
// Timeout per frame set to 100ms to absorb normal OS thread scheduling jitter
|
||||
const frameWaitTimeout = 250 * time.Millisecond
|
||||
|
||||
// Cleanup prior event state
|
||||
eventutils.CleanupEvent(eventName)
|
||||
defer eventutils.CleanupEvent(eventName)
|
||||
|
||||
// Initialize Reader
|
||||
uReader, err := eventutils.Init()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to initialize reader: %v", err)
|
||||
}
|
||||
defer uReader.Close()
|
||||
|
||||
if err := uReader.OpenEvent(eventName); err != nil {
|
||||
t.Fatalf("Failed to open event on reader: %v", err)
|
||||
}
|
||||
|
||||
// Initialize Writer
|
||||
uWriter, err := eventutils.Init()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to initialize writer: %v", err)
|
||||
}
|
||||
defer uWriter.Close()
|
||||
|
||||
if err := uWriter.OpenEvent(eventName); err != nil {
|
||||
t.Fatalf("Failed to open event on writer: %v", err)
|
||||
}
|
||||
|
||||
stopWriter := make(chan struct{})
|
||||
writerDone := make(chan struct{})
|
||||
|
||||
// Producer Goroutine: Emits pulse at 60 FPS
|
||||
go func() {
|
||||
defer close(writerDone)
|
||||
ticker := time.NewTicker(frameInterval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-stopWriter:
|
||||
return
|
||||
case <-ticker.C:
|
||||
if err := uWriter.SignalEvent(); err != nil {
|
||||
t.Errorf("SignalEvent failed on writer: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
startTime := time.Now()
|
||||
receivedFrames := 0
|
||||
|
||||
// Consumer Loop: Consumes 3,600 frames continuously
|
||||
for i := 1; i <= totalFrames; i++ {
|
||||
ok := uReader.CheckValidDataEvent(frameWaitTimeout)
|
||||
if !ok {
|
||||
close(stopWriter)
|
||||
<-writerDone
|
||||
t.Fatalf("FAILED at frame %d/%d (elapsed: %v). Semaphore timed out after %v.",
|
||||
i, totalFrames, time.Since(startTime), frameWaitTimeout)
|
||||
}
|
||||
receivedFrames++
|
||||
}
|
||||
|
||||
elapsed := time.Since(startTime)
|
||||
close(stopWriter)
|
||||
<-writerDone
|
||||
|
||||
actualFPS := float64(receivedFrames) / elapsed.Seconds()
|
||||
t.Logf("Passed: Processed %d/%d frames continuously in %v (Average FPS: %.2f)",
|
||||
receivedFrames, totalFrames, elapsed, actualFPS)
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
// go:build windows
|
||||
//go:build windows
|
||||
|
||||
package mmaputils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
@@ -55,9 +57,14 @@ func (u *utils) OpenEvent(eventName string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
event, err := windows.OpenEvent(windows.SYNCHRONIZE, false, name)
|
||||
// Request EVENT_MODIFY_STATE so SetEvent can be called on this handle
|
||||
event, err := windows.OpenEvent(windows.SYNCHRONIZE|windows.EVENT_MODIFY_STATE, false, name)
|
||||
if err != nil {
|
||||
return err
|
||||
// If event does not exist yet, create it
|
||||
event, err = windows.CreateEvent(nil, 0, 0, name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
u.wEvent = event
|
||||
|
||||
@@ -87,6 +94,33 @@ func (u *utils) OpenBroadcastChannel(name string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *utils) SignalEvent() error {
|
||||
if u.wEvent != 0 {
|
||||
err := windows.SetEvent(u.wEvent)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to signal Win32 event: %+v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func signalEvent(name string) {
|
||||
cName, err := syscall.UTF16PtrFromString(name)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
h, err := windows.OpenEvent(windows.EVENT_MODIFY_STATE, false, cName)
|
||||
if err == nil {
|
||||
windows.SetEvent(h)
|
||||
windows.CloseHandle(h)
|
||||
}
|
||||
}
|
||||
|
||||
func cleanupEvent(name string) {
|
||||
// Win32 events clean up automatically when handles close
|
||||
}
|
||||
|
||||
// INITIALIZATION
|
||||
|
||||
// closeEvent closes a given windows.Handle
|
||||
@@ -3,6 +3,8 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"log/slog"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/ESilva15/goirsdk"
|
||||
@@ -13,8 +15,20 @@ func msToKph(v float32) int {
|
||||
}
|
||||
|
||||
func main() {
|
||||
output, err := os.OpenFile("./output.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o755)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open log file: %+v", err)
|
||||
}
|
||||
|
||||
logger := slog.New(
|
||||
slog.NewTextHandler(output, &slog.HandlerOptions{
|
||||
Level: slog.LevelDebug,
|
||||
}),
|
||||
)
|
||||
|
||||
// Instantiate our iRacing SDK instance
|
||||
irsdk, err := goirsdk.Init(goirsdk.Options{
|
||||
Logger: logger,
|
||||
SourceType: goirsdk.IBTFile,
|
||||
SourcePath: "../../../../testTelemetry/mx5_2016Okayama_full_2024_10_19_22_02_12.ibt",
|
||||
IBTExportType: goirsdk.SharedMemoryFile,
|
||||
@@ -60,14 +74,14 @@ func main() {
|
||||
sessionState := irsdk.Vars.Vars["SessionState"].Value.(int)
|
||||
trkloc := irsdk.Vars.Vars["PlayerTrackSurface"].Value.(int)
|
||||
trksurf := irsdk.Vars.Vars["PlayerTrackSurfaceMaterial"].Value.(int)
|
||||
pitsvflags := irsdk.Vars.Vars["PitSvFlags"].Value.(string)
|
||||
pitsvflags := irsdk.Vars.Vars["PitSvFlags"].Value.(uint32)
|
||||
|
||||
fmt.Printf("\033[?25l\033[2J\033[H")
|
||||
fmt.Printf("Gear: %d, RPM: %d, Speed: %d\n", gear, rpm, speed)
|
||||
fmt.Printf("SessionState: %s\n", goirsdk.SessionStateToString(sessionState))
|
||||
fmt.Printf("TrkLoc: %s\n", goirsdk.TrkLocToString(trkloc))
|
||||
fmt.Printf("TrkSurf: %s\n", goirsdk.TrkSurfToString(trksurf))
|
||||
fmt.Printf("PitSvFlags: %s\n", pitsvflags)
|
||||
fmt.Printf("PitSvFlags: %d\n", pitsvflags)
|
||||
fmt.Printf(" FL FR\n")
|
||||
fmt.Printf(" %t %t\n", irsdk.LFTireChange(), irsdk.RFTireChange())
|
||||
fmt.Printf("\n")
|
||||
|
||||
@@ -3,6 +3,8 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"log/slog"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/ESilva15/goirsdk"
|
||||
@@ -13,8 +15,22 @@ func msToKph(v float32) int {
|
||||
}
|
||||
|
||||
func main() {
|
||||
output, err := os.OpenFile("./output.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o755)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open log file: %+v", err)
|
||||
}
|
||||
|
||||
logger := slog.New(
|
||||
slog.NewTextHandler(output, &slog.HandlerOptions{
|
||||
Level: slog.LevelDebug,
|
||||
}),
|
||||
)
|
||||
|
||||
logger.Debug("Starting test")
|
||||
|
||||
// Instantiate our iRacing SDK instance
|
||||
irsdk, err := goirsdk.Init(goirsdk.Options{
|
||||
Logger: logger,
|
||||
SourceType: goirsdk.SharedMemoryFile,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -53,9 +69,29 @@ func main() {
|
||||
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)))
|
||||
sessionState := irsdk.Vars.Vars["SessionState"].Value.(int)
|
||||
trkloc := irsdk.Vars.Vars["PlayerTrackSurface"].Value.(int)
|
||||
trksurf := irsdk.Vars.Vars["PlayerTrackSurfaceMaterial"].Value.(int)
|
||||
pitsvflags := irsdk.Vars.Vars["PitSvFlags"].Value.(uint32)
|
||||
|
||||
fmt.Printf("\033[?25l\033[2J\033[H")
|
||||
fmt.Printf("Gear: %d, RPM: %d, Speed: %d", gear, rpm, speed)
|
||||
fmt.Printf("Gear: %d, RPM: %d, Speed: %d\n", gear, rpm, speed)
|
||||
fmt.Printf("SessionState: %s\n", goirsdk.SessionStateToString(sessionState))
|
||||
fmt.Printf("TrkLoc: %s\n", goirsdk.TrkLocToString(trkloc))
|
||||
fmt.Printf("TrkSurf: %s\n", goirsdk.TrkSurfToString(trksurf))
|
||||
fmt.Printf("PitSvFlags: %d\n", pitsvflags)
|
||||
fmt.Printf(" FL FR\n")
|
||||
fmt.Printf(" %t %t\n", irsdk.LFTireChange(), irsdk.RFTireChange())
|
||||
fmt.Printf("\n")
|
||||
fmt.Printf(" RL RR\n")
|
||||
fmt.Printf(" %t %t\n", irsdk.LRTireChange(), irsdk.RRTireChange())
|
||||
fmt.Printf(" FuelFill: %t\n", irsdk.FuelFill())
|
||||
fmt.Printf(" WindshieldTearoff: %t\n", irsdk.WindshieldTearoff())
|
||||
fmt.Printf(" FastRepair: %t\n", irsdk.FastRepair())
|
||||
fmt.Printf(" ClearTires: %t\n", irsdk.ClearTires())
|
||||
fmt.Printf(" ClearWS: %t\n", irsdk.ClearWS())
|
||||
fmt.Printf(" ClearFR: %t\n", irsdk.ClearFR())
|
||||
fmt.Printf(" ClearFuel: %t\n", irsdk.ClearFuel())
|
||||
|
||||
<-mainLoopTicker.C
|
||||
}
|
||||
|
||||
+1
-5
@@ -4,8 +4,6 @@ import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
|
||||
"github.com/ESilva15/goirsdk/logger"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -32,8 +30,6 @@ type TelemetryHeaders struct {
|
||||
|
||||
// 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 {
|
||||
@@ -48,7 +44,7 @@ func (i *IBT) readHeader() error {
|
||||
if i.Opts.IBTExport {
|
||||
err = i.exportIBT(headerRaw[:], 0)
|
||||
if err != nil {
|
||||
log.Printf("Failed to export headers: %v\n", err)
|
||||
i.Opts.Logger.Debug("Failed to export headers", "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,10 +4,11 @@ package goirsdk
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/ESilva15/goirsdk/logger"
|
||||
"github.com/ESilva15/goirsdk/mmaputils"
|
||||
eventutils "github.com/ESilva15/goirsdk/eventutils"
|
||||
"github.com/ESilva15/goirsdk/sharedMem"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
@@ -33,6 +34,7 @@ const (
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
Logger *slog.Logger
|
||||
SourceType TelemetryContainer // type of source data
|
||||
SourcePath string // Path to source
|
||||
IBTExportType TelemetryContainer // export type of telemetry: store .ibt or replay in shm
|
||||
@@ -47,7 +49,7 @@ type IBT struct {
|
||||
File Reader // Source of the data
|
||||
Opts Options
|
||||
IBTExporter Writer
|
||||
winUtils *mmaputils.IRacingWinUtils // WinUtils gives access to the system utilities
|
||||
winUtils *eventutils.EventUtils // WinUtils gives access to the system utilities
|
||||
|
||||
// TODO: fragment this struct a little bit, for now I want to actually get
|
||||
// stuff done so its enough to work as is
|
||||
@@ -75,11 +77,9 @@ func (i *IBT) IsConnected() bool {
|
||||
}
|
||||
|
||||
func (i *IBT) exportYAML() error {
|
||||
log := logger.GetInstance()
|
||||
|
||||
file, err := os.OpenFile(i.Opts.SessionInfoExportPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644)
|
||||
if err != nil {
|
||||
log.Printf("Failed to open file for YAML export: %v\n", err)
|
||||
i.Opts.Logger.Debug(fmt.Sprintf("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()
|
||||
@@ -88,7 +88,7 @@ func (i *IBT) exportYAML() error {
|
||||
|
||||
err = enc.Encode(i.SessionInfo)
|
||||
if err != nil {
|
||||
log.Printf("Failed to write into file for YAML export: %v\n", err)
|
||||
i.Opts.Logger.Debug(fmt.Sprintf("Failed to write into file for YAML export: %v\n", err))
|
||||
return fmt.Errorf("failed to write YAML contents to file: %v", err)
|
||||
}
|
||||
|
||||
@@ -96,16 +96,24 @@ func (i *IBT) exportYAML() error {
|
||||
}
|
||||
|
||||
func (i *IBT) exportIBT(data []byte, offset int64) error {
|
||||
log := logger.GetInstance()
|
||||
|
||||
_, err := i.IBTExporter.WriteAt(data, offset)
|
||||
nBytes, err := i.IBTExporter.WriteAt(data, offset)
|
||||
if err != nil {
|
||||
i.IBTExporter.Close()
|
||||
i.IBTExporter = nil
|
||||
log.Println("Won't attempt to export anymore")
|
||||
i.Opts.Logger.Debug(fmt.Sprintf("won't attempt to export anymore: %+v", err))
|
||||
return err
|
||||
}
|
||||
|
||||
if nBytes > 0 {
|
||||
// Send the event stating the data has been created
|
||||
err = i.winUtils.Utils.SignalEvent()
|
||||
if err != nil {
|
||||
i.Opts.Logger.Debug("failed to signal event", "err", err)
|
||||
} else {
|
||||
i.Opts.Logger.Debug("no error signaling", "nBytes", nBytes)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -115,7 +123,7 @@ func (i *IBT) openSource() error {
|
||||
switch i.Opts.SourceType {
|
||||
case SharedMemoryFile:
|
||||
// User is requesting us to read live data - present in the mem map file
|
||||
i.File, err = mmaputils.OpenMemMap(MEMMAPFILENAME, fileMapSize)
|
||||
i.File, err = eventutils.OpenMemMap(MEMMAPFILENAME, fileMapSize)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open memory mapped file: %+v", err)
|
||||
}
|
||||
@@ -123,10 +131,10 @@ func (i *IBT) openSource() error {
|
||||
// To use our windows interface we need to initialize it first
|
||||
// it will return a struct with a pointer to the windows handles
|
||||
// if, for some reason, we need to stub out this to run in on Linux its easier
|
||||
i.winUtils, err = mmaputils.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// i.winUtils, err = eventutils.Init()
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
// I don't believe we need this on windows either, but I'll have to check
|
||||
// We need to open the windows event thing
|
||||
@@ -177,16 +185,24 @@ func (i *IBT) openExporter() error {
|
||||
// Init serves to initialize and get a hold of a IBT struct
|
||||
// Receives an Options struct with the required configurations
|
||||
func Init(opts Options) (*IBT, error) {
|
||||
// log := logger.GetInstance()
|
||||
|
||||
// Create our irsdk instance
|
||||
var err error
|
||||
ibt := IBT{
|
||||
Opts: opts,
|
||||
Vars: &TelemetryVars{},
|
||||
winUtils: nil,
|
||||
Opts: opts,
|
||||
Vars: &TelemetryVars{},
|
||||
}
|
||||
|
||||
// Set up the event utils
|
||||
evutils, err := eventutils.Init()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = evutils.OpenEvent(IRSDK_DATAVALIDEVENTNAME)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ibt.winUtils = evutils
|
||||
|
||||
// Setup the source
|
||||
err = ibt.openSource()
|
||||
if err != nil {
|
||||
@@ -224,7 +240,7 @@ func Init(opts Options) (*IBT, error) {
|
||||
// Read the telemetry vars info
|
||||
err = ibt.readVariablerHeaders()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Unable to parser variable headers from file: %v", err)
|
||||
return nil, fmt.Errorf("unable to parser variable headers from file: %v", err)
|
||||
}
|
||||
|
||||
return &ibt, nil
|
||||
@@ -234,8 +250,22 @@ func (i *IBT) ListVariables() map[string]Var {
|
||||
return i.Vars.Vars
|
||||
}
|
||||
|
||||
// CheckForDataEvent
|
||||
// timeout is in ms
|
||||
func (i *IBT) CheckForDataEvent(timeout time.Duration) bool {
|
||||
if i.winUtils.CheckValidDataEvent(timeout) {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// Close cleans up our irsdk instance
|
||||
func (i *IBT) Close() {
|
||||
if i == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if i.winUtils != nil {
|
||||
// If its not live data, the user is the one with ownership of the handle
|
||||
i.File.Close()
|
||||
|
||||
+99
-99
@@ -1,101 +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
|
||||
}
|
||||
}
|
||||
// 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,23 +0,0 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var l *log.Logger
|
||||
var once sync.Once
|
||||
|
||||
func createLogger() {
|
||||
l = log.New(os.Stdout, "[ibtReader] ", log.LstdFlags | log.Lshortfile)
|
||||
}
|
||||
|
||||
func GetInstance() *log.Logger {
|
||||
once.Do(func() {
|
||||
createLogger()
|
||||
})
|
||||
|
||||
return l
|
||||
}
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
// I should rename winutils to something else but what this package does
|
||||
// is interface some windows stuff that we need for the:
|
||||
// - Broadcast Channel
|
||||
// - Valid Data Event windows thing
|
||||
package mmaputils
|
||||
|
||||
import (
|
||||
"io"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Reader interface {
|
||||
io.Reader
|
||||
io.ReaderAt
|
||||
io.ReadCloser
|
||||
}
|
||||
|
||||
type IRacingWinUtils struct {
|
||||
Utils *utils
|
||||
}
|
||||
|
||||
func Init() (*IRacingWinUtils, error) {
|
||||
u, err := newUtils()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &IRacingWinUtils{u}, nil
|
||||
}
|
||||
|
||||
func (u *IRacingWinUtils) Close() {
|
||||
u.Utils.Close()
|
||||
}
|
||||
|
||||
// OpenWinEvent will open the named windows event
|
||||
func (u *IRacingWinUtils) OpenWinEvent(name string) error {
|
||||
return u.Utils.OpenEvent(name)
|
||||
}
|
||||
|
||||
// OpenBroadcastChannel will open the broadcast channel
|
||||
func (u *IRacingWinUtils) OpenBroadcastChannel(name string) error {
|
||||
return u.Utils.OpenBroadcastChannel(name)
|
||||
}
|
||||
|
||||
// CheckValidDataEvent checks if our windows even is telling us we are good to go
|
||||
func (u *IRacingWinUtils) CheckValidDataEvent(timeout time.Duration) bool {
|
||||
return u.Utils.CheckValidDataEvent(timeout)
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
//go:build (linux && cgo) || (darwin && cgo)
|
||||
|
||||
package mmaputils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/ESilva15/goirsdk/sharedMem"
|
||||
)
|
||||
|
||||
type utils struct {
|
||||
socketPath string
|
||||
listener *net.UnixConn
|
||||
}
|
||||
|
||||
func newUtils() (*utils, error) {
|
||||
return &utils{}, nil
|
||||
}
|
||||
|
||||
func (u *utils) Close() {
|
||||
if u.listener != nil {
|
||||
u.listener.Close()
|
||||
os.Remove(u.socketPath)
|
||||
}
|
||||
}
|
||||
|
||||
// OpenMemMap returns a Reader interface that can be used to read the data
|
||||
// No need to encapsulate it
|
||||
func OpenMemMap(name string, size uint32) (Reader, error) {
|
||||
file, err := sharedMem.Open(name, size)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open `%s` with err: %+v", name, err)
|
||||
}
|
||||
|
||||
return file, nil
|
||||
}
|
||||
|
||||
// OpenEvent creates or connects to a Unix socket for event signaling on Linux
|
||||
func (u *utils) OpenEvent(eventName string) error {
|
||||
u.socketPath = fmt.Sprintf("/tmp/iracing_%s.sock", eventName)
|
||||
_ = os.Remove(u.socketPath)
|
||||
|
||||
addr, err := net.ResolveUnixAddr("unixgram", u.socketPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
l, err := net.ListenUnixgram("unixgram", addr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
u.listener = l
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *utils) OpenBroadcastChannel(name string) error {
|
||||
// No-op or log stub on Linux
|
||||
return nil
|
||||
}
|
||||
|
||||
// CheckValidDataEvent waits for a pulse byte sent over the socket
|
||||
func (u *utils) CheckValidDataEvent(timeout time.Duration) bool {
|
||||
if u.listener == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
_ = u.listener.SetReadDeadline(time.Now().Add(timeout))
|
||||
buf := make([]byte, 1)
|
||||
_, err := u.listener.Read(buf)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func (u *utils) SendBroadcastMessage(id, p1, p2 uintptr) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// package winutils
|
||||
//
|
||||
// import (
|
||||
// "errors"
|
||||
// "sync"
|
||||
// "time"
|
||||
// )
|
||||
//
|
||||
// const (
|
||||
// WAIT_OBJECT_0 = 0
|
||||
// WAIT_TIMEOUT = 258
|
||||
// )
|
||||
//
|
||||
// var (
|
||||
// once sync.Once
|
||||
// ErrUnsupportedOS = errors.New("not found")
|
||||
// )
|
||||
//
|
||||
// type utils struct {
|
||||
// }
|
||||
//
|
||||
// // INITIALIZATION
|
||||
// func newUtils() (*utils, error) {
|
||||
// return nil, ErrUnsupportedOS
|
||||
// }
|
||||
//
|
||||
// func (u *utils) Close() {
|
||||
// }
|
||||
//
|
||||
// // openEvent opens a windows.Handle for a given event
|
||||
// func (u *utils) OpenEvent(eventName string) error {
|
||||
// return ErrUnsupportedOS
|
||||
// }
|
||||
//
|
||||
// // OpenBroadcastChannel opens up a broadcast channel to send commands to iracing
|
||||
// func (u *utils) OpenBroadcastChannel(name string) error {
|
||||
// return ErrUnsupportedOS
|
||||
// }
|
||||
//
|
||||
// // INITIALIZATION
|
||||
//
|
||||
// // openEvent waits for a good response for some given time
|
||||
// func (u *utils) CheckValidDataEvent(timeout time.Duration) bool {
|
||||
// return false
|
||||
// }
|
||||
//
|
||||
// // SendBroadcastMessage sends a message trough the broadcast channel
|
||||
// func (u *utils) SendBroadcastMessage(id, p1, p2 uintptr) error {
|
||||
// return ErrUnsupportedOS
|
||||
// }
|
||||
+1
-5
@@ -6,8 +6,6 @@ import (
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/ESilva15/goirsdk/logger"
|
||||
|
||||
"golang.org/x/text/encoding/charmap"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
@@ -312,8 +310,6 @@ type Driver struct {
|
||||
|
||||
// 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 {
|
||||
@@ -337,7 +333,7 @@ func (i *IBT) readSessionInfo() error {
|
||||
if i.Opts.SessionInfoExport {
|
||||
err := i.exportYAML()
|
||||
if err != nil {
|
||||
log.Printf("Failed to export YAML string: %v\n", err)
|
||||
i.Opts.Logger.Debug("Failed to export YAML string", "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+40
-73
@@ -7,7 +7,6 @@ import (
|
||||
"io"
|
||||
"log"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -157,38 +156,6 @@ func (i *IBT) readVariablerHeaders() error {
|
||||
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 {
|
||||
for k, v := range i.Vars.Vars {
|
||||
// Slice of the variable value in the buffer
|
||||
@@ -249,18 +216,17 @@ func (i *IBT) readData(buf []byte) error {
|
||||
case IRSDK_bitField:
|
||||
if v.Count > 1 {
|
||||
// Array of data
|
||||
data := make([]string, v.Count)
|
||||
data := make([]uint32, 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
|
||||
data[entry] = binary.LittleEndian.Uint32(rbuf)
|
||||
}
|
||||
|
||||
v.Value = data
|
||||
} else {
|
||||
// Single value
|
||||
v.Value = fmt.Sprintf("0x%x", int(binary.LittleEndian.Uint32(rbuf)))
|
||||
v.Value = binary.LittleEndian.Uint32(rbuf)
|
||||
}
|
||||
case IRSDK_float:
|
||||
if v.Count > 1 {
|
||||
@@ -295,14 +261,10 @@ func (i *IBT) readData(buf []byte) error {
|
||||
v.Value = math.Float64frombits(uint64(binary.LittleEndian.Uint64(rbuf)))
|
||||
}
|
||||
}
|
||||
// --------------
|
||||
|
||||
i.Vars.Vars[k] = v
|
||||
}
|
||||
|
||||
// Parse the bitfield variables here
|
||||
i.parseBitfieldVariables()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -310,7 +272,8 @@ func (i *IBT) readData(buf []byte) error {
|
||||
// live and offline data
|
||||
func (i *IBT) Update(timeout time.Duration) (IRacingState, error) {
|
||||
// This is what happens if we are reading live data
|
||||
if i.winUtils != nil {
|
||||
switch i.Opts.SourceType {
|
||||
case SharedMemoryFile:
|
||||
// Put a way to check if the sim is active here
|
||||
// fmt.Println("NOT CHECKING IF SIM IS ACTIVE - ADD ME")
|
||||
|
||||
@@ -344,23 +307,29 @@ func (i *IBT) Update(timeout time.Duration) (IRacingState, error) {
|
||||
buf := make([]byte, i.Headers.BufLen)
|
||||
|
||||
_, err := i.File.ReadAt(buf, int64(start))
|
||||
if err == io.EOF {
|
||||
return Ended, nil
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return Failed, err
|
||||
}
|
||||
|
||||
if i.Opts.IBTExport {
|
||||
// Dirty attempt at getting this to work to write to a memory mapped file
|
||||
var offset int64 = 0
|
||||
switch i.Opts.IBTExportType {
|
||||
case IBTFile:
|
||||
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)
|
||||
}
|
||||
// i.Opts.Logger.Debug("Reading live data and exporting to IBT file")
|
||||
offset = int64(i.Headers.BufOffset + i.Vars.RecorderTick*i.Headers.BufLen)
|
||||
case SharedMemoryFile:
|
||||
err = i.exportIBT(buf, int64(i.Headers.BufOffset))
|
||||
if err != nil {
|
||||
log.Printf("Failed to export live telemetry data: %v", err)
|
||||
}
|
||||
// i.Opts.Logger.Debug("Reading live data and exporting to SHM file")
|
||||
offset = int64(i.Headers.BufOffset)
|
||||
}
|
||||
|
||||
err = i.exportIBT(buf, offset)
|
||||
if err != nil {
|
||||
i.Opts.Logger.Debug(fmt.Sprintf("Failed to export offline telemetry data: %+v", err))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -369,37 +338,15 @@ func (i *IBT) Update(timeout time.Duration) (IRacingState, error) {
|
||||
return Unknown, err
|
||||
}
|
||||
|
||||
if err == io.EOF {
|
||||
return Ended, nil
|
||||
}
|
||||
|
||||
// Document why this is here, I don't remember the exact words right now
|
||||
i.Vars.RecorderTick++
|
||||
} else {
|
||||
case IBTFile:
|
||||
// This is what happens if we are reading from an .ibt file
|
||||
// This will get the dataframe corresponding to a given tick
|
||||
start := i.Headers.BufOffset + i.Vars.Tick*i.Headers.BufLen
|
||||
buf := make([]byte, i.Headers.BufLen)
|
||||
_, err := i.File.ReadAt(buf, int64(start))
|
||||
|
||||
// Make this happen in a different thread, or have this send to a queue that has a thread
|
||||
// writing to a file
|
||||
if i.Opts.IBTExport {
|
||||
// Dirty attempt at getting this to work to write to a memory mapped file
|
||||
switch i.Opts.IBTExportType {
|
||||
case IBTFile:
|
||||
err = i.exportIBT(buf, int64(start))
|
||||
if err != nil {
|
||||
log.Printf("Failed to export offline telemetry data: %v", err)
|
||||
}
|
||||
case SharedMemoryFile:
|
||||
err = i.exportIBT(buf, int64(i.Headers.BufOffset))
|
||||
if err != nil {
|
||||
log.Printf("Failed to export live telemetry data: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err == io.EOF {
|
||||
return Ended, nil
|
||||
}
|
||||
@@ -407,6 +354,26 @@ func (i *IBT) Update(timeout time.Duration) (IRacingState, error) {
|
||||
return Unknown, err
|
||||
}
|
||||
|
||||
// Make this happen in a different thread, or have this send to a queue that has a thread
|
||||
// writing to a file
|
||||
if i.Opts.IBTExport {
|
||||
// Dirty attempt at getting this to work to write to a memory mapped file
|
||||
var offset int64 = 0
|
||||
switch i.Opts.IBTExportType {
|
||||
case IBTFile:
|
||||
// i.Opts.Logger.Debug("Reading IBT file and exporting to IBT file")
|
||||
offset = int64(start)
|
||||
case SharedMemoryFile:
|
||||
// i.Opts.Logger.Debug("Reading IBT file and exporting to SHM file")
|
||||
offset = int64(i.Headers.BufOffset)
|
||||
}
|
||||
|
||||
err = i.exportIBT(buf, offset)
|
||||
if err != nil {
|
||||
i.Opts.Logger.Debug(fmt.Sprintf("Failed to export offline telemetry data: %+v", err))
|
||||
}
|
||||
}
|
||||
|
||||
err = i.readData(buf)
|
||||
if err != nil && err != io.EOF {
|
||||
log.Fatalf("What happened?\n%v\n", err)
|
||||
|
||||
Reference in New Issue
Block a user