implement new json_data model
This commit is contained in:
@@ -2,45 +2,74 @@ package dbm
|
||||
|
||||
import (
|
||||
"github.com/coder/websocket/wsjson"
|
||||
"github.com/zuadi/tecamino-dbm/models"
|
||||
"golang.org/x/net/context"
|
||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||
)
|
||||
|
||||
func (d *DBMHandler) Subscribe(ctx context.Context, sub *models.Subscribe, id string) {
|
||||
func (d *DBMHandler) Subscribe(subs []json_dataModels.Subscribe, id string) {
|
||||
if subs == nil {
|
||||
return
|
||||
}
|
||||
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 {
|
||||
client, ok := d.Conns.Clients[id]
|
||||
if !ok {
|
||||
d.Log.Error("subscribe.Subscribe", "client not found for id "+id)
|
||||
return
|
||||
}
|
||||
|
||||
response := json_dataModels.NewResponse()
|
||||
|
||||
for _, sub := range subs {
|
||||
for _, dp := range d.DB.QueryDatapoints(sub.Depth, sub.Path) {
|
||||
if sub.Driver != "" {
|
||||
if dp.Drivers == nil || dp.Drivers[sub.Driver] == nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
dp.AddSubscribtion(id, sub)
|
||||
response.AddSubscription(json_dataModels.Subscribe{
|
||||
Uuid: dp.Uuid,
|
||||
Path: dp.Path,
|
||||
Value: dp.Value,
|
||||
Driver: sub.Driver,
|
||||
Drivers: &dp.Drivers,
|
||||
})
|
||||
}
|
||||
}
|
||||
if err := wsjson.Write(client.Ctx, client.Conn, response); err != nil {
|
||||
d.Log.Error("subscribe.Subscribe", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DBMHandler) Unsubscribe(subs []json_dataModels.Subscribe, id string) {
|
||||
if subs == nil {
|
||||
return
|
||||
}
|
||||
d.RLock()
|
||||
defer d.RUnlock()
|
||||
|
||||
client, ok := d.Conns.Clients[id]
|
||||
if !ok {
|
||||
d.Log.Error("subscribe.Subscribe", "client not found for id "+id)
|
||||
return
|
||||
}
|
||||
|
||||
response := json_dataModels.NewResponse()
|
||||
|
||||
for _, sub := range subs {
|
||||
for _, dp := range d.DB.QueryDatapoints(sub.Depth, sub.Path) {
|
||||
if _, ok := dp.Subscriptions[id]; !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())
|
||||
dp.RemoveSubscribtion(id)
|
||||
response.AddUnsubscription(json_dataModels.Subscribe{
|
||||
Uuid: dp.Uuid,
|
||||
Path: dp.Path,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
if err := wsjson.Write(client.Ctx, client.Conn, response); err != nil {
|
||||
d.Log.Error("subscribe.Subscribe", err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user