summaryrefslogtreecommitdiffstats
path: root/src/declarative_opcua/opcuamethodnode.cpp
blob: 67285494819ce6105efb4721e07f8ee4b0110a3c (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include <private/opcuamethodnode_p.h>
#include <private/opcuanodeidtype_p.h>

#include <QOpcUaNode>
#include <QLoggingCategory>

QT_BEGIN_NAMESPACE

/*!
    \qmltype MethodNode
    \inqmlmodule QtOpcUa
    \brief Calls a method on the server.
    \since QtOpcUa 5.12
    \inherits Node

    This QML element supports calling method nodes on a server.
    The target object node ID has to be specified by the \l objectNodeId property.

    \code
    import QtOpcUa as QtOpcUa

    QtOpcUa.MethodNode {
        nodeId : QtOpcUa.NodeId {
            identifier: "s=Example.Method"
            ns: "Example Namespace"
        }
        objectNodeId : QtOpcUa.NodeId {
            identifier: "s=Example.Object"
            ns: "Example Namespace"
        }
        connection: myConnection
     }
     \endcode

     The actual function call can be triggered by a signal.

     \code
     Button {
           text: "Start"
           onClicked: myNode.callMethod
     }
     \endcode

     or by JavaScript

     \code
          myNode.callMethod()
     \endcode
*/

/*!
    \qmlmethod MethodNode::callMethod

    Calls the method on the connected server.
*/

/*!
    \qmlproperty OpcUaNode MethodNode::objectNodeId

    Determines the actual node on which the method is called.
    It can be a relative or absolute node Id.
*/

/*!
    \qmlproperty Status MethodNode::resultStatus
    \readonly

    Status of the last method call. This property has to be checked
    to determine if the method call was successful.

    \sa Status
*/

/*!
    \qmlproperty list<MethodArgument> MethodNode::inputArguments

    Arguments to be used when calling the method on the server.

    This example shows how to call a method with two double arguments.
    \code
    QtOpcUa.MethodNode {
        ...
        inputArguments: [
            QtOpcUa.MethodArgument {
                value: 3
                type: QtOpcUa.Constants.Double
            },
            QtOpcUa.MethodArgument {
                value: 4
                type: QtOpcUa.Constants.Double
            }
        ]
    }
    \endcode

    \sa callMethod
*/

/*!
    \qmlproperty list<var> MethodNode::outputArguments
    \readonly

    Returns values from the method call. Depending on the output arguments,
    this list may contain zero or more values. The \l resultStatus has to be checked
    separately. In case the method call failed, the list will be empty.

    \code
    if (node.status.isGood) {
        // print two arguments
        console.log("Number of return values:", node.outputArguments.length)
        console.log("Return value #1:", node.outputArguments[0])
        console.log("Return value #2:", node.outputArguments[1])
    }
    \endcode

    \sa callMethod, resultStatus
*/

Q_DECLARE_LOGGING_CATEGORY(QT_OPCUA_PLUGINS_QML)

OpcUaMethodNode::OpcUaMethodNode(QObject *parent):
    OpcUaNode(parent)
{
}

OpcUaNodeIdType *OpcUaMethodNode::objectNodeId() const
{
    return m_objectNodeId;
}

QQmlListProperty<OpcUaMethodArgument> OpcUaMethodNode::inputArguments()
{
    return QQmlListProperty<OpcUaMethodArgument>(this,
        &m_inputArguments,
        &OpcUaMethodNode::appendArgument,
        &OpcUaMethodNode::argumentCount,
        &OpcUaMethodNode::argument,
        &OpcUaMethodNode::clearArguments);
}

QVariantList OpcUaMethodNode::outputArguments()
{
    return m_outputArguments;
}

void OpcUaMethodNode::setObjectNodeId(OpcUaNodeIdType *node)
{
    if (m_objectNodeId)
        disconnect(m_objectNodeId);

    m_objectNodeId = node;
    connect(m_objectNodeId, &OpcUaNodeIdType::nodeChanged, this, &OpcUaMethodNode::handleObjectNodeIdChanged);
    handleObjectNodeIdChanged();
}

void OpcUaMethodNode::callMethod()
{
    if (!m_objectNode) {
        qCWarning(QT_OPCUA_PLUGINS_QML) << "No object node";
        setStatus(Status::InvalidObjectNode);
        return;
    }
    if (!m_objectNode->node()) {
        qCWarning(QT_OPCUA_PLUGINS_QML) << "Invalid object node";
        setStatus(Status::InvalidObjectNode);
        return;
    }
    if (!m_node) {
        qCWarning(QT_OPCUA_PLUGINS_QML) << "Invalid node Id";
        setStatus(Status::InvalidNodeId);
        return;
    }

    QList<QOpcUa::TypedVariant> arguments;
    for (const auto item : std::as_const(m_inputArguments))
        arguments.push_back(QOpcUa::TypedVariant(item->value(), item->type()));
    m_objectNode->node()->callMethod(m_node->nodeId(), arguments);
}

void OpcUaMethodNode::handleObjectNodeIdChanged()
{
    if (m_objectNode)
        m_objectNode->deleteLater();
    m_objectNode = new OpcUaNode(this);
    m_objectNode->setNodeId(m_objectNodeId);
    connect(m_objectNode, &OpcUaNode::readyToUseChanged, this, [this](){
        connect(m_objectNode->node(), &QOpcUaNode::methodCallFinished, this, &OpcUaMethodNode::handleMethodCallFinished, Qt::UniqueConnection);
        checkValidity();
    });

    emit objectNodeIdChanged();
}

void OpcUaMethodNode::handleMethodCallFinished(QString methodNodeId, QVariant result, QOpcUa::UaStatusCode statusCode)
{
    Q_UNUSED(methodNodeId);
    m_resultStatus = OpcUaStatus(statusCode);

    m_outputArguments.clear();
    if (result.canConvert<QVariantList>())
        m_outputArguments = result.value<QVariantList>();
    else
        m_outputArguments.append(result);
    emit resultStatusChanged(m_resultStatus);
    emit outputArgumentsChanged();
}

void OpcUaMethodNode::setupNode(const QString &absolutePath)
{
    OpcUaNode::setupNode(absolutePath);
}

bool OpcUaMethodNode::checkValidity()
{
    if (m_node->attribute(QOpcUa::NodeAttribute::NodeClass).value<QOpcUa::NodeClass>() != QOpcUa::NodeClass::Method) {
        setStatus(Status::InvalidNodeType);
        return false;
    }
    if (!m_objectNode || !m_objectNode->node()) {
        setStatus(Status::InvalidObjectNode);
        return false;
    }

    const auto objectNodeClass = m_objectNode->node()->attribute(QOpcUa::NodeAttribute::NodeClass).value<QOpcUa::NodeClass>();
    if (objectNodeClass != QOpcUa::NodeClass::Object && objectNodeClass != QOpcUa::NodeClass::ObjectType) {
        setStatus(Status::InvalidObjectNode, tr("Object node is not of type `Object' or `ObjectType'"));
        return false;
    }

    setStatus(Status::Valid);

    return true;
}

OpcUaStatus OpcUaMethodNode::resultStatus() const
{
    return m_resultStatus;
}

void OpcUaMethodNode::appendArgument(QQmlListProperty<OpcUaMethodArgument>* list, OpcUaMethodArgument* p) {
    reinterpret_cast< QList<OpcUaMethodArgument*>* >(list->data)->append(p);
}

void OpcUaMethodNode::clearArguments(QQmlListProperty<OpcUaMethodArgument>* list) {
    reinterpret_cast< QList<OpcUaMethodArgument*>* >(list->data)->clear();
}

OpcUaMethodArgument* OpcUaMethodNode::argument(QQmlListProperty<OpcUaMethodArgument>* list, qsizetype i) {
    return reinterpret_cast< QList<OpcUaMethodArgument*>* >(list->data)->at(i);
}

qsizetype OpcUaMethodNode::argumentCount(QQmlListProperty<OpcUaMethodArgument>* list) {
    return reinterpret_cast< QList<OpcUaMethodArgument*>* >(list->data)->size();
}


QT_END_NAMESPACE