fix faulty data send to artNet with new chan queue

This commit is contained in:
Adrian Zuercher
2025-07-13 19:56:00 +02:00
parent 258323f5b7
commit 76a036707f
4 changed files with 43 additions and 24 deletions

View File

@@ -29,6 +29,7 @@ type Bus struct {
Resubscribe *[]json_dataModels.Subscription `yaml:"-" json:"resubscribe"`
Watchdog context.CancelFunc `yaml:"-" json:"-"`
Reachable bool `yaml:"-" json:"-"`
Send chan *DMX `yaml:"-" json:"-"`
}
// adds new Art-Net interface to driver port 0 = 6454 (default art-net)
@@ -102,7 +103,7 @@ func (b *Bus) Poll(interval time.Duration) error {
}
// start bus
func (b *Bus) Start(log *logging.Logger) {
func (b *Bus) Start(log *logging.Logger) error {
var ctx context.Context
ctx, b.Watchdog = context.WithCancel(context.Background())
@@ -132,26 +133,7 @@ func (b *Bus) Start(log *logging.Logger) {
}
}
}()
}
// stop bus
func (b *Bus) Stop() {
if b.Watchdog != nil {
b.Watchdog()
}
}
// status bus
func (b *Bus) Status() bool {
return b.Watchdog != nil
}
// send dmx data
func (b *Bus) SendData() error {
if !b.Reachable {
return nil
}
// Send packet over UDP
conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
IP: net.ParseIP(b.Ip),
Port: *b.Port,
@@ -160,11 +142,38 @@ func (b *Bus) SendData() error {
if err != nil {
return err
}
defer conn.Close()
_, err = conn.Write(NewArtNetPackage(b.Data))
b.Send = make(chan *DMX, 1024)
return err
go func() {
defer conn.Close()
//close send channel
close(b.Send)
for send := range b.Send {
_, err = conn.Write(NewArtNetPackage(send))
if err != nil {
log.Error("bus.Start", err)
return
}
time.Sleep(23 * time.Millisecond)
}
}()
return nil
}
// stop bus
func (b *Bus) Stop() {
if b.Watchdog != nil {
//cancels context
b.Watchdog()
//close send channel
close(b.Send)
}
}
// status bus
func (b *Bus) Status() bool {
return b.Watchdog != nil
}
func (b *Bus) ParsePayload(c *gin.Context) error {