open62541pp 0.19.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{
opcua::FilterOperator::OfType,
{
opcua::LiteralOperand{opcua::NodeId{opcua::ObjectTypeId::BaseEventType}},
}
};
const opcua::ContentFilterElement filterLowSeverity{
opcua::FilterOperator::LessThan,
{
opcua::ObjectTypeId::BaseEventType, {{0, "Severity"}}, opcua::AttributeId::Value
),
}
};
// 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
{
{opcua::ObjectTypeId::BaseEventType, {{0, "Time"}}, opcua::AttributeId::Value},
{opcua::ObjectTypeId::BaseEventType, {{0, "Severity"}}, opcua::AttributeId::Value},
{opcua::ObjectTypeId::BaseEventType, {{0, "Message"}}, opcua::AttributeId::Value},
},
// Where clause
filterCombined
};
opcua::Subscription sub{client};
sub.subscribeEvent(
opcua::ObjectId::Server,
eventFilter,
[&](opcua::IntegerId subId,
opcua::MonitoredItem item{client, subId, monId};
std::cout
<< "Event notification:\n"
<< "- subscription id: " << item.subscriptionId() << "\n"
<< "- monitored item id: " << item.monitoredItemId() << "\n"
<< "- node id: " << opcua::toString(item.nodeId()) << "\n"
<< "- attribute id: " << static_cast<int>(item.attributeId()) << "\n";
const auto& time = eventFields.at(0).scalar<opcua::DateTime>();
const auto& severity = eventFields.at(1).scalar<uint16_t>();
const auto& message = eventFields.at(2).scalar<opcua::LocalizedText>();
std::cout << "Time: " << time.format("%Y-%m-%d %H:%M:%S") << "\n";
std::cout << "Severity: " << severity << "\n";
std::cout << "Message: " << message.text() << "\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:130
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_DateTime wrapper class.
Definition types.hpp:381
UA_LocalizedText wrapper class.
Definition types.hpp:956
High-level monitored item class.
UA_NodeId wrapper class.
Definition types.hpp:641
View to a contiguous sequence of objects, similar to std::span in C++20.
Definition span.hpp:29
constexpr reference at(size_t index) const
Access element by index with bounds checking.
Definition span.hpp:114
High-level subscription class.
UA_ContentFilterElement wrapper class.
Definition types.hpp:1786
UA_ContentFilter wrapper class.
Definition types.hpp:1807
UA_EventFilter wrapper class.
Definition types.hpp:1891
UA_LiteralOperand wrapper class.
Definition types.hpp:1679
UA_SimpleAttributeOperand wrapper class.
Definition types.hpp:1733
uint32_t IntegerId
IntegerId.
Definition types.hpp:84
String toString(const NumericRange &range)