refactored the peripheral configuration loop

This commit is contained in:
2026-09-21 16:07:00 +01:00
parent d10c6997f4
commit 91f33c79ed
+42 -33
View File
@@ -24,6 +24,8 @@ type DeviceState = uint8
const ( const (
DeviceTimedOut uint8 = iota DeviceTimedOut uint8 = iota
DeviceIsConnected DeviceIsConnected
DeviceIsUnconfigured
DeviceIsConfigured
DeviceIsDisconnected DeviceIsDisconnected
DeviceReconnected DeviceReconnected
) )
@@ -32,7 +34,6 @@ type PeripheralState struct {
device *devices.Device device *devices.Device
Peripheral peripheral.Peripheral Peripheral peripheral.Peripheral
State DeviceState State DeviceState
Setup bool
} }
func NewPeripheralState( func NewPeripheralState(
@@ -44,7 +45,6 @@ func NewPeripheralState(
device: dev, device: dev,
Peripheral: peripheral, Peripheral: peripheral,
State: state, State: state,
Setup: false,
} }
return &perState return &perState
@@ -121,18 +121,6 @@ func (pss *PeripheralStateStore) AddDevice(dev *devices.Device) error {
return nil return nil
} }
func (pss *PeripheralStateStore) UpdatePeripheralSetupState(pname string, nState bool) error {
if !pss.DeviceExists(pname) {
return ErrNoSuchDevice
}
pss.mu.Lock()
defer pss.mu.Unlock()
pss.store[pname].Setup = nState
return nil
}
// DeviceExists returns whether the store is already tracking `pname` // DeviceExists returns whether the store is already tracking `pname`
func (pss *PeripheralStateStore) DeviceExists(pname string) bool { func (pss *PeripheralStateStore) DeviceExists(pname string) bool {
pss.mu.RLock() pss.mu.RLock()
@@ -177,7 +165,6 @@ func (pss *PeripheralStateStore) setDeviceConnected(pname string, per peripheral
pss.mu.Unlock() pss.mu.Unlock()
pss.Messages <- fmt.Sprintf("Device successfuly connected: %s\n", pname) pss.Messages <- fmt.Sprintf("Device successfuly connected: %s\n", pname)
// pss.OnDeviceFound(pname)
} }
func (pss *PeripheralStateStore) setDeviceTimedOut(pname string) { func (pss *PeripheralStateStore) setDeviceTimedOut(pname string) {
@@ -198,6 +185,22 @@ func (pss *PeripheralStateStore) setDeviceReconnected(pname string, per peripher
pss.store[pname].State = DeviceReconnected pss.store[pname].State = DeviceReconnected
} }
func (pss *PeripheralStateStore) setDeviceUnconfigured(pname string) {
pss.Logger.Info("device is connected but not configured", "device", pname)
pss.mu.Lock()
defer pss.mu.Unlock()
pss.store[pname].State = DeviceIsUnconfigured
}
func (pss *PeripheralStateStore) setDeviceConfigured(pname string) {
pss.Logger.Info("device is configured and ready for data", "device", pname)
pss.mu.Lock()
defer pss.mu.Unlock()
pss.store[pname].State = DeviceIsConfigured
}
// Device State Handling [END] ------------------------------------------------- // Device State Handling [END] -------------------------------------------------
// Device Handling [START] ----------------------------------------------------- // Device Handling [START] -----------------------------------------------------
@@ -241,7 +244,6 @@ func (pss *PeripheralStateStore) handleDeviceReconnected(pname string) error {
// Update the peripheral state // Update the peripheral state
pss.setDeviceConnected(pname, state.Peripheral) pss.setDeviceConnected(pname, state.Peripheral)
pss.UpdatePeripheralSetupState(pname, false)
return nil return nil
} }
@@ -251,22 +253,17 @@ func (pss *PeripheralStateStore) handleDeviceReconnected(pname string) error {
// Connected -> Unconfigured -> Configured I believe this would work nicely // Connected -> Unconfigured -> Configured I believe this would work nicely
// THIS IS A TODO ↑↑↑↑↑↑ // THIS IS A TODO ↑↑↑↑↑↑
func (pss *PeripheralStateStore) handleDeviceConnected(pname string) error { func (pss *PeripheralStateStore) handleDeviceConnected(pname string) error {
// Things to do once the device is connected pss.setDeviceUnconfigured(pname)
// 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 return nil
} }
func (pss *PeripheralStateStore) setupPeripheral(state *PeripheralState) error { func (pss *PeripheralStateStore) handleDeviceIsUnconfigured(pname string) error {
// Here we need to configure our device. If no error occurs its configured!
state, err := pss.GetState(pname)
if err != nil {
return err
}
provider, err := pss.telemetryProvider() provider, err := pss.telemetryProvider()
if err != nil { if err != nil {
return err return err
@@ -277,14 +274,16 @@ func (pss *PeripheralStateStore) setupPeripheral(state *PeripheralState) error {
return err return err
} }
err = pss.UpdatePeripheralSetupState(state.device.Name, true) pss.setDeviceConfigured(pname)
if err != nil {
return err
}
return nil return nil
} }
func (pss *PeripheralStateStore) handleDeviceIsConfigured(pname string) error {
// Nothing to do - this method shouldn't even exist then
return nil
}
func (pss *PeripheralStateStore) HandleDeviceState() { func (pss *PeripheralStateStore) HandleDeviceState() {
peripherals := pss.GetStates() peripherals := pss.GetStates()
@@ -301,6 +300,16 @@ func (pss *PeripheralStateStore) HandleDeviceState() {
// Need to check if its streaming, if its not streaming than we have to do a healthcheck // Need to check if its streaming, if its not streaming than we have to do a healthcheck
pss.Logger.Debug("Device is connected. Normal", "device", pName) pss.Logger.Debug("Device is connected. Normal", "device", pName)
pss.handleDeviceConnected(pName) pss.handleDeviceConnected(pName)
case DeviceIsUnconfigured:
pss.Logger.Debug("Device is still being configured.", "device", pName)
err := pss.handleDeviceIsUnconfigured(pName)
if err != nil {
// Something is not adding up, it should be logged somewhere... SYKE
continue
}
case DeviceIsConfigured:
// Nothing to do here
continue
case DeviceReconnected: case DeviceReconnected:
// If the device has reconnected we need to reset the device and then set it as connected // If the device has reconnected we need to reset the device and then set it as connected
pss.Logger.Debug("Device has reconnected. Clearing up state", "device", pName) pss.Logger.Debug("Device has reconnected. Clearing up state", "device", pName)