clean up a bit the logic of how we get to know whether the telemetry started on devices lookup

This commit is contained in:
2026-09-21 15:55:31 +01:00
parent b364931c80
commit d10c6997f4
5 changed files with 45 additions and 61 deletions
+7 -42
View File
@@ -2,7 +2,6 @@ package services
import (
"context"
"fmt"
"log/slog"
"sync/atomic"
@@ -24,7 +23,6 @@ type DeviceService struct {
// Output
Messages chan string
// Callbacks
OnPeripheralFound func(string)
// Telemetry service data fetchers
telemetryProvider func() (string, error)
}
@@ -44,12 +42,18 @@ func NewDeviceService(logger *slog.Logger, msg chan string) *DeviceService {
go dev.FindDevices()
// Set the callbacks for PSS
dev.PSS.OnDeviceFound = dev.deviceFound
dev.PSS.telemetryProvider = dev.getTelemetryProvider
return dev
}
// Getters [START] -------------------------------------------------------------
// This function is currently only being used by PSS, we may have to find a better
// pattern for this
func (ds *DeviceService) getTelemetryProvider() (string, error) {
return ds.telemetryProvider()
}
func (ds *DeviceService) GetDevices() []peripheral.Peripheral {
snapshot := ds.PSS.GetStates()
peripherals := make([]peripheral.Peripheral, 0, len(snapshot))
@@ -138,45 +142,6 @@ func (ds *DeviceService) transmit(ctx context.Context) {
}
}
func (ds *DeviceService) deviceFound(pname string) {
ds.OnPeripheralFound(pname)
}
// Callbacks [START] -----------------------------------------------------------
// ProviderFoundCallback should be called once the telemetry service finds a provider
// Here we need to setup our devices. Some devices might have different settings for
// different sims
func (ds *DeviceService) ProviderFoundCallback(name string) {
ds.Messages <- "Device services got triggered by a provider being found\n"
for _, peripheral := range ds.PSS.GetStates() {
// ds.Messages <- fmt.Sprintf("dev: %s, SETUP: %t, STATE: %d\n",
// peripheral.device.Name, peripheral.Setup, peripheral.State)
if peripheral.State != DeviceIsConnected || peripheral.Setup {
continue
}
// The device is connected and still needs to run the setup
ds.Messages <- fmt.Sprintf("device '%s' needs to be setup\n", peripheral.device.Name)
provider, err := ds.telemetryProvider()
if err != nil {
// Can't setup anything
continue
}
err = peripheral.Peripheral.Setup(provider)
if err != nil {
// TODO: log do something
continue
}
err = ds.PSS.UpdatePeripheralSetupState(peripheral.device.Name, true)
if err != nil {
// TODO: log do something
continue
}
}
}
// Callbacks [END] -------------------------------------------------------------
+38 -9
View File
@@ -57,7 +57,7 @@ type PeripheralStateStore struct {
// Messaging for UI and stuff
Messages chan string
// Callbacks
OnDeviceFound func(string)
telemetryProvider func() (string, error)
}
func NewPeripheralStateStore(
@@ -177,7 +177,7 @@ func (pss *PeripheralStateStore) setDeviceConnected(pname string, per peripheral
pss.mu.Unlock()
pss.Messages <- fmt.Sprintf("Device successfuly connected: %s\n", pname)
pss.OnDeviceFound(pname)
// pss.OnDeviceFound(pname)
}
func (pss *PeripheralStateStore) setDeviceTimedOut(pname string) {
@@ -239,20 +239,49 @@ func (pss *PeripheralStateStore) handleDeviceReconnected(pname string) error {
return err
}
if err != nil {
pss.Logger.Error("failed to setup peripheral", "peripheral", pname, "error", err)
return ErrFailedToSetupPeripheral
}
// Around here I believe I need to swap the states so the peripheral is setup
// Update the peripheral state
pss.setDeviceConnected(pname, state.Peripheral)
pss.UpdatePeripheralSetupState(pname, false)
return nil
}
// handleDeviceConnected will handle the device setup after it connects
// NOTE: should this be a state after Connected?
// Connected -> Unconfigured -> Configured I believe this would work nicely
// THIS IS A TODO ↑↑↑↑↑↑
func (pss *PeripheralStateStore) handleDeviceConnected(pname string) error {
// We need to query wheter we have a telemetry provider running or not
// Things to do once the device is connected
// 1. Setup
state, err := pss.GetState(pname)
if err != nil {
// We need to log something here or something
return err
}
if !state.Setup {
err = pss.setupPeripheral(state)
}
return nil
}
func (pss *PeripheralStateStore) setupPeripheral(state *PeripheralState) error {
provider, err := pss.telemetryProvider()
if err != nil {
return err
}
err = state.Peripheral.Setup(provider)
if err != nil {
return err
}
err = pss.UpdatePeripheralSetupState(state.device.Name, true)
if err != nil {
return err
}
return nil
}
-2
View File
@@ -25,11 +25,9 @@ func NewOrchestrator(logger *slog.Logger) (*Orchestrator, error) {
go telemService.FindProvider(telemService.CtxMonitor)
// Setup device service callbacks
devService.OnPeripheralFound = telemService.PeripheralFoundCallback
devService.telemetryProvider = telemService.GetTelemetryProviderName
// Setup telemetry service callbacks
telemService.OnProviderFound = devService.ProviderFoundCallback
telemService.peripheralProvider = devService.GetDevices
return &Orchestrator{
-5
View File
@@ -36,7 +36,6 @@ type TelemetryService struct {
CtxHealthcheck context.Context
healthCheckCancel context.CancelFunc
// Callbacks
OnProviderFound func(string)
// Devices data request
peripheralProvider func() []peripheral.Peripheral
}
@@ -277,8 +276,4 @@ func (t *TelemetryService) IsStreaming() bool {
// Callbacks [START] -----------------------------------------------------------
func (t *TelemetryService) PeripheralFoundCallback(pname string) {
t.Messages <- "Telemetry service callback for peripheral found called\n"
}
// Callbacks [END] -------------------------------------------------------------
-3
View File
@@ -26,9 +26,6 @@ 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)
// Tell the devices service we got a provider
t.OnProviderFound(prov.Name())
}
func (t *TelemetryService) onProviderStopsMidStream() {