Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
05409c2544 | ||
![]() |
b484745ed1 | ||
![]() |
04e306c6da | ||
![]() |
239e0050a1 |
@@ -102,15 +102,18 @@ func (d *ArtNetDriver) Connect(ip, id string, port uint) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
client.OnMessage = func(data []byte) {
|
client.OnMessage = func(data []byte) {
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
if request.Subscribe != nil {
|
if request.Subscribe != nil {
|
||||||
d.Subscribe(request.Subscribe...)
|
d.Subscribe(request.Subscribe...)
|
||||||
}
|
}
|
||||||
|
|
||||||
if request.Publish != nil {
|
if request.Publish != nil {
|
||||||
d.Publish(request.Publish...)
|
d.Publish(request.Publish...)
|
||||||
}
|
}
|
||||||
@@ -138,6 +141,6 @@ func (d *ArtNetDriver) Connect(ip, id string, port uint) error {
|
|||||||
// send data to all buses that the send flage is true
|
// send data to all buses that the send flage is true
|
||||||
func (d *ArtNetDriver) SendData() {
|
func (d *ArtNetDriver) SendData() {
|
||||||
for _, bus := range d.Buses {
|
for _, bus := range d.Buses {
|
||||||
bus.Send <- bus.Data
|
bus.Send = bus.Reachable
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
2
go.mod
2
go.mod
@@ -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.16
|
github.com/tecamino/tecamino-json_data v0.0.20
|
||||||
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
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/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.16 h1:aZFxnhm4g6WMDPoqy4HosUk7vl0DB0iIcVs8bbT4MzU=
|
github.com/tecamino/tecamino-json_data v0.0.20 h1:0YHjcGq4T37Z0mu24CMbFA5zgYiUXZNrI5C44nvWexU=
|
||||||
github.com/tecamino/tecamino-json_data v0.0.16/go.mod h1:LLlyD7Wwqplb2BP4PeO86EokEcTRidlW5MwgPd1T2JY=
|
github.com/tecamino/tecamino-json_data v0.0.20/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=
|
||||||
|
1
main.go
1
main.go
@@ -75,6 +75,7 @@ func main() {
|
|||||||
if err := artNetDriver.Connect(*serverIp, DriverName, *serverPort); err != nil {
|
if err := artNetDriver.Connect(*serverIp, DriverName, *serverPort); err != nil {
|
||||||
artNetDriver.Log.Error("main", err)
|
artNetDriver.Log.Error("main", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
artNetDriver.Log.Info("main", "next reconnecting attempt in 10 seconds")
|
artNetDriver.Log.Info("main", "next reconnecting attempt in 10 seconds")
|
||||||
time.Sleep(10 * time.Second)
|
time.Sleep(10 * time.Second)
|
||||||
}
|
}
|
||||||
|
@@ -29,7 +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:"-"`
|
Send bool `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)
|
||||||
@@ -107,8 +107,6 @@ 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())
|
||||||
|
|
||||||
b.Send = make(chan *DMX, 1024)
|
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
var interval time.Duration = 10 * time.Second
|
var interval time.Duration = 10 * time.Second
|
||||||
log.Info("bus.Start", fmt.Sprintf("device:%s ip:%s watchdog starting", b.Name, b.Ip))
|
log.Info("bus.Start", fmt.Sprintf("device:%s ip:%s watchdog starting", b.Name, b.Ip))
|
||||||
@@ -129,7 +127,7 @@ func (b *Bus) Start(log *logging.Logger) error {
|
|||||||
} else {
|
} else {
|
||||||
b.Reachable = true
|
b.Reachable = true
|
||||||
// send data as a heartbeat for she ArtNet Protocol
|
// send data as a heartbeat for she ArtNet Protocol
|
||||||
b.Send <- b.Data
|
b.Send = true
|
||||||
log.Info("bus.Start", fmt.Sprintf("device:%s ip:%s watchdog running", b.Name, b.Ip))
|
log.Info("bus.Start", fmt.Sprintf("device:%s ip:%s watchdog running", b.Name, b.Ip))
|
||||||
interval = 30 * time.Second
|
interval = 30 * time.Second
|
||||||
}
|
}
|
||||||
@@ -148,17 +146,26 @@ func (b *Bus) Start(log *logging.Logger) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer conn.Close()
|
ticker := time.NewTicker(23 * time.Millisecond)
|
||||||
//close send channel
|
|
||||||
defer close(b.Send)
|
|
||||||
|
|
||||||
for send := range b.Send {
|
defer func() {
|
||||||
_, err = conn.Write(NewArtNetPackage(send))
|
b.Send = false
|
||||||
|
conn.Close()
|
||||||
|
ticker.Stop()
|
||||||
|
}()
|
||||||
|
|
||||||
|
for range ticker.C {
|
||||||
|
if !b.Send {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
b.Send = false
|
||||||
|
|
||||||
|
_, err = conn.Write(NewArtNetPackage(b.Data))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("bus.Start", err)
|
log.Error("bus.Start", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
time.Sleep(23 * time.Millisecond)
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
return nil
|
return nil
|
||||||
@@ -169,8 +176,8 @@ func (b *Bus) Stop() {
|
|||||||
if b.Watchdog != nil {
|
if b.Watchdog != nil {
|
||||||
//cancels context
|
//cancels context
|
||||||
b.Watchdog()
|
b.Watchdog()
|
||||||
//close send channel
|
|
||||||
close(b.Send)
|
b.Send = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -12,13 +12,10 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
// Time allowed to write a message to the peer.
|
// 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.
|
// Time allowed to read the next pong message from the peer.
|
||||||
pongWait = 10 * time.Second
|
pongWait = 40 * time.Second
|
||||||
|
|
||||||
// Send pings to peer with this period. Must be less than pongWait.
|
|
||||||
pingPeriod = (pongWait * 9) / 10
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
@@ -32,7 +29,6 @@ type Client struct {
|
|||||||
OnError func(err error)
|
OnError func(err error)
|
||||||
OnPing func()
|
OnPing func()
|
||||||
OnPong func()
|
OnPong func()
|
||||||
sendPong chan string
|
|
||||||
send chan []byte
|
send chan []byte
|
||||||
unregister chan []byte
|
unregister chan []byte
|
||||||
}
|
}
|
||||||
@@ -45,9 +41,8 @@ func NewClient(ip, id string, port uint) (*Client, error) {
|
|||||||
ip: ip,
|
ip: ip,
|
||||||
port: port,
|
port: port,
|
||||||
Connected: true,
|
Connected: true,
|
||||||
sendPong: make(chan string),
|
send: make(chan []byte, 256),
|
||||||
send: make(chan []byte),
|
unregister: make(chan []byte, 256),
|
||||||
unregister: make(chan []byte),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dialer := websocket.DefaultDialer
|
dialer := websocket.DefaultDialer
|
||||||
@@ -60,18 +55,21 @@ func NewClient(ip, id string, port uint) (*Client, error) {
|
|||||||
}
|
}
|
||||||
c.conn = conn
|
c.conn = conn
|
||||||
|
|
||||||
// Setup control handlers
|
//Setup control handlers
|
||||||
conn.SetPingHandler(func(appData string) error {
|
conn.SetPingHandler(func(appData string) error {
|
||||||
if c.OnPing != nil {
|
if c.OnPing != nil {
|
||||||
c.OnPing()
|
c.OnPing()
|
||||||
}
|
}
|
||||||
conn.SetWriteDeadline(time.Now().Add(writeWait))
|
conn.SetReadDeadline(time.Now().Add(pongWait))
|
||||||
conn.SetReadDeadline(time.Now().Add(writeWait))
|
conn.SetWriteDeadline(time.Now().Add(pongWait))
|
||||||
c.sendPong <- appData
|
if err := conn.WriteControl(websocket.PongMessage, []byte(appData), time.Now().Add(2*pongWait)); err != nil {
|
||||||
|
c.OnError(err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
conn.SetPongHandler(func(string) error {
|
conn.SetPongHandler(func(appData string) error {
|
||||||
conn.SetReadDeadline(time.Now().Add(pongWait))
|
conn.SetReadDeadline(time.Now().Add(pongWait))
|
||||||
if c.OnPong != nil {
|
if c.OnPong != nil {
|
||||||
c.OnPong()
|
c.OnPong()
|
||||||
@@ -79,17 +77,6 @@ func NewClient(ip, id string, port uint) (*Client, error) {
|
|||||||
return nil
|
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
|
// Start reading messages from client
|
||||||
go c.Read()
|
go c.Read()
|
||||||
go c.Write()
|
go c.Write()
|
||||||
@@ -101,13 +88,16 @@ func (c *Client) Read() {
|
|||||||
c.OnOpen()
|
c.OnOpen()
|
||||||
}
|
}
|
||||||
|
|
||||||
c.conn.SetReadDeadline(time.Now().Add(writeWait))
|
c.conn.SetReadDeadline(time.Now().Add(pongWait))
|
||||||
for c.Connected {
|
for c.Connected {
|
||||||
|
c.conn.SetReadDeadline(time.Now().Add(pongWait))
|
||||||
msgType, msg, err := c.conn.ReadMessage()
|
msgType, msg, err := c.conn.ReadMessage()
|
||||||
|
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch msgType {
|
switch msgType {
|
||||||
case websocket.CloseMessage:
|
case websocket.CloseMessage:
|
||||||
c.Close(websocket.CloseNormalClosure, "Client closed")
|
c.Close(websocket.CloseNormalClosure, "Client closed")
|
||||||
@@ -116,32 +106,29 @@ func (c *Client) Read() {
|
|||||||
if c.OnMessage != nil {
|
if c.OnMessage != nil {
|
||||||
c.OnMessage(msg)
|
c.OnMessage(msg)
|
||||||
} else {
|
} 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:
|
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() {
|
func (c *Client) Write() {
|
||||||
ticker := time.NewTicker(pingPeriod)
|
defer c.conn.Close()
|
||||||
defer func() {
|
|
||||||
ticker.Stop()
|
|
||||||
c.conn.Close()
|
|
||||||
}()
|
|
||||||
for {
|
|
||||||
|
|
||||||
|
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||||
|
for {
|
||||||
select {
|
select {
|
||||||
case message, ok := <-c.send:
|
case message, ok := <-c.send:
|
||||||
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||||
if !ok {
|
if !ok {
|
||||||
// The hub closed the channel.
|
// 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)
|
c.handleError(err)
|
||||||
return
|
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
|
return
|
||||||
} else {
|
} else {
|
||||||
if err := c.conn.WriteMessage(websocket.TextMessage, message); err != nil {
|
if err := c.conn.WriteMessage(websocket.TextMessage, message); err != nil {
|
||||||
@@ -149,26 +136,10 @@ func (c *Client) Write() {
|
|||||||
return
|
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:
|
case message := <-c.unregister:
|
||||||
c.conn.WriteMessage(websocket.CloseMessage, message)
|
c.conn.WriteMessage(websocket.CloseMessage, message)
|
||||||
c.Connected = false
|
c.Connected = false
|
||||||
close(c.send)
|
close(c.send)
|
||||||
close(c.sendPong)
|
|
||||||
close(c.unregister)
|
close(c.unregister)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -217,7 +188,7 @@ func (c *Client) handleError(err error) {
|
|||||||
if c.OnError != nil {
|
if c.OnError != nil {
|
||||||
c.OnError(err)
|
c.OnError(err)
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("error: ", err)
|
log.Println("error:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user