updated websocket as client broker
This commit is contained in:
@@ -2,11 +2,13 @@ package driver
|
||||
|
||||
import (
|
||||
"artNet/cfg"
|
||||
"artNet/client"
|
||||
"artNet/models"
|
||||
ws "artNet/websocket"
|
||||
"fmt"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
json_data "github.com/tecamino/tecamino-json_data"
|
||||
"github.com/tecamino/tecamino-logger/logging"
|
||||
)
|
||||
|
||||
@@ -14,7 +16,6 @@ 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:"-"`
|
||||
Conn *client.Client `yaml:"-" json:"-"`
|
||||
Subscriptions models.Subscriptions `yaml:"-" json:"-"`
|
||||
Log *logging.Logger `yaml:"-" json:"-"`
|
||||
}
|
||||
@@ -89,30 +90,67 @@ 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 {
|
||||
d.Conn = client.NewClient(d.Log)
|
||||
if err := d.Conn.Connect(ip, id, port); err != nil {
|
||||
var err error
|
||||
client, err := ws.NewClient(ip, id, port)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer d.Conn.Disconnect()
|
||||
client.OnError = func(err error) {
|
||||
d.Log.Error("websocket connection", err)
|
||||
}
|
||||
client.OnMessage = func(data []byte) {
|
||||
//fmt.Println(100, string(data))
|
||||
fmt.Println(100, string(data))
|
||||
}
|
||||
|
||||
if err := d.Conn.Subscribe(id); err != nil {
|
||||
return err
|
||||
client.Connect(5)
|
||||
|
||||
req := json_data.NewRequest()
|
||||
req.AddDriverSubscription(".*", id, 0, true, false, false)
|
||||
|
||||
if err := client.SendData(req); err != nil {
|
||||
d.Log.Error("websocket send data", 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())
|
||||
}
|
||||
}
|
||||
}
|
||||
time.Sleep(1)
|
||||
}
|
||||
return nil
|
||||
|
||||
// d.Conn = websocket.NewClient()
|
||||
// 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
|
||||
// }
|
||||
|
||||
// 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())
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
Reference in New Issue
Block a user