Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENG-380: Use Extended-NodeID strings #109

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft

ENG-380: Use Extended-NodeID strings #109

wants to merge 1 commit into from

Conversation

led0nk
Copy link
Member

@led0nk led0nk commented Jan 19, 2025

I will need some advice here since this seems to work just fine by:

  • connecting
  • reading namespaceArray of node i=2255
  • converting the namespaceArray-result to a usable []string
  • providing this to ua.ParseExpandedNodeID since this checks for the namespace-uri (If provided) and returning the related index to get a format like e.g. <ns=4; s= i=1>

benthos-expandednodeID
(I removed the Printf's for this draft)

What is still quite poor (imo):

  • context-handling

  • maybe at least some Info-logging to see if namespace was proven correctly here?

  • should be only usable through feature-flag (currently)

  • creating a new connection since it could be more intentional to move this to here:

    func (g *OPCUAInput) GetOPCUAServerInformation(ctx context.Context) (ServerInfo, error) {
    if g.Client == nil {
    return ServerInfo{}, errors.New("client is nil")
    }
    // Fetch ManufacturerName node from i=2263
    manufacturerNameNodeID := ua.NewNumericNodeID(0, 2263)
    productNameNodeID := ua.NewNumericNodeID(0, 2261)
    softwareVersionNodeID := ua.NewNumericNodeID(0, 2264)
    nodeChan := make(chan NodeDef, 3)
    errChan := make(chan error, 3)
    // opcuaBrowserChan is declared to satisfy the browse function signature.
    // The data inside opcuaBrowserChan is not used for this function.
    // It is more useful for the GetNodeTree function.
    opcuaBrowserChan := make(chan NodeDef, 3)
    var wg TrackedWaitGroup
    wg.Add(3)
    go browse(ctx, NewOpcuaNodeWrapper(g.Client.Node(manufacturerNameNodeID)), "", 0, g.Log, manufacturerNameNodeID.String(), nodeChan, errChan, &wg, g.BrowseHierarchicalReferences, opcuaBrowserChan)
    go browse(ctx, NewOpcuaNodeWrapper(g.Client.Node(productNameNodeID)), "", 0, g.Log, productNameNodeID.String(), nodeChan, errChan, &wg, g.BrowseHierarchicalReferences, opcuaBrowserChan)
    go browse(ctx, NewOpcuaNodeWrapper(g.Client.Node(softwareVersionNodeID)), "", 0, g.Log, softwareVersionNodeID.String(), nodeChan, errChan, &wg, g.BrowseHierarchicalReferences, opcuaBrowserChan)
    wg.Wait()
    close(nodeChan)
    close(errChan)
    close(opcuaBrowserChan)
    if len(errChan) > 0 {
    return ServerInfo{}, <-errChan
    }
    var nodeList []NodeDef
    for node := range nodeChan {
    nodeList = append(nodeList, node)
    }
    if len(nodeList) != 3 {
    g.Log.Warn("Could not find OPC UA Server Information")
    return ServerInfo{}, errors.New("could not find OPC UA Server Information")
    }
    var nodesToRead []*ua.ReadValueID
    for _, node := range nodeList {
    nodesToRead = append(nodesToRead, &ua.ReadValueID{
    NodeID: node.NodeID,
    })
    }
    req := &ua.ReadRequest{
    MaxAge: 2000,
    NodesToRead: nodesToRead,
    TimestampsToReturn: ua.TimestampsToReturnBoth,
    }
    resp, err := g.Read(ctx, req)
    if err != nil {
    g.Log.Errorf("Read failed: %s", err)
    return ServerInfo{}, err
    }
    if len(resp.Results) != 3 {
    g.Log.Errorf("Expected 3 results, got %d", len(resp.Results))
    return ServerInfo{}, errors.New("expected 3 results")
    }
    serverInfo := ServerInfo{}
    for i, node := range nodeList {
    value := resp.Results[i]
    if value == nil || value.Value == nil {
    g.Log.Debugf("Received nil in item structure for OPC UA Server Information")
    }
    message := g.createMessageFromValue(value, node)
    if message != nil {
    messageBytes, err := message.AsBytes()
    if err != nil {
    return ServerInfo{}, err
    }
    if node.NodeID.IntID() == 2263 {
    serverInfo.ManufacturerName = string(messageBytes)
    } else if node.NodeID.IntID() == 2261 {
    serverInfo.ProductName = string(messageBytes)
    } else if node.NodeID.IntID() == 2264 {
    serverInfo.SoftwareVersion = string(messageBytes)
    }
    }
    }
    return serverInfo, nil
    }

  • resolves Using Expanded-NodeId-Strings rather then regular NodeId-String in configuration #5
    (when finished)

restore server_info
Copy link
Contributor

coderabbitai bot commented Jan 19, 2025

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Comment on lines +88 to +93
client, err := opcua.NewClient(endpoint, opcua.AuthAnonymous())
if err != nil {
return nil, err
}

err = client.Connect(ctx)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(major) You should be reusing the Client that is initialized from the connect method.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion , reading the namespace array can be done at the place where we gather Server info while trying to connect.

}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(nit) Formatting seems bit off here.

return nil, err
}

namespaceArray, ok := resp.Results[0].Value.Value().([]string)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(style) This seems like an Hungarian notation in variable names. Please use namespaces instead of namespaceArray

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using Expanded-NodeId-Strings rather then regular NodeId-String in configuration
2 participants