open62541pp 0.19.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
subscription.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <type_traits>
4#include <utility> // move
5#include <vector>
6
7#include "open62541pp/common.hpp" // AttributeId
8#include "open62541pp/config.hpp"
12#include "open62541pp/types.hpp"
13#include "open62541pp/ua/types.hpp" // IntegerId
14
15#ifdef UA_ENABLE_SUBSCRIPTIONS
16
17namespace opcua {
18
19class Server;
20
25
26/**
27 * High-level subscription class.
28 *
29 * The API is symmetric for both Server and Client, although servers don't use the subscription
30 * mechanism of OPC UA to transport notifications of data changes and events. Instead MonitoredItems
31 * are registered locally. Notifications are then forwarded to user-defined callbacks instead of a
32 * remote client. The `subscriptionId` for servers is always `0U`.
33 *
34 * @tparam Connection Server or Client
35 * @note Not all methods are available and implemented for servers.
36 *
37 * Use the free functions in the opcua::services namespace for more advanced usage:
38 * - @ref Subscription
39 * - @ref MonitoredItem
40 */
41template <typename Connection>
43public:
44 /// Create a new subscription.
45 /// The subscription is not automatically deleted by the destructor.
46 /// You must delete it manually with @ref deleteSubscription or services::deleteSubscription.
47 explicit Subscription(Connection& connection, const SubscriptionParameters& parameters = {});
48
49 /// Wrap an existing subscription.
50 /// The `subscriptionId` is ignored and set to `0U` for servers.
52 : connection_{&connection},
53 subscriptionId_{std::is_same_v<Connection, Server> ? 0U : subscriptionId} {}
54
55 /// Get the server/client instance.
56 Connection& connection() noexcept {
57 return *connection_;
58 }
59
60 /// Get the server/client instance.
61 const Connection& connection() const noexcept {
62 return *connection_;
63 }
64
65 /// Get the server-assigned identifier of this subscription.
66 IntegerId subscriptionId() const noexcept {
67 return subscriptionId_;
68 }
69
70 /// Get all local monitored items.
71 std::vector<MonitoredItem<Connection>> monitoredItems();
72
73 /// Modify this subscription.
74 /// @note Not implemented for Server.
75 /// @see services::modifySubscription
77 const auto response = services::modifySubscription(
78 connection(), subscriptionId(), parameters
79 );
80 response.responseHeader().serviceResult().throwIfBad();
81 }
82
83 /// Enable/disable publishing of notification messages.
84 /// @note Not implemented for Server.
85 /// @see services::setPublishingMode
86 void setPublishingMode(bool publishing) {
87 services::setPublishingMode(connection(), subscriptionId(), publishing).throwIfBad();
88 }
89
90 /// Create a monitored item for data change notifications.
92 const NodeId& id,
93 AttributeId attribute,
94 MonitoringMode monitoringMode,
95 const MonitoringParametersEx& parameters,
97 ) {
99 connection(),
101 {id, attribute},
102 monitoringMode,
103 parameters,
104 std::move(onDataChange),
105 {}
106 );
107 result.statusCode().throwIfBad();
108 return {connection(), subscriptionId(), result.monitoredItemId()};
109 }
110
111 /// Create a monitored item for data change notifications (default settings).
112 /// The monitoring mode is set to MonitoringMode::Reporting and the default open62541
113 /// MonitoringParametersEx are used.
115 const NodeId& id, AttributeId attribute, DataChangeNotificationCallback onDataChange
116 ) {
117 const MonitoringParametersEx parameters;
118 return subscribeDataChange(
119 id, attribute, MonitoringMode::Reporting, parameters, std::move(onDataChange)
120 );
121 }
122
123 /// Create a monitored item for event notifications.
124 /// @note Not implemented for Server.
126 const NodeId& id,
127 MonitoringMode monitoringMode,
128 const MonitoringParametersEx& parameters,
129 EventNotificationCallback onEvent // NOLINT(*-unnecessary-value-param), false positive?
130 ) {
131 const auto result = services::createMonitoredItemEvent(
132 connection(),
135 monitoringMode,
136 parameters,
137 std::move(onEvent)
138 );
139 result.statusCode().throwIfBad();
140 return {connection(), subscriptionId(), result.monitoredItemId()};
141 }
142
143 /// Create a monitored item for event notifications (default settings).
144 /// The monitoring mode is set to MonitoringMode::Reporting and the default open62541
145 /// MonitoringParametersEx are used.
146 /// @note Not implemented for Server.
148 const NodeId& id, const EventFilter& eventFilter, EventNotificationCallback onEvent
149 ) {
150 MonitoringParametersEx parameters;
151 parameters.filter = ExtensionObject(eventFilter);
152 return subscribeEvent(id, MonitoringMode::Reporting, parameters, std::move(onEvent));
153 }
154
155 /// Delete this subscription.
156 /// @note Not implemented for Server.
160
161private:
162 Connection* connection_;
163 IntegerId subscriptionId_{0U};
164};
165
166/* ---------------------------------------------------------------------------------------------- */
167
168/// @relates Subscription
169template <typename T>
170inline bool operator==(const Subscription<T>& lhs, const Subscription<T>& rhs) noexcept {
171 return (lhs.connection() == rhs.connection()) && (lhs.subscriptionId() == rhs.subscriptionId());
172}
173
174/// @relates Subscription
175template <typename T>
176inline bool operator!=(const Subscription<T>& lhs, const Subscription<T>& rhs) noexcept {
177 return !(lhs == rhs);
178}
179
180} // namespace opcua
181
182#endif
UA_ExtensionObject wrapper class.
Definition types.hpp:1742
High-level monitored item class.
UA_NodeId wrapper class.
Definition types.hpp:641
constexpr void throwIfBad() const
Throw a BadStatus exception if the status code is bad.
Definition types.hpp:82
High-level subscription class.
void deleteSubscription()
Delete this subscription.
IntegerId subscriptionId() const noexcept
Get the server-assigned identifier of this subscription.
MonitoredItem< Connection > subscribeDataChange(const NodeId &id, AttributeId attribute, MonitoringMode monitoringMode, const MonitoringParametersEx &parameters, DataChangeNotificationCallback onDataChange)
Create a monitored item for data change notifications.
const Connection & connection() const noexcept
Get the server/client instance.
void setSubscriptionParameters(const SubscriptionParameters &parameters)
Modify this subscription.
Connection & connection() noexcept
Get the server/client instance.
bool operator!=(const Subscription< T > &lhs, const Subscription< T > &rhs) noexcept
bool operator==(const Subscription< T > &lhs, const Subscription< T > &rhs) noexcept
MonitoredItem< Connection > subscribeDataChange(const NodeId &id, AttributeId attribute, DataChangeNotificationCallback onDataChange)
Create a monitored item for data change notifications (default settings).
std::vector< MonitoredItem< Connection > > monitoredItems()
Get all local monitored items.
Subscription(Connection &connection, const SubscriptionParameters &parameters={})
Create a new subscription.
MonitoredItem< Connection > subscribeEvent(const NodeId &id, const EventFilter &eventFilter, EventNotificationCallback onEvent)
Create a monitored item for event notifications (default settings).
void setPublishingMode(bool publishing)
Enable/disable publishing of notification messages.
MonitoredItem< Connection > subscribeEvent(const NodeId &id, MonitoringMode monitoringMode, const MonitoringParametersEx &parameters, EventNotificationCallback onEvent)
Create a monitored item for event notifications.
Subscription(Connection &connection, IntegerId subscriptionId) noexcept
Wrap an existing subscription.
UA_EventFilter wrapper class.
Definition types.hpp:1891
const StatusCode & statusCode() const noexcept
Definition types.hpp:1997
MonitoredItemCreateResult createMonitoredItemEvent(Client &connection, IntegerId subscriptionId, const ReadValueId &itemToMonitor, MonitoringMode monitoringMode, const MonitoringParametersEx &parameters, EventNotificationCallback eventCallback, DeleteMonitoredItemCallback deleteCallback={})
Create and add a monitored item to a subscription for event notifications.
MonitoredItemCreateResult createMonitoredItemDataChange(T &connection, IntegerId subscriptionId, const ReadValueId &itemToMonitor, MonitoringMode monitoringMode, const MonitoringParametersEx &parameters, DataChangeNotificationCallback dataChangeCallback, DeleteMonitoredItemCallback deleteCallback)
Create and add a monitored item to a subscription for data change notifications.
std::function< void(IntegerId subId, IntegerId monId, Span< const Variant > eventFields)> EventNotificationCallback
Event notification callback.
std::function< void(IntegerId subId, IntegerId monId, const DataValue &value)> DataChangeNotificationCallback
Data change notification callback.
StatusCode deleteSubscription(Client &connection, IntegerId subscriptionId) noexcept
Delete a single subscription.
ModifySubscriptionResponse modifySubscription(Client &connection, const ModifySubscriptionRequest &request) noexcept
Modify a subscription.
SetPublishingModeResponse setPublishingMode(Client &connection, const SetPublishingModeRequest &request) noexcept
Enable/disable publishing of notification messages of subscriptions.
MonitoringMode
Monitoring mode.
Definition types.hpp:1623
uint32_t IntegerId
IntegerId.
Definition types.hpp:84
services::DataChangeNotificationCallback DataChangeNotificationCallback
services::EventNotificationCallback EventNotificationCallback
AttributeId
Attribute identifiers.
Definition common.hpp:28
Extended monitoring parameters with default values from open62541.
ExtensionObject filter
Filter is used by the server to determine if the MonitoredItem should generate notifications.
Subscription parameters with default values from open62541.