generated from thinkode/modelRepository
38 lines
1.8 KiB
Go
38 lines
1.8 KiB
Go
|
|
package hardware
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"github.com/rs/zerolog/log"
|
||
|
|
)
|
||
|
|
|
||
|
|
// Provider represents how compatible endpoint drivers are implemented
|
||
|
|
type Provider interface {
|
||
|
|
Initialize() error // Initializes the protocol
|
||
|
|
Create(ctx context.Context, endpointInfo EndpointInfo) (EndpointInfo, error) // Manually create a endpoint
|
||
|
|
Remove(ctx context.Context, endpoint Endpoint) error // Manually remove a endpoint
|
||
|
|
OnArrival(cb func(context.Context, Endpoint)) // Callback function when a endpoint arrives
|
||
|
|
OnRemoval(cb func(context.Context, Endpoint)) // Callback function when a endpoint goes away
|
||
|
|
Start(context.Context) error // Start the detection
|
||
|
|
WaitStop() error // Waiting for provider to close
|
||
|
|
GetName() string // Get the name of the provider
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetProvider returns a register provider
|
||
|
|
func (h *Manager) GetProvider(providerName string) (Provider, error) {
|
||
|
|
provider, exists := h.providers[providerName]
|
||
|
|
if !exists {
|
||
|
|
log.Error().Str("file", "hardware").Str("providerName", providerName).Msg("unable to get the provider")
|
||
|
|
return nil, fmt.Errorf("unable to locate the '%s' provider", providerName)
|
||
|
|
}
|
||
|
|
log.Debug().Str("file", "hardware").Str("providerName", providerName).Msg("got provider")
|
||
|
|
return provider, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// RegisterProvider registers a new endpoints provider
|
||
|
|
func (h *Manager) RegisterProvider(provider Provider) {
|
||
|
|
h.providers[provider.GetName()] = provider
|
||
|
|
log.Info().Str("file", "hardware").Str("providerName", provider.GetName()).Msg("provider registered")
|
||
|
|
}
|