generated from thinkode/modelRepository
79 lines
2.4 KiB
Go
79 lines
2.4 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 {
|
||
|
|
peripherals map[string]Peripheral // The list of 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),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Initialize initializes the finder
|
||
|
|
func (f *OS2LFinder) Initialize() error {
|
||
|
|
log.Trace().Str("file", "OS2LFinder").Msg("OS2L finder initialized")
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// CreatePeripheral creates a new OS2L peripheral
|
||
|
|
func (f *OS2LFinder) CreatePeripheral(ctx context.Context) (Peripheral, 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
|
||
|
|
}
|
||
|
|
|
||
|
|
// 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")
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetName returns the name of the driver
|
||
|
|
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) {
|
||
|
|
// Return the specified peripheral
|
||
|
|
peripheral, found := f.peripherals[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.Trace().Str("file", "OS2LFinder").Str("peripheralID", peripheralID).Msg("OS2L peripheral found in the finder")
|
||
|
|
return peripheral, true
|
||
|
|
}
|
||
|
|
|
||
|
|
// 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() {
|
||
|
|
}
|