settings save optimizations

This commit is contained in:
2025-01-25 18:30:48 +01:00
parent 9d00b99730
commit 1d6bbdc370
7 changed files with 73 additions and 36 deletions

View File

@@ -16,6 +16,9 @@ func (a *App) AddPeripheral(protocolName string, peripheralID string) error {
return fmt.Errorf("unable to found the peripheral ID '%s'", peripheralID)
}
// 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")
@@ -35,26 +38,23 @@ func (a *App) GetPeripheralSettings(protocolName, peripheralID string) (map[stri
return p.GetSettings(), nil
}
// UpdatePeripheralSetting updates a specific setting of a peripheral
func (a *App) UpdatePeripheralSetting(protocolName, peripheralID, settingName string, settingValue interface{}) error {
// 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 {
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 setting in the application
// Save the settings in the application
if a.projectInfo.PeripheralsInfo == nil {
a.projectInfo.PeripheralsInfo = make(map[string]hardware.PeripheralInfo)
}
if a.projectInfo.PeripheralsInfo[peripheralID].Settings == nil {
a.projectInfo.PeripheralsInfo[peripheralID] = hardware.PeripheralInfo{
Settings: make(map[string]interface{}),
}
}
a.projectInfo.PeripheralsInfo[peripheralID].Settings[settingName] = settingValue
pInfo := a.projectInfo.PeripheralsInfo[peripheralID]
pInfo.Settings = settings
a.projectInfo.PeripheralsInfo[peripheralID] = pInfo
// Apply changes in the peripheral
return p.SetPeripheralSetting(settingName, settingValue)
return p.SetPeripheralSettings(pInfo.Settings)
}
// RemovePeripheral adds a peripheral to the project