open62541pp 0.17.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,
opcua::VariableId::Server_ServerStatus_CurrentTime,
std::cout << "Read result with status code: " << result.code() << std::endl;
const auto dt = result.value().scalar<opcua::DateTime>();
std::cout << "Server date (UTC): " << dt.format("%Y-%m-%d %H:%M:%S") << std::endl;
}
);
// 2. Browse request of Server object
opcua::ObjectId::Server,
opcua::BrowseDirection::Forward,
opcua::ReferenceTypeId::References
);
std::cout << "Browse result with " << result.references().size() << " references:\n";
for (const auto& reference : result.references()) {
std::cout << "- " << reference.browseName().name() << std::endl;
}
});
// 3. Subscription
client,
opcua::SubscriptionParameters{}, // default subscription parameters
true, // publishingEnabled
{}, // statusChangeCallback
[](opcua::IntegerId subId) {
std::cout << "Subscription deleted: " << subId << std::endl;
},
std::cout
<< "Subscription created:\n"
<< "- status code: " << response.responseHeader().serviceResult() << "\n"
<< "- subscription id: " << response.subscriptionId() << std::endl;
// Create MonitoredItem
client,
response.subscriptionId(),
opcua::VariableId::Server_ServerStatus_CurrentTime,
),
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"
<< "- timestamp: " << dv.sourceTimestamp() << std::endl;
},
{}, // delete callback
std::cout
<< "MonitoredItem created:\n"
<< "- status code: " << result.statusCode() << "\n"
<< "- monitored item id: " << result.monitoredItemId() << 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();
}
High-level client class.
Definition client.hpp:122
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_DataValue wrapper class.
Definition types.hpp:1832
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).
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:158
constexpr StatusCode code() const noexcept
Get the StatusCode of the Result.
Definition result.hpp:136
UA_BrowseDescription wrapper class.
Definition types.hpp:1059
UA_BrowseResult wrapper class.
Definition types.hpp:1135
UA_CreateSubscriptionResponse wrapper class.
Definition types.hpp:2352
UA_MonitoredItemCreateResult wrapper class.
Definition types.hpp:2036
UA_ReadValueId wrapper class.
Definition types.hpp:1393
auto browseAsync(Client &connection, const BrowseRequest &request, CompletionToken &&token)
Discover the references of one or more nodes (client only).
Definition view.hpp:55
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.
auto readValueAsync(Client &connection, const NodeId &id, CompletionToken &&token)
Read the AttributeId::Value attribute of a node.
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.
Subscription parameters with default values from open62541.