Device reconnection handling #15

Merged
esilva merged 10 commits from device-reconnection-handling into auto-detect-devices 2026-09-23 10:27:52 +01:00
3 changed files with 44 additions and 4 deletions
Showing only changes of commit df1e3c26fa - Show all commits
+12
View File
@@ -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)
+8
View File
@@ -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()
+24 -4
View File
@@ -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)
}