From d677c66a223b83c417946db94cc4dffa74feb7a4 Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Fri, 25 Sep 2026 11:06:33 +0100 Subject: [PATCH 1/3] automated stream start May return the user ability of stopping and starting but its really what we want --- services/devices.go | 7 ++++++- services/devices_lookup.go | 12 ++++++++++++ services/services.go | 2 +- services/telemetry.go | 13 +++++++------ services/telemetry_events.go | 4 ++++ tui/internal/controllers/streaming.go | 26 ++------------------------ 6 files changed, 32 insertions(+), 32 deletions(-) diff --git a/services/devices.go b/services/devices.go index 8bb2c49..875b6fb 100644 --- a/services/devices.go +++ b/services/devices.go @@ -83,14 +83,17 @@ func (ds *DeviceService) PeripheralExists(pname string) bool { // Getters [END] --------------------------------------------------------------- // Actions [START] ------------------------------------------------------------- -func (ds *DeviceService) StartStream() { +func (ds *DeviceService) StartStream(ch <-chan telemetry.TelemetryData) { // NOTE: i'm using this pattern a whole lot. Maybe I can create a struct to handle this + ds.SetTelemetryChannel(ch) + var ctx context.Context ctx, ds.streamCancel = context.WithCancel(context.Background()) ds.PSS.OnStartStream() go ds.transmit(ctx) + ds.Messages <- "Device service started stream\n" } func (ds *DeviceService) StopStream() { @@ -154,6 +157,8 @@ func (ds *DeviceService) peripheralConfigured(pname string, fields []telemetry.F // We need to retrigger field subscription here subscribedTo := ds.triggerFieldSubscription(fields) ds.Messages <- fmt.Sprintf("subscribed to fields: %q\n", subscribedTo) + + ds.PSS.OnStartStreamPeripheral(pname) } // Callbacks [END] ------------------------------------------------------------- diff --git a/services/devices_lookup.go b/services/devices_lookup.go index 788e553..0034e09 100644 --- a/services/devices_lookup.go +++ b/services/devices_lookup.go @@ -211,6 +211,17 @@ func (pss *PeripheralStateStore) OnStartStream() { } } +func (pss *PeripheralStateStore) OnStartStreamPeripheral(pname string) { + state, err := pss.GetState(pname) + if err != nil { + return + } + + if state.State == DeviceIsConfigured { + pss.setDeviceIsStreaming(pname) + } +} + func (pss *PeripheralStateStore) OnStopStream() { pss.mu.Lock() pss.isStreaming = false @@ -349,6 +360,7 @@ func (pss *PeripheralStateStore) configurePeripheral( onFailure(pname) return } + pss.Messages <- fmt.Sprintf("Configuring peripheral for provider: %s\n", provider) err = state.Peripheral.Setup(provider) if err != nil { diff --git a/services/services.go b/services/services.go index f8960fc..8356ddf 100644 --- a/services/services.go +++ b/services/services.go @@ -29,7 +29,7 @@ func NewOrchestrator(logger *slog.Logger) (*Orchestrator, error) { devService.triggerFieldSubscription = telemService.SubscribeToFields // Setup telemetry service callbacks - // telemService.getRequiredFields = devService.GetRequiredFields + telemService.startedStreaming = devService.StartStream return &Orchestrator{ DeviceService: devService, diff --git a/services/telemetry.go b/services/telemetry.go index e05e177..9be6a4d 100644 --- a/services/telemetry.go +++ b/services/telemetry.go @@ -35,6 +35,7 @@ type TelemetryService struct { CtxHealthcheck context.Context healthCheckCancel context.CancelFunc // Callbacks + startedStreaming func(ch <-chan telemetry.TelemetryData) // Devices data request // peripheralProvider func() []peripheral.Peripheral // getRequiredFields func() []telemetry.FieldID @@ -204,21 +205,17 @@ func (t *TelemetryService) StopStream() { t.isStreaming = false } -func (t *TelemetryService) StartStream() { - slog.Debug("Stream started") - +func (t *TelemetryService) StartStream() <-chan telemetry.TelemetryData { // Start the new stream if t.activeProvider == nil { slog.Debug("there's no active provider. not starting the stream") - return + return nil } // Stop the provider healthcheck t.healthCheckCancel() simInCh, _ := t.activeProvider.Stream() - // TODO: the provider needs to be able to tell the data has stopped - // so we can restart the provider lookup routine // Create the context so we can control the lifecycle ctx, cancel := context.WithCancel(context.Background()) @@ -227,6 +224,10 @@ func (t *TelemetryService) StartStream() { // Multiplex this data go t.multiplexData(ctx, simInCh) t.isStreaming = true + + t.Messages <- "Provider has started streaming data\n" + + return simInCh } func (t *TelemetryService) multiplexData(ctx context.Context, dataCh <-chan telem.TelemetryData) { diff --git a/services/telemetry_events.go b/services/telemetry_events.go index 6774a63..603903d 100644 --- a/services/telemetry_events.go +++ b/services/telemetry_events.go @@ -16,6 +16,7 @@ func (t *TelemetryService) onFindProvider(prov telem.TelemetryProvider) { // Attach to the provider t.logger.Info("found provider for " + prov.Name()) t.Messages <- fmt.Sprintf("Found provider \"%s\"\n", prov.Name()) + err := t.SwitchProvider(prov) if err != nil { t.Messages <- fmt.Sprintf("Failed to switch to provider: %+v\n", err.Error()) @@ -26,6 +27,9 @@ func (t *TelemetryService) onFindProvider(prov telem.TelemetryProvider) { // Start the healthcheck on our provider so we can drop it if it stops t.CtxHealthcheck, t.healthCheckCancel = context.WithCancel(context.Background()) go t.ProviderMonitor(t.CtxHealthcheck) + + ch := t.StartStream() + t.startedStreaming(ch) } func (t *TelemetryService) onProviderStopsMidStream() { diff --git a/tui/internal/controllers/streaming.go b/tui/internal/controllers/streaming.go index 8f40005..1fc6d0b 100644 --- a/tui/internal/controllers/streaming.go +++ b/tui/internal/controllers/streaming.go @@ -88,23 +88,9 @@ func (sc *StreamingCtrl) registerHooks() { } } +// TODO: this isn't the startstop method/action anymore. It should show up the stream +// visualizer - TODO: add metrics to the visualizer too, instead of just data func (sc *StreamingCtrl) StartStop() { - if sc.TelemServ.IsStreaming() { - slog.Info("stopping stream") - - sc.TelemServ.StopStream() - sc.DevService.StopStream() - - sc.isRunning = false - return - } - - // stream is not running, we have to start it now - // NOTE: - // Subscribe the only existing device - needs to be discovered by now - slog.Debug("setting the data stream for device servie") - sc.DevService.SetTelemetryChannel(sc.TelemServ.SubscribeListener("DeviceService", 1)) - dev, err := sc.DevService.GetPeripheral(uidevice.NAME) if err == nil { if uiDev, ok := dev.(*uidevice.UIDevice); ok { @@ -112,14 +98,6 @@ func (sc *StreamingCtrl) StartStop() { go sc.listenToUIStream() } } - - slog.Debug("starting services") - sc.DevService.StartStream() - sc.TelemServ.StartStream() - - sc.isRunning = true - - slog.Debug("starting stream") } func (sc *StreamingCtrl) parseStreamUpdateForm(form *views.StreamOptionsView) (*models.StreamOptions, error) { From 584d13e1fd4feae28118c1f453ecf6f0960c89ab Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Fri, 25 Sep 2026 14:47:41 +0100 Subject: [PATCH 2/3] removed unecessary logs from the providers and also implemented the connection dropped into beamng This needs to be reworked tho. There's lots of code thats used both in BeamNG and iRacing. I believe other providers will have the same issue --- providers/beamng/beamng.go | 23 +++++++++++------------ providers/iracing/iracing.go | 4 ++-- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/providers/beamng/beamng.go b/providers/beamng/beamng.go index f8abeb2..7c46a57 100644 --- a/providers/beamng/beamng.go +++ b/providers/beamng/beamng.go @@ -186,13 +186,11 @@ func (b *BeamNG) Subscribe(requestFields []telemetry.FieldID) []string { // Internal -func (b *BeamNG) readData() { - slog.Debug("READING THIS DATA") +func (b *BeamNG) readData() error { ogSnapshot, err := b.SDK.Update() - slog.Debug("THE DATA WAS READ") if err != nil { slog.Error("Error getting data", "error", err) - return + return err } b.mut.Lock() @@ -200,22 +198,20 @@ func (b *BeamNG) readData() { defer b.mut.Unlock() // Read 1 to 1 data - slog.Debug("Reading normal data binds") for _, bind := range b.data.ActiveBinds { - slog.Debug("Current bind: ", "id", bind.ID) b.updaters[bind.ID](&b.data.Values[bind.ID]) } // Set up virtual binds - slog.Debug("Entering virtual binds loop") for _, vBind := range b.data.VirtualBinds { // NOTE: delete the logs here, they are really bad - slog.Debug("Processing virtual binds") vBind.Process(b.data) } b.data.PenultimateDataPoll = b.data.LastDataPoll b.data.LastDataPoll = time.Now() + + return nil } func (b *BeamNG) stream(ctx context.Context) <-chan telemetry.TelemetryData { @@ -239,14 +235,17 @@ func (b *BeamNG) stream(ctx context.Context) <-chan telemetry.TelemetryData { case <-ctx.Done(): return case <-b.ticker.C: - slog.Debug("READING DATA") - b.readData() - slog.Debug("READ DATA") + err := b.readData() + if err != nil { + if b.streamCancel != nil { + b.streamCancel() + } + } // Publish data select { case outCh <- *b.data: - slog.Debug("PUBLISHED DATA") + // Successfuly read and sent data default: // skip this data, don't allow publishers to lag behind } diff --git a/providers/iracing/iracing.go b/providers/iracing/iracing.go index bcf9c93..c8c90d3 100644 --- a/providers/iracing/iracing.go +++ b/providers/iracing/iracing.go @@ -143,8 +143,8 @@ func (i *IRacing) stream(ctx context.Context) <-chan telemetry.TelemetryData { // Put this into the configuration file consecutiveTimeouts := 0 - maxTimeouts := 30 - dataEvTimeout := 100 + maxTimeouts := 10 + dataEvTimeout := 30 for { // Explicitly intercept cancellation select { From 48b10afbba332e95e73d9564ba858456e891c62a Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Fri, 25 Sep 2026 14:48:29 +0100 Subject: [PATCH 3/3] devices and providers can now drop mid stream and then reconnect without impact --- devices/cdashdisplay/display.go | 7 +++++++ services/devices_events.go | 5 +++++ services/devices_lookup.go | 4 ++-- services/services.go | 1 + services/telemetry.go | 4 +++- services/telemetry_events.go | 6 ++++++ 6 files changed, 24 insertions(+), 3 deletions(-) diff --git a/devices/cdashdisplay/display.go b/devices/cdashdisplay/display.go index 51b84a0..9294d10 100644 --- a/devices/cdashdisplay/display.go +++ b/devices/cdashdisplay/display.go @@ -405,6 +405,11 @@ func (d *CDashDisplay) UnloadLayout() error { return nil } +func (cds *CDashDisplay) resetState() { + cds.fieldToWindows = make(map[telemetry.FieldID][]int16) + cds.State.Layout = NewLayoutTree() +} + func (cds *CDashDisplay) reset() error { err := cds.WT.SendCommand(resetCMDID, []byte{0x01, 0x02, 0x03, 0x04}, nil) if err != nil { @@ -413,6 +418,8 @@ func (cds *CDashDisplay) reset() error { time.Sleep(3000 * time.Millisecond) + cds.resetState() + return nil } diff --git a/services/devices_events.go b/services/devices_events.go index 5e568ea..bd45330 100644 --- a/services/devices_events.go +++ b/services/devices_events.go @@ -1 +1,6 @@ package services + +func (ds *DeviceService) OnStoppedMidStream() { + // Need to make every device be unconfigured + ds.StopStream() +} diff --git a/services/devices_lookup.go b/services/devices_lookup.go index 0034e09..b53da2e 100644 --- a/services/devices_lookup.go +++ b/services/devices_lookup.go @@ -228,8 +228,8 @@ func (pss *PeripheralStateStore) OnStopStream() { pss.mu.Unlock() for _, state := range pss.GetStates() { - if state.State == DeviceIsStreaming { - pss.setDeviceConfigured(state.device.Name) + if state.State > DeviceIsConfigured { + pss.setDeviceUnconfigured(state.device.Name) } } } diff --git a/services/services.go b/services/services.go index 8356ddf..7686f6a 100644 --- a/services/services.go +++ b/services/services.go @@ -30,6 +30,7 @@ func NewOrchestrator(logger *slog.Logger) (*Orchestrator, error) { // Setup telemetry service callbacks telemService.startedStreaming = devService.StartStream + telemService.providerStoppedMidStream = devService.OnStoppedMidStream return &Orchestrator{ DeviceService: devService, diff --git a/services/telemetry.go b/services/telemetry.go index 9be6a4d..f449ce6 100644 --- a/services/telemetry.go +++ b/services/telemetry.go @@ -35,7 +35,8 @@ type TelemetryService struct { CtxHealthcheck context.Context healthCheckCancel context.CancelFunc // Callbacks - startedStreaming func(ch <-chan telemetry.TelemetryData) + startedStreaming func(ch <-chan telemetry.TelemetryData) + providerStoppedMidStream func() // Devices data request // peripheralProvider func() []peripheral.Peripheral // getRequiredFields func() []telemetry.FieldID @@ -139,6 +140,7 @@ func (t *TelemetryService) dropActiveProvider() { t.cancelForward() } + // NOTE: can we move the stop stream into the provider Close() method? t.activeProvider.StopStream() t.activeProvider.Close() t.activeProvider = nil diff --git a/services/telemetry_events.go b/services/telemetry_events.go index 603903d..8416f21 100644 --- a/services/telemetry_events.go +++ b/services/telemetry_events.go @@ -30,6 +30,8 @@ func (t *TelemetryService) onFindProvider(prov telem.TelemetryProvider) { ch := t.StartStream() t.startedStreaming(ch) + + t.Messages <- "provider switch completed" } func (t *TelemetryService) onProviderStopsMidStream() { @@ -38,6 +40,10 @@ func (t *TelemetryService) onProviderStopsMidStream() { // if the stream stopped we have to restart the devices and everything t.Messages <- "Telemetry provider stopped mid stream\n" t.logger.Info("cleaning dropped provider and restarting lookup service") + t.dropActiveProvider() go t.FindProvider(t.CtxMonitor) + + // Tell the devices service that we lost the thing + t.providerStoppedMidStream() }