Commit 04d1e6a7 authored by olivier R-D's avatar olivier R-D

fix typo, add test for enum, move test from server to common

parent 0ddc2c6b
""" """
instantiate a node from a node type. Instantiate a new node and its child nodes from a node type.
Can also be used to duplicate a node tree
""" """
...@@ -12,6 +11,8 @@ def instantiate(parent, node_type, nodeid=None, bname=None, idx=0): ...@@ -12,6 +11,8 @@ def instantiate(parent, node_type, nodeid=None, bname=None, idx=0):
""" """
instantiate a node type under a parent node. instantiate a node type under a parent node.
nodeid and browse name of new node can be specified, or just namespace index nodeid and browse name of new node can be specified, or just namespace index
If they exists children of the node type, such as components, variables and
properties are also instantiated
""" """
results = node_type.get_attributes([ua.AttributeIds.NodeClass, ua.AttributeIds.BrowseName, ua.AttributeIds.DisplayName]) results = node_type.get_attributes([ua.AttributeIds.NodeClass, ua.AttributeIds.BrowseName, ua.AttributeIds.DisplayName])
...@@ -22,7 +23,6 @@ def instantiate(parent, node_type, nodeid=None, bname=None, idx=0): ...@@ -22,7 +23,6 @@ def instantiate(parent, node_type, nodeid=None, bname=None, idx=0):
rdesc.BrowseName = qname rdesc.BrowseName = qname
rdesc.DisplayName = dname rdesc.DisplayName = dname
rdesc.NodeClass = nclass rdesc.NodeClass = nclass
if parent.get_type_definition() == ua.NodeId(ua.ObjectIds.FolderType): if parent.get_type_definition() == ua.NodeId(ua.ObjectIds.FolderType):
rdesc.ReferenceTypeId = ua.NodeId(ua.ObjectIds.Organizes) rdesc.ReferenceTypeId = ua.NodeId(ua.ObjectIds.Organizes)
else: else:
......
...@@ -97,7 +97,7 @@ def create_variable_type(parent, nodeid, bname, datatype): ...@@ -97,7 +97,7 @@ def create_variable_type(parent, nodeid, bname, datatype):
addnode.BrowseName = qname addnode.BrowseName = qname
addnode.NodeClass = ua.NodeClass.Variable addnode.NodeClass = ua.NodeClass.Variable
addnode.ParentNodeId = parent.nodeid addnode.ParentNodeId = parent.nodeid
addnode.ReferenceTypeId = ua.NodeId(ua.ObjectIds.HasSubType) addnode.ReferenceTypeId = ua.NodeId(ua.ObjectIds.HasSubtype)
attrs = ua.VariableTypeAttributes() attrs = ua.VariableTypeAttributes()
attrs.Description = ua.LocalizedText(qname.Name) attrs.Description = ua.LocalizedText(qname.Name)
attrs.DisplayName = ua.LocalizedText(qname.Name) attrs.DisplayName = ua.LocalizedText(qname.Name)
...@@ -126,7 +126,7 @@ def create_reference_type(parent, nodeid, bname): ...@@ -126,7 +126,7 @@ def create_reference_type(parent, nodeid, bname):
addnode.BrowseName = qname addnode.BrowseName = qname
addnode.NodeClass = ua.NodeClass.Variable addnode.NodeClass = ua.NodeClass.Variable
addnode.ParentNodeId = parent.nodeid addnode.ParentNodeId = parent.nodeid
addnode.ReferenceTypeId = ua.NodeId(ua.ObjectIds.HasSubType) addnode.ReferenceTypeId = ua.NodeId(ua.ObjectIds.HasSubtype)
attrs = ua.ReferenceTypeAttributes() attrs = ua.ReferenceTypeAttributes()
attrs.IsAbstract = False attrs.IsAbstract = False
attrs.Description = ua.LocalizedText(qname.Name) attrs.Description = ua.LocalizedText(qname.Name)
...@@ -260,7 +260,7 @@ def _create_variable_type(server, parentnodeid, nodeid, qname, datatype, value=N ...@@ -260,7 +260,7 @@ def _create_variable_type(server, parentnodeid, nodeid, qname, datatype, value=N
addnode.BrowseName = qname addnode.BrowseName = qname
addnode.NodeClass = ua.NodeClass.VariableType addnode.NodeClass = ua.NodeClass.VariableType
addnode.ParentNodeId = parentnodeid addnode.ParentNodeId = parentnodeid
addnode.ReferenceTypeId = ua.NodeId(ua.ObjectIds.HasSubType) addnode.ReferenceTypeId = ua.NodeId(ua.ObjectIds.HasSubtype)
#addnode.TypeDefinition = ua.NodeId(ua.ObjectIds.BaseDataVariableType) #addnode.TypeDefinition = ua.NodeId(ua.ObjectIds.BaseDataVariableType)
attrs = ua.VariableTypeAttributes() attrs = ua.VariableTypeAttributes()
attrs.Description = ua.LocalizedText(qname.Name) attrs.Description = ua.LocalizedText(qname.Name)
...@@ -284,7 +284,7 @@ def _create_variable_type(server, parentnodeid, nodeid, qname, datatype, value=N ...@@ -284,7 +284,7 @@ def _create_variable_type(server, parentnodeid, nodeid, qname, datatype, value=N
return results[0].AddedNodeId return results[0].AddedNodeId
def create_data_type(server, parentnodeid, nodeid, qname, description=None): def create_data_type(parent, nodeid, bname, description=None):
""" """
Create a new data type to be used in new variables, etc .. Create a new data type to be used in new variables, etc ..
arguments are nodeid, browsename arguments are nodeid, browsename
...@@ -296,8 +296,8 @@ def create_data_type(server, parentnodeid, nodeid, qname, description=None): ...@@ -296,8 +296,8 @@ def create_data_type(server, parentnodeid, nodeid, qname, description=None):
addnode.RequestedNewNodeId = nodeid addnode.RequestedNewNodeId = nodeid
addnode.BrowseName = qname addnode.BrowseName = qname
addnode.NodeClass = ua.NodeClass.DataType addnode.NodeClass = ua.NodeClass.DataType
addnode.ParentNodeId = parentnodeid addnode.ParentNodeId = parent.nodeid
addnode.ReferenceTypeId = ua.NodeId(ua.ObjectIds.HasSubType) addnode.ReferenceTypeId = ua.NodeId(ua.ObjectIds.HasSubtype)
#addnode.TypeDefinition = ua.NodeId(ua.ObjectIds.BaseDataVariableType) # No type definition for types #addnode.TypeDefinition = ua.NodeId(ua.ObjectIds.BaseDataVariableType) # No type definition for types
attrs = ua.DataTypeAttributes() attrs = ua.DataTypeAttributes()
if description is None: if description is None:
...@@ -307,12 +307,11 @@ def create_data_type(server, parentnodeid, nodeid, qname, description=None): ...@@ -307,12 +307,11 @@ def create_data_type(server, parentnodeid, nodeid, qname, description=None):
attrs.DisplayName = ua.LocalizedText(qname.Name) attrs.DisplayName = ua.LocalizedText(qname.Name)
attrs.WriteMask = 0 attrs.WriteMask = 0
attrs.UserWriteMask = 0 attrs.UserWriteMask = 0
attrs.Historizing = 0
attrs.IsAbstract = False # True mean they cannot be instanciated attrs.IsAbstract = False # True mean they cannot be instanciated
addnode.NodeAttributes = attrs addnode.NodeAttributes = attrs
results = server.add_nodes([addnode]) results = parent.server.add_nodes([addnode])
results[0].StatusCode.check() results[0].StatusCode.check()
return results[0].AddedNodeId return node.Node(parent.server, results[0].AddedNodeId)
def _create_method(parent, nodeid, qname, callback, inputs, outputs): def _create_method(parent, nodeid, qname, callback, inputs, outputs):
......
...@@ -564,4 +564,28 @@ class CommonTests(object): ...@@ -564,4 +564,28 @@ class CommonTests(object):
self.assertNotEqual(prop.nodeid, prop_t.nodeid) self.assertNotEqual(prop.nodeid, prop_t.nodeid)
def test_variable_with_datatype(self):
o = self.opc.get_objects_node()
v1 = o.add_variable(3, 'VariableEnumType1', ua.ApplicationType.ClientAndServer, datatype=ua.NodeId(ua.ObjectIds.ApplicationType))
tp1 = v1.get_data_type()
self.assertEqual(ua.NodeId(ua.ObjectIds.ApplicationType), tp1)
v2 = o.add_variable(3, 'VariableEnumType2', ua.ApplicationType.ClientAndServer, datatype=ua.NodeId(ua.ObjectIds.ApplicationType) )
tp2 = v2.get_data_type()
self.assertEqual( ua.NodeId(ua.ObjectIds.ApplicationType), tp2)
def test_enum(self):
# create enum type
enums = self.opc.get_root_node().get_child(["0:Types", "0:DataTypes", "0:BaseDataType", "0:Enumeration"])
myenum_type = enums.add_data_type(0, "MyEnum")
es = myenum_type.add_variable(0, "EnumStrings", ["String0", "String1", "String2"], ua.VariantType.LocalizedText)
#es.set_value_rank(1)
# instantiate
o = self.opc.get_objects_node()
myvar = o.add_variable(2, "MyEnumVar", "String1", ua.VariantType.LocalizedText, datatype=myenum_type.nodeid)
#myvar.set_writable(True)
# tests
self.assertEqual(myvar.get_data_type(), myenum_type.nodeid)
myvar.set_value("String2", ua.VariantType.LocalizedText)
...@@ -16,7 +16,7 @@ from opcua import uamethod ...@@ -16,7 +16,7 @@ from opcua import uamethod
from opcua.common.event_objects import BaseEvent, AuditEvent from opcua.common.event_objects import BaseEvent, AuditEvent
port_num = 485140 port_num = 48540
port_discovery = 48550 port_discovery = 48550
...@@ -363,16 +363,6 @@ class TestServer(unittest.TestCase, CommonTests, SubscriptionTests): ...@@ -363,16 +363,6 @@ class TestServer(unittest.TestCase, CommonTests, SubscriptionTests):
self.assertEqual(evgen.event.PropertyNum, None) self.assertEqual(evgen.event.PropertyNum, None)
self.assertEqual(evgen.event.PropertyString, None) self.assertEqual(evgen.event.PropertyString, None)
def test_add_variable_with_datatype(self):
o = self.opc.get_objects_node()
v1 = o.add_variable(3, 'VariableEnumType1', ua.ApplicationType.ClientAndServer, datatype=ua.NodeId(ua.ObjectIds.ApplicationType))
tp1 = v1.get_data_type()
self.assertEqual( ua.NodeId(ua.ObjectIds.ApplicationType), tp1)
v2 = o.add_variable(3, 'VariableEnumType2', ua.ApplicationType.ClientAndServer, datatype=ua.NodeId(ua.ObjectIds.ApplicationType) )
tp2 = v2.get_data_type()
self.assertEqual( ua.NodeId(ua.ObjectIds.ApplicationType), tp2)
def test_context_manager(self): def test_context_manager(self):
""" Context manager calls start() and stop() """ Context manager calls start() and stop()
""" """
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment