implements the OS2L finder and improve behaviors of finders

This commit is contained in:
2025-11-12 13:42:38 +01:00
parent 121a14ac61
commit 1b63698059
6 changed files with 183 additions and 82 deletions

View File

@@ -2,6 +2,7 @@ package hardware
import (
"context"
"errors"
"fmt"
goRuntime "runtime"
"sync"
@@ -62,21 +63,25 @@ func (f *FTDIFinder) RegisterPeripheral(ctx context.Context, peripheralData Peri
// If already detected, connect it
if peripheral, ok := f.detected[peripheralData.SerialNumber]; ok {
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusConnecting)
err := peripheral.Connect(ctx)
if err != nil {
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusDisconnected)
log.Err(err).Str("file", "FTDIFinder").Str("peripheralSN", peripheralData.SerialNumber).Msg("unable to connect the peripheral")
return peripheralData.SerialNumber, nil
}
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusDeactivated)
// Peripheral connected, activate it
err = peripheral.Activate(ctx)
if err != nil {
log.Err(err).Str("file", "FTDIFinder").Str("peripheralSN", peripheralData.SerialNumber).Msg("unable to activate the FTDI peripheral")
return peripheralData.SerialNumber, nil
}
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusActivated)
f.wg.Add(1)
go func() {
defer f.wg.Done()
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusConnecting)
err := peripheral.Connect(ctx)
if err != nil {
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusDisconnected)
log.Err(err).Str("file", "FTDIFinder").Str("peripheralSN", peripheralData.SerialNumber).Msg("unable to connect the peripheral")
return
}
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusDeactivated)
// Peripheral connected, activate it
err = peripheral.Activate(ctx)
if err != nil {
log.Err(err).Str("file", "FTDIFinder").Str("peripheralSN", peripheralData.SerialNumber).Msg("unable to activate the FTDI peripheral")
return
}
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusActivated)
}()
}
// Emits the event in the hardware
@@ -233,7 +238,7 @@ func (f *FTDIFinder) scanPeripherals(ctx context.Context) error {
f.detected[sn] = peripheral
if f.onArrival != nil {
go f.onArrival(peripheralData)
f.onArrival(peripheralData)
}
log.Info().Str("sn", sn).Str("name", peripheralData.Name).Msg("[FTDI] New peripheral detected")
@@ -242,20 +247,24 @@ func (f *FTDIFinder) scanPeripherals(ctx context.Context) error {
// If the peripheral is saved in the project => connect
if _, saved := f.saved[sn]; saved {
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusConnecting)
err := peripheral.Connect(ctx)
if err != nil {
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusDisconnected)
log.Err(err).Str("sn", sn).Msg("unable to connect the FTDI peripheral")
return nil
}
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusDeactivated)
err = peripheral.Activate(ctx)
if err != nil {
log.Err(err).Str("sn", sn).Msg("unable to activate the FTDI peripheral")
return nil
}
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusActivated)
f.wg.Add(1)
go func(p PeripheralInfo) {
defer f.wg.Done()
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), p, PeripheralStatusConnecting)
err := peripheral.Connect(ctx)
if err != nil {
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), p, PeripheralStatusDisconnected)
log.Err(err).Str("sn", p.SerialNumber).Msg("unable to connect the FTDI peripheral")
return
}
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), p, PeripheralStatusDeactivated)
err = peripheral.Activate(ctx)
if err != nil {
log.Err(err).Str("sn", p.SerialNumber).Msg("unable to activate the FTDI peripheral")
return
}
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), p, PeripheralStatusActivated)
}(peripheralData)
}
}
}
@@ -265,9 +274,14 @@ func (f *FTDIFinder) scanPeripherals(ctx context.Context) error {
if _, still := currentMap[sn]; !still {
// Properly clean the DMX device
err := oldPeripheral.Disconnect()
err := oldPeripheral.Deactivate(ctx)
if err != nil {
log.Err(err).Str("sn", sn).Msg("unable to clean the FTDI peripheral after disconnection")
log.Err(err).Str("sn", sn).Msg("unable to deactivate the FTDI peripheral after disconnection")
}
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), oldPeripheral.GetInfo(), PeripheralStatusDeactivated)
err = oldPeripheral.Disconnect()
if err != nil {
log.Err(err).Str("sn", sn).Msg("unable to disconnect the FTDI peripheral after disconnection")
}
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), oldPeripheral.GetInfo(), PeripheralStatusDisconnected)
@@ -277,7 +291,7 @@ func (f *FTDIFinder) scanPeripherals(ctx context.Context) error {
// Execute the removal callback
if f.onRemoval != nil {
go f.onRemoval(oldPeripheral.GetInfo())
f.onRemoval(oldPeripheral.GetInfo())
}
}
}
@@ -292,22 +306,22 @@ func (f *FTDIFinder) WaitStop() error {
// close(f.scanChannel)
// Wait for all the peripherals to close
// log.Trace().Str("file", "FTDIFinder").Msg("closing all FTDI peripherals")
// var errs []error
// for registeredPeripheralSN, registeredPeripheral := range f.registeredPeripherals {
// err := registeredPeripheral.WaitStop()
// if err != nil {
// errs = append(errs, fmt.Errorf("%s: %w", registeredPeripheralSN, err))
// }
// }
log.Trace().Str("file", "FTDIFinder").Msg("closing all FTDI peripherals")
var errs []error
for registeredPeripheralSN, registeredPeripheral := range f.detected {
err := registeredPeripheral.WaitStop()
if err != nil {
errs = append(errs, fmt.Errorf("%s: %w", registeredPeripheralSN, err))
}
}
// Wait for goroutines to stop
f.wg.Wait()
// Returning errors
// if len(errs) > 0 {
// return errors.Join(errs...)
// }
if len(errs) > 0 {
return errors.Join(errs...)
}
log.Trace().Str("file", "FTDIFinder").Msg("FTDI finder stopped")
return nil
}