new json_data model

This commit is contained in:
Adrian Zuercher
2025-05-04 22:21:12 +02:00
parent 7d6b09cf11
commit 5203fb8543
13 changed files with 79 additions and 45 deletions

View File

@@ -4,17 +4,20 @@ import (
json_dataModels "github.com/tecamino/tecamino-json_data/models"
)
func (d *DBMHandler) Subscribe(subs []json_dataModels.Subscribe, id, id2 string) {
if subs == nil {
func (d *DBMHandler) Subscribe(req *json_dataModels.Request, id string) {
if req == nil {
return
}
if len(req.Subscribe) == 0 {
return
}
d.RLock()
defer d.RUnlock()
r := json_dataModels.NewResponse()
r.Id = id2
resp := json_dataModels.NewResponse()
resp.Id = req.Id
for _, sub := range subs {
for _, sub := range req.Subscribe {
for _, dp := range d.DB.QueryDatapoints(sub.Depth, sub.Path) {
if sub.Driver != "" {
if dp.Drivers == nil || dp.Drivers[sub.Driver] == nil {
@@ -22,7 +25,7 @@ func (d *DBMHandler) Subscribe(subs []json_dataModels.Subscribe, id, id2 string)
}
}
dp.AddSubscribtion(id, sub)
r.AddSubscription(json_dataModels.Subscribe{
resp.AddSubscription(json_dataModels.Subscription{
Uuid: dp.Uuid,
Path: dp.Path,
Value: dp.Value,
@@ -32,34 +35,37 @@ func (d *DBMHandler) Subscribe(subs []json_dataModels.Subscribe, id, id2 string)
}
}
if err := d.Conns.SendResponse(id, r); err != nil {
if err := d.Conns.SendResponse(id, resp); err != nil {
d.Log.Error("subscribe.Subscribe", err.Error())
}
}
func (d *DBMHandler) Unsubscribe(subs []json_dataModels.Subscribe, id string) {
if subs == nil {
func (d *DBMHandler) Unsubscribe(req *json_dataModels.Request, id string) {
if req == nil {
return
}
if len(req.Unsubscribe) == 0 {
return
}
d.RLock()
defer d.RUnlock()
r := json_dataModels.NewResponse()
resp := json_dataModels.NewResponse()
for _, sub := range subs {
for _, sub := range req.Unsubscribe {
for _, dp := range d.DB.QueryDatapoints(sub.Depth, sub.Path) {
if _, ok := dp.Subscriptions[id]; !ok {
continue
}
dp.RemoveSubscribtion(id)
r.AddUnsubscription(json_dataModels.Subscribe{
resp.AddUnsubscription(json_dataModels.Subscription{
Uuid: dp.Uuid,
Path: dp.Path,
})
}
}
if err := d.Conns.SendResponse(id, r); err != nil {
if err := d.Conns.SendResponse(id, resp); err != nil {
d.Log.Error("subscribe.Unsubscribe", err.Error())
}
}