open62541pp 0.16.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
client_async.cpp
#include <iostream>
int main() {
opcua::Client client;
client.onConnected([] { std::cout << "Client connected" << std::endl; });
client.onSessionActivated([&] {
std::cout << "Session activated" << std::endl;
// Schedule async operations once the client session is activated
// 1. Read request
client,
std::cout << "Read result with status code: " << result.code() << std::endl;
const auto dt = result.value().getScalar<opcua::DateTime>();
std::cout << "Server date (UTC): " << dt.format("%Y-%m-%d %H:%M:%S") << std::endl;
}
);
// 2. Browse request of Server object
);
std::cout << "Browse result with " << result.getReferences().size() << " references:\n";
for (const auto& reference : result.getReferences()) {
std::cout << "- " << reference.getBrowseName().getName() << std::endl;
}
});
// 3. Subscription
client,
opcua::SubscriptionParameters{}, // default subscription parameters
true, // publishingEnabled
{}, // statusChangeCallback
[](uint32_t subId) { std::cout << "Subscription deleted: " << subId << std::endl; },
std::cout
<< "Subscription created:\n"
<< "- status code: " << response.getResponseHeader().getServiceResult() << "\n"
<< "- subscription id: " << response.getSubscriptionId() << std::endl;
// Create MonitoredItem
client,
response.getSubscriptionId(),
),
opcua::MonitoringParametersEx{}, // default monitoring parameters
[](uint32_t subId, uint32_t monId, const opcua::DataValue& dv) {
std::cout
<< "Data change notification:\n"
<< "- subscription id: " << subId << "\n"
<< "- monitored item id: " << monId << "\n"
<< "- timestamp: " << dv.getSourceTimestamp() << std::endl;
},
{}, // delete callback
std::cout
<< "MonitoredItem created:\n"
<< "- status code: " << result.getStatusCode() << "\n"
<< "- monitored item id: " << result.getMonitoredItemId() << std::endl;
}
);
}
);
});
client.onSessionClosed([] { std::cout << "Session closed" << std::endl; });
client.onDisconnected([] { std::cout << "Client disconnected" << std::endl; });
client.connectAsync("opc.tcp://localhost:4840");
client.run();
}
UA_BrowseDescription wrapper class.
UA_BrowseResult wrapper class.
High-level client class.
Definition client.hpp:121
void onConnected(StateCallback callback)
Set a state callback that will be called after the client is connected.
void onDisconnected(StateCallback callback)
Set a state callback that will be called after the client is disconnected.
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.
void onSessionClosed(StateCallback callback)
Set a state callback that will be called after the session is closed.
void connectAsync(std::string_view endpointUrl)
Asynchronously connect to the selected server.
UA_CreateSubscriptionResponse wrapper class.
UA_DataValue wrapper class.
Definition types.hpp:1478
UA_MonitoredItemCreateResult wrapper class.
UA_ReadValueId wrapper class.
The template class Result encapsulates a StatusCode and optionally a value.
Definition result.hpp:53
constexpr T & value() &
Get the value of the Result.
Definition result.hpp:162
constexpr StatusCode code() const noexcept
Get the StatusCode of the Result.
Definition result.hpp:140
auto browseAsync(Client &connection, const BrowseRequest &request, CompletionToken &&token)
Discover the references of one or more nodes (client only).
Definition view.hpp:54
auto createMonitoredItemDataChangeAsync(Client &connection, uint32_t 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.
auto readValueAsync(Client &connection, const NodeId &id, CompletionToken &&token)
Read the AttributeId::Value attribute of a node.
@ Value
The most recent value of the variable that the server has.
Extended monitoring parameters with default values from open62541.
Subscription parameters with default values from open62541.