new respond to send all create change delete different bug fixes
This commit is contained in:
@@ -55,8 +55,8 @@ func (d *Datapoint) Set(path string, set json_dataModels.Set) (bool, error) {
|
||||
changed = true
|
||||
d.Value = d.Type.ConvertValue(set.Value)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if set.Rights != "" {
|
||||
changed = true
|
||||
d.ReadWrite = set.Rights.GetRights()
|
||||
@@ -108,7 +108,8 @@ func (d *Datapoint) CreateDatapoints(sets ...json_dataModels.Set) (created []jso
|
||||
if i == len(parts)-1 {
|
||||
// Leaf node: create or update datapoint
|
||||
if existing, ok := current.Datapoints[part]; ok {
|
||||
publish, err := existing.Set("", dp)
|
||||
|
||||
_, err := existing.Set("", dp)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
@@ -122,18 +123,18 @@ func (d *Datapoint) CreateDatapoints(sets ...json_dataModels.Set) (created []jso
|
||||
Updated: true,
|
||||
})
|
||||
|
||||
if publish {
|
||||
existing.Publish(OnChange)
|
||||
}
|
||||
existing.Publish(OnChange)
|
||||
|
||||
} else {
|
||||
ndp := Datapoint{
|
||||
Uuid: uuid.New(),
|
||||
CreateDateTime: time.Now().UnixMilli(),
|
||||
Subscriptions: InitSubscribtion(),
|
||||
}
|
||||
|
||||
// Create new
|
||||
current.Datapoints[part] = &ndp
|
||||
publish, err := ndp.Set(strings.Join(parts, ":"), dp)
|
||||
_, err := ndp.Set(strings.Join(parts, ":"), dp)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
@@ -145,10 +146,9 @@ func (d *Datapoint) CreateDatapoints(sets ...json_dataModels.Set) (created []jso
|
||||
Rights: ndp.ReadWrite,
|
||||
Driver: dp.Driver,
|
||||
})
|
||||
if publish {
|
||||
current.Publish(OnChange)
|
||||
}
|
||||
//add uuid to flat map for faster lookuo
|
||||
ndp.Publish(OnCreate)
|
||||
|
||||
//add uuid to flat map for faster lookup
|
||||
uuids[ndp.Uuid] = &ndp
|
||||
}
|
||||
}
|
||||
@@ -178,6 +178,8 @@ func (d *Datapoint) CreateDatapoints(sets ...json_dataModels.Set) (created []jso
|
||||
newDp.ReadWrite = dp.Rights.GetRights()
|
||||
}
|
||||
|
||||
newDp.Publish(OnCreate)
|
||||
|
||||
current.Datapoints[part] = newDp
|
||||
current = newDp
|
||||
|
||||
@@ -214,7 +216,7 @@ func (d *Datapoint) ImportDatapoint(dp *Datapoint, path string) (uuids Uuids, er
|
||||
dp.UpdateDateTime = time.Now().UnixMilli()
|
||||
dp.Subscriptions = InitSubscribtion()
|
||||
current.Datapoints[part] = dp
|
||||
//add uuid to flat map for faster lookuo
|
||||
//add uuid to flat map for faster lookup
|
||||
uuids[dp.Uuid] = dp
|
||||
dp.Publish(OnChange)
|
||||
}
|
||||
@@ -270,47 +272,64 @@ func (d *Datapoint) UpdateValue(conns *ws.ClientHandler, value any) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Datapoint) RemoveDatapoint(conns *ws.ClientHandler, set json_dataModels.Set) (json_dataModels.Set, error) {
|
||||
func (d *Datapoint) RemoveDatapoint(conns *ws.ClientHandler, set json_dataModels.Set) (sets []json_dataModels.Set, err error) {
|
||||
parts := strings.Split(set.Path, ":")
|
||||
|
||||
if len(parts) < 1 {
|
||||
return json_dataModels.Set{}, fmt.Errorf("invalid path: '%s'", set.Path)
|
||||
return sets, fmt.Errorf("invalid path: '%s'", set.Path)
|
||||
}
|
||||
|
||||
current := d
|
||||
for i := 0; i < len(parts)-1; i++ {
|
||||
next, ok := current.Datapoints[parts[i]]
|
||||
if !ok {
|
||||
return json_dataModels.Set{}, fmt.Errorf("path not found: '%s'", strings.Join(parts[:i+1], ":"))
|
||||
return sets, fmt.Errorf("path not found: '%s'", strings.Join(parts[:i+1], ":"))
|
||||
}
|
||||
current = next
|
||||
}
|
||||
|
||||
toDelete := parts[len(parts)-1]
|
||||
if dp, ok := current.Datapoints[toDelete]; ok {
|
||||
sets = append(sets, removeChildren(dp)...)
|
||||
dp.Publish(OnDelete)
|
||||
sets = append(sets, json_dataModels.Set{
|
||||
Uuid: dp.Uuid,
|
||||
Path: dp.Path,
|
||||
})
|
||||
delete(current.Datapoints, toDelete)
|
||||
return json_dataModels.Set{
|
||||
Uuid: set.Uuid,
|
||||
Path: set.Path,
|
||||
}, nil
|
||||
return sets, nil
|
||||
}
|
||||
return json_dataModels.Set{}, fmt.Errorf("datapoint '%s' not found", set.Path)
|
||||
return sets, fmt.Errorf("datapoint '%s' not found", set.Path)
|
||||
}
|
||||
|
||||
// removes all children and grandchlidren of datapoint
|
||||
func removeChildren(dp *Datapoint) (sets []json_dataModels.Set) {
|
||||
for name, d := range dp.Datapoints {
|
||||
sets = append(sets, removeChildren(d)...)
|
||||
d.Publish(OnDelete)
|
||||
sets = append(sets, json_dataModels.Set{
|
||||
Uuid: d.Uuid,
|
||||
Path: d.Path,
|
||||
})
|
||||
delete(d.Datapoints, name)
|
||||
}
|
||||
return sets
|
||||
}
|
||||
|
||||
func (d *Datapoint) GetAllDatapoints(depth uint) (dps Datapoints) {
|
||||
|
||||
var dfs func(dp *Datapoint, currentDepth uint)
|
||||
dfs = func(dp *Datapoint, currentDepth uint) {
|
||||
if depth == 1 {
|
||||
return
|
||||
} else if depth == 0 {
|
||||
switch depth {
|
||||
case 0:
|
||||
// Return all descendants
|
||||
for _, child := range dp.Datapoints {
|
||||
dps = append(dps, child)
|
||||
dfs(child, currentDepth+1)
|
||||
}
|
||||
return
|
||||
case 1:
|
||||
return
|
||||
}
|
||||
|
||||
if currentDepth == depth-1 {
|
||||
@@ -379,22 +398,26 @@ func (d *Datapoint) RemoveSubscribtion(client *wsModels.Client) {
|
||||
}
|
||||
|
||||
func (d *Datapoint) Publish(eventType string) error {
|
||||
for client := range d.Subscriptions {
|
||||
r := json_data.NewResponse()
|
||||
r.AddPublish(json_dataModels.Publish{
|
||||
Event: eventType,
|
||||
Uuid: d.Uuid,
|
||||
Path: d.Path,
|
||||
Type: d.Type,
|
||||
Value: d.Value,
|
||||
})
|
||||
r := json_data.NewResponse()
|
||||
r.AddPublish(json_dataModels.Publish{
|
||||
Event: eventType,
|
||||
Uuid: d.Uuid,
|
||||
Path: d.Path,
|
||||
Type: d.Type,
|
||||
Value: d.Value,
|
||||
})
|
||||
|
||||
b, err := json.Marshal(r)
|
||||
if err != nil {
|
||||
return err
|
||||
b, err := json.Marshal(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
switch eventType {
|
||||
case OnCreate, OnDelete:
|
||||
ws.SendBroadcast(b)
|
||||
default:
|
||||
for client := range d.Subscriptions {
|
||||
client.SendResponse(b)
|
||||
}
|
||||
|
||||
client.SendResponse(b)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user