4 Commits

Author SHA1 Message Date
Adrian Zuercher
7ef6e614cc update to newest json_data model 2025-07-31 12:27:53 +02:00
Adrian Zuercher
49dff4f609 update json_data and model so more than one address can be revieved from server 2025-07-30 17:16:50 +02:00
Adrian Zuercher
3704edebd5 fix wrong call and missing send set 2025-07-27 13:07:16 +02:00
Adrian Zuercher
a503b71fb6 add read write lock to dmx data 2025-07-26 07:18:26 +02:00
10 changed files with 78 additions and 34 deletions

View File

@@ -78,10 +78,10 @@ func (d *ArtNetDriver) SetValue(bus string, address uint, value uint8) error {
if _, ok := d.Buses[bus]; !ok { if _, ok := d.Buses[bus]; !ok {
return fmt.Errorf("no bus '%s' found", bus) return fmt.Errorf("no bus '%s' found", bus)
} }
if d.Buses[bus].Data == nil { if d.Buses[bus].DMX.Data == nil {
return fmt.Errorf("no dmx data on bus '%s' found", bus) return fmt.Errorf("no dmx data on bus '%s' found", bus)
} }
d.Buses[bus].Data.SetValue(address, value) d.Buses[bus].SetDMXData(address, value)
return nil return nil
} }
@@ -105,7 +105,6 @@ func (d *ArtNetDriver) Connect(ip, id string, port uint) error {
request := json_data.NewResponse() request := json_data.NewResponse()
err = json.Unmarshal(data, &request) err = json.Unmarshal(data, &request)
if err != nil { if err != nil {
return return
} }

View File

@@ -10,8 +10,12 @@ func (d *ArtNetDriver) Publish(pubs ...json_dataModels.Publish) error {
} }
for _, pub := range pubs { for _, pub := range pubs {
if drv, ok := (d.Subscriptions)[pub.Uuid]; ok { if subs, ok := (d.Subscriptions)[pub.Uuid]; ok {
d.SetValue(drv.Bus, drv.Address, uint8(pub.Value.(float64))) for _, sub := range subs {
for _, address := range sub.Address {
d.SetValue(sub.Bus, address, uint8(pub.Value.(float64)))
}
}
} }
} }
d.SendData() d.SendData()

View File

@@ -14,7 +14,12 @@ func (d *ArtNetDriver) Subscribe(subs ...json_dataModels.Subscription) {
for _, sub := range subs { for _, sub := range subs {
if drv, ok := (*sub.Drivers)[sub.Driver]; ok { if drv, ok := (*sub.Drivers)[sub.Driver]; ok {
d.Subscriptions.AddSubscription(sub.Uuid, drv) d.Subscriptions.AddSubscription(sub.Uuid, drv)
d.SetValue(drv.Bus, drv.Address, uint8(sub.Value.(float64))) for _, bus := range drv.Buses {
for _, address := range bus.Address {
d.SetValue(bus.Name, address, uint8(sub.Value.(float64)))
}
}
} }
} }
d.SendData() d.SendData()

2
go.mod
View File

@@ -9,7 +9,7 @@ require (
github.com/google/uuid v1.6.0 github.com/google/uuid v1.6.0
github.com/gorilla/websocket v1.5.3 github.com/gorilla/websocket v1.5.3
github.com/tatsushid/go-fastping v0.0.0-20160109021039-d7bb493dee3e github.com/tatsushid/go-fastping v0.0.0-20160109021039-d7bb493dee3e
github.com/tecamino/tecamino-json_data v0.0.20 github.com/tecamino/tecamino-json_data v0.0.31
github.com/tecamino/tecamino-logger v0.2.0 github.com/tecamino/tecamino-logger v0.2.0
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1
) )

4
go.sum
View File

