2025-01-18 14:53:29 +00:00
|
|
|
package hardware
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-01-25 17:43:45 +00:00
|
|
|
"fmt"
|
2025-01-18 14:53:29 +00:00
|
|
|
|
|
|
|
|
"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
|
2025-01-25 17:43:45 +00:00
|
|
|
serverIP string // OS2L server IP
|
|
|
|
|
serverPort int // OS2L server port
|
2025-01-18 14:53:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 17:43:45 +00:00
|
|
|
serverIP: "127.0.0.1",
|
|
|
|
|
serverPort: 9995,
|
2025-01-18 14:53:29 +00:00
|
|
|
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 17:43:45 +00: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")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-18 14:53:29 +00:00
|
|
|
// SetDeviceProperty - not implemented for this kind of peripheral
|
|
|
|
|
func (p *OS2LPeripheral) SetDeviceProperty(context.Context, uint32, uint32, byte) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-25 17:43:45 +00:00
|
|
|
// GetSettings gets the peripheral settings
|
|
|
|
|
func (p *OS2LPeripheral) GetSettings() map[string]interface{} {
|
|
|
|
|
return map[string]interface{}{
|
|
|
|
|
"os2lIp": p.serverIP,
|
|
|
|
|
"os2lPort": p.serverPort,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-18 14:53:29 +00:00
|
|
|
// GetInfo gets the peripheral information
|
|
|
|
|
func (p *OS2LPeripheral) GetInfo() PeripheralInfo {
|
|
|
|
|
return PeripheralInfo{
|
|
|
|
|
Name: p.name,
|
|
|
|
|
SerialNumber: p.serialNumber,
|
|
|
|
|
ProtocolName: "OS2L",
|
|
|
|
|
}
|
|
|
|
|
}
|