1// Copyright (C) 2018 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#pragma once
5
6#include <private/opcuareaditem_p.h>
7#include <private/opcuaserverdiscovery_p.h>
8
9#include <QJSValue>
10#include <QObject>
11#include <QOpcUaClient>
12#include <QOpcUaAuthenticationInformation>
13
14#include <QtQml/qqml.h>
15
16//
17// W A R N I N G
18// -------------
19//
20// This file is not part of the Qt API. It exists purely as an
21// implementation detail. This header file may change from version to
22// version without notice, or even be removed.
23//
24// We mean it.
25//
26
27QT_BEGIN_NAMESPACE
28
29class QOpcUaReadResult;
30class OpcUaEndpointDiscovery;
31
32class OpcUaConnection : public QObject
33{
34 Q_OBJECT
35 Q_DISABLE_COPY(OpcUaConnection)
36 Q_PROPERTY(QStringList availableBackends READ availableBackends NOTIFY availableBackendsChanged)
37 Q_PROPERTY(bool connected READ connected NOTIFY connectedChanged)
38 Q_PROPERTY(QString backend READ backend WRITE setBackend NOTIFY backendChanged)
39 Q_PROPERTY(bool defaultConnection READ defaultConnection WRITE setDefaultConnection NOTIFY defaultConnectionChanged)
40 Q_PROPERTY(QStringList namespaces READ namespaces NOTIFY namespacesChanged)
41 Q_PROPERTY(QOpcUaAuthenticationInformation authenticationInformation READ authenticationInformation WRITE setAuthenticationInformation)
42 Q_PROPERTY(QStringList supportedSecurityPolicies READ supportedSecurityPolicies CONSTANT)
43 Q_PROPERTY(QJSValue supportedUserTokenTypes READ supportedUserTokenTypes CONSTANT)
44 Q_PROPERTY(QOpcUaEndpointDescription currentEndpoint READ currentEndpoint)
45 Q_PROPERTY(QOpcUaClient* connection READ connection WRITE setConnection NOTIFY connectionChanged)
46 QML_NAMED_ELEMENT(Connection)
47 QML_ADDED_IN_VERSION(5, 12)
48
49public:
50 OpcUaConnection(QObject *parent = nullptr);
51 ~OpcUaConnection();
52 QStringList availableBackends() const;
53 bool connected() const;
54 void setBackend(const QString &name);
55 QString backend() const;
56 static OpcUaConnection *defaultConnection();
57 bool isDefaultConnection() const;
58 QStringList namespaces() const;
59
60 QOpcUaEndpointDescription currentEndpoint() const;
61
62 QOpcUaAuthenticationInformation authenticationInformation() const;
63 Q_INVOKABLE bool readNodeAttributes(const QJSValue &value);
64 Q_INVOKABLE bool writeNodeAttributes(const QJSValue &value);
65
66 QStringList supportedSecurityPolicies() const;
67 QJSValue supportedUserTokenTypes() const;
68
69 QOpcUaClient *connection() const;
70
71public slots:
72 void connectToEndpoint(const QOpcUaEndpointDescription &endpointDescription);
73 void disconnectFromEndpoint();
74 void setDefaultConnection(bool defaultConnection = true);
75 void setAuthenticationInformation(const QOpcUaAuthenticationInformation &authenticationInformation);
76 void setConnection(QOpcUaClient *client);
77
78signals:
79 void availableBackendsChanged();
80 void connectedChanged();
81 void backendChanged();
82 void defaultConnectionChanged();
83 void namespacesChanged();
84 void readNodeAttributesFinished(const QVariant &value);
85 void writeNodeAttributesFinished(const QVariant &value);
86 void connectionChanged();
87
88private slots:
89 void clientStateHandler(QOpcUaClient::ClientState state);
90 void handleReadNodeAttributesFinished(const QList<QOpcUaReadResult> &results);
91 void handleWriteNodeAttributesFinished(const QList<QOpcUaWriteResult> &results);
92
93private:
94 void removeConnection();
95 void setupConnection();
96
97 QOpcUaClient *m_client = nullptr;
98 bool m_connected = false;
99 static OpcUaConnection* m_defaultConnection;
100
101friend class OpcUaNode;
102friend class OpcUaValueNode;
103friend class OpcUaMethodNode;
104friend class OpcUaEndpointDiscovery;
105friend class OpcUaServerDiscovery;
106};
107
108QT_END_NAMESPACE
109

source code of qtopcua/src/declarative_opcua/opcuaconnection_p.h