generated from thinkode/modelRepository
resolved created peripheral
This commit is contained in:
@@ -40,6 +40,51 @@ const (
|
||||
PeripheralStatusActivated PeripheralStatus = "PERIPHERAL_ACTIVATED"
|
||||
)
|
||||
|
||||
// EventBus handles events between hardware and finders
|
||||
type EventBus struct {
|
||||
register map[string][]PeripheralFinder // Register is a map[peripheralType (string)][]Finder
|
||||
}
|
||||
|
||||
// SubscribeType is called from finders that want to subscribe for a peripheral loaded in project
|
||||
func (b *EventBus) SubscribeType(protocolName string, finder PeripheralFinder) {
|
||||
// Check if we need to initialize the slice (key not found)
|
||||
if _, found := b.register[protocolName]; !found {
|
||||
b.register[protocolName] = make([]PeripheralFinder, 0)
|
||||
}
|
||||
// Add the finder to the list of subscription
|
||||
b.register[protocolName] = append(b.register[protocolName], finder)
|
||||
}
|
||||
|
||||
// EmitSavedPeripheral emits a peripheral loaded event for all the finders suscribed to the peripheral type
|
||||
func (b *EventBus) EmitSavedPeripheral(ctx context.Context, peripheralInfo PeripheralInfo) (string, error) {
|
||||
finders, found := b.register[peripheralInfo.ProtocolName]
|
||||
if !found {
|
||||
return "", nil
|
||||
}
|
||||
for _, finder := range finders {
|
||||
err := finder.Create(ctx, peripheralInfo)
|
||||
if err != nil {
|
||||
log.Err(err).Str("finder", finder.GetName()).Str("S/N", peripheralInfo.SerialNumber).Msg("unable to create the peripheral")
|
||||
}
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
// EmitUnsavedPeripheral emits a peripheral unloaded event for all the finders suscribed to the peripheral type
|
||||
func (b *EventBus) EmitUnsavedPeripheral(ctx context.Context, peripheral Peripheral) (string, error) {
|
||||
finders, found := b.register[peripheral.GetInfo().ProtocolName]
|
||||
if !found {
|
||||
return "", nil
|
||||
}
|
||||
for _, finder := range finders {
|
||||
err := finder.Remove(ctx, peripheral)
|
||||
if err != nil {
|
||||
log.Err(err).Str("finder", finder.GetName()).Str("S/N", peripheral.GetInfo().SerialNumber).Msg("unable to remove the peripheral")
|
||||
}
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
// Manager is the class who manages the hardware
|
||||
type Manager struct {
|
||||
mu sync.Mutex
|
||||
@@ -48,6 +93,8 @@ type Manager struct {
|
||||
finders map[string]PeripheralFinder // The map of peripherals finders
|
||||
peripherals map[string]Peripheral // The current list of peripherals
|
||||
savedPeripherals map[string]PeripheralInfo // The list of stored peripherals
|
||||
|
||||
eventBus EventBus // The hardware event bus
|
||||
}
|
||||
|
||||
// NewManager creates a new hardware manager
|
||||
@@ -57,14 +104,28 @@ func NewManager() *Manager {
|
||||
finders: make(map[string]PeripheralFinder),
|
||||
peripherals: make(map[string]Peripheral, 0),
|
||||
savedPeripherals: make(map[string]PeripheralInfo, 0),
|
||||
eventBus: EventBus{
|
||||
make(map[string][]PeripheralFinder),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// CreatePeripheral asks the providers to create a new peripheral
|
||||
func (h *Manager) CreatePeripheral(ctx context.Context, peripheralInfo PeripheralInfo) {
|
||||
// Emit an event to the hardware event bus
|
||||
h.eventBus.EmitSavedPeripheral(ctx, peripheralInfo)
|
||||
}
|
||||
|
||||
// RegisterPeripheral registers a new peripheral
|
||||
func (h *Manager) RegisterPeripheral(ctx context.Context, peripheralData PeripheralInfo) (string, error) {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
|
||||
// Do not save if the peripheral doesn't have a S/N
|
||||
if peripheralData.SerialNumber == "" {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
h.savedPeripherals[peripheralData.SerialNumber] = peripheralData
|
||||
|
||||
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusDisconnected)
|
||||
@@ -112,6 +173,8 @@ func (h *Manager) UnregisterPeripheral(ctx context.Context, peripheralData Perip
|
||||
log.Err(err).Str("sn", peripheralData.SerialNumber).Msg("unable to disconnect the peripheral")
|
||||
return nil
|
||||
}
|
||||
// Emit the unload event
|
||||
h.eventBus.EmitUnsavedPeripheral(ctx, peripheral)
|
||||
}
|
||||
delete(h.savedPeripherals, peripheralData.SerialNumber)
|
||||
runtime.EventsEmit(ctx, string(PeripheralUnload), peripheralData)
|
||||
@@ -147,7 +210,7 @@ func (h *Manager) Start(ctx context.Context) error {
|
||||
|
||||
// Register all the finders to use as hardware scanners
|
||||
h.RegisterFinder(NewFTDIFinder(3 * time.Second))
|
||||
h.RegisterFinder(NewOS2LFinder())
|
||||
h.RegisterFinder(NewOS2LFinder(h.eventBus))
|
||||
h.RegisterFinder(NewMIDIFinder(3 * time.Second))
|
||||
|
||||
for finderName, finder := range h.finders {
|
||||
@@ -174,7 +237,12 @@ func (h *Manager) Start(ctx context.Context) error {
|
||||
}
|
||||
|
||||
// OnPeripheralArrival is called when a peripheral arrives in the system
|
||||
func (h *Manager) OnPeripheralArrival(ctx context.Context, peripheral Peripheral) {
|
||||
func (h *Manager) OnPeripheralArrival(ctx context.Context, peripheral Peripheral, needSave bool) {
|
||||
// Save the peripheral in the project if needed
|
||||
if needSave {
|
||||
h.RegisterPeripheral(ctx, peripheral.GetInfo())
|
||||
}
|
||||
|
||||
// Add the peripheral to the detected hardware
|
||||
h.peripherals[peripheral.GetInfo().SerialNumber] = peripheral
|
||||
|
||||
|
||||
Reference in New Issue
Block a user