ReadHistoryAtTime
Given a set of discrete time points (FileTime), the server returns the value for each time point—use_simple_bounds=true using the most recent actual value or false performing linear interpolation (the server must support interpolation). Used for "timeline alignment":
- To generate aggregated statistics, please use ReadHistoryProcessed.
- To select all origin points in a range, use ReadHistory (Raw).
Signature
The following prototypes are member methods Session of.
std::vector<DataValue> ReadHistoryAtTime(
std::string const& node_id,
std::vector<int64_t> const& req_times,
bool use_simple_bounds = true);
| Parameters | Note |
|---|---|
| node_id | Objective NodeId |
| req_times | List of discrete time points (FileTime) |
| use_simple_bounds | true = Use the most recent actual value; false = Linear interpolation (must be supported by the server) |
Return: std::vector<DataValue>, the order corresponds req_times one-to-one with, butThe length may be less than req_times.size() — The server may not return a point for a value that does not exist, or it may use the BadNoData marker (the server’s raw value StatusCode, which is not in the Status enumeration; use dv.StatusCode() to read the raw value). Before retrieving the result using the req_times index, first verify dv.StatusCode() i < values.size();
req_times once retrieved, perform a self-check. If it is empty, directly return an empty vector without sending a request.
Usage
#include <darra/opcua/client.hpp>
#include <chrono>
#include <iostream>
#include <vector>
using namespace darra::opcua;
Session s("opc.tcp://localhost:4840");
s.Connect();
constexpr int64_t WIN_EPOCH_OFFSET = 116444736000000000LL;
// Temperatures at every hour on the hour over the past 24 hours
std::vector<int64_t> hours;
auto now_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
int64_t now_ft = WIN_EPOCH_OFFSET + now_ns / 100;
// Round to the nearest hour (UTC)
int64_t hour_ft = now_ft - (now_ft % (3600LL * 10000000LL));
for (int h = -24; h <= 0; ++h) {
hours.push_back(hour_ft + h * 3600LL * 10000000LL);
}
auto values = s.ReadHistoryAtTime("ns=2;s=Temperature", hours);
// The order of the `values` corresponds to the `hours`, and their length may be less than `hours.size()`
for (size_t i = 0; i < values.size() && i < hours.size(); ++i) {
if (!values[i].IsGood()) continue;
std::cout << "ft=" << hours[i] << " val=" << values[i].Value().AsString() << "\n";
}
Difference from ReadHistoryProcessed
| API | Return |
|---|---|
| ReadHistoryAtTime | Each time point corresponds to an interpolated value (not aggregated) |
| ReadHistoryProcessed | One aggregate value (Avg / Min / Max / ...) per subinterval |
Example: "Pull data for 24 hours, one value per hour":
ReadHistoryAtTime: Enter 25 integer points; return up to 25 interpolated points (do not return or mark points where no value existsBadNoData).ReadHistoryProcessed: aggregate=Average, interval=1 h, returns the average of 24 intervals
Use Cases
- Temporal Alignment: Different sensors have different sampling times; they must be synchronized to the same point in time to be compared.
- Database Export: On-the-hour sampling
- Reports: 24 lines per day