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

34
dbm/publish.go Normal file
View File

@@ -0,0 +1,34 @@
package dbm
import (
"context"
"github.com/coder/websocket/wsjson"
"github.com/zuadi/tecamino-dbm/models"
)
func (d *DBMHandler) Publish(ctx context.Context, eventType, path string, value any) error {
d.RLock()
defer d.RUnlock()
for _, dp := range d.DB.QueryDatapoints(1, path) {
for id, pub := range dp.Subscribtions {
if client, ok := d.Clients[id]; !ok {
delete(dp.Subscribtions, id)
} else {
if pub.OnChange {
err := wsjson.Write(ctx, client.Conn, models.JsonResponse{
Event: OnChange,
Path: dp.Path,
Value: value,
})
if err != nil {
d.Log.Error("publish.Publish", err.Error())
return err
}
}
}
}
}
return nil
}