rework on the peripherals and finders

This commit is contained in:
2025-01-26 12:01:31 +01:00
parent c7fe171cb4
commit 4e0829e821
10 changed files with 360 additions and 344 deletions

View File

@@ -93,19 +93,19 @@ func (h *HardwareManager) RegisterFinder(finder PeripheralFinder) {
log.Info().Str("file", "hardware").Str("finderName", finder.GetName()).Msg("finder registered")
}
// GetPeripheral gets the peripheral object from the parent finder
func (h *HardwareManager) GetPeripheral(finderName string, peripheralID string) (Peripheral, bool) {
// Get the finder
parentFinder, found := h.finders[finderName]
// If no finder found, return false
if !found {
log.Error().Str("file", "hardware").Str("finderName", finderName).Msg("unable to get the finder")
return nil, false
}
log.Trace().Str("file", "hardware").Str("finderName", parentFinder.GetName()).Msg("finder got")
// Contact the finder to get the peripheral
return parentFinder.GetPeripheral(peripheralID)
}
// // GetPeripheral gets the peripheral object from the parent finder
// func (h *HardwareManager) GetPeripheral(finderName string, peripheralID string) (Peripheral, bool) {
// // Get the finder
// parentFinder, found := h.finders[finderName]
// // If no finder found, return false
// if !found {
// log.Error().Str("file", "hardware").Str("finderName", finderName).Msg("unable to get the finder")
// return nil, false
// }
// log.Trace().Str("file", "hardware").Str("finderName", parentFinder.GetName()).Msg("finder got")
// // Contact the finder to get the peripheral
// return parentFinder.GetPeripheral(peripheralID)
// }
// Scan scans all the peripherals for the registered finders
func (h *HardwareManager) Scan() error {
@@ -130,13 +130,13 @@ func (h *HardwareManager) Stop() error {
}
// emitPeripheralsChanges compares the old and new peripherals to determine which ones have been added or removed.
func emitPeripheralsChanges(ctx context.Context, oldPeripherals map[string]Peripheral, newPeripherals map[string]Peripheral) {
func emitPeripheralsChanges(ctx context.Context, oldPeripherals map[string]PeripheralInfo, newPeripherals map[string]PeripheralInfo) {
log.Trace().Any("oldList", oldPeripherals).Any("newList", newPeripherals).Msg("emitting peripherals changes to the front")
// Identify removed peripherals: present in the old list but not in the new list
for oldPeriphName := range oldPeripherals {
if _, exists := newPeripherals[oldPeriphName]; !exists {
runtime.EventsEmit(ctx, string(PeripheralRemoval), oldPeripherals[oldPeriphName].GetInfo())
runtime.EventsEmit(ctx, string(PeripheralRemoval), oldPeripherals[oldPeriphName])
log.Trace().Str("file", "hardware").Str("event", string(PeripheralRemoval)).Msg("emit peripheral removal event")
}
}
@@ -144,7 +144,7 @@ func emitPeripheralsChanges(ctx context.Context, oldPeripherals map[string]Perip
// Identify added peripherals: present in the new list but not in the old list
for newPeriphName := range newPeripherals {
if _, exists := oldPeripherals[newPeriphName]; !exists {
runtime.EventsEmit(ctx, string(PeripheralArrival), newPeripherals[newPeriphName].GetInfo())
runtime.EventsEmit(ctx, string(PeripheralArrival), newPeripherals[newPeriphName])
log.Trace().Str("file", "hardware").Str("event", string(PeripheralArrival)).Msg("emit peripheral arrival event")
}
}