示例#1
0
    def test_client_Client_unions(self):
        # read the structure
        result = self.client.read( [self.address_union] )
        
        self.assertTrue( isinstance(result.targets[0].data, ExtensionObject) )
        extensionObject = result.targets[0].data
        
        result = self.client.read( [self.address_union] , attributeId = attributeids.DataType)
        
        dataTypeId = result.targets[0].data
        
        self.assertEqual( dataTypeId.identifier().idNumeric, 543213)
        
        # using the datatypeId, get the definition of the structure
        definition = self.client.structureDefinition(dataTypeId)
        self.assertEqual( definition.isUnion(), True )
        self.assertEqual( definition.child(0).name(), 'Temperature' )
        self.assertEqual( definition.child(0).valueType(), opcuatypes.Float )
        
        # using the original ExtensionObject the StructureDefinition, we can now create the union value:
        union = GenericUnionValue(extensionObject, definition)
        
        # verify the union
        switchValue = union.switchValue()
        field       = union.field()
        
        self.assertEquals(switchValue, 1)
        
        # now change the active value
        union.setValue(union.field().name(), primitives.Float(111.222))
        
        # write back the union
        newExtensionObject = ExtensionObject()
        union.toExtensionObject(newExtensionObject)
        
        result = self.client.write( [self.address_union], [newExtensionObject] )
        self.assertTrue( result.overallStatus.isGood() )

        result          = self.client.read( [self.address_union] )
        extensionObject = result.targets[0].data
        union           = GenericUnionValue(extensionObject, definition)
        
        self.assertEquals(union.value(), primitives.Float(111.222))
assert type(result.targets[0].data) == ExtensionObject
extensionObject = result.targets[0].data

# now let's find out the datatype of the ExtensionObject
result = myClient.read( [address] , attributeId = attributeids.DataType)
dataTypeId = result.targets[0].data # data represents a NodeId

# using the datatypeId, get the definition of the structure
definition = myClient.structureDefinition(dataTypeId)

print("The definition of the union:")
print(str(definition))
print("")

# we can now create the union:
union = GenericUnionValue(extensionObject, definition)

# print the union:
print("So we can now print the full union:")
printUnion(union)

# now change the active value
union.setValue(union.field().name(), primitives.Float(111.222))

# write back the union
newExtensionObject = ExtensionObject()
union.toExtensionObject(newExtensionObject)

print("Now writing union['%s'] = 111.222333" %union.field().name())
print("")
try:
assert type(result.targets[0].data) == ExtensionObject
extensionObject = result.targets[0].data

# now let's find out the datatype of the ExtensionObject
result = myClient.read([address], attributeId=attributeids.DataType)
dataTypeId = result.targets[0].data  # data represents a NodeId

# using the datatypeId, get the definition of the structure
definition = myClient.structureDefinition(dataTypeId)

print("The definition of the union:")
print(str(definition))
print("")

# we can now create the union:
union = GenericUnionValue(extensionObject, definition)

# print the union:
print("So we can now print the full union:")
printUnion(union)

# now change the active value
union.setValue(union.field().name(), primitives.Float(111.222))

# write back the union
newExtensionObject = ExtensionObject()
union.toExtensionObject(newExtensionObject)

print("Now writing union['%s'] = 111.222333" % union.field().name())
print("")
try: