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

How to get list of all published nodes? #863

Closed
HubertLaczak opened this issue Jul 10, 2019 · 9 comments
Closed

How to get list of all published nodes? #863

HubertLaczak opened this issue Jul 10, 2019 · 9 comments

Comments

@HubertLaczak
Copy link

@HubertLaczak HubertLaczak commented Jul 10, 2019

Hi, I'm writing Client app and I don't want to hardcode methods to get specific node with ids. I would like to ask server which nodes (variables) are published, I want to get list of each nodes.

How to make it? I was reading example from
https://github.com/FreeOpcUa/python-opcua/blob/master/examples/client-example.py
but I am not sure which methods are necessary. Can you give me tips?

@oroulet
Copy link
Member

@oroulet oroulet commented Jul 10, 2019

Every server publishes 1000s of nodes. You do not want to access all of them. The protocol is based on knowing what nodes you want. Ideally using domain specific profiles

@brubbel
Copy link
Contributor

@brubbel brubbel commented Jul 11, 2019

I would just browse the tree upwards starting from the root node.

@zerox1212
Copy link
Contributor

@zerox1212 zerox1212 commented Jul 11, 2019

You would need to browse the tree recursively to achieve this.

@HubertLaczak
Copy link
Author

@HubertLaczak HubertLaczak commented Jul 12, 2019

When I try with:
client.connect()
print ("\nConnected\n")
root = client.get_root_node()
print("%s" %root)
print("Children of root are: %s" %root.get_children())

The answer is:
Node(TwoByteNodeId(i=84))
Children of root are: [Node(TwoByteNodeId(i=87)), Node(TwoByteNodeId(i=85)), Node(TwoByteNodeId(i=86))]

Am I right, that ids of my nodes are: 87,85,86? At least I use:
var = client.get_node(ua.NodeId(85,2))
val = var.get_value()

but there is an error: "The node id refers to a node that does not exist in the server address.."

Could you give me an advise or some example? I read:

https://github.com/FreeOpcUa/python-opcua/blob/master/examples/client-example.py

@brubbel
Copy link
Contributor

@brubbel brubbel commented Jul 12, 2019

root = client.get_root_node()
print("Root node is: {:s} ".format(str(root)))

def browse_recursive(node):
    for childId in node.get_children():
        ch = client.get_node(childId)
        print(ch.get_node_class())
        if ch.get_node_class() == ua.NodeClass.Object:
            browse_recursive(ch)
        elif ch.get_node_class() == ua.NodeClass.Variable:
            try:
                print("{bn} has value {val}".format(
                    bn=ch.get_browse_name(),
                    val=str(ch.get_value()))
                )
            except ua.uaerrors._auto.BadWaitingForInitialData:
                pass

browse_recursive(root)

@HubertLaczak
Copy link
Author

@HubertLaczak HubertLaczak commented Jul 12, 2019

I still have problems. I receive:

 Root node is Node(TwoByteNodeId(i=84))
 NodeClass.Object
 3
 NodeClass.Object
 3
 NodeClass.Object
 3

I copied your code:

 try:
    root = client.get_root_node()
    print("Root node is {:s} " .format(str(root)))

    def browse_recursive(node):
    for childId in node.get_children():
           ch = client.get_node(childId)
           print(ch.get_node_class())
           if ch.get_node_class == ua.NodeClass.Object:
                   print("1")
                   browse_recursive(ch)
           elif ch.get_node_class() == ua.NodeClass.Variable:
                     print("2")
                     try:
                               print("{bn} has value {val}".format(bn=$
                     except ua.uaerror._auto.BadWaitingForInitialDat$
                               pass
            else:
                      print("3")
    browse_recursive(root)

It looks like NodeClass.Object != ua.NodeClass.Object?
Normally, I use:

    var = client.get_node("ns=2;s=Register1")

to get node and later to get the value. But when I add in the code above:

 for childId in node.get_children():
           ch = client.get_node(childId)
           print(ch.get_node_class())
           print(ch.get_browse_name()) #I added this line

I receive:

 NodeClass.Object
 QualifiedName(0:Views)
 3
 NodeClass.Object
 QualifiedName(0:Objects)
 3
 NodeClass.Object
 QualifiedName(0:Types)
 3

@brubbel
Copy link
Contributor

@brubbel brubbel commented Jul 12, 2019

ch.get_node_class**()** == ua.NodeClass.Object
There is a typo in your code.

@HubertLaczak
Copy link
Author

@HubertLaczak HubertLaczak commented Jul 17, 2019

Great work, thank you :)

@twodonkeys
Copy link

@twodonkeys twodonkeys commented Jun 2, 2020

mark

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

Successfully merging a pull request may close this issue.

None yet
5 participants