Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
67f25c76ae | ||
![]() |
7ef6e614cc | ||
![]() |
49dff4f609 | ||
![]() |
3704edebd5 | ||
![]() |
a503b71fb6 | ||
![]() |
05409c2544 | ||
![]() |
b484745ed1 | ||
![]() |
04e306c6da | ||
![]() |
239e0050a1 | ||
![]() |
de4758c563 | ||
![]() |
a85c9ccd66 | ||
![]() |
da78a00446 | ||
![]() |
ba3c55dc34 | ||
![]() |
e0950b44ee |
@@ -36,9 +36,9 @@ jobs:
|
||||
run: |
|
||||
mkdir -p build
|
||||
if [ "${{ matrix.goos }}" == "windows" ]; then
|
||||
GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} go build -o build/tecamino-driver-artNet-${{ matrix.goos }}-${{ matrix.goarch }}.exe main.go
|
||||
GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} go build -ldflags="-s -w" -trimpath -o build/tecamino-driver-artNet-${{ matrix.goos }}-${{ matrix.goarch }}.exe main.go
|
||||
else
|
||||
GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} go build -o build/tecamino-driver-artNet-${{ matrix.goos }}-${{ matrix.goarch }} main.go
|
||||
GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} go build -ldflags="-s -w" -trimpath -o build/tecamino-driver-artNet-${{ matrix.goos }}-${{ matrix.goarch }} main.go
|
||||
fi
|
||||
|
||||
- name: Upload artifacts
|
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
.DS_Store
|
||||
*.cfg
|
||||
*.log
|
||||
artNetDriver-arm64
|
||||
|
@@ -78,10 +78,10 @@ func (d *ArtNetDriver) SetValue(bus string, address uint, value uint8) error {
|
||||
if _, ok := d.Buses[bus]; !ok {
|
||||
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)
|
||||
}
|
||||
d.Buses[bus].Data.SetValue(address, value)
|
||||
d.Buses[bus].SetDMXData(address, value)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -102,15 +102,17 @@ func (d *ArtNetDriver) Connect(ip, id string, port uint) error {
|
||||
}
|
||||
|
||||
client.OnMessage = func(data []byte) {
|
||||
|
||||
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...)
|
||||
}
|
||||
@@ -138,6 +140,6 @@ func (d *ArtNetDriver) Connect(ip, id string, port uint) error {
|
||||
// send data to all buses that the send flage is true
|
||||
func (d *ArtNetDriver) SendData() {
|
||||
for _, bus := range d.Buses {
|
||||
bus.Send <- bus.Data
|
||||
bus.Send = bus.Reachable
|
||||
}
|
||||
}
|
||||
|
@@ -10,8 +10,12 @@ func (d *ArtNetDriver) Publish(pubs ...json_dataModels.Publish) error {
|
||||
}
|
||||
|
||||
for _, pub := range pubs {
|
||||
if drv, ok := (d.Subscriptions)[pub.Uuid]; ok {
|
||||
d.SetValue(drv.Bus, drv.Address, uint8(pub.Value.(float64)))
|
||||
if subs, ok := (d.Subscriptions)[pub.Uuid]; ok {
|
||||
for _, sub := range subs {
|
||||
for _, address := range sub.Address {
|
||||
d.SetValue(sub.Bus, address, uint8(pub.Value.(float64)))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
d.SendData()
|
||||
|
@@ -14,7 +14,12 @@ func (d *ArtNetDriver) Subscribe(subs ...json_dataModels.Subscription) {
|
||||
for _, sub := range subs {
|
||||
if drv, ok := (*sub.Drivers)[sub.Driver]; ok {
|
||||
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()
|
||||
|
2
go.mod
2
go.mod
@@ -9,7 +9,7 @@ require (
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/gorilla/websocket v1.5.3
|
||||
github.com/tatsushid/go-fastping v0.0.0-20160109021039-d7bb493dee3e
|
||||
github.com/tecamino/tecamino-json_data v0.0.16
|
||||
github.com/tecamino/tecamino-json_data v0.0.31
|
||||
github.com/tecamino/tecamino-logger v0.2.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
4
go.sum
4
go.sum
@@ -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/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/tecamino/tecamino-json_data v0.0.16 h1:aZFxnhm4g6WMDPoqy4HosUk7vl0DB0iIcVs8bbT4MzU=
|
||||
github.com/tecamino/tecamino-json_data v0.0.16/go.mod h1:LLlyD7Wwqplb2BP4PeO86EokEcTRidlW5MwgPd1T2JY=
|
||||
github.com/tecamino/tecamino-json_data v0.0.31 h1:7zFbANj1Lihr64CCCh3O+9hxxsMcEPniRJZ5NgPrS5Y=
|
||||
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/go.mod h1:0M1E9Uei/qw3e3WA1x3lBo1eP3H5oeYE7GjYrMahnj8=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
|
1
main.go
1
main.go
@@ -75,6 +75,7 @@ func main() {
|
||||
if err := artNetDriver.Connect(*serverIp, DriverName, *serverPort); err != nil {
|
||||
artNetDriver.Log.Error("main", err)
|
||||
}
|
||||
|
||||
artNetDriver.Log.Info("main", "next reconnecting attempt in 10 seconds")
|
||||
time.Sleep(10 * time.Second)
|
||||
}
|
||||
|
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -25,11 +26,12 @@ 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 chan *DMX `yaml:"-" json:"-"`
|
||||
Send bool `yaml:"-" json:"-"`
|
||||
}
|
||||
|
||||
// adds new Art-Net interface to driver port 0 = 6454 (default art-net)
|
||||
@@ -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
|
||||
}
|
||||
@@ -109,7 +111,7 @@ func (b *Bus) Start(log *logging.Logger) error {
|
||||
|
||||
go func() {
|
||||
var interval time.Duration = 10 * time.Second
|
||||
log.Info("bus.Start", fmt.Sprintf("device:%s ip:%s watchdog stopped", b.Name, b.Ip))
|
||||
log.Info("bus.Start", fmt.Sprintf("device:%s ip:%s watchdog starting", b.Name, b.Ip))
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
@@ -126,6 +128,8 @@ func (b *Bus) Start(log *logging.Logger) error {
|
||||
interval = 5 * time.Second
|
||||
} else {
|
||||
b.Reachable = true
|
||||
// send data as a heartbeat for she ArtNet Protocol
|
||||
b.Send = true
|
||||
log.Info("bus.Start", fmt.Sprintf("device:%s ip:%s watchdog running", b.Name, b.Ip))
|
||||
interval = 30 * time.Second
|
||||
}
|
||||
@@ -143,20 +147,35 @@ func (b *Bus) Start(log *logging.Logger) error {
|
||||
return err
|
||||
}
|
||||
|
||||
b.Send = make(chan *DMX, 1024)
|
||||
|
||||
go func() {
|
||||
defer conn.Close()
|
||||
//close send channel
|
||||
close(b.Send)
|
||||
for send := range b.Send {
|
||||
_, err = conn.Write(NewArtNetPackage(send))
|
||||
ticker := time.NewTicker(23 * time.Millisecond)
|
||||
|
||||
defer func() {
|
||||
b.Send = false
|
||||
conn.Close()
|
||||
ticker.Stop()
|
||||
}()
|
||||
|
||||
for range ticker.C {
|
||||
|
||||
if !b.Send {
|
||||
continue
|
||||
}
|
||||
|
||||
b.Send = false
|
||||
|
||||
b.mu.Lock()
|
||||
data := NewDMXUniverse()
|
||||
copy(data.Data, b.DMX.GetDMXData())
|
||||
b.mu.Unlock()
|
||||
|
||||
_, err = conn.Write(NewArtNetPackage(data))
|
||||
if err != nil {
|
||||
log.Error("bus.Start", err)
|
||||
return
|
||||
}
|
||||
time.Sleep(23 * time.Millisecond)
|
||||
}
|
||||
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
@@ -166,8 +185,8 @@ func (b *Bus) Stop() {
|
||||
if b.Watchdog != nil {
|
||||
//cancels context
|
||||
b.Watchdog()
|
||||
//close send channel
|
||||
close(b.Send)
|
||||
|
||||
b.Send = false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -196,6 +215,13 @@ func (b *Bus) ParsePayload(c *gin.Context) error {
|
||||
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) {
|
||||
p := fastping.NewPinger()
|
||||
ra, err := net.ResolveIPAddr("ip4:icmp", ip)
|
||||
|
@@ -1,12 +1,29 @@
|
||||
package models
|
||||
|
||||
type DMX []byte
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
type DMX struct {
|
||||
Data []byte
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
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) {
|
||||
(*d)[channel] = value
|
||||
d.mu.Lock()
|
||||
d.Data[channel] = value
|
||||
d.mu.Unlock()
|
||||
}
|
||||
|
@@ -23,7 +23,7 @@ func NewArtNetPackage(data *DMX) Package {
|
||||
packet.WriteByte(0x00) // Sequence
|
||||
packet.WriteByte(0x00) // Physical
|
||||
binary.Write(packet, binary.BigEndian, uint16(0)) // Universe (net:subuni, usually 0)
|
||||
binary.Write(packet, binary.BigEndian, uint16(len(*data))) // Length
|
||||
packet.Write(*data)
|
||||
binary.Write(packet, binary.BigEndian, uint16(len(data.Data))) // Length
|
||||
packet.Write(data.Data)
|
||||
return packet.Bytes()
|
||||
}
|
||||
|
@@ -5,11 +5,11 @@ import (
|
||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||
)
|
||||
|
||||
type Subscriptions map[uuid.UUID]Subscription
|
||||
type Subscriptions map[uuid.UUID][]Subscription
|
||||
|
||||
type Subscription struct {
|
||||
Bus string
|
||||
Address uint
|
||||
Address []uint
|
||||
}
|
||||
|
||||
func NewSubscriptions() Subscriptions {
|
||||
@@ -17,8 +17,11 @@ func NewSubscriptions() Subscriptions {
|
||||
}
|
||||
|
||||
func (s *Subscriptions) AddSubscription(uid uuid.UUID, drv *json_dataModels.Driver) {
|
||||
(*s)[uid] = Subscription{
|
||||
Bus: drv.Bus,
|
||||
Address: drv.Address,
|
||||
subs := []Subscription{}
|
||||
for _, bus := range drv.Buses {
|
||||
sub := Subscription{Bus: bus.Name}
|
||||
sub.Address = append(sub.Address, bus.Address...)
|
||||
subs = append(subs, sub)
|
||||
}
|
||||
(*s)[uid] = subs
|
||||
}
|
||||
|
@@ -12,13 +12,10 @@ import (
|
||||
|
||||
const (
|
||||
// Time allowed to write a message to the peer.
|
||||
writeWait = 10 * time.Second
|
||||
writeWait = 40 * time.Second
|
||||
|
||||
// Time allowed to read the next pong message from the peer.
|
||||
pongWait = 10 * time.Second
|
||||
|
||||
// Send pings to peer with this period. Must be less than pongWait.
|
||||
pingPeriod = (pongWait * 9) / 10
|
||||
pongWait = 40 * time.Second
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
@@ -32,7 +29,6 @@ type Client struct {
|
||||
OnError func(err error)
|
||||
OnPing func()
|
||||
OnPong func()
|
||||
sendPong chan string
|
||||
send chan []byte
|
||||
unregister chan []byte
|
||||
}
|
||||
@@ -45,9 +41,8 @@ func NewClient(ip, id string, port uint) (*Client, error) {
|
||||
ip: ip,
|
||||
port: port,
|
||||
Connected: true,
|
||||
sendPong: make(chan string),
|
||||
send: make(chan []byte),
|
||||
unregister: make(chan []byte),
|
||||
send: make(chan []byte, 256),
|
||||
unregister: make(chan []byte, 256),
|
||||
}
|
||||
|
||||
dialer := websocket.DefaultDialer
|
||||
@@ -60,18 +55,21 @@ func NewClient(ip, id string, port uint) (*Client, error) {
|
||||
}
|
||||
c.conn = conn
|
||||
|
||||
// Setup control handlers
|
||||
//Setup control handlers
|
||||
conn.SetPingHandler(func(appData string) error {
|
||||
if c.OnPing != nil {
|
||||
c.OnPing()
|
||||
}
|
||||
conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||
conn.SetReadDeadline(time.Now().Add(writeWait))
|
||||
c.sendPong <- appData
|
||||
conn.SetReadDeadline(time.Now().Add(pongWait))
|
||||
conn.SetWriteDeadline(time.Now().Add(pongWait))
|
||||
if err := conn.WriteControl(websocket.PongMessage, []byte(appData), time.Now().Add(2*pongWait)); err != nil {
|
||||
c.OnError(err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
conn.SetPongHandler(func(string) error {
|
||||
conn.SetPongHandler(func(appData string) error {
|
||||
conn.SetReadDeadline(time.Now().Add(pongWait))
|
||||
if c.OnPong != nil {
|
||||
c.OnPong()
|
||||
@@ -79,17 +77,6 @@ func NewClient(ip, id string, port uint) (*Client, error) {
|
||||
return nil
|
||||
})
|
||||
|
||||
conn.SetCloseHandler(func(code int, text string) error {
|
||||
if c.OnClose != nil {
|
||||
c.OnClose(code, text)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
if c.OnOpen != nil {
|
||||
c.OnOpen()
|
||||
}
|
||||
|
||||
// Start reading messages from client
|
||||
go c.Read()
|
||||
go c.Write()
|
||||
@@ -101,13 +88,16 @@ func (c *Client) Read() {
|
||||
c.OnOpen()
|
||||
}
|
||||
|
||||
c.conn.SetReadDeadline(time.Now().Add(writeWait))
|
||||
c.conn.SetReadDeadline(time.Now().Add(pongWait))
|
||||
|
||||
for c.Connected {
|
||||
msgType, msg, err := c.conn.ReadMessage()
|
||||
c.conn.SetReadDeadline(time.Now().Add(pongWait))
|
||||
if err != nil {
|
||||
c.handleError(fmt.Errorf("read error (id:%s:%d): %w", c.ip, c.port, err))
|
||||
c.handleError(fmt.Errorf("read error (ip:%s): %w", c.ip, err))
|
||||
return
|
||||
}
|
||||
|
||||
switch msgType {
|
||||
case websocket.CloseMessage:
|
||||
c.Close(websocket.CloseNormalClosure, "Client closed")
|
||||
@@ -116,32 +106,29 @@ func (c *Client) Read() {
|
||||
if c.OnMessage != nil {
|
||||
c.OnMessage(msg)
|
||||
} else {
|
||||
log.Printf("Received message but no handler set (id:%s:%d): %s", c.ip, c.port, string(msg))
|
||||
log.Printf("Received message but no handler set (ip:%s): %s", c.ip, string(msg))
|
||||
}
|
||||
default:
|
||||
log.Printf("Unhandled message type %d (id:%s:%d)", msgType, c.ip, c.port)
|
||||
log.Printf("Unhandled message type %d (ip:%s)", msgType, c.ip)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) Write() {
|
||||
ticker := time.NewTicker(pingPeriod)
|
||||
defer func() {
|
||||
ticker.Stop()
|
||||
c.conn.Close()
|
||||
}()
|
||||
for {
|
||||
defer c.conn.Close()
|
||||
|
||||
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||
for {
|
||||
select {
|
||||
case message, ok := <-c.send:
|
||||
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||
if !ok {
|
||||
// The hub closed the channel.
|
||||
if err := c.conn.WriteMessage(websocket.CloseMessage, []byte{}); err != nil {
|
||||
if err := c.conn.WriteMessage(websocket.CloseMessage, []byte("ping")); err != nil {
|
||||
c.handleError(err)
|
||||
return
|
||||
}
|
||||
c.handleError(fmt.Errorf("server %s:%d closed channel", c.ip, c.port))
|
||||
c.handleError(fmt.Errorf("server %s closed channel", c.ip))
|
||||
return
|
||||
} else {
|
||||
if err := c.conn.WriteMessage(websocket.TextMessage, message); err != nil {
|
||||
@@ -149,26 +136,10 @@ func (c *Client) Write() {
|
||||
return
|
||||
}
|
||||
}
|
||||
case <-ticker.C:
|
||||
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||
if err := c.conn.WriteMessage(websocket.PingMessage, nil); err != nil {
|
||||
c.handleError(err)
|
||||
return
|
||||
}
|
||||
|
||||
if c.OnPing != nil {
|
||||
c.OnPing()
|
||||
}
|
||||
|
||||
case message, ok := <-c.sendPong:
|
||||
if ok {
|
||||
c.conn.WriteMessage(websocket.PongMessage, []byte(message))
|
||||
}
|
||||
case message := <-c.unregister:
|
||||
c.conn.WriteMessage(websocket.CloseMessage, message)
|
||||
c.Connected = false
|
||||
close(c.send)
|
||||
close(c.sendPong)
|
||||
close(c.unregister)
|
||||
return
|
||||
}
|
||||
@@ -217,7 +188,7 @@ func (c *Client) handleError(err error) {
|
||||
if c.OnError != nil {
|
||||
c.OnError(err)
|
||||
} else {
|
||||
fmt.Println("error: ", err)
|
||||
log.Println("error:", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user