2025-01-18 14:53:29 +00:00
package main
import (
"dmxconnect/hardware"
"fmt"
"github.com/rs/zerolog/log"
)
// AddPeripheral adds a peripheral to the project
2025-01-26 12:01:31 +01:00
func ( a * App ) AddPeripheral ( peripheralData hardware . PeripheralInfo ) ( string , error ) {
2025-01-25 17:43:45 +00:00
// Get the peripheral from its finder
2025-01-26 12:01:31 +01:00
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 )
2025-01-18 14:53:29 +00:00
}
2025-01-26 12:01:31 +01:00
// Register this new peripheral
serialNumber , err := f . RegisterPeripheral ( a . ctx , peripheralData )
if err != nil {
2025-11-13 16:00:50 +01:00
return "" , fmt . Errorf ( "unable to register the peripheral '%s': %w" , serialNumber , err )
2025-01-26 12:01:31 +01:00
}
2025-11-13 16:00:50 +01:00
log . Trace ( ) . Str ( "file" , "peripheral" ) . Str ( "protocolName" , peripheralData . ProtocolName ) . Str ( "periphID" , serialNumber ) . Msg ( "device registered to the finder" )
2025-01-26 12:01:31 +01:00
// Rewrite the serialnumber for virtual devices
peripheralData . SerialNumber = serialNumber
2025-01-18 14:53:29 +00:00
// Add the peripheral ID to the project
2025-01-25 17:43:45 +00:00
if a . projectInfo . PeripheralsInfo == nil {
a . projectInfo . PeripheralsInfo = make ( map [ string ] hardware . PeripheralInfo )
}
2025-01-18 14:53:29 +00:00
2025-01-26 12:01:31 +01:00
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
2025-01-18 14:53:29 +00:00
}
2025-01-25 17:43:45 +00:00
// GetPeripheralSettings gets the peripheral settings
2025-11-13 16:00:50 +01:00
func ( a * App ) GetPeripheralSettings ( protocolName , peripheralID string ) ( map [ string ] any , error ) {
2025-01-25 17:43:45 +00:00
// Get the peripheral from its finder
2025-01-26 12:01:31 +01:00
f , err := a . hardwareManager . GetFinder ( protocolName )
if err != nil {
2025-01-25 17:43:45 +00:00
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 )
}
2025-01-26 12:01:31 +01:00
return f . GetPeripheralSettings ( peripheralID )
2025-01-25 17:43:45 +00:00
}
// UpdatePeripheralSettings updates a specific setting of a peripheral
2025-11-13 16:00:50 +01:00
func ( a * App ) UpdatePeripheralSettings ( protocolName , peripheralID string , settings map [ string ] any ) error {
2025-01-26 12:01:31 +01:00
// Sets the settings with the finder
f , err := a . hardwareManager . GetFinder ( protocolName )
if err != nil {
2025-01-25 17:43:45 +00:00
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 )
}
pInfo := a . projectInfo . PeripheralsInfo [ peripheralID ]
pInfo . Settings = settings
a . projectInfo . PeripheralsInfo [ peripheralID ] = pInfo
// Apply changes in the peripheral
2025-11-13 16:00:50 +01:00
return f . SetPeripheralSettings ( a . ctx , peripheralID , pInfo . Settings )
2025-01-25 17:43:45 +00:00
}
2025-08-31 11:15:38 +02:00
// RemovePeripheral removes a peripheral from the project
2025-11-11 19:14:44 +00:00
func ( a * App ) RemovePeripheral ( peripheralData hardware . PeripheralInfo ) error {
2025-01-26 12:01:31 +01:00
// Unregister the peripheral from the finder
2025-11-11 19:14:44 +00:00
f , err := a . hardwareManager . GetFinder ( peripheralData . ProtocolName )
2025-01-18 14:53:29 +00:00
if err != nil {
2025-11-11 19:14:44 +00:00
log . Err ( err ) . Str ( "file" , "peripherals" ) . Str ( "protocolName" , peripheralData . ProtocolName ) . Msg ( "unable to find the finder" )
2025-01-26 12:01:31 +01:00
return fmt . Errorf ( "unable to find the finder" )
2025-01-18 14:53:29 +00:00
}
2025-11-11 19:14:44 +00:00
err = f . UnregisterPeripheral ( a . ctx , peripheralData )
2025-01-18 14:53:29 +00:00
if err != nil {
2025-11-11 19:14:44 +00:00
log . Err ( err ) . Str ( "file" , "peripherals" ) . Str ( "peripheralID" , peripheralData . SerialNumber ) . Msg ( "unable to unregister this peripheral" )
2025-01-26 12:01:31 +01:00
return fmt . Errorf ( "unable to unregister this peripheral" )
2025-01-18 14:53:29 +00:00
}
2025-01-26 12:01:31 +01:00
// Remove the peripheral ID from the project
2025-11-11 19:14:44 +00:00
delete ( a . projectInfo . PeripheralsInfo , peripheralData . SerialNumber )
log . Info ( ) . Str ( "file" , "peripheral" ) . Str ( "protocolName" , peripheralData . ProtocolName ) . Str ( "periphID" , peripheralData . SerialNumber ) . Msg ( "peripheral removed from project" )
2025-01-26 12:01:31 +01:00
return nil
2025-01-18 14:53:29 +00:00
}
// FOR TESTING PURPOSE ONLY
2025-01-26 12:01:31 +01:00
// 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)
// }