python-opcua icon indicating copy to clipboard operation
python-opcua copied to clipboard

Why is get_data_type() method of nodeid always returning: TwoByteNodeId

Open Demolishun opened this issue 5 years ago • 5 comments

Describe the bug
Calling get_data_type() on a node always returns TwoByteNodeId NodeId sub type.

To Reproduce

#!/usr/bin/python3

#
#  Install:
#  pip3 install opcua
#

import sys, getopt

from opcua import Client
from opcua import ua
from opcua.ua.uatypes import get_default_value

serverurl = "opc.tcp://{}:4840/"
ipaddress = "127.0.0.1"

helptext = "testserver.py [-a <ipaddress> ]"

def testserver(client):
    nodeMachine = client.get_node(ua.NodeId("Objects/Machine", 1))
    print(nodeMachine)
    #print(nodeMachine.get_data_value()) # fails, no data "OSError"
    ignoretypes = [ua.NodeClass.Method, ua.NodeClass.Object]
    for child in nodeMachine.get_children():
        #print(child)
        #print()
        nodeclass = child.get_node_class()
        #print(nodeclass)
        if nodeclass in ignoretypes:
            continue
        nodeid = child.get_data_type()
        print(child, nodeclass, nodeid)


def main(argv):
    global ipaddress

    try:
        opts, args = getopt.getopt(argv,"ha:",["help=","address="])
    except getopt.GetoptError:
        print(helptext)
        sys.exit(2)
    for opt, arg in opts:
        if opt == "-h":
            print(helptext)
            sys.exit()
        elif opt in ("-a","--address"):
            ipaddress = arg

    print("Testing Server")

    url = serverurl.format(ipaddress)
    print("Connecting to {}".format(url))
    client = Client(url)

    try:        
        client.connect()
        client.load_type_definitions()

        # test code here
        testserver(client)
    
    except ConnectionRefusedError:
        print("Failed to connect, refused")

    finally:
        try:
            client.disconnect()
        except AttributeError:
            pass


if __name__ == "__main__":
    main(sys.argv[1:])

Output:

Testing Server
Connecting to opc.tcp://10.0.10.86:4840/
Requested secure channel timeout to be 3600000ms, got 600000ms instead
ns=1;s=Objects/Machine
ns=1;s=Objects/Machine/ArcCntr0 NodeClass.Variable TwoByteNodeId(i=6)
ns=1;s=Objects/Machine/CurrentFixture NodeClass.Variable TwoByteNodeId(i=12)
ns=1;s=Objects/Machine/PartId NodeClass.Variable TwoByteNodeId(i=12)
ns=1;s=Objects/Machine/LastDataFile NodeClass.Variable TwoByteNodeId(i=12)
ns=1;s=Objects/Machine/CellName NodeClass.Variable TwoByteNodeId(i=12)
ns=1;s=Objects/Machine/CurrentUser NodeClass.Variable TwoByteNodeId(i=12)
ns=1;s=Objects/Machine/DeviceType NodeClass.Variable TwoByteNodeId(i=12)
ns=1;s=Objects/Machine/DeviceVersion NodeClass.Variable TwoByteNodeId(i=12)

Expected behavior
I expected to see the nodeid return as StringNodeId types.

Version
Python-Version:3.6.9
python-opcua Version (e.g. master branch, 0.9): latest installed via pip3 install opcua as of today 2021/10/21

Demolishun avatar Oct 21 '21 22:10 Demolishun

I can't remember exactly, but I do not think data types defined by the spec are ever going to be a String. Or at least it would depend on how the UA server you are connecting to sets up the standard address space... and I would guess anyone that designs a UA server is going to use TwoByteNodeId for performance reasons.

zerox1212 avatar Oct 22 '21 02:10 zerox1212

I can't remember exactly, but I do not think data types defined by the spec are ever going to be a String. Or at least it would depend on how the UA server you are connecting to sets up the standard address space... and I would guess anyone that designs a UA server is going to use TwoByteNodeId for performance reasons.

I wrote the server and it is in fact using string node ids: "ns=1;s=Objects/Machine/ArcCntr0" this is what printing the node gives me above, but that particular function is returning the wrong nodeid type. The print out of the text shows every one of those nodes in a string nodeid node. That is why when printing the node it shows s=. So the library knows they are string nodes, but some reason that function gives the wrong info.

Demolishun avatar Oct 22 '21 14:10 Demolishun

The data type of a variable node is not the same as the type of node. You have a string nodeid which is a variable of type teobytesNodeId ( the default on that server i suppose)

oroulet avatar Oct 22 '21 14:10 oroulet

The data type of a variable node is not the same as the type of node. You have a string nodeid which is a variable of type teobytesNodeId ( the default on that server i suppose)

I am trying to get the nodeid string and namespace index from the node. The only way I can see to do this is doing a str(node) and parsing the resulting string. Is there another way to get the actual nodeid info?

Okay, that function makes more sense now. The data type of the node is a type reference to tell me the type of data. How do I get the node's nodeid? I am not seeing a method other than str(node) and parsing the string.

Demolishun avatar Oct 22 '21 14:10 Demolishun

Okay, I figured this out: node.nodeid of class gives me the correct nodeid.

Thanks for the help.

Demolishun avatar Oct 22 '21 14:10 Demolishun