open62541pp 0.16.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([&] {
auto sub = client.createSubscription();
// 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::AttributeId::Value, // monitored attribute
[&](uint32_t subId, uint32_t monId, const opcua::DataValue& value) {
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.getNodeId().toString() << "\n"
<< "- attribute id: " << static_cast<int>(item.getAttributeId()) << "\n";
const auto dt = value.getValue().getScalarCopy<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:121
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.
Subscription< Client > createSubscription(const SubscriptionParameters &parameters={})
Create a subscription to monitor data changes and events.
UA_DataValue wrapper class.
Definition types.hpp:1478
Variant & getValue() &noexcept
Get value.
Definition types.hpp:1550
UA_DateTime wrapper class.
Definition types.hpp:354
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.
uint32_t subscriptionId() const noexcept
Get the server-assigned identifier of the underlying subscription.
uint32_t monitoredItemId() const noexcept
Get the server-assigned identifier of this monitored item.
const NodeId & getNodeId()
Get the monitored NodeId.
AttributeId getAttributeId()
Get the monitored AttributeId.
std::string toString() const
Encode NodeId as a string like ns=1;s=SomeNode.
T getScalarCopy() const
Get copy of scalar value with given template type.
Definition types.hpp:1050
@ 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.