def test_client_Client_structures(self): # read the structure result = self.client.read( [self.address_vector] ) self.assertTrue( isinstance(result.targets[0].data, ExtensionObject) ) extensionObject = result.targets[0].data result = self.client.read( [self.address_vector] , attributeId = attributeids.DataType) dataTypeId = result.targets[0].data self.assertEqual( dataTypeId.identifier().idNumeric, 3002 ) # using the datatypeId, get the definition of the structure definition = self.client.structureDefinition(dataTypeId) self.assertEqual( definition.isUnion(), False ) self.assertEqual( definition.child(0).name(), 'X' ) self.assertEqual( definition.child(1).valueType(), opcuatypes.Double ) # using the original ExtensionObject the StructureDefinition, we can now create the GenericStructureValue: structureValue = GenericStructureValue(extensionObject, definition) # verify the structure: fieldType, opcUaStatusCode = structureValue.valueType(0) self.assertEqual( fieldType, structurefielddatatypes.Variant ) value, statuscode = structureValue.value(0) self.assertTrue( isinstance(value, primitives.Double ) ) self.assertEquals(statuscode, 0) # now change the value of the first child (i = 0): structureValue.setField(0, primitives.Double(0.1) ) # write back the structure newExtensionObject = ExtensionObject() structureValue.toExtensionObject(newExtensionObject) result = self.client.write( [self.address_vector], [newExtensionObject] ) self.assertTrue( result.overallStatus.isGood() ) result = self.client.read( [self.address_vector] ) extensionObject = result.targets[0].data structureValue = GenericStructureValue(extensionObject, definition) value, statuscode = structureValue.value(0) self.assertEqual( value, primitives.Double(0.1) ) self.assertEqual( statuscode, 0 )
# using the original ExtensionObject the StructureDefinition, we can now create the GenericStructureValue: structureValue = GenericStructureValue(extensionObject, definition) # print the structure: print("So we can now print the full structure:") printStructure(structureValue) # now change the value of the first child (i = 0): structureValue.setField(0, primitives.Double(0.1)) # we can also change a field via its name: structureValue.setField("Y", primitives.Double(0.2)) # write back the structure newExtensionObject = ExtensionObject() structureValue.toExtensionObject(newExtensionObject) print("Now writing {structure}.X = 0.1 and {structure}.Y = 0.2") print("") try: result = myClient.write( [address], [newExtensionObject] ) if result.overallStatus.isGood(): print("OK, the new structure value has been written successfully") else: print("Oops, some OPC UA problem occurred. Here's the result:\n%s" %result) except UafError, e: print("Oops, some error occurred on the client side. Here's the error message: %s" %e) print("") print("Let's read the same structure again, to verify that the values have changed:") result = myClient.read( [address] )
# using the original ExtensionObject the StructureDefinition, we can now create the GenericStructureValue: structureValue = GenericStructureValue(extensionObject, definition) # print the structure: print("So we can now print the full structure:") printStructure(structureValue) # now change the value of the first child (i = 0): structureValue.setField(0, primitives.Double(0.1)) # we can also change a field via its name: structureValue.setField("Y", primitives.Double(0.2)) # write back the structure newExtensionObject = ExtensionObject() structureValue.toExtensionObject(newExtensionObject) print("Now writing {structure}.X = 0.1 and {structure}.Y = 0.2") print("") try: result = myClient.write([address], [newExtensionObject]) if result.overallStatus.isGood(): print("OK, the new structure value has been written successfully") else: print("Oops, some OPC UA problem occurred. Here's the result:\n%s" % result) except UafError, e: print( "Oops, some error occurred on the client side. Here's the error message: %s" % e)