21-peripherals-settings (#22)

Added the concept of peripheral settings.

Reviewed-on: #22
This commit was merged in pull request #22.
This commit is contained in:
2025-01-25 17:43:45 +00:00
parent 0e3f57f5fb
commit c7fe171cb4
10 changed files with 258 additions and 52 deletions

View File

@@ -2,6 +2,7 @@ package hardware
import (
"context"
"fmt"
"github.com/rs/zerolog/log"
)
@@ -10,6 +11,8 @@ import (
type OS2LPeripheral struct {
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
@@ -17,6 +20,8 @@ func NewOS2LPeripheral(name string, serialNumber string) *OS2LPeripheral {
log.Trace().Str("file", "OS2LPeripheral").Str("name", name).Str("s/n", serialNumber).Msg("OS2L peripheral created")
return &OS2LPeripheral{
name: name,
serverIP: "127.0.0.1",
serverPort: 9995,
serialNumber: serialNumber,
}
}
@@ -45,11 +50,49 @@ func (p *OS2LPeripheral) Deactivate(ctx context.Context) error {
return nil
}
// 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
}
// SetDeviceProperty - not implemented for this kind of peripheral
func (p *OS2LPeripheral) SetDeviceProperty(context.Context, uint32, uint32, byte) error {
return nil
}
// GetSettings gets the peripheral settings
func (p *OS2LPeripheral) GetSettings() map[string]interface{} {
return map[string]interface{}{
"os2lIp": p.serverIP,
"os2lPort": p.serverPort,
}
}
// GetInfo gets the peripheral information
func (p *OS2LPeripheral) GetInfo() PeripheralInfo {
return PeripheralInfo{