@@ -65,8 +65,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tatsushid/go-fastping v0.0.0-20160109021039-d7bb493dee3e h1:nt2877sKfojlHCTOBXbpWjBkuWKritFaGIfgQwbQUls= github.com/tatsushid/go-fastping v0.0.0-20160109021039-d7bb493dee3e h1:nt2877sKfojlHCTOBXbpWjBkuWKritFaGIfgQwbQUls=
github.com/tatsushid/go-fastping v0.0.0-20160109021039-d7bb493dee3e/go.mod h1:B4+Kq1u5FlULTjFSM707Q6e/cOHFv0z/6QRoxubDIQ8= github.com/tatsushid/go-fastping v0.0.0-20160109021039-d7bb493dee3e/go.mod h1:B4+Kq1u5FlULTjFSM707Q6e/cOHFv0z/6QRoxubDIQ8=
github.com/tecamino/tecamino-json_data v0.0.20 h1:0YHjcGq4T37Z0mu24CMbFA5zgYiUXZNrI5C44nvWexU= github.com/tecamino/tecamino-json_data v0.0.31 h1:7zFbANj1Lihr64CCCh3O+9hxxsMcEPniRJZ5NgPrS5Y=
github.com/tecamino/tecamino-json_data v0.0.20/go.mod h1:LLlyD7Wwqplb2BP4PeO86EokEcTRidlW5MwgPd1T2JY= github.com/tecamino/tecamino-json_data v0.0.31/go.mod h1:LLlyD7Wwqplb2BP4PeO86EokEcTRidlW5MwgPd1T2JY=
github.com/tecamino/tecamino-logger v0.2.0 h1:NPH/Gg9qRhmVoW8b39i1eXu/LEftHc74nyISpcRG+XU= github.com/tecamino/tecamino-logger v0.2.0 h1:NPH/Gg9qRhmVoW8b39i1eXu/LEftHc74nyISpcRG+XU=
github.com/tecamino/tecamino-logger v0.2.0/go.mod h1:0M1E9Uei/qw3e3WA1x3lBo1eP3H5oeYE7GjYrMahnj8= github.com/tecamino/tecamino-logger v0.2.0/go.mod h1:0M1E9Uei/qw3e3WA1x3lBo1eP3H5oeYE7GjYrMahnj8=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=

View File

