HistoryReadEvents (C)
If the server logs event history, the client can retrieve historical events (alarms/conditions) by time range.
Supporting
- To subscribe to real-time events, please use Event Subscription.
- Please use the following historical variable data: ReadRaw / ReadProcessed.
API
#include <opcua/opcua_history.h>
#include <opcua/opcua_event.h>
OPCUA_API DarraUa_Status OPCUA_CALL DarraUa_Session_HistoryReadEvents(
DarraUa_SessionHandle h,
const char* node_id_str, /* Typically, "i=2253" Server */
DarraUa_DateTime start_time,
DarraUa_DateTime end_time,
uint32_t num_values_per_node,
DarraUa_Event*** out_events,
uint32_t* out_count);
DarraUa_Event* Structure is the same as Event Subscription.
Built-in SelectClauses
Set 4 standard fields (same as Subscription Event):
- Severity (UInt16)
- Message (LocalizedText)
- SourceName (String)
- Time (DateTime)
whereClause Leave blank = All events.
Release
DarraUa_EventArray_Delete(events, count);
_EventArray_Delete Free the array itself, each Event element, and the string fields created by strdup within each Event.
Usage
#include <opcua/opcua_history.h>
#include <opcua/opcua_event.h>
DarraUa_DateTime end = utc_now_ft();
DarraUa_DateTime start = end - 864000000000LL; /* 24 hours */
DarraUa_Event** events = NULL;
uint32_t count = 0;
DarraUa_Status st = DarraUa_Session_HistoryReadEvents(
h, "i=2253",
start, end,
/*num*/ 0, /* No restrictions */
&events, &count);
if (UA_STATUS_IS_GOOD(st)) {
for (uint32_t i = 0; i < count; ++i) {
const DarraUa_Event* ev = events[i];
printf("[sev=%u] src=\"%s\" time=%lld msg=\"%s\"\n",
(unsigned)ev->severity,
ev->source_name ? ev->source_name : "",
(long long)ev->time,
ev->message ? ev->message : "");
}
}
DarraUa_EventArray_Delete(events, count);
Server Requirements
- The server must implement
ReadEventsDetails HistoryRead(Part 11 §6.4.4); if not supported, the server will pass through the status code as-is. - The event source node must have the EventNotifier Attribute (12) bit HistoryRead (
0x02) set.
Relationship to Real-Time Event Subscriptions
| API | What to bring |
|---|---|
| DarraUa_Subscription_AddEventItem(...) | Real-time Events (Occurring During the Subscription Period) |
| DarraUa_Session_HistoryReadEvents(...) | Historical Events (Archived on the Server) |
Typical setup: Pull ReadEvents data for the past 24 hours at startup, then let the subscription take over for real-time data.