From 4e0829e821de441d79a85a90d91257210b7fb056 Mon Sep 17 00:00:00 2001 From: Valentin Boulanger Date: Sun, 26 Jan 2025 12:01:31 +0100 Subject: [PATCH] rework on the peripherals and finders --- .../Settings/InputsOutputsContent.svelte | 72 +------ hardware/FTDIFinder.go | 106 ++++++---- hardware/FTDIPeripheral.go | 80 ++++--- hardware/MIDIFinder.go | 70 +++++-- hardware/MIDIPeripheral.go | 30 +-- hardware/OS2LFinder.go | 69 ++++-- hardware/OS2LPeripheral.go | 30 ++- hardware/hardware.go | 32 +-- hardware/interfaces.go | 19 +- peripherals.go | 196 +++++++++--------- 10 files changed, 360 insertions(+), 344 deletions(-) diff --git a/frontend/src/components/Settings/InputsOutputsContent.svelte b/frontend/src/components/Settings/InputsOutputsContent.svelte index 415e380..c629c44 100644 --- a/frontend/src/components/Settings/InputsOutputsContent.svelte +++ b/frontend/src/components/Settings/InputsOutputsContent.svelte @@ -5,59 +5,20 @@ import { t, _ } from 'svelte-i18n' import { generateToast, needProjectSave, peripherals } from "../../stores"; import { get } from "svelte/store" - import { UpdatePeripheralSettings, GetPeripheralSettings, AddOS2LPeripheral, RemovePeripheral, ConnectFTDI, ActivateFTDI, DeactivateFTDI, DisconnectFTDI, SetDeviceFTDI, AddPeripheral } from "../../../wailsjs/go/main/App"; + import { UpdatePeripheralSettings, GetPeripheralSettings, RemovePeripheral, AddPeripheral } from "../../../wailsjs/go/main/App"; import RoundedButton from "../General/RoundedButton.svelte"; - function ftdiConnect(){ - ConnectFTDI().then(() => - console.log("FTDI connected")) - .catch((error) => { - console.log("Error when trying to connect: " + error) - }) - } - - function ftdiActivate(){ - ActivateFTDI().then(() => - console.log("FTDI activated")) - .catch((error) => { - console.log("Error when trying to activate: " + error) - }) - } - - function ftdiDeactivate(){ - DeactivateFTDI().then(() => - console.log("FTDI deactivated")) - .catch((error) => { - console.log("Error when trying to deactivate: " + error) - }) - } - - let sliderValue = 0 - function ftdiSetDevice(value){ - console.log("value is " + value) - SetDeviceFTDI(value).then(() => - console.log("FTDI device set up")) - .catch((error) => { - console.log("Error when trying to set the device: " + error) - }) - } - - function ftdiDisconnect(){ - DisconnectFTDI().then(() => - console.log("FTDI disconnected")) - .catch((error) => { - console.log("Error when trying to disconnect: " + error) - }) - } - // Add the peripheral to the project function addPeripheral(peripheral){ // Add the peripheral to the project (backend) - AddPeripheral(peripheral.ProtocolName, peripheral.SerialNumber).then(() => { + AddPeripheral(peripheral).then((serialNumber) => { + peripheral.SerialNumber = serialNumber peripherals.update((value) => { - if (value[peripheral.SerialNumber]) { - value[peripheral.SerialNumber].isSaved = true; + // If the peripheral doesn't exists yet, create it + if (!(peripheral.SerialNumber in value)) { + value[peripheral.SerialNumber] = peripheral } + value[peripheral.SerialNumber].isSaved = true; return {...value} }) $needProjectSave = true @@ -96,23 +57,6 @@ }) } - // Create the OS2L peripheral - function createOS2L(){ - AddOS2LPeripheral().then(os2lDevice => { - peripherals.update(currentPeriph => { - os2lDevice.isSaved = true - os2lDevice.isDetected = true - currentPeriph[os2lDevice.SerialNumber] = os2lDevice - return {...currentPeriph} - }) - $needProjectSave = true - generateToast('info', 'bx-signal-5', $_("os2lPeripheralCreatedToast")) - }).catch(error => { - console.log("Unable to add the OS2L peripheral: " + error) - generateToast('danger', 'bx-error', $_("os2lPeripheralCreateErrorToast")) - }) - } - // Select the peripheral to edit its settings let selectedPeripheralSN = null let selectedPeripheralSettings = {} @@ -179,7 +123,7 @@ {/if} {/each}

{$_("projectHardwareOthersLabel")}

