working on the peripheral state handling
This commit is contained in:
@@ -15,6 +15,7 @@ var (
|
||||
ErrPeripheralAlreadyRegistered = errors.New("peripheral is already registered")
|
||||
ErrNoSuchDevice = errors.New("device doesn't exist")
|
||||
ErrDeviceIsNotConnected = errors.New("device isn't connected")
|
||||
ErrFailedToSetupPeripheral = errors.New("peripheral setup failed")
|
||||
)
|
||||
|
||||
type DeviceState = uint8
|
||||
@@ -176,6 +177,56 @@ func (pss *PeripheralStateStore) setDeviceReconnected(pname string, per peripher
|
||||
// Device State Handling [END] -------------------------------------------------
|
||||
|
||||
// Device Handling [START] -----------------------------------------------------
|
||||
func (pss *PeripheralStateStore) discoverPeripheral(
|
||||
pname string,
|
||||
onDiscovery func(pName string, peripheral peripheral.Peripheral),
|
||||
) error {
|
||||
state, err := pss.GetState(pname)
|
||||
if err != nil {
|
||||
pss.Logger.Error("Can't reconnect device", "device", pname, "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
dev, err := state.device.Discover()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Register the device we just found
|
||||
onDiscovery(pname, dev)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (pss *PeripheralStateStore) handleDeviceTimedOut(pname string) error {
|
||||
err := pss.discoverPeripheral(pname, pss.setDeviceReconnected)
|
||||
if err != nil {
|
||||
// Log something
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (pss *PeripheralStateStore) handleDeviceReconnected(pname string) error {
|
||||
// The device has reconnected, but we must set its state again
|
||||
state, err := pss.GetState(pname)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = state.Peripheral.Setup()
|
||||
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
|
||||
pss.setDeviceConnected(pname, state.Peripheral)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (pss *PeripheralStateStore) HandleDeviceState() {
|
||||
peripherals := pss.GetStates()
|
||||
|
||||
@@ -183,22 +234,30 @@ func (pss *PeripheralStateStore) HandleDeviceState() {
|
||||
switch pState.State {
|
||||
case DeviceIsDisconnected:
|
||||
pss.Logger.Debug("looking for device", "name", pName)
|
||||
dev, err := pState.device.Discover()
|
||||
err := pss.discoverPeripheral(pName, pss.setDeviceConnected)
|
||||
if err != nil {
|
||||
// Log something
|
||||
continue
|
||||
}
|
||||
|
||||
// Register the device we just found
|
||||
pss.setDeviceConnected(pName, dev)
|
||||
case DeviceIsConnected:
|
||||
// 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)
|
||||
case DeviceReconnected:
|
||||
// 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)
|
||||
err := pss.handleDeviceReconnected(pName)
|
||||
if err != nil {
|
||||
pss.Logger.Error("device reconnection handler failed", "error", err)
|
||||
continue
|
||||
}
|
||||
case DeviceTimedOut:
|
||||
// If the device has timed out we need to re-discover it or something
|
||||
pss.Logger.Debug("Device is timed out. Attempting to recconect", "device", pName)
|
||||
err := pss.handleDeviceTimedOut(pName)
|
||||
if err != nil {
|
||||
pss.Logger.Error("device timing out handler failed", "error", err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user