add new ip ping for udp device
add new function start and stop bus
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/tatsushid/go-fastping"
|
||||
"github.com/tecamino/tecamino-logger/logging"
|
||||
)
|
||||
|
||||
// Art-Net constants
|
||||
@@ -16,10 +17,12 @@ const (
|
||||
|
||||
// Art-Net Interface
|
||||
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:"-"`
|
||||
Name string `yaml:"name" json:"name"`
|
||||
Ip string `yaml:"ip" json:"ip"`
|
||||
Port *int `yaml:"port" json:"port,omitempty"`
|
||||
Data *DMX `yaml:"-" json:"-"`
|
||||
Watchdog context.CancelFunc `yaml:"-" json:"-"`
|
||||
Reachable bool `yaml:"-" json:"-"`
|
||||
}
|
||||
|
||||
// adds new Art-Net interface to driver port 0 = 6454 (default art-net)
|
||||
@@ -92,8 +95,51 @@ func (b *Bus) Poll(interval time.Duration) error {
|
||||
}
|
||||
}
|
||||
|
||||
// start polling dmx data in milliseconds 0 = aprox. 44Hertz
|
||||
// start bus
|
||||
func (b *Bus) Start(log *logging.Logger) {
|
||||
var ctx context.Context
|
||||
ctx, b.Watchdog = context.WithCancel(context.Background())
|
||||
|
||||
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))
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
log.Info("bus.Start", fmt.Sprintf("device:%s ip:%s watchdog stopped", b.Name, b.Ip))
|
||||
b.Reachable = false
|
||||
return
|
||||
default:
|
||||
b.Reachable = false
|
||||
if reached, err := isUDPReachable(b.Ip); err != nil {
|
||||
log.Error("bus.Start", err)
|
||||
interval = 5 * time.Second
|
||||
} else if !reached {
|
||||
log.Error("bus.Start", fmt.Sprintf("device:%s ip:%s not reached", b.Name, b.Ip))
|
||||
interval = 5 * time.Second
|
||||
} else {
|
||||
b.Reachable = true
|
||||
log.Info("bus.Start", fmt.Sprintf("device:%s ip:%s watchdog running", b.Name, b.Ip))
|
||||
interval = 30 * time.Second
|
||||
}
|
||||
time.Sleep(interval)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// stop bus
|
||||
func (b *Bus) Stop() {
|
||||
if b.Watchdog != nil {
|
||||
b.Watchdog()
|
||||
}
|
||||
}
|
||||
|
||||
// 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{
|
||||
IP: net.ParseIP(b.Ip),
|
||||
@@ -105,16 +151,6 @@ func (b *Bus) SendData() error {
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
go func() {
|
||||
if reached, err := isUDPReachable(b.Ip); err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
} else if !reached {
|
||||
log.Println("device not reachable")
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
_, err = conn.Write(NewArtNetPackage(b.Data))
|
||||
|
||||
return err
|
||||
|
Reference in New Issue
Block a user