NOISSUE - Fix StatusBadDecodingError for opc-ua browse (#1074)

* NOISSUE - Fix StatusBadDecodingError for opc-ua browse

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Reduce number of browse childrens level

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
This commit is contained in:
Manuel Imperiale 2020-03-14 20:09:02 +01:00 committed by GitHub
parent 3e4a9eb16a
commit c91fe0d453
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 14 deletions

View File

@ -14,7 +14,7 @@ import (
"github.com/mainflux/mainflux/opcua"
)
const maxChildrens = 5 // max browsing node children level
const maxChildrens = 4 // max browsing node children level
// NodeDef represents the node browser responnse
type NodeDef struct {
@ -58,12 +58,7 @@ func (c browser) Browse(serverURI, nodeID string) ([]opcua.BrowsedNode, error) {
}
defer oc.Close()
n, err := uaGopcua.ParseNodeID(nodeID)
if err != nil {
return nil, errors.Wrap(errFailedParseNodeID, err)
}
nodeList, err := browse(oc.Node(n), "", 0)
nodeList, err := browse(oc, nodeID, "", 0)
if err != nil {
return nil, err
}
@ -84,11 +79,17 @@ func (c browser) Browse(serverURI, nodeID string) ([]opcua.BrowsedNode, error) {
return nodes, nil
}
func browse(n *opcuaGopcua.Node, path string, level int) ([]NodeDef, error) {
func browse(oc *opcuaGopcua.Client, nodeID, path string, level int) ([]NodeDef, error) {
if level > maxChildrens {
return nil, nil
}
nid, err := uaGopcua.ParseNodeID(nodeID)
if err != nil {
return []NodeDef{}, err
}
n := oc.Node(nid)
attrs, err := n.Attributes(
uaGopcua.AttributeIDNodeClass,
uaGopcua.AttributeIDBrowseName,
@ -180,19 +181,19 @@ func browse(n *opcuaGopcua.Node, path string, level int) ([]NodeDef, error) {
nodes = append(nodes, def)
}
bc, err := browseChildren(n, def.Path, level, id.HasComponent)
bc, err := browseChildren(oc, n, def.Path, level, id.HasComponent)
if err != nil {
return nil, err
}
nodes = append(nodes, bc...)
bc, err = browseChildren(n, def.Path, level, id.Organizes)
bc, err = browseChildren(oc, n, def.Path, level, id.Organizes)
if err != nil {
return nil, err
}
nodes = append(nodes, bc...)
bc, err = browseChildren(n, def.Path, level, id.HasProperty)
bc, err = browseChildren(oc, n, def.Path, level, id.HasProperty)
if err != nil {
return nil, err
}
@ -201,15 +202,15 @@ func browse(n *opcuaGopcua.Node, path string, level int) ([]NodeDef, error) {
return nodes, nil
}
func browseChildren(n *opcuaGopcua.Node, path string, level int, typeDef uint32) ([]NodeDef, error) {
func browseChildren(c *opcuaGopcua.Client, n *opcuaGopcua.Node, path string, level int, typeDef uint32) ([]NodeDef, error) {
nodes := []NodeDef{}
refs, err := n.ReferencedNodes(typeDef, uaGopcua.BrowseDirectionForward, uaGopcua.NodeClassAll, true)
if err != nil {
return []NodeDef{}, err
}
for _, rn := range refs {
children, err := browse(rn, path, level+1)
for _, ref := range refs {
children, err := browse(c, ref.ID.String(), path, level+1)
if err != nil {
return []NodeDef{}, err
}