3 Commits

Author SHA1 Message Date
Adrian Zuercher
76a036707f fix faulty data send to artNet with new chan queue 2025-07-13 19:56:00 +02:00
Adrian Zuercher
258323f5b7 add new executable name 2025-07-13 19:54:27 +02:00
Adrian Zuercher
473eb22b97 fix missing subscribe and publish on artNet Protocol 2025-06-29 15:36:14 +02:00
5 changed files with 83 additions and 31 deletions

3
.gitignore vendored
View File

@@ -1,3 +1,4 @@
*.cfg *.cfg
*.log *.log
artNetDriver-arm64 artNetDriver-arm64
tecamino-driver-artNet-linux-arm64

View File

@@ -4,6 +4,7 @@ import (
"artNet/cfg" "artNet/cfg"
"artNet/models" "artNet/models"
ws "artNet/websocket" ws "artNet/websocket"
"encoding/json"
"fmt" "fmt"
"path" "path"
@@ -81,7 +82,7 @@ func (d *ArtNetDriver) SetValue(bus string, address uint, value uint8) error {
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].Data.SetValue(address, value)
return d.Buses[bus].SendData() return nil
} }
// connect to websocket server and listen to subscriptions // connect to websocket server and listen to subscriptions
@@ -101,7 +102,23 @@ func (d *ArtNetDriver) Connect(ip, id string, port uint) error {
} }
client.OnMessage = func(data []byte) { client.OnMessage = func(data []byte) {
fmt.Println(100, string(data))
request := json_data.NewResponse()
err = json.Unmarshal(data, &request)
if err != nil {
return
}
if request.Subscribe != nil {
d.Subscribe(request.Subscribe...)
}
if request.Publish != nil {
d.Publish(request.Publish...)
}
}
if err != nil {
d.Log.Error("artNet.Connect", err)
return err
} }
req := json_data.NewRequest() req := json_data.NewRequest()
@@ -112,10 +129,15 @@ func (d *ArtNetDriver) Connect(ip, id string, port uint) error {
d.Log.Error("websocket send data", err) d.Log.Error("websocket send data", err)
} }
for { for err := range errChan {
select { return err
case err := <-errChan: }
return err return nil
} }
// send data to all buses that the send flage is true
func (d *ArtNetDriver) SendData() {
for _, bus := range d.Buses {
bus.Send <- bus.Data
} }
} }

19
driver/publish.go Normal file
View File

@@ -0,0 +1,19 @@
package driver
import (
json_dataModels "github.com/tecamino/tecamino-json_data/models"
)
func (d *ArtNetDriver) Publish(pubs ...json_dataModels.Publish) error {
if d.Subscriptions == nil {
return nil
}
for _, pub := range pubs {
if drv, ok := (d.Subscriptions)[pub.Uuid]; ok {
d.SetValue(drv.Bus, drv.Address, uint8(pub.Value.(float64)))
}
}
d.SendData()
return nil
}

View File

@@ -17,4 +17,5 @@ func (d *ArtNetDriver) Subscribe(subs ...json_dataModels.Subscription) {
d.SetValue(drv.Bus, drv.Address, uint8(sub.Value.(float64))) d.SetValue(drv.Bus, drv.Address, uint8(sub.Value.(float64)))
} }
} }
d.SendData()
} }

View File

@@ -29,6 +29,7 @@ type Bus struct {
Resubscribe *[]json_dataModels.Subscription `yaml:"-" json:"resubscribe"` Resubscribe *[]json_dataModels.Subscription `yaml:"-" json:"resubscribe"`
Watchdog context.CancelFunc `yaml:"-" json:"-"` Watchdog context.CancelFunc `yaml:"-" json:"-"`
Reachable bool `yaml:"-" json:"-"` Reachable bool `yaml:"-" json:"-"`
Send chan *DMX `yaml:"-" json:"-"`
} }
// adds new Art-Net interface to driver port 0 = 6454 (default art-net) // 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 // start bus
func (b *Bus) Start(log *logging.Logger) { func (b *Bus) Start(log *logging.Logger) error {
var ctx context.Context var ctx context.Context
ctx, b.Watchdog = context.WithCancel(context.Background()) 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{ conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
IP: net.ParseIP(b.Ip), IP: net.ParseIP(b.Ip),
Port: *b.Port, Port: *b.Port,
@@ -160,11 +142,38 @@ func (b *Bus) SendData() error {
if err != nil { if err != nil {
return err 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 { func (b *Bus) ParsePayload(c *gin.Context) error {