Adapting interfaces

This commit is contained in:
2025-11-29 17:42:42 +01:00
parent ea46430b71
commit 848d2758d7
11 changed files with 264 additions and 564 deletions

View File

@@ -9,14 +9,8 @@ import (
// AddPeripheral adds a peripheral to the project
func (a *App) AddPeripheral(peripheralData hardware.PeripheralInfo) (string, error) {
// Get the peripheral from its finder
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)
serialNumber, err := a.hardwareManager.RegisterPeripheral(a.ctx, peripheralData)
if err != nil {
return "", fmt.Errorf("unable to register the peripheral '%s': %w", serialNumber, err)
}
@@ -35,25 +29,24 @@ func (a *App) AddPeripheral(peripheralData hardware.PeripheralInfo) (string, err
return peripheralData.SerialNumber, nil
}
// GetPeripheralSettings gets the peripheral settings
func (a *App) GetPeripheralSettings(protocolName, peripheralID string) (map[string]any, error) {
// Get the peripheral from its finder
f, err := a.hardwareManager.GetFinder(protocolName)
// CreatePeripheral creates a new peripheral
func (a *App) CreatePeripheral(peripheralData hardware.PeripheralInfo) (string, error) {
// Get the appropriate finder to create the device
finder, err := a.hardwareManager.GetFinder(peripheralData.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 "", fmt.Errorf("unable to find the appropriate finder (%s): %w", peripheralData.ProtocolName, err)
}
return f.GetPeripheralSettings(peripheralID)
// Manually create the peripheral from its finder
return finder.Create(a.ctx, peripheralData)
}
// GetPeripheralSettings gets the peripheral settings
func (a *App) GetPeripheralSettings(protocolName, peripheralSN string) (map[string]any, error) {
return a.hardwareManager.GetPeripheralSettings(peripheralSN)
}
// UpdatePeripheralSettings updates a specific setting of a peripheral
func (a *App) UpdatePeripheralSettings(protocolName, peripheralID string, settings map[string]any) error {
// 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)
}
// Save the settings in the application
if a.projectInfo.PeripheralsInfo == nil {
a.projectInfo.PeripheralsInfo = make(map[string]hardware.PeripheralInfo)
@@ -62,65 +55,18 @@ func (a *App) UpdatePeripheralSettings(protocolName, peripheralID string, settin
pInfo.Settings = settings
a.projectInfo.PeripheralsInfo[peripheralID] = pInfo
// Apply changes in the peripheral
return f.SetPeripheralSettings(a.ctx, peripheralID, pInfo.Settings)
return a.hardwareManager.SetPeripheralSettings(a.ctx, peripheralID, pInfo.Settings)
}
// RemovePeripheral removes a peripheral from the project
func (a *App) RemovePeripheral(peripheralData hardware.PeripheralInfo) error {
// Unregister the peripheral from the finder
f, err := a.hardwareManager.GetFinder(peripheralData.ProtocolName)
err := a.hardwareManager.UnregisterPeripheral(a.ctx, peripheralData)
if err != nil {
log.Err(err).Str("file", "peripherals").Str("protocolName", peripheralData.ProtocolName).Msg("unable to find the finder")
return fmt.Errorf("unable to find the finder")
}
err = f.UnregisterPeripheral(a.ctx, peripheralData)
if err != nil {
log.Err(err).Str("file", "peripherals").Str("peripheralID", peripheralData.SerialNumber).Msg("unable to unregister this peripheral")
return fmt.Errorf("unable to unregister this peripheral")
return fmt.Errorf("unable to unregister this peripheral: %w", err)
}
// Remove the peripheral ID from the project
delete(a.projectInfo.PeripheralsInfo, peripheralData.SerialNumber)
log.Info().Str("file", "peripheral").Str("protocolName", peripheralData.ProtocolName).Str("periphID", peripheralData.SerialNumber).Msg("peripheral removed from project")
return nil
}
// FOR TESTING PURPOSE ONLY
// 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) 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)
// }