Compare commits

...
13 changed files with 168 additions and 300 deletions
+2
View File
@@ -1,2 +1,4 @@
*.bin *.bin
*.work* *.work*
*.pprof
*.log
+12 -7
View File
@@ -1,23 +1,28 @@
# BeamNGMockOG # TelemetryMockserver
Utility to both record (with the caveat that it doesn't record yet) and replay Utility to both record and replay telemetry data from multiple simulators.
binary data relating to the Outgauge struct of beamng.
This will help me develop some stuff I'm doing for `ESDI` and my peripherals This will help me develop some stuff I'm doing for `ESDI` and my peripherals
without having to be playing the game while doing it (fans are noisy). without having to be playing the game while doing it (fans are noisy).
## Related projects
- [ESDI](https://github.com/ESilva15/ESDI)
- [iRacing Go SDK](https://github.com/ESilva15/goirsdk)
- [BeamNG Go SDK](https://github.com/ESilva15/gobngsdk)
# Installation # Installation
`go install github.com/ESilva15/BeamNGMockOg@latest` `go install github.com/ESilva15/TelemetryMockserver@latest`
# Usage # Usage
To start replaying: To start replaying (BeamNG as an example):
`telemetrymockserver beamng replay [--loop] -a 127.0.0.1 -p 4443 -i sunburstManual.bin` `telemetrymockserver beamng replay [--loop] -a 127.0.0.1 -p 4443 -i sunburstManual.bin`
- `loop` allows the replay functionality to keep replaying the same data file - `loop` allows the replay functionality to keep replaying the same data file
To start recording: To start recording (BeamNG as an example):
`BeamNGMockOg beamng record -a 127.0.0.1 -p 4443 -o sunburstDCT.bin` `TelemetryMockserver beamng record -a 127.0.0.1 -p 4443 -o sunburstDCT.bin`
## Development ## Development
Use `tcpdump` to listen to the socket and check if data is coming through: Use `tcpdump` to listen to the socket and check if data is coming through:
+6 -3
View File
@@ -3,6 +3,7 @@ package cmd
import ( import (
"context" "context"
"fmt" "fmt"
"log/slog"
beamng "github.com/ESilva15/TelemetryMockserver/internal/mockservers/beamng" beamng "github.com/ESilva15/TelemetryMockserver/internal/mockservers/beamng"
@@ -59,14 +60,16 @@ func replayAction(cmd *cobra.Command, args []string) {
replayer, err := beamng.NewReplayer(address, port, inputFile) replayer, err := beamng.NewReplayer(address, port, inputFile)
if err != nil { if err != nil {
fmt.Printf("Something went wrong setting up the player: %+v", err) slog.Error("Something went wrong setting up the player", "err", err)
return return
} }
// NOTE: is this doing anything at all?? // NOTE: is this doing anything at all??
ctx := context.Background() ctx := context.Background()
if err := replayer.Replay(ctx, loop); err != nil { err = replayer.Replay(ctx, loop)
fmt.Printf("Something went wrong while playing the file: %v", err) if err != nil {
slog.Error("Something went wrong while playing the file", "err", err)
return
} }
} }
+3
View File
@@ -0,0 +1,3 @@
package constants
const ProgramName = "TelemetryMockserver"
+5 -2
View File
@@ -3,8 +3,8 @@ module github.com/ESilva15/TelemetryMockserver
go 1.23.2 go 1.23.2
require ( require (
github.com/ESilva15/gobngsdk v1.1.3 github.com/ESilva15/gobngsdk v1.2.0
github.com/ESilva15/goirsdk v0.3.0 github.com/ESilva15/goirsdk v0.3.8
github.com/spf13/cobra v1.10.1 github.com/spf13/cobra v1.10.1
) )
@@ -15,3 +15,6 @@ require (
golang.org/x/text v0.19.0 // indirect golang.org/x/text v0.19.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
) )
//
// replace github.com/ESilva15/goirsdk => ../pkg/goirsdk/
// replace github.com/ESilva15/gobngsdk => ../pkg/gobngsdk/
+4 -4
View File
@@ -1,7 +1,7 @@
github.com/ESilva15/gobngsdk v1.1.3 h1:CFaz2KjHKnBLrQuEN1QHOd1761cWM/rmTwLpSLrgbe4= github.com/ESilva15/gobngsdk v1.2.0 h1:jX2ByFaIHnjCdPmrj1JiqCoNRLyu4vqIXC7++EUOCK4=
github.com/ESilva15/gobngsdk v1.1.3/go.mod h1:cKLaZRgM0tGGXDvosaSjHnxeyC5Y6rg7zCLqEUJnOzw= github.com/ESilva15/gobngsdk v1.2.0/go.mod h1:cKLaZRgM0tGGXDvosaSjHnxeyC5Y6rg7zCLqEUJnOzw=
github.com/ESilva15/goirsdk v0.3.0 h1:6D95Avq7chikGHwEUKh7Y/HaLHOWQ75Qxaz8oMDSfrw= github.com/ESilva15/goirsdk v0.3.8 h1:Eiabg4TqMkJQCImLEqwN93/BAuafbYyTrNXxg/Ck9bQ=
github.com/ESilva15/goirsdk v0.3.0/go.mod h1:5borQbw+L4fe9b58JFHRHZwrzz0sMVAteuYbRnFOc0Q= github.com/ESilva15/goirsdk v0.3.8/go.mod h1:5borQbw+L4fe9b58JFHRHZwrzz0sMVAteuYbRnFOc0Q=
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
-45
View File
@@ -1,45 +0,0 @@
package mockserver
import (
"io"
"unsafe"
sdk "github.com/ESilva15/gobngsdk"
)
type GobReader struct {
TotalRead int64
File io.ReadSeeker
Buf []byte
}
func NewGobReader(r io.ReadSeeker) *GobReader {
return &GobReader{
TotalRead: 0,
File: r,
Buf: make([]byte, unsafe.Sizeof(sdk.Outgauge{})),
}
}
func (g *GobReader) Reset() error {
_, err := g.File.Seek(0, io.SeekStart)
if err != nil {
return err
}
g.TotalRead = 0
return nil
}
func (g *GobReader) Next(buffer []byte) error {
_, err := io.ReadFull(g.File, buffer)
if err != nil {
return err
}
pos, _ := g.File.Seek(0, io.SeekCurrent)
g.TotalRead = pos
return nil
}
+22 -54
View File
@@ -3,82 +3,59 @@ package mockserver
import ( import (
"bytes" "bytes"
"context" "context"
"encoding/binary"
"fmt" "fmt"
"log/slog"
"os" "os"
"sync" "sync"
"time" "time"
"github.com/ESilva15/TelemetryMockserver/constants"
bngsdk "github.com/ESilva15/gobngsdk" bngsdk "github.com/ESilva15/gobngsdk"
) )
type recorderViewData struct { type recorderViewData struct {
TotalBytes int TotalBytes int64
SDK *bngsdk.BeamNGSDK Og bngsdk.Outgauge
} }
type Recorder struct { type Recorder struct {
SDK bngsdk.BeamNGSDK SDK *bngsdk.BeamNGSDK
OutputFile *os.File
TotalBytes int
// Views // Views
mut sync.RWMutex mut sync.RWMutex
viewDataMut sync.RWMutex viewDataMut sync.RWMutex
viewData recorderViewData viewData recorderViewData
viewCh chan *recorderViewData viewCh chan *recorderViewData
recorderCh chan []byte
} }
func NewRecorder(fp string, address string, port int) (*Recorder, error) { func NewRecorder(fp string, address string, port int) (*Recorder, error) {
var recorder Recorder var recorder Recorder
var err error var err error
recorder.SDK, err = bngsdk.Init(address, port) recorder.SDK, err = bngsdk.NewBngSDK(bngsdk.Options{
Logger: slog.Default().With("SDK", "BeamNG"),
SourceType: bngsdk.UDPData,
ImportUDPAddress: address,
ImportUDPPort: port,
ExportData: true,
ExportDataType: bngsdk.BinaryFile,
ExportDataPath: fp,
})
if err != nil { if err != nil {
return &Recorder{}, err return nil, err
}
recorder.OutputFile, err = os.Create(fp)
if err != nil {
return &Recorder{}, err
} }
recorder.viewData = recorderViewData{} recorder.viewData = recorderViewData{}
recorder.viewCh = make(chan *recorderViewData, 1) recorder.viewCh = make(chan *recorderViewData, 1)
recorder.recorderCh = make(chan []byte, 1)
return &recorder, nil return &recorder, nil
} }
func (r *Recorder) Close() { func (r *Recorder) Close() {
r.SDK.Close() r.SDK.Close()
if r.OutputFile != nil {
r.OutputFile.Close()
}
}
func (r *Recorder) record(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
case data := <-r.recorderCh:
r.mut.Lock()
err := binary.Write(r.OutputFile, binary.LittleEndian, r.SDK.Data)
r.TotalBytes += len(data)
r.mut.Unlock()
if err != nil {
// NOTE: find a way of logging this somehow
}
}
}
} }
func (r *Recorder) view(ctx context.Context) { func (r *Recorder) view(ctx context.Context) {
var buf bytes.Buffer var buf bytes.Buffer
var nBytes int
buf.Grow(2048) buf.Grow(2048)
@@ -89,16 +66,15 @@ func (r *Recorder) view(ctx context.Context) {
case viewData := <-r.viewCh: case viewData := <-r.viewCh:
buf.Reset() buf.Reset()
buf.WriteString("\x1b[2J\x1b[H") buf.WriteString("\x1b[2J\x1b[H")
fmt.Fprintf(&buf, "\x1b]0;%s - Recording ", ProgramName) fmt.Fprintf(&buf, "\x1b]0;%s - Recording ", constants.ProgramName)
stringifyRecordingProgress(&buf, nBytes) stringifyRecordingProgress(&buf, viewData.TotalBytes)
fmt.Fprintf(&buf, "\x07") fmt.Fprintf(&buf, "\x07")
stringifyRecordingProgress(&buf, nBytes) stringifyRecordingProgress(&buf, viewData.TotalBytes)
fmt.Fprintf(&buf, "\n\n") fmt.Fprintf(&buf, "\n\n")
r.viewDataMut.RLock() r.viewDataMut.RLock()
nBytes = viewData.TotalBytes stringifyOutgaugeData(&buf, &viewData.Og)
stringifyOutgaugeData(&buf, &r.SDK)
r.viewDataMut.RUnlock() r.viewDataMut.RUnlock()
_, _ = buf.WriteTo(os.Stdout) _, _ = buf.WriteTo(os.Stdout)
@@ -111,7 +87,6 @@ func (r *Recorder) Record(ctx context.Context) error {
ticker := time.NewTicker(time.Second / 60) ticker := time.NewTicker(time.Second / 60)
defer ticker.Stop() defer ticker.Stop()
go r.record(ctx)
go r.view(ctx) go r.view(ctx)
for { for {
@@ -119,12 +94,13 @@ func (r *Recorder) Record(ctx context.Context) error {
case <-ctx.Done(): case <-ctx.Done():
return nil return nil
case <-ticker.C: case <-ticker.C:
err := r.SDK.ReadData() og, err := r.SDK.Update()
if err != nil { if err != nil {
return err return err
} }
r.viewData.TotalBytes = r.TotalBytes r.viewData.TotalBytes = r.SDK.GetTotalWritten()
r.viewData.Og = *og
// Send the data to the view // Send the data to the view
select { select {
@@ -133,14 +109,6 @@ func (r *Recorder) Record(ctx context.Context) error {
default: default:
// Dropped the frame! // Dropped the frame!
} }
// Write the data to the file
select {
case r.recorderCh <- r.SDK.Buffer:
// Sent the data
default:
// Dropped the frame!
}
} }
} }
} }
+31 -95
View File
@@ -5,32 +5,30 @@ package mockserver
import ( import (
"bytes" "bytes"
"context" "context"
"encoding/binary"
"fmt" "fmt"
"io" "log/slog"
"math"
"os" "os"
"sync" "sync"
"time" "time"
"unsafe"
"github.com/ESilva15/TelemetryMockserver/constants"
bngsdk "github.com/ESilva15/gobngsdk" bngsdk "github.com/ESilva15/gobngsdk"
) )
type ViewData struct { type ViewData struct {
SDK *bngsdk.BeamNGSDK Outgauge bngsdk.Outgauge
SizeRead int64 SizeRead int64
FileSize int64
} }
// Replayer does the replaying // Replayer does the replaying
// Should we make a "player" struct that can record and replay? // Should we make a "player" struct that can record and replay?
type Replayer struct { type Replayer struct {
SDK bngsdk.BeamNGSDK SDK *bngsdk.BeamNGSDK
DataSourcePath string
Socket *UDPTransport
// Streams // Streams
dataViewCh chan ViewData dataViewCh chan ViewData
socketCh chan []byte
// Mut // Mut
mut sync.RWMutex mut sync.RWMutex
@@ -40,21 +38,23 @@ type Replayer struct {
} }
func NewReplayer(address string, port int, fp string) (*Replayer, error) { func NewReplayer(address string, port int, fp string) (*Replayer, error) {
udp, err := NewUDPTransport(address, port) sdk, err := bngsdk.NewBngSDK(bngsdk.Options{
Logger: slog.Default().With("SDK", "BeamNG"),
SourceType: bngsdk.BinaryFile,
BinSourcePath: fp,
ExportData: true,
ExportDataType: bngsdk.UDPData,
ExportUDPAddress: address,
ExportUDPPort: port,
Loop: true,
})
if err != nil { if err != nil {
return nil, err return nil, err
} }
replayer := &Replayer{ replayer := &Replayer{
DataSourcePath: fp, SDK: sdk,
SDK: bngsdk.BeamNGSDK{
Data: bngsdk.Outgauge{},
Buffer: make([]byte, unsafe.Sizeof(bngsdk.Outgauge{})),
},
Socket: udp,
viewData: ViewData{},
dataViewCh: make(chan ViewData, 1), dataViewCh: make(chan ViewData, 1),
socketCh: make(chan []byte, 1),
} }
return replayer, nil return replayer, nil
@@ -62,15 +62,7 @@ func NewReplayer(address string, port int, fp string) (*Replayer, error) {
// renderToTerminal will render the data for the users viewing pleasure // renderToTerminal will render the data for the users viewing pleasure
func (r *Replayer) renderToTerminal(ctx context.Context) { func (r *Replayer) renderToTerminal(ctx context.Context) {
fileInfo, err := os.Stat(r.DataSourcePath)
if err != nil {
// NOTE: learn how to handle this error
// return fmt.Errorf("error stating file: %v", err)
}
var buf bytes.Buffer var buf bytes.Buffer
var bytesReader bytes.Reader
buf.Grow(2048) buf.Grow(2048)
for { for {
@@ -79,60 +71,25 @@ func (r *Replayer) renderToTerminal(ctx context.Context) {
return return
case data := <-r.dataViewCh: case data := <-r.dataViewCh:
// Reset to the start of the terminal // Reset to the start of the terminal
percent := int(float64(data.SizeRead) / float64(fileInfo.Size()) * 100) // percent := int(float64(data.SizeRead) / float64(data.FileSize) * 100)
percent := int(math.Round((float64(data.SizeRead) / float64(data.FileSize)) * 100))
buf.Reset() buf.Reset()
buf.WriteString("\x1b[2J\x1b[H") buf.WriteString("\x1b[2J\x1b[H")
fmt.Fprintf(&buf, "\x1b]0;%s - Replaying %d%%\x07", ProgramName, percent) fmt.Fprintf(&buf, "\x1b]0;%s - Replaying %d%%\x07", constants.ProgramName, percent)
bytesReader.Reset(data.SDK.Buffer)
err := binary.Read(&bytesReader, binary.LittleEndian, &r.SDK.Data)
if err != nil {
fmt.Fprintf(&buf, "FAILED TO PARSE DATA\nError: %+v", err)
_, _ = buf.WriteTo(os.Stdout)
continue
}
fmt.Fprintf(&buf, "Replayed: %d%%\n", percent) fmt.Fprintf(&buf, "Replayed: %d%%\n", percent)
stringifyOutgaugeData(&buf, data.SDK) stringifyOutgaugeData(&buf, &data.Outgauge)
buf.WriteTo(os.Stdout) buf.WriteTo(os.Stdout)
} }
} }
} }
// writeToUDPSocket will write the telemetry data to the UDP socket
func (r *Replayer) writeToUDPSocket(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
case data := <-r.socketCh:
r.mut.RLock()
_, err := r.Socket.Send(data)
r.mut.RUnlock()
if err != nil {
panic(fmt.Sprintf("error writing buffer to socket: %+v", err))
continue
// NOTE: log the error somewhere maybe
// return err
}
}
}
}
// Replay replays a given file <fp> in a UDP server <addr>:<port> // Replay replays a given file <fp> in a UDP server <addr>:<port>
func (r *Replayer) Replay(ctx context.Context, loop bool) error { func (r *Replayer) Replay(ctx context.Context, loop bool) error {
bin, err := os.Open(r.DataSourcePath)
if err != nil {
return fmt.Errorf("error opening file: %v", err)
}
reader := NewGobReader(bin)
go r.renderToTerminal(ctx) go r.renderToTerminal(ctx)
go r.writeToUDPSocket(ctx)
ticker := time.NewTicker(time.Second / 60) ticker := time.NewTicker(time.Second / 60)
defer ticker.Stop() defer ticker.Stop()
@@ -142,31 +99,19 @@ func (r *Replayer) Replay(ctx context.Context, loop bool) error {
return ctx.Err() return ctx.Err()
case <-ticker.C: case <-ticker.C:
og, err := r.SDK.Update()
if err != nil {
// TODO: log here
slog.Error("an error occurred when updating", "err", err)
return err
}
r.mut.Lock() r.mut.Lock()
err := reader.Next(r.SDK.Buffer) r.viewData.SizeRead = r.SDK.GetTotalRead()
r.viewData.FileSize = r.SDK.GetSourceSize()
r.viewData.Outgauge = *og
r.mut.Unlock() r.mut.Unlock()
if err == io.EOF {
if !loop {
return r.Socket.Close()
}
err = reader.Reset()
if err != nil {
return err
}
continue
}
if err != nil {
return err
}
// NOTE: Really like this???
r.viewData.SDK = &r.SDK
r.viewData.SizeRead = reader.TotalRead
// Send the data to the view // Send the data to the view
select { select {
case r.dataViewCh <- r.viewData: case r.dataViewCh <- r.viewData:
@@ -174,15 +119,6 @@ func (r *Replayer) Replay(ctx context.Context, loop bool) error {
default: default:
// Dropped the frame! // Dropped the frame!
} }
// Send the data to the UDP socket
select {
case r.socketCh <- r.SDK.Buffer:
// Sent the data
default:
// Dropped the frame!
}
} }
} }
} }
-41
View File
@@ -1,41 +0,0 @@
package mockserver
import (
"fmt"
"net"
)
const ProgramName = "TelemetryMockerserver"
type Transport interface {
Send(data []byte) error
Close() error
}
type UDPTransport struct {
Conn *net.UDPConn
Addr *net.UDPAddr
}
func NewUDPTransport(address string, port int) (*UDPTransport, error) {
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", address, port))
if err != nil {
return nil, err
}
conn, err := net.ListenUDP("udp", nil)
if err != nil {
return nil, err
}
return &UDPTransport{Conn: conn, Addr: addr}, nil
}
// Send will send a byte array of data trough the UDP server
func (u *UDPTransport) Send(data []byte) (int, error) {
return u.Conn.WriteToUDP(data, u.Addr)
}
func (u *UDPTransport) Close() error {
return u.Conn.Close()
}
+49 -49
View File
@@ -14,7 +14,7 @@ const (
GiB GiB
) )
func stringifyRecordingProgress(s *bytes.Buffer, nBytes int) { func stringifyRecordingProgress(s *bytes.Buffer, nBytes int64) {
if nBytes < KiB { if nBytes < KiB {
fmt.Fprintf(s, "%d B", nBytes) fmt.Fprintf(s, "%d B", nBytes)
} else if nBytes < MiB { } else if nBytes < MiB {
@@ -26,64 +26,64 @@ func stringifyRecordingProgress(s *bytes.Buffer, nBytes int) {
} }
} }
func stringifyOutgaugeData(s *bytes.Buffer, sdk *bngsdk.BeamNGSDK) { func stringifyOutgaugeData(s *bytes.Buffer, og *bngsdk.Outgauge) {
// NOTE: write a string serialization function on the SDK itself // NOTE: write a string serialization function on the SDK itself
fmt.Fprint(s, "Outgauge {\n") fmt.Fprint(s, "Outgauge {\n")
fmt.Fprintf(s, " Time: %d ms\n", sdk.Data.Time) fmt.Fprintf(s, " Time: %d ms\n", og.Time)
fmt.Fprintf(s, " Car: %s\n", sdk.Data.Car) fmt.Fprintf(s, " Car: %s\n", og.Car)
fmt.Fprintf(s, " Flags: %b\n", sdk.Data.Flags) fmt.Fprintf(s, " Flags: %b\n", og.Flags)
fmt.Fprintf(s, " Gear: %d\n", sdk.Data.Gear) fmt.Fprintf(s, " Gear: %d\n", og.Gear)
fmt.Fprintf(s, " Plid: %d\n", sdk.Data.Plid) fmt.Fprintf(s, " Plid: %d\n", og.Plid)
fmt.Fprintf(s, " Speed: %f m/s\n", sdk.Data.Speed) fmt.Fprintf(s, " Speed: %f m/s\n", og.Speed)
fmt.Fprintf(s, " RPM: %f RPM\n", sdk.Data.RPM) fmt.Fprintf(s, " RPM: %f RPM\n", og.RPM)
fmt.Fprintf(s, " Turbo: %f Bar\n", sdk.Data.Turbo) fmt.Fprintf(s, " Turbo: %f Bar\n", og.Turbo)
fmt.Fprintf(s, " EngTemp: %f °C\n", sdk.Data.EngTemp) fmt.Fprintf(s, " EngTemp: %f °C\n", og.EngTemp)
fmt.Fprintf(s, " Fuel: %f\n", sdk.Data.Fuel) fmt.Fprintf(s, " Fuel: %f\n", og.Fuel)
fmt.Fprintf(s, " OilPressure: %f Bar\n", sdk.Data.OilPressure) fmt.Fprintf(s, " OilPressure: %f Bar\n", og.OilPressure)
fmt.Fprintf(s, " OilTemp: %f °C\n", sdk.Data.OilTemp) fmt.Fprintf(s, " OilTemp: %f °C\n", og.OilTemp)
fmt.Fprintf(s, " DashLights: %b\n", sdk.Data.DashLights) fmt.Fprintf(s, " DashLights: %b\n", og.DashLights)
fmt.Fprintf(s, " ShowLights: %b\n", sdk.Data.ShowLights) fmt.Fprintf(s, " ShowLights: %b\n", og.ShowLights)
fmt.Fprintf(s, " Throttle: %f\n", sdk.Data.Throttle) fmt.Fprintf(s, " Throttle: %f\n", og.Throttle)
fmt.Fprintf(s, " Brakes: %f\n", sdk.Data.Brake) fmt.Fprintf(s, " Brakes: %f\n", og.Brake)
fmt.Fprintf(s, " Clutch: %f\n", sdk.Data.Clutch) fmt.Fprintf(s, " Clutch: %f\n", og.Clutch)
fmt.Fprintf(s, " Display1: %s\n", sdk.Data.Display1) fmt.Fprintf(s, " Display1: %s\n", og.Display1)
fmt.Fprintf(s, " Display2: %s\n", sdk.Data.Display2) fmt.Fprintf(s, " Display2: %s\n", og.Display2)
fmt.Fprintf(s, " ID: %d\n", sdk.Data.ID) fmt.Fprintf(s, " ID: %d\n", og.ID)
fmt.Fprint(s, "}\n\n") fmt.Fprint(s, "}\n\n")
fmt.Fprint(s, "DashLights {\n") fmt.Fprint(s, "DashLights {\n")
fmt.Fprintf(s, " DL_SHIFT: %t\n", sdk.HasShiftLight()) fmt.Fprintf(s, " DL_SHIFT: %t\n", og.HasShiftLight())
fmt.Fprintf(s, " DL_FULLBEAM: %t\n", sdk.HasHighBeamLight()) fmt.Fprintf(s, " DL_FULLBEAM: %t\n", og.HasHighBeamLight())
fmt.Fprintf(s, " DL_HANDBRAKE: %t\n", sdk.HasHandbrakeLight()) fmt.Fprintf(s, " DL_HANDBRAKE: %t\n", og.HasHandbrakeLight())
fmt.Fprintf(s, " DL_PITSPEED: %t\n", sdk.HasPitspeed()) fmt.Fprintf(s, " DL_PITSPEED: %t\n", og.HasPitspeed())
fmt.Fprintf(s, " DL_TC: %t\n", sdk.HasTractionControlLight()) fmt.Fprintf(s, " DL_TC: %t\n", og.HasTractionControlLight())
fmt.Fprintf(s, " DL_SIGNAL_L: %t\n", sdk.HasLeftIndicatorLight()) fmt.Fprintf(s, " DL_SIGNAL_L: %t\n", og.HasLeftIndicatorLight())
fmt.Fprintf(s, " DL_SIGNAL_R: %t\n", sdk.HasRightIndicatorLight()) fmt.Fprintf(s, " DL_SIGNAL_R: %t\n", og.HasRightIndicatorLight())
fmt.Fprintf(s, " DL_SIGNAL_ANY: %t\n", sdk.HasAnyIndicatorLight()) fmt.Fprintf(s, " DL_SIGNAL_ANY: %t\n", og.HasAnyIndicatorLight())
fmt.Fprintf(s, " DL_OILWARN: %t\n", sdk.HasOilLight()) fmt.Fprintf(s, " DL_OILWARN: %t\n", og.HasOilLight())
fmt.Fprintf(s, " DL_BATTERY: %t\n", sdk.HasBatteryLight()) fmt.Fprintf(s, " DL_BATTERY: %t\n", og.HasBatteryLight())
fmt.Fprintf(s, " DL_ABS: %t\n", sdk.HasABSLight()) fmt.Fprintf(s, " DL_ABS: %t\n", og.HasABSLight())
fmt.Fprintf(s, " DL_SPARE: %t\n", sdk.Data.DashLights&bngsdk.DL_SPARE != 0) fmt.Fprintf(s, " DL_SPARE: %t\n", og.HasSpare())
fmt.Fprint(s, "}\n\n") fmt.Fprint(s, "}\n\n")
fmt.Fprint(s, "ShowLights {\n") // Fixed typo "ShowLigths" fmt.Fprint(s, "ShowLights {\n") // Fixed typo "ShowLigths"
fmt.Fprintf(s, " DL_SHIFT: %t\n", sdk.ShiftLight()) fmt.Fprintf(s, " DL_SHIFT: %t\n", og.ShiftLight())
fmt.Fprintf(s, " DL_FULLBEAM: %t\n", sdk.HighBeam()) fmt.Fprintf(s, " DL_FULLBEAM: %t\n", og.HighBeam())
fmt.Fprintf(s, " DL_HANDBRAKE: %t\n", sdk.Handbrake()) fmt.Fprintf(s, " DL_HANDBRAKE: %t\n", og.Handbrake())
fmt.Fprintf(s, " DL_PITSPEED: %t\n", sdk.Pitspeed()) fmt.Fprintf(s, " DL_PITSPEED: %t\n", og.Pitspeed())
fmt.Fprintf(s, " DL_TC: %t\n", sdk.TractionControl()) fmt.Fprintf(s, " DL_TC: %t\n", og.TractionControl())
fmt.Fprintf(s, " DL_SIGNAL_L: %t\n", sdk.LeftIndicator()) fmt.Fprintf(s, " DL_SIGNAL_L: %t\n", og.LeftIndicator())
fmt.Fprintf(s, " DL_SIGNAL_R: %t\n", sdk.RightIndicator()) fmt.Fprintf(s, " DL_SIGNAL_R: %t\n", og.RightIndicator())
fmt.Fprintf(s, " DL_SIGNAL_ANY: %t\n", sdk.AnyIndicator()) fmt.Fprintf(s, " DL_SIGNAL_ANY: %t\n", og.AnyIndicator())
fmt.Fprintf(s, " DL_OILWARN: %t\n", sdk.OilLight()) fmt.Fprintf(s, " DL_OILWARN: %t\n", og.OilLight())
fmt.Fprintf(s, " DL_BATTERY: %t\n", sdk.BatteryLight()) fmt.Fprintf(s, " DL_BATTERY: %t\n", og.BatteryLight())
fmt.Fprintf(s, " DL_ABS: %t\n", sdk.ABS()) fmt.Fprintf(s, " DL_ABS: %t\n", og.ABS())
fmt.Fprintf(s, " DL_SPARE: %t\n", sdk.Data.ShowLights&bngsdk.DL_SPARE != 0) fmt.Fprintf(s, " DL_SPARE: %t\n", og.Spare())
fmt.Fprint(s, "}\n\n") fmt.Fprint(s, "}\n\n")
fmt.Fprint(s, "Flags {\n") fmt.Fprint(s, "Flags {\n")
fmt.Fprintf(s, " OG_TURBO (Has Turbo): %t\n", sdk.HasTurbo()) fmt.Fprintf(s, " OG_TURBO (Has Turbo): %t\n", og.HasTurbo())
fmt.Fprintf(s, " OG_KM (Is Metric): %t\n", sdk.PrefersKm()) fmt.Fprintf(s, " OG_KM (Is Metric): %t\n", og.PrefersKm())
fmt.Fprintf(s, " OG_BAR (Pressure): %t\n", sdk.PrefersBAR()) fmt.Fprintf(s, " OG_BAR (Pressure): %t\n", og.PrefersBAR())
fmt.Fprint(s, "}") fmt.Fprint(s, "}")
} }
+9 -1
View File
@@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"log" "log"
"log/slog"
"sync" "sync"
"time" "time"
@@ -31,6 +32,7 @@ type Replayer struct {
func NewReplayer(input string, output string) (*Replayer, error) { func NewReplayer(input string, output string) (*Replayer, error) {
ibt, err := goirsdk.Init(goirsdk.Options{ ibt, err := goirsdk.Init(goirsdk.Options{
Logger: slog.Default(),
SourceType: goirsdk.IBTFile, SourceType: goirsdk.IBTFile,
SourcePath: "../testTelemetry/gt3_mustang_bathurst.ibt", SourcePath: "../testTelemetry/gt3_mustang_bathurst.ibt",
IBTExportType: goirsdk.SharedMemoryFile, IBTExportType: goirsdk.SharedMemoryFile,
@@ -77,9 +79,15 @@ func (r *Replayer) Replay(ctx context.Context, loop bool) error {
gear := int32(r.SDK.Vars.Vars["Gear"].Value.(int)) gear := int32(r.SDK.Vars.Vars["Gear"].Value.(int))
rpm := int32(r.SDK.Vars.Vars["RPM"].Value.(float32)) rpm := int32(r.SDK.Vars.Vars["RPM"].Value.(float32))
speed := int32(r.SDK.Vars.Vars["Speed"].Value.(float32)) speed := int32(r.SDK.Vars.Vars["Speed"].Value.(float32))
sessionState := r.SDK.Vars.Vars["SessionState"].Value.(int)
fmt.Printf("\033[?25l\033[2J\033[H") 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("\n")
fmt.Printf("IsConnected: %t\n", r.SDK.IsConnected())
fmt.Printf(" SessionStatusConnected: %t\n", r.SDK.SessionStatusConnected())
fmt.Printf(" SessionStatusInvalid: %t\n", r.SDK.SessionStateInvalid())
fmt.Printf(" SessionState: %d\n", sessionState)
<-mainLoopTicker.C <-mainLoopTicker.C
} }
+27 -1
View File
@@ -1,6 +1,32 @@
package main package main
import "github.com/ESilva15/TelemetryMockserver/cmd" import (
"log/slog"
"os"
"github.com/ESilva15/TelemetryMockserver/cmd"
)
func init() {
_ = setupLogger()
}
func setupLogger() error {
output, err := os.OpenFile("./output.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o755)
if err != nil {
return err
}
logger := slog.New(
slog.NewTextHandler(output, &slog.HandlerOptions{
Level: slog.LevelDebug,
}),
)
slog.SetDefault(logger)
return nil
}
func main() { func main() {
cmd.Execute() cmd.Execute()