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

BrowseCrawler

概要​

BFS キューを使用して AddressSpace サブツリー全体を走査し、二重の保護 (max_depth + max_nodes) を行う。C言語版は以下の通りである。リコール式: ノードが見つかるたびに一回呼び出し on_node、クロールが完了すると関数が戻り、結果の配列は返さない。

API​

関数カテゴリ読み書き説明
DarraUa_CrawlerConfig_Init(&cfg)構造—デフォルト設定を入力する
DarraUa_Session_CrawlAddressSpace(session, &cfg, on_node, ctx, &total)方法読み取り可同期 BFS、ブロック型呼び出し
#define UA_CRAWLER_DEFAULT_MAX_DEPTH  10
#define UA_CRAWLER_DEFAULT_MAX_NODES 100000

typedef struct {
const char* root_node_id; /* NULL = "i=85" (ObjectsFolder) */
uint32_t max_depth; /* 0 = デフォルトの 10 を使用 */
uint32_t max_nodes; /* 0 = デフォルトの 100000 を使用 */
DarraUa_NodeClass node_class_filter; /* UA_NODE_CLASS_UNSPECIFIED = フィルタなし */
} DarraUa_CrawlerConfig;

typedef struct {
const char* node_id;
const char* display_name;
const char* browse_name;
DarraUa_NodeClass node_class;
const char* parent_node_id; /* ルートノードは NULL */
uint32_t depth; /* root = 0 */
} DarraUa_CrawlNode;

/* 戻り値 1 = 続行、0 = すぐにキャンセル */
typedef int32_t (*DarraUa_OnCrawlNode)(
const DarraUa_CrawlNode* node,
uint32_t running_count,
void* user_context);

DarraUa_Status DarraUa_Session_CrawlAddressSpace(
DarraUa_SessionHandle session,
const DarraUa_CrawlerConfig* cfg, /* NULL = すべてデフォルト */
DarraUa_OnCrawlNode on_node, /* 必ず「非」でなければならない NULL */
void* user_context,
uint32_t* out_total); /* 可能である NULL */
パラメータデフォルト説明
session—接続済みのセッションハンドル (DarraUa_SessionHandle)
cfgNULL → すべてデフォルト設定の取得
on_node—ノードのコールバック。NULLであってはならない。
user_context—ユーザーコンテキストの呼び出し戻し
out_totalNULL可出力:合計で捕捉されたノード数

ノードフィールドのライフサイクル: DarraUa_CrawlNode ここにあるすべての文字列フィールドは「borrowed」であり、そのライフサイクルはコールバック内部に限定されます。保持したい場合は、コールバック内で処理してください strdup。

キャンセル: コールバックが 0 を返した場合、直ちにキャンセルされ、関数は終了します UA_STATUS_GOOD。out_total それでも、捕捉された数は返されます。

コード例​

#include <opcua.h>
#include <opcua_browse_crawler.h>
#include <stdio.h>

typedef struct {
uint32_t shown;
int32_t cancel;
} CrawlCtx;

static int32_t on_node(const DarraUa_CrawlNode* node,
uint32_t running_count, void* ctx)
{
CrawlCtx* c = (CrawlCtx*)ctx;
printf("\r已抓 %u 个, 当前 %s", running_count,
node->node_id ? node->node_id : "");

/* 変数ノードのみをキャッシュする(ドリル BFS ダウンには影響しない) */
if (node->node_class == UA_NODE_CLASS_VARIABLE) {
/* 文字列は借用されているため、保持したい場合は自分でコピーする必要があります。 */
cache_put(node->node_id, node->browse_name);
c->shown++;
}

/* ユーザーが「キャンセル」をクリックすると、0を返す */
return c->cancel ? 0 : 1;
}

int main(void)
{
DarraUa_SessionConfig scfg;
DarraUa_SessionConfig_Init(&scfg);
scfg.endpoint_url = "opc.tcp://localhost:4840";
/* ... DarraUa_Session_Create + DarraUa_Session_Connect 中略 ... */

DarraUa_CrawlerConfig cfg;
DarraUa_CrawlerConfig_Init(&cfg); /* 深さ 10 / ノード 100000 / フィルタリングなし */
cfg.root_node_id = "i=85"; /* ObjectsFolder */
cfg.max_depth = 8;
cfg.max_nodes = 50000;
cfg.node_class_filter = UA_NODE_CLASS_UNSPECIFIED;

CrawlCtx ctx = { 0, 0 };
uint32_t total = 0;
DarraUa_Status st = DarraUa_Session_CrawlAddressSpace(
session, &cfg, on_node, &ctx, &total);

printf("\n共 %u 节点 (变量 %u), status=0x%08X\n",
total, ctx.shown, st);
/* 結果のない配列を解放する:コールバック内でノードをその場で消費する */
return 0;
}

ベストプラクティス​

  • rootに限定し、i=84(Root)から開始しないこと
  • これにより、コールバックの回 node_class_filter 数を減らすことができます(注:フィルタリングはコールバックを記述しないだけであり、すべての階層的な子ノードに対してドリル BFS ダウンは行われます)。
  • 起動時に一度だけクロールする
  • コールバックが 0 を返すとキャンセルが実行され、外部フラグによるポーリングは不要となる
  • 取得した文字列を保持したい場合は、コピー DarraUa_CrawlNode する必要があります

多言語対照​

C#PythonJavaC++RustC
BrowseCrawlerBrowseCrawlerBrowseCrawlerBrowseCrawlerBrowseCrawlerDarraUa_CrawlerConfig + DarraUa_Session_CrawlAddressSpace
CrawlAsynccrawl_asynccrawlAsyncCrawlAsynccrawl_asyncDarraUa_Session_CrawlAddressSpace

次のステップ​