open62541pp 0.22.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
client_subscription.cpp
#include <atomic>
#include <chrono>
#include <csignal>
#include <iostream>
#include <thread>
inline static std::atomic<bool> isRunning = true; // NOLINT(*global-variables)
static void signalHandler(int sig) noexcept {
if (sig == SIGINT || sig == SIGTERM) {
isRunning = false;
}
}
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([&] {
// Synchronous service calls are not allowed from a state callback because it is invoked
// while the client's event loop is running. Use the asynchronous service API instead.
opcua::SubscriptionParameters subscriptionParameters{};
subscriptionParameters.publishingInterval = 1000.0;
client,
subscriptionParameters,
true, // publishingEnabled
{}, // statusChangeCallback
{}, // deleteCallback
std::cout
<< "Subscription created:\n"
<< "- status code: " << response.responseHeader().serviceResult() << "\n"
<< "- subscription id: " << response.subscriptionId() << std::endl;
client,
response.subscriptionId(),
opcua::VariableId::Server_ServerStatus_CurrentTime,
opcua::AttributeId::Value
},
opcua::MonitoringMode::Reporting,
opcua::MonitoringParametersEx{}, // default monitoring parameters
[](opcua::IntegerId subId, opcua::IntegerId monId, const opcua::DataValue& dv) {
std::cout
<< "Data change notification:\n"
<< "- subscription id: " << subId << "\n"
<< "- monitored item id: " << monId << "\n"
<< "- value: " << opcua::toString(dv) << std::endl;
},
{}, // deleteCallback
std::cout
<< "Monitored item created:\n"
<< "- status code: " << result.statusCode() << "\n"
<< "- monitored item id: " << result.monitoredItemId() << std::endl;
}
);
}
);
});
std::signal(SIGINT, signalHandler); // NOLINT
// Endless loop to automatically (try to) reconnect to server.
while (isRunning) {
try {
client.connect("opc.tcp://localhost:4840");
// Run the client's main loop to process callbacks and events.
while (isRunning) {
client.runIterate(100);
}
} 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:150
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 runIterate(uint16_t timeoutMilliseconds=1000)
Run a single iteration of the client's main loop.
UA_DataValue wrapper class.
Definition types.hpp:1577
UA_CreateSubscriptionResponse wrapper class.
Definition types.hpp:2293
UA_MonitoredItemCreateResult wrapper class.
Definition types.hpp:1993
UA_ReadValueId wrapper class.
Definition types.hpp:1363
auto createMonitoredItemDataChangeAsync(Client &connection, IntegerId subscriptionId, const ReadValueId &itemToMonitor, MonitoringMode monitoringMode, const MonitoringParametersEx &parameters, DataChangeNotificationCallback dataChangeCallback, DeleteMonitoredItemCallback deleteCallback, CompletionToken &&token)
auto createSubscriptionAsync(Client &connection, const CreateSubscriptionRequest &request, StatusChangeNotificationCallback statusChangeCallback, DeleteSubscriptionCallback deleteCallback, CompletionToken &&token)
Create a subscription.
uint32_t IntegerId
IntegerId.
Definition types.hpp:84
Extended monitoring parameters with default values from open62541.
Subscription parameters with default values from open62541.
double publishingInterval
Cyclic interval in milliseconds that the subscription is requested to return notifications.