generated from thinkode/modelRepository
Adapting interfaces
This commit is contained in:
@@ -2,7 +2,6 @@ package hardware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
@@ -11,7 +10,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/wailsapp/wails/v2/pkg/runtime"
|
||||
"gitlab.com/gomidi/rtmididrv"
|
||||
)
|
||||
|
||||
@@ -20,13 +18,12 @@ type MIDIFinder struct {
|
||||
wg sync.WaitGroup
|
||||
mu sync.Mutex
|
||||
|
||||
saved map[string]PeripheralInfo // Peripherals saved in the project
|
||||
detected map[string]*MIDIPeripheral // Detected peripherals
|
||||
|
||||
scanEvery time.Duration // Scans peripherals periodically
|
||||
|
||||
onArrival func(p PeripheralInfo) // When a peripheral arrives
|
||||
onRemoval func(p PeripheralInfo) // When a peripheral goes away
|
||||
onArrival func(context.Context, Peripheral) // When a peripheral arrives
|
||||
onRemoval func(context.Context, Peripheral) // When a peripheral goes away
|
||||
}
|
||||
|
||||
// NewMIDIFinder creates a new MIDI finder
|
||||
@@ -34,18 +31,17 @@ func NewMIDIFinder(scanEvery time.Duration) *MIDIFinder {
|
||||
log.Trace().Str("file", "MIDIFinder").Msg("MIDI finder created")
|
||||
return &MIDIFinder{
|
||||
scanEvery: scanEvery,
|
||||
saved: make(map[string]PeripheralInfo),
|
||||
detected: make(map[string]*MIDIPeripheral),
|
||||
}
|
||||
}
|
||||
|
||||
// OnArrival is the callback function when a new peripheral arrives
|
||||
func (f *MIDIFinder) OnArrival(cb func(p PeripheralInfo)) {
|
||||
func (f *MIDIFinder) OnArrival(cb func(context.Context, Peripheral)) {
|
||||
f.onArrival = cb
|
||||
}
|
||||
|
||||
// OnRemoval i the callback when a peripheral goes away
|
||||
func (f *MIDIFinder) OnRemoval(cb func(p PeripheralInfo)) {
|
||||
func (f *MIDIFinder) OnRemoval(cb func(context.Context, Peripheral)) {
|
||||
f.onRemoval = cb
|
||||
}
|
||||
|
||||
@@ -55,59 +51,13 @@ func (f *MIDIFinder) Initialize() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// RegisterPeripheral registers a new peripheral
|
||||
func (f *MIDIFinder) RegisterPeripheral(ctx context.Context, peripheralData PeripheralInfo) (string, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
|
||||
f.saved[peripheralData.SerialNumber] = peripheralData
|
||||
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusDisconnected)
|
||||
|
||||
// If already detected, connect it
|
||||
if peripheral, ok := f.detected[peripheralData.SerialNumber]; ok {
|
||||
f.wg.Add(1)
|
||||
go func() {
|
||||
defer f.wg.Done()
|
||||
err := peripheral.Connect(ctx)
|
||||
if err != nil {
|
||||
log.Err(err).Str("file", "FTDIFinder").Str("peripheralSN", peripheralData.SerialNumber).Msg("unable to connect the peripheral")
|
||||
return
|
||||
}
|
||||
// Peripheral connected, activate it
|
||||
err = peripheral.Activate(ctx)
|
||||
if err != nil {
|
||||
log.Err(err).Str("file", "FTDIFinder").Str("peripheralSN", peripheralData.SerialNumber).Msg("unable to activate the FTDI peripheral")
|
||||
return
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// Emits the event in the hardware
|
||||
runtime.EventsEmit(ctx, "LOAD_PERIPHERAL", peripheralData)
|
||||
return peripheralData.SerialNumber, nil
|
||||
// Create creates a new peripheral, based on the peripheral information (manually created)
|
||||
func (f *MIDIFinder) Create(ctx context.Context, peripheralInfo PeripheralInfo) (string, error) {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
// UnregisterPeripheral unregisters an existing peripheral
|
||||
func (f *MIDIFinder) UnregisterPeripheral(ctx context.Context, peripheralData PeripheralInfo) error {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
|
||||
if peripheral, detected := f.detected[peripheralData.SerialNumber]; detected {
|
||||
// Deactivating peripheral
|
||||
err := peripheral.Deactivate(ctx)
|
||||
if err != nil {
|
||||
log.Err(err).Str("sn", peripheralData.SerialNumber).Msg("unable to deactivate the peripheral")
|
||||
return nil
|
||||
}
|
||||
// Disconnecting peripheral
|
||||
err = peripheral.Disconnect(ctx)
|
||||
if err != nil {
|
||||
log.Err(err).Str("sn", peripheralData.SerialNumber).Msg("unable to disconnect the peripheral")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
delete(f.saved, peripheralData.SerialNumber)
|
||||
runtime.EventsEmit(ctx, "UNLOAD_PERIPHERAL", peripheralData)
|
||||
// Remove removes an existing peripheral (manually created)
|
||||
func (f *MIDIFinder) Remove(ctx context.Context, peripheral Peripheral) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -139,26 +89,9 @@ func (f *MIDIFinder) Start(ctx context.Context) error {
|
||||
func (f *MIDIFinder) WaitStop() error {
|
||||
log.Trace().Str("file", "MIDIFinder").Msg("stopping the MIDI finder...")
|
||||
|
||||
// Close the channel
|
||||
// close(f.scanChannel)
|
||||
|
||||
// Wait for all the peripherals to close
|
||||
log.Trace().Str("file", "MIDIFinder").Msg("closing all MIDI peripherals")
|
||||
var errs []error
|
||||
for registeredPeripheralSN, registeredPeripheral := range f.detected {
|
||||
err := registeredPeripheral.WaitStop()
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Errorf("%s: %w", registeredPeripheralSN, err))
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for goroutines to stop
|
||||
f.wg.Wait()
|
||||
|
||||
// Returning errors
|
||||
if len(errs) > 0 {
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
log.Trace().Str("file", "MIDIFinder").Msg("MIDI finder stopped")
|
||||
return nil
|
||||
}
|
||||
@@ -168,31 +101,6 @@ func (f *MIDIFinder) GetName() string {
|
||||
return "MIDI"
|
||||
}
|
||||
|
||||
// GetPeripheralSettings gets the peripheral settings
|
||||
func (f *MIDIFinder) GetPeripheralSettings(peripheralID string) (map[string]interface{}, error) {
|
||||
// Return the specified peripheral
|
||||
peripheral, found := f.detected[peripheralID]
|
||||
if !found {
|
||||
// FTDI not detected, return the last settings saved
|
||||
if savedPeripheral, isFound := f.saved[peripheralID]; isFound {
|
||||
return savedPeripheral.Settings, nil
|
||||
}
|
||||
return nil, fmt.Errorf("unable to found the peripheral")
|
||||
}
|
||||
return peripheral.GetSettings(), nil
|
||||
}
|
||||
|
||||
// SetPeripheralSettings sets the peripheral settings
|
||||
func (f *MIDIFinder) SetPeripheralSettings(ctx context.Context, peripheralID string, settings map[string]interface{}) error {
|
||||
// Return the specified peripheral
|
||||
peripheral, found := f.detected[peripheralID]
|
||||
if !found {
|
||||
return fmt.Errorf("unable to found the MIDI peripheral")
|
||||
}
|
||||
log.Debug().Str("file", "MIDIFinder").Str("peripheralID", peripheralID).Msg("peripheral found by the MIDI finder")
|
||||
return peripheral.SetSettings(settings)
|
||||
}
|
||||
|
||||
func splitStringAndNumber(input string) (string, int, error) {
|
||||
// Regular expression to match the text part and the number at the end
|
||||
re := regexp.MustCompile(`^(.*?)(\d+)$`)
|
||||
@@ -214,11 +122,6 @@ func splitStringAndNumber(input string) (string, int, error) {
|
||||
return "", 0, fmt.Errorf("no number found at the end of the string")
|
||||
}
|
||||
|
||||
// ForceScan explicily asks for scanning peripherals
|
||||
func (f *MIDIFinder) ForceScan() {
|
||||
// f.scanChannel <- struct{}{}
|
||||
}
|
||||
|
||||
// scanPeripherals scans the MIDI peripherals
|
||||
func (f *MIDIFinder) scanPeripherals(ctx context.Context) error {
|
||||
currentMap := make(map[string]*MIDIPeripheral)
|
||||
@@ -298,26 +201,7 @@ func (f *MIDIFinder) scanPeripherals(ctx context.Context) error {
|
||||
f.detected[sn] = peripheral
|
||||
|
||||
if f.onArrival != nil {
|
||||
f.onArrival(discovery.GetInfo())
|
||||
}
|
||||
log.Info().Str("sn", sn).Str("name", discovery.GetInfo().SerialNumber).Msg("[MIDI] New peripheral detected")
|
||||
|
||||
// If the peripheral is saved in the project => connect
|
||||
if _, saved := f.saved[sn]; saved {
|
||||
f.wg.Add(1)
|
||||
go func(p PeripheralInfo) {
|
||||
defer f.wg.Done()
|
||||
err := peripheral.Connect(ctx)
|
||||
if err != nil {
|
||||
log.Err(err).Str("sn", p.SerialNumber).Msg("unable to connect the MIDI peripheral")
|
||||
return
|
||||
}
|
||||
err = peripheral.Activate(ctx)
|
||||
if err != nil {
|
||||
log.Err(err).Str("sn", p.SerialNumber).Msg("unable to activate the MIDI peripheral")
|
||||
return
|
||||
}
|
||||
}(discovery.GetInfo())
|
||||
f.onArrival(ctx, discovery)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -326,24 +210,15 @@ func (f *MIDIFinder) scanPeripherals(ctx context.Context) error {
|
||||
for sn, oldPeripheral := range f.detected {
|
||||
if _, still := currentMap[sn]; !still {
|
||||
|
||||
// Properly clean the DMX device
|
||||
err := oldPeripheral.Deactivate(ctx)
|
||||
if err != nil {
|
||||
log.Err(err).Str("sn", sn).Msg("unable to deactivate the MIDI peripheral after disconnection")
|
||||
}
|
||||
err = oldPeripheral.Disconnect(ctx)
|
||||
if err != nil {
|
||||
log.Err(err).Str("sn", sn).Msg("unable to disconnect the MIDI peripheral after disconnection")
|
||||
}
|
||||
|
||||
// Delete it from the detected list
|
||||
delete(f.detected, sn)
|
||||
log.Info().Str("sn", sn).Str("name", oldPeripheral.GetInfo().Name).Msg("[MIDI] peripheral removed")
|
||||
|
||||
// Execute the removal callback
|
||||
if f.onRemoval != nil {
|
||||
f.onRemoval(oldPeripheral.GetInfo())
|
||||
f.onRemoval(ctx, oldPeripheral)
|
||||
}
|
||||
|
||||
// Delete it from the detected list
|
||||
delete(f.detected, sn)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
@@ -370,17 +245,3 @@ func normalizeName(raw string) string {
|
||||
|
||||
return strings.Join(parts, " ")
|
||||
}
|
||||
|
||||
// func normalizeName(name string) string {
|
||||
// // Cas communs : "MIDIIN2 (XXX)" → "XXX"
|
||||
// if strings.Contains(name, "(") && strings.Contains(name, ")") {
|
||||
// start := strings.Index(name, "(")
|
||||
// end := strings.Index(name, ")")
|
||||
// if start < end {
|
||||
// return strings.TrimSpace(name[start+1 : end])
|
||||
// }
|
||||
// }
|
||||
|
||||
// // Sinon, on retourne tel quel
|
||||
// return strings.TrimSpace(name)
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user