PySide6.QtOpcUa.QOpcUaGenericStructHandler¶
- class QOpcUaGenericStructHandler¶
Reads a server’s data types and decodes/encodes generic structs from/to extension objects. More…
Added in version 6.7.
Synopsis¶
Methods¶
def
__init__()
def
initialize()
def
initialized()
Signals¶
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description¶
The binary data encoding used in OPC UA was designed with a small message size in mind and doesn’t contain any information about the structure of the data. This means that a decoder must known the structure of the encoded data in advance to be able to decode a data buffer.
Since OPC UA 1.04, nodes of the DataType node class may have the DataTypeDefinition attribute which contain information about the fields of structured types and the mapping of enum values to names. Together with the knowledge about how to decode built-in types, this allows a client to decode generic custom structured types without relying on outside knowledge.
QOpcUaGenericStructHandler
traverses the type hierarchy of a server by following the HasSubtype references starting from BaseDataType and reads the DataTypeDefinition attribute of the nodes.For structured types where a
QOpcUaStructureDefinition
value is present in the DataTypeDefinition attribute, automatic decoding of extension objects containing them is available. Fields with a built-in type or a type where a C++ data class exists are deserialized to the corresponding Qt OPC UA type, other generic structs are serialized to a nestedQOpcUaGenericStructValue
. All nested generic struct values must have aQOpcUaStructureDefinition
in the server or decoding fails.The same conditions apply to encoding a custom struct.
Example for decoding a custom struct:
QOpcUaGenericStructHandler handler(opcuaClient); handler.initialize(); QObject::connect(&handler, &QOpcUaGenericStructHandler::initializedChanged, [opcuaClient, &handler](bool initialized) { if (!initialized) return; auto node = opcuaClient->node("ns=4;i=3003"); // A custom struct test node in the open62541-testserver node->readValueAttribute(); QObject::connect(node, &QOpcUaNode::attributeRead, [node, &handler](QOpcUa::NodeAttributes attr) { if (!attr.testFlag(QOpcUa::NodeAttribute::Value) || node->valueAttributeError() != QOpcUa::UaStatusCode::Good) return; auto extObj = node->valueAttribute().value<QOpcUaExtensionObject>(); qDebug() << "Got object of type" << handler.typeNameForBinaryEncodingId(extObj.encodingTypeId()); const auto result = handler.decode(extObj); if (!result) return; qDebug() << *result; }); });
Example for encoding a custom struct:
QOpcUaGenericStructHandler handler(opcuaClient); handler.initialize(); QObject::connect(&handler, &QOpcUaGenericStructHandler::initializedChanged, [opcuaClient, &handler](bool initialized) { if (!initialized) return; QOpcUaGenericStructValue value = handler.createGenericStructValueForTypeId("ns=4;i=3006"); value.fieldsRef()["MandatoryMember"] = 23.0; value.fieldsRef()["OptionalMember"] = 42.0; const auto ext = handler.encode(value); if (!ext) return; // Use the extension object to write a node's value attribute, in a method parameter, etc... });
- class DataTypeKind¶
This enum type specifies data type kind of a data type node.
Constant
Description
QOpcUaGenericStructHandler.DataTypeKind.Unknown
The type node id is unknown.
QOpcUaGenericStructHandler.DataTypeKind.Struct
The type node id belongs to a structured type.
QOpcUaGenericStructHandler.DataTypeKind.Enum
The type node id belongs to an enum type.
QOpcUaGenericStructHandler.DataTypeKind.Other
The type node id belongs to a type which is not a struct or enum (other built-in types or their subtypes)
- __init__(client[, parent=None])¶
- Parameters:
client –
QOpcUaClient
parent –
QObject
Constructs a generic struct handler for
client
.- addCustomEnumDefinition(definition, typeId, name[, isAbstract=QOpcUa.IsAbstract.NotAbstract])¶
- Parameters:
definition –
QOpcUaEnumDefinition
typeId – str
name – str
isAbstract –
IsAbstract
- Return type:
bool
Adds the custom enum definition
definition
to the known types. This can be used to support custom structures the server doesn’t expose a StructureDefinition for. The parametersdefinition
,typeId
andname
are required for proper decoding and encoding. IfisAbstract
is set, the type can’t be encoded and decoded.Returns
true
if the enum definition was successfully added.- addCustomStructureDefinition(definition, typeId, name[, isAbstract=QOpcUa.IsAbstract.NotAbstract])¶
- Parameters:
definition –
QOpcUaStructureDefinition
typeId – str
name – str
isAbstract –
IsAbstract
- Return type:
bool
Adds the custom structure definition
definition
to the known types. This can be used to support custom structures the server doesn’t expose a StructureDefinition for. The parametersdefinition
,typeId
andname
are required for proper decoding and encoding. IfisAbstract
is set, the type can’t be encoded and decoded.Returns
true
if the structure definition was successfully added.- createGenericStructValueForTypeId(typeId)¶
- Parameters:
typeId – str
- Return type:
Returns a generic struct value pre-filled with the struct definition, type id and type name corresponding to
typeId
. For all mandatory fields, an invalid placeholder QVariant will be inserted.- dataTypeKindForTypeId(id)¶
- Parameters:
id – str
- Return type:
Returns the data type kind for the type node id
id
.- enumDefinitionForTypeId(id)¶
- Parameters:
id – str
- Return type:
Returns the
QOpcUaEnumDefinition
for the type node idid
. If the node id is unknown or does not belong to an enum type, a default constructed value is returned.- initialize()¶
- Return type:
bool
Starts the data type hierarchy traversal. Success or failure is reported in the
initializedChanged
signal.Returns
false
if the operation can’t be started.- initialized()¶
- Return type:
bool
Returns
true
if the generic struct handler is initialized.- initializedChanged(initialized)¶
- Parameters:
initialized – bool
This signal is emitted when the initialization process has finished.
initialized
indicates if the initialization was successful.- isAbstractTypeId(id)¶
- Parameters:
id – str
- Return type:
bool
Returns true if the data type described by
id
is abstract.- structureDefinitionForBinaryEncodingId(id)¶
- Parameters:
id – str
- Return type:
Returns the
QOpcUaStructureDefinition
for the binary encoding node idid
. If the node id is unknown or does not belong to a struct type, a default constructed value is returned.- structureDefinitionForTypeId(id)¶
- Parameters:
id – str
- Return type:
Returns the
QOpcUaStructureDefinition
for the type node idid
. If the node id is unknown or does not belong to a struct type, a default constructed value is returned.- typeIdForBinaryEncodingId(id)¶
- Parameters:
id – str
- Return type:
str
Returns the type node id associated with the binary encoding id
id
.- typeNameForBinaryEncodingId(id)¶
- Parameters:
id – str
- Return type:
str
Returns the type name belonging to the binary encoding node id
id
. If the node id is unknown or does not belong to a struct type, an empty string is returned.- typeNameForTypeId(id)¶
- Parameters:
id – str
- Return type:
str
Returns the type name belonging to a data type node identified by type node id
id
. If the node id is unknown, an empty string is returned.