Skip to content

Commit

Permalink
NOISSUE - Fix opc-ua message type handling (#1071)
Browse files Browse the repository at this point in the history
* NOISSUE - Fix opc-ua message type handling

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

* Add comments

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

* Fix reviews

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

* Fix typo

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

* Return error

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
  • Loading branch information
manuio committed Mar 12, 2020
1 parent a3481e7 commit 0899846
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 30 deletions.
8 changes: 8 additions & 0 deletions opcua/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ const (

defOffset = 0
defLimit = 10

defNamespace = "ns=0" // Standard root namespace
defIdentifier = "i=84" // Standard root identifier
)

var (
Expand Down Expand Up @@ -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,
Expand Down
49 changes: 30 additions & 19 deletions opcua/gopcua/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
12 changes: 7 additions & 5 deletions opcua/gopcua/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 1 addition & 6 deletions opcua/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
)

const protocol = "opcua"
const defNodeID = "ns=0;i=84"

var (
// ErrMalformedEntity indicates malformed entity specification.
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 0899846

Please sign in to comment.