improve websocket ping and remodel for rename datapoint
This commit is contained in:
@@ -23,16 +23,17 @@ const (
|
||||
)
|
||||
|
||||
type Datapoint struct {
|
||||
Datapoints map[string]*Datapoint `json:"-"`
|
||||
Uuid uuid.UUID `json:"uuid"`
|
||||
Path string `json:"path"`
|
||||
Value any `json:"value,omitempty"`
|
||||
Path string `json:"path"`
|
||||
CreateDateTime int64 `json:"createDateTime,omitempty"`
|
||||
UpdateDateTime int64 `json:"updateDateTime,omitempty"`
|
||||
Type json_dataModels.Type `json:"type"`
|
||||
ReadWrite json_dataModels.Rights `json:"readWrite"`
|
||||
Datapoints map[string]*Datapoint `json:"-"`
|
||||
Drivers json_dataModels.Drivers `json:"drivers,omitempty"`
|
||||
Subscriptions Subscriptions `json:"-"`
|
||||
Type json_dataModels.Type `json:"type"`
|
||||
ReadWrite json_dataModels.Rights `json:"readWrite"`
|
||||
HasChild bool `json:"-"`
|
||||
}
|
||||
|
||||
func (d *Datapoint) Set(path string, set json_dataModels.Set) (bool, error) {
|
||||
@@ -89,14 +90,15 @@ func (d *Datapoint) GetValueUint64() uint64 {
|
||||
return utils.Uint64From(d.Value)
|
||||
}
|
||||
|
||||
func (d *Datapoint) CreateDatapoints(sets ...json_dataModels.Set) (created []json_dataModels.Set, uuids Uuids, err error) {
|
||||
func (d *Datapoint) CreateDatapoints(uuids *Uuids, sets ...json_dataModels.Set) (created []json_dataModels.Set, err error) {
|
||||
if len(sets) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
uuids = make(Uuids, 1)
|
||||
publishes := []json_dataModels.Publish{}
|
||||
|
||||
for _, dp := range sets {
|
||||
|
||||
parts := strings.Split(dp.Path, ":")
|
||||
|
||||
current := d
|
||||
@@ -104,52 +106,66 @@ func (d *Datapoint) CreateDatapoints(sets ...json_dataModels.Set) (created []jso
|
||||
if current.Datapoints == nil {
|
||||
current.Datapoints = make(map[string]*Datapoint)
|
||||
}
|
||||
|
||||
if i == len(parts)-1 {
|
||||
// Leaf node: create or update datapoint
|
||||
if existing, ok := current.Datapoints[part]; ok {
|
||||
|
||||
_, err := existing.Set("", dp)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, err
|
||||
}
|
||||
created = append(created, json_dataModels.Set{
|
||||
Uuid: existing.Uuid,
|
||||
Path: existing.Path,
|
||||
Type: existing.Type,
|
||||
Value: existing.Value,
|
||||
Rights: existing.ReadWrite,
|
||||
Drivers: &existing.Drivers,
|
||||
Updated: true,
|
||||
Uuid: existing.Uuid,
|
||||
Path: existing.Path,
|
||||
Type: existing.Type,
|
||||
Value: existing.Value,
|
||||
Rights: existing.ReadWrite,
|
||||
Drivers: &existing.Drivers,
|
||||
Updated: true,
|
||||
HasChild: existing.HasChild,
|
||||
})
|
||||
|
||||
existing.Publish(OnChange)
|
||||
|
||||
} else {
|
||||
ndp := Datapoint{
|
||||
Uuid: uuid.New(),
|
||||
var uid uuid.UUID = uuid.New()
|
||||
if dp.Uuid != uuid.Nil {
|
||||
uid = dp.Uuid
|
||||
}
|
||||
ndp := &Datapoint{
|
||||
Uuid: uid,
|
||||
CreateDateTime: time.Now().UnixMilli(),
|
||||
Subscriptions: InitSubscribtion(),
|
||||
}
|
||||
|
||||
// Create new
|
||||
current.Datapoints[part] = &ndp
|
||||
current.Datapoints[part] = ndp
|
||||
|
||||
_, err := ndp.Set(strings.Join(parts, ":"), dp)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, err
|
||||
}
|
||||
created = append(created, json_dataModels.Set{
|
||||
Uuid: ndp.Uuid,
|
||||
Path: ndp.Path,
|
||||
Type: ndp.Type,
|
||||
Value: ndp.Value,
|
||||
Rights: ndp.ReadWrite,
|
||||
Driver: dp.Driver,
|
||||
})
|
||||
ndp.Publish(OnCreate)
|
||||
|
||||
//add uuid to flat map for faster lookup
|
||||
uuids[ndp.Uuid] = &ndp
|
||||
uuids.AddDatapoint(current, ndp)
|
||||
|
||||
created = append(created, json_dataModels.Set{
|
||||
Uuid: ndp.Uuid,
|
||||
Path: ndp.Path,
|
||||
Type: ndp.Type,
|
||||
Value: ndp.Value,
|
||||
Rights: ndp.ReadWrite,
|
||||
Driver: dp.Driver,
|
||||
HasChild: ndp.HasChild,
|
||||
})
|
||||
|
||||
publishes = append(publishes, json_dataModels.Publish{
|
||||
Event: OnCreate,
|
||||
Uuid: ndp.Uuid,
|
||||
Path: ndp.Path,
|
||||
Type: ndp.Type,
|
||||
Value: ndp.Value,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,36 +182,51 @@ func (d *Datapoint) CreateDatapoints(sets ...json_dataModels.Set) (created []jso
|
||||
Subscriptions: InitSubscribtion(),
|
||||
}
|
||||
|
||||
//add uuid to flat map for faster lookup
|
||||
uuids.AddDatapoint(current, newDp)
|
||||
|
||||
created = append(created, json_dataModels.Set{
|
||||
Uuid: newDp.Uuid,
|
||||
Path: newDp.Path,
|
||||
Type: newDp.Type,
|
||||
Value: newDp.Value,
|
||||
Rights: newDp.ReadWrite,
|
||||
Uuid: newDp.Uuid,
|
||||
Path: newDp.Path,
|
||||
Type: newDp.Type,
|
||||
Value: newDp.Value,
|
||||
Rights: newDp.ReadWrite,
|
||||
HasChild: newDp.HasChild,
|
||||
})
|
||||
|
||||
if dp.Rights != "" {
|
||||
newDp.ReadWrite = dp.Rights.GetRights()
|
||||
}
|
||||
|
||||
newDp.Publish(OnCreate)
|
||||
publishes = append(publishes, json_dataModels.Publish{
|
||||
Event: OnCreate,
|
||||
Uuid: newDp.Uuid,
|
||||
Path: newDp.Path,
|
||||
Type: newDp.Type,
|
||||
Value: newDp.Value,
|
||||
})
|
||||
|
||||
current.Datapoints[part] = newDp
|
||||
current = newDp
|
||||
|
||||
//add uuid to flat map for faster lookuo
|
||||
uuids[newDp.Uuid] = newDp
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
r := json_data.NewResponse()
|
||||
r.Publish = append(r.Publish, publishes...)
|
||||
|
||||
b, err := json.Marshal(r)
|
||||
if err != nil {
|
||||
return created, err
|
||||
}
|
||||
ws.SendBroadcast(b)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *Datapoint) ImportDatapoint(dp *Datapoint, path string) (uuids Uuids, err error) {
|
||||
func (d *Datapoint) ImportDatapoint(uuids *Uuids, dp *Datapoint, path string) (err error) {
|
||||
parts := strings.Split(dp.Path, ":")
|
||||
|
||||
uuids = make(Uuids, 1)
|
||||
|
||||
current := d
|
||||
for i, part := range parts {
|
||||
if current.Datapoints == nil {
|
||||
@@ -217,11 +248,12 @@ func (d *Datapoint) ImportDatapoint(dp *Datapoint, path string) (uuids Uuids, er
|
||||
dp.Subscriptions = InitSubscribtion()
|
||||
current.Datapoints[part] = dp
|
||||
//add uuid to flat map for faster lookup
|
||||
uuids[dp.Uuid] = dp
|
||||
uuids.AddDatapoint(current, dp)
|
||||
|
||||
dp.Publish(OnChange)
|
||||
}
|
||||
|
||||
return uuids, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
// Traverse or create intermediate nodes
|
||||
@@ -238,10 +270,10 @@ func (d *Datapoint) ImportDatapoint(dp *Datapoint, path string) (uuids Uuids, er
|
||||
current.Datapoints[part] = newDp
|
||||
current = newDp
|
||||
//add uuid to flat map for faster lookup
|
||||
uuids[newDp.Uuid] = newDp
|
||||
uuids.AddDatapoint(current, newDp)
|
||||
}
|
||||
}
|
||||
return uuids, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Datapoint) UpdateDatapointValue(value any, path string) error {
|
||||
@@ -265,20 +297,22 @@ func (d *Datapoint) UpdateDatapointValue(value any, path string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Datapoint) UpdateValue(conns *ws.ClientHandler, value any) error {
|
||||
func (d *Datapoint) UpdateValue(value any) error {
|
||||
d.Value = d.Type.ConvertValue(value)
|
||||
d.UpdateDateTime = time.Now().UnixMilli()
|
||||
d.Publish(OnChange)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Datapoint) RemoveDatapoint(conns *ws.ClientHandler, set json_dataModels.Set) (sets []json_dataModels.Set, err error) {
|
||||
func (d *Datapoint) RemoveDatapoint(set json_dataModels.Set) (sets []json_dataModels.Set, err error) {
|
||||
parts := strings.Split(set.Path, ":")
|
||||
|
||||
if len(parts) < 1 {
|
||||
return sets, fmt.Errorf("invalid path: '%s'", set.Path)
|
||||
}
|
||||
|
||||
publishes := []json_dataModels.Publish{}
|
||||
|
||||
current := d
|
||||
for i := 0; i < len(parts)-1; i++ {
|
||||
next, ok := current.Datapoints[parts[i]]
|
||||
@@ -290,49 +324,64 @@ func (d *Datapoint) RemoveDatapoint(conns *ws.ClientHandler, set json_dataModels
|
||||
|
||||
toDelete := parts[len(parts)-1]
|
||||
if dp, ok := current.Datapoints[toDelete]; ok {
|
||||
sets = append(sets, removeChildren(dp)...)
|
||||
dp.Publish(OnDelete)
|
||||
s, p := removeChildren(dp)
|
||||
sets = append(sets, s...)
|
||||
publishes = append(publishes, p...)
|
||||
publishes = append(publishes, json_dataModels.Publish{
|
||||
Event: OnDelete,
|
||||
Uuid: dp.Uuid,
|
||||
Path: dp.Path,
|
||||
})
|
||||
sets = append(sets, json_dataModels.Set{
|
||||
Uuid: dp.Uuid,
|
||||
Path: dp.Path,
|
||||
})
|
||||
delete(current.Datapoints, toDelete)
|
||||
|
||||
r := json_data.NewResponse()
|
||||
r.Publish = append(r.Publish, publishes...)
|
||||
|
||||
b, err := json.Marshal(r)
|
||||
if err != nil {
|
||||
return sets, err
|
||||
}
|
||||
ws.SendBroadcast(b)
|
||||
|
||||
return sets, nil
|
||||
}
|
||||
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) {
|
||||
func removeChildren(dp *Datapoint) (sets []json_dataModels.Set, pubs []json_dataModels.Publish) {
|
||||
for name, d := range dp.Datapoints {
|
||||
sets = append(sets, removeChildren(d)...)
|
||||
d.Publish(OnDelete)
|
||||
s, p := removeChildren(d)
|
||||
sets = append(sets, s...)
|
||||
pubs = append(pubs, p...)
|
||||
|
||||
sets = append(sets, json_dataModels.Set{
|
||||
Uuid: d.Uuid,
|
||||
Path: d.Path,
|
||||
})
|
||||
delete(d.Datapoints, name)
|
||||
}
|
||||
return sets
|
||||
return sets, pubs
|
||||
}
|
||||
|
||||
func (d *Datapoint) GetAllDatapoints(depth uint) (dps Datapoints) {
|
||||
|
||||
dps = append(dps, d)
|
||||
if depth == 1 {
|
||||
return
|
||||
} else if depth > 0 {
|
||||
depth--
|
||||
}
|
||||
var dfs func(dp *Datapoint, currentDepth uint)
|
||||
dfs = func(dp *Datapoint, currentDepth uint) {
|
||||
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 {
|
||||
case currentDepth:
|
||||
return
|
||||
}
|
||||
|
||||
@@ -341,7 +390,7 @@ func (d *Datapoint) GetAllDatapoints(depth uint) (dps Datapoints) {
|
||||
dfs(child, currentDepth+1)
|
||||
}
|
||||
}
|
||||
dps = append(dps, d)
|
||||
|
||||
dfs(d, 0)
|
||||
dps.SortSlice()
|
||||
return
|
||||
@@ -393,6 +442,35 @@ func (d *Datapoint) AddSubscribtion(conn *wsModels.Client, sub json_dataModels.S
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Datapoint) RenamePaths(oldPath string) {
|
||||
visited := make(map[*Datapoint]bool)
|
||||
|
||||
if len(d.Datapoints) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
for _, dp := range d.Datapoints {
|
||||
dp.Path = strings.Replace(dp.Path, oldPath, d.Path, 1)
|
||||
dp.renameSubPaths(oldPath, d.Path, visited)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Datapoint) renameSubPaths(oldPath, newPath string, visited map[*Datapoint]bool) {
|
||||
if visited[d] {
|
||||
return
|
||||
}
|
||||
visited[d] = true
|
||||
|
||||
if len(d.Datapoints) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
for _, dp := range d.Datapoints {
|
||||
dp.Path = strings.Replace(dp.Path, oldPath, newPath, 1)
|
||||
dp.renameSubPaths(oldPath, newPath, visited)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Datapoint) RemoveSubscribtion(client *wsModels.Client) {
|
||||
delete(d.Subscriptions, client)
|
||||
}
|
||||
|
Reference in New Issue
Block a user