generated from thinkode/modelRepository
106 lines
3.6 KiB
Go
106 lines
3.6 KiB
Go
package hardware
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"math/rand"
|
|
"strings"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
// OS2LFinder represents how the protocol is defined
|
|
type OS2LFinder struct {
|
|
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{
|
|
registeredPeripherals: make(map[string]OS2LPeripheral),
|
|
}
|
|
}
|
|
|
|
// Initialize initializes the finder
|
|
func (f *OS2LFinder) Initialize() error {
|
|
log.Trace().Str("file", "OS2LFinder").Msg("OS2L finder initialized")
|
|
return nil
|
|
}
|
|
|
|
// RegisterPeripheral registers a new peripheral
|
|
func (f *OS2LFinder) RegisterPeripheral(ctx context.Context, peripheralData PeripheralInfo) (string, error) {
|
|
// Create a random serial number for this peripheral
|
|
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
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// GetName returns the name of the driver
|
|
func (f *OS2LFinder) GetName() string {
|
|
return "OS2L"
|
|
}
|
|
|
|
// GetPeripheralSettings gets the peripheral settings
|
|
func (f *OS2LFinder) GetPeripheralSettings(peripheralID string) (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 nil, fmt.Errorf("unable to found the peripheral")
|
|
}
|
|
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
|
|
func (f *OS2LFinder) Start(ctx context.Context) error {
|
|
return nil
|
|
}
|
|
|
|
// Stop stops this finder
|
|
func (f *OS2LFinder) Stop() error {
|
|
return nil
|
|
}
|
|
|
|
// ForceScan scans the interfaces (not implemented)
|
|
func (f *OS2LFinder) ForceScan() {
|
|
}
|