メインコンテンツまでスキップ

履歴のストリーム読み取り

概要​

IDE トレンドチャートで過去のデータを読み込む際、一度に一万ポイントを std::vector に積むとプロセスが重くなって OOM する。ReadHistoryStream はコールバックモード — 一件読み取るごとに同期コールバックが一回発生し、業務側は一件ずつ取得して処理する。結果を一括で欲しいときは ReadHistoryStreamAsync で std::future を受け取る。どちらも darra::opcua 名前空間の自由関数で、最初の引数に Session& を渡す。Session のメンバメソッドではない。

該当する規格の節: OPC UA Part 11(Historical Access)§6.5。

API​

関数カテゴリ読み書き説明
ReadHistoryStream履歴読み取り可コールバック方式のストリーム読み取り。各レコードごとに一回、同期的にコールバックが呼び出される。
ReadHistoryStreamAsync履歴読み取り可バッチ単位で非同期読み取りを行い、結果を返す std::future<std::vector<DataValue>>
namespace darra::opcua {

// コールバック方式。on_value は各 DataValue ごとに同期的にトリガーされる(読み取りスレッド)。全体のステータスを返し、個々の成否は dv.IsGood() で自己診断する。
Status ReadHistoryStream(
Session& s,
std::string const& nid,
int64_t start_ft, int64_t end_ft,
std::function<void(DataValue const&)> on_value,
uint32_t batch_size = 1000,
std::shared_ptr<std::atomic<bool>> cancel = {});

// 非同期一括処理。future.get() ですべてを一括で取得する。
std::future<std::vector<DataValue>> ReadHistoryStreamAsync(
Session& s,
std::string const& nid,
int64_t start_ft, int64_t end_ft,
uint32_t batch_size = 1000);
}
パラメータデフォルト説明
s—接続済み Session
nid—履歴ノードの NodeId 文字列
start_ft—期間の開始 (FileTime, 1601年以降 UTC 100ns )
end_ft—範囲の終端; <= start_ft の場合はその場で Status::Good を直接返し、コールバックは呼ばれない
on_value—void(DataValue const&) コールバック;空の関数オブジェクトを渡すと Status::DarraInvalidArgument を返す
batch_size1000一度に最大で何ポイントまで獲得できるか(目安値。「実装上の制限」を参照)
cancel空std::shared_ptr<std::atomic<bool>>; true に設定すると、コールバックが停止する

戻り値: ReadHistoryStream は Status (Status::Good / Status::DarraInvalidArgument) を返す, ReadHistoryStreamAsync は std::future<std::vector<DataValue>> を返す。コールバック内でスローされた例外は一つずつ無視され、次の処理に進みます。ビジネスロジック側で dv.IsGood() を自らチェックする必要がある。

コード例​

#include <darra/opcua/client.hpp>
#include <darra/opcua/history_stream.hpp>
#include <iostream>

using namespace darra::opcua;
using namespace std::chrono;

Session s("opc.tcp://localhost:4840");
s.Connect();

// 経過時間 FileTime (UTC 1601-01-01 100ns 以来)
constexpr int64_t WIN_EPOCH_OFFSET = 116444736000000000LL;
auto now_ns = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
int64_t t1 = WIN_EPOCH_OFFSET + now_ns / 100;
int64_t t0 = t1 - 24LL * 3600 * 10000000LL; // 24時間前

// 1) 引きながらレンダリングする
ReadHistoryStream(s, "ns=2;s=Temp", t0, t1,
[&](DataValue const& dv) {
double d = 0;
if (dv.Value().TryGetDouble(d))
chartSeries->AddPoint(dv.SourceTimestamp(), d);
},
/*batch_size=*/1000);

// 2) 早期終了 — cancel フラグを処理せず、ループ内の各項目を読み取り、チェックした後、終了する
auto cancel = std::make_shared<std::atomic<bool>>(false);
stopButton->OnClicked = [cancel] { cancel->store(true); };

ReadHistoryStream(s, "ns=2;s=Temp", t0, t1,
[&](DataValue const& dv) { Process(dv); },
/*batch_size=*/1000, cancel);

// 3) 不良値の集計
size_t bad = 0;
ReadHistoryStream(s, "ns=2;s=Temp", t0, t1,
[&](DataValue const& dv) {
if (!dv.IsGood()) ++bad;
},
/*batch_size=*/1000);
std::cout << "24h 内 " << bad << " 个坏值\n";

// 4) 一括処理が必要な場合は、future版を使用してください
auto fut = ReadHistoryStreamAsync(s, "ns=2;s=Temp", t0, t1);
auto values = fut.get(); // ブロックしてすべて取得

実装上の制約​

  • 現在 Session::ReadHistory は ContinuationPoint を公開せず、一回の RPC で範囲内のすべての点を返す(batch_size の制限あり)
  • 大規模な範囲の業務において、業務側で独自にスライスを行う
  • batch_size 「呼び出し側が一度に取得できる最大数」というヒントに過ぎず、一回の呼び出しでもすべてのデータが取得される
  • 早期終了 = cancel を true に設定し、ループ停止・コールバック後に Status::Good を返す;コールバックのシグネチャは void であり、「false を返して終了」のような仕組みは存在しない

ベストプラクティス​

  • コールバックは読み取りスレッド上で実行される同期実行:コールバック内の長時間かかる処理は、読み取り処理全体を遅延させる — 重い処理はワーカースレッドやキューに委ねる
  • 区間 > 1 時間の場合は、外側のスライスを推奨
  • UI レンダリングの制限:100件に達したらリフレッシュ

多言語対照​

C#PythonJavaC++RustC
ReadHistoryStreamAsyncread_history_streamreadHistoryStreamReadHistoryStreamread_history_streamDarraUa_HistoryStream_New/_Next/_IsEnd/_Free

次のステップ​