generated from thinkode/modelRepository
73 lines
3.0 KiB
Go
73 lines
3.0 KiB
Go
package main
|
|
|
|
import (
|
|
"dmxconnect/hardware"
|
|
"fmt"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
// AddPeripheral adds a peripheral to the project
|
|
func (a *App) AddPeripheral(peripheralData hardware.PeripheralInfo) (string, error) {
|
|
// Register this new peripheral
|
|
serialNumber, err := a.hardwareManager.RegisterPeripheral(a.ctx, peripheralData)
|
|
if err != nil {
|
|
return "", fmt.Errorf("unable to register the peripheral '%s': %w", serialNumber, err)
|
|
}
|
|
log.Trace().Str("file", "peripheral").Str("protocolName", peripheralData.ProtocolName).Str("periphID", serialNumber).Msg("device registered to the finder")
|
|
|
|
// 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[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
|
|
}
|
|
|
|
// 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 {
|
|
return "", fmt.Errorf("unable to find the appropriate finder (%s): %w", peripheralData.ProtocolName, err)
|
|
}
|
|
// 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 {
|
|
// 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
|
|
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
|
|
err := a.hardwareManager.UnregisterPeripheral(a.ctx, peripheralData)
|
|
if err != nil {
|
|
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
|
|
}
|