35 lines
704 B
Go
35 lines
704 B
Go
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
|
|
}
|