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

@@ -2,27 +2,27 @@ package hardware
import (
"context"
"fmt"
"github.com/rs/zerolog/log"
)
// 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
settings map[string]interface{} // The settings of the peripheral
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
}
// 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")
settings := make(map[string]interface{})
settings["os2lIpSetting"] = "192.168.1.1"
settings["os2lPortSetting"] = 9995
return &OS2LPeripheral{
name: name,
serverIP: "127.0.0.1",
serverPort: 9995,
serialNumber: serialNumber,
settings: settings,
}
}
@@ -50,9 +50,33 @@ func (p *OS2LPeripheral) Deactivate(ctx context.Context) error {
return nil
}
// SetPeripheralSetting sets a specific setting for this peripheral
func (p *OS2LPeripheral) SetPeripheralSetting(settingName string, settingValue interface{}) error {
p.settings[settingName] = settingValue
// SetPeripheralSettings sets a specific setting for this peripheral
func (p *OS2LPeripheral) SetPeripheralSettings(settings map[string]interface{}) error {
// Check if the IP exists
serverIP, found := settings["os2lIp"]
if !found {
return fmt.Errorf("Unable to find the OS2L server IP")
}
// Check if it is a string
ipSetting, ok := serverIP.(string)
if ok {
p.serverIP = ipSetting
} else {
return fmt.Errorf("The specified IP is not a string")
}
// Check if the port exists
serverPort, found := settings["os2lPort"]
if !found {
return fmt.Errorf("Unable to find the OS2L server port")
}
// Check if it is a float and convert to int
portFloat, ok := serverPort.(float64)
if ok {
p.serverPort = int(portFloat)
} else {
return fmt.Errorf("The specified port is not an int")
}
return nil
}
@@ -63,7 +87,10 @@ func (p *OS2LPeripheral) SetDeviceProperty(context.Context, uint32, uint32, byte
// GetSettings gets the peripheral settings
func (p *OS2LPeripheral) GetSettings() map[string]interface{} {
return p.settings
return map[string]interface{}{
"os2lIp": p.serverIP,
"os2lPort": p.serverPort,
}
}
// GetInfo gets the peripheral information