add read write lock to dmx data

This commit is contained in:
Adrian Zuercher
2025-07-26 07:18:26 +02:00
parent 05409c2544
commit a503b71fb6
5 changed files with 49 additions and 23 deletions

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"net"
"net/http"
"sync"
"time"
"github.com/gin-gonic/gin"
@@ -25,9 +26,10 @@ type Bus struct {
Name string `yaml:"name" json:"name"`
Ip string `yaml:"ip" json:"ip"`
Port *int `yaml:"port" json:"port,omitempty"`
Data *DMX `yaml:"-" json:"-"`
DMX *DMX `yaml:"-" json:"-"`
Resubscribe *[]json_dataModels.Subscription `yaml:"-" json:"resubscribe"`
Watchdog context.CancelFunc `yaml:"-" json:"-"`
mu sync.Mutex `yaml:"-" json:"-"`
Reachable bool `yaml:"-" json:"-"`
Send bool `yaml:"-" json:"-"`
}
@@ -42,7 +44,7 @@ func NewBus(name, ip string, port int) *Bus {
Name: name,
Ip: ip,
Port: &port,
Data: NewDMXUniverse(),
DMX: NewDMXUniverse(),
}
return &i
}
@@ -90,7 +92,7 @@ func (b *Bus) Poll(interval time.Duration) error {
}
}()
_, err = conn.Write(NewArtNetPackage(b.Data))
_, err = conn.Write(NewArtNetPackage(b.DMX))
if err != nil {
return err
}
@@ -161,7 +163,12 @@ func (b *Bus) Start(log *logging.Logger) error {
b.Send = false
_, err = conn.Write(NewArtNetPackage(b.Data))
b.mu.Lock()
data := NewDMXUniverse()
copy(data.Data, data.GetDMXData())
b.mu.Unlock()
_, err = conn.Write(NewArtNetPackage(data))
if err != nil {
log.Error("bus.Start", err)
return
@@ -206,6 +213,11 @@ func (b *Bus) ParsePayload(c *gin.Context) error {
return nil
}
func (b *Bus) SetDMXData(channel uint, value uint8) error {
b.DMX.SetValue(channel, value)
return nil
}
func isUDPReachable(ip string) (recieved bool, err error) {
p := fastping.NewPinger()
ra, err := net.ResolveIPAddr("ip4:icmp", ip)