| 1 | // Copyright (C) 2017 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qopen62541backend.h" |
| 5 | #include "qopen62541node.h" |
| 6 | #include "qopen62541utils.h" |
| 7 | #include "qopen62541valueconverter.h" |
| 8 | |
| 9 | #include <private/qopcuahistoryreadresponse_p.h> |
| 10 | |
| 11 | #include <QtCore/qdatetime.h> |
| 12 | #include <QtCore/qstring.h> |
| 13 | #include <QtCore/qlist.h> |
| 14 | |
| 15 | QT_BEGIN_NAMESPACE |
| 16 | |
| 17 | QOpen62541Node::QOpen62541Node(const UA_NodeId nodeId, QOpen62541Client *client, const QString nodeIdString) |
| 18 | : m_client(client) |
| 19 | , m_nodeIdString(nodeIdString) |
| 20 | , m_nodeId(nodeId) |
| 21 | { |
| 22 | bool success = m_client->registerNode(obj: this); |
| 23 | setRegistered(success); |
| 24 | } |
| 25 | |
| 26 | QOpen62541Node::~QOpen62541Node() |
| 27 | { |
| 28 | if (m_client) |
| 29 | m_client->unregisterNode(obj: this); |
| 30 | |
| 31 | UA_NodeId_clear(p: &m_nodeId); |
| 32 | } |
| 33 | |
| 34 | bool QOpen62541Node::readAttributes(QOpcUa::NodeAttributes attr, const QString &indexRange) |
| 35 | { |
| 36 | if (!m_client) |
| 37 | return false; |
| 38 | |
| 39 | UA_NodeId tempId; |
| 40 | UA_NodeId_copy(src: &m_nodeId, dst: &tempId); |
| 41 | return QMetaObject::invokeMethod(object: m_client->m_backend, function: &Open62541AsyncBackend::readAttributes, |
| 42 | type: Qt::QueuedConnection, args: handle(), args&: tempId, args&: attr, args: indexRange); |
| 43 | } |
| 44 | |
| 45 | bool QOpen62541Node::enableMonitoring(QOpcUa::NodeAttributes attr, const QOpcUaMonitoringParameters &settings) |
| 46 | { |
| 47 | if (!m_client) |
| 48 | return false; |
| 49 | |
| 50 | UA_NodeId tempId; |
| 51 | UA_NodeId_copy(src: &m_nodeId, dst: &tempId); |
| 52 | return QMetaObject::invokeMethod(object: m_client->m_backend, function: &Open62541AsyncBackend::enableMonitoring, |
| 53 | type: Qt::QueuedConnection, args: handle(), args&: tempId, args&: attr, args: settings); |
| 54 | } |
| 55 | |
| 56 | bool QOpen62541Node::disableMonitoring(QOpcUa::NodeAttributes attr) |
| 57 | { |
| 58 | if (!m_client) |
| 59 | return false; |
| 60 | |
| 61 | return QMetaObject::invokeMethod(object: m_client->m_backend, function: &Open62541AsyncBackend::disableMonitoring, |
| 62 | type: Qt::QueuedConnection, args: handle(), args&: attr); |
| 63 | } |
| 64 | |
| 65 | bool QOpen62541Node::modifyMonitoring(QOpcUa::NodeAttribute attr, QOpcUaMonitoringParameters::Parameter item, const QVariant &value) |
| 66 | { |
| 67 | if (!m_client) |
| 68 | return false; |
| 69 | |
| 70 | return QMetaObject::invokeMethod(object: m_client->m_backend, function: &Open62541AsyncBackend::modifyMonitoring, |
| 71 | type: Qt::QueuedConnection, args: handle(), args&: attr, args&: item, args: value); |
| 72 | } |
| 73 | |
| 74 | QString QOpen62541Node::nodeId() const |
| 75 | { |
| 76 | return m_nodeIdString; |
| 77 | } |
| 78 | |
| 79 | bool QOpen62541Node::browse(const QOpcUaBrowseRequest &request) |
| 80 | { |
| 81 | if (!m_client) |
| 82 | return false; |
| 83 | |
| 84 | UA_NodeId tempId; |
| 85 | UA_NodeId_copy(src: &m_nodeId, dst: &tempId); |
| 86 | return QMetaObject::invokeMethod(object: m_client->m_backend, function: &Open62541AsyncBackend::browse, |
| 87 | type: Qt::QueuedConnection, args: handle(), args&: tempId, args: request); |
| 88 | } |
| 89 | |
| 90 | bool QOpen62541Node::writeAttribute(QOpcUa::NodeAttribute attribute, const QVariant &value, QOpcUa::Types type, const QString &indexRange) |
| 91 | { |
| 92 | if (!m_client) |
| 93 | return false; |
| 94 | |
| 95 | UA_NodeId tempId; |
| 96 | UA_NodeId_copy(src: &m_nodeId, dst: &tempId); |
| 97 | return QMetaObject::invokeMethod(object: m_client->m_backend, function: &Open62541AsyncBackend::writeAttribute, |
| 98 | type: Qt::QueuedConnection, args: handle(), args&: tempId, args&: attribute, args: value, args&: type, args: indexRange); |
| 99 | } |
| 100 | |
| 101 | bool QOpen62541Node::writeAttributes(const QOpcUaNode::AttributeMap &toWrite, QOpcUa::Types valueAttributeType) |
| 102 | { |
| 103 | if (!m_client) |
| 104 | return false; |
| 105 | |
| 106 | UA_NodeId tempId; |
| 107 | UA_NodeId_copy(src: &m_nodeId, dst: &tempId); |
| 108 | return QMetaObject::invokeMethod(object: m_client->m_backend, function: &Open62541AsyncBackend::writeAttributes, |
| 109 | type: Qt::QueuedConnection, args: handle(), args&: tempId, args: toWrite, args&: valueAttributeType); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | bool QOpen62541Node::callMethod(const QString &methodNodeId, const QList<QOpcUa::TypedVariant> &args) |
| 114 | |
| 115 | { |
| 116 | if (!m_client) |
| 117 | return false; |
| 118 | |
| 119 | UA_NodeId obj; |
| 120 | UA_NodeId_copy(src: &m_nodeId, dst: &obj); |
| 121 | return QMetaObject::invokeMethod(object: m_client->m_backend, function: &Open62541AsyncBackend::callMethod, |
| 122 | type: Qt::QueuedConnection, args: handle(), args&: obj, |
| 123 | args: Open62541Utils::nodeIdFromQString(name: methodNodeId), args); |
| 124 | } |
| 125 | |
| 126 | QOpcUaHistoryReadResponse *QOpen62541Node::readHistoryRaw(const QDateTime &startTime, const QDateTime &endTime, |
| 127 | quint32 numValues, bool returnBounds, |
| 128 | QOpcUa::TimestampsToReturn timestampsToReturn) |
| 129 | { |
| 130 | if (!m_client) |
| 131 | return nullptr; |
| 132 | |
| 133 | QOpcUaHistoryReadRawRequest request({ QOpcUaReadItem(m_nodeIdString) }, startTime, endTime, timestampsToReturn); |
| 134 | request.setNumValuesPerNode(numValues); |
| 135 | request.setReturnBounds(returnBounds); |
| 136 | return m_client->readHistoryData(request); |
| 137 | } |
| 138 | |
| 139 | QOpcUaHistoryReadResponse *QOpen62541Node::readHistoryEvents(const QDateTime &startTime, const QDateTime &endTime, |
| 140 | const QOpcUaMonitoringParameters::EventFilter &filter, quint32 numValues) |
| 141 | { |
| 142 | if (!m_client) |
| 143 | return nullptr; |
| 144 | |
| 145 | QOpcUaHistoryReadEventRequest request({QOpcUaReadItem(m_nodeIdString)}, |
| 146 | startTime, endTime, filter); |
| 147 | request.setNumValuesPerNode(numValues); |
| 148 | return m_client->readHistoryEvents(request); |
| 149 | } |
| 150 | |
| 151 | bool QOpen62541Node::resolveBrowsePath(const QList<QOpcUaRelativePathElement> &path) |
| 152 | { |
| 153 | if (!m_client) |
| 154 | return false; |
| 155 | |
| 156 | UA_NodeId start; |
| 157 | UA_NodeId_copy(src: &m_nodeId, dst: &start); |
| 158 | |
| 159 | return QMetaObject::invokeMethod(object: m_client->m_backend, function: &Open62541AsyncBackend::resolveBrowsePath, |
| 160 | type: Qt::QueuedConnection, args: handle(), args&: start, args: path); |
| 161 | } |
| 162 | |
| 163 | QT_END_NAMESPACE |
| 164 | |