modify websocketserver to broker with callback functions
This commit is contained in:
@@ -1,15 +1,18 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
serverModels "github.com/tecamino/tecamino-dbm/server/models"
|
||||
"github.com/tecamino/tecamino-dbm/utils"
|
||||
ws "github.com/tecamino/tecamino-dbm/websocket"
|
||||
wsModels "github.com/tecamino/tecamino-dbm/websocket/models"
|
||||
json_data "github.com/tecamino/tecamino-json_data"
|
||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||
)
|
||||
@@ -31,6 +34,7 @@ type Datapoint struct {
|
||||
ReadWrite json_dataModels.Rights `json:"readWrite"`
|
||||
Drivers json_dataModels.Drivers `json:"drivers,omitempty"`
|
||||
Subscriptions Subscriptions `json:"-"`
|
||||
sync.RWMutex `json:"-"`
|
||||
}
|
||||
|
||||
func (d *Datapoint) Set(path string, set json_dataModels.Set) (bool, error) {
|
||||
@@ -87,7 +91,7 @@ func (d *Datapoint) GetValueUint64() uint64 {
|
||||
return utils.Uint64From(d.Value)
|
||||
}
|
||||
|
||||
func (d *Datapoint) CreateDatapoints(conns *serverModels.Connections, sets ...json_dataModels.Set) (created []json_dataModels.Set, uuids Uuids, err error) {
|
||||
func (d *Datapoint) CreateDatapoints(sets ...json_dataModels.Set) (created []json_dataModels.Set, uuids Uuids, err error) {
|
||||
if len(sets) == 0 {
|
||||
return
|
||||
}
|
||||
@@ -121,7 +125,7 @@ func (d *Datapoint) CreateDatapoints(conns *serverModels.Connections, sets ...js
|
||||
})
|
||||
|
||||
if publish {
|
||||
existing.Publish(conns, OnChange)
|
||||
existing.Publish(OnChange)
|
||||
}
|
||||
} else {
|
||||
ndp := Datapoint{
|
||||
@@ -144,7 +148,7 @@ func (d *Datapoint) CreateDatapoints(conns *serverModels.Connections, sets ...js
|
||||
Driver: dp.Driver,
|
||||
})
|
||||
if publish {
|
||||
current.Publish(conns, OnChange)
|
||||
current.Publish(OnChange)
|
||||
}
|
||||
//add uuid to flat map for faster lookuo
|
||||
uuids[ndp.Uuid] = &ndp
|
||||
@@ -187,7 +191,7 @@ func (d *Datapoint) CreateDatapoints(conns *serverModels.Connections, sets ...js
|
||||
return
|
||||
}
|
||||
|
||||
func (d *Datapoint) ImportDatapoint(conns *serverModels.Connections, dp Datapoint, path string) (uuids Uuids, err error) {
|
||||
func (d *Datapoint) ImportDatapoint(dp *Datapoint, path string) (uuids Uuids, err error) {
|
||||
parts := strings.Split(dp.Path, ":")
|
||||
|
||||
uuids = make(Uuids, 1)
|
||||
@@ -205,16 +209,16 @@ func (d *Datapoint) ImportDatapoint(conns *serverModels.Connections, dp Datapoin
|
||||
existing.Value = dp.Type.ConvertValue(dp.Value)
|
||||
existing.ReadWrite = dp.ReadWrite.GetRights()
|
||||
existing.UpdateDateTime = time.Now().UnixMilli()
|
||||
dp.Publish(conns, OnChange)
|
||||
dp.Publish(OnChange)
|
||||
} else {
|
||||
dp.Path = strings.Join(parts, ":")
|
||||
dp.ReadWrite = dp.ReadWrite.GetRights()
|
||||
dp.UpdateDateTime = time.Now().UnixMilli()
|
||||
dp.Subscriptions = InitSubscribtion()
|
||||
current.Datapoints[part] = &dp
|
||||
current.Datapoints[part] = dp
|
||||
//add uuid to flat map for faster lookuo
|
||||
uuids[dp.Uuid] = &dp
|
||||
dp.Publish(conns, OnChange)
|
||||
uuids[dp.Uuid] = dp
|
||||
dp.Publish(OnChange)
|
||||
}
|
||||
|
||||
return uuids, nil
|
||||
@@ -233,14 +237,14 @@ func (d *Datapoint) ImportDatapoint(conns *serverModels.Connections, dp Datapoin
|
||||
newDp.ReadWrite = newDp.ReadWrite.GetRights()
|
||||
current.Datapoints[part] = newDp
|
||||
current = newDp
|
||||
//add uuid to flat map for faster lookuo
|
||||
//add uuid to flat map for faster lookup
|
||||
uuids[newDp.Uuid] = newDp
|
||||
}
|
||||
}
|
||||
return uuids, nil
|
||||
}
|
||||
|
||||
func (d *Datapoint) UpdateDatapointValue(conns *serverModels.Connections, value any, path string) error {
|
||||
func (d *Datapoint) UpdateDatapointValue(value any, path string) error {
|
||||
|
||||
paths := strings.Split(path, ":")
|
||||
|
||||
@@ -253,7 +257,7 @@ func (d *Datapoint) UpdateDatapointValue(conns *serverModels.Connections, value
|
||||
if i == len(paths)-1 {
|
||||
dp.Value = dp.Type.ConvertValue(value)
|
||||
dp.UpdateDateTime = time.Now().UnixMilli()
|
||||
dp.Publish(conns, OnChange)
|
||||
dp.Publish(OnChange)
|
||||
return nil
|
||||
}
|
||||
current = dp
|
||||
@@ -261,14 +265,14 @@ func (d *Datapoint) UpdateDatapointValue(conns *serverModels.Connections, value
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Datapoint) UpdateValue(conns *serverModels.Connections, value any) error {
|
||||
func (d *Datapoint) UpdateValue(conns *ws.ClientHandler, value any) error {
|
||||
d.Value = d.Type.ConvertValue(value)
|
||||
d.UpdateDateTime = time.Now().UnixMilli()
|
||||
d.Publish(conns, OnChange)
|
||||
d.Publish(OnChange)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Datapoint) RemoveDatapoint(conns *serverModels.Connections, set json_dataModels.Set) (json_dataModels.Set, error) {
|
||||
func (d *Datapoint) RemoveDatapoint(conns *ws.ClientHandler, set json_dataModels.Set) (json_dataModels.Set, error) {
|
||||
parts := strings.Split(set.Path, ":")
|
||||
|
||||
if len(parts) < 1 {
|
||||
@@ -276,7 +280,7 @@ func (d *Datapoint) RemoveDatapoint(conns *serverModels.Connections, set json_da
|
||||
}
|
||||
|
||||
current := d
|
||||
for i := range len(parts) - 1 {
|
||||
for i := 0; i < len(parts)-1; i++ {
|
||||
next, ok := current.Datapoints[parts[i]]
|
||||
if !ok {
|
||||
return json_dataModels.Set{}, fmt.Errorf("path not found: '%s'", strings.Join(parts[:i+1], ":"))
|
||||
@@ -286,7 +290,7 @@ func (d *Datapoint) RemoveDatapoint(conns *serverModels.Connections, set json_da
|
||||
|
||||
toDelete := parts[len(parts)-1]
|
||||
if dp, ok := current.Datapoints[toDelete]; ok {
|
||||
dp.Publish(conns, OnDelete)
|
||||
dp.Publish(OnDelete)
|
||||
delete(current.Datapoints, toDelete)
|
||||
return json_dataModels.Set{
|
||||
Uuid: set.Uuid,
|
||||
@@ -354,17 +358,17 @@ func (d *Datapoint) QueryDatapoints(depth uint, path string) (dps Datapoints) {
|
||||
return
|
||||
}
|
||||
|
||||
func (d *Datapoint) AddSubscribtion(id string, sub json_dataModels.Subscription) {
|
||||
func (d *Datapoint) AddSubscribtion(conn *wsModels.Client, sub json_dataModels.Subscription) {
|
||||
if d.Subscriptions == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if s, ok := d.Subscriptions[id]; ok {
|
||||
if s, ok := d.Subscriptions[conn]; ok {
|
||||
s.OnCreate = sub.OnCreate
|
||||
s.OnChange = sub.OnChange
|
||||
s.OnDelete = sub.OnDelete
|
||||
} else {
|
||||
d.Subscriptions[id] = &Subscription{
|
||||
d.Subscriptions[conn] = &Subscription{
|
||||
OnCreate: sub.OnCreate,
|
||||
OnChange: sub.OnChange,
|
||||
OnDelete: sub.OnDelete,
|
||||
@@ -372,36 +376,30 @@ func (d *Datapoint) AddSubscribtion(id string, sub json_dataModels.Subscription)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Datapoint) RemoveSubscribtion(id string) {
|
||||
if _, ok := d.Subscriptions[id]; !ok {
|
||||
return
|
||||
}
|
||||
delete(d.Subscriptions, id)
|
||||
func (d *Datapoint) RemoveSubscribtion(client *wsModels.Client) {
|
||||
delete(d.Subscriptions, client)
|
||||
}
|
||||
|
||||
func (d *Datapoint) Publish(conns *serverModels.Connections, eventType string) error {
|
||||
if conns.Clients == nil {
|
||||
return nil
|
||||
}
|
||||
conns.RLock()
|
||||
defer conns.RUnlock()
|
||||
func (d *Datapoint) Publish(eventType string) error {
|
||||
d.RLock()
|
||||
defer d.RUnlock()
|
||||
|
||||
for id := range d.Subscriptions {
|
||||
if _, ok := conns.Clients[id]; !ok {
|
||||
delete(d.Subscriptions, id)
|
||||
} else {
|
||||
r := json_data.NewResponse()
|
||||
r.AddUPublish(json_dataModels.Publish{
|
||||
Event: eventType,
|
||||
Uuid: d.Uuid,
|
||||
Path: d.Path,
|
||||
Value: d.Value,
|
||||
})
|
||||
|
||||
if err := conns.SendResponse(id, r); err != nil {
|
||||
return err
|
||||
}
|
||||
for client := range d.Subscriptions {
|
||||
r := json_data.NewResponse()
|
||||
r.AddPublish(json_dataModels.Publish{
|
||||
Event: eventType,
|
||||
Uuid: d.Uuid,
|
||||
Path: d.Path,
|
||||
Value: d.Value,
|
||||
})
|
||||
b, err := json.Marshal(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := client.SendResponse(b, 5); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@@ -8,8 +8,8 @@ import (
|
||||
"maps"
|
||||
|
||||
"github.com/google/uuid"
|
||||
serverModels "github.com/tecamino/tecamino-dbm/server/models"
|
||||
"github.com/tecamino/tecamino-dbm/utils"
|
||||
ws "github.com/tecamino/tecamino-dbm/websocket"
|
||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||
"github.com/tecamino/tecamino-logger/logging"
|
||||
)
|
||||
@@ -17,13 +17,13 @@ import (
|
||||
type DBM struct {
|
||||
Datapoints Datapoint
|
||||
Uuids Uuids
|
||||
Conns *serverModels.Connections
|
||||
Conns *ws.ClientHandler
|
||||
Log *logging.Logger
|
||||
}
|
||||
|
||||
var SystemDatapoints uuid.UUID
|
||||
|
||||
func NewDBM(conns *serverModels.Connections, log *logging.Logger) *DBM {
|
||||
func NewDBM(conns *ws.ClientHandler, log *logging.Logger) *DBM {
|
||||
return &DBM{
|
||||
Uuids: make(Uuids),
|
||||
Conns: conns,
|
||||
@@ -36,7 +36,7 @@ func (d *DBM) CreateDatapoints(sets ...json_dataModels.Set) ([]json_dataModels.S
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
dps, uuids, err := d.Datapoints.CreateDatapoints(d.Conns, sets...)
|
||||
dps, uuids, err := d.Datapoints.CreateDatapoints(sets...)
|
||||
|
||||
//save uuid in seperate map for fast look up
|
||||
maps.Copy(d.Uuids, uuids)
|
||||
@@ -56,9 +56,9 @@ func (d *DBM) CreateDatapoints(sets ...json_dataModels.Set) ([]json_dataModels.S
|
||||
return dps, nil
|
||||
}
|
||||
|
||||
func (d *DBM) ImportDatapoints(dps ...Datapoint) error {
|
||||
func (d *DBM) ImportDatapoints(dps ...*Datapoint) error {
|
||||
for _, dp := range dps {
|
||||
uuids, err := d.Datapoints.ImportDatapoint(d.Conns, dp, dp.Path)
|
||||
uuids, err := d.Datapoints.ImportDatapoint(dp, dp.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -77,14 +77,14 @@ func (d *DBM) UpdateDatapointValue(value any, uid uuid.UUID, path ...string) err
|
||||
dp := d.Uuids[uid]
|
||||
dp.Value = dp.Type.ConvertValue(value)
|
||||
dp.UpdateDateTime = time.Now().UnixMilli()
|
||||
dp.Publish(d.Conns, OnChange)
|
||||
dp.Publish(OnChange)
|
||||
}
|
||||
|
||||
if len(path) > 1 {
|
||||
return fmt.Errorf("only one path allowed")
|
||||
}
|
||||
|
||||
return d.Datapoints.UpdateDatapointValue(d.Conns, value, path[0])
|
||||
return d.Datapoints.UpdateDatapointValue(value, path[0])
|
||||
}
|
||||
|
||||
func (d *DBM) RemoveDatapoint(sets ...json_dataModels.Set) ([]json_dataModels.Set, error) {
|
||||
@@ -136,7 +136,6 @@ func (d *DBM) ModifyCountedDatapoints(count uint64, countDown bool) {
|
||||
|
||||
func (d *DBM) GoSystemTime() error {
|
||||
path := "System:Time"
|
||||
var tOld int64
|
||||
|
||||
typ := json_dataModels.STR
|
||||
rights := json_dataModels.Read
|
||||
@@ -147,15 +146,12 @@ func (d *DBM) GoSystemTime() error {
|
||||
}
|
||||
|
||||
go func() {
|
||||
for {
|
||||
t := time.Now().UnixMilli()
|
||||
if tOld != t {
|
||||
if er := d.UpdateDatapointValue(time.UnixMilli(t).Format("2006-01-02 15:04:05"), uuid.Nil, path); er != nil {
|
||||
d.Log.Error("dmb.Handler.AddSystemDps.UpdateDatapointValue", er.Error())
|
||||
}
|
||||
tOld = t
|
||||
ticker := time.NewTicker(time.Second)
|
||||
for range ticker.C {
|
||||
if er := d.UpdateDatapointValue(time.Now().Format("2006-01-02 15:04:05"), uuid.Nil, path); er != nil {
|
||||
d.Log.Error("dmb.Handler.AddSystemDps.UpdateDatapointValue", er.Error())
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
|
@@ -1,6 +1,8 @@
|
||||
package models
|
||||
|
||||
type Subscriptions map[string]*Subscription
|
||||
import wsModels "github.com/tecamino/tecamino-dbm/websocket/models"
|
||||
|
||||
type Subscriptions map[*wsModels.Client]*Subscription
|
||||
|
||||
type Subscription struct {
|
||||
OnCreate bool
|
||||
|
Reference in New Issue
Block a user