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#ifndef QOPEN62541SUBSCRIPTION_H
5#define QOPEN62541SUBSCRIPTION_H
6
7#include "qopen62541.h"
8#include <QtOpcUa/qopcuanode.h>
9
10QT_BEGIN_NAMESPACE
11
12class Open62541AsyncBackend;
13
14class QOpen62541Subscription : public QObject
15{
16 Q_OBJECT
17
18public:
19 QOpen62541Subscription(Open62541AsyncBackend *backend, const QOpcUaMonitoringParameters &settings);
20 ~QOpen62541Subscription();
21
22 UA_UInt32 createOnServer();
23 bool removeOnServer();
24
25 void modifyMonitoring(quint64 handle, QOpcUa::NodeAttribute attr, QOpcUaMonitoringParameters::Parameter item, QVariant value);
26
27 bool addAttributeMonitoredItem(quint64 handle, QOpcUa::NodeAttribute attr, const UA_NodeId &id, QOpcUaMonitoringParameters settings);
28 bool removeAttributeMonitoredItem(quint64 handle, QOpcUa::NodeAttribute attr);
29
30 void monitoredValueUpdated(UA_UInt32 monId, UA_DataValue *value);
31 void eventReceived(UA_UInt32 monId, QVariantList list);
32
33 void sendTimeoutNotification();
34
35 struct MonitoredItem {
36 quint64 handle;
37 QOpcUa::NodeAttribute attr;
38 UA_UInt32 monitoredItemId;
39 UA_UInt32 clientHandle;
40 QOpcUaMonitoringParameters parameters;
41 MonitoredItem(quint64 h, QOpcUa::NodeAttribute a, UA_UInt32 id)
42 : handle(h)
43 , attr(a)
44 , monitoredItemId(id)
45 {}
46 MonitoredItem()
47 : handle(0)
48 , monitoredItemId(0)
49 {}
50 };
51
52 double interval() const;
53 UA_UInt32 subscriptionId() const;
54 int monitoredItemsCount() const;
55
56 QOpcUaMonitoringParameters::SubscriptionType shared() const;
57
58signals:
59 void timeout(QOpen62541Subscription *sub, QList<QPair<quint64, QOpcUa::NodeAttribute>> items);
60
61private:
62 MonitoredItem *getItemForAttribute(quint64 nodeHandle, QOpcUa::NodeAttribute attr);
63 UA_ExtensionObject createFilter(const QVariant &filterData);
64 void createDataChangeFilter(const QOpcUaMonitoringParameters::DataChangeFilter &filter, UA_ExtensionObject *out);
65 void createEventFilter(const QOpcUaMonitoringParameters::EventFilter &filter, UA_ExtensionObject *out);
66 bool convertSelectClause(const QOpcUaMonitoringParameters::EventFilter &filter,
67 UA_SimpleAttributeOperand **selectClauses, size_t *size);
68 bool convertWhereClause(const QOpcUaMonitoringParameters::EventFilter &filter, UA_ContentFilter *result);
69
70 bool modifySubscriptionParameters(quint64 nodeHandle, QOpcUa::NodeAttribute attr, const QOpcUaMonitoringParameters::Parameter &item, const QVariant &value);
71 bool modifyMonitoredItemParameters(quint64 nodeHandle, QOpcUa::NodeAttribute attr, const QOpcUaMonitoringParameters::Parameter &item, const QVariant &value);
72 QOpcUaEventFilterResult convertEventFilterResult(UA_ExtensionObject *obj);
73
74 Open62541AsyncBackend *m_backend;
75 double m_interval;
76 UA_UInt32 m_subscriptionId;
77 UA_UInt32 m_lifetimeCount;
78 UA_UInt32 m_maxKeepaliveCount;
79 QOpcUaMonitoringParameters::SubscriptionType m_shared;
80 quint8 m_priority;
81 quint32 m_maxNotificationsPerPublish;
82
83 QHash<quint64, QHash<QOpcUa::NodeAttribute, MonitoredItem *>> m_nodeHandleToItemMapping; // Handle -> Attribute -> MonitoredItem
84 QHash<UA_UInt32, MonitoredItem *> m_itemIdToItemMapping; // ItemId -> Item for fast lookup on data change
85
86 quint32 m_clientHandle;
87 bool m_timeout;
88};
89
90QT_END_NAMESPACE
91
92#endif // QOPEN62541SUBSCRIPTION_H
93

source code of qtopcua/src/plugins/opcua/open62541/qopen62541subscription.h