implement new json_data model
This commit is contained in:
66
client/client.go
Normal file
66
client/client.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/coder/websocket"
|
||||
"github.com/coder/websocket/wsjson"
|
||||
json_data "github.com/tecamino/tecamino-json_data"
|
||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||
"github.com/tecamino/tecamino-logger/logging"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
conn *websocket.Conn
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
log *logging.Logger
|
||||
}
|
||||
|
||||
// Initialize new websocket server
|
||||
func NewClient(log *logging.Logger) *Client {
|
||||
c := Client{
|
||||
log: log,
|
||||
}
|
||||
c.ctx, c.cancel = context.WithCancel(context.Background())
|
||||
return &c
|
||||
}
|
||||
|
||||
func (c *Client) Connect(ip, id string, port uint) (err error) {
|
||||
c.conn, _, err = websocket.Dial(c.ctx, fmt.Sprintf("ws://%s:%d/ws?id=%s", ip, port, id), nil)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Client) Disconnect() {
|
||||
c.cancel()
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
func (c *Client) ReadJsonData() (response json_dataModels.Response, err error) {
|
||||
|
||||
err = wsjson.Read(c.ctx, c.conn, &response)
|
||||
if err != nil {
|
||||
code := websocket.CloseStatus(err)
|
||||
|
||||
switch code {
|
||||
case websocket.StatusNormalClosure,
|
||||
websocket.StatusGoingAway,
|
||||
websocket.StatusNoStatusRcvd:
|
||||
c.log.Info("webSocket.readJsonData", fmt.Sprintf("WebSocket closed: %v (code: %v)\n", err, code))
|
||||
return
|
||||
default:
|
||||
c.log.Error("webSocket.readJsonData", fmt.Sprintf("WebSocket read error: %v (code: %v)\n", err, code))
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
@@ -2,8 +2,8 @@ package driver
|
||||
|
||||
import (
|
||||
"artNet/cfg"
|
||||
"artNet/client"
|
||||
"artNet/models"
|
||||
serverModels "artNet/server/models"
|
||||
"fmt"
|
||||
"path"
|
||||
|
||||
@@ -11,11 +11,12 @@ import (
|
||||
)
|
||||
|
||||
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:"-"`
|
||||
Connections serverModels.Clients `yaml:"-"`
|
||||
Log *logging.Logger `yaml:"-"`
|
||||
Name string `yaml:"driver" json:"driver"`
|
||||
Buses map[string]*models.Bus `yaml:"buses,omitempty" json:"buses,omitempty"`
|
||||
cfgHandler *cfg.Cfg `yaml:"-" json:"-"`
|
||||
Conn *client.Client `yaml:"-"`
|
||||
Subscriptions models.Subscriptions `yaml:"-"`
|
||||
Log *logging.Logger `yaml:"-"`
|
||||
}
|
||||
|
||||
// initialize new Art-Net driver
|
||||
@@ -39,11 +40,10 @@ func NewDriver(cfgDir, name string, debug bool) (*ArtNetDriver, error) {
|
||||
|
||||
logger.Debug("artNet.NewDriver", "initialize "+name+" driver")
|
||||
d := ArtNetDriver{
|
||||
Name: name,
|
||||
Buses: make(map[string]*models.Bus),
|
||||
cfgHandler: cfg.NewCfgHandler(cfgDir, name),
|
||||
Connections: serverModels.NewClients(),
|
||||
Log: logger,
|
||||
Name: name,
|
||||
Buses: make(map[string]*models.Bus),
|
||||
cfgHandler: cfg.NewCfgHandler(cfgDir, name),
|
||||
Log: logger,
|
||||
}
|
||||
|
||||
if err := d.LoadCfg(); err != nil {
|
||||
@@ -51,6 +51,8 @@ func NewDriver(cfgDir, name string, debug bool) (*ArtNetDriver, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
d.Conn = client.NewClient(logger)
|
||||
|
||||
return &d, nil
|
||||
}
|
||||
|
||||
@@ -75,11 +77,39 @@ func (d *ArtNetDriver) NewBus(name, ip string, port int) *models.Bus {
|
||||
return b
|
||||
}
|
||||
|
||||
func (d *ArtNetDriver) SetValue(set models.Set) error {
|
||||
if _, ok := d.Buses[set.Bus]; !ok {
|
||||
return fmt.Errorf("no bus '%s' found", set.Bus)
|
||||
func (d *ArtNetDriver) SetValue(bus string, address uint, value uint8) error {
|
||||
if _, ok := d.Buses[bus]; !ok {
|
||||
return fmt.Errorf("no bus '%s' found", bus)
|
||||
}
|
||||
d.Buses[set.Bus].Data.SetValue(set.Address, set.Value)
|
||||
d.Buses[bus].Data.SetValue(address, value)
|
||||
|
||||
return d.Buses[set.Bus].SendData()
|
||||
return d.Buses[bus].SendData()
|
||||
}
|
||||
|
||||
func (d *ArtNetDriver) Connect(ip, id string, port uint) error {
|
||||
if err := d.Conn.Connect(ip, id, port); err != nil {
|
||||
return err
|
||||
}
|
||||
defer d.Conn.Disconnect()
|
||||
|
||||
if err := d.Conn.Subscribe(id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -8,6 +8,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||
)
|
||||
|
||||
// sends a list of all buses in the driver
|
||||
@@ -79,17 +80,16 @@ func (d *ArtNetDriver) RemoveBus(c *gin.Context) {
|
||||
}
|
||||
|
||||
if _, ok := d.Buses[payload.Name]; !ok {
|
||||
c.JSON(http.StatusOK, gin.H{"message": "bus " + payload.Name + " not found"})
|
||||
c.JSON(http.StatusOK, models.JsonResponse{
|
||||
Message: "bus " + payload.Name + " not found"})
|
||||
r := json_dataModels.NewResponse()
|
||||
r.SendMessage("bus " + payload.Name + " not found")
|
||||
c.JSON(http.StatusOK, r)
|
||||
return
|
||||
} else {
|
||||
delete(d.Buses, payload.Name)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, models.JsonResponse{
|
||||
Message: fmt.Sprintf("bus '%s' successfully removed", payload.Name),
|
||||
})
|
||||
r := json_dataModels.NewResponse()
|
||||
r.SendMessage(fmt.Sprintf("bus '%s' successfully removed", payload.Name))
|
||||
c.JSON(http.StatusOK, r)
|
||||
d.cfgHandler.SaveCfg(*d)
|
||||
}
|
||||
|
||||
@@ -114,9 +114,9 @@ func (d *ArtNetDriver) Start(c *gin.Context) {
|
||||
|
||||
d.Buses[payload.Name].Start(d.Log)
|
||||
|
||||
c.JSON(http.StatusOK, models.JsonResponse{
|
||||
Message: fmt.Sprintf("bus '%s' running", payload.Name),
|
||||
})
|
||||
r := json_dataModels.NewResponse()
|
||||
r.SendMessage(fmt.Sprintf("bus '%s' running", payload.Name))
|
||||
c.JSON(http.StatusOK, r)
|
||||
d.cfgHandler.SaveCfg(*d)
|
||||
}
|
||||
|
||||
@@ -141,8 +141,8 @@ func (d *ArtNetDriver) Stop(c *gin.Context) {
|
||||
|
||||
d.Buses[payload.Name].Stop()
|
||||
|
||||
c.JSON(http.StatusOK, models.JsonResponse{
|
||||
Message: fmt.Sprintf("bus '%s' stopped", payload.Name),
|
||||
})
|
||||
r := json_dataModels.NewResponse()
|
||||
r.SendMessage(fmt.Sprintf("bus '%s' stopped", payload.Name))
|
||||
c.JSON(http.StatusOK, r)
|
||||
d.cfgHandler.SaveCfg(*d)
|
||||
}
|
||||
|
20
driver/subscribe.go
Normal file
20
driver/subscribe.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package driver
|
||||
|
||||
import (
|
||||
"artNet/models"
|
||||
|
||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||
)
|
||||
|
||||
func (d *ArtNetDriver) Subscribe(subs ...json_dataModels.Subscribe) {
|
||||
if d.Subscriptions == nil {
|
||||
d.Subscriptions = models.NewSubscriptions()
|
||||
}
|
||||
|
||||
for _, sub := range subs {
|
||||
if drv, ok := (*sub.Drivers)[sub.Driver]; ok {
|
||||
d.Subscriptions.AddSubscription(sub.Uuid, drv)
|
||||
d.SetValue(drv.Bus, drv.Address, uint8(sub.Value.(float64)))
|
||||
}
|
||||
}
|
||||
}
|
2
go.mod
2
go.mod
@@ -7,7 +7,9 @@ toolchain go1.23.8
|
||||
require (
|
||||
github.com/coder/websocket v1.8.13
|
||||
github.com/gin-gonic/gin v1.10.0
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/tatsushid/go-fastping v0.0.0-20160109021039-d7bb493dee3e
|
||||
github.com/tecamino/tecamino-json_data v0.0.11
|
||||
github.com/tecamino/tecamino-logger v0.2.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
4
go.sum
4
go.sum
@@ -30,6 +30,8 @@ github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MG
|
||||
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
@@ -63,6 +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/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/tecamino/tecamino-json_data v0.0.11 h1:WVcF0tj+ElM9hRM1PccbSnY8DHJnLPauqzY0o0ib9O0=
|
||||
github.com/tecamino/tecamino-json_data v0.0.11/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/go.mod h1:0M1E9Uei/qw3e3WA1x3lBo1eP3H5oeYE7GjYrMahnj8=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
|
@@ -1,23 +0,0 @@
|
||||
package models
|
||||
|
||||
type JsonData struct {
|
||||
Set *[]Set `json:"set,omitempty"`
|
||||
Create *[]Bus `json:"create,omitempty"`
|
||||
}
|
||||
|
||||
func NewRequest() *JsonData {
|
||||
return &JsonData{}
|
||||
|
||||
}
|
||||
|
||||
func (r *JsonData) AddSet(bus string, address uint, value uint8) {
|
||||
if r.Set == nil {
|
||||
r.Set = &[]Set{}
|
||||
}
|
||||
|
||||
*r.Set = append(*r.Set, Set{
|
||||
Bus: bus,
|
||||
Address: address,
|
||||
Value: value,
|
||||
})
|
||||
}
|
@@ -1,7 +0,0 @@
|
||||
package models
|
||||
|
||||
type JsonResponse struct {
|
||||
Error bool `json:"error,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
Data string `json:"data,omitempty"`
|
||||
}
|
25
models/subscriptions.go
Normal file
25
models/subscriptions.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||
)
|
||||
|
||||
type Subscriptions map[uuid.UUID]Subscription
|
||||
|
||||
type Subscription struct {
|
||||
Bus string
|
||||
Address uint
|
||||
}
|
||||
|
||||
func NewSubscriptions() Subscriptions {
|
||||
return make(Subscriptions)
|
||||
}
|
||||
|
||||
func (s *Subscriptions) AddSubscription(uid uuid.UUID, drv *json_dataModels.Driver) {
|
||||
sub := Subscription{
|
||||
Bus: drv.Bus,
|
||||
Address: drv.Address,
|
||||
}
|
||||
(*s)[uid] = sub
|
||||
}
|
Reference in New Issue
Block a user