open62541pp 0.17.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
client_subscription.cpp
#include <chrono>
#include <iostream>
#include <thread>
int main() {
opcua::Client client;
// Add a state callback (session activated) to create subscription(s) and monitored items(s) in
// a newly activated session. If the client is disconnected, the subscriptions and monitored
// items are deleted. This approach with the state callback assures, that the subscriptions are
// recreated whenever the client reconnects to the server.
client.onSessionActivated([&] {
opcua::Subscription sub(client);
// Modify and delete the subscription via the returned Subscription<T> object
opcua::SubscriptionParameters subscriptionParameters{};
subscriptionParameters.publishingInterval = 1000.0;
sub.setSubscriptionParameters(subscriptionParameters);
sub.setPublishingMode(true);
// sub.deleteSubscription();
// Create a monitored item within the subscription for data change notifications
auto mon = sub.subscribeDataChange(
opcua::VariableId::Server_ServerStatus_CurrentTime, // monitored node id
opcua::AttributeId::Value, // monitored attribute
[&](opcua::IntegerId subId, opcua::IntegerId monId, const opcua::DataValue& dv) {
opcua::MonitoredItem item(client, subId, monId);
std::cout
<< "Data change notification:\n"
<< "- subscription id: " << item.subscriptionId() << "\n"
<< "- monitored item id: " << item.monitoredItemId() << "\n"
<< "- node id: " << item.nodeId().toString() << "\n"
<< "- attribute id: " << static_cast<int>(item.attributeId()) << "\n";
const auto dt = dv.value().scalar<opcua::DateTime>();
std::cout << "Current server time (UTC): " << dt.format("%H:%M:%S") << std::endl;
}
);
// Modify and delete the monitored item via the returned MonitoredItem<T> object
opcua::MonitoringParametersEx monitoringParameters{};
monitoringParameters.samplingInterval = 100.0;
mon.setMonitoringParameters(monitoringParameters);
mon.setMonitoringMode(opcua::MonitoringMode::Reporting);
// mon.deleteMonitoredItem();
});
// Endless loop to automatically (try to) reconnect to server.
while (true) {
try {
client.connect("opc.tcp://localhost:4840");
// Run the client's main loop to process callbacks and events.
// This will block until client.stop() is called or an exception is thrown.
client.run();
} catch (const opcua::BadStatus& e) {
// Workaround to enforce a new session
// https://github.com/open62541pp/open62541pp/issues/51
client.disconnect();
std::cout << "Error: " << e.what() << "\nRetry to connect in 3 seconds\n";
std::this_thread::sleep_for(std::chrono::seconds(3));
}
}
}
Exception for bad status codes from open62541 UA_STATUSCODE_*.
Definition exception.hpp:15
const char * what() const noexcept override
Definition exception.hpp:24
High-level client class.
Definition client.hpp:122
void disconnect()
Disconnect and close the connection to the server.
void connect(std::string_view endpointUrl)
Connect to the selected server.
void onSessionActivated(StateCallback callback)
Set a state callback that will be called after the session is activated.
void run()
Run the client's main loop by. This method will block until Client::stop is called.
UA_DataValue wrapper class.
Definition types.hpp:1832
Variant & value() &noexcept
Get value.
Definition types.hpp:1951
UA_DateTime wrapper class.
Definition types.hpp:390
std::string format(std::string_view format, bool localtime=false) const
Convert to string with given format (same format codes as strftime).
High-level monitored item class.
IntegerId subscriptionId() const noexcept
Get the server-assigned identifier of the underlying subscription.
const NodeId & nodeId()
Get the monitored NodeId.
IntegerId monitoredItemId() const noexcept
Get the server-assigned identifier of this monitored item.
AttributeId attributeId()
Get the monitored AttributeId.
std::string toString() const
Encode NodeId as a string like ns=1;s=SomeNode.
High-level subscription class.
MonitoredItem< Connection > subscribeDataChange(const NodeId &id, AttributeId attribute, MonitoringMode monitoringMode, const MonitoringParametersEx &parameters, DataChangeNotificationCallback onDataChange)
Create a monitored item for data change notifications.
void setSubscriptionParameters(const SubscriptionParameters &parameters)
Modify this subscription.
void setPublishingMode(bool publishing)
Enable/disable publishing of notification messages.
T & scalar() &
Get reference to scalar value with given template type (only native or wrapper types).
Definition types.hpp:1528
uint32_t IntegerId
IntegerId.
Definition types.hpp:123
@ Value
The most recent value of the variable that the server has.
Extended monitoring parameters with default values from open62541.
double samplingInterval
Interval in milliseconds that defines the fastest rate at which the MonitoredItem should be accessed ...
Subscription parameters with default values from open62541.
double publishingInterval
Cyclic interval in milliseconds that the subscription is requested to return notifications.