Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
31a2f85407 | ||
|
|
b8d915af4f | ||
|
|
99b3891c60 | ||
|
|
988124ef60 |
+10
-1
@@ -1,6 +1,7 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/ESilva15/BeamNGMockOg/mockserver"
|
||||
@@ -13,7 +14,15 @@ func recordAction(cmd *cobra.Command, args []string) {
|
||||
address, _ := cmd.Flags().GetString("address")
|
||||
port, _ := cmd.Flags().GetInt("port")
|
||||
|
||||
if err := mockserver.Record(address, port, outputFile); err != nil {
|
||||
recorder, err := mockserver.NewRecorder(outputFile, address, port)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("failed to create new BeamNG recorder: %+v", err))
|
||||
}
|
||||
defer recorder.Close()
|
||||
|
||||
// NOTE: is this doing anything at all??
|
||||
ctx := context.Background()
|
||||
if err := recorder.Record(ctx); err != nil {
|
||||
fmt.Printf("Something went wrong while recording the file: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ func replayAction(cmd *cobra.Command, args []string) {
|
||||
return
|
||||
}
|
||||
|
||||
// NOTE: is this doing anything at all??
|
||||
ctx := context.Background()
|
||||
if err := replayer.Replay(ctx, loop); err != nil {
|
||||
fmt.Printf("Something went wrong while playing the file: %v", err)
|
||||
|
||||
+133
-26
@@ -1,47 +1,154 @@
|
||||
package mockserver
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"log"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
bngsdk "github.com/ESilva15/gobngsdk"
|
||||
)
|
||||
|
||||
// NOTE: add some visual feedback of whats happening.
|
||||
// Maybe reuse the replay view function
|
||||
type recorderViewData struct {
|
||||
TotalBytes int
|
||||
Data []byte
|
||||
}
|
||||
|
||||
type Recorder struct {
|
||||
SDK bngsdk.BeamNGSDK
|
||||
OutputFile *os.File
|
||||
TotalBytes int
|
||||
// Views
|
||||
mut sync.RWMutex
|
||||
viewDataMut sync.RWMutex
|
||||
viewData recorderViewData
|
||||
viewCh chan *recorderViewData
|
||||
recorderCh chan []byte
|
||||
}
|
||||
|
||||
func NewRecorder(fp string, address string, port int) (*Recorder, error) {
|
||||
var recorder Recorder
|
||||
var err error
|
||||
|
||||
recorder.SDK, err = bngsdk.Init(address, port)
|
||||
if err != nil {
|
||||
return &Recorder{}, err
|
||||
}
|
||||
|
||||
recorder.OutputFile, err = os.Create(fp)
|
||||
if err != nil {
|
||||
return &Recorder{}, err
|
||||
}
|
||||
|
||||
recorder.viewData = recorderViewData{}
|
||||
recorder.viewCh = make(chan *recorderViewData, 1)
|
||||
recorder.recorderCh = make(chan []byte, 1)
|
||||
|
||||
return &recorder, nil
|
||||
}
|
||||
|
||||
func (r *Recorder) 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.RLock()
|
||||
err := binary.Write(r.OutputFile, binary.LittleEndian, data)
|
||||
r.TotalBytes += len(data)
|
||||
r.mut.RUnlock()
|
||||
|
||||
if err != nil {
|
||||
// NOTE: find a way of logging this somehow
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Recorder) view(ctx context.Context) {
|
||||
var s strings.Builder
|
||||
var nBytes int
|
||||
var bytesReader bytes.Reader
|
||||
og := bngsdk.Outgauge{}
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case viewData := <-r.viewCh:
|
||||
s.Reset()
|
||||
fmt.Fprintf(os.Stdout, "\x1b[2J\x1b[H")
|
||||
|
||||
r.viewDataMut.RLock()
|
||||
nBytes = viewData.TotalBytes
|
||||
r.viewDataMut.RUnlock()
|
||||
|
||||
stringifyRecordingProgress(&s, nBytes)
|
||||
fmt.Fprintf(&s, "\n\n")
|
||||
|
||||
bytesReader.Reset(viewData.Data)
|
||||
err := binary.Read(&bytesReader, binary.LittleEndian, &og)
|
||||
if err != nil {
|
||||
fmt.Fprintf(&s, "\n\nFAILED TO PARSE DATA\nError: %+v\n\n", err)
|
||||
fmt.Fprint(os.Stdout, s.String())
|
||||
continue
|
||||
}
|
||||
|
||||
stringifyOutgaugeData(&s, &og)
|
||||
|
||||
fmt.Fprint(os.Stdout, s.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Record records data from the UDP connection created by address and port
|
||||
func Record(address string, port int, filePath string) error {
|
||||
func (r *Recorder) Record(ctx context.Context) error {
|
||||
ticker := time.NewTicker(time.Second / 60)
|
||||
defer ticker.Stop()
|
||||
|
||||
// Create the output file
|
||||
bin, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer bin.Close()
|
||||
|
||||
// Create the BeamNGSDK instance
|
||||
beam, err := bngsdk.Init(address, port)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer beam.Close()
|
||||
go r.record(ctx)
|
||||
go r.view(ctx)
|
||||
|
||||
for {
|
||||
err := beam.ReadData()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil
|
||||
case <-ticker.C:
|
||||
err := r.SDK.ReadData()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = binary.Write(bin, binary.LittleEndian, beam.Data)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
r.viewData.TotalBytes = r.TotalBytes
|
||||
r.viewData.Data = r.SDK.Buffer
|
||||
|
||||
<-ticker.C
|
||||
// Send the data to the view
|
||||
select {
|
||||
case r.viewCh <- &r.viewData:
|
||||
// Sent the data
|
||||
default:
|
||||
// Dropped the frame!
|
||||
}
|
||||
|
||||
// Send the data to the UDP socket
|
||||
select {
|
||||
case r.recorderCh <- r.SDK.Buffer:
|
||||
// Sent the data
|
||||
default:
|
||||
// Dropped the frame!
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-58
@@ -84,70 +84,14 @@ func (r *Replayer) renderToTerminal(ctx context.Context) {
|
||||
err := binary.Read(&bytesReader, binary.LittleEndian, &og)
|
||||
if err != nil {
|
||||
fmt.Fprintf(&s, "FAILED TO PARSE DATA\nError: %+v", err)
|
||||
fmt.Fprint(os.Stdout, s.String())
|
||||
continue
|
||||
}
|
||||
|
||||
percent := int(float64(data.SizeRead) / float64(fileInfo.Size()) * 100)
|
||||
fmt.Fprintf(&s, "Replayed: %d%%\n", percent)
|
||||
// NOTE: write a string serialization function on the SDK itself
|
||||
fmt.Fprint(&s, "Outgauge {\n")
|
||||
fmt.Fprintf(&s, " Time: %d ms\n", og.Time)
|
||||
fmt.Fprintf(&s, " Car: %s\n", og.Car)
|
||||
fmt.Fprintf(&s, " Flags: %b\n", og.Flags)
|
||||
fmt.Fprintf(&s, " Gear: %d\n", og.Gear)
|
||||
fmt.Fprintf(&s, " Plid: %d\n", og.Plid)
|
||||
fmt.Fprintf(&s, " Speed: %f m/s\n", og.Speed)
|
||||
fmt.Fprintf(&s, " RPM: %f RPM\n", og.RPM)
|
||||
fmt.Fprintf(&s, " Turbo: %f Bar\n", og.Turbo)
|
||||
fmt.Fprintf(&s, " EngTemp: %f °C\n", og.EngTemp)
|
||||
fmt.Fprintf(&s, " Fuel: %f\n", og.Fuel)
|
||||
fmt.Fprintf(&s, " OilPressure: %f Bar\n", og.OilPressure)
|
||||
fmt.Fprintf(&s, " OilTemp: %f °C\n", og.OilTemp)
|
||||
fmt.Fprintf(&s, " DashLights: %b\n", og.DashLights)
|
||||
fmt.Fprintf(&s, " ShowLights: %b\n", og.ShowLights)
|
||||
fmt.Fprintf(&s, " Throttle: %f\n", og.Throttle)
|
||||
fmt.Fprintf(&s, " Brakes: %f\n", og.Brake)
|
||||
fmt.Fprintf(&s, " Clutch: %f\n", og.Clutch)
|
||||
fmt.Fprintf(&s, " Display1: %s\n", og.Display1)
|
||||
fmt.Fprintf(&s, " Display2: %s\n", og.Display2)
|
||||
fmt.Fprintf(&s, " ID: %d\n", og.Display2)
|
||||
fmt.Fprint(&s, "}\n\n")
|
||||
|
||||
fmt.Fprint(&s, "DashLights {\n")
|
||||
fmt.Fprintf(&s, " DL_SHIFT: %t\n", og.DashLights&bngsdk.DL_SHIFT != 0)
|
||||
fmt.Fprintf(&s, " DL_FULLBEAM: %t\n", og.DashLights&bngsdk.DL_FULLBEAM != 0)
|
||||
fmt.Fprintf(&s, " DL_HANDBRAKE: %t\n", og.DashLights&bngsdk.DL_HANDBRAKE != 0)
|
||||
fmt.Fprintf(&s, " DL_PITSPEED: %t\n", og.DashLights&bngsdk.DL_PITSPEED != 0)
|
||||
fmt.Fprintf(&s, " DL_TC: %t\n", og.DashLights&bngsdk.DL_TC != 0)
|
||||
fmt.Fprintf(&s, " DL_SIGNAL_L: %t\n", og.DashLights&bngsdk.DL_SIGNAL_L != 0)
|
||||
fmt.Fprintf(&s, " DL_SIGNAL_R: %t\n", og.DashLights&bngsdk.DL_SIGNAL_R != 0)
|
||||
fmt.Fprintf(&s, " DL_SIGNAL_ANY: %t\n", og.DashLights&bngsdk.DL_SIGNAL_ANY != 0)
|
||||
fmt.Fprintf(&s, " DL_OILWARN: %t\n", og.DashLights&bngsdk.DL_OILWARN != 0)
|
||||
fmt.Fprintf(&s, " DL_BATTERY: %t\n", og.DashLights&bngsdk.DL_BATTERY != 0)
|
||||
fmt.Fprintf(&s, " DL_ABS: %t\n", og.DashLights&bngsdk.DL_ABS != 0)
|
||||
fmt.Fprintf(&s, " DL_SPARE: %t\n", og.DashLights&bngsdk.DL_SPARE != 0)
|
||||
fmt.Fprint(&s, "}\n\n")
|
||||
|
||||
fmt.Fprint(&s, "ShowLights {\n") // Fixed typo "ShowLigths"
|
||||
fmt.Fprintf(&s, " DL_SHIFT: %t\n", og.ShowLights&bngsdk.DL_SHIFT != 0)
|
||||
fmt.Fprintf(&s, " DL_FULLBEAM: %t\n", og.ShowLights&bngsdk.DL_FULLBEAM != 0)
|
||||
fmt.Fprintf(&s, " DL_HANDBRAKE: %t\n", og.ShowLights&bngsdk.DL_HANDBRAKE != 0)
|
||||
fmt.Fprintf(&s, " DL_PITSPEED: %t\n", og.ShowLights&bngsdk.DL_PITSPEED != 0)
|
||||
fmt.Fprintf(&s, " DL_TC: %t\n", og.ShowLights&bngsdk.DL_TC != 0)
|
||||
fmt.Fprintf(&s, " DL_SIGNAL_L: %t\n", og.ShowLights&bngsdk.DL_SIGNAL_L != 0)
|
||||
fmt.Fprintf(&s, " DL_SIGNAL_R: %t\n", og.ShowLights&bngsdk.DL_SIGNAL_R != 0)
|
||||
fmt.Fprintf(&s, " DL_SIGNAL_ANY: %t\n", og.ShowLights&bngsdk.DL_SIGNAL_ANY != 0)
|
||||
fmt.Fprintf(&s, " DL_OILWARN: %t\n", og.ShowLights&bngsdk.DL_OILWARN != 0)
|
||||
fmt.Fprintf(&s, " DL_BATTERY: %t\n", og.ShowLights&bngsdk.DL_BATTERY != 0)
|
||||
fmt.Fprintf(&s, " DL_ABS: %t\n", og.ShowLights&bngsdk.DL_ABS != 0)
|
||||
fmt.Fprintf(&s, " DL_SPARE: %t\n", og.ShowLights&bngsdk.DL_SPARE != 0)
|
||||
fmt.Fprint(&s, "}\n\n")
|
||||
|
||||
fmt.Fprint(&s, "Flags {\n")
|
||||
fmt.Fprintf(&s, " OG_TURBO (Has Turbo): %t\n", og.Flags&bngsdk.OG_TURBO != 0)
|
||||
fmt.Fprintf(&s, " OG_KM (Is Metric): %t\n", og.Flags&bngsdk.OG_KM != 0)
|
||||
fmt.Fprintf(&s, " OG_BAR (Pressure): %t\n", og.Flags&bngsdk.OG_BAR != 0)
|
||||
fmt.Fprint(&s, "}")
|
||||
stringifyOutgaugeData(&s, &og)
|
||||
|
||||
fmt.Fprint(os.Stdout, s.String())
|
||||
}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
package mockserver
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
bngsdk "github.com/ESilva15/gobngsdk"
|
||||
)
|
||||
|
||||
const (
|
||||
_ = iota
|
||||
KiB = 1 << (10 * iota)
|
||||
MiB
|
||||
GiB
|
||||
)
|
||||
|
||||
func stringifyRecordingProgress(s *strings.Builder, nBytes int) {
|
||||
if nBytes < KiB {
|
||||
fmt.Fprintf(s, "%d B", nBytes)
|
||||
} else if nBytes < MiB {
|
||||
fmt.Fprintf(s, "%.2f KiB", float64(nBytes)/float64(KiB))
|
||||
} else if nBytes < GiB {
|
||||
fmt.Fprintf(s, "%.2f MiB", float64(nBytes)/float64(MiB))
|
||||
} else {
|
||||
fmt.Fprintf(s, "%.2f GiB", float64(nBytes)/float64(GiB))
|
||||
}
|
||||
}
|
||||
|
||||
func stringifyOutgaugeData(s *strings.Builder, og *bngsdk.Outgauge) {
|
||||
// NOTE: write a string serialization function on the SDK itself
|
||||
fmt.Fprint(s, "Outgauge {\n")
|
||||
fmt.Fprintf(s, " Time: %d ms\n", og.Time)
|
||||
fmt.Fprintf(s, " Car: %s\n", og.Car)
|
||||
fmt.Fprintf(s, " Flags: %b\n", og.Flags)
|
||||
fmt.Fprintf(s, " Gear: %d\n", og.Gear)
|
||||
fmt.Fprintf(s, " Plid: %d\n", og.Plid)
|
||||
fmt.Fprintf(s, " Speed: %f m/s\n", og.Speed)
|
||||
fmt.Fprintf(s, " RPM: %f RPM\n", og.RPM)
|
||||
fmt.Fprintf(s, " Turbo: %f Bar\n", og.Turbo)
|
||||
fmt.Fprintf(s, " EngTemp: %f °C\n", og.EngTemp)
|
||||
fmt.Fprintf(s, " Fuel: %f\n", og.Fuel)
|
||||
fmt.Fprintf(s, " OilPressure: %f Bar\n", og.OilPressure)
|
||||
fmt.Fprintf(s, " OilTemp: %f °C\n", og.OilTemp)
|
||||
fmt.Fprintf(s, " DashLights: %b\n", og.DashLights)
|
||||
fmt.Fprintf(s, " ShowLights: %b\n", og.ShowLights)
|
||||
fmt.Fprintf(s, " Throttle: %f\n", og.Throttle)
|
||||
fmt.Fprintf(s, " Brakes: %f\n", og.Brake)
|
||||
fmt.Fprintf(s, " Clutch: %f\n", og.Clutch)
|
||||
fmt.Fprintf(s, " Display1: %s\n", og.Display1)
|
||||
fmt.Fprintf(s, " Display2: %s\n", og.Display2)
|
||||
fmt.Fprintf(s, " ID: %d\n", og.Display2)
|
||||
fmt.Fprint(s, "}\n\n")
|
||||
|
||||
fmt.Fprint(s, "DashLights {\n")
|
||||
fmt.Fprintf(s, " DL_SHIFT: %t\n", og.DashLights&bngsdk.DL_SHIFT != 0)
|
||||
fmt.Fprintf(s, " DL_FULLBEAM: %t\n", og.DashLights&bngsdk.DL_FULLBEAM != 0)
|
||||
fmt.Fprintf(s, " DL_HANDBRAKE: %t\n", og.DashLights&bngsdk.DL_HANDBRAKE != 0)
|
||||
fmt.Fprintf(s, " DL_PITSPEED: %t\n", og.DashLights&bngsdk.DL_PITSPEED != 0)
|
||||
fmt.Fprintf(s, " DL_TC: %t\n", og.DashLights&bngsdk.DL_TC != 0)
|
||||
fmt.Fprintf(s, " DL_SIGNAL_L: %t\n", og.DashLights&bngsdk.DL_SIGNAL_L != 0)
|
||||
fmt.Fprintf(s, " DL_SIGNAL_R: %t\n", og.DashLights&bngsdk.DL_SIGNAL_R != 0)
|
||||
fmt.Fprintf(s, " DL_SIGNAL_ANY: %t\n", og.DashLights&bngsdk.DL_SIGNAL_ANY != 0)
|
||||
fmt.Fprintf(s, " DL_OILWARN: %t\n", og.DashLights&bngsdk.DL_OILWARN != 0)
|
||||
fmt.Fprintf(s, " DL_BATTERY: %t\n", og.DashLights&bngsdk.DL_BATTERY != 0)
|
||||
fmt.Fprintf(s, " DL_ABS: %t\n", og.DashLights&bngsdk.DL_ABS != 0)
|
||||
fmt.Fprintf(s, " DL_SPARE: %t\n", og.DashLights&bngsdk.DL_SPARE != 0)
|
||||
fmt.Fprint(s, "}\n\n")
|
||||
|
||||
fmt.Fprint(s, "ShowLights {\n") // Fixed typo "ShowLigths"
|
||||
fmt.Fprintf(s, " DL_SHIFT: %t\n", og.ShowLights&bngsdk.DL_SHIFT != 0)
|
||||
fmt.Fprintf(s, " DL_FULLBEAM: %t\n", og.ShowLights&bngsdk.DL_FULLBEAM != 0)
|
||||
fmt.Fprintf(s, " DL_HANDBRAKE: %t\n", og.ShowLights&bngsdk.DL_HANDBRAKE != 0)
|
||||
fmt.Fprintf(s, " DL_PITSPEED: %t\n", og.ShowLights&bngsdk.DL_PITSPEED != 0)
|
||||
fmt.Fprintf(s, " DL_TC: %t\n", og.ShowLights&bngsdk.DL_TC != 0)
|
||||
fmt.Fprintf(s, " DL_SIGNAL_L: %t\n", og.ShowLights&bngsdk.DL_SIGNAL_L != 0)
|
||||
fmt.Fprintf(s, " DL_SIGNAL_R: %t\n", og.ShowLights&bngsdk.DL_SIGNAL_R != 0)
|
||||
fmt.Fprintf(s, " DL_SIGNAL_ANY: %t\n", og.ShowLights&bngsdk.DL_SIGNAL_ANY != 0)
|
||||
fmt.Fprintf(s, " DL_OILWARN: %t\n", og.ShowLights&bngsdk.DL_OILWARN != 0)
|
||||
fmt.Fprintf(s, " DL_BATTERY: %t\n", og.ShowLights&bngsdk.DL_BATTERY != 0)
|
||||
fmt.Fprintf(s, " DL_ABS: %t\n", og.ShowLights&bngsdk.DL_ABS != 0)
|
||||
fmt.Fprintf(s, " DL_SPARE: %t\n", og.ShowLights&bngsdk.DL_SPARE != 0)
|
||||
fmt.Fprint(s, "}\n\n")
|
||||
|
||||
fmt.Fprint(s, "Flags {\n")
|
||||
fmt.Fprintf(s, " OG_TURBO (Has Turbo): %t\n", og.Flags&bngsdk.OG_TURBO != 0)
|
||||
fmt.Fprintf(s, " OG_KM (Is Metric): %t\n", og.Flags&bngsdk.OG_KM != 0)
|
||||
fmt.Fprintf(s, " OG_BAR (Pressure): %t\n", og.Flags&bngsdk.OG_BAR != 0)
|
||||
fmt.Fprint(s, "}")
|
||||
}
|
||||
Reference in New Issue
Block a user