Why is get_data_type() method of nodeid always returning: TwoByteNodeId
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
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 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
TwoByteNodeIdfor 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=
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)
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.
Okay, I figured this out: node.nodeid of class gives me the correct nodeid.
Thanks for the help.