Skip to content

Commit

Permalink
first draft
Browse files Browse the repository at this point in the history
restore server_info
  • Loading branch information
led0nk committed Jan 19, 2025
1 parent aa2177b commit 1e9465a
Showing 1 changed file with 73 additions and 10 deletions.
83 changes: 73 additions & 10 deletions opcua_plugin/opcua.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package opcua_plugin

import (
"context"
"fmt"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -67,6 +68,56 @@ func ParseNodeIDs(incomingNodes []string) []*ua.NodeID {
return parsedNodeIDs
}

func ParseExpandedNodeIDs(incomingNodes []string, namespaceArray []string) ([]*ua.ExpandedNodeID, error) {
var parsedExpandedNodeIDs []*ua.ExpandedNodeID

for _, incomingNodeID := range incomingNodes {
parsedExpandedNodeID, err := ua.ParseExpandedNodeID(incomingNodeID, namespaceArray)
if err != nil {
return nil, err
}
parsedExpandedNodeIDs = append(parsedExpandedNodeIDs, parsedExpandedNodeID)
}
return parsedExpandedNodeIDs, nil
}

func ReadNamespaceArray(endpoint string) ([]string, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer cancel()

client, err := opcua.NewClient(endpoint, opcua.AuthAnonymous())
if err != nil {
return nil, err
}

err = client.Connect(ctx)
if err != nil {
return nil, err
}
defer client.Close(ctx)

req := &ua.ReadRequest{
NodesToRead: []*ua.ReadValueID{
{
NodeID: ua.NewNumericNodeID(0, 2255),
AttributeID: ua.AttributeIDValue,
},
},
}

resp, err := client.Read(ctx, req)
if err != nil {
return nil, err
}

namespaceArray, ok := resp.Results[0].Value.Value().([]string)
if !ok {
return nil, fmt.Errorf("failed to parse namespaceArray as []string")
}

return namespaceArray, nil
}

func newOPCUAInput(conf *service.ParsedConfig, mgr *service.Resources) (service.BatchInput, error) {
endpoint, err := conf.FieldString("endpoint")
if err != nil {
Expand Down Expand Up @@ -148,13 +199,24 @@ func newOPCUAInput(conf *service.ParsedConfig, mgr *service.Resources) (service.
return nil, errors.New("no nodeIDs provided")
}

namespaceArray, err := ReadNamespaceArray(endpoint)
if err != nil {
return nil, err
}

parsedExpandedNodeIDs, err := ParseExpandedNodeIDs(nodeIDs, namespaceArray)
if err != nil {
return nil, err
}

parsedNodeIDs := ParseNodeIDs(nodeIDs)

m := &OPCUAInput{
Endpoint: endpoint,
Username: username,
Password: password,
NodeIDs: parsedNodeIDs,
ExpandedNodeIDs: parsedExpandedNodeIDs,
Log: mgr.Logger(),
SecurityMode: securityMode,
SecurityPolicy: securityPolicy,
Expand Down Expand Up @@ -192,16 +254,17 @@ func init() {
}

type OPCUAInput struct {
Endpoint string
Username string
Password string
NodeIDs []*ua.NodeID
NodeList []NodeDef
SecurityMode string
SecurityPolicy string
Insecure bool
Client *opcua.Client
Log *service.Logger
Endpoint string
Username string
Password string
NodeIDs []*ua.NodeID
ExpandedNodeIDs []*ua.ExpandedNodeID
NodeList []NodeDef
SecurityMode string
SecurityPolicy string
Insecure bool
Client *opcua.Client
Log *service.Logger
// this is required for subscription
SubscribeEnabled bool
SubNotifyChan chan *opcua.PublishNotificationData
Expand Down

0 comments on commit 1e9465a

Please sign in to comment.