rework on the peripherals and finders

This commit is contained in:
2025-01-26 12:01:31 +01:00
parent c7fe171cb4
commit 4e0829e821
10 changed files with 360 additions and 344 deletions

View File

@@ -11,14 +11,14 @@ import (
// OS2LFinder represents how the protocol is defined
type OS2LFinder struct {
peripherals map[string]Peripheral // The list of peripherals
registeredPeripherals map[string]OS2LPeripheral // The list of found peripherals
}
// NewOS2LFinder creates a new OS2L finder
func NewOS2LFinder() *OS2LFinder {
log.Trace().Str("file", "OS2LFinder").Msg("OS2L finder created")
return &OS2LFinder{
peripherals: make(map[string]Peripheral),
registeredPeripherals: make(map[string]OS2LPeripheral),
}
}
@@ -28,21 +28,36 @@ func (f *OS2LFinder) Initialize() error {
return nil
}
// CreatePeripheral creates a new OS2L peripheral
func (f *OS2LFinder) CreatePeripheral(ctx context.Context) (Peripheral, error) {
// RegisterPeripheral registers a new peripheral
func (f *OS2LFinder) RegisterPeripheral(ctx context.Context, peripheralData PeripheralInfo) (string, error) {
// Create a random serial number for this peripheral
randomSerialNumber := strings.ToUpper(fmt.Sprintf("%08x", rand.Intn(1<<32)))
log.Trace().Str("file", "OS2LFinder").Str("serialNumber", randomSerialNumber).Msg("OS2L peripheral created")
peripheral := NewOS2LPeripheral("OS2L", randomSerialNumber)
f.peripherals[randomSerialNumber] = peripheral
log.Info().Str("file", "OS2LFinder").Str("serialNumber", randomSerialNumber).Msg("OS2L peripheral created and registered")
return peripheral, nil
peripheralData.SerialNumber = strings.ToUpper(fmt.Sprintf("%08x", rand.Intn(1<<32)))
log.Trace().Str("file", "OS2LFinder").Str("serialNumber", peripheralData.SerialNumber).Msg("OS2L peripheral created")
os2lPeripheral, err := NewOS2LPeripheral(peripheralData)
if err != nil {
return "", fmt.Errorf("unable to create the OS2L peripheral: %v", err)
}
f.registeredPeripherals[peripheralData.SerialNumber] = *os2lPeripheral
log.Trace().Any("periph", &os2lPeripheral).Str("file", "OS2LFinder").Str("peripheralName", peripheralData.Name).Msg("OS2L peripheral has been created")
// Send the change to the front
// runtime.EventsEmit(ctx, string(PeripheralArrival), peripheralData)
return peripheralData.SerialNumber, nil
}
// DeletePeripheral removes an OS2L peripheral
func (f *OS2LFinder) DeletePeripheral(serialNumber string) error {
delete(f.peripherals, serialNumber)
log.Info().Str("file", "OS2LFinder").Str("serialNumber", serialNumber).Msg("OS2L peripheral removed")
// UnregisterPeripheral unregisters an existing peripheral
func (f *OS2LFinder) UnregisterPeripheral(ctx context.Context, peripheralID string) error {
peripheral, registered := f.registeredPeripherals[peripheralID]
if registered {
err := peripheral.Disconnect(ctx)
if err != nil {
return err
}
}
delete(f.registeredPeripherals, peripheralID)
// Send the change to the front
// runtime.EventsEmit(ctx, string(PeripheralRemoval), peripheral.GetInfo())
return nil
}
@@ -51,16 +66,28 @@ func (f *OS2LFinder) GetName() string {
return "OS2L"
}
// GetPeripheral gets the peripheral that correspond to the specified ID
func (f *OS2LFinder) GetPeripheral(peripheralID string) (Peripheral, bool) {
// GetPeripheralSettings gets the peripheral settings
func (f *OS2LFinder) GetPeripheralSettings(peripheralID string) (map[string]interface{}, error) {
// Return the specified peripheral
peripheral, found := f.peripherals[peripheralID]
peripheral, found := f.registeredPeripherals[peripheralID]
if !found {
log.Error().Str("file", "OS2LFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral in the OS2L finder")
return nil, false
log.Error().Str("file", "OS2LFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral from the FTDI finder")
return nil, fmt.Errorf("unable to found the peripheral")
}
log.Trace().Str("file", "OS2LFinder").Str("peripheralID", peripheralID).Msg("OS2L peripheral found in the finder")
return peripheral, true
log.Debug().Str("file", "OS2LFinder").Str("peripheralID", peripheralID).Msg("peripheral found by the FTDI finder")
return peripheral.GetSettings(), nil
}
// SetPeripheralSettings sets the peripheral settings
func (f *OS2LFinder) SetPeripheralSettings(peripheralID string, settings map[string]interface{}) error {
// Return the specified peripheral
peripheral, found := f.registeredPeripherals[peripheralID]
if !found {
log.Error().Str("file", "OS2LFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral from the FTDI finder")
return fmt.Errorf("unable to found the peripheral")
}
log.Debug().Str("file", "OS2LFinder").Str("peripheralID", peripheralID).Msg("peripheral found by the FTDI finder")
return peripheral.SetSettings(settings)
}
// Start starts the finder