From 5392b680fecf422efe17cec25ac45958b0f6044e Mon Sep 17 00:00:00 2001 From: Jakob Lorz Date: Tue, 16 Aug 2022 00:09:07 +0200 Subject: [PATCH] Switch to active mode, debounce session history --- cmd/bind.go | 3 +- packets/process/writer/bolt_writer.go | 16 +++++-- .../process/writer/bolt_writer_debouncer.go | 45 ++++++++++++++++++- pkg/privateapi/client.go | 3 +- pkg/streamdb/bolt.go | 2 +- 5 files changed, 60 insertions(+), 9 deletions(-) diff --git a/cmd/bind.go b/cmd/bind.go index ee813ee..fd0d374 100644 --- a/cmd/bind.go +++ b/cmd/bind.go @@ -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 @@ -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 { diff --git a/packets/process/writer/bolt_writer.go b/packets/process/writer/bolt_writer.go index d88aa51..7dad032 100644 --- a/packets/process/writer/bolt_writer.go +++ b/packets/process/writer/bolt_writer.go @@ -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 } @@ -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 } @@ -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) } diff --git a/packets/process/writer/bolt_writer_debouncer.go b/packets/process/writer/bolt_writer_debouncer.go index f703051..46d537d 100644 --- a/packets/process/writer/bolt_writer_debouncer.go +++ b/packets/process/writer/bolt_writer_debouncer.go @@ -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 { @@ -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 + } +} diff --git a/pkg/privateapi/client.go b/pkg/privateapi/client.go index fd239f6..ecfd8c4 100644 --- a/pkg/privateapi/client.go +++ b/pkg/privateapi/client.go @@ -6,10 +6,11 @@ import ( "encoding/json" "fmt" "io" - "log" "net/http" "sync" "time" + + "github.com/jakoblorz/autofone/pkg/log" ) var ( diff --git a/pkg/streamdb/bolt.go b/pkg/streamdb/bolt.go index e5f30d9..4824e11 100644 --- a/pkg/streamdb/bolt.go +++ b/pkg/streamdb/bolt.go @@ -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 )