From 7b62f1ce8a36414f6206e938d5f65e91ba41b5fa Mon Sep 17 00:00:00 2001 From: Manuel Imperiale Date: Mon, 27 Jan 2020 12:25:16 +0100 Subject: [PATCH] NOISSUE - Add details to browsed OPC-UA nodes (#1019) Signed-off-by: Manuel Imperiale --- opcua/api/endpoint.go | 5 +---- opcua/api/logging.go | 2 +- opcua/api/metrics.go | 2 +- opcua/api/responses.go | 3 ++- opcua/browser.go | 9 ++++++++- opcua/gopcua/browser.go | 11 +++++++---- opcua/service.go | 4 ++-- 7 files changed, 22 insertions(+), 14 deletions(-) diff --git a/opcua/api/endpoint.go b/opcua/api/endpoint.go index be672e8e..f6e483ea 100644 --- a/opcua/api/endpoint.go +++ b/opcua/api/endpoint.go @@ -26,10 +26,7 @@ func browseEndpoint(svc opcua.Service) endpoint.Endpoint { } res := browseRes{ - Nodes: []string{}, - } - for _, node := range nodes { - res.Nodes = append(res.Nodes, node) + Nodes: nodes, } return res, nil diff --git a/opcua/api/logging.go b/opcua/api/logging.go index 4ff626e1..0545bc35 100644 --- a/opcua/api/logging.go +++ b/opcua/api/logging.go @@ -130,7 +130,7 @@ func (lm loggingMiddleware) DisconnectThing(mfxChanID, mfxThingID string) (err e return lm.svc.DisconnectThing(mfxChanID, mfxThingID) } -func (lm loggingMiddleware) Browse(serverURI, nodeID string) (nodes []string, err error) { +func (lm loggingMiddleware) Browse(serverURI, nodeID string) (nodes []opcua.BrowsedNode, err error) { defer func(begin time.Time) { message := fmt.Sprintf("browse server URI %s and node ID %s, took %s to complete", serverURI, nodeID, time.Since(begin)) if err != nil { diff --git a/opcua/api/metrics.go b/opcua/api/metrics.go index 57886668..6cb28b54 100644 --- a/opcua/api/metrics.go +++ b/opcua/api/metrics.go @@ -99,7 +99,7 @@ func (mm *metricsMiddleware) DisconnectThing(mfxChanID, mfxThingID string) error return mm.svc.DisconnectThing(mfxChanID, mfxThingID) } -func (mm *metricsMiddleware) Browse(serverURI, nodeID string) ([]string, error) { +func (mm *metricsMiddleware) Browse(serverURI, nodeID string) ([]opcua.BrowsedNode, error) { defer func(begin time.Time) { mm.counter.With("method", "browse").Add(1) mm.latency.With("method", "browse").Observe(time.Since(begin).Seconds()) diff --git a/opcua/api/responses.go b/opcua/api/responses.go index 9e4fb1c3..7703d86d 100644 --- a/opcua/api/responses.go +++ b/opcua/api/responses.go @@ -7,12 +7,13 @@ import ( "net/http" "github.com/mainflux/mainflux" + "github.com/mainflux/mainflux/opcua" ) var _ mainflux.Response = (*browseRes)(nil) type browseRes struct { - Nodes []string `json:"nodes"` + Nodes []opcua.BrowsedNode `json:"nodes"` } func (res browseRes) Code() int { diff --git a/opcua/browser.go b/opcua/browser.go index 0594ac12..358819b6 100644 --- a/opcua/browser.go +++ b/opcua/browser.go @@ -3,8 +3,15 @@ package opcua +// BrowsedNode represents the details of a browsed OPC-UA node. +type BrowsedNode struct { + NodeID string + Type string + Description string +} + // Browser represents the OPC-UA Server Nodes browser. type Browser interface { // Browse availlable Nodes for a given URI. - Browse(string, string) ([]string, error) + Browse(string, string) ([]BrowsedNode, error) } diff --git a/opcua/gopcua/browser.go b/opcua/gopcua/browser.go index 8b3eb716..38a345a0 100644 --- a/opcua/gopcua/browser.go +++ b/opcua/gopcua/browser.go @@ -5,7 +5,6 @@ package gopcua import ( "context" - "fmt" opcuaGopcua "github.com/gopcua/opcua" "github.com/gopcua/opcua/id" @@ -45,7 +44,7 @@ func NewBrowser(ctx context.Context, log logger.Logger) opcua.Browser { } } -func (c browser) Browse(serverURI, nodeID string) ([]string, error) { +func (c browser) Browse(serverURI, nodeID string) ([]opcua.BrowsedNode, error) { opts := []opcuaGopcua.Option{ opcuaGopcua.SecurityMode(uaGopcua.MessageSecurityModeNone), } @@ -66,9 +65,13 @@ func (c browser) Browse(serverURI, nodeID string) ([]string, error) { return nil, err } - nodes := []string{} + nodes := []opcua.BrowsedNode{} for _, s := range nodeList { - node := fmt.Sprintf("ns=%d;%s", s.NodeID.Namespace(), s.NodeID.String()) + node := opcua.BrowsedNode{ + NodeID: s.NodeID.String(), + Type: s.DataType, + Description: s.Description, + } nodes = append(nodes, node) } diff --git a/opcua/service.go b/opcua/service.go index 7ac0e49f..e35d43f6 100644 --- a/opcua/service.go +++ b/opcua/service.go @@ -45,7 +45,7 @@ type Service interface { DisconnectThing(string, string) error // Browse browses available nodes for a given OPC-UA Server URI and NodeID - Browse(string, string) ([]string, error) + Browse(string, string) ([]BrowsedNode, error) } // Config OPC-UA Server @@ -131,7 +131,7 @@ func (as *adapterService) ConnectThing(mfxChanID, mfxThingID string) error { return as.connectRM.Save(c, c) } -func (as *adapterService) Browse(serverURI, nodeID string) ([]string, error) { +func (as *adapterService) Browse(serverURI, nodeID string) ([]BrowsedNode, error) { nodes, err := as.browser.Browse(serverURI, nodeID) if err != nil { return nil, err