Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b21101958d | ||
|
|
c3a3060129 | ||
|
|
a23f82e9fe | ||
|
|
c37dd87a37 | ||
|
|
8be5c80a22 | ||
|
|
47a065aaf9 |
4
.github/workflows/build.yml
vendored
4
.github/workflows/build.yml
vendored
@@ -36,9 +36,9 @@ jobs:
|
||||
run: |
|
||||
mkdir -p build
|
||||
if [ "${{ matrix.goos }}" == "windows" ]; then
|
||||
GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} go build -o build/tecamino-dbm-${{ matrix.goos }}-${{ matrix.goarch }}.exe main.go
|
||||
GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} go build -ldflags="-s -w" -trimpath -o build/tecamino-dbm-${{ matrix.goos }}-${{ matrix.goarch }}.exe main.go
|
||||
else
|
||||
GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} go build -o build/tecamino-dbm-${{ matrix.goos }}-${{ matrix.goarch }} main.go
|
||||
GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} go build -ldflags="-s -w" -trimpath -o build/tecamino-dbm-${{ matrix.goos }}-${{ matrix.goarch }} main.go
|
||||
fi
|
||||
|
||||
- name: Upload artifacts
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package dbm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -29,6 +30,7 @@ func (d *DBMHandler) Json_Data(c *gin.Context) {
|
||||
if get.Query != nil {
|
||||
depth = get.Query.Depth
|
||||
}
|
||||
|
||||
for _, res := range d.DBM.QueryDatapoints(depth, get.Uuid, get.Path) {
|
||||
resp.AddGet(json_dataModels.Get{
|
||||
Uuid: res.Uuid,
|
||||
@@ -44,6 +46,10 @@ func (d *DBMHandler) Json_Data(c *gin.Context) {
|
||||
}
|
||||
if len(payload.Set) > 0 {
|
||||
resp.Set, err = d.DBM.CreateDatapoints(payload.Set...)
|
||||
for i, o := range resp.Set {
|
||||
fmt.Println(1000, i, o)
|
||||
fmt.Println(1001, o.HasChild)
|
||||
}
|
||||
if err != nil {
|
||||
r := json_data.NewResponse()
|
||||
r.SetError()
|
||||
@@ -52,6 +58,7 @@ func (d *DBMHandler) Json_Data(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(200, resp)
|
||||
}
|
||||
|
||||
@@ -67,9 +74,7 @@ func (d *DBMHandler) Delete(c *gin.Context) {
|
||||
}
|
||||
|
||||
response := json_data.NewResponse()
|
||||
|
||||
if payload.Set != nil {
|
||||
|
||||
response.Set, err = d.DBM.RemoveDatapoint(payload.Set...)
|
||||
if err != nil {
|
||||
r := json_data.NewResponse()
|
||||
|
||||
@@ -30,7 +30,7 @@ func (d *DBMHandler) Set(req *json_dataModels.Request, id string) {
|
||||
}
|
||||
|
||||
for _, dp := range dps {
|
||||
dp.UpdateValue(d.Conns, set.Value)
|
||||
dp.UpdateValue(set.Value)
|
||||
|
||||
resp.AddSet(json_dataModels.Set{
|
||||
Uuid: dp.Uuid,
|
||||
|
||||
@@ -49,6 +49,7 @@ func (d *DBMHandler) Subscribe(req *json_dataModels.Request, id string) {
|
||||
Path: dp.Path,
|
||||
Value: dp.Value,
|
||||
HasChild: dp.Datapoints != nil,
|
||||
Rights: dp.ReadWrite.GetRights(),
|
||||
Driver: sub.Driver,
|
||||
Drivers: &dp.Drivers,
|
||||
})
|
||||
|
||||
@@ -46,6 +46,10 @@ func (d *DBMHandler) WebSocket(c *gin.Context) {
|
||||
d.Unsubscribe(request, id)
|
||||
}
|
||||
|
||||
client.OnWarning = func(s string) {
|
||||
d.Log.Warning("dbmHandler.webSocket.Websocket", "warning on websocket connection: "+s)
|
||||
}
|
||||
|
||||
client.OnError = func(err error) {
|
||||
d.Log.Error("dbmHandler.webSocket.Websocket", "error on websocket connection: "+err.Error())
|
||||
}
|
||||
|
||||
2
go.mod
2
go.mod
@@ -7,7 +7,7 @@ require (
|
||||
github.com/gin-gonic/gin v1.10.0
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/gorilla/websocket v1.5.3
|
||||
github.com/tecamino/tecamino-json_data v0.0.17
|
||||
github.com/tecamino/tecamino-json_data v0.0.21
|
||||
github.com/tecamino/tecamino-logger v0.2.0
|
||||
)
|
||||
|
||||
|
||||
4
go.sum
4
go.sum
@@ -71,8 +71,8 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/tecamino/tecamino-json_data v0.0.17 h1:M12UzKbfIgu/q3ERsEhGNDV3DYOvc0TioU288kQjDCA=
|
||||
github.com/tecamino/tecamino-json_data v0.0.17/go.mod h1:LLlyD7Wwqplb2BP4PeO86EokEcTRidlW5MwgPd1T2JY=
|
||||
github.com/tecamino/tecamino-json_data v0.0.21 h1:ZRN9wyn+p6J1T4b0e9xf1HStVaAy4wb+3yZ9xn5LFc0=
|
||||
github.com/tecamino/tecamino-json_data v0.0.21/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=
|
||||
|
||||
2
main.go
2
main.go
@@ -18,7 +18,7 @@ func main() {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
//save database after exeutabe ends
|
||||
//save database after executabe ends
|
||||
defer dbmHandler.SaveDb()
|
||||
|
||||
//initialize new server
|
||||
|
||||
@@ -23,16 +23,17 @@ const (
|
||||
)
|
||||
|
||||
type Datapoint struct {
|
||||
Datapoints map[string]*Datapoint `json:"-"`
|
||||
Uuid uuid.UUID `json:"uuid"`
|
||||
Path string `json:"path"`
|
||||
Value any `json:"value,omitempty"`
|
||||
Path string `json:"path"`
|
||||
CreateDateTime int64 `json:"createDateTime,omitempty"`
|
||||
UpdateDateTime int64 `json:"updateDateTime,omitempty"`
|
||||
Type json_dataModels.Type `json:"type"`
|
||||
ReadWrite json_dataModels.Rights `json:"readWrite"`
|
||||
Datapoints map[string]*Datapoint `json:"-"`
|
||||
Drivers json_dataModels.Drivers `json:"drivers,omitempty"`
|
||||
Subscriptions Subscriptions `json:"-"`
|
||||
Type json_dataModels.Type `json:"type"`
|
||||
ReadWrite json_dataModels.Rights `json:"readWrite"`
|
||||
HasChild bool `json:"-"`
|
||||
}
|
||||
|
||||
func (d *Datapoint) Set(path string, set json_dataModels.Set) (bool, error) {
|
||||
@@ -55,8 +56,8 @@ func (d *Datapoint) Set(path string, set json_dataModels.Set) (bool, error) {
|
||||
changed = true
|
||||
d.Value = d.Type.ConvertValue(set.Value)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if set.Rights != "" {
|
||||
changed = true
|
||||
d.ReadWrite = set.Rights.GetRights()
|
||||
@@ -89,14 +90,15 @@ func (d *Datapoint) GetValueUint64() uint64 {
|
||||
return utils.Uint64From(d.Value)
|
||||
}
|
||||
|
||||
func (d *Datapoint) CreateDatapoints(sets ...json_dataModels.Set) (created []json_dataModels.Set, uuids Uuids, err error) {
|
||||
func (d *Datapoint) CreateDatapoints(uuids *Uuids, sets ...json_dataModels.Set) (created []json_dataModels.Set, err error) {
|
||||
if len(sets) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
uuids = make(Uuids, 1)
|
||||
publishes := []json_dataModels.Publish{}
|
||||
|
||||
for _, dp := range sets {
|
||||
|
||||
parts := strings.Split(dp.Path, ":")
|
||||
|
||||
current := d
|
||||
@@ -104,52 +106,66 @@ func (d *Datapoint) CreateDatapoints(sets ...json_dataModels.Set) (created []jso
|
||||
if current.Datapoints == nil {
|
||||
current.Datapoints = make(map[string]*Datapoint)
|
||||
}
|
||||
|
||||
if i == len(parts)-1 {
|
||||
// Leaf node: create or update datapoint
|
||||
if existing, ok := current.Datapoints[part]; ok {
|
||||
publish, err := existing.Set("", dp)
|
||||
|
||||
_, err := existing.Set("", dp)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, err
|
||||
}
|
||||
created = append(created, json_dataModels.Set{
|
||||
Uuid: existing.Uuid,
|
||||
Path: existing.Path,
|
||||
Type: existing.Type,
|
||||
Value: existing.Value,
|
||||
Rights: existing.ReadWrite,
|
||||
Drivers: &existing.Drivers,
|
||||
Updated: true,
|
||||
Uuid: existing.Uuid,
|
||||
Path: existing.Path,
|
||||
Type: existing.Type,
|
||||
Value: existing.Value,
|
||||
Rights: existing.ReadWrite,
|
||||
Drivers: &existing.Drivers,
|
||||
Updated: true,
|
||||
HasChild: existing.HasChild,
|
||||
})
|
||||
|
||||
if publish {
|
||||
existing.Publish(OnChange)
|
||||
}
|
||||
existing.Publish(OnChange)
|
||||
|
||||
} else {
|
||||
ndp := Datapoint{
|
||||
Uuid: uuid.New(),
|
||||
var uid uuid.UUID = uuid.New()
|
||||
if dp.Uuid != uuid.Nil {
|
||||
uid = dp.Uuid
|
||||
}
|
||||
ndp := &Datapoint{
|
||||
Uuid: uid,
|
||||
CreateDateTime: time.Now().UnixMilli(),
|
||||
Subscriptions: InitSubscribtion(),
|
||||
}
|
||||
|
||||
// Create new
|
||||
current.Datapoints[part] = &ndp
|
||||
publish, err := ndp.Set(strings.Join(parts, ":"), dp)
|
||||
current.Datapoints[part] = ndp
|
||||
|
||||
_, err := ndp.Set(strings.Join(parts, ":"), dp)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//add uuid to flat map for faster lookup
|
||||
uuids.AddDatapoint(current, ndp)
|
||||
|
||||
created = append(created, json_dataModels.Set{
|
||||
Uuid: ndp.Uuid,
|
||||
Path: ndp.Path,
|
||||
Type: ndp.Type,
|
||||
Value: ndp.Value,
|
||||
Rights: ndp.ReadWrite,
|
||||
Driver: dp.Driver,
|
||||
Uuid: ndp.Uuid,
|
||||
Path: ndp.Path,
|
||||
Type: ndp.Type,
|
||||
Value: ndp.Value,
|
||||
Rights: ndp.ReadWrite,
|
||||
Driver: dp.Driver,
|
||||
HasChild: ndp.HasChild,
|
||||
})
|
||||
|
||||
publishes = append(publishes, json_dataModels.Publish{
|
||||
Event: OnCreate,
|
||||
Uuid: ndp.Uuid,
|
||||
Path: ndp.Path,
|
||||
Type: ndp.Type,
|
||||
Value: ndp.Value,
|
||||
})
|
||||
if publish {
|
||||
current.Publish(OnChange)
|
||||
}
|
||||
//add uuid to flat map for faster lookuo
|
||||
uuids[ndp.Uuid] = &ndp
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,34 +182,51 @@ func (d *Datapoint) CreateDatapoints(sets ...json_dataModels.Set) (created []jso
|
||||
Subscriptions: InitSubscribtion(),
|
||||
}
|
||||
|
||||
//add uuid to flat map for faster lookup
|
||||
uuids.AddDatapoint(current, newDp)
|
||||
|
||||
created = append(created, json_dataModels.Set{
|
||||
Uuid: newDp.Uuid,
|
||||
Path: newDp.Path,
|
||||
Type: newDp.Type,
|
||||
Value: newDp.Value,
|
||||
Rights: newDp.ReadWrite,
|
||||
Uuid: newDp.Uuid,
|
||||
Path: newDp.Path,
|
||||
Type: newDp.Type,
|
||||
Value: newDp.Value,
|
||||
Rights: newDp.ReadWrite,
|
||||
HasChild: newDp.HasChild,
|
||||
})
|
||||
|
||||
if dp.Rights != "" {
|
||||
newDp.ReadWrite = dp.Rights.GetRights()
|
||||
}
|
||||
|
||||
publishes = append(publishes, json_dataModels.Publish{
|
||||
Event: OnCreate,
|
||||
Uuid: newDp.Uuid,
|
||||
Path: newDp.Path,
|
||||
Type: newDp.Type,
|
||||
Value: newDp.Value,
|
||||
})
|
||||
|
||||
current.Datapoints[part] = newDp
|
||||
current = newDp
|
||||
|
||||
//add uuid to flat map for faster lookuo
|
||||
uuids[newDp.Uuid] = newDp
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
r := json_data.NewResponse()
|
||||
r.Publish = append(r.Publish, publishes...)
|
||||
|
||||
b, err := json.Marshal(r)
|
||||
if err != nil {
|
||||
return created, err
|
||||
}
|
||||
ws.SendBroadcast(b)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *Datapoint) ImportDatapoint(dp *Datapoint, path string) (uuids Uuids, err error) {
|
||||
func (d *Datapoint) ImportDatapoint(uuids *Uuids, dp *Datapoint, path string) (err error) {
|
||||
parts := strings.Split(dp.Path, ":")
|
||||
|
||||
uuids = make(Uuids, 1)
|
||||
|
||||
current := d
|
||||
for i, part := range parts {
|
||||
if current.Datapoints == nil {
|
||||
@@ -214,12 +247,13 @@ func (d *Datapoint) ImportDatapoint(dp *Datapoint, path string) (uuids Uuids, er
|
||||
dp.UpdateDateTime = time.Now().UnixMilli()
|
||||
dp.Subscriptions = InitSubscribtion()
|
||||
current.Datapoints[part] = dp
|
||||
//add uuid to flat map for faster lookuo
|
||||
uuids[dp.Uuid] = dp
|
||||
//add uuid to flat map for faster lookup
|
||||
uuids.AddDatapoint(current, dp)
|
||||
|
||||
dp.Publish(OnChange)
|
||||
}
|
||||
|
||||
return uuids, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
// Traverse or create intermediate nodes
|
||||
@@ -236,10 +270,10 @@ func (d *Datapoint) ImportDatapoint(dp *Datapoint, path string) (uuids Uuids, er
|
||||
current.Datapoints[part] = newDp
|
||||
current = newDp
|
||||
//add uuid to flat map for faster lookup
|
||||
uuids[newDp.Uuid] = newDp
|
||||
uuids.AddDatapoint(current, newDp)
|
||||
}
|
||||
}
|
||||
return uuids, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Datapoint) UpdateDatapointValue(value any, path string) error {
|
||||
@@ -263,57 +297,91 @@ func (d *Datapoint) UpdateDatapointValue(value any, path string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Datapoint) UpdateValue(conns *ws.ClientHandler, value any) error {
|
||||
func (d *Datapoint) UpdateValue(value any) error {
|
||||
d.Value = d.Type.ConvertValue(value)
|
||||
d.UpdateDateTime = time.Now().UnixMilli()
|
||||
d.Publish(OnChange)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Datapoint) RemoveDatapoint(conns *ws.ClientHandler, set json_dataModels.Set) (json_dataModels.Set, error) {
|
||||
func (d *Datapoint) RemoveDatapoint(set json_dataModels.Set) (sets []json_dataModels.Set, err error) {
|
||||
parts := strings.Split(set.Path, ":")
|
||||
|
||||
if len(parts) < 1 {
|
||||
return json_dataModels.Set{}, fmt.Errorf("invalid path: '%s'", set.Path)
|
||||
return sets, fmt.Errorf("invalid path: '%s'", set.Path)
|
||||
}
|
||||
|
||||
publishes := []json_dataModels.Publish{}
|
||||
|
||||
current := d
|
||||
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], ":"))
|
||||
return sets, fmt.Errorf("path not found: '%s'", strings.Join(parts[:i+1], ":"))
|
||||
}
|
||||
current = next
|
||||
}
|
||||
|
||||
toDelete := parts[len(parts)-1]
|
||||
if dp, ok := current.Datapoints[toDelete]; ok {
|
||||
dp.Publish(OnDelete)
|
||||
s, p := removeChildren(dp)
|
||||
sets = append(sets, s...)
|
||||
publishes = append(publishes, p...)
|
||||
publishes = append(publishes, json_dataModels.Publish{
|
||||
Event: OnDelete,
|
||||
Uuid: dp.Uuid,
|
||||
Path: dp.Path,
|
||||
})
|
||||
sets = append(sets, json_dataModels.Set{
|
||||
Uuid: dp.Uuid,
|
||||
Path: dp.Path,
|
||||
})
|
||||
delete(current.Datapoints, toDelete)
|
||||
return json_dataModels.Set{
|
||||
Uuid: set.Uuid,
|
||||
Path: set.Path,
|
||||
}, nil
|
||||
|
||||
r := json_data.NewResponse()
|
||||
r.Publish = append(r.Publish, publishes...)
|
||||
|
||||
b, err := json.Marshal(r)
|
||||
if err != nil {
|
||||
return sets, err
|
||||
}
|
||||
ws.SendBroadcast(b)
|
||||
|
||||
return sets, nil
|
||||
}
|
||||
return json_dataModels.Set{}, fmt.Errorf("datapoint '%s' not found", set.Path)
|
||||
return sets, fmt.Errorf("datapoint '%s' not found", set.Path)
|
||||
}
|
||||
|
||||
// removes all children and grandchlidren of datapoint
|
||||
func removeChildren(dp *Datapoint) (sets []json_dataModels.Set, pubs []json_dataModels.Publish) {
|
||||
for name, d := range dp.Datapoints {
|
||||
s, p := removeChildren(d)
|
||||
sets = append(sets, s...)
|
||||
pubs = append(pubs, p...)
|
||||
|
||||
sets = append(sets, json_dataModels.Set{
|
||||
Uuid: d.Uuid,
|
||||
Path: d.Path,
|
||||
})
|
||||
delete(d.Datapoints, name)
|
||||
}
|
||||
return sets, pubs
|
||||
}
|
||||
|
||||
func (d *Datapoint) GetAllDatapoints(depth uint) (dps Datapoints) {
|
||||
|
||||
dps = append(dps, d)
|
||||
if depth == 1 {
|
||||
return
|
||||
} else if depth > 0 {
|
||||
depth--
|
||||
}
|
||||
var dfs func(dp *Datapoint, currentDepth uint)
|
||||
dfs = func(dp *Datapoint, currentDepth uint) {
|
||||
if depth == 1 {
|
||||
return
|
||||
} else if depth == 0 {
|
||||
switch depth {
|
||||
case 0:
|
||||
// Return all descendants
|
||||
for _, child := range dp.Datapoints {
|
||||
dps = append(dps, child)
|
||||
dfs(child, currentDepth+1)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if currentDepth == depth-1 {
|
||||
case currentDepth:
|
||||
return
|
||||
}
|
||||
|
||||
@@ -322,7 +390,7 @@ func (d *Datapoint) GetAllDatapoints(depth uint) (dps Datapoints) {
|
||||
dfs(child, currentDepth+1)
|
||||
}
|
||||
}
|
||||
dps = append(dps, d)
|
||||
|
||||
dfs(d, 0)
|
||||
dps.SortSlice()
|
||||
return
|
||||
@@ -374,27 +442,60 @@ func (d *Datapoint) AddSubscribtion(conn *wsModels.Client, sub json_dataModels.S
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Datapoint) RenamePaths(oldPath string) {
|
||||
visited := make(map[*Datapoint]bool)
|
||||
|
||||
if len(d.Datapoints) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
for _, dp := range d.Datapoints {
|
||||
dp.Path = strings.Replace(dp.Path, oldPath, d.Path, 1)
|
||||
dp.renameSubPaths(oldPath, d.Path, visited)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Datapoint) renameSubPaths(oldPath, newPath string, visited map[*Datapoint]bool) {
|
||||
if visited[d] {
|
||||
return
|
||||
}
|
||||
visited[d] = true
|
||||
|
||||
if len(d.Datapoints) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
for _, dp := range d.Datapoints {
|
||||
dp.Path = strings.Replace(dp.Path, oldPath, newPath, 1)
|
||||
dp.renameSubPaths(oldPath, newPath, visited)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Datapoint) RemoveSubscribtion(client *wsModels.Client) {
|
||||
delete(d.Subscriptions, client)
|
||||
}
|
||||
|
||||
func (d *Datapoint) Publish(eventType string) error {
|
||||
for client := range d.Subscriptions {
|
||||
r := json_data.NewResponse()
|
||||
r.AddPublish(json_dataModels.Publish{
|
||||
Event: eventType,
|
||||
Uuid: d.Uuid,
|
||||
Path: d.Path,
|
||||
Type: d.Type,
|
||||
Value: d.Value,
|
||||
})
|
||||
r := json_data.NewResponse()
|
||||
r.AddPublish(json_dataModels.Publish{
|
||||
Event: eventType,
|
||||
Uuid: d.Uuid,
|
||||
Path: d.Path,
|
||||
Type: d.Type,
|
||||
Value: d.Value,
|
||||
})
|
||||
|
||||
b, err := json.Marshal(r)
|
||||
if err != nil {
|
||||
return err
|
||||
b, err := json.Marshal(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
switch eventType {
|
||||
case OnCreate, OnDelete:
|
||||
ws.SendBroadcast(b)
|
||||
default:
|
||||
for client := range d.Subscriptions {
|
||||
client.SendResponse(b)
|
||||
}
|
||||
|
||||
client.SendResponse(b)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -5,8 +5,6 @@ import (
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"maps"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/tecamino/tecamino-dbm/utils"
|
||||
ws "github.com/tecamino/tecamino-dbm/websocket"
|
||||
@@ -16,7 +14,7 @@ import (
|
||||
|
||||
type DBM struct {
|
||||
Datapoints Datapoint
|
||||
Uuids Uuids
|
||||
Uuids *Uuids
|
||||
Conns *ws.ClientHandler
|
||||
Log *logging.Logger
|
||||
}
|
||||
@@ -25,7 +23,7 @@ var SystemDatapoints uuid.UUID
|
||||
|
||||
func NewDBM(conns *ws.ClientHandler, log *logging.Logger) *DBM {
|
||||
return &DBM{
|
||||
Uuids: make(Uuids),
|
||||
Uuids: &Uuids{},
|
||||
Conns: conns,
|
||||
Log: log,
|
||||
}
|
||||
@@ -36,11 +34,7 @@ func (d *DBM) CreateDatapoints(sets ...json_dataModels.Set) ([]json_dataModels.S
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
dps, uuids, err := d.Datapoints.CreateDatapoints(sets...)
|
||||
|
||||
//save uuid in seperate map for fast look up
|
||||
maps.Copy(d.Uuids, uuids)
|
||||
|
||||
dps, err := d.Datapoints.CreateDatapoints(d.Uuids, sets...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -58,11 +52,10 @@ func (d *DBM) CreateDatapoints(sets ...json_dataModels.Set) ([]json_dataModels.S
|
||||
|
||||
func (d *DBM) ImportDatapoints(dps ...*Datapoint) error {
|
||||
for _, dp := range dps {
|
||||
uuids, err := d.Datapoints.ImportDatapoint(dp, dp.Path)
|
||||
err := d.Datapoints.ImportDatapoint(d.Uuids, dp, dp.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
maps.Copy(d.Uuids, uuids)
|
||||
|
||||
d.ModifyCountedDatapoints(1, false)
|
||||
}
|
||||
@@ -71,10 +64,10 @@ func (d *DBM) ImportDatapoints(dps ...*Datapoint) error {
|
||||
|
||||
func (d *DBM) UpdateDatapointValue(value any, uid uuid.UUID, path ...string) error {
|
||||
if uid != uuid.Nil {
|
||||
if _, ok := d.Uuids[uid]; !ok {
|
||||
dp := d.Uuids.GetDatapoint(uid)
|
||||
if dp == nil {
|
||||
return fmt.Errorf("uuid %s not found", uid.String())
|
||||
}
|
||||
dp := d.Uuids[uid]
|
||||
dp.Value = dp.Type.ConvertValue(value)
|
||||
dp.UpdateDateTime = time.Now().UnixMilli()
|
||||
dp.Publish(OnChange)
|
||||
@@ -87,27 +80,29 @@ func (d *DBM) UpdateDatapointValue(value any, uid uuid.UUID, path ...string) err
|
||||
return d.Datapoints.UpdateDatapointValue(value, path[0])
|
||||
}
|
||||
|
||||
func (d *DBM) RemoveDatapoint(sets ...json_dataModels.Set) ([]json_dataModels.Set, error) {
|
||||
var lsRemoved []json_dataModels.Set
|
||||
func (d *DBM) RemoveDatapoint(sets ...json_dataModels.Set) (lsRemoved []json_dataModels.Set, err error) {
|
||||
for _, set := range sets {
|
||||
removed, err := d.Datapoints.RemoveDatapoint(d.Conns, set)
|
||||
if err != nil {
|
||||
return lsRemoved, err
|
||||
if set.Path == "" {
|
||||
if dp := d.Uuids.GetDatapoint(set.Uuid); dp != nil {
|
||||
set.Path = dp.Path
|
||||
}
|
||||
}
|
||||
lsRemoved = append(lsRemoved, removed)
|
||||
d.ModifyCountedDatapoints(1, true)
|
||||
}
|
||||
|
||||
return lsRemoved, nil
|
||||
lsRemoved, err = d.Datapoints.RemoveDatapoint(set)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
d.ModifyCountedDatapoints(uint64(len(lsRemoved)), true)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (d *DBM) QueryDatapoints(depth uint, uid uuid.UUID, key ...string) []*Datapoint {
|
||||
if uid != uuid.Nil {
|
||||
if _, ok := d.Uuids[uid]; !ok {
|
||||
dp := d.Uuids.GetDatapoint(uid)
|
||||
if dp == nil {
|
||||
return nil
|
||||
}
|
||||
dp := d.Uuids[uid]
|
||||
if depth == 1 {
|
||||
} else if depth == 1 {
|
||||
return []*Datapoint{dp}
|
||||
}
|
||||
return append([]*Datapoint{}, dp.QueryDatapoints(depth, key[0])...)
|
||||
@@ -159,8 +154,6 @@ func (d *DBM) GoSystemTime() error {
|
||||
|
||||
func (d *DBM) GoSystemMemory() error {
|
||||
path := "System:UsedMemory"
|
||||
var m runtime.MemStats
|
||||
var mOld uint64
|
||||
|
||||
typ := json_dataModels.STR
|
||||
rights := json_dataModels.Read
|
||||
@@ -172,13 +165,11 @@ func (d *DBM) GoSystemMemory() error {
|
||||
|
||||
go func() {
|
||||
for {
|
||||
var m runtime.MemStats
|
||||
runtime.ReadMemStats(&m)
|
||||
if m.Sys != mOld {
|
||||
mem := fmt.Sprintf("%.2f MB", float64(m.Sys)/1024/1024)
|
||||
if er := d.UpdateDatapointValue(mem, uuid.Nil, path); er != nil {
|
||||
d.Log.Error("dmb.Handler.AddSystemDps.UpdateDatapointValue", er.Error())
|
||||
}
|
||||
mOld = m.Sys
|
||||
mem := fmt.Sprintf("%.2f MB", float64(m.Alloc)/1024/1024)
|
||||
if er := d.UpdateDatapointValue(mem, uuid.Nil, path); er != nil {
|
||||
d.Log.Error("dmb.Handler.AddSystemDps.UpdateDatapointValue", er.Error())
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,52 @@
|
||||
package models
|
||||
|
||||
import "github.com/google/uuid"
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||
)
|
||||
|
||||
type Uuids map[uuid.UUID]*Datapoint
|
||||
|
||||
func NewUuids() *Uuids {
|
||||
return &Uuids{}
|
||||
}
|
||||
|
||||
func (u *Uuids) AddDatapoint(parentDp, newDp *Datapoint) {
|
||||
if odp, ok := (*u)[newDp.Uuid]; ok {
|
||||
if odp.Path == newDp.Path {
|
||||
return
|
||||
}
|
||||
newDp.Datapoints = odp.Datapoints
|
||||
newDp.HasChild = len(odp.Datapoints) > 0
|
||||
odp.Datapoints = map[string]*Datapoint{}
|
||||
newDp.RenamePaths(odp.Path)
|
||||
rmDps, _ := parentDp.RemoveDatapoint(json_dataModels.Set{Path: odp.Path})
|
||||
datapoints := u.GetDatapointByPath("System:Datapoints")
|
||||
datapoints.UpdateValue(datapoints.Value.(uint64) - uint64(len(rmDps)))
|
||||
fmt.Println(11, newDp.HasChild)
|
||||
}
|
||||
|
||||
(*u)[newDp.Uuid] = newDp
|
||||
}
|
||||
|
||||
func (u *Uuids) GetDatapoint(uuid uuid.UUID) *Datapoint {
|
||||
if dp, ok := (*u)[uuid]; ok {
|
||||
return dp
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *Uuids) GetDatapointByPath(path string) *Datapoint {
|
||||
for _, dp := range *u {
|
||||
if dp.Path == path {
|
||||
return dp
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *Uuids) RemoveDatapoint(uuid uuid.UUID) {
|
||||
delete(*u, uuid)
|
||||
}
|
||||
|
||||
@@ -17,6 +17,12 @@ type ClientHandler struct {
|
||||
Clients models.Clients
|
||||
}
|
||||
|
||||
func SendBroadcast(msg []byte) {
|
||||
for _, c := range models.Broadcast {
|
||||
c.SendResponse(msg)
|
||||
}
|
||||
}
|
||||
|
||||
// initaiates new conections with client map
|
||||
func NewConnectionHandler() *ClientHandler {
|
||||
return &ClientHandler{
|
||||
@@ -32,6 +38,7 @@ func (cH *ClientHandler) ConnectNewClient(id string, c *gin.Context) (client *mo
|
||||
|
||||
client, err = models.ConnectNewClient(id, c)
|
||||
client.OnClose = func(code int, reason string) {
|
||||
fmt.Println(23, "closing", id)
|
||||
delete(cH.Clients, id)
|
||||
}
|
||||
|
||||
@@ -41,7 +48,6 @@ func (cH *ClientHandler) ConnectNewClient(id string, c *gin.Context) (client *mo
|
||||
cH.Lock()
|
||||
cH.Clients[id] = client
|
||||
cH.Unlock()
|
||||
|
||||
return client, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -13,12 +13,14 @@ import (
|
||||
|
||||
var Origins []string = []string{"*"}
|
||||
|
||||
var Broadcast Clients = make(Clients)
|
||||
|
||||
const (
|
||||
// Time allowed to write a message to the peer.
|
||||
writeWait = 10 * time.Second
|
||||
writeWait = 30 * time.Second
|
||||
|
||||
// Time allowed to read the next pong message from the peer.
|
||||
pongWait = 10 * time.Second
|
||||
pongWait = 40 * time.Second
|
||||
|
||||
// Send pings to peer with this period. Must be less than pongWait.
|
||||
pingPeriod = (pongWait * 9) / 10
|
||||
@@ -32,9 +34,9 @@ type Client struct {
|
||||
OnMessage func(data []byte)
|
||||
OnClose func(code int, reason string)
|
||||
OnError func(err error)
|
||||
OnWarning func(warn string)
|
||||
OnPing func()
|
||||
OnPong func()
|
||||
sendPong chan string
|
||||
send chan []byte
|
||||
unregister chan []byte
|
||||
}
|
||||
@@ -43,17 +45,10 @@ var upgrader = websocket.Upgrader{
|
||||
CheckOrigin: func(r *http.Request) bool {
|
||||
if len(Origins) == 0 {
|
||||
return false
|
||||
}
|
||||
if Origins[0] == "*" {
|
||||
} else if Origins[0] == "*" {
|
||||
return true
|
||||
}
|
||||
origin := r.Header.Get("Origin")
|
||||
for _, o := range Origins {
|
||||
if o == origin {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
return slices.Contains(Origins, r.Header.Get("Origin"))
|
||||
},
|
||||
EnableCompression: false,
|
||||
}
|
||||
@@ -68,22 +63,25 @@ func ConnectNewClient(id string, c *gin.Context) (*Client, error) {
|
||||
Id: id,
|
||||
Connected: true,
|
||||
conn: conn,
|
||||
sendPong: make(chan string),
|
||||
send: make(chan []byte),
|
||||
unregister: make(chan []byte),
|
||||
send: make(chan []byte, 512),
|
||||
unregister: make(chan []byte, 256),
|
||||
}
|
||||
|
||||
Broadcast[client.Id] = client
|
||||
|
||||
conn.SetPingHandler(func(appData string) error {
|
||||
if client.OnPing != nil {
|
||||
client.OnPing()
|
||||
}
|
||||
conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||
conn.SetReadDeadline(time.Now().Add(writeWait))
|
||||
client.sendPong <- appData
|
||||
if err := client.conn.WriteControl(websocket.PongMessage, []byte(appData), time.Now().Add(pongWait)); err != nil {
|
||||
client.OnError(err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
conn.SetPongHandler(func(string) error {
|
||||
conn.SetPongHandler(func(appData string) error {
|
||||
conn.SetReadDeadline(time.Now().Add(pongWait))
|
||||
if client.OnPong != nil {
|
||||
client.OnPong()
|
||||
@@ -94,6 +92,7 @@ func ConnectNewClient(id string, c *gin.Context) (*Client, error) {
|
||||
// Start reading messages from client
|
||||
go client.Read()
|
||||
go client.Write()
|
||||
go client.PingInterval(pingPeriod)
|
||||
return client, nil
|
||||
}
|
||||
|
||||
@@ -105,6 +104,7 @@ func (c *Client) Read() {
|
||||
c.conn.SetReadDeadline(time.Now().Add(writeWait))
|
||||
for c.Connected {
|
||||
msgType, msg, err := c.conn.ReadMessage()
|
||||
|
||||
if err != nil {
|
||||
c.handleError(fmt.Errorf("read error (id:%s): %w", c.Id, err))
|
||||
return
|
||||
@@ -114,12 +114,10 @@ func (c *Client) Read() {
|
||||
c.Close(websocket.CloseNormalClosure, "Client closed")
|
||||
return
|
||||
case websocket.TextMessage:
|
||||
if isPing := c.handleJsonPing(msg); !isPing {
|
||||
if c.OnMessage != nil {
|
||||
c.OnMessage(msg)
|
||||
} else {
|
||||
log.Printf("Received message but no handler set (id:%s): %s", c.Id, string(msg))
|
||||
}
|
||||
if c.OnMessage != nil {
|
||||
c.OnMessage(msg)
|
||||
} else {
|
||||
log.Printf("Received message but no handler set (id:%s): %s", c.Id, string(msg))
|
||||
}
|
||||
default:
|
||||
log.Printf("Unhandled message type %d (id:%s)", msgType, c.Id)
|
||||
@@ -128,19 +126,15 @@ func (c *Client) Read() {
|
||||
}
|
||||
|
||||
func (c *Client) Write() {
|
||||
ticker := time.NewTicker(pingPeriod)
|
||||
defer func() {
|
||||
ticker.Stop()
|
||||
c.conn.Close()
|
||||
}()
|
||||
for {
|
||||
defer c.conn.Close()
|
||||
|
||||
for {
|
||||
select {
|
||||
case message, ok := <-c.send:
|
||||
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||
if !ok {
|
||||
// The hub closed the channel.
|
||||
if err := c.conn.WriteMessage(websocket.CloseMessage, []byte{}); err != nil {
|
||||
if err := c.conn.WriteMessage(websocket.CloseMessage, []byte("ping")); err != nil {
|
||||
c.handleError(err)
|
||||
return
|
||||
}
|
||||
@@ -152,26 +146,11 @@ func (c *Client) Write() {
|
||||
return
|
||||
}
|
||||
}
|
||||
case <-ticker.C:
|
||||
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||
if err := c.conn.WriteMessage(websocket.PingMessage, nil); err != nil {
|
||||
c.handleError(err)
|
||||
return
|
||||
}
|
||||
|
||||
if c.OnPing != nil {
|
||||
c.OnPing()
|
||||
}
|
||||
|
||||
case message, ok := <-c.sendPong:
|
||||
if ok {
|
||||
c.conn.WriteMessage(websocket.PongMessage, []byte(message))
|
||||
}
|
||||
case message := <-c.unregister:
|
||||
c.conn.WriteMessage(websocket.CloseMessage, message)
|
||||
c.Connected = false
|
||||
close(c.sendPong)
|
||||
close(c.send)
|
||||
delete(Broadcast, c.Id)
|
||||
close(c.unregister)
|
||||
return
|
||||
}
|
||||
@@ -179,42 +158,44 @@ func (c *Client) Write() {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) handleJsonPing(msg []byte) (isPing bool) {
|
||||
var wsMsg WSMessage
|
||||
err := json.Unmarshal(msg, &wsMsg)
|
||||
func (c *Client) PingInterval(interval time.Duration) {
|
||||
ticker := time.NewTicker(interval)
|
||||
defer ticker.Stop()
|
||||
|
||||
if err == nil && wsMsg.IsPing() {
|
||||
c.conn.SetReadDeadline(time.Now().Add(writeWait))
|
||||
// Respond with pong JSON
|
||||
select {
|
||||
case c.send <- GetPongByteSlice():
|
||||
default:
|
||||
// optional: log or handle if send buffer is full
|
||||
c.handleError(fmt.Errorf("failed to queue pong message"))
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
c.handleError(fmt.Errorf("write pong error: %w", err))
|
||||
return
|
||||
}
|
||||
if err := c.conn.WriteControl(websocket.PingMessage, []byte("ping"), time.Now().Add(pongWait)); err != nil {
|
||||
c.OnError(err)
|
||||
return
|
||||
}
|
||||
for range ticker.C {
|
||||
if c.OnPing != nil {
|
||||
c.OnPing()
|
||||
}
|
||||
isPing = true
|
||||
}
|
||||
|
||||
return
|
||||
if err := c.conn.WriteControl(websocket.PingMessage, []byte("ping"), time.Now().Add(pongWait)); err != nil {
|
||||
c.OnError(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) SendResponse(data []byte) {
|
||||
if !c.Connected {
|
||||
return
|
||||
}
|
||||
c.send <- data
|
||||
select {
|
||||
case c.send <- data:
|
||||
// sent successfully
|
||||
default:
|
||||
// channel full, drop or log
|
||||
if c.OnWarning != nil {
|
||||
c.OnWarning("Dropping message: channel full")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) Close(code int, reason string) error {
|
||||
closeMsg := websocket.FormatCloseMessage(code, reason)
|
||||
|
||||
select {
|
||||
case c.unregister <- closeMsg: // Attempt to send
|
||||
default: // If the channel is full, this runs
|
||||
@@ -230,6 +211,7 @@ func (c *Client) handleError(err error) {
|
||||
if c.OnError != nil {
|
||||
c.OnError(err)
|
||||
}
|
||||
|
||||
if err := c.Close(websocket.CloseInternalServerErr, err.Error()); err != nil {
|
||||
if c.OnError != nil {
|
||||
c.OnError(err)
|
||||
|
||||
Reference in New Issue
Block a user