add new ip ping for udp device

add new function start and stop bus
This commit is contained in:
Adrian Zürcher
2025-04-22 18:01:22 +02:00
parent 274e6acf0e
commit f5e66af3d8
4 changed files with 112 additions and 19 deletions

View File

@@ -1,7 +1,6 @@
package cfg package cfg
import ( import (
"fmt"
"os" "os"
"path" "path"
@@ -51,7 +50,7 @@ func (c *Cfg) SaveCfg(data any) error {
func (c *Cfg) LoadCfg(data any) error { func (c *Cfg) LoadCfg(data any) error {
p := path.Join(c.Dir, c.Name) + ".cfg" p := path.Join(c.Dir, c.Name) + ".cfg"
if _, err := os.Stat(p); err != nil { if _, err := os.Stat(p); err != nil {
return fmt.Errorf("no configuration file: '%s' found", p) return nil
} }
b, err := os.ReadFile(p) b, err := os.ReadFile(p)

View File

@@ -42,7 +42,6 @@ func (d *ArtNetDriver) CreateBus(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"error": "bus name missing"}) c.JSON(http.StatusBadRequest, gin.H{"error": "bus name missing"})
return return
} else if addr := net.ParseIP(payload.Ip); addr == nil { } else if addr := net.ParseIP(payload.Ip); addr == nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "wrong ip '" + payload.Ip + "'"}) c.JSON(http.StatusBadRequest, gin.H{"error": "wrong ip '" + payload.Ip + "'"})
return return
} }
@@ -53,6 +52,7 @@ func (d *ArtNetDriver) CreateBus(c *gin.Context) {
} }
bus := d.NewBus(payload.Name, payload.Ip, payload.GetPort()) bus := d.NewBus(payload.Name, payload.Ip, payload.GetPort())
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"message": fmt.Sprintf("bus '%s' successfully created with ip: %s and on port: %d", bus.Name, bus.Ip, bus.GetPort()), "message": fmt.Sprintf("bus '%s' successfully created with ip: %s and on port: %d", bus.Name, bus.Ip, bus.GetPort()),
}) })
@@ -92,3 +92,57 @@ func (d *ArtNetDriver) RemoveBus(c *gin.Context) {
}) })
d.cfgHandler.SaveCfg(*d) d.cfgHandler.SaveCfg(*d)
} }
func (d *ArtNetDriver) Start(c *gin.Context) {
_, err := auth.GetIDFromAuth(c)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "id: " + err.Error()})
return
}
var payload models.Bus
if err := c.BindJSON(&payload); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "json: " + err.Error()})
return
}
if payload.Name == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "bus name missing"})
return
}
d.Buses[payload.Name].Start(d.Log)
c.JSON(http.StatusOK, models.JsonResponse{
Message: fmt.Sprintf("bus '%s' running", payload.Name),
})
d.cfgHandler.SaveCfg(*d)
}
func (d *ArtNetDriver) Stop(c *gin.Context) {
_, err := auth.GetIDFromAuth(c)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "id: " + err.Error()})
return
}
var payload models.Bus
if err := c.BindJSON(&payload); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "json: " + err.Error()})
return
}
if payload.Name == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "bus name missing"})
return
}
d.Buses[payload.Name].Stop()
c.JSON(http.StatusOK, models.JsonResponse{
Message: fmt.Sprintf("bus '%s' stopped", payload.Name),
})
d.cfgHandler.SaveCfg(*d)
}

View File

@@ -4,6 +4,7 @@ import (
"artNet/driver" "artNet/driver"
"artNet/server" "artNet/server"
"flag" "flag"
"fmt"
"os" "os"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@@ -41,11 +42,14 @@ func main() {
s.Routes.GET("/buses/all", artNetDriver.GetAllBuses) s.Routes.GET("/buses/all", artNetDriver.GetAllBuses)
s.Routes.POST("/buses/create", artNetDriver.CreateBus) s.Routes.POST("/buses/create", artNetDriver.CreateBus)
s.Routes.POST("/buses/remove", artNetDriver.RemoveBus) s.Routes.POST("/buses/remove", artNetDriver.RemoveBus)
s.Routes.POST("/buses/start", artNetDriver.Start)
s.Routes.POST("/buses/stop", artNetDriver.Stop)
s.Routes.GET("/", func(c *gin.Context) { s.Routes.GET("/", func(c *gin.Context) {
c.String(200, "ArtNet Driver WebSocket Server is running!") c.String(200, "ArtNet Driver WebSocket Server is running!")
}) })
artNetDriver.Log.Info("main", fmt.Sprintf("listen on %d", *port))
// start http server // start http server
if err := s.ServeHttp(*port); err != nil { if err := s.ServeHttp(*port); err != nil {
artNetDriver.Log.Error("main", "error http server "+err.Error()) artNetDriver.Log.Error("main", "error http server "+err.Error())

View File

@@ -1,12 +1,13 @@
package models package models
import ( import (
"context"
"fmt" "fmt"
"log"
"net" "net"
"time" "time"
"github.com/tatsushid/go-fastping" "github.com/tatsushid/go-fastping"
"github.com/tecamino/tecamino-logger/logging"
) )
// Art-Net constants // Art-Net constants
@@ -20,6 +21,8 @@ type Bus struct {
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:"-"` 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) // 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 { func (b *Bus) SendData() error {
if !b.Reachable {
return nil
}
// Send packet over UDP // 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),
@@ -105,16 +151,6 @@ func (b *Bus) SendData() error {
} }
defer conn.Close() 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)) _, err = conn.Write(NewArtNetPackage(b.Data))
return err return err