implement two separate concrete clients

add one factory function for each type
This commit is contained in:
onyx-and-iris 2026-02-07 03:49:44 +00:00
parent 3fd6c52cad
commit 65d20408b1

View File

@ -12,6 +12,10 @@ import (
type Client struct { type Client struct {
engine engine
}
type XAirClient struct {
Client
Main *Main Main *Main
Strip *Strip Strip *Strip
Bus *Bus Bus *Bus
@ -19,8 +23,18 @@ type Client struct {
Snapshot *Snapshot Snapshot *Snapshot
} }
// NewClient creates a new XAirClient instance type X32Client struct {
func NewClient(mixerIP string, mixerPort int, opts ...Option) (*Client, error) { Client
Main *Main
MainMono *Main
Matrix *Matrix
Strip *Strip
Bus *Bus
HeadAmp *HeadAmp
Snapshot *Snapshot
}
func createEngine(mixerIP string, mixerPort int, opts ...Option) (*engine, error) {
localAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf(":%d", 0)) localAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf(":%d", 0))
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to resolve local address: %v", err) return nil, fmt.Errorf("failed to resolve local address: %v", err)
@ -40,28 +54,57 @@ func NewClient(mixerIP string, mixerPort int, opts ...Option) (*Client, error) {
log.Debugf("Local UDP connection: %s ", conn.LocalAddr().String()) log.Debugf("Local UDP connection: %s ", conn.LocalAddr().String())
e := &engine{ e := &engine{
Kind: KindXAir, timeout: 100 * time.Millisecond,
timeout: 100 * time.Millisecond, conn: conn,
conn: conn, mixerAddr: mixerAddr,
mixerAddr: mixerAddr, parser: newParser(),
parser: newParser(), done: make(chan bool),
addressMap: addressMapForMixerKind(KindXAir), respChan: make(chan *osc.Message, 100),
done: make(chan bool),
respChan: make(chan *osc.Message, 100),
} }
for _, opt := range opts { for _, opt := range opts {
opt(e) opt(e)
} }
c := &Client{ return e, nil
engine: *e, }
// NewX32Client creates a new X32Client instance
func NewX32Client(mixerIP string, mixerPort int, opts ...Option) (*X32Client, error) {
e, err := createEngine(mixerIP, mixerPort, opts...)
if err != nil {
return nil, err
} }
c.Main = newMainStereo(c)
c.Strip = newStrip(c) c := &X32Client{
c.Bus = newBus(c) Client: Client{*e},
c.HeadAmp = newHeadAmp(c) }
c.Snapshot = newSnapshot(c) c.Main = newMainStereo(&c.Client)
c.MainMono = newMainMono(&c.Client)
c.Matrix = newMatrix(&c.Client)
c.Strip = newStrip(&c.Client)
c.Bus = newBus(&c.Client)
c.HeadAmp = newHeadAmp(&c.Client)
c.Snapshot = newSnapshot(&c.Client)
return c, nil
}
// NewXAirClient creates a new XAirClient instance
func NewXAirClient(mixerIP string, mixerPort int, opts ...Option) (*XAirClient, error) {
e, err := createEngine(mixerIP, mixerPort, opts...)
if err != nil {
return nil, err
}
c := &XAirClient{
Client: Client{*e},
}
c.Main = newMainStereo(&c.Client)
c.Strip = newStrip(&c.Client)
c.Bus = newBus(&c.Client)
c.HeadAmp = newHeadAmp(&c.Client)
c.Snapshot = newSnapshot(&c.Client)
return c, nil return c, nil
} }