package hardware import ( "context" "fmt" "time" "github.com/rs/zerolog/log" "github.com/wailsapp/wails/v2/pkg/runtime" ) // OS2LPeripheral contains the data of an OS2L peripheral type OS2LPeripheral struct { info PeripheralInfo // The basic info for this peripheral serverIP string // OS2L server IP serverPort int // OS2L server port } // NewOS2LPeripheral creates a new OS2L peripheral func NewOS2LPeripheral(peripheralData PeripheralInfo) (*OS2LPeripheral, error) { log.Trace().Str("file", "OS2LPeripheral").Str("name", peripheralData.Name).Str("s/n", peripheralData.SerialNumber).Msg("OS2L peripheral created") return &OS2LPeripheral{ info: peripheralData, serverIP: "127.0.0.1", serverPort: 9005, }, nil } // Connect connects the MIDI peripheral func (p *OS2LPeripheral) Connect(ctx context.Context) error { log.Info().Str("file", "OS2LPeripheral").Msg("OS2L peripheral connected") go func() { runtime.EventsEmit(ctx, string(PeripheralStatus), p.info, "connecting") time.Sleep(5 * time.Second) runtime.EventsEmit(ctx, string(PeripheralStatus), p.info, "disconnected") }() return nil } // Disconnect disconnects the MIDI peripheral func (p *OS2LPeripheral) Disconnect() 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 } // SetSettings sets a specific setting for this peripheral func (p *OS2LPeripheral) SetSettings(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 p.info }