From df1e3c26fa44e131f284ba62e2da9d3c152baed6 Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Tue, 22 Sep 2026 17:07:53 +0100 Subject: [PATCH] reconnects are slightly improved I don't really like the reset thing with the delay. I need to implement something better later on --- devices/cdashdisplay/display.go | 12 ++++++++++++ devices/cdashdisplay/setup.go | 8 ++++++++ services/devices_lookup.go | 28 ++++++++++++++++++++++++---- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/devices/cdashdisplay/display.go b/devices/cdashdisplay/display.go index 4314ec1..51b84a0 100644 --- a/devices/cdashdisplay/display.go +++ b/devices/cdashdisplay/display.go @@ -34,6 +34,7 @@ const ( sendDataCMDID types.Command = 7 newLayoutCMDID types.Command = 8 healthCheckCMDID types.Command = 9 + resetCMDID types.Command = 10 ) const ( @@ -404,6 +405,17 @@ func (d *CDashDisplay) UnloadLayout() error { return nil } +func (cds *CDashDisplay) reset() error { + err := cds.WT.SendCommand(resetCMDID, []byte{0x01, 0x02, 0x03, 0x04}, nil) + if err != nil { + return err + } + + time.Sleep(3000 * time.Millisecond) + + return nil +} + func (d *CDashDisplay) SendData(data *telemetry.TelemetryData) error { packet := d.encodePacket(data) diff --git a/devices/cdashdisplay/setup.go b/devices/cdashdisplay/setup.go index 0afd001..74117f4 100644 --- a/devices/cdashdisplay/setup.go +++ b/devices/cdashdisplay/setup.go @@ -25,6 +25,14 @@ func (cds *CDashDisplay) setupForBeamNG() error { } func (cds *CDashDisplay) Setup(provider string) error { + // Doesn't matter which one we are picking, we need to reset the CDashDisplay + // first - we will improve this setup behaviour later on with an Update to + // change it without dropping connection or some shit + err := cds.reset() + if err != nil { + return err + } + switch provider { case constants.IRacingProviderName: return cds.setupForIracing() diff --git a/services/devices_lookup.go b/services/devices_lookup.go index 3203dbc..499b8ea 100644 --- a/services/devices_lookup.go +++ b/services/devices_lookup.go @@ -86,6 +86,8 @@ type PeripheralStateStore struct { Messages chan string // Callbacks telemetryProvider func() (string, error) + // Internal State + isStreaming bool } func NewPeripheralStateStore( @@ -175,12 +177,22 @@ func (pss *PeripheralStateStore) DeleteDevice(pname string) error { return nil } +func (pss *PeripheralStateStore) GetStreamingState() bool { + pss.mu.RLock() + defer pss.mu.RUnlock() + return pss.isStreaming +} + // "Events" [START] ------------------------------------------------------------ func (ds *DeviceService) onDeviceTimedOut(pname string) { ds.PSS.setDeviceTimedOut(pname) } func (pss *PeripheralStateStore) OnStartStream() { + pss.mu.Lock() + pss.isStreaming = true + pss.mu.Unlock() + for _, state := range pss.GetStates() { if state.State == DeviceIsConfigured { pss.setDeviceIsStreaming(state.device.Name) @@ -188,6 +200,12 @@ func (pss *PeripheralStateStore) OnStartStream() { } } +func (pss *PeripheralStateStore) OnStopStream() { + pss.mu.Lock() + pss.isStreaming = false + pss.mu.Unlock() +} + // "Events" [END] -------------------------------------------------------------- // Device State Handling [START] ----------------------------------------------- @@ -325,6 +343,7 @@ func (pss *PeripheralStateStore) configurePeripheral( } func (pss *PeripheralStateStore) handleDeviceTimedOut(pname string) error { + pss.Messages <- "device " + pname + " timed out\n" pss.discoverPeripheral(pname, pss.setDeviceReconnected, pss.setDeviceTimedOut) return nil } @@ -367,6 +386,11 @@ func (pss *PeripheralStateStore) handleDeviceIsUnconfigured(pname string) error func (pss *PeripheralStateStore) handleDeviceIsConfigured(pname string) error { // Here we have to check wheter we are streaming or not. If we aren't streaming // then we ought to do a healthcheck on the peripheral + if pss.GetStreamingState() { + pss.Messages <- "returning device " + pname + " into streaming\n" + pss.setDeviceIsStreaming(pname) + } + return nil } @@ -431,10 +455,6 @@ func (pss *PeripheralStateStore) HandleDeviceState() { if updatedState.State == DeviceIsConnected || updatedState.State == DeviceIsUnconfigured || updatedState.State == DeviceIsConfigured { - pss.Messages <- fmt.Sprintf( - "Performing healthcheck. STATE: %s\n", - DeviceStateToStr(updatedState.State), - ) pss.performHealthCheck(pName, updatedState) }