add status server for driver state
All checks were successful
Build ArtNet Driver / build (amd64, .exe, windows) (push) Successful in 3m49s
Build ArtNet Driver / build (amd64, , linux) (push) Successful in 3m58s
Build ArtNet Driver / build (arm, 6, , linux) (push) Successful in 3m30s
Build ArtNet Driver / build (arm64, , linux) (push) Successful in 3m31s
All checks were successful
Build ArtNet Driver / build (amd64, .exe, windows) (push) Successful in 3m49s
Build ArtNet Driver / build (amd64, , linux) (push) Successful in 3m58s
Build ArtNet Driver / build (arm, 6, , linux) (push) Successful in 3m30s
Build ArtNet Driver / build (arm64, , linux) (push) Successful in 3m31s
This commit is contained in:
@@ -5,25 +5,29 @@ import (
|
||||
"artNet/models"
|
||||
ws "artNet/websocket"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"path"
|
||||
|
||||
"gitea.tecamino.com/paadi/statusServer"
|
||||
json_data "gitea.tecamino.com/paadi/tecamino-json_data"
|
||||
"gitea.tecamino.com/paadi/tecamino-logger/logging"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ArtNetDriver struct {
|
||||
Name string `yaml:"driver" json:"driver"`
|
||||
Buses map[string]*models.Bus `yaml:"buses,omitempty" json:"buses,omitempty"`
|
||||
cfgHandler *cfg.Cfg `yaml:"-" json:"-"`
|
||||
Subscriptions models.Subscriptions `yaml:"-" json:"-"`
|
||||
Log *logging.Logger `yaml:"-" json:"-"`
|
||||
statusServer *statusServer.StatusClient `yaml:"-" json:"-"`
|
||||
Name string `yaml:"driver" json:"driver"`
|
||||
Buses map[string]*models.Bus `yaml:"buses,omitempty" json:"buses,omitempty"`
|
||||
cfgHandler *cfg.Cfg `yaml:"-" json:"-"`
|
||||
Subscriptions models.Subscriptions `yaml:"-" json:"-"`
|
||||
Log *logging.Logger `yaml:"-" json:"-"`
|
||||
}
|
||||
|
||||
// initialize new Art-Net driver
|
||||
// cfgDir config directory
|
||||
// name name of driver
|
||||
func NewDriver(cfgDir, name string, debug bool) (*ArtNetDriver, error) {
|
||||
func NewDriver(cfgDir, name, ip string, port uint, debug bool) (*ArtNetDriver, error) {
|
||||
if cfgDir == "" {
|
||||
cfgDir = "./cfg"
|
||||
}
|
||||
@@ -39,12 +43,18 @@ func NewDriver(cfgDir, name string, debug bool) (*ArtNetDriver, error) {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
statusS, err := statusServer.NewStatusClient(name, ip, "api/status", port, false)
|
||||
if err != nil {
|
||||
return nil, errors.New("status server: " + err.Error())
|
||||
}
|
||||
|
||||
logger.Debug("artNet.NewDriver", "initialize "+name+" driver")
|
||||
d := ArtNetDriver{
|
||||
Name: name,
|
||||
Buses: make(map[string]*models.Bus),
|
||||
cfgHandler: cfg.NewCfgHandler(cfgDir, name),
|
||||
Log: logger,
|
||||
statusServer: statusS,
|
||||
Name: name,
|
||||
Buses: make(map[string]*models.Bus),
|
||||
cfgHandler: cfg.NewCfgHandler(cfgDir, name),
|
||||
Log: logger,
|
||||
}
|
||||
|
||||
if err := d.LoadCfg(); err != nil {
|
||||
@@ -90,13 +100,25 @@ func (d *ArtNetDriver) SetValue(bus string, address uint, value uint8) error {
|
||||
// id: id of driver
|
||||
// port: port of server
|
||||
func (d *ArtNetDriver) Connect(ip, id string, port uint) error {
|
||||
topic := "status/" + id
|
||||
|
||||
d.statusServer.Publish(topic, gin.H{
|
||||
"state": "Starting",
|
||||
})
|
||||
defer d.statusServer.Publish(topic, gin.H{
|
||||
"state": "Stopped",
|
||||
})
|
||||
errChan := make(chan error)
|
||||
client, err := ws.NewClient(ip, id, port)
|
||||
if err != nil {
|
||||
d.statusServer.Publish(topic, gin.H{
|
||||
"state": "Error",
|
||||
"message": err.Error()})
|
||||
return err
|
||||
}
|
||||
|
||||
client.OnError = func(err error) {
|
||||
d.statusServer.Publish(topic, gin.H{"state": "Error", "message": err.Error()})
|
||||
d.Log.Error("websocket connection", err)
|
||||
errChan <- err
|
||||
}
|
||||
@@ -106,6 +128,7 @@ func (d *ArtNetDriver) Connect(ip, id string, port uint) error {
|
||||
|
||||
err = json.Unmarshal(data, &request)
|
||||
if err != nil {
|
||||
d.statusServer.Publish(topic, gin.H{"state": "Error", "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -119,6 +142,7 @@ func (d *ArtNetDriver) Connect(ip, id string, port uint) error {
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
d.statusServer.Publish("error", err.Error())
|
||||
d.Log.Error("artNet.Connect", err)
|
||||
return err
|
||||
}
|
||||
@@ -131,9 +155,13 @@ func (d *ArtNetDriver) Connect(ip, id string, port uint) error {
|
||||
d.Log.Error("websocket send data", err)
|
||||
}
|
||||
|
||||
d.statusServer.Publish(topic, gin.H{"state": "Running"})
|
||||
|
||||
for err := range errChan {
|
||||
d.statusServer.Publish(topic, gin.H{"state": "Error", "message": err.Error()})
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@@ -110,6 +110,7 @@ func (d *ArtNetDriver) Start(c *gin.Context) {
|
||||
}
|
||||
|
||||
d.Buses[busPayload.Name].Start(d.Log)
|
||||
d.statusServer.Publish(fmt.Sprintf("status/%s/%s", d.Name, busPayload.Name), "Running")
|
||||
|
||||
r := json_dataModels.NewResponse()
|
||||
r.SetMessage(fmt.Sprintf("bus '%s' running", busPayload.Name))
|
||||
@@ -132,6 +133,7 @@ func (d *ArtNetDriver) Stop(c *gin.Context) {
|
||||
}
|
||||
|
||||
d.Buses[busPayload.Name].Stop()
|
||||
d.statusServer.Publish(fmt.Sprintf("status/%s/%s", d.Name, busPayload.Name), "Stopped")
|
||||
|
||||
r := json_dataModels.NewResponse()
|
||||
r.SetMessage(fmt.Sprintf("bus '%s' stopped", busPayload.Name))
|
||||
|
Reference in New Issue
Block a user