ReadHistoryEvents
If the server logs event history, the client can retrieve historical events by time range.
- To subscribe to real-time events, please use Event Subscription.
- Please use the following historical variable data: ReadHistory (Raw) / ReadHistoryProcessed.
Signature
The following prototypes are member methods Session of.
std::vector<EventArrivedEventArgs> ReadHistoryEvents(
std::string const& node_id,
int64_t start_ft, int64_t end_ft,
uint32_t max_events = 0);
| Parameters | Default | Note |
|---|---|---|
| node_id | — | Event source node NodeId, typically a server i=2253 |
| start_ft | — | Range starts at (FileTime) |
| end_ft | — | Range Stop (FileTime) |
| max_events | 0 | Maximum number of records to return; 0 = unlimited (the server may still set its own limit) |
EventArrivedEventArgs
struct EventArrivedEventArgs {
uint16_t severity = 0; // 1-1000
std::string message;
std::string source_name;
int64_t time = 0; // FileTime (UTC 100ns since 1601)
std::string event_type;
};
Each event returned by the server has already been flattened into a flat structure at the layer SDK. Built-in SelectClause Fixed: 4 fields: Severity / Message / SourceName / Time; Leave the filter criteria blank to return all events within the range.
event_type is always an empty string in history reads — it is not among these 4 SelectClauses; the history path does not resolve EventType (the default event subscription does not resolve it either, see Event Subscription).
Usage
#include <darra/opcua/client.hpp>
#include <chrono>
#include <iostream>
using namespace darra::opcua;
Session s("opc.tcp://localhost:4840");
s.Connect();
constexpr int64_t WIN_EPOCH_OFFSET = 116444736000000000LL;
auto now_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
int64_t end_ft = WIN_EPOCH_OFFSET + now_ns / 100;
int64_t start_ft = end_ft - 24LL * 3600 * 10000000;
auto events = s.ReadHistoryEvents("i=2253", start_ft, end_ft);
for (auto const& e : events) {
std::cout << "[" << e.severity << "] " << e.source_name
<< ": " << e.message
<< " ft=" << e.time << "\n";
// e.event_type The history always returns an empty string; see above.
}
Custom Filtering
ReadHistoryEvents has no EventFilter parameter — which fields to capture and what to filter on are fixed: 4 SelectClause fields, with the filter criteria left blank (all events). Every alarm and condition in the range comes back, and the business layer then filters them by Severity / SourceName itself.
To customize SelectClause or filter criteria, go toEvent Subscription That path: C API DarraUa_EventSubscription_SubscribeWithFilter Pass a custom type EventFilter. The C++ side darra/opcua/event_filter.hpp provides EventFilter / SimpleAttributeOperand type definitions, but Session currently does not accept these types, and this file's WhereClause is also not implemented (leave it blank).
Server Requirements
- The server must support HistoryRead Events (ReadEventsDetails)
- The 1st bit (
EventNotifier, HistoryRead) of0x02Attribute (12) of the event source node must be set.
Relationship to Real-Time Event Subscriptions
| API | What to bring |
|---|---|
| s.SubscribeEvents(...) | Real-time Events (Occurring During the Subscription Period) |
| s.ReadHistoryEvents(...) | Historical Events (Archived on the Server) |
Typical setup: ReadHistoryEvents Run it for 24 hours upon startup, then let SubscribeEvents take over for real-time processing.