Skip to content

Commit

Permalink
start sending speedmaps when racing
Browse files Browse the repository at this point in the history
  • Loading branch information
mpapenbr committed May 30, 2024
1 parent 5d2a252 commit 6c7c6ca
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
22 changes: 18 additions & 4 deletions internal/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ type Processor struct {
stateOutput chan *racestatev1.PublishStateRequest
speedmapOutput chan *racestatev1.PublishSpeedmapRequest
extraInfoOutput chan *racestatev1.PublishEventExtraInfoRequest
recording bool
racing bool
}

//nolint:whitespace,funlen // can't get different linters happy
Expand Down Expand Up @@ -147,7 +149,7 @@ func NewProcessor(
api: api,
options: opts,
lastTimeSendState: time.Time{},
lastTimeSendSpeedmap: time.Time{}.Add(opts.SpeedmapPublishInterval),
lastTimeSendSpeedmap: time.Now(),
stateOutput: stateOutput,
speedmapOutput: speedmapOutput,
extraInfoOutput: extraInfoOutput,
Expand All @@ -158,12 +160,18 @@ func NewProcessor(
speedmapProc: speedmapProc,
carDriverProc: carDriverProc,
pitBoundaryProc: pitBoundaryProc,
recording: true,
racing: false,
}
ret.init()
return &ret
}

func (p *Processor) init() {
p.raceProc.RaceRunCallback = func() {
p.racing = true
p.lastTimeSendSpeedmap = time.Now().Add(p.options.SpeedmapPublishInterval)
}
p.raceProc.RaceDoneCallback = func() {
p.sendSpeedmapMessage()
p.sendStateMessage()
Expand Down Expand Up @@ -195,10 +203,11 @@ func (p *Processor) init() {
Timestamp: timestamppb.Now(),
ExtraInfo: &racestatev1.ExtraInfo{PitInfo: &pitInfo},
}

p.extraInfoOutput <- &msg
}
time.Sleep(1 * time.Second) // wait a little to get outstandig messages transmitted
p.recording = false // signal recording done
p.racing = false // signal racing done
if p.options.RecordingDoneChannel != nil {
log.Debug("Signaling recording done")
close(p.options.RecordingDoneChannel)
Expand Down Expand Up @@ -227,10 +236,15 @@ func (p *Processor) Process() {
p.carDriverProc.Process(&freshYaml)
}

if time.Now().After(p.lastTimeSendState.Add(p.options.StatePublishInterval)) {
if p.recording &&
time.Now().After(p.lastTimeSendState.Add(p.options.StatePublishInterval)) {

p.sendStateMessage()
}
if time.Now().After(p.lastTimeSendSpeedmap.Add(p.options.SpeedmapPublishInterval)) {

if p.recording && p.racing &&
time.Now().After(p.lastTimeSendSpeedmap.Add(p.options.SpeedmapPublishInterval)) {

p.sendSpeedmapMessage()
}
}
Expand Down
4 changes: 4 additions & 0 deletions internal/processor/race.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type RaceProc struct {
cooldownEntered time.Time
carProc *CarProc
messageProc *MessageProc
RaceRunCallback func()
RaceDoneCallback func()
}

Expand All @@ -37,6 +38,9 @@ func (ri *RaceInvalid) Update(rp *RaceProc) {
rp.messageProc.RaceStarts()
rp.carProc.RaceStarts()
rp.setState(&RaceRun{})
if rp.RaceRunCallback != nil {
rp.RaceRunCallback()
}
}
}
}
Expand Down

0 comments on commit 6c7c6ca

Please sign in to comment.