Files
dmxconnect/hardware/OS2LPeripheral.go

104 lines
2.9 KiB
Go
Raw Normal View History

package hardware
import (
"context"
2025-01-25 18:30:48 +01:00
"fmt"
"github.com/rs/zerolog/log"
)
// OS2LPeripheral contains the data of an OS2L peripheral
type OS2LPeripheral struct {
2025-01-25 18:30:48 +01:00
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")
return &OS2LPeripheral{
name: name,
2025-01-25 18:30:48 +01:00
serverIP: "127.0.0.1",
serverPort: 9995,
serialNumber: serialNumber,
}
}
// Connect connects the MIDI peripheral
func (p *OS2LPeripheral) Connect(ctx context.Context) error {
log.Info().Str("file", "OS2LPeripheral").Msg("OS2L peripheral connected")
return nil
}
// Disconnect disconnects the MIDI peripheral
func (p *OS2LPeripheral) Disconnect(ctx context.Context) error {
log.Info().Str("file", "OS2LPeripheral").Msg("OS2L peripheral disconnected")
return nil
}
// Activate activates the MIDI peripheral
func (p *OS2LPeripheral) Activate(ctx context.Context) error {
log.Info().Str("file", "OS2LPeripheral").Msg("OS2L peripheral activated")
return nil
}
// Deactivate deactivates the MIDI peripheral
func (p *OS2LPeripheral) Deactivate(ctx context.Context) error {
log.Info().Str("file", "OS2LPeripheral").Msg("OS2L peripheral deactivated")
return nil
}
2025-01-25 18:30:48 +01:00
// 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")
}
2025-01-25 16:00:02 +01:00
return nil
}
// SetDeviceProperty - not implemented for this kind of peripheral
func (p *OS2LPeripheral) SetDeviceProperty(context.Context, uint32, uint32, byte) error {
return nil
}
2025-01-25 11:06:03 +01:00
// GetSettings gets the peripheral settings
func (p *OS2LPeripheral) GetSettings() map[string]interface{} {
2025-01-25 18:30:48 +01:00
return map[string]interface{}{
"os2lIp": p.serverIP,
"os2lPort": p.serverPort,
}
2025-01-25 11:06:03 +01:00
}
// GetInfo gets the peripheral information
func (p *OS2LPeripheral) GetInfo() PeripheralInfo {
return PeripheralInfo{
Name: p.name,
SerialNumber: p.serialNumber,
ProtocolName: "OS2L",
}
}