Skip to content

Commit

Permalink
Configure WaitForValidData
Browse files Browse the repository at this point in the history
Fixes #11
  • Loading branch information
mpapenbr committed Jan 1, 2024
1 parent 3e86750 commit f665bad
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 174 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (

require (
github.com/google/uuid v1.5.0
github.com/mpapenbr/goirsdk v0.4.6
github.com/mpapenbr/goirsdk v0.5.0
github.com/mpapenbr/iracelog-service-manager-go v0.9.0
github.com/samber/lo v1.39.0
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ github.com/mpapenbr/goirsdk v0.4.6-0.20231231121623-6c1f8f63856c h1:OT8eAHt9mFE5
github.com/mpapenbr/goirsdk v0.4.6-0.20231231121623-6c1f8f63856c/go.mod h1:9xFsEF9FvX5cAZj42ZFp0of6dsZ5HAZaQqc0wXtJSYI=
github.com/mpapenbr/goirsdk v0.4.6 h1:HL6mru7TJA45FeNKhLZU+su5cgseWHQEmVwCyWv/rXI=
github.com/mpapenbr/goirsdk v0.4.6/go.mod h1:9xFsEF9FvX5cAZj42ZFp0of6dsZ5HAZaQqc0wXtJSYI=
github.com/mpapenbr/goirsdk v0.4.7-0.20240101091649-57b1c13ca63e h1:tflaQrhjtLKXlDopZV5yV3XuoiYTjV+pfzl8dXNJsLE=
github.com/mpapenbr/goirsdk v0.4.7-0.20240101091649-57b1c13ca63e/go.mod h1:9xFsEF9FvX5cAZj42ZFp0of6dsZ5HAZaQqc0wXtJSYI=
github.com/mpapenbr/goirsdk v0.4.7-0.20240101102008-fa5d85eb6562 h1:+JPoESNknsMWdtlDewe96ckhW1IWOWHIaTYEzBTJAs0=
github.com/mpapenbr/goirsdk v0.4.7-0.20240101102008-fa5d85eb6562/go.mod h1:9xFsEF9FvX5cAZj42ZFp0of6dsZ5HAZaQqc0wXtJSYI=
github.com/mpapenbr/goirsdk v0.5.0 h1:VSzFNexW4kvktWQVz238FZ8o/btL7Jqn84JSabpRnVI=
github.com/mpapenbr/goirsdk v0.5.0/go.mod h1:9xFsEF9FvX5cAZj42ZFp0of6dsZ5HAZaQqc0wXtJSYI=
github.com/mpapenbr/iracelog-service-manager-go v0.9.0 h1:yPnRqLjK6mtXyNWYub+ezeLUMAUba0mgkvskK6iWuME=
github.com/mpapenbr/iracelog-service-manager-go v0.9.0/go.mod h1:nFt83bG1Az6PzbOa3fl3Mnw9QIaSWhyMREESOY4ju1Y=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
Expand Down
31 changes: 1 addition & 30 deletions internal/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (p *Processor) Process() {
y := p.api.GetLatestYaml()
p.raceProc.Process()

if p.hasDriverChange(&y.DriverInfo, &p.lastDriverInfo) {
if HasDriverChange(&y.DriverInfo, &p.lastDriverInfo) {
log.Info("DriverInfo changed, updating state")
var freshYaml iryaml.IrsdkYaml
if err := yaml.Unmarshal([]byte(p.api.GetYamlString()), &freshYaml); err != nil {
Expand Down Expand Up @@ -227,32 +227,3 @@ func (p *Processor) sendStateMessage() {
p.lastTimeSendState = time.Now()
p.messageProc.Clear()
}

// checks if relevant driver info changed
// we need this to detect new drivers and driver changes in team races
func (p *Processor) hasDriverChange(current, last *iryaml.DriverInfo) bool {
if len(current.Drivers) != len(last.Drivers) {
return true
}
createLookup := func(data []iryaml.Drivers) map[int]string {
ret := make(map[int]string)
for i := 0; i < len(data); i++ {
ret[data[i].CarIdx] = data[i].UserName
}
return ret
}
currentLookup := createLookup(current.Drivers)
lastLookup := createLookup(last.Drivers)
changeDetected := false
for k, v := range currentLookup {
if lastLookup[k] != v {
log.Debug("Driver change detected",
log.Int("carIdx", k),
log.String("current", v),
log.String("last", lastLookup[k]),
)
changeDetected = true
}
}
return changeDetected
}
109 changes: 0 additions & 109 deletions internal/processor/processor_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/processor/race.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type RaceInvalid struct{}
func (ri *RaceInvalid) Enter() { log.Info("Entering state: RaceInvalid") }
func (ri *RaceInvalid) Exit() { log.Info("Leaving state: RaceInvalid") }
func (ri *RaceInvalid) Update(rp *RaceProc) {
y, _ := rp.api.GetYaml()
y := rp.api.GetLatestYaml()
sessionNum := justValue(rp.api.GetIntValue("SessionNum")).(int32)
if y.SessionInfo.Sessions[sessionNum].SessionType == "Race" {
sessionSate := justValue(rp.api.GetIntValue("SessionState")).(int32)
Expand Down
29 changes: 29 additions & 0 deletions internal/processor/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,32 @@ func collectCars(drivers []yaml.Drivers) []model.CarInfo {
}
return ret
}

// checks if relevant driver info changed
// we need this to detect new drivers and driver changes in team races
func HasDriverChange(current, last *yaml.DriverInfo) bool {
if len(current.Drivers) != len(last.Drivers) {
return true
}
createLookup := func(data []yaml.Drivers) map[int]string {
ret := make(map[int]string)
for i := 0; i < len(data); i++ {
ret[data[i].CarIdx] = data[i].UserName
}
return ret
}
currentLookup := createLookup(current.Drivers)
lastLookup := createLookup(last.Drivers)
changeDetected := false
for k, v := range currentLookup {
if lastLookup[k] != v {
log.Debug("Driver change detected",
log.Int("carIdx", k),
log.String("current", v),
log.String("last", lastLookup[k]),
)
changeDetected = true
}
}
return changeDetected
}
108 changes: 107 additions & 1 deletion internal/processor/util_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
//nolint:funlen // by design for tests
package processor

import "testing"
import (
"testing"

iryaml "github.com/mpapenbr/goirsdk/yaml"
"gopkg.in/yaml.v3"
)

func TestGetMetricUnit(t *testing.T) {
type args struct {
Expand Down Expand Up @@ -31,3 +37,103 @@ func TestGetMetricUnit(t *testing.T) {
})
}
}

func TestHasDriverChange(t *testing.T) {
type args struct {
current string
last string
}

tests := []struct {
name string
args args
want bool
}{
// TODO: Add test cases.
{
"no change",
args{
current: `
Drivers:
- CarIdx: 1
UserName: A
`,
last: `
Drivers:
- CarIdx: 1
UserName: A
`,
},
false,
},
{
"driver name change",
args{
current: `
Drivers:
- CarIdx: 1
UserName: A
`,
last: `
Drivers:
- CarIdx: 1
UserName: B
`,
},
true,
},
{
"same size, additional driver",
args{
current: `
Drivers:
- CarIdx: 1
UserName: A
`,
last: `
Drivers:
- CarIdx: 2
UserName: B
`,
},
true,
},
{
"additional entry",
args{
current: `
Drivers:
- CarIdx: 1
UserName: A
- CarIdx: 2
UserName: B
`,
last: `
Drivers:
- CarIdx: 1
UserName: A
`,
},
true,
},
}

for _, tt := range tests {
var current, last iryaml.DriverInfo
var err error
t.Run(tt.name, func(t *testing.T) {
err = yaml.Unmarshal([]byte(tt.args.current), &current)
if err != nil {
t.Errorf("Error unmarshalling current yaml: %v", err)
}
err = yaml.Unmarshal([]byte(tt.args.last), &last)
if err != nil {
t.Errorf("Error unmarshalling last yaml: %v", err)
}
if got := HasDriverChange(&current, &last); got != tt.want {
t.Errorf("HasDriverChange() = %v, want %v", got, tt.want)
}
})
}
}
Loading

0 comments on commit f665bad

Please sign in to comment.