Skip to content

Commit

Permalink
Switch to active mode, debounce session history
Browse files Browse the repository at this point in the history
  • Loading branch information
jakoblorz committed Aug 15, 2022
1 parent 1391402 commit 5392b68
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 9 deletions.
3 changes: 2 additions & 1 deletion cmd/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ for the packet ids to select.
api := privateapi.New(token, baseURL)
defer api.Close()

db, err := streamdb.Open("autofone", &snapshotWriter{api}, streamdb.DebounceModeDelay)
db, err := streamdb.Open("autofone", &snapshotWriter{api}, streamdb.DebounceModeActive)
if err != nil {
log.Printf("%+v", err)
return
Expand Down Expand Up @@ -170,6 +170,7 @@ for the packet ids to select.
boltWriter.Lap = writer.NewPacketDebouncer(boltWriter, 0)
boltWriter.CarTelemetry = writer.NewPacketDebouncer(boltWriter, 0)
boltWriter.CarStatus = writer.NewPacketDebouncer(boltWriter, 0)
boltWriter.SessionHistory = writer.NewSessionHistoryDebouncer(boltWriter, 0)
defer boltWriter.Close()

for {
Expand Down
16 changes: 12 additions & 4 deletions packets/process/writer/bolt_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ type Bolt struct {
*process.P
privateapi.Client

Motion *motionDebouncer
Lap *packetDebouncer
CarTelemetry *packetDebouncer
CarStatus *packetDebouncer
Motion *motionDebouncer
Lap *packetDebouncer
CarTelemetry *packetDebouncer
CarStatus *packetDebouncer
SessionHistory *sessionHistoryDebouncer

DB streamdb.I
}
Expand All @@ -37,6 +38,9 @@ func (ch *Bolt) Close() error {
if ch.CarStatus != nil {
ch.CarStatus.timer.Stop()
}
if ch.SessionHistory != nil {
ch.SessionHistory.Stop()
}
return nil
}

Expand Down Expand Up @@ -74,5 +78,9 @@ func (ch *Bolt) Write(m *process.M) {
ch.CarStatus.Write(m)
return
}
if ch.SessionHistory != nil && m.Header.PacketID == constants.PacketSessionHistory {
ch.SessionHistory.Write(m)
return
}
ch.write(m)
}
45 changes: 43 additions & 2 deletions packets/process/writer/bolt_writer_debouncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
)

const (
motionDebouncerInterval = 250 * time.Millisecond
packetDebouncerInterval = 1 * time.Second
motionDebouncerInterval = 250 * time.Millisecond
packetDebouncerInterval = 1 * time.Second
sessionHistoryDebouncerInterval = 10 * time.Second
)

type writer interface {
Expand Down Expand Up @@ -260,3 +261,43 @@ func (a averageAndLastPlayerCarMotion22) AverageAndLastPlayerCarMotion() *packet

return &pmd
}

func NewSessionHistoryDebouncer(ch writer, interval time.Duration) *sessionHistoryDebouncer {
if interval == 0 {
interval = sessionHistoryDebouncerInterval
}
pdc := &sessionHistoryDebouncer{
ch: ch,
interval: packetDebouncerInterval,
}
return pdc
}

type sessionHistoryDebouncer struct {
ch writer
interval time.Duration
debouncers map[uint8]*packetDebouncer
}

func (dbc *sessionHistoryDebouncer) Stop() {
for _, d := range dbc.debouncers {
d.timer.Stop()
}
}

func (dbc *sessionHistoryDebouncer) Write(m *process.M) {
if sh21, ok := m.Pack.(*packets.PacketSessionHistoryData21); ok {
if dbc.debouncers[sh21.CarIdx] == nil {
dbc.debouncers[sh21.CarIdx] = NewPacketDebouncer(dbc.ch, dbc.interval)
}
dbc.debouncers[sh21.CarIdx].Write(m)
return
}
if sh22, ok := m.Pack.(*packets.PacketSessionHistoryData22); ok {
if dbc.debouncers[sh22.CarIdx] == nil {
dbc.debouncers[sh22.CarIdx] = NewPacketDebouncer(dbc.ch, dbc.interval)
}
dbc.debouncers[sh22.CarIdx].Write(m)
return
}
}
3 changes: 2 additions & 1 deletion pkg/privateapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"sync"
"time"

"github.com/jakoblorz/autofone/pkg/log"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion pkg/streamdb/bolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const (

const (
DebounceModeDelay_Interval time.Duration = 1 * time.Minute
DebounceModeActive_Interval time.Duration = 4 * time.Second
DebounceModeActive_Interval time.Duration = 25 * time.Second
DebounceModeAggressive_Interval time.Duration = 250 * time.Millisecond
)

Expand Down

0 comments on commit 5392b68

Please sign in to comment.