diff --git a/opcua/api/transport.go b/opcua/api/transport.go index cf63560dfef..b116f3c96cc 100644 --- a/opcua/api/transport.go +++ b/opcua/api/transport.go @@ -24,6 +24,9 @@ const ( defOffset = 0 defLimit = 10 + + defNamespace = "ns=0" // Standard root namespace + defIdentifier = "i=84" // Standard root identifier ) var ( @@ -68,6 +71,11 @@ func decodeBrowse(_ context.Context, r *http.Request) (interface{}, error) { return nil, err } + if n == "" || i == "" { + n = defNamespace + i = defIdentifier + } + req := browseReq{ ServerURI: s, Namespace: n, diff --git a/opcua/gopcua/browser.go b/opcua/gopcua/browser.go index 8e187d7e386..b6209409401 100644 --- a/opcua/gopcua/browser.go +++ b/opcua/gopcua/browser.go @@ -14,7 +14,7 @@ import ( "github.com/mainflux/mainflux/opcua" ) -const maxChildrens = 7 +const maxChildrens = 5 // max browsing node children level // NodeDef represents the node browser responnse type NodeDef struct { @@ -180,31 +180,42 @@ func browse(n *opcuaGopcua.Node, path string, level int) ([]NodeDef, error) { nodes = append(nodes, def) } - browseChildren := func(refType uint32) error { - refs, err := n.ReferencedNodes(refType, uaGopcua.BrowseDirectionForward, uaGopcua.NodeClassAll, true) - if err != nil { - return err - } - - for _, rn := range refs { - children, err := browse(rn, def.Path, level+1) - if err != nil { - return err - } - nodes = append(nodes, children...) - } - return nil + bc, err := browseChildren(n, def.Path, level, id.HasComponent) + if err != nil { + return nil, err } + nodes = append(nodes, bc...) - if err := browseChildren(id.HasComponent); err != nil { + bc, err = browseChildren(n, def.Path, level, id.Organizes) + if err != nil { return nil, err } - if err := browseChildren(id.Organizes); err != nil { + nodes = append(nodes, bc...) + + bc, err = browseChildren(n, def.Path, level, id.HasProperty) + if err != nil { return nil, err } - if err := browseChildren(id.HasProperty); err != nil { - return nil, err + nodes = append(nodes, bc...) + + return nodes, nil +} + +func browseChildren(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) + if err != nil { + return []NodeDef{}, err + } + nodes = append(nodes, children...) + } + return nodes, nil } diff --git a/opcua/gopcua/subscribe.go b/opcua/gopcua/subscribe.go index f3c18ce4d77..19058162b74 100644 --- a/opcua/gopcua/subscribe.go +++ b/opcua/gopcua/subscribe.go @@ -167,20 +167,22 @@ func (c client) runHandler(sub *opcuaGopcua.Subscription, uri, node string) erro case uaGopcua.TypeIDBoolean: msg.DataKey = "vb" msg.Data = item.Value.Value.Bool() - case uaGopcua.TypeIDString: + case uaGopcua.TypeIDString, uaGopcua.TypeIDByteString: msg.DataKey = "vs" msg.Data = item.Value.Value.String() + case uaGopcua.TypeIDDataValue: + msg.DataKey = "vd" + msg.Data = item.Value.Value.String() case uaGopcua.TypeIDInt64, uaGopcua.TypeIDInt32, uaGopcua.TypeIDInt16: msg.Data = float64(item.Value.Value.Int()) - case uaGopcua.TypeIDUint64: + case uaGopcua.TypeIDUint64, uaGopcua.TypeIDUint32, uaGopcua.TypeIDUint16: msg.Data = float64(item.Value.Value.Uint()) case uaGopcua.TypeIDFloat, uaGopcua.TypeIDDouble: msg.Data = item.Value.Value.Float() case uaGopcua.TypeIDByte: - msg.DataKey = "vs" - msg.Data = string(item.Value.Value.EncodingMask()) + msg.Data = float64(item.Value.Value.Uint()) case uaGopcua.TypeIDDateTime: - msg.Data = item.Value.Value.Time() + msg.Data = item.Value.Value.Time().Unix() default: msg.Data = 0 } diff --git a/opcua/service.go b/opcua/service.go index e72eb03920c..21e48c5a2f7 100644 --- a/opcua/service.go +++ b/opcua/service.go @@ -12,7 +12,6 @@ import ( ) const protocol = "opcua" -const defNodeID = "ns=0;i=84" var ( // ErrMalformedEntity indicates malformed entity specification. @@ -140,11 +139,7 @@ func (as *adapterService) ConnectThing(mfxChanID, mfxThingID string) error { } func (as *adapterService) Browse(serverURI, namespace, identifier string) ([]BrowsedNode, error) { - nodeID := defNodeID - - if namespace != "" && identifier != "" { - nodeID = fmt.Sprintf("%s;%s", namespace, identifier) - } + nodeID := fmt.Sprintf("%s;%s", namespace, identifier) nodes, err := as.browser.Browse(serverURI, nodeID) if err != nil {