modify websocketserver to broker with callback functions
This commit is contained in:
@@ -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
|
||||
|
Reference in New Issue
Block a user