def _show_structs(self): base_struct = self.server_mgr.get_node(ua.ObjectIds.Structure) opc_binary = self.server_mgr.get_node( ua.ObjectIds.OPCBinarySchema_TypeSystem) opc_schema = self.server_mgr.get_node(ua.ObjectIds.OpcUa_BinarySchema) for node in opc_binary.get_children(): if node == opc_schema: continue # This is standard namespace structures try: ns = node.get_child("0:NamespaceUri").read_value() ar = self.server_mgr.get_namespace_array() idx = ar.index(ns) except ua.UaError: idx = 1 xml = node.read_value() if not xml: return xml = xml.decode("utf-8") generator = StructGenerator() generator.make_model_from_string(xml) for el in generator.model: # we only care about structs, ignoring enums if isinstance(el, Struct): self._add_design_node(base_struct, idx, el)
def test_custom_structs_array(tmpdir): c = StructGenerator() c.make_model_from_file(EXAMPLE_BSD_PATH) ns = {} output_path = tmpdir.join("test_custom_structs_array.py").strpath c.save_to_file(output_path) with open(output_path) as s: exec(s.read(), ns) # test with default values v = ns["ArrayValueDataType"]() data = struct_to_binary(v) v2 = struct_from_binary(ns["ArrayValueDataType"], ua.utils.Buffer(data)) # set some values v = ns["ArrayValueDataType"]() v.SbyteValue = [1] v.ByteValue = [2] v.Int16Value = [3] v.UInt16Value = [4] v.Int32Value = [5] v.UInt32Value = [6] v.Int64Value = [7] v.UInt64Value = [8] v.FloatValue = [9.0] v.DoubleValue = [10.0] v.StringValue = ["elleven"] v.DateTimeValue = [datetime.utcnow()] # self.GuidValue = uuid.uudib"14" v.ByteStringValue = [b"fifteen", b"sixteen"] v.XmlElementValue = [ua.XmlElement("<toto>titi</toto>")] v.NodeIdValue = [ua.NodeId.from_string("ns=4;i=9999"), ua.NodeId.from_string("i=6")] data = struct_to_binary(v) v2 = struct_from_binary(ns["ArrayValueDataType"], ua.utils.Buffer(data)) assert v.NodeIdValue == v2.NodeIdValue
def test_custom_structs(tmpdir): c = StructGenerator() c.make_model_from_file(EXAMPLE_BSD_PATH) output_path = tmpdir.join("test_custom_structs.py").strpath c.save_to_file(output_path) ns = {} with open(output_path) as s: exec(s.read(), ns) # test with default values v = ns["ScalarValueDataType"]() data = struct_to_binary(v) v2 = struct_from_binary(ns["ScalarValueDataType"], ua.utils.Buffer(data)) # set some values v = ns["ScalarValueDataType"]() v.SbyteValue = 1 v.ByteValue = 2 v.Int16Value = 3 v.UInt16Value = 4 v.Int32Value = 5 v.UInt32Value = 6 v.Int64Value = 7 v.UInt64Value = 8 v.FloatValue = 9.0 v.DoubleValue = 10.0 v.StringValue = "elleven" v.DateTimeValue = datetime.utcnow() # self.GuidValue = uuid.uudib"14" v.ByteStringValue = b"fifteen" v.XmlElementValue = ua.XmlElement("<toto>titi</toto>") v.NodeIdValue = ua.NodeId.from_string("ns=4;i=9999") # self.ExpandedNodeIdValue = # self.QualifiedNameValue = # self.LocalizedTextValue = # self.StatusCodeValue = # self.VariantValue = # self.EnumerationValue = # self.StructureValue = # self.Number = # self.Integer = # self.UInteger = data = struct_to_binary(v) v2 = struct_from_binary(ns["ScalarValueDataType"], ua.utils.Buffer(data)) assert v.NodeIdValue == v2.NodeIdValue