summaryrefslogtreecommitdiffstats
path: root/src/imports/opcua/opcuapathresolver.cpp
blob: 95b09e94d09fd9a657c8e47d8542d23cf9a0bc7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt OPC UA module.
**
** $QT_BEGIN_LICENSE:LGPL3$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or later as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information to
** ensure the GNU General Public License version 2.0 requirements will be
** met: http://www.gnu.org/licenses/gpl-2.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "opcuapathresolver.h"
#include "opcuarelativenodeid.h"
#include "opcuarelativenodepath.h"
#include <QOpcUaClient>
#include <QMetaEnum>
#include <QLoggingCategory>

QT_BEGIN_NAMESPACE

/*!
    \class OpcUaPathResolver
    \inqmlmodule QtOpcUa
    \internal
    \brief This class resolves relative nodes.

    This class is used to resolve relative node IDs. It will emit \c resolvedNode
    with the result and delete itself afterwards.
    In case of errors the resolved node is empty and the error message is set.

    This class is capable of resolving cascaded relative nodes by recursively instantiating
    further resolvers. The maximum recursion depth is 50.

    \sa RelativeNodeId, Node
*/
const int maxRecursionDepth = 50;
Q_DECLARE_LOGGING_CATEGORY(QT_OPCUA_PLUGINS_QML)

OpcUaPathResolver::OpcUaPathResolver(OpcUaRelativeNodeId *relativeNode, QOpcUaClient *client, QObject *target)
    : QObject(target)
    , m_level(0)
    , m_relativeNode(relativeNode)
    , m_target(target)
    , m_client(client)
    , m_node(nullptr)
{
}

OpcUaPathResolver::OpcUaPathResolver(int level, OpcUaRelativeNodeId *relativeNode, QOpcUaClient *client, QObject *target)
    : QObject(target)
    , m_level(level)
    , m_relativeNode(relativeNode)
    , m_target(target)
    , m_client(client)
    , m_node(nullptr)
{
}

OpcUaPathResolver::~OpcUaPathResolver()
{
    if (m_node) {
        m_node->deleteLater();
        m_node = nullptr;
    }
}

void OpcUaPathResolver::startResolving()
{
    if (!m_relativeNode || !m_client || !m_target) {
        emit resolvedNode(UniversalNode(), QLatin1String("Member has been deleted"));
        deleteLater();
        return;
    }

    auto startNode = m_relativeNode->startNode();
    if (!startNode) {
        emit resolvedNode(UniversalNode(), QLatin1String("Aborted resolving because start node not present"));
        deleteLater();
        return;
    }

    if (m_relativeNode->pathCount() == 0) {
        emit resolvedNode(UniversalNode(), QLatin1String("Skipping to resolve relative node with empty path"));
        deleteLater();
        return;
    }

    if (qobject_cast<const OpcUaRelativeNodeId *>(startNode)) {
        // Trigger recursive resolving
        if (m_level >= maxRecursionDepth) {
            emit resolvedNode(UniversalNode(), QLatin1String("Maximum recursion depth reached during node resolution"));
            deleteLater();
            return;
        }
        auto node = qobject_cast<OpcUaRelativeNodeId *>(startNode);
        auto resolver = new OpcUaPathResolver(m_level + 1, node, m_client, this);
        connect(resolver, &OpcUaPathResolver::resolvedNode, this, &OpcUaPathResolver::startNodeResolved);
        resolver->startResolving();
        return;
    } else {
        startNodeResolved(startNode, QString());
    }
}

void OpcUaPathResolver::startNodeResolved(UniversalNode startNode, const QString &errorMessage)
{
    if (!m_relativeNode || !m_client || !m_target) {
        emit resolvedNode(UniversalNode(), QLatin1String("Member has been deleted"));
        deleteLater();
        return;
    }

    if (sender())
        sender()->deleteLater();

    if (!errorMessage.isEmpty()) {
        emit resolvedNode(startNode, errorMessage);
        deleteLater();
        return;
    }

    startNode.resolveNamespace(m_client);
    m_node = m_client->node(startNode.fullNodeId());
    if (!m_node) {
        emit resolvedNode(startNode, QStringLiteral("Could not create node from '%1'")
                          .arg(startNode.fullNodeId()));
        deleteLater();
        return;
    }

    // construct path vector
    QList<QOpcUaRelativePathElement> path;
    for (int i = 0; i < m_relativeNode->pathCount(); ++i)
        path.append(m_relativeNode->path(i)->toRelativePathElement(m_client));

    qCDebug(QT_OPCUA_PLUGINS_QML) << "Starting browse on" << m_node->nodeId();
    connect(m_node, &QOpcUaNode::resolveBrowsePathFinished, this, &OpcUaPathResolver::browsePathFinished);
    if (!m_node->resolveBrowsePath(path)) {
        emit resolvedNode(UniversalNode(), QStringLiteral("Failed to start browse"));
        deleteLater();
        return;
    }
}

void OpcUaPathResolver::browsePathFinished(QList<QOpcUaBrowsePathTarget> results, QList<QOpcUaRelativePathElement> path, QOpcUa::UaStatusCode status)
{
    Q_UNUSED(path);
    UniversalNode nodeToUse;

    if (status != QOpcUa::Good) {
        const char *name = QMetaEnum::fromType<QOpcUa::UaStatusCode>().valueToKey(status);
        emit resolvedNode(UniversalNode(),
                          QStringLiteral("Resolving browse path return error code %1").arg(name));
        deleteLater();
        return;
    }

    if (results.size() == 0) {
        emit resolvedNode(UniversalNode(),
                          QStringLiteral("Relative path could not be resolved: Results are empty"));
        deleteLater();
        return;
    } else if (results.size() == 1) {
        if (results.at(0).targetId().serverIndex() > 0) {
            emit resolvedNode(UniversalNode(),
                              QStringLiteral("Relative path could not be resolved: "
                                             "Resulting node is located on a remote server"));
            deleteLater();
            return;
        }
        nodeToUse.from(results.at(0));

    } else { // greater than one
        UniversalNode tmp;
        QString message = "No resolved node found";

        for (const auto &result : results) {
            if (result.isFullyResolved()) {
                if (result.targetId().serverIndex() > 0) {
                    message = QStringLiteral("Relative path could not be resolved: "
                                             "Resulting node is located on a remote server");
                    continue;
                }
                if (!tmp.nodeIdentifier().isEmpty()) {
                    emit resolvedNode(UniversalNode(), QLatin1String("There are multiple resolved nodes"));
                    deleteLater();
                    return;
                }
                tmp.from(result);
            }
        }

        if (!tmp.nodeIdentifier().isEmpty()) {
            nodeToUse = tmp;
        } else {
            emit resolvedNode(UniversalNode(), message);
            deleteLater();
            return;
        }
    }

    nodeToUse.resolveNamespace(m_client);
    qCDebug(QT_OPCUA_PLUGINS_QML) << "Relative node fully resolved to:" << nodeToUse.fullNodeId();
    emit resolvedNode(nodeToUse, QString());
    deleteLater();
}

QT_END_NAMESPACE