Adapting interfaces

This commit is contained in:
2025-11-29 17:42:42 +01:00
parent ea46430b71
commit 848d2758d7
11 changed files with 264 additions and 564 deletions

View File

@@ -2,7 +2,6 @@ package hardware
import (
"context"
"errors"
"fmt"
goRuntime "runtime"
"sync"
@@ -10,7 +9,6 @@ import (
"unsafe"
"github.com/rs/zerolog/log"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
/*
@@ -25,13 +23,12 @@ type FTDIFinder struct {
wg sync.WaitGroup
mu sync.Mutex
saved map[string]PeripheralInfo // Peripherals saved in the project
detected map[string]*FTDIPeripheral // Detected peripherals
scanEvery time.Duration // Scans peripherals periodically
onArrival func(p PeripheralInfo) // When a peripheral arrives
onRemoval func(p PeripheralInfo) // When a peripheral goes away
onArrival func(context.Context, Peripheral) // When a peripheral arrives
onRemoval func(context.Context, Peripheral) // When a peripheral goes away
}
// NewFTDIFinder creates a new FTDI finder
@@ -39,79 +36,20 @@ func NewFTDIFinder(scanEvery time.Duration) *FTDIFinder {
log.Trace().Str("file", "FTDIFinder").Msg("FTDI finder created")
return &FTDIFinder{
scanEvery: scanEvery,
saved: make(map[string]PeripheralInfo),
detected: make(map[string]*FTDIPeripheral),
}
}
// OnArrival is the callback function when a new peripheral arrives
func (f *FTDIFinder) OnArrival(cb func(p PeripheralInfo)) {
func (f *FTDIFinder) OnArrival(cb func(context.Context, Peripheral)) {
f.onArrival = cb
}
// OnRemoval i the callback when a peripheral goes away
func (f *FTDIFinder) OnRemoval(cb func(p PeripheralInfo)) {
func (f *FTDIFinder) OnRemoval(cb func(context.Context, Peripheral)) {
f.onRemoval = cb
}
// RegisterPeripheral registers a new peripheral
func (f *FTDIFinder) RegisterPeripheral(ctx context.Context, peripheralData PeripheralInfo) (string, error) {
f.mu.Lock()
defer f.mu.Unlock()
f.saved[peripheralData.SerialNumber] = peripheralData
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusDisconnected)
// If already detected, connect it
if peripheral, ok := f.detected[peripheralData.SerialNumber]; ok {
f.wg.Add(1)
go func() {
defer f.wg.Done()
err := peripheral.Connect(ctx)
if err != nil {
log.Err(err).Str("file", "FTDIFinder").Str("peripheralSN", peripheralData.SerialNumber).Msg("unable to connect the peripheral")
return
}
// 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
}
}()
}
// Emits the event in the hardware
runtime.EventsEmit(ctx, "LOAD_PERIPHERAL", peripheralData)
return peripheralData.SerialNumber, nil
}
// UnregisterPeripheral unregisters an existing peripheral
func (f *FTDIFinder) UnregisterPeripheral(ctx context.Context, peripheralData PeripheralInfo) error {
f.mu.Lock()
defer f.mu.Unlock()
if peripheral, detected := f.detected[peripheralData.SerialNumber]; detected {
// Deactivating peripheral
err := peripheral.Deactivate(ctx)
if err != nil {
log.Err(err).Str("sn", peripheralData.SerialNumber).Msg("unable to deactivate the peripheral")
return nil
}
// Disconnecting peripheral
err = peripheral.Disconnect(ctx)
if err != nil {
log.Err(err).Str("sn", peripheralData.SerialNumber).Msg("unable to disconnect the peripheral")
return nil
}
}
delete(f.saved, peripheralData.SerialNumber)
runtime.EventsEmit(ctx, "UNLOAD_PERIPHERAL", peripheralData)
return nil
}
// Initialize initializes the FTDI finder
func (f *FTDIFinder) Initialize() error {
// Check platform
@@ -123,6 +61,16 @@ func (f *FTDIFinder) Initialize() error {
return nil
}
// Create creates a new peripheral, based on the peripheral information (manually created)
func (f *FTDIFinder) Create(ctx context.Context, peripheralInfo PeripheralInfo) (string, error) {
return "", nil
}
// Remove removes an existing peripheral (manually created)
func (f *FTDIFinder) Remove(ctx context.Context, peripheral Peripheral) error {
return nil
}
// Start starts the finder and search for peripherals
func (f *FTDIFinder) Start(ctx context.Context) error {
f.wg.Add(1)
@@ -147,45 +95,11 @@ func (f *FTDIFinder) Start(ctx context.Context) error {
return nil
}
// ForceScan explicitly asks for scanning peripherals
func (f *FTDIFinder) ForceScan() {
// select {
// case f.scanChannel <- struct{}{}:
// default:
// // Ignore if the channel is full or if it is closed
// }
}
// GetName returns the name of the driver
func (f *FTDIFinder) GetName() string {
return "FTDI"
}
// GetPeripheralSettings gets the peripheral settings
func (f *FTDIFinder) GetPeripheralSettings(peripheralID string) (map[string]interface{}, error) {
// Return the specified peripheral
peripheral, found := f.detected[peripheralID]
if !found {
// FTDI not detected, return the last settings saved
if savedPeripheral, isFound := f.saved[peripheralID]; isFound {
return savedPeripheral.Settings, nil
}
return nil, fmt.Errorf("unable to found the peripheral")
}
return peripheral.GetSettings(), nil
}
// SetPeripheralSettings sets the peripheral settings
func (f *FTDIFinder) SetPeripheralSettings(ctx context.Context, peripheralID string, settings map[string]any) error {
// Return the specified peripheral
peripheral, found := f.detected[peripheralID]
if !found {
return fmt.Errorf("unable to found the FTDI peripheral")
}
log.Debug().Str("file", "FTDIFinder").Str("peripheralID", peripheralID).Msg("peripheral found by the FTDI finder")
return peripheral.SetSettings(settings)
}
// scanPeripherals scans the FTDI peripherals
func (f *FTDIFinder) scanPeripherals(ctx context.Context) error {
log.Trace().Str("file", "FTDIFinder").Msg("FTDI scan triggered")
@@ -227,56 +141,27 @@ func (f *FTDIFinder) scanPeripherals(ctx context.Context) error {
// Detect arrivals
for sn, peripheralData := range currentMap {
// If the scanned peripheral isn't in the detected list, create it
if _, known := f.detected[sn]; !known {
peripheral := NewFTDIPeripheral(peripheralData)
f.detected[sn] = peripheral
if f.onArrival != nil {
f.onArrival(peripheralData)
}
log.Info().Str("sn", sn).Str("name", peripheralData.Name).Msg("[FTDI] New peripheral detected")
// If the peripheral is saved in the project => connect
if _, saved := f.saved[sn]; saved {
f.wg.Add(1)
go func(p PeripheralInfo) {
defer f.wg.Done()
err := peripheral.Connect(ctx)
if err != nil {
log.Err(err).Str("sn", p.SerialNumber).Msg("unable to connect the FTDI peripheral")
return
}
err = peripheral.Activate(ctx)
if err != nil {
log.Err(err).Str("sn", p.SerialNumber).Msg("unable to activate the FTDI peripheral")
return
}
}(peripheralData)
f.onArrival(ctx, peripheral)
}
}
}
// Detect removals
for sn, oldPeripheral := range f.detected {
if _, still := currentMap[sn]; !still {
// Properly clean the DMX device
err := oldPeripheral.Deactivate(ctx)
if err != nil {
log.Err(err).Str("sn", sn).Msg("unable to deactivate the FTDI peripheral after disconnection")
}
err = oldPeripheral.Disconnect(ctx)
if err != nil {
log.Err(err).Str("sn", sn).Msg("unable to disconnect the FTDI peripheral after disconnection")
}
for detectedSN, detectedPeripheral := range f.detected {
if _, still := currentMap[detectedSN]; !still {
// Delete it from the detected list
delete(f.detected, sn)
log.Info().Str("sn", sn).Str("name", oldPeripheral.GetInfo().Name).Msg("[FTDI] peripheral removed")
delete(f.detected, detectedSN)
// Execute the removal callback
if f.onRemoval != nil {
f.onRemoval(oldPeripheral.GetInfo())
f.onRemoval(ctx, detectedPeripheral)
}
}
}
@@ -287,26 +172,9 @@ func (f *FTDIFinder) scanPeripherals(ctx context.Context) error {
func (f *FTDIFinder) WaitStop() error {
log.Trace().Str("file", "FTDIFinder").Msg("stopping the FTDI finder...")
// Close the channel
// 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.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...)
}
log.Trace().Str("file", "FTDIFinder").Msg("FTDI finder stopped")
return nil
}