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