add new parameter hasChild and set with connection id

This commit is contained in:
Adrian Zürcher
2025-05-28 22:04:40 +02:00
parent 60b3f77e29
commit c57c22f93a
5 changed files with 62 additions and 27 deletions

View File

@@ -24,11 +24,12 @@ func (d *DBMHandler) Get(req *json_dataModels.Request, id string) {
for _, dp := range d.DBM.QueryDatapoints(depth, get.Uuid, get.Path) { for _, dp := range d.DBM.QueryDatapoints(depth, get.Uuid, get.Path) {
resp.AddGet(json_dataModels.Get{ resp.AddGet(json_dataModels.Get{
Uuid: dp.Uuid, Uuid: dp.Uuid,
Path: dp.Path, Path: dp.Path,
Type: dp.Type, Type: dp.Type,
Value: dp.Value, Value: dp.Value,
Rights: dp.ReadWrite, HasChild: dp.Datapoints != nil,
Rights: dp.ReadWrite,
}) })
} }
} }

View File

@@ -20,29 +20,29 @@ func (d *DBMHandler) Json_Data(c *gin.Context) {
return return
} }
respond := json_dataModels.NewResponse() resp := json_dataModels.NewResponse()
if payload.Get != nil { if len(payload.Get) > 0 {
var depth uint = 1 var depth uint = 1
for _, get := range payload.Get { for _, get := range payload.Get {
if get.Query != nil { if get.Query != nil {
depth = get.Query.Depth depth = get.Query.Depth
} }
for _, res := range d.DBM.QueryDatapoints(depth, get.Uuid, get.Path) { for _, res := range d.DBM.QueryDatapoints(depth, get.Uuid, get.Path) {
respond.AddGet(json_dataModels.Get{ resp.AddGet(json_dataModels.Get{
Uuid: res.Uuid, Uuid: res.Uuid,
Path: res.Path, Path: res.Path,
Type: res.Type, Type: res.Type,
Value: res.Value, Value: res.Value,
Rights: res.ReadWrite, HasChild: res.Datapoints != nil,
Drivers: &res.Drivers, Rights: res.ReadWrite,
}) })
} }
} }
} }
if payload.Set != nil { if len(payload.Set) > 0 {
respond.Set, err = d.DBM.CreateDatapoints(payload.Set...) resp.Set, err = d.DBM.CreateDatapoints(payload.Set...)
if err != nil { if err != nil {
r := json_data.NewResponse() r := json_data.NewResponse()
r.SetError() r.SetError()
@@ -52,7 +52,7 @@ func (d *DBMHandler) Json_Data(c *gin.Context) {
} }
} }
c.JSON(200, respond) c.JSON(200, resp)
} }
func (d *DBMHandler) Delete(c *gin.Context) { func (d *DBMHandler) Delete(c *gin.Context) {

View File

@@ -1,10 +1,12 @@
package dbm package dbm
import ( import (
"fmt"
json_dataModels "github.com/tecamino/tecamino-json_data/models" json_dataModels "github.com/tecamino/tecamino-json_data/models"
) )
func (d *DBMHandler) Set(req *json_dataModels.Request) { func (d *DBMHandler) Set(req *json_dataModels.Request, id string) {
if req == nil { if req == nil {
return return
} else if len(req.Set) == 0 { } else if len(req.Set) == 0 {
@@ -13,9 +15,28 @@ func (d *DBMHandler) Set(req *json_dataModels.Request) {
d.RLock() d.RLock()
defer d.RUnlock() defer d.RUnlock()
resp := json_dataModels.NewResponse()
resp.Id = req.Id
for _, set := range req.Set { for _, set := range req.Set {
for _, dp := range d.DBM.QueryDatapoints(1, set.Uuid, set.Path) { dps := d.DBM.QueryDatapoints(1, set.Uuid, set.Path)
if len(dps) == 0 {
resp.SetError()
if resp.Message == "" {
resp.SetMessage(fmt.Sprintf("datapoint %s not found", set.Path))
}
continue
}
for _, dp := range dps {
dp.UpdateValue(d.Conns, set.Value) dp.UpdateValue(d.Conns, set.Value)
resp.AddSet(json_dataModels.Set{
Uuid: dp.Uuid,
Path: dp.Path,
Value: dp.Value,
})
} }
} }
if err := d.Conns.SendResponse(id, resp); err != nil {
d.Log.Error("get.Set", err.Error())
}
} }

View File

@@ -1,6 +1,8 @@
package dbm package dbm
import ( import (
"fmt"
json_dataModels "github.com/tecamino/tecamino-json_data/models" json_dataModels "github.com/tecamino/tecamino-json_data/models"
) )
@@ -18,7 +20,17 @@ func (d *DBMHandler) Subscribe(req *json_dataModels.Request, id string) {
resp.Id = req.Id resp.Id = req.Id
for _, sub := range req.Subscribe { for _, sub := range req.Subscribe {
for _, dp := range d.DBM.QueryDatapoints(sub.Depth, sub.Uuid, sub.Path) { dps := d.DBM.QueryDatapoints(sub.Depth, sub.Uuid, sub.Path)
if len(dps) == 0 {
resp.SetError()
if resp.Message == "" {
resp.SetMessage(fmt.Sprintf("datapoint %s not found", sub.Path))
}
continue
}
for _, dp := range dps {
if sub.Driver != "" { if sub.Driver != "" {
if dp.Drivers == nil || dp.Drivers[sub.Driver] == nil { if dp.Drivers == nil || dp.Drivers[sub.Driver] == nil {
continue continue
@@ -26,11 +38,12 @@ func (d *DBMHandler) Subscribe(req *json_dataModels.Request, id string) {
} }
dp.AddSubscribtion(id, sub) dp.AddSubscribtion(id, sub)
resp.AddSubscription(json_dataModels.Subscription{ resp.AddSubscription(json_dataModels.Subscription{
Uuid: dp.Uuid, Uuid: dp.Uuid,
Path: dp.Path, Path: dp.Path,
Value: dp.Value, Value: dp.Value,
Driver: sub.Driver, HasChild: dp.Datapoints != nil,
Drivers: &dp.Drivers, Driver: sub.Driver,
Drivers: &dp.Drivers,
}) })
} }
} }

View File

@@ -47,7 +47,7 @@ func (d *DBMHandler) WebSocket(c *gin.Context) {
d.Get(request, id) d.Get(request, id)
// Sets // Sets
d.Set(request) d.Set(request, id)
// Subscribe // Subscribe
d.Subscribe(request, id) d.Subscribe(request, id)