automatic setup for devices

Its not close to being done and still need to work out the flows due to
who arrives first: telemetry or the peripheral?
This commit is contained in:
2026-09-21 15:27:42 +01:00
parent 6ff4cc3014
commit b364931c80
8 changed files with 134 additions and 15 deletions
+50 -1
View File
@@ -2,6 +2,7 @@ package services
import (
"context"
"fmt"
"log/slog"
"sync/atomic"
@@ -23,7 +24,9 @@ type DeviceService struct {
// Output
Messages chan string
// Callbacks
OnTelemetryProviderDiscovered func()
OnPeripheralFound func(string)
// Telemetry service data fetchers
telemetryProvider func() (string, error)
}
func NewDeviceService(logger *slog.Logger, msg chan string) *DeviceService {
@@ -40,6 +43,9 @@ func NewDeviceService(logger *slog.Logger, msg chan string) *DeviceService {
dev.ctxDiscovery, dev.ctxDiscoveryCancel = context.WithCancel(context.Background())
go dev.FindDevices()
// Set the callbacks for PSS
dev.PSS.OnDeviceFound = dev.deviceFound
return dev
}
@@ -131,3 +137,46 @@ 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] -------------------------------------------------------------