mirror of
https://github.com/onyx-and-iris/xair-cli.git
synced 2026-02-26 08:19:11 +00:00
we no longer need to pass in the kind
remove option function WithKind
This commit is contained in:
parent
8a452c83b9
commit
3c47d12719
@ -121,7 +121,6 @@ func connect(config Config) (*xair.X32Client, error) {
|
||||
client, err := xair.NewX32Client(
|
||||
config.Host,
|
||||
config.Port,
|
||||
xair.WithKind("x32"),
|
||||
xair.WithTimeout(config.Timeout),
|
||||
)
|
||||
if err != nil {
|
||||
|
||||
@ -119,7 +119,6 @@ func connect(config Config) (*xair.XAirClient, error) {
|
||||
client, err := xair.NewXAirClient(
|
||||
config.Host,
|
||||
config.Port,
|
||||
xair.WithKind("xair"),
|
||||
xair.WithTimeout(config.Timeout),
|
||||
)
|
||||
if err != nil {
|
||||
|
||||
@ -18,9 +18,9 @@ var x32AddressMap = map[string]string{
|
||||
"snapshot": "/-snap",
|
||||
}
|
||||
|
||||
func addressMapForMixerKind(kind MixerKind) map[string]string {
|
||||
func addressMapFromMixerKind(kind mixerKind) map[string]string {
|
||||
switch kind {
|
||||
case KindX32:
|
||||
case kindX32:
|
||||
return x32AddressMap
|
||||
default:
|
||||
return xairAddressMap
|
||||
|
||||
@ -2,7 +2,6 @@ package xair
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/charmbracelet/log"
|
||||
@ -11,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
engine
|
||||
*engine
|
||||
}
|
||||
|
||||
type XAirClient struct {
|
||||
@ -34,50 +33,15 @@ type X32Client struct {
|
||||
Snapshot *Snapshot
|
||||
}
|
||||
|
||||
func createEngine(mixerIP string, mixerPort int, opts ...Option) (*engine, error) {
|
||||
localAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf(":%d", 0))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to resolve local address: %v", err)
|
||||
}
|
||||
|
||||
conn, err := net.ListenUDP("udp", localAddr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create UDP connection: %v", err)
|
||||
}
|
||||
|
||||
mixerAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", mixerIP, mixerPort))
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
return nil, fmt.Errorf("failed to resolve mixer address: %v", err)
|
||||
}
|
||||
|
||||
log.Debugf("Local UDP connection: %s ", conn.LocalAddr().String())
|
||||
|
||||
e := &engine{
|
||||
timeout: 100 * time.Millisecond,
|
||||
conn: conn,
|
||||
mixerAddr: mixerAddr,
|
||||
parser: newParser(),
|
||||
done: make(chan bool),
|
||||
respChan: make(chan *osc.Message, 100),
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(e)
|
||||
}
|
||||
|
||||
return e, nil
|
||||
}
|
||||
|
||||
// NewX32Client creates a new X32Client instance
|
||||
func NewX32Client(mixerIP string, mixerPort int, opts ...Option) (*X32Client, error) {
|
||||
e, err := createEngine(mixerIP, mixerPort, opts...)
|
||||
e, err := newEngine(mixerIP, mixerPort, kindX32, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c := &X32Client{
|
||||
Client: Client{*e},
|
||||
Client: Client{e},
|
||||
}
|
||||
c.Main = newMainStereo(&c.Client)
|
||||
c.MainMono = newMainMono(&c.Client)
|
||||
@ -92,13 +56,13 @@ func NewX32Client(mixerIP string, mixerPort int, opts ...Option) (*X32Client, er
|
||||
|
||||
// NewXAirClient creates a new XAirClient instance
|
||||
func NewXAirClient(mixerIP string, mixerPort int, opts ...Option) (*XAirClient, error) {
|
||||
e, err := createEngine(mixerIP, mixerPort, opts...)
|
||||
e, err := newEngine(mixerIP, mixerPort, kindXAir, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c := &XAirClient{
|
||||
Client: Client{*e},
|
||||
Client: Client{e},
|
||||
}
|
||||
c.Main = newMainStereo(&c.Client)
|
||||
c.Strip = newStrip(&c.Client)
|
||||
|
||||
@ -14,7 +14,7 @@ type parser interface {
|
||||
}
|
||||
|
||||
type engine struct {
|
||||
Kind MixerKind
|
||||
Kind mixerKind
|
||||
timeout time.Duration
|
||||
conn *net.UDPConn
|
||||
mixerAddr *net.UDPAddr
|
||||
@ -26,6 +26,42 @@ type engine struct {
|
||||
respChan chan *osc.Message
|
||||
}
|
||||
|
||||
func newEngine(mixerIP string, mixerPort int, kind mixerKind, opts ...Option) (*engine, error) {
|
||||
localAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf(":%d", 0))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to resolve local address: %v", err)
|
||||
}
|
||||
|
||||
conn, err := net.ListenUDP("udp", localAddr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create UDP connection: %v", err)
|
||||
}
|
||||
|
||||
mixerAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", mixerIP, mixerPort))
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
return nil, fmt.Errorf("failed to resolve mixer address: %v", err)
|
||||
}
|
||||
|
||||
log.Debugf("Local UDP connection: %s ", conn.LocalAddr().String())
|
||||
|
||||
e := &engine{
|
||||
timeout: 100 * time.Millisecond,
|
||||
conn: conn,
|
||||
mixerAddr: mixerAddr,
|
||||
parser: newParser(),
|
||||
addressMap: addressMapFromMixerKind(kind),
|
||||
done: make(chan bool),
|
||||
respChan: make(chan *osc.Message, 100),
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(e)
|
||||
}
|
||||
|
||||
return e, nil
|
||||
}
|
||||
|
||||
// receiveLoop handles incoming OSC messages
|
||||
func (e *engine) receiveLoop() {
|
||||
buffer := make([]byte, 4096)
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
package xair
|
||||
|
||||
type MixerKind string
|
||||
type mixerKind string
|
||||
|
||||
const (
|
||||
KindXAir MixerKind = "xair"
|
||||
KindX32 MixerKind = "x32"
|
||||
kindXAir mixerKind = "xair"
|
||||
kindX32 mixerKind = "x32"
|
||||
)
|
||||
|
||||
@ -4,13 +4,6 @@ import "time"
|
||||
|
||||
type Option func(*engine)
|
||||
|
||||
func WithKind(kind string) Option {
|
||||
return func(e *engine) {
|
||||
e.Kind = MixerKind(kind)
|
||||
e.addressMap = addressMapForMixerKind(e.Kind)
|
||||
}
|
||||
}
|
||||
|
||||
func WithTimeout(timeout time.Duration) Option {
|
||||
return func(e *engine) {
|
||||
e.timeout = timeout
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user