open62541pp 0.16.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
subscription.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <type_traits>
5#include <utility> // move
6#include <vector>
7
8#include "open62541pp/common.hpp" // AttributeId, MonitoringMode
9#include "open62541pp/config.hpp"
13#include "open62541pp/types.hpp"
14
15#ifdef UA_ENABLE_SUBSCRIPTIONS
16
17namespace opcua {
18
19class EventFilter;
20class Server;
21
26
27/**
28 * High-level subscription class.
29 *
30 * The API is symmetric for both Server and Client, although servers don't use the subscription
31 * mechanism of OPC UA to transport notifications of data changes and events. Instead MonitoredItems
32 * are registered locally. Notifications are then forwarded to user-defined callbacks instead of a
33 * remote client. The `subscriptionId` for servers is always `0U`.
34 *
35 * @tparam Connection Server or Client
36 * @note Not all methods are available and implemented for servers.
37 *
38 * Use the free functions in the opcua::services namespace for more advanced usage:
39 * - @ref Subscription
40 * - @ref MonitoredItem
41 */
42template <typename Connection>
44public:
45 /// Wrap an existing subscription.
46 /// The `subscriptionId` is ignored and set to `0U` for servers.
47 Subscription(Connection& connection, uint32_t subscriptionId) noexcept
48 : connection_(&connection),
49 subscriptionId_(std::is_same_v<Connection, Server> ? 0U : subscriptionId) {}
50
51 /// Get the server/client instance.
52 Connection& connection() noexcept {
53 return *connection_;
54 }
55
56 /// Get the server/client instance.
57 const Connection& connection() const noexcept {
58 return *connection_;
59 }
60
61 /// Get the server-assigned identifier of this subscription.
62 uint32_t subscriptionId() const noexcept {
63 return subscriptionId_;
64 }
65
66 /// Get all local monitored items.
67 std::vector<MonitoredItem<Connection>> getMonitoredItems();
68
69 /// Modify this subscription.
70 /// @note Not implemented for Server.
71 /// @see services::modifySubscription
73 const auto response = services::modifySubscription(
74 connection(), subscriptionId(), parameters
75 );
76 response.getResponseHeader().getServiceResult().throwIfBad();
77 }
78
79 /// Enable/disable publishing of notification messages.
80 /// @note Not implemented for Server.
81 /// @see services::setPublishingMode
82 void setPublishingMode(bool publishing) {
83 services::setPublishingMode(connection(), subscriptionId(), publishing).throwIfBad();
84 }
85
86 /// Create a monitored item for data change notifications.
88 const NodeId& id,
89 AttributeId attribute,
90 MonitoringMode monitoringMode,
91 const MonitoringParametersEx& parameters,
93 ) {
95 connection(),
97 {id, attribute},
98 monitoringMode,
99 parameters,
100 std::move(onDataChange),
101 {}
102 );
103 result.getStatusCode().throwIfBad();
104 return {connection(), subscriptionId(), result.getMonitoredItemId()};
105 }
106
107 /// Create a monitored item for data change notifications (default settings).
108 /// The monitoring mode is set to MonitoringMode::Reporting and the default open62541
109 /// MonitoringParametersEx are used.
111 const NodeId& id, AttributeId attribute, DataChangeNotificationCallback onDataChange
112 ) {
113 const MonitoringParametersEx parameters;
114 return subscribeDataChange(
115 id, attribute, MonitoringMode::Reporting, parameters, std::move(onDataChange)
116 );
117 }
118
119 /// Create a monitored item for event notifications.
120 /// @note Not implemented for Server.
122 const NodeId& id,
123 MonitoringMode monitoringMode,
124 const MonitoringParametersEx& parameters,
125 EventNotificationCallback onEvent // NOLINT(*-unnecessary-value-param), false positive?
126 ) {
127 const auto result = services::createMonitoredItemEvent(
128 connection(),
131 monitoringMode,
132 parameters,
133 std::move(onEvent)
134 );
135 result.getStatusCode().throwIfBad();
136 return {connection(), subscriptionId(), result.getMonitoredItemId()};
137 }
138
139 /// Create a monitored item for event notifications (default settings).
140 /// The monitoring mode is set to MonitoringMode::Reporting and the default open62541
141 /// MonitoringParametersEx are used.
142 /// @note Not implemented for Server.
144 const NodeId& id, const EventFilter& eventFilter, EventNotificationCallback onEvent
145 ) {
146 MonitoringParametersEx parameters;
147 parameters.filter = ExtensionObject::fromDecodedCopy(eventFilter);
148 return subscribeEvent(id, MonitoringMode::Reporting, parameters, std::move(onEvent));
149 }
150
151 /// Delete this subscription.
152 /// @note Not implemented for Server.
156
157private:
158 Connection* connection_;
159 uint32_t subscriptionId_{0U};
160};
161
162/* ---------------------------------------------------------------------------------------------- */
163
164template <typename T>
165inline bool operator==(const Subscription<T>& lhs, const Subscription<T>& rhs) noexcept {
166 return (lhs.connection() == rhs.connection()) && (lhs.subscriptionId() == rhs.subscriptionId());
167}
168
169template <typename T>
170inline bool operator!=(const Subscription<T>& lhs, const Subscription<T>& rhs) noexcept {
171 return !(lhs == rhs);
172}
173
174} // namespace opcua
175
176#endif
UA_EventFilter wrapper class.
static ExtensionObject fromDecodedCopy(const T &data)
Create an ExtensionObject from a decoded object (copy).
Definition types.hpp:1693
const StatusCode & getStatusCode() const noexcept
High-level monitored item class.
UA_NodeId wrapper class.
Definition types.hpp:590
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.
MonitoredItem< Connection > subscribeDataChange(const NodeId &id, AttributeId attribute, MonitoringMode monitoringMode, const MonitoringParametersEx &parameters, DataChangeNotificationCallback onDataChange)
Create a monitored item for data change notifications.
uint32_t subscriptionId() const noexcept
Get the server-assigned identifier of this subscription.
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.
MonitoredItem< Connection > subscribeDataChange(const NodeId &id, AttributeId attribute, DataChangeNotificationCallback onDataChange)
Create a monitored item for data change notifications (default settings).
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.
Subscription(Connection &connection, uint32_t subscriptionId) noexcept
Wrap an existing subscription.
MonitoredItem< Connection > subscribeEvent(const NodeId &id, MonitoringMode monitoringMode, const MonitoringParametersEx &parameters, EventNotificationCallback onEvent)
Create a monitored item for event notifications.
std::vector< MonitoredItem< Connection > > getMonitoredItems()
Get all local monitored items.
MonitoredItemCreateResult createMonitoredItemDataChange(T &connection, uint32_t 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(uint32_t subId, uint32_t monId, const DataValue &value)> DataChangeNotificationCallback
Data change notification callback.
MonitoredItemCreateResult createMonitoredItemEvent(Client &connection, uint32_t 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.
std::function< void(uint32_t subId, uint32_t monId, Span< const Variant > eventFields)> EventNotificationCallback
Event notification callback.
StatusCode deleteSubscription(Client &connection, uint32_t 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 common.hpp:302
services::EventNotificationCallback EventNotificationCallback
services::DataChangeNotificationCallback DataChangeNotificationCallback
bool operator!=(const Client &lhs, const Client &rhs) noexcept
Definition client.hpp:295
AttributeId
Attribute identifiers.
Definition common.hpp:28
@ EventNotifier
Indicates if the node can be used to subscribe to events or the read/write historic events.
bool operator==(const Client &lhs, const Client &rhs) noexcept
Definition client.hpp:291
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.