open62541pp 0.15.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
events/client_eventfilter.cpp
// This example should be executed while `server_events` is running. You can use e.g. UaExpert to
// trigger events with custom severity and messages using the `GenerateEvent` method node.
#include <iostream>
int main() {
opcua::Client client;
client.connect("opc.tcp://localhost:4840");
// Define filter elements
const opcua::ContentFilterElement filterBaseEventType(
{
}
);
const opcua::ContentFilterElement filterLowSeverity(
{
),
}
);
// Combine filter with unary operators (!) and binary operators (&&, ||).
// The resulting filter will filter events of type BaseEventType with severity >= 200.
const opcua::ContentFilter filterCombined = filterBaseEventType && !filterLowSeverity;
const opcua::EventFilter eventFilter(
// Select clause
{
},
// Where clause
filterCombined
);
auto sub = client.createSubscription();
sub.subscribeEvent(
eventFilter,
[&](uint32_t subId, uint32_t monId, opcua::Span<const opcua::Variant> eventFields) {
opcua::MonitoredItem item(client, subId, monId);
std::cout
<< "Event 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& time = eventFields[0].getScalar<opcua::DateTime>();
const auto& severity = eventFields[1].getScalar<uint16_t>();
const auto& message = eventFields[2].getScalar<opcua::LocalizedText>();
std::cout << "Time: " << time.format("%Y-%m-%d %H:%M:%S") << "\n";
std::cout << "Severity: " << severity << "\n";
std::cout << "Message: " << message.getText() << "\n";
}
);
// 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();
}
High-level client class.
Definition client.hpp:63
Subscription< Client > createSubscription()
Create a subscription to monitor data changes and events (default subscription parameters).
void connect(std::string_view endpointUrl)
Connect to the selected server.
void run()
Run the client's main loop by. This method will block until Client::stop is called.
UA_ContentFilterElement wrapper class.
UA_ContentFilter wrapper class.
UA_DateTime wrapper class.
Definition types.hpp:354
UA_EventFilter wrapper class.
UA_LiteralOperand wrapper class.
UA_LocalizedText wrapper class.
Definition types.hpp:837
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.
UA_NodeId wrapper class.
Definition types.hpp:590
std::string toString() const
Encode NodeId as a string like ns=1;s=SomeNode.
View to a contiguous sequence of objects, similar to std::span in C++20.
Definition span.hpp:26
@ Value
The most recent value of the variable that the server has.