@@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"net" "net"
"net/http" "net/http"
"sync"
"time" "time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@@ -25,9 +26,10 @@ type Bus struct {
Name string `yaml:"name" json:"name"` Name string `yaml:"name" json:"name"`
Ip string `yaml:"ip" json:"ip"` Ip string `yaml:"ip" json:"ip"`
Port *int `yaml:"port" json:"port,omitempty"` Port *int `yaml:"port" json:"port,omitempty"`
Data *DMX `yaml:"-" json:"-"` DMX *DMX `yaml:"-" json:"-"`
Resubscribe *[]json_dataModels.Subscription `yaml:"-" json:"resubscribe"` Resubscribe *[]json_dataModels.Subscription `yaml:"-" json:"resubscribe"`
Watchdog context.CancelFunc `yaml:"-" json:"-"` Watchdog context.CancelFunc `yaml:"-" json:"-"`
mu sync.Mutex `yaml:"-" json:"-"`
Reachable bool `yaml:"-" json:"-"` Reachable bool `yaml:"-" json:"-"`
Send bool `yaml:"-" json:"-"` Send bool `yaml:"-" json:"-"`
} }
@@ -42,7 +44,7 @@ func NewBus(name, ip string, port int) *Bus {
Name: name, Name: name,
Ip: ip, Ip: ip,
Port: &port, Port: &port,
Data: NewDMXUniverse(), DMX: NewDMXUniverse(),
} }
return &i 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 { if err != nil {
return err return err
} }
@@ -155,18 +157,25 @@ func (b *Bus) Start(log *logging.Logger) error {
}() }()
for range ticker.C { for range ticker.C {
if !b.Send { if !b.Send {
continue continue
} }
b.Send = false b.Send = false
_, err = conn.Write(NewArtNetPackage(b.Data)) b.mu.Lock()
data := NewDMXUniverse()
copy(data.Data, b.DMX.GetDMXData())
b.mu.Unlock()
_, err = conn.Write(NewArtNetPackage(data))
if err != nil { if err != nil {
log.Error("bus.Start", err) log.Error("bus.Start", err)
return return
} }
} }
}() }()
return nil return nil
} }
@@ -206,6 +215,13 @@ func (b *Bus) ParsePayload(c *gin.Context) error {
return nil return nil
} }
func (b *Bus) SetDMXData(channel uint, value uint8) error {
fmt.Println(100, channel, value)
b.DMX.SetValue(channel, value)
b.Send = true
return nil
}
func isUDPReachable(ip string) (recieved bool, err error) { func isUDPReachable(ip string) (recieved bool, err error) {
p := fastping.NewPinger() p := fastping.NewPinger()
ra, err := net.ResolveIPAddr("ip4:icmp", ip) ra, err := net.ResolveIPAddr("ip4:icmp", ip)

View File

@@ -1,12 +1,29 @@
package models package models
type DMX []byte import (
"sync"
)
type DMX struct {
Data []byte
mu sync.Mutex
}
func NewDMXUniverse() *DMX { func NewDMXUniverse() *DMX {
dmx := make(DMX, 512) return &DMX{
return &dmx Data: make([]byte, 512),
}
}
func (d *DMX) GetDMXData() (data []byte) {
d.mu.Lock()
data = d.Data
d.mu.Unlock()
return
} }
func (d *DMX) SetValue(channel uint, value uint8) { func (d *DMX) SetValue(channel uint, value uint8) {
(*d)[channel] = value d.mu.Lock()
d.Data[channel] = value
d.mu.Unlock()
} }

View File

@@ -16,14 +16,14 @@ type Package []byte
func NewArtNetPackage(data *DMX) Package { func NewArtNetPackage(data *DMX) Package {
// Build ArtDMX packet // Build ArtDMX packet
packet := &bytes.Buffer{} packet := &bytes.Buffer{}
packet.WriteString(protocolID) // Art-Net ID packet.WriteString(protocolID) // Art-Net ID
binary.Write(packet, binary.LittleEndian, uint16(opCode)) // OpCode (OpDmx) binary.Write(packet, binary.LittleEndian, uint16(opCode)) // OpCode (OpDmx)
packet.WriteByte(0x00) // Protocol Version High packet.WriteByte(0x00) // Protocol Version High
packet.WriteByte(14) // Protocol Version Low (14 for Art-Net 4) packet.WriteByte(14) // Protocol Version Low (14 for Art-Net 4)
packet.WriteByte(0x00) // Sequence packet.WriteByte(0x00) // Sequence
packet.WriteByte(0x00) // Physical packet.WriteByte(0x00) // Physical
binary.Write(packet, binary.BigEndian, uint16(0)) // Universe (net:subuni, usually 0) binary.Write(packet, binary.BigEndian, uint16(0)) // Universe (net:subuni, usually 0)
binary.Write(packet, binary.BigEndian, uint16(len(*data))) // Length binary.Write(packet, binary.BigEndian, uint16(len(data.Data))) // Length
packet.Write(*data) packet.Write(data.Data)
return packet.Bytes() return packet.Bytes()
} }

View File

@@ -5,11 +5,11 @@ import (
json_dataModels "github.com/tecamino/tecamino-json_data/models" json_dataModels "github.com/tecamino/tecamino-json_data/models"
) )
type Subscriptions map[uuid.UUID]Subscription type Subscriptions map[uuid.UUID][]Subscription
type Subscription struct { type Subscription struct {
Bus string Bus string
Address uint Address []uint
} }
func NewSubscriptions() Subscriptions { func NewSubscriptions() Subscriptions {
@@ -17,8 +17,11 @@ func NewSubscriptions() Subscriptions {
} }
func (s *Subscriptions) AddSubscription(uid uuid.UUID, drv *json_dataModels.Driver) { func (s *Subscriptions) AddSubscription(uid uuid.UUID, drv *json_dataModels.Driver) {
(*s)[uid] = Subscription{ subs := []Subscription{}
Bus: drv.Bus, for _, bus := range drv.Buses {
Address: drv.Address, sub := Subscription{Bus: bus.Name}
sub.Address = append(sub.Address, bus.Address...)
subs = append(subs, sub)
} }
(*s)[uid] = subs
} }

View File

@@ -89,10 +89,10 @@ func (c *Client) Read() {
} }
c.conn.SetReadDeadline(time.Now().Add(pongWait)) c.conn.SetReadDeadline(time.Now().Add(pongWait))
for c.Connected {
c.conn.SetReadDeadline(time.Now().Add(pongWait))
msgType, msg, err := c.conn.ReadMessage()
for c.Connected {
msgType, msg, err := c.conn.ReadMessage()
c.conn.SetReadDeadline(time.Now().Add(pongWait))
if err != nil { if err != nil {
c.handleError(fmt.Errorf("read error (ip:%s): %w", c.ip, err)) c.handleError(fmt.Errorf("read error (ip:%s): %w", c.ip, err))
return return