- + addPeripheral({Name: "OS2L connection", ProtocolName: "OS2L"})} text="Add an OS2L peripheral" icon="bx-plus-circle" tooltip="Configure an OS2L connection"/> diff --git a/hardware/FTDIFinder.go b/hardware/FTDIFinder.go index 4d1fc29..075fa92 100644 --- a/hardware/FTDIFinder.go +++ b/hardware/FTDIFinder.go @@ -24,22 +24,48 @@ const ( // FTDIFinder represents how the protocol is defined type FTDIFinder struct { - findTicker time.Ticker // Peripherals find ticker - peripherals map[string]Peripheral // The list of peripherals handled by this finder - scanChannel chan struct{} // The channel to trigger a scan event - goWait sync.WaitGroup // Check goroutines execution + findTicker time.Ticker // Peripherals find ticker + foundPeripherals map[string]PeripheralInfo // The list of peripherals handled by this finder + registeredPeripherals map[string]FTDIPeripheral // The list of found peripherals + scanChannel chan struct{} // The channel to trigger a scan event + goWait sync.WaitGroup // Check goroutines execution } // NewFTDIFinder creates a new FTDI finder func NewFTDIFinder(findPeriod time.Duration) *FTDIFinder { log.Trace().Str("file", "FTDIFinder").Msg("FTDI finder created") return &FTDIFinder{ - findTicker: *time.NewTicker(findPeriod), - peripherals: make(map[string]Peripheral), - scanChannel: make(chan struct{}), + findTicker: *time.NewTicker(findPeriod), + foundPeripherals: make(map[string]PeripheralInfo), + registeredPeripherals: make(map[string]FTDIPeripheral), + scanChannel: make(chan struct{}), } } +// RegisterPeripheral registers a new peripheral +func (f *FTDIFinder) RegisterPeripheral(ctx context.Context, peripheralData PeripheralInfo) (string, error) { + ftdiPeripheral, err := NewFTDIPeripheral(peripheralData) + if err != nil { + return "", fmt.Errorf("unable to create the FTDI peripheral: %v", err) + } + f.registeredPeripherals[peripheralData.SerialNumber] = *ftdiPeripheral + log.Trace().Any("periph", &ftdiPeripheral).Str("file", "FTDIFinder").Str("peripheralName", peripheralData.Name).Msg("FTDI peripheral has been created") + return peripheralData.SerialNumber, nil +} + +// UnregisterPeripheral unregisters an existing peripheral +func (f *FTDIFinder) UnregisterPeripheral(ctx context.Context, peripheralID string) error { + peripheral, registered := f.registeredPeripherals[peripheralID] + if registered { + err := peripheral.Disconnect(ctx) + if err != nil { + return err + } + } + delete(f.registeredPeripherals, peripheralID) + return nil +} + //go:embed third-party/ftdi/detectFTDI.exe var finderExe []byte @@ -58,7 +84,7 @@ func (f *FTDIFinder) Initialize() error { if err != nil { return err } - createExecutable(ftdiSenderExecutableName, senderExe) + err = createExecutable(ftdiSenderExecutableName, senderExe) if err != nil { return err } @@ -144,16 +170,28 @@ func (f *FTDIFinder) GetName() string { return "FTDI" } -// GetPeripheral gets the peripheral that correspond to the specified ID -func (f *FTDIFinder) GetPeripheral(peripheralID string) (Peripheral, bool) { +// GetPeripheralSettings gets the peripheral settings +func (f *FTDIFinder) GetPeripheralSettings(peripheralID string) (map[string]interface{}, error) { // Return the specified peripheral - peripheral := f.peripherals[peripheralID] - if peripheral == nil { + peripheral, found := f.registeredPeripherals[peripheralID] + if !found { log.Error().Str("file", "FTDIFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral from the FTDI finder") - return nil, false + return nil, fmt.Errorf("unable to found the peripheral") } log.Debug().Str("file", "FTDIFinder").Str("peripheralID", peripheralID).Msg("peripheral found by the FTDI finder") - return peripheral, true + return peripheral.GetSettings(), nil +} + +// SetPeripheralSettings sets the peripheral settings +func (f *FTDIFinder) SetPeripheralSettings(peripheralID string, settings map[string]interface{}) error { + // Return the specified peripheral + peripheral, found := f.registeredPeripherals[peripheralID] + if !found { + log.Error().Str("file", "FTDIFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral from the FTDI finder") + return fmt.Errorf("unable to found the peripheral") + } + log.Debug().Str("file", "FTDIFinder").Str("peripheralID", peripheralID).Msg("peripheral found by the FTDI finder") + return peripheral.SetSettings(settings) } // scanPeripherals scans the FTDI peripherals @@ -163,8 +201,6 @@ func (f *FTDIFinder) scanPeripherals(ctx context.Context) error { log.Trace().Str("file", "FTDIFinder").Msg("FTDI scan triggered") - ftdiPeripherals := make(map[string]Peripheral) - finder := exec.CommandContext(detectionCtx, filepath.Join(os.TempDir(), ftdiFinderExecutableName)) log.Trace().Str("file", "FTDIFinder").Msg("has executed the FIND executable") @@ -190,6 +226,8 @@ func (f *FTDIFinder) scanPeripherals(ctx context.Context) error { return fmt.Errorf("unable to find FTDI peripherals: %s", scannerErr.Text()) } + temporaryPeripherals := make(map[string]PeripheralInfo) + scanner := bufio.NewScanner(stdout) for scanner.Scan() { peripheralString := scanner.Text() @@ -203,29 +241,27 @@ func (f *FTDIFinder) scanPeripherals(ctx context.Context) error { log.Warn().Str("file", "FTDIFinder").Str("peripheralName", peripheralInfo[2]).Msg("no location provided for this FTDI peripheral") location = -1 } - // Add the peripheral to the temporary list - peripheral, err := NewFTDIPeripheral(peripheralInfo[2], peripheralInfo[1], location) - if err != nil { - return fmt.Errorf("unable to create the FTDI peripheral: %v", err) + // Add the peripheral info to the found list + temporaryPeripherals[peripheralInfo[1]] = PeripheralInfo{ + Name: peripheralInfo[2], + SerialNumber: peripheralInfo[1], + ProtocolName: "FTDI", } - log.Trace().Any("periph", &peripheral).Str("file", "FTDIFinder").Str("peripheralName", peripheralInfo[2]).Msg("has been created") - ftdiPeripherals[peripheralInfo[1]] = peripheral - log.Trace().Any("periph", ftdiPeripherals).Str("file", "FTDIFinder").Str("peripheralName", peripheralInfo[2]).Msg("successfully added the FTDI peripheral to the finder") + // If this peripheral is already registered, connect it + peripheral, registered := f.registeredPeripherals[peripheralInfo[1]] + if registered { + err := peripheral.Connect(ctx, location) + if err != nil { + log.Err(err).Str("file", "FTDIFinder").Str("peripheralSN", peripheralInfo[1]).Msg("unable to connect the peripheral") + } + } + + log.Trace().Any("periph", temporaryPeripherals).Str("file", "FTDIFinder").Str("peripheralName", peripheralInfo[2]).Msg("successfully added the FTDI peripheral to the finder") } // Emit the peripherals changes to the front - emitPeripheralsChanges(ctx, f.peripherals, ftdiPeripherals) + emitPeripheralsChanges(ctx, f.foundPeripherals, temporaryPeripherals) // Store the new peripherals list - f.peripherals = ftdiPeripherals - return nil -} - -// CreatePeripheral is not implemented here -func (f *FTDIFinder) CreatePeripheral(context.Context) (Peripheral, error) { - return nil, nil -} - -// DeletePeripheral is not implemented here -func (f *FTDIFinder) DeletePeripheral(serialNumber string) error { + f.foundPeripherals = temporaryPeripherals return nil } diff --git a/hardware/FTDIPeripheral.go b/hardware/FTDIPeripheral.go index a005cc9..7e03e57 100644 --- a/hardware/FTDIPeripheral.go +++ b/hardware/FTDIPeripheral.go @@ -21,9 +21,7 @@ const ( // FTDIPeripheral contains the data of an FTDI peripheral type FTDIPeripheral struct { - name string // The name of the peripheral - serialNumber string // The S/N of the FTDI peripheral - location int // The location of the peripheral + info PeripheralInfo // The peripheral basic data programName string // The temp file name of the executable settings map[string]interface{} // The settings of the peripheral dmxSender *exec.Cmd // The command to pilot the DMX sender program @@ -35,14 +33,12 @@ type FTDIPeripheral struct { } // NewFTDIPeripheral creates a new FTDI peripheral -func NewFTDIPeripheral(name string, serialNumber string, location int) (*FTDIPeripheral, error) { - log.Info().Str("file", "FTDIPeripheral").Str("name", name).Str("s/n", serialNumber).Int("location", location).Msg("FTDI peripheral created") +func NewFTDIPeripheral(info PeripheralInfo) (*FTDIPeripheral, error) { + log.Info().Str("file", "FTDIPeripheral").Str("name", info.Name).Str("s/n", info.SerialNumber).Msg("FTDI peripheral created") settings := make(map[string]interface{}) return &FTDIPeripheral{ - name: name, + info: info, dmxSender: nil, - serialNumber: serialNumber, - location: location, settings: settings, disconnectChan: make(chan struct{}), errorsChan: make(chan error, 1), @@ -50,32 +46,32 @@ func NewFTDIPeripheral(name string, serialNumber string, location int) (*FTDIPer } // Connect connects the FTDI peripheral -func (p *FTDIPeripheral) Connect(ctx context.Context) error { +func (p *FTDIPeripheral) Connect(ctx context.Context, location int) error { // Connect if no connection is already running - log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("connecting FTDI peripheral...") + log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("connecting FTDI peripheral...") // Check if the connection has already been established if p.dmxSender != nil { - log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("dmxSender already initialized") + log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmxSender already initialized") return nil } // Initialize the exec.Command for running the process - p.dmxSender = exec.Command(p.programName, fmt.Sprintf("%d", p.location)) - log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("dmxSender instance created") + p.dmxSender = exec.Command(p.programName, fmt.Sprintf("%d", location)) + log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmxSender instance created") // Create the pipes for stdin, stdout, and stderr asynchronously without blocking var err error if p.stdout, err = p.dmxSender.StdoutPipe(); err != nil { - log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("unable to create stdout pipe") + log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("unable to create stdout pipe") return fmt.Errorf("unable to create stdout pipe: %v", err) } if p.stdin, err = p.dmxSender.StdinPipe(); err != nil { - log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("unable to create stdin pipe") + log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("unable to create stdin pipe") return fmt.Errorf("unable to create stdin pipe: %v", err) } if p.stderr, err = p.dmxSender.StderrPipe(); err != nil { - log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("unable to create stderr pipe") + log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("unable to create stderr pipe") return fmt.Errorf("unable to create stderr pipe: %v", err) } @@ -84,10 +80,10 @@ func (p *FTDIPeripheral) Connect(ctx context.Context) error { scanner := bufio.NewScanner(p.stderr) for scanner.Scan() { // Process each line read from stderr - log.Err(fmt.Errorf(scanner.Text())).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("error detected in dmx sender") + log.Err(fmt.Errorf(scanner.Text())).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("error detected in dmx sender") } if err := scanner.Err(); err != nil { - log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("error reading from stderr") + log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("error reading from stderr") } }() @@ -98,33 +94,33 @@ func (p *FTDIPeripheral) Connect(ctx context.Context) error { select { case <-ctx.Done(): // If the context is canceled, handle it gracefully - log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("dmxSender was canceled by context") + log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmxSender was canceled by context") return default: // Handle command exit normally if err != nil { - log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("error while execution of dmx sender") + log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("error while execution of dmx sender") if exitError, ok := err.(*exec.ExitError); ok { - log.Warn().Str("file", "FTDIPeripheral").Int("exitCode", exitError.ExitCode()).Str("s/n", p.serialNumber).Msg("dmx sender exited with code") + log.Warn().Str("file", "FTDIPeripheral").Int("exitCode", exitError.ExitCode()).Str("s/n", p.info.SerialNumber).Msg("dmx sender exited with code") } } else { - log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("dmx sender exited successfully") + log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmx sender exited successfully") } } }() - log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("dmxSender process started successfully") + log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmxSender process started successfully") return nil } // Disconnect disconnects the FTDI peripheral func (p *FTDIPeripheral) Disconnect(ctx context.Context) error { - log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("disconnecting FTDI peripheral...") + log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("disconnecting FTDI peripheral...") if p.dmxSender != nil { - log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("dmxsender is defined for this FTDI") + log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmxsender is defined for this FTDI") _, err := io.WriteString(p.stdin, string([]byte{0x04, 0x00, 0x00, 0x00})) if err != nil { - log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("unable to write command to sender") + log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("unable to write command to sender") return fmt.Errorf("unable to disconnect: %v", err) } p.stdin.Close() @@ -132,47 +128,47 @@ func (p *FTDIPeripheral) Disconnect(ctx context.Context) error { p.dmxSender = nil err = os.Remove(p.programName) if err != nil { - log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Str("senderPath", p.programName).Msg("unable to delete the dmx sender temporary file") + log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Str("senderPath", p.programName).Msg("unable to delete the dmx sender temporary file") return fmt.Errorf("unable to delete the temporary file: %v", err) } return nil } - log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("error while disconnecting: not connected") + log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("error while disconnecting: not connected") return fmt.Errorf("unable to disconnect: not connected") } // Activate activates the FTDI peripheral func (p *FTDIPeripheral) Activate(ctx context.Context) error { if p.dmxSender != nil { - log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("dmxsender is defined for this FTDI") + log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmxsender is defined for this FTDI") _, err := io.WriteString(p.stdin, string([]byte{0x01, 0x00, 0x00, 0x00})) if err != nil { - log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("unable to write command to sender") + log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("unable to write command to sender") return fmt.Errorf("unable to activate: %v", err) } return nil } - log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("error while activating: not connected") + log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("error while activating: not connected") return fmt.Errorf("unable to activate: not connected") } // Deactivate deactivates the FTDI peripheral func (p *FTDIPeripheral) Deactivate(ctx context.Context) error { if p.dmxSender != nil { - log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("dmxsender is defined for this FTDI") + log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmxsender is defined for this FTDI") _, err := io.WriteString(p.stdin, string([]byte{0x02, 0x00, 0x00, 0x00})) if err != nil { - log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("unable to write command to sender") + log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("unable to write command to sender") return fmt.Errorf("unable to deactivate: %v", err) } return nil } - log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("error while deactivating: not connected") + log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("error while deactivating: not connected") return fmt.Errorf("unable to deactivate: not connected") } -// SetPeripheralSettings sets a specific setting for this peripheral -func (p *FTDIPeripheral) SetPeripheralSettings(settings map[string]interface{}) error { +// SetSettings sets a specific setting for this peripheral +func (p *FTDIPeripheral) SetSettings(settings map[string]interface{}) error { p.settings = settings return nil } @@ -180,16 +176,16 @@ func (p *FTDIPeripheral) SetPeripheralSettings(settings map[string]interface{}) // SetDeviceProperty sends a command to the specified device func (p *FTDIPeripheral) SetDeviceProperty(ctx context.Context, uint32, channelNumber uint32, channelValue byte) error { if p.dmxSender != nil { - log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("dmxsender is defined for this FTDI") + log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmxsender is defined for this FTDI") commandString := []byte{0x03, 0x01, 0x00, 0xff, 0x03, 0x02, 0x00, channelValue} _, err := io.WriteString(p.stdin, string(commandString)) if err != nil { - log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("unable to write command to sender") + log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("unable to write command to sender") return fmt.Errorf("unable to set device property: %v", err) } return nil } - log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("error while setting device property: not connected") + log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("error while setting device property: not connected") return fmt.Errorf("unable to set device property: not connected") } @@ -200,9 +196,5 @@ func (p *FTDIPeripheral) GetSettings() map[string]interface{} { // GetInfo gets all the peripheral information func (p *FTDIPeripheral) GetInfo() PeripheralInfo { - return PeripheralInfo{ - Name: p.name, - SerialNumber: p.serialNumber, - ProtocolName: "FTDI", - } + return p.info } diff --git a/hardware/MIDIFinder.go b/hardware/MIDIFinder.go index 4d7a65e..ecc4100 100644 --- a/hardware/MIDIFinder.go +++ b/hardware/MIDIFinder.go @@ -15,18 +15,18 @@ import ( // MIDIFinder represents how the protocol is defined type MIDIFinder struct { - findTicker time.Ticker // Peripherals find ticker - peripherals map[string]Peripheral // The list of peripherals - scanChannel chan struct{} // The channel to trigger a scan event - goWait sync.WaitGroup // Check goroutines execution + findTicker time.Ticker // Peripherals find ticker + registeredPeripherals map[string]MIDIPeripheral // The list of peripherals + scanChannel chan struct{} // The channel to trigger a scan event + goWait sync.WaitGroup // Check goroutines execution } // NewMIDIFinder creates a new DMXUSB protocol func NewMIDIFinder(findPeriod time.Duration) *MIDIFinder { log.Trace().Str("file", "MIDIFinder").Msg("MIDI finder created") return &MIDIFinder{ - findTicker: *time.NewTicker(findPeriod), - peripherals: make(map[string]Peripheral), + findTicker: *time.NewTicker(findPeriod), + registeredPeripherals: make(map[string]MIDIPeripheral), } } @@ -36,6 +36,30 @@ func (f *MIDIFinder) Initialize() error { return nil } +// RegisterPeripheral registers a new peripheral +func (f *MIDIFinder) RegisterPeripheral(ctx context.Context, peripheralData PeripheralInfo) (string, error) { + peripheral, err := NewMIDIPeripheral(peripheralData) + if err != nil { + return "", fmt.Errorf("unable to create the MIDI peripheral: %v", err) + } + f.registeredPeripherals[peripheralData.SerialNumber] = *peripheral + log.Trace().Any("periph", &peripheral).Str("file", "MIDIFinder").Str("peripheralName", peripheralData.Name).Msg("FTDI peripheral has been created") + return peripheralData.SerialNumber, nil +} + +// UnregisterPeripheral unregisters an existing peripheral +func (f *MIDIFinder) UnregisterPeripheral(ctx context.Context, peripheralID string) error { + peripheral, registered := f.registeredPeripherals[peripheralID] + if registered { + err := peripheral.Disconnect(ctx) + if err != nil { + return err + } + } + delete(f.registeredPeripherals, peripheralID) + return nil +} + // Start starts the finder and search for peripherals func (f *MIDIFinder) Start(ctx context.Context) error { f.goWait.Add(1) @@ -79,16 +103,28 @@ func (f *MIDIFinder) GetName() string { return "MIDI" } -// GetPeripheral gets the peripheral that correspond to the specified ID -func (f *MIDIFinder) GetPeripheral(peripheralID string) (Peripheral, bool) { +// GetPeripheralSettings gets the peripheral settings +func (f *MIDIFinder) GetPeripheralSettings(peripheralID string) (map[string]interface{}, error) { // Return the specified peripheral - peripheral, found := f.peripherals[peripheralID] + peripheral, found := f.registeredPeripherals[peripheralID] if !found { - log.Error().Str("file", "MIDIFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral in the MIDI finder") - return nil, false + log.Error().Str("file", "MIDIFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral from the FTDI finder") + return nil, fmt.Errorf("unable to found the peripheral") } - log.Trace().Str("file", "MIDIFinder").Str("peripheralID", peripheralID).Msg("MIDI peripheral found in the driver") - return peripheral, true + log.Debug().Str("file", "MIDIFinder").Str("peripheralID", peripheralID).Msg("peripheral found by the FTDI finder") + return peripheral.GetSettings(), nil +} + +// SetPeripheralSettings sets the peripheral settings +func (f *MIDIFinder) SetPeripheralSettings(peripheralID string, settings map[string]interface{}) error { + // Return the specified peripheral + peripheral, found := f.registeredPeripherals[peripheralID] + if !found { + log.Error().Str("file", "MIDIFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral from the FTDI finder") + return fmt.Errorf("unable to found the peripheral") + } + log.Debug().Str("file", "MIDIFinder").Str("peripheralID", peripheralID).Msg("peripheral found by the FTDI finder") + return peripheral.SetSettings(settings) } func splitStringAndNumber(input string) (string, int, error) { @@ -119,7 +155,7 @@ func (f *MIDIFinder) ForceScan() { // scanPeripherals scans the MIDI peripherals func (f *MIDIFinder) scanPeripherals(ctx context.Context) error { - midiPeripherals := make(map[string]Peripheral) + // midiPeripherals := make(map[string]Peripheral) log.Trace().Str("file", "MIDIFinder").Msg("opening MIDI scanner port...") midiScanner, err := rtmidi.NewMIDIInDefault() if err != nil { @@ -148,8 +184,8 @@ func (f *MIDIFinder) scanPeripherals(ctx context.Context) error { } log.Info().Str("file", "MIDIFinder").Str("name", name).Int("location", location).Msg("MIDI peripheral found") // Add the peripheral to the temporary list - sn := strings.ToLower(strings.Replace(name, " ", "_", -1)) - midiPeripherals[sn] = NewMIDIPeripheral(name, location, sn) + // sn := strings.ToLower(strings.Replace(name, " ", "_", -1)) + // midiPeripherals[sn] = NewMIDIPeripheral(name, location, sn) } // Compare with the current peripherals to detect arrivals/removals // removedList, addedList := comparePeripherals(f.peripherals, midiPeripherals) @@ -159,7 +195,7 @@ func (f *MIDIFinder) scanPeripherals(ctx context.Context) error { // emitPeripheralsEvents(ctx, addedList, PeripheralArrival) log.Info().Str("file", "MIDIFinder").Msg("MIDI add list emitted to the front") // Store the new peripherals list - f.peripherals = midiPeripherals + // f.peripherals = midiPeripherals return nil } diff --git a/hardware/MIDIPeripheral.go b/hardware/MIDIPeripheral.go index e3083ba..a143b64 100644 --- a/hardware/MIDIPeripheral.go +++ b/hardware/MIDIPeripheral.go @@ -8,22 +8,18 @@ import ( // MIDIPeripheral contains the data of a MIDI peripheral type MIDIPeripheral struct { - name string // The name of the peripheral - location int // The location of the peripheral - serialNumber string // The S/N of the peripheral - settings map[string]interface{} // The settings of the peripheral + info PeripheralInfo // The peripheral info + location int // The location of the peripheral + settings map[string]interface{} // The settings of the peripheral } // NewMIDIPeripheral creates a new MIDI peripheral -func NewMIDIPeripheral(name string, location int, serialNumber string) *MIDIPeripheral { - log.Trace().Str("file", "MIDIPeripheral").Str("name", name).Str("s/n", serialNumber).Int("location", location).Msg("MIDI peripheral created") - settings := make(map[string]interface{}) +func NewMIDIPeripheral(peripheralData PeripheralInfo) (*MIDIPeripheral, error) { + log.Trace().Str("file", "MIDIPeripheral").Str("name", peripheralData.Name).Str("s/n", peripheralData.SerialNumber).Msg("MIDI peripheral created") return &MIDIPeripheral{ - name: name, - location: location, - serialNumber: serialNumber, - settings: settings, - } + info: peripheralData, + settings: peripheralData.Settings, + }, nil } // Connect connects the MIDI peripheral @@ -46,8 +42,8 @@ func (p *MIDIPeripheral) Deactivate(ctx context.Context) error { return nil } -// SetPeripheralSettings sets a specific setting for this peripheral -func (p *MIDIPeripheral) SetPeripheralSettings(settings map[string]interface{}) error { +// SetSettings sets a specific setting for this peripheral +func (p *MIDIPeripheral) SetSettings(settings map[string]interface{}) error { p.settings = settings return nil } @@ -64,9 +60,5 @@ func (p *MIDIPeripheral) GetSettings() map[string]interface{} { // GetInfo gets the peripheral information func (p *MIDIPeripheral) GetInfo() PeripheralInfo { - return PeripheralInfo{ - Name: p.name, - ProtocolName: "MIDI", - SerialNumber: p.serialNumber, - } + return p.info } diff --git a/hardware/OS2LFinder.go b/hardware/OS2LFinder.go index 33fae6b..2acdb8e 100644 --- a/hardware/OS2LFinder.go +++ b/hardware/OS2LFinder.go @@ -11,14 +11,14 @@ import ( // OS2LFinder represents how the protocol is defined type OS2LFinder struct { - peripherals map[string]Peripheral // The list of peripherals + registeredPeripherals map[string]OS2LPeripheral // The list of found peripherals } // NewOS2LFinder creates a new OS2L finder func NewOS2LFinder() *OS2LFinder { log.Trace().Str("file", "OS2LFinder").Msg("OS2L finder created") return &OS2LFinder{ - peripherals: make(map[string]Peripheral), + registeredPeripherals: make(map[string]OS2LPeripheral), } } @@ -28,21 +28,36 @@ func (f *OS2LFinder) Initialize() error { return nil } -// CreatePeripheral creates a new OS2L peripheral -func (f *OS2LFinder) CreatePeripheral(ctx context.Context) (Peripheral, error) { +// RegisterPeripheral registers a new peripheral +func (f *OS2LFinder) RegisterPeripheral(ctx context.Context, peripheralData PeripheralInfo) (string, error) { // Create a random serial number for this peripheral - randomSerialNumber := strings.ToUpper(fmt.Sprintf("%08x", rand.Intn(1<<32))) - log.Trace().Str("file", "OS2LFinder").Str("serialNumber", randomSerialNumber).Msg("OS2L peripheral created") - peripheral := NewOS2LPeripheral("OS2L", randomSerialNumber) - f.peripherals[randomSerialNumber] = peripheral - log.Info().Str("file", "OS2LFinder").Str("serialNumber", randomSerialNumber).Msg("OS2L peripheral created and registered") - return peripheral, nil + peripheralData.SerialNumber = strings.ToUpper(fmt.Sprintf("%08x", rand.Intn(1<<32))) + log.Trace().Str("file", "OS2LFinder").Str("serialNumber", peripheralData.SerialNumber).Msg("OS2L peripheral created") + + os2lPeripheral, err := NewOS2LPeripheral(peripheralData) + if err != nil { + return "", fmt.Errorf("unable to create the OS2L peripheral: %v", err) + } + + f.registeredPeripherals[peripheralData.SerialNumber] = *os2lPeripheral + log.Trace().Any("periph", &os2lPeripheral).Str("file", "OS2LFinder").Str("peripheralName", peripheralData.Name).Msg("OS2L peripheral has been created") + // Send the change to the front + // runtime.EventsEmit(ctx, string(PeripheralArrival), peripheralData) + return peripheralData.SerialNumber, nil } -// DeletePeripheral removes an OS2L peripheral -func (f *OS2LFinder) DeletePeripheral(serialNumber string) error { - delete(f.peripherals, serialNumber) - log.Info().Str("file", "OS2LFinder").Str("serialNumber", serialNumber).Msg("OS2L peripheral removed") +// UnregisterPeripheral unregisters an existing peripheral +func (f *OS2LFinder) UnregisterPeripheral(ctx context.Context, peripheralID string) error { + peripheral, registered := f.registeredPeripherals[peripheralID] + if registered { + err := peripheral.Disconnect(ctx) + if err != nil { + return err + } + } + delete(f.registeredPeripherals, peripheralID) + // Send the change to the front + // runtime.EventsEmit(ctx, string(PeripheralRemoval), peripheral.GetInfo()) return nil } @@ -51,16 +66,28 @@ func (f *OS2LFinder) GetName() string { return "OS2L" } -// GetPeripheral gets the peripheral that correspond to the specified ID -func (f *OS2LFinder) GetPeripheral(peripheralID string) (Peripheral, bool) { +// GetPeripheralSettings gets the peripheral settings +func (f *OS2LFinder) GetPeripheralSettings(peripheralID string) (map[string]interface{}, error) { // Return the specified peripheral - peripheral, found := f.peripherals[peripheralID] + peripheral, found := f.registeredPeripherals[peripheralID] if !found { - log.Error().Str("file", "OS2LFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral in the OS2L finder") - return nil, false + log.Error().Str("file", "OS2LFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral from the FTDI finder") + return nil, fmt.Errorf("unable to found the peripheral") } - log.Trace().Str("file", "OS2LFinder").Str("peripheralID", peripheralID).Msg("OS2L peripheral found in the finder") - return peripheral, true + log.Debug().Str("file", "OS2LFinder").Str("peripheralID", peripheralID).Msg("peripheral found by the FTDI finder") + return peripheral.GetSettings(), nil +} + +// SetPeripheralSettings sets the peripheral settings +func (f *OS2LFinder) SetPeripheralSettings(peripheralID string, settings map[string]interface{}) error { + // Return the specified peripheral + peripheral, found := f.registeredPeripherals[peripheralID] + if !found { + log.Error().Str("file", "OS2LFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral from the FTDI finder") + return fmt.Errorf("unable to found the peripheral") + } + log.Debug().Str("file", "OS2LFinder").Str("peripheralID", peripheralID).Msg("peripheral found by the FTDI finder") + return peripheral.SetSettings(settings) } // Start starts the finder diff --git a/hardware/OS2LPeripheral.go b/hardware/OS2LPeripheral.go index e6845e5..6e12008 100644 --- a/hardware/OS2LPeripheral.go +++ b/hardware/OS2LPeripheral.go @@ -9,21 +9,19 @@ import ( // OS2LPeripheral contains the data of an OS2L peripheral type OS2LPeripheral struct { - name string // The name of the peripheral - serialNumber string // The serial number of the peripheral - serverIP string // OS2L server IP - serverPort int // OS2L server port + info PeripheralInfo // The basic info for this peripheral + serverIP string // OS2L server IP + serverPort int // OS2L server port } // NewOS2LPeripheral creates a new OS2L peripheral -func NewOS2LPeripheral(name string, serialNumber string) *OS2LPeripheral { - log.Trace().Str("file", "OS2LPeripheral").Str("name", name).Str("s/n", serialNumber).Msg("OS2L peripheral created") +func NewOS2LPeripheral(peripheralData PeripheralInfo) (*OS2LPeripheral, error) { + log.Trace().Str("file", "OS2LPeripheral").Str("name", peripheralData.Name).Str("s/n", peripheralData.SerialNumber).Msg("OS2L peripheral created") return &OS2LPeripheral{ - name: name, - serverIP: "127.0.0.1", - serverPort: 9995, - serialNumber: serialNumber, - } + info: peripheralData, + serverIP: "127.0.0.1", + serverPort: 9005, + }, nil } // Connect connects the MIDI peripheral @@ -50,8 +48,8 @@ func (p *OS2LPeripheral) Deactivate(ctx context.Context) error { return nil } -// SetPeripheralSettings sets a specific setting for this peripheral -func (p *OS2LPeripheral) SetPeripheralSettings(settings map[string]interface{}) error { +// SetSettings sets a specific setting for this peripheral +func (p *OS2LPeripheral) SetSettings(settings map[string]interface{}) error { // Check if the IP exists serverIP, found := settings["os2lIp"] if !found { @@ -95,9 +93,5 @@ func (p *OS2LPeripheral) GetSettings() map[string]interface{} { // GetInfo gets the peripheral information func (p *OS2LPeripheral) GetInfo() PeripheralInfo { - return PeripheralInfo{ - Name: p.name, - SerialNumber: p.serialNumber, - ProtocolName: "OS2L", - } + return p.info } diff --git a/hardware/hardware.go b/hardware/hardware.go index f7aeabf..1409567 100644 --- a/hardware/hardware.go +++ b/hardware/hardware.go @@ -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") } } diff --git a/hardware/interfaces.go b/hardware/interfaces.go index 213c3e3..2041002 100644 --- a/hardware/interfaces.go +++ b/hardware/interfaces.go @@ -8,7 +8,7 @@ type Peripheral interface { Disconnect(context.Context) error // Disconnect the peripheral Activate(context.Context) error // Activate the peripheral Deactivate(context.Context) error // Deactivate the peripheral - SetPeripheralSettings(map[string]interface{}) error // Set a peripheral setting + SetSettings(map[string]interface{}) error // Set a peripheral setting SetDeviceProperty(context.Context, uint32, uint32, byte) error // Update a device property GetInfo() PeripheralInfo // Get the peripheral information @@ -25,12 +25,13 @@ type PeripheralInfo struct { // PeripheralFinder represents how compatible peripheral drivers are implemented type PeripheralFinder interface { - Initialize() error // Initializes the protocol - Start(context.Context) error // Start the detection - Stop() error // Stop the detection - ForceScan() // Explicitly scans for peripherals - CreatePeripheral(ctx context.Context) (Peripheral, error) // Creates a new peripheral - DeletePeripheral(serialNumber string) error // Removes a peripheral - GetName() string // Get the name of the finder - GetPeripheral(string) (Peripheral, bool) // Get the peripheral + Initialize() error // Initializes the protocol + Start(context.Context) error // Start the detection + Stop() error // Stop the detection + ForceScan() // Explicitly scans for peripherals + RegisterPeripheral(context.Context, PeripheralInfo) (string, error) // Registers a new peripheral data + UnregisterPeripheral(context.Context, string) error // Unregisters an existing peripheral + GetPeripheralSettings(string) (map[string]interface{}, error) // Gets the peripheral settings + SetPeripheralSettings(string, map[string]interface{}) error // Sets the peripheral settings + GetName() string // Get the name of the finder } diff --git a/peripherals.go b/peripherals.go index d98ea46..9120603 100644 --- a/peripherals.go +++ b/peripherals.go @@ -8,41 +8,48 @@ import ( ) // AddPeripheral adds a peripheral to the project -func (a *App) AddPeripheral(protocolName string, peripheralID string) error { +func (a *App) AddPeripheral(peripheralData hardware.PeripheralInfo) (string, error) { // Get the peripheral from its finder - p, found := a.hardwareManager.GetPeripheral(protocolName, peripheralID) - if !found { - log.Error().Str("file", "peripheral").Str("protocolName", protocolName).Str("periphID", peripheralID).Msg("unable to found the specified peripheral") - return fmt.Errorf("unable to found the peripheral ID '%s'", peripheralID) + f, err := a.hardwareManager.GetFinder(peripheralData.ProtocolName) + if err != nil { + log.Error().Str("file", "peripheral").Str("protocolName", peripheralData.ProtocolName).Msg("unable to found the specified finder") + return "", fmt.Errorf("unable to found the peripheral ID '%s'", peripheralData.SerialNumber) } + // Register this new peripheral + serialNumber, err := f.RegisterPeripheral(a.ctx, peripheralData) + if err != nil { + log.Trace().Str("file", "peripheral").Str("protocolName", peripheralData.ProtocolName).Str("periphID", serialNumber).Msg("device registered to the finder") + return "", fmt.Errorf("unable to register the peripheral '%s'", serialNumber) + } + // Rewrite the serialnumber for virtual devices + peripheralData.SerialNumber = serialNumber + // Add the peripheral ID to the project if a.projectInfo.PeripheralsInfo == nil { a.projectInfo.PeripheralsInfo = make(map[string]hardware.PeripheralInfo) } - a.projectInfo.PeripheralsInfo[peripheralID] = p.GetInfo() - log.Info().Str("file", "peripheral").Str("protocolName", protocolName).Str("periphID", peripheralID).Msg("peripheral added to project") - // TODO: Connect the peripheral - return nil + a.projectInfo.PeripheralsInfo[peripheralData.SerialNumber] = peripheralData + log.Info().Str("file", "peripheral").Str("protocolName", peripheralData.ProtocolName).Str("periphID", peripheralData.SerialNumber).Msg("peripheral added to project") + return peripheralData.SerialNumber, nil } // GetPeripheralSettings gets the peripheral settings func (a *App) GetPeripheralSettings(protocolName, peripheralID string) (map[string]interface{}, error) { // Get the peripheral from its finder - p, found := a.hardwareManager.GetPeripheral(protocolName, peripheralID) - if !found { + f, err := a.hardwareManager.GetFinder(protocolName) + if err != nil { log.Error().Str("file", "peripheral").Str("protocolName", protocolName).Str("periphID", peripheralID).Msg("unable to found the specified peripheral") return nil, fmt.Errorf("unable to found the peripheral ID '%s'", peripheralID) } - // Return the peripheral settings - return p.GetSettings(), nil + return f.GetPeripheralSettings(peripheralID) } // UpdatePeripheralSettings updates a specific setting of a peripheral func (a *App) UpdatePeripheralSettings(protocolName, peripheralID string, settings map[string]interface{}) error { - // Get the peripheral from its finder - p, found := a.hardwareManager.GetPeripheral(protocolName, peripheralID) - if !found { + // Sets the settings with the finder + f, err := a.hardwareManager.GetFinder(protocolName) + if err != nil { log.Error().Str("file", "peripheral").Str("protocolName", protocolName).Str("periphID", peripheralID).Msg("unable to found the specified peripheral") return fmt.Errorf("unable to found the peripheral ID '%s'", peripheralID) } @@ -54,104 +61,91 @@ func (a *App) UpdatePeripheralSettings(protocolName, peripheralID string, settin pInfo.Settings = settings a.projectInfo.PeripheralsInfo[peripheralID] = pInfo // Apply changes in the peripheral - return p.SetPeripheralSettings(pInfo.Settings) + return f.SetPeripheralSettings(peripheralID, pInfo.Settings) } // RemovePeripheral adds a peripheral to the project func (a *App) RemovePeripheral(protocolName string, peripheralID string) error { - // TODO: Disconnect the peripheral + // Unregister the peripheral from the finder + f, err := a.hardwareManager.GetFinder(protocolName) + if err != nil { + log.Err(err).Str("file", "peripherals").Str("protocolName", protocolName).Msg("unable to find the finder") + return fmt.Errorf("unable to find the finder") + } + err = f.UnregisterPeripheral(a.ctx, peripheralID) + if err != nil { + log.Err(err).Str("file", "peripherals").Str("peripheralID", peripheralID).Msg("unable to unregister this peripheral") + return fmt.Errorf("unable to unregister this peripheral") + } // Remove the peripheral ID from the project delete(a.projectInfo.PeripheralsInfo, peripheralID) log.Info().Str("file", "peripheral").Str("protocolName", protocolName).Str("periphID", peripheralID).Msg("peripheral removed from project") return nil } -// AddOS2LPeripheral adds a new OS2L peripheral -func (a *App) AddOS2LPeripheral() (hardware.PeripheralInfo, error) { - // Get the OS2L finder - os2lDriver, err := a.hardwareManager.GetFinder("OS2L") - if err != nil { - log.Err(err).Str("file", "peripheral").Msg("unable to found the OS2L driver") - return hardware.PeripheralInfo{}, err - } - log.Trace().Str("file", "peripheral").Msg("OS2L driver got") - - // Create a new OS2L peripheral with this finder - os2lPeripheral, err := os2lDriver.CreatePeripheral(a.ctx) - if err != nil { - log.Err(err).Str("file", "peripheral").Msg("unable to create the OS2L peripheral") - return hardware.PeripheralInfo{}, err - } - - os2lInfo := os2lPeripheral.GetInfo() - log.Info().Str("file", "peripheral").Str("s/n", os2lInfo.SerialNumber).Msg("OS2L peripheral created, adding to project") - // Add this new peripheral to the project - return os2lInfo, a.AddPeripheral(os2lDriver.GetName(), os2lInfo.SerialNumber) -} - // FOR TESTING PURPOSE ONLY -func (a *App) ConnectFTDI() error { - // Connect the FTDI - driver, err := a.hardwareManager.GetFinder("FTDI") - if err != nil { - return err - } - periph, found := driver.GetPeripheral("A50285BI") - if !found { - return fmt.Errorf("unable to find the peripheral s/n %s", "A50285BI") - } - return periph.Connect(a.ctx) -} +// func (a *App) ConnectFTDI() error { +// // Connect the FTDI +// driver, err := a.hardwareManager.GetFinder("FTDI") +// if err != nil { +// return err +// } +// periph, found := driver.GetPeripheral("A50285BI") +// if !found { +// return fmt.Errorf("unable to find the peripheral s/n %s", "A50285BI") +// } +// return periph.Connect(a.ctx) +// } -func (a *App) ActivateFTDI() error { - // Connect the FTDI - driver, err := a.hardwareManager.GetFinder("FTDI") - if err != nil { - return err - } - periph, found := driver.GetPeripheral("A50285BI") - if !found { - return fmt.Errorf("unable to find the peripheral s/n %s", "A50285BI") - } - return periph.Activate(a.ctx) -} +// func (a *App) ActivateFTDI() error { +// // Connect the FTDI +// driver, err := a.hardwareManager.GetFinder("FTDI") +// if err != nil { +// return err +// } +// periph, found := driver.GetPeripheral("A50285BI") +// if !found { +// return fmt.Errorf("unable to find the peripheral s/n %s", "A50285BI") +// } +// return periph.Activate(a.ctx) +// } -func (a *App) SetDeviceFTDI(channelValue byte) error { - // Connect the FTDI - driver, err := a.hardwareManager.GetFinder("FTDI") - if err != nil { - return err - } - periph, found := driver.GetPeripheral("A50285BI") - if !found { - return fmt.Errorf("unable to find the peripheral s/n %s", "A50285BI") - } - return periph.SetDeviceProperty(a.ctx, 0, 0, channelValue) -} +// func (a *App) SetDeviceFTDI(channelValue byte) error { +// // Connect the FTDI +// driver, err := a.hardwareManager.GetFinder("FTDI") +// if err != nil { +// return err +// } +// periph, found := driver.GetPeripheral("A50285BI") +// if !found { +// return fmt.Errorf("unable to find the peripheral s/n %s", "A50285BI") +// } +// return periph.SetDeviceProperty(a.ctx, 0, 0, channelValue) +// } -func (a *App) DeactivateFTDI() error { - // Connect the FTDI - driver, err := a.hardwareManager.GetFinder("FTDI") - if err != nil { - return err - } - periph, found := driver.GetPeripheral("A50285BI") - if !found { - return fmt.Errorf("unable to find the peripheral s/n %s", "A50285BI") - } - return periph.Deactivate(a.ctx) -} +// func (a *App) DeactivateFTDI() error { +// // Connect the FTDI +// driver, err := a.hardwareManager.GetFinder("FTDI") +// if err != nil { +// return err +// } +// periph, found := driver.GetPeripheral("A50285BI") +// if !found { +// return fmt.Errorf("unable to find the peripheral s/n %s", "A50285BI") +// } +// return periph.Deactivate(a.ctx) +// } -func (a *App) DisconnectFTDI() error { - // Connect the FTDI - driver, err := a.hardwareManager.GetFinder("FTDI") - if err != nil { - return err - } - periph, found := driver.GetPeripheral("A50285BI") - if !found { - return fmt.Errorf("unable to find the peripheral s/n %s", "A50285BI") - } - return periph.Disconnect(a.ctx) -} +// func (a *App) DisconnectFTDI() error { +// // Connect the FTDI +// driver, err := a.hardwareManager.GetFinder("FTDI") +// if err != nil { +// return err +// } +// periph, found := driver.GetPeripheral("A50285BI") +// if !found { +// return fmt.Errorf("unable to find the peripheral s/n %s", "A50285BI") +// } +// return periph.Disconnect(a.ctx) +// }