major change of websocket dbmHandler structure

This commit is contained in:
Adrian Zürcher
2025-04-23 21:53:01 +02:00
parent a1f947e24a
commit 0a137c9d86
35 changed files with 1676 additions and 424 deletions

46
dbm/subscribe.go Normal file
View File

@@ -0,0 +1,46 @@
package dbm
import (
"github.com/coder/websocket/wsjson"
"github.com/zuadi/tecamino-dbm/models"
"golang.org/x/net/context"
)
func (d *DBMHandler) Subscribe(ctx context.Context, sub *models.Subscribe, id string) {
d.RLock()
defer d.RUnlock()
for _, dp := range d.DB.QueryDatapoints(sub.Depth, sub.Path) {
if sub.Driver != nil {
if dp.Drivers == nil {
continue
} else if _, ok := dp.Drivers[*sub.Driver]; !ok {
continue
}
}
dp.AddSubscribtion(id, sub)
err := wsjson.Write(ctx, d.Clients[id].Conn, models.JsonResponse{
Event: OnCreate,
Path: dp.Path,
Value: dp.Value,
})
if err != nil {
d.Log.Error("subscribe.Subscribe", err.Error())
}
}
}
func (d *DBMHandler) Unsubscribe(ctx context.Context, sub *models.Subscribe, id string) error {
d.RLock()
defer d.RUnlock()
for _, dp := range d.DB.QueryDatapoints(sub.Depth, sub.Path) {
if _, ok := dp.Subscribtions[id]; !ok {
continue
}
dp.RemoveSubscribtion(id)
}
return nil
}