diff --git a/app.go b/app.go index b6f6736..2eb3264 100644 --- a/app.go +++ b/app.go @@ -5,7 +5,6 @@ import ( "dmxconnect/hardware" "fmt" "io" - "time" "github.com/rs/zerolog/log" @@ -20,20 +19,16 @@ type App struct { cancelFunc context.CancelFunc wait sync.WaitGroup - hardwareManager *hardware.HardwareManager // For managing all the hardware - wmiMutex sync.Mutex // Avoid some WMI operations at the same time - projectInfo ProjectInfo // The project information structure - projectSave string // The file name of the project + hardwareManager *hardware.Manager // For managing all the hardware + wmiMutex sync.Mutex // Avoid some WMI operations at the same time + projectInfo ProjectInfo // The project information structure + projectSave string // The file name of the project } // NewApp creates a new App application struct func NewApp() *App { // Create a new hadware manager - hardwareManager := hardware.NewHardwareManager() - hardwareManager.RegisterFinder(hardware.NewFTDIFinder(3 * time.Second)) - hardwareManager.RegisterFinder(hardware.NewOS2LFinder()) - hardwareManager.RegisterFinder(hardware.NewMIDIFinder(3 * time.Second)) - + hardwareManager := hardware.NewManager() return &App{ hardwareManager: hardwareManager, projectSave: "", diff --git a/hardware/hardware.go b/hardware/hardware.go index 81ba457..d581306 100644 --- a/hardware/hardware.go +++ b/hardware/hardware.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "sync" + "time" "github.com/rs/zerolog/log" "github.com/wailsapp/wails/v2/pkg/runtime" @@ -35,28 +36,32 @@ const ( PeripheralStatusActivated PeripheralStatus = "PERIPHERAL_ACTIVATED" ) -// HardwareManager is the class who manages the hardware -type HardwareManager struct { +// Manager is the class who manages the hardware +type Manager struct { wg sync.WaitGroup finders map[string]PeripheralFinder // The map of peripherals finders - peripherals []Peripheral // The current list of peripherals + peripherals []*Peripheral // The current list of peripherals peripheralsScanTrigger chan struct{} // Trigger the peripherals scans } -// NewHardwareManager creates a new HardwareManager -func NewHardwareManager() *HardwareManager { +// NewManager creates a new hardware manager +func NewManager() *Manager { log.Trace().Str("package", "hardware").Msg("Hardware instance created") - return &HardwareManager{ + return &Manager{ finders: make(map[string]PeripheralFinder), - peripherals: make([]Peripheral, 0), + peripherals: make([]*Peripheral, 0), peripheralsScanTrigger: make(chan struct{}), } } // Start starts to find new peripheral events -func (h *HardwareManager) Start(ctx context.Context) error { +func (h *Manager) Start(ctx context.Context) error { // Initialize all the finders and their callback functions + h.RegisterFinder(NewFTDIFinder(3 * time.Second)) + h.RegisterFinder(NewOS2LFinder()) + h.RegisterFinder(NewMIDIFinder(3 * time.Second)) + for finderName, finder := range h.finders { err := finder.Initialize() if err != nil { @@ -96,7 +101,7 @@ func (h *HardwareManager) Start(ctx context.Context) error { } // GetFinder returns a register finder -func (h *HardwareManager) GetFinder(finderName string) (PeripheralFinder, error) { +func (h *Manager) GetFinder(finderName string) (PeripheralFinder, error) { finder, exists := h.finders[finderName] if !exists { log.Error().Str("file", "hardware").Str("finderName", finderName).Msg("unable to get the finder") @@ -107,13 +112,13 @@ func (h *HardwareManager) GetFinder(finderName string) (PeripheralFinder, error) } // RegisterFinder registers a new peripherals finder -func (h *HardwareManager) RegisterFinder(finder PeripheralFinder) { +func (h *Manager) RegisterFinder(finder PeripheralFinder) { h.finders[finder.GetName()] = finder log.Info().Str("file", "hardware").Str("finderName", finder.GetName()).Msg("finder registered") } // Scan scans all the peripherals for the registered finders -func (h *HardwareManager) Scan() error { +func (h *Manager) Scan() error { select { case h.peripheralsScanTrigger <- struct{}{}: return nil @@ -123,7 +128,7 @@ func (h *HardwareManager) Scan() error { } // WaitStop stops the hardware manager -func (h *HardwareManager) WaitStop() error { +func (h *Manager) WaitStop() error { log.Trace().Str("file", "hardware").Msg("closing the hardware manager") // Closing trigger channel