resolved created peripheral

This commit is contained in:
2025-11-29 20:06:04 +01:00
parent 2dc6f4a38a
commit d1fda9f075
8 changed files with 112 additions and 42 deletions

View File

@@ -18,26 +18,31 @@ type OS2LFinder struct {
detected map[string]*OS2LPeripheral // The list of saved peripherals
onArrival func(context.Context, Peripheral) // When a peripheral arrives
onRemoval func(context.Context, Peripheral) // When a peripheral goes away
eventBus EventBus // The hardware event bus
onArrival func(context.Context, Peripheral, bool) // When a peripheral arrives
onRemoval func(context.Context, Peripheral) // When a peripheral goes away
}
// NewOS2LFinder creates a new OS2L finder
func NewOS2LFinder() *OS2LFinder {
func NewOS2LFinder(eventBus EventBus) *OS2LFinder {
log.Trace().Str("file", "OS2LFinder").Msg("OS2L finder created")
return &OS2LFinder{
detected: make(map[string]*OS2LPeripheral),
eventBus: eventBus,
}
}
// Initialize initializes the finder
func (f *OS2LFinder) Initialize() error {
// Subscribe to all OS2L peripherals that are added to the project
f.eventBus.SubscribeType(f.GetName(), f)
log.Trace().Str("file", "OS2LFinder").Msg("OS2L finder initialized")
return nil
}
// OnArrival is the callback function when a new peripheral arrives
func (f *OS2LFinder) OnArrival(cb func(context.Context, Peripheral)) {
func (f *OS2LFinder) OnArrival(cb func(context.Context, Peripheral, bool)) {
f.onArrival = cb
}
@@ -47,7 +52,7 @@ func (f *OS2LFinder) OnRemoval(cb func(context.Context, Peripheral)) {
}
// Create creates a new peripheral, based on the peripheral information (manually created)
func (f *OS2LFinder) Create(ctx context.Context, peripheralInfo PeripheralInfo) (string, error) {
func (f *OS2LFinder) Create(ctx context.Context, peripheralInfo PeripheralInfo) error {
// If the SerialNumber is empty, generate another one
if peripheralInfo.SerialNumber == "" {
peripheralInfo.SerialNumber = strings.ToUpper(fmt.Sprintf("%08x", rand.Intn(1<<32)))
@@ -56,7 +61,7 @@ func (f *OS2LFinder) Create(ctx context.Context, peripheralInfo PeripheralInfo)
// Create a new OS2L peripheral
peripheral, err := NewOS2LPeripheral(peripheralInfo)
if err != nil {
return "", fmt.Errorf("unable to create the OS2L peripheral: %w", err)
return fmt.Errorf("unable to create the OS2L peripheral: %w", err)
}
// Set the event callback
@@ -67,9 +72,9 @@ func (f *OS2LFinder) Create(ctx context.Context, peripheralInfo PeripheralInfo)
f.detected[peripheralInfo.SerialNumber] = peripheral
if f.onArrival != nil {
f.onArrival(ctx, peripheral)
f.onArrival(ctx, peripheral, true) // Ask to register the peripheral in the project
}
return peripheralInfo.SerialNumber, err
return err
}
// Remove removes an existing peripheral (manually created)