peripheral can connect mid stream and start go into streaming immidiately

This commit is contained in:
2026-09-23 13:40:31 +01:00
parent 76d758e264
commit a519cf473e
6 changed files with 53 additions and 25 deletions
+30 -2
View File
@@ -2,6 +2,7 @@ package services
import (
"context"
"fmt"
"log/slog"
"sync/atomic"
@@ -24,7 +25,8 @@ type DeviceService struct {
Messages chan string
// Callbacks
// Telemetry service data fetchers
telemetryProvider func() (string, error)
telemetryProvider func() (string, error)
triggerFieldSubscription func() []telemetry.FieldID
}
func NewDeviceService(logger *slog.Logger, msg chan string) *DeviceService {
@@ -43,6 +45,7 @@ func NewDeviceService(logger *slog.Logger, msg chan string) *DeviceService {
// Set the callbacks for PSS
dev.PSS.telemetryProvider = dev.getTelemetryProvider
dev.PSS.onPeripheralConfigured = dev.peripheralConfigured
return dev
}
@@ -59,7 +62,7 @@ func (ds *DeviceService) GetDevices() []peripheral.Peripheral {
peripherals := make([]peripheral.Peripheral, 0, len(snapshot))
for _, state := range snapshot {
if state.State < DeviceIsConnected {
if state.State < DeviceIsConfigured {
continue
}
peripherals = append(peripherals, state.Peripheral)
@@ -77,6 +80,24 @@ func (ds *DeviceService) PeripheralExists(pname string) bool {
return err == nil
}
func (ds *DeviceService) GetRequiredFields() []telemetry.FieldID {
// NOTE: this can be optimized, not that it matters at this stage, but if
// it runs while telemetry is running we want it optimized I guess
seen := make(map[telemetry.FieldID]struct{})
var allFields []telemetry.FieldID
for _, dev := range ds.GetDevices() {
for _, field := range dev.RequiredFields() {
if _, exists := seen[field]; !exists {
seen[field] = struct{}{}
allFields = append(allFields, field)
}
}
}
return allFields
}
// Getters [END] ---------------------------------------------------------------
// Actions [START] -------------------------------------------------------------
@@ -95,6 +116,8 @@ func (ds *DeviceService) StopStream() {
return
}
ds.PSS.OnStopStream()
ds.streamCancel()
ds.streamCancel = nil
}
@@ -145,5 +168,10 @@ func (ds *DeviceService) transmit(ctx context.Context) {
}
// Callbacks [START] -----------------------------------------------------------
func (ds *DeviceService) peripheralConfigured(pname string) {
// We need to retrigger field subscription here
fields := ds.triggerFieldSubscription()
ds.Messages <- fmt.Sprintf("subscribed to fields: %+v\n", fields)
}
// Callbacks [END] -------------------------------------------------------------