Compare commits
17 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
49dff4f609 | ||
![]() |
3704edebd5 | ||
![]() |
a503b71fb6 | ||
![]() |
05409c2544 | ||
![]() |
b484745ed1 | ||
![]() |
04e306c6da | ||
![]() |
239e0050a1 | ||
![]() |
de4758c563 | ||
![]() |
a85c9ccd66 | ||
![]() |
da78a00446 | ||
![]() |
ba3c55dc34 | ||
![]() |
e0950b44ee | ||
![]() |
76a036707f | ||
![]() |
258323f5b7 | ||
![]() |
473eb22b97 | ||
![]() |
64ad4e8b3e | ||
![]() |
b29e7a97b8 |
4
.github/workflows/build.yml
vendored
4
.github/workflows/build.yml
vendored
@@ -36,9 +36,9 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
mkdir -p build
|
mkdir -p build
|
||||||
if [ "${{ matrix.goos }}" == "windows" ]; then
|
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
|
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
|
fi
|
||||||
|
|
||||||
- name: Upload artifacts
|
- name: Upload artifacts
|
||||||
|
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,3 +1,5 @@
|
|||||||
|
.DS_Store
|
||||||
*.cfg
|
*.cfg
|
||||||
*.log
|
*.log
|
||||||
artNetDriver-arm64
|
artNetDriver-arm64
|
||||||
|
tecamino-driver-artNet-linux-arm64
|
||||||
|
@@ -4,9 +4,9 @@ import (
|
|||||||
"artNet/cfg"
|
"artNet/cfg"
|
||||||
"artNet/models"
|
"artNet/models"
|
||||||
ws "artNet/websocket"
|
ws "artNet/websocket"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"path"
|
"path"
|
||||||
"time"
|
|
||||||
|
|
||||||
json_data "github.com/tecamino/tecamino-json_data"
|
json_data "github.com/tecamino/tecamino-json_data"
|
||||||
"github.com/tecamino/tecamino-logger/logging"
|
"github.com/tecamino/tecamino-logger/logging"
|
||||||
@@ -78,11 +78,11 @@ 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 d.Buses[bus].SendData()
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// connect to websocket server and listen to subscriptions
|
// connect to websocket server and listen to subscriptions
|
||||||
@@ -90,67 +90,56 @@ func (d *ArtNetDriver) SetValue(bus string, address uint, value uint8) error {
|
|||||||
// id: id of driver
|
// id: id of driver
|
||||||
// port: port of server
|
// port: port of server
|
||||||
func (d *ArtNetDriver) Connect(ip, id string, port uint) error {
|
func (d *ArtNetDriver) Connect(ip, id string, port uint) error {
|
||||||
var err error
|
errChan := make(chan error)
|
||||||
client, err := ws.NewClient(ip, id, port)
|
client, err := ws.NewClient(ip, id, port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
client.OnError = func(err error) {
|
client.OnError = func(err error) {
|
||||||
d.Log.Error("websocket connection", err)
|
d.Log.Error("websocket connection", err)
|
||||||
}
|
errChan <- err
|
||||||
client.OnMessage = func(data []byte) {
|
|
||||||
//fmt.Println(100, string(data))
|
|
||||||
fmt.Println(100, string(data))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
client.Connect(5)
|
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...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
d.Log.Error("artNet.Connect", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
req := json_data.NewRequest()
|
req := json_data.NewRequest()
|
||||||
req.AddDriverSubscription(".*", id, 0, true, false, false)
|
req.AddDriverSubscription(".*", id, 0, true, false, false)
|
||||||
|
|
||||||
if err := client.SendData(req); err != nil {
|
if err := client.SendRequest(req); err != nil {
|
||||||
|
errChan <- err
|
||||||
d.Log.Error("websocket send data", err)
|
d.Log.Error("websocket send data", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for err := range errChan {
|
||||||
time.Sleep(1)
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
}
|
||||||
// d.Conn = websocket.NewClient()
|
|
||||||
// if err := d.Conn.Connect(ip, id, port); err != nil {
|
// send data to all buses that the send flage is true
|
||||||
// return err
|
func (d *ArtNetDriver) SendData() {
|
||||||
// }
|
for _, bus := range d.Buses {
|
||||||
// defer d.Conn.Disconnect()
|
bus.Send = bus.Reachable
|
||||||
|
}
|
||||||
// if err := d.Conn.Subscribe(id); err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Subscribe to websocket server
|
|
||||||
// func (c *Client) Subscribe(id string) error {
|
|
||||||
// req := json_data.NewRequest()
|
|
||||||
// req.AddDriverSubscription(".*", id, 0, true, false, false)
|
|
||||||
// if err := wsjson.Write(c.ctx, c.conn, req); err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
// return nil
|
|
||||||
// }
|
|
||||||
|
|
||||||
// for {
|
|
||||||
// respond, err := d.Conn.ReadJsonData()
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
|
|
||||||
// d.Subscribe(respond.Subscribe...)
|
|
||||||
|
|
||||||
// for _, pub := range respond.Publish {
|
|
||||||
// if sub, ok := d.Subscriptions[pub.Uuid]; ok {
|
|
||||||
// if err := d.SetValue(sub.Bus, sub.Address, uint8(pub.Value.(float64))); err != nil {
|
|
||||||
// d.Log.Info("artNet.Connect", err.Error())
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
@@ -105,7 +105,7 @@ func (d *ArtNetDriver) Start(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
busPayload := models.Bus{}
|
busPayload := models.Bus{}
|
||||||
if busPayload.ParsePayload(c); err != nil {
|
if err := busPayload.ParsePayload(c); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
23
driver/publish.go
Normal file
23
driver/publish.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
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 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()
|
||||||
|
return nil
|
||||||
|
}
|
@@ -14,7 +14,13 @@ 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()
|
||||||
}
|
}
|
||||||
|
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.13
|
github.com/tecamino/tecamino-json_data v0.0.30
|
||||||
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.13 h1:hugbmCgXXh0F7YQAEbbJYHkSdq1caejD7SajDiMs42I=
|
github.com/tecamino/tecamino-json_data v0.0.30 h1:MRx2POR2VmkAvbl1EZRUZeU4ygccuu5Gw6El74RbF+U=
|
||||||
github.com/tecamino/tecamino-json_data v0.0.13/go.mod h1:LLlyD7Wwqplb2BP4PeO86EokEcTRidlW5MwgPd1T2JY=
|
github.com/tecamino/tecamino-json_data v0.0.30/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=
|
||||||
|
3
main.go
3
main.go
@@ -75,7 +75,8 @@ 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)
|
||||||
}
|
}
|
||||||
fmt.Println(555)
|
|
||||||
|
artNetDriver.Log.Info("main", "next reconnecting attempt in 10 seconds")
|
||||||
time.Sleep(10 * time.Second)
|
time.Sleep(10 * time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -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,10 +26,12 @@ 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:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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)
|
||||||
@@ -41,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
|
||||||
}
|
}
|
||||||
@@ -89,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
|
||||||
}
|
}
|
||||||
@@ -102,13 +105,13 @@ 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())
|
||||||
|
|
||||||
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 stopped", b.Name, b.Ip))
|
log.Info("bus.Start", fmt.Sprintf("device:%s ip:%s watchdog starting", b.Name, b.Ip))
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
@@ -125,6 +128,8 @@ func (b *Bus) Start(log *logging.Logger) {
|
|||||||
interval = 5 * time.Second
|
interval = 5 * time.Second
|
||||||
} else {
|
} else {
|
||||||
b.Reachable = true
|
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))
|
log.Info("bus.Start", fmt.Sprintf("device:%s ip:%s watchdog running", b.Name, b.Ip))
|
||||||
interval = 30 * time.Second
|
interval = 30 * time.Second
|
||||||
}
|
}
|
||||||
@@ -132,26 +137,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 +146,53 @@ func (b *Bus) SendData() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
_, err = conn.Write(NewArtNetPackage(b.Data))
|
go func() {
|
||||||
|
ticker := time.NewTicker(23 * time.Millisecond)
|
||||||
|
|
||||||
return err
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// stop bus
|
||||||
|
func (b *Bus) Stop() {
|
||||||
|
if b.Watchdog != nil {
|
||||||
|
//cancels context
|
||||||
|
b.Watchdog()
|
||||||
|
|
||||||
|
b.Send = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 {
|
||||||
@@ -187,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)
|
||||||
|
@@ -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()
|
||||||
}
|
}
|
||||||
|
@@ -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()
|
||||||
}
|
}
|
||||||
|
@@ -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
|
||||||
}
|
}
|
||||||
|
@@ -1,44 +0,0 @@
|
|||||||
package models
|
|
||||||
|
|
||||||
// type Clients map[string]Client
|
|
||||||
|
|
||||||
// type Client struct {
|
|
||||||
// Connected *bool `json:"connected"`
|
|
||||||
// SndConn *websocket.Conn `json:"-"` //sending connection
|
|
||||||
// RvcConn *websocket.Conn `json:"-"` // revieving connection
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func NewClients() Clients {
|
|
||||||
// return make(Clients)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Connect a recieving websocket connection
|
|
||||||
// func (c *Clients) ConnectRecievingWsConnection(id string, ctx *gin.Context) (*websocket.Conn, error) {
|
|
||||||
// conn, err := websocket.Accept(ctx.Writer, ctx.Request, &websocket.AcceptOptions{
|
|
||||||
// OriginPatterns: []string{"*"},
|
|
||||||
// })
|
|
||||||
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, fmt.Errorf("error accept websocket client: %s", err)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// b := true
|
|
||||||
// (*c)[id] = Client{
|
|
||||||
// Connected: &b,
|
|
||||||
// RvcConn: conn,
|
|
||||||
// }
|
|
||||||
// return conn, nil
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (c *Clients) RemoveClient(id string) {
|
|
||||||
// delete(*c, id)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (c *Clients) GetClientPointer(id string) *bool {
|
|
||||||
// return (*c)[id].Connected
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (c *Clients) DisconnectRecievingWsConnection(id string, code websocket.StatusCode, reason string) {
|
|
||||||
// *(*c)[id].Connected = false
|
|
||||||
// (*c)[id].RvcConn.Close(code, reason)
|
|
||||||
// }
|
|
@@ -4,30 +4,46 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Time allowed to write a message to the peer.
|
||||||
|
writeWait = 40 * time.Second
|
||||||
|
|
||||||
|
// Time allowed to read the next pong message from the peer.
|
||||||
|
pongWait = 40 * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
conn *websocket.Conn
|
ip string
|
||||||
writeMu sync.Mutex
|
port uint
|
||||||
OnMessage func(data []byte)
|
Connected bool
|
||||||
OnOpen func()
|
conn *websocket.Conn
|
||||||
OnClose func(code int, reason string)
|
OnOpen func()
|
||||||
OnError func(err error)
|
OnMessage func(data []byte)
|
||||||
OnPing func()
|
OnClose func(code int, reason string)
|
||||||
OnPong func()
|
OnError func(err error)
|
||||||
timeout time.Duration
|
OnPing func()
|
||||||
|
OnPong func()
|
||||||
|
send chan []byte
|
||||||
|
unregister chan []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connect to websocket server
|
// Connect to websocket server
|
||||||
// ip: ip address of server
|
// ip: ip address of server
|
||||||
func NewClient(ip, id string, port uint) (*Client, error) {
|
func NewClient(ip, id string, port uint) (*Client, error) {
|
||||||
url := fmt.Sprintf("ws://%s:%d/ws?id=%s", ip, port, id)
|
url := fmt.Sprintf("ws://%s:%d/ws?id=%s", ip, port, id)
|
||||||
c := &Client{}
|
c := &Client{
|
||||||
|
ip: ip,
|
||||||
|
port: port,
|
||||||
|
Connected: true,
|
||||||
|
send: make(chan []byte, 256),
|
||||||
|
unregister: make(chan []byte, 256),
|
||||||
|
}
|
||||||
|
|
||||||
dialer := websocket.DefaultDialer
|
dialer := websocket.DefaultDialer
|
||||||
conn, resp, err := dialer.Dial(url, nil)
|
conn, resp, err := dialer.Dial(url, nil)
|
||||||
@@ -39,122 +55,108 @@ func NewClient(ip, id string, port uint) (*Client, error) {
|
|||||||
}
|
}
|
||||||
c.conn = conn
|
c.conn = conn
|
||||||
|
|
||||||
// Setup control handlers
|
//Setup control handlers
|
||||||
c.conn.SetPingHandler(func(appData string) error {
|
conn.SetPingHandler(func(appData string) error {
|
||||||
if c.OnPing != nil {
|
if c.OnPing != nil {
|
||||||
c.OnPing()
|
c.OnPing()
|
||||||
}
|
}
|
||||||
return c.conn.WriteMessage(websocket.PongMessage, nil)
|
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
|
||||||
})
|
})
|
||||||
c.conn.SetPongHandler(func(appData string) error {
|
|
||||||
c.conn.SetReadDeadline(time.Now().Add(c.timeout))
|
conn.SetPongHandler(func(appData string) error {
|
||||||
|
conn.SetReadDeadline(time.Now().Add(pongWait))
|
||||||
if c.OnPong != nil {
|
if c.OnPong != nil {
|
||||||
c.OnPong()
|
c.OnPong()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
c.conn.SetCloseHandler(func(code int, text string) error {
|
|
||||||
if c.OnClose != nil {
|
|
||||||
c.OnClose(code, text)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
if c.OnOpen != nil {
|
// Start reading messages from client
|
||||||
c.OnOpen()
|
go c.Read()
|
||||||
}
|
go c.Write()
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Connect(timeout uint) {
|
func (c *Client) Read() {
|
||||||
if timeout > 0 {
|
if c.OnOpen != nil {
|
||||||
fmt.Println(1234, timeout)
|
c.OnOpen()
|
||||||
c.timeout = time.Duration(timeout) * time.Second
|
|
||||||
}
|
}
|
||||||
|
|
||||||
go c.pingLoop()
|
c.conn.SetReadDeadline(time.Now().Add(pongWait))
|
||||||
|
|
||||||
c.conn.SetReadDeadline(time.Now().Add(c.timeout))
|
for c.Connected {
|
||||||
go func() {
|
msgType, msg, err := c.conn.ReadMessage()
|
||||||
|
c.conn.SetReadDeadline(time.Now().Add(pongWait))
|
||||||
for {
|
if err != nil {
|
||||||
msgType, msg, err := c.conn.ReadMessage()
|
c.handleError(fmt.Errorf("read error (ip:%s): %w", c.ip, err))
|
||||||
if err != nil {
|
|
||||||
if websocket.IsCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
|
|
||||||
log.Println("WebSocket closed:", err)
|
|
||||||
}
|
|
||||||
if c.OnError != nil {
|
|
||||||
c.OnError(fmt.Errorf("read error: %w", err))
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
switch msgType {
|
|
||||||
case websocket.TextMessage, websocket.BinaryMessage:
|
|
||||||
if c.OnMessage != nil {
|
|
||||||
c.OnMessage(msg)
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
log.Printf("Unhandled message type: %d", msgType)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) pingLoop() {
|
|
||||||
interval := c.timeout / 2
|
|
||||||
if interval <= 0 {
|
|
||||||
interval = 5 * time.Second
|
|
||||||
}
|
|
||||||
ticker := time.NewTicker(interval)
|
|
||||||
defer ticker.Stop()
|
|
||||||
|
|
||||||
for range ticker.C {
|
|
||||||
if err := c.Write(websocket.PingMessage, nil); err != nil {
|
|
||||||
if c.OnError != nil {
|
|
||||||
c.OnError(fmt.Errorf("ping error: %w", err))
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if c.OnPing != nil {
|
|
||||||
c.OnPing()
|
switch msgType {
|
||||||
|
case websocket.CloseMessage:
|
||||||
|
c.Close(websocket.CloseNormalClosure, "Client closed")
|
||||||
|
return
|
||||||
|
case websocket.TextMessage:
|
||||||
|
if c.OnMessage != nil {
|
||||||
|
c.OnMessage(msg)
|
||||||
|
} else {
|
||||||
|
log.Printf("Received message but no handler set (ip:%s): %s", c.ip, string(msg))
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
log.Printf("Unhandled message type %d (ip:%s)", msgType, c.ip)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Write(msgType int, data []byte) error {
|
func (c *Client) Write() {
|
||||||
c.writeMu.Lock()
|
defer c.conn.Close()
|
||||||
defer c.writeMu.Unlock()
|
|
||||||
|
|
||||||
c.conn.SetWriteDeadline(time.Now().Add(c.timeout))
|
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||||
|
for {
|
||||||
if err := c.conn.WriteMessage(msgType, data); err != nil {
|
select {
|
||||||
if c.OnError != nil {
|
case message, ok := <-c.send:
|
||||||
c.OnError(err)
|
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||||
}
|
if !ok {
|
||||||
return err
|
// The hub closed the channel.
|
||||||
}
|
if err := c.conn.WriteMessage(websocket.CloseMessage, []byte("ping")); err != nil {
|
||||||
return nil
|
c.handleError(err)
|
||||||
}
|
return
|
||||||
|
}
|
||||||
// Close connection to websocket server
|
c.handleError(fmt.Errorf("server %s closed channel", c.ip))
|
||||||
func (c *Client) Close(code int, reason string) {
|
return
|
||||||
if c.conn != nil {
|
} else {
|
||||||
if c.OnClose != nil {
|
if err := c.conn.WriteMessage(websocket.TextMessage, message); err != nil {
|
||||||
c.OnClose(code, reason)
|
c.handleError(err)
|
||||||
}
|
return
|
||||||
c.conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(code, reason))
|
}
|
||||||
c.conn.Close()
|
}
|
||||||
}
|
case message := <-c.unregister:
|
||||||
}
|
c.conn.WriteMessage(websocket.CloseMessage, message)
|
||||||
|
c.Connected = false
|
||||||
func (c *Client) SendData(data any) error {
|
close(c.send)
|
||||||
c.conn.SetWriteDeadline(time.Now().Add(c.timeout))
|
close(c.unregister)
|
||||||
if err := c.conn.WriteJSON(data); err != nil {
|
return
|
||||||
if c.OnError != nil {
|
|
||||||
c.OnError(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) SendRequest(req *json_dataModels.Request) error {
|
||||||
|
if !c.Connected {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := json.Marshal(*req)
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
c.send <- data
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,3 +165,30 @@ func (c *Client) ReadJsonData(data []byte) (json_dataModels.Response, error) {
|
|||||||
err := json.Unmarshal(data, &resp)
|
err := json.Unmarshal(data, &resp)
|
||||||
return resp, err
|
return resp, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close connection to websocket server
|
||||||
|
func (c *Client) Close(code int, reason string) error {
|
||||||
|
closeMsg := websocket.FormatCloseMessage(code, reason)
|
||||||
|
select {
|
||||||
|
case c.unregister <- closeMsg: // Attempt to send
|
||||||
|
default: // If the channel is full, this runs
|
||||||
|
return fmt.Errorf("attempt close client socket failed")
|
||||||
|
}
|
||||||
|
if c.OnClose != nil {
|
||||||
|
c.OnClose(code, reason)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) handleError(err error) {
|
||||||
|
if c.OnError != nil {
|
||||||
|
c.OnError(err)
|
||||||
|
}
|
||||||
|
if err := c.Close(websocket.CloseInternalServerErr, err.Error()); err != nil {
|
||||||
|
if c.OnError != nil {
|
||||||
|
c.OnError(err)
|
||||||
|
} else {
|
||||||
|
log.Println("error:", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user