47 lines
989 B
Go
47 lines
989 B
Go
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
|
|
}
|