PySide6.QtOpcUa.QOpcUaNode¶
- class QOpcUaNode¶
QOpcUaNode允许与 OPC UA 节点进行交互。更多…概要¶
方法¶
def
attribute()def
attributeError()def
browse()def
browseChildren()def
callMethod()def
client()def
nodeId()def
readAttributes()def
readHistoryRaw()def
valueAttribute()def
writeAttribute()
信号¶
def
attributeRead()def
browseFinished()def
eventOccurred()
静态函数¶
注意
本文档可能包含从C++自动翻译到Python的代码片段。我们始终欢迎对代码片段翻译的贡献。如果您发现翻译问题,您也可以通过在我们的https:/bugreports.qt.io/projects/PYSIDE上创建工单来告知我们。
详细描述¶
节点是OPC UA地址空间的基本构建块。它具有诸如浏览名称、值、关联属性等属性,并且可以引用地址空间中的其他节点。节点被组织在命名空间中,并具有ID,这些ID可以是数字、字符串、命名空间特定格式(不透明)或全局唯一标识符。节点由命名空间ID和节点ID标识。此标识符通常以字符串形式给出:位于命名空间0且具有数字标识符42的节点的标识符结果为字符串
ns=0;i=42。具有字符串标识符的节点可以通过ns=0;s=myStringIdentifier进行寻址。此类型的对象由用户拥有,当不再需要时必须删除。只要创建它们的
QOpcUaClient存在,它们就有效。属性的读取和写入¶
节点属性在调用
readAttributes()或readAttributeRange()时从服务器读取。结果会缓存在本地,并且在接收到attributeRead信号后,可以使用attribute()来检索。如果用户拥有必要的权限,可以使用
writeAttribute()、writeAttributes()和writeAttributeRange()来写入属性。写入操作的成功与否通过attributeWritten信号报告。attributeError()包含与属性上的最后一次读取或写入操作相关的状态码。这是由OPC UA服务返回的低级状态码。可以通过使用errorCategory()将其转换为ErrorCategory来简化此状态码。订阅和监控项¶
订阅是OPC UA中的一个概念,它允许接收数据变化或事件的通知,而不是持续轮询节点以获取变化。监控项定义了如何监视节点的属性变化。它们被添加到订阅中,它们生成的任何通知都通过订阅转发给用户。更新的间隔以及监控项和订阅的许多其他选项可以由用户配置。
QOpcUaNode提供了一个与订阅和监控项交互的抽象。enableMonitoring()启用一个或多个属性的数据更改通知。dataChangeOccurred信号包含新值,并且本地缓存会更新。disableMonitoring()禁用数据更改通知。monitoringStatusChanged信号通知监控状态的变化,例如在手动启用和禁用后或服务器上的状态变化。事件监控使用相同的API进行设置和生命周期管理。
EventNotifier属性必须使用EventFilter进行监控,该过滤器选择所需的事件字段并根据用户定义的标准过滤报告的事件。事件在eventOccurred()信号中报告为QVariantList,其中包含所选事件字段的值。订阅和监控项的设置可以在运行时使用
modifyMonitoring()进行修改。浏览地址空间¶
OPC UA地址空间由通过引用连接的节点组成。
browseChildren沿着这些引用向前遍历,并在browseFinished信号中返回连接到QOpcUaNode实例后面的所有节点的属性。browse()类似于browseChildren(),但提供了更多选项来配置浏览调用。方法调用¶
OPC UA 指定了服务器上可以由用户调用的方法。
QOpcUaNode通过callMethod支持这一点,它接受参数并在methodCallFinished信号中返回调用结果。解析浏览路径¶
为了支持根据类型描述进行编程,OPC UA支持从某个节点开始解析浏览名称的路径,以获取目标节点的节点ID。
resolveBrowsePath()方法从调用它的节点开始跟随路径,并在resolveBrowsePathFinished()信号中返回结果。示例¶
要将客户端连接到服务器并获取
QOpcUaNode对象,请参阅QOpcUaClient。节点成功创建后,从服务器读取根节点的BrowseName:
QOpcUaNode *rootNode; // Created before, see QOpcUaClient documentation. // Connect to the attributeRead signal. Compatible slots of QObjects can be used instead of a lambda. QObject::connect(rootNode, &QOpcUaNode::attributeRead, [rootNode, client](QOpcUa::NodeAttributes attr) { qDebug() << "Signal for attributes:" << attr; if (rootNode->attributeError(QOpcUa::NodeAttribute::BrowseName) != QOpcUa::UaStatusCode::Good) { qDebug() << "Failed to read attribute:" << rootNode->attributeError(QOpcUa::NodeAttribute::BrowseName); client->disconnectFromEndpoint(); } qDebug() << "Browse name:" << rootNode->attribute(QOpcUa::NodeAttribute::BrowseName).value<QOpcUaQualifiedName>().name(); }); rootNode->readAttributes(QOpcUa::NodeAttribute::BrowseName); // Start a read operation for the node's BrowseName attribute.
- static allBaseAttributes()¶
- Return type:
包含OPC UA基础节点类的所有属性。
- attribute(attribute)¶
- Parameters:
属性 –
NodeAttribute- Return type:
对象
返回
attribute中给定的属性值。该值仅在发出
attributeRead信号后有效。如果属性没有缓存值,则返回一个空的QVariant。- attributeError(attribute)¶
- Parameters:
属性 –
NodeAttribute- Return type:
返回
attribute中给定属性的错误代码。错误代码仅在
attributeRead或attributeWritten信号发出后才有效。如果属性缓存中没有条目,
BadNoEntryExists将被返回。另请参阅
- attributeRead(attributes)¶
- Parameters:
属性 –
NodeAttribute的组合
此信号在
readAttributes()或readAttributeRange()操作完成后发出。接收者需要检查attributes中包含的属性的状态码。- attributeUpdated(attr, value)¶
- Parameters:
attr –
NodeAttributevalue – 对象
此信号在属性缓存中的值通过来自服务器的数据更改通知、读取或写入操作更新后发出。
value包含节点属性attr的新值。- attributeWritten(attribute, statusCode)¶
- Parameters:
属性 –
NodeAttributestatusCode –
UaStatusCode
此信号在
writeAttribute()、writeAttributes()或writeAttributeRange()操作完成后发出。在发出此信号之前,如果写入成功,属性缓存将被更新。对于
writeAttributes(),写入调用中的每个属性都会发出一个信号。statusCode包含对attribute进行写入操作的成功信息。- browse(request)¶
- Parameters:
请求 –
QOpcUaBrowseRequest- Return type:
布尔
从这个节点开始一个浏览调用。
如果异步调用已成功分发,则返回
true。所有符合
request中指定条件的引用都会在browseFinished()信号中返回。例如,可以使用反向浏览调用来查找属性节点的父节点:
QOpcUaBrowseRequest request; request.setBrowseDirection(QOpcUaBrowseRequest::BrowseDirection::Inverse); request.setReferenceTypeId(QOpcUa::ReferenceTypeId::HasProperty); propertyNode->browse(request);
- browseChildren([referenceType=QOpcUa.ReferenceTypeId.HierarchicalReferences[, nodeClassMask=QOpcUa.NodeClass.Undefined]])¶
- Parameters:
referenceType –
ReferenceTypeIdnodeClassMask –
NodeClass的组合
- Return type:
布尔
从调用此方法的节点开始执行前向浏览调用。浏览操作收集有关连接到该节点的子节点的信息,并在
browseFinished()信号中传递结果。如果异步调用已成功分发,则返回
true。要请求仅通过特定类型的引用连接到节点的子节点,必须将
referenceType设置为该引用类型。例如,可以通过在referenceType中传递HasProperty来获取节点的所有属性。可以通过在nodeClassMask中设置它们来过滤结果,使其仅包含具有特定节点类的节点。- browseFinished(children, statusCode)¶
- Parameters:
children – QOpcUaReferenceDescription 的列表
statusCode –
UaStatusCode
此信号在
browseChildren()或browse()操作完成后发出。children包含有关所有符合browseChildren()中条件的节点的信息。statusCode包含浏览操作的服务结果。如果statusCode不是Good,则传递的children向量为空。- callMethod(methodNodeId[, args=list()])¶
- Parameters:
methodNodeId – str
args – std.pairQVariant,QOpcUa.Types 的列表
- Return type:
布尔
调用OPC UA方法
methodNodeId,并使用通过args提供的参数。结果在methodCallFinished信号中返回。如果异步调用已成功分发,则返回
true。- client()¶
- Return type:
返回指向创建此节点的客户端的指针。
- dataChangeOccurred(attr, value)¶
- Parameters:
attr –
NodeAttributevalue – 对象
此信号在接收到数据更改通知后发出。
value包含节点属性attr的新值。- disableMonitoring(attr)¶
- Parameters:
attr –
NodeAttribute的组合- Return type:
布尔
此方法禁用对
attr中给出的属性的监控。如果异步调用已成功分发,则返回
true。调用完成后,会发出
disableMonitoringFinished信号,并且monitoringStatus会返回一个默认构造的值,状态码为BadMonitoredItemIdIinvalid,用于attr。- disableMonitoringFinished(attr, statusCode)¶
- Parameters:
attr –
NodeAttributestatusCode –
UaStatusCode
此信号在对
disableMonitoring()的异步调用完成后发出。statusCode包含操作生成的状态码。在此信号发出后,monitoringStatus返回一个默认构造的值,状态码为BadMonitoredItemIdIinvalid,用于attr。- enableMonitoring(attr, settings)¶
- Parameters:
attr –
NodeAttribute的组合
- Return type:
布尔
此方法为
attr中给出的每个属性创建一个监控项。settings中的设置用于创建监控项和订阅。如果异步调用已成功分发,则返回
true。调用完成后,会发出
enableMonitoringFinished信号。在多种错误情况下会生成错误状态码:settings中指定的订阅ID不存在,节点在服务器上不存在,节点没有请求的属性,或者服务器已达到监控项的最大数量。相同的方法用于启用事件监控。事件是OPC UA地址空间中的特殊对象,包含有关已发生事件的信息。如果在服务器上触发了事件,事件监控项会收集事件对象及其子节点的选定节点属性值。每个具有事件源的节点都可以进行事件监控。要监控节点的事件,必须使用包含用户所需事件字段的EventFilter来监控属性
EventNotifier,并可选择使用where子句来按条件过滤事件(更多详情,请参见EventFilter)。- enableMonitoringFinished(attr, statusCode)¶
- Parameters:
attr –
NodeAttributestatusCode –
UaStatusCode
此信号在对
enableMonitoring()的异步调用完成后发出。在此信号发出后,monitoringStatus()返回attr的有效信息。statusCode包含操作的状态码。- eventOccurred(eventFields)¶
- Parameters:
eventFields – QVariant 的列表
此信号在新事件被接收后发出。
eventFields包含事件字段的值,顺序与事件过滤器的select子句中指定的顺序一致。- static mandatoryBaseAttributes()¶
- Return type:
包含OPC UA基础节点类的所有必需属性。
- methodCallFinished(methodNodeId, result, statusCode)¶
- Parameters:
methodNodeId – str
result – 对象
statusCode –
UaStatusCode
此信号在服务器上完成对
methodNodeId的方法调用后发出。statusCode包含方法调用的状态码,result包含方法的输出参数。如果方法没有输出参数或statusCode不是Good,则result为空。result变体如果只有一个输出参数则为单个值,如果调用的函数返回多个输出参数则包含变体列表。if (result.canConvert<QVariantList>()) { // handle list type } else { // handle value type }
- modifyDataChangeFilter(attr, filter)¶
- Parameters:
attr –
NodeAttributefilter –
DataChangeFilter
- Return type:
布尔
修改现有的数据更改监控以使用
filter作为数据更改过滤器。如果过滤器修改请求已成功发送到后端,则返回
true。monitoringStatusChanged用于attr在操作完成后发出。- modifyEventFilter(eventFilter)¶
- Parameters:
eventFilter –
EventFilter- Return type:
布尔
修改现有的事件监控以使用
eventFilter作为事件过滤器。如果过滤器修改请求已成功发送到后端,则返回
true。monitoringStatusChanged用于EventNotifier在操作完成后发出。- modifyMonitoring(attr, item, value)¶
- Parameters:
attr –
NodeAttribute项目 –
参数value – 对象
- Return type:
布尔
此方法修改监控项或订阅的设置。尝试将与
attr关联的监控项或订阅的参数item设置为value。如果异步调用已成功分发,则返回
true。调用完成后,会发出
monitoringStatusChanged信号。该信号包含修改后的参数和状态码。如果没有与请求的属性关联的监控项、修改请求的参数未实现或服务器拒绝了请求的值,则会生成错误状态码。- monitoringStatus(attr)¶
- Parameters:
attr –
NodeAttribute- Return type:
返回与属性
attr相关的监控参数。这可以用于检查enableMonitoring()的成功与否,或者参数是否已被修订。返回的值仅在enableMonitoringFinished或monitoringStatusChanged为attr发出后才有效。如果在信号发出之前查询状态,statusCode()将返回BadNoEntryExists。- monitoringStatusChanged(attr, items, statusCode)¶
- Parameters:
attr –
NodeAttributeitems –
Parameter的组合statusCode –
UaStatusCode
此信号在对
modifyMonitoring()的异步调用完成后发出。请求操作的节点属性在attr中返回。items包含已修改的参数。statusCode包含服务器上修改操作的结果。- nodeId()¶
- Return type:
字符串
返回OPC UA节点的ID。
- readAttributeRange(attribute, indexRange)¶
- Parameters:
属性 –
NodeAttributeindexRange – str
- Return type:
布尔
启动节点属性
attribute的异步读取操作。indexRange是一个字符串,可用于选择数组的一部分。它在OPC UA 1.05第4部分7.27中定义。数组中的第一个元素是0,“1”返回第二个元素,“0:9”返回前10个元素,“0,1”返回二维数组中第一行的第二个元素。如果异步调用已成功分发,则返回
true。属性值仅在发出
attributeRead信号后才包含有效信息。- readAttributes([attributes=QOpcUaNode.mandatoryBaseAttributes()])¶
- Parameters:
属性 –
NodeAttribute的组合- Return type:
布尔
启动对
attributes中节点属性的异步读取操作。如果异步调用已成功分发,则返回
true。属性值仅在发出
attributeRead信号后才包含有效信息。- readHistoryEvents(startTime, endTime, filter[, numValues=0])¶
- Parameters:
startTime –
QDateTimeendTime –
QDateTimefilter –
EventFilternumValues – int
- Return type:
使用参数
startTime、endTime、filter和numValues为此节点启动读取事件历史请求。filter由服务器用于确定应返回哪些事件及其字段集。返回一个
QOpcUaHistoryReadResponse,其中包含异步请求成功分发的请求状态。结果在UaStatusCode serviceResult)信号中返回。以下示例检索过去两天的历史事件。每次最多返回10个事件。当有更多事件符合过滤器和提供的时间范围时,
hasMoreData()将为true,并且可以通过readMoreData()获取更多事件。QScopedPointer<QOpcUaNode> node(opcuaClient->node("ns=2;s=EventHistorian")); QVERIFY(node != nullptr); QOpcUaMonitoringParameters::EventFilter filter; filter << QOpcUaSimpleAttributeOperand("Message"); filter << QOpcUaSimpleAttributeOperand("Time"); const auto response = node->readHistoryEvents(QDateTime::currentDateTime().addDays(-2), QDateTime::currentDateTime(), filter, 10); QObject::connect(response, &QOpcUaHistoryReadResponse::readHistoryEventsFinished, this, [response](const QList<QOpcUaHistoryEvent> &results, QOpcUa::UaStatusCode serviceResult) { if (serviceResult != QOpcUa::UaStatusCode::Good) { qDebug() << "Service call failed with" << serviceResult; return; } // Print what we got so far for (const auto &result : response->events()) { qDebug() << "Results for" << result.nodeId() << result.statusCode(); for (const auto &event : result.events()) qDebug() << " Event:" << event; } if (response->hasMoreData()) response->readMoreData(); });
- readHistoryRaw(startTime, endTime, numValues, returnBounds)¶
- Parameters:
- Return type:
为此节点启动读取历史记录请求。这是Qt OPC UA对OPC UA读取历史服务的表示,用于读取在OPC UA 1.05第4部分,5.10.3中定义的原始历史数据。它根据参数
startTime、endTime、numValues和returnBounds读取历史记录。返回一个
QOpcUaHistoryReadResponse,其中包含异步请求成功分发的请求状态。结果在UaStatusCode serviceResult)信号中返回。在以下示例中,请求并打印节点过去两天的历史数据。结果限制为每个节点十个值。
QOpcUaHistoryReadResponse *response = node->readHistoryRaw(QDateTime::currentDateTime(), QDateTime::currentDateTime().addDays(-2), 10, true); if (response) { QObject::connect(response123, &QOpcUaHistoryReadResponse::readHistoryDataFinished, [] (QList<QOpcUaHistoryData> results, QOpcUa::UaStatusCode serviceResult) { if (serviceResult != QOpcUa::UaStatusCode::Good) { qWarning() << "Fetching historical data failed with:" << serviceResult; } else { for (const auto& result : results) { qInfo() << "NodeId:" << result.nodeId(); for (const auto &dataValue : result.result()) qInfo() << "Value:" << dataValue.value(); } } }); }
- readHistoryRaw(startTime, endTime, numValues, returnBounds, timestampsToReturn)
- Parameters:
startTime –
QDateTimeendTime –
QDateTimenumValues – int
returnBounds – 布尔值
timestampsToReturn –
TimestampsToReturn
- Return type:
为此节点启动读取历史记录请求。额外的
timestampsToReturn参数决定了将为每个值返回哪些时间戳。- readValueAttribute()¶
- Return type:
布尔
启动节点Value属性的异步读取操作。
如果异步调用已成功分发,则返回
true。另请参阅
- resolveBrowsePath(path)¶
- Parameters:
path – QOpcUaRelativePathElement 的列表
- Return type:
布尔
使用OPC UA 1.05第4部分5.8.4中指定的TranslateBrowsePathsToNodeIds服务,将浏览路径
path解析为从此节点开始的一个或多个节点ID。如果异步调用已成功分发,则返回
true。TranslateBrowsePathsToNodeIds 主要用于针对类型定义进行编程,而不是针对 OPC UA 地址空间中的具体节点集。例如,一个机器模型的类型定义可能由一个浏览名称为“Machine”的起始节点组成,该节点有一个浏览名称为“Fan”的组件。Fan 有一个浏览名称为“RPM”的组件,它是一个变量节点,保存着风扇的当前 RPM 值。有多个该类型的机器,每个机器都作为机器类型的对象映射到 OPC UA 地址空间中。对于每个机器对象,从机器节点到“RPM”节点的路径是相同的。如果客户端想要读取当前的 RPM 值,它需要以机器节点为起始节点,并使用从机器到“RPM”节点的浏览路径调用 resolveBrowsePath():
QScopedPointer<QOpcUaNode> node(opcuaClient->node("ns=1;s=machine1")); QList<QOpcUaRelativePathElement> path; path.append(QOpcUaRelativePathElement(QOpcUaQualifiedName(1, "Fan"), QOpcUa::ReferenceTypeId::HasComponent)); path.append(QOpcUaRelativePathElement(QOpcUaQualifiedName(1, "RPM"), QOpcUa::ReferenceTypeId::HasComponent)); node->resolveBrowsePath(path);
在
resolveBrowsePathFinished()中返回的结果包含“RPM”节点的节点ID,该ID可用于访问节点的属性:if (!results.size()) { qWarning() << "Browse path resolution failed"; return; } if (results.at(0).isFullyResolved()) { QOpcUaNode *rpmNode = client->node(results.at(0).targetId()); if (!rpmNode) { qWarning() << "Failed to create node"; return; } // Connect slots, call methods } else { qWarning() << "Browse path could not be fully resolved, the target node is on another server"; return; }- resolveBrowsePathFinished(targets, path, statusCode)¶
- Parameters:
targets – QOpcUaBrowsePathTarget 的列表
path – QOpcUaRelativePathElement 的列表
statusCode –
UaStatusCode
此信号在
resolveBrowsePath()调用完成后发出。QOpcUaBrowsePathTargettargets包含匹配项,statusCode是操作的状态码。如果statusCode不是Good,targets则为空。浏览路径path是请求中的浏览路径。它可以用于将结果与请求关联起来。- serverTimestamp(attribute)¶
- Parameters:
属性 –
NodeAttribute- Return type:
返回从最后一次读取或数据更改
attribute的服务器时间戳。在至少一个attributeRead或dataChangeOccurred信号发出之前,返回一个空的日期时间。- sourceTimestamp(attribute)¶
- Parameters:
属性 –
NodeAttribute- Return type:
返回从最后一次读取或数据更改
attribute的源时间戳。在至少一个attributeRead或dataChangeOccurred信号发出之前,返回一个空的日期时间。- valueAttribute()¶
- Return type:
对象
返回节点的Value属性的值。
返回值仅在成功读取或写入Value属性后,或监控数据更改更新属性缓存后有效。这由状态码为
Good的attributeRead()或attributeWritten()信号,或Value属性的dataChangeOccurred()信号指示。如果属性缓存中没有值,则返回一个无效的QVariant。
- valueAttributeError()¶
- Return type:
返回节点Value属性的错误代码。状态码
Good表示valueAttribute()的有效返回值。如果属性缓存中没有条目,则返回BadNoEntryExists。- valueAttributeUpdated(value)¶
- Parameters:
值 – 对象
当服务器通过数据更改通知、读取或写入操作更新属性缓存中的value属性后,会发出此信号。
value包含value属性的新值。- writeAttribute(attribute, value[, type=QOpcUa.Types.Undefined])¶
- Parameters:
属性 –
NodeAttributevalue – 对象
type –
类型
- Return type:
布尔
使用
type中的类型信息将value写入attribute中给定的属性。如果异步调用已成功分派,则返回true。如果省略了
type参数,后端会尝试找到正确的类型。假设以下默认类型:Qt MetaType
OPC UA 类型
布尔
布尔值
UChar
字节
字符
有符号字节
UShort
UInt16
短整型
16位整型
整数
32位整数
UInt
UInt32
ULongLong
UInt64
长整型
64位整数
双精度
双精度
浮点数
浮点数
QString
字符串
QDateTime
日期时间
QByteArray
字节字符串
QUuid
Guid
- writeAttributeRange(attribute, value, indexRange[, type=QOpcUa.Types.Undefined])¶
- Parameters:
属性 –
NodeAttributevalue – 对象
indexRange – str
type –
类型
- Return type:
布尔
使用
type中的类型信息将value写入attribute中指定的属性。关于indexRange,请参见readAttributeRange()。如果异步调用已成功分发,则返回
true。- writeAttributes(toWrite[, valueAttributeType=QOpcUa.Types.Undefined])¶
- Parameters:
toWrite – 字典,键类型为 .QOpcUa.NodeAttribute,值类型为 QVariant。
valueAttributeType –
Types
- Return type:
布尔
执行对
toWrite中指定的属性和值的写操作。如果异步调用已成功分发,则返回
true。valueAttributeType参数可用于为值属性提供类型信息。所有其他属性都有已知的类型。另请参阅
- writeValueAttribute(value[, type=QOpcUa.Types.Undefined])¶
- Parameters:
value – 对象
type –
类型
- Return type:
布尔
使用来自
type的类型信息将value写入节点的Value属性。如果异步调用已成功分发,则返回
true。另请参阅