NodeCollection (C には対応する項目がない)
C# では、このような ua.Nodes["ns=2;s=Foo"] Lazy Load インデクサーが提供されており、 C SDK 対等な存在がない — C には演算子のオーバーロードがなく、永続的なノードオブジェクトも保持しない。すべてが文字 NodeId 列に対して直接呼び出される API。
事前学習
- 単一ノードの閲覧についてはこちらをご覧ください Browse.
- チェーン式アクセスの代替についてはこちらをご覧ください TranslateBrowsePaths.
C# の記述法 → C の記述法との対照表
文字列 NodeId 直接読み込み
// C#
using var dv = ua.Nodes["ns=2;s=Temperature"].Value;
/* C */
DarraUa_Status st = 0;
DarraUa_DataValue* dv = DarraUa_Session_ReadNode(
h, "ns=2;s=Temperature", UA_ATTR_VALUE, &st);
/* 使い切る */
DarraUa_DataValue_Delete(dv);
チェーンインデクサー (Server.ServerStatus.CurrentTime)
// C# - 4回 Browse + 1回の閲覧
using var dv = ua.Nodes.Server["ServerStatus"]["CurrentTime"].Value;
C 推奨方法: を使用してパスを一度に明確に指定 ResolveBrowsePath する(内部ではパス区間ごとに階層的に処理し Browse、一区間ずつ処理する RPC)。呼び出し側は一行だけ記述すればよい:
/* C - ResolveBrowsePath (N 区間の経路 = N 回 Browse RPC) + 1 回の読み取り */
char node_id[128];
int32_t n = DarraUa_Session_ResolveBrowsePath(
h, "i=2253" /* Server */,
"/ServerStatus/CurrentTime",
node_id, (int32_t)sizeof(node_id));
if (n == 1) { /* 1 = 成功、0 = 失敗 */
DarraUa_Status st = 0;
DarraUa_DataValue* dv = DarraUa_Session_ReadNode(
h, node_id, UA_ATTR_VALUE, &st);
DarraUa_DataValue_Delete(dv);
}
あるいはもっと直接的には — 標準ノード Server.ServerStatus.CurrentTime の場合、マクロを直接使用します:
DarraUa_DataValue* dv = DarraUa_Session_ReadNode(
h, "i=2258" /* CurrentTime */, UA_ATTR_VALUE, &st);
簡単なキャッシュの実装(推奨)
業務上、頻繁に で検索 BrowsePath する場合は NodeId、自分で path → NodeId のマッピングを作成しましょう:
typedef struct {
char path[256];
char node_id[128];
} PathCacheEntry;
static PathCacheEntry g_cache[1024];
static int g_cache_count = 0;
static const char* resolve_cached(DarraUa_SessionHandle h,
const char* start, const char* path)
{
/* 1. キャッシュを確認する */
char key[256];
snprintf(key, sizeof(key), "%s|%s", start, path);
for (int i = 0; i < g_cache_count; ++i) {
if (strcmp(g_cache[i].path, key) == 0)
return g_cache[i].node_id;
}
/* 2. 解析後にキャッシュに格納する */
if (g_cache_count >= 1024) return NULL;
PathCacheEntry* e = &g_cache[g_cache_count];
int32_t n = DarraUa_Session_ResolveBrowsePath(
h, start, path, e->node_id, (int32_t)sizeof(e->node_id));
if (n != 1) return NULL; /* 1 = 成功、0 = 失敗 */
snprintf(e->path, sizeof(e->path), "%s", key);
g_cache_count++;
return e->node_id;
}
どのような方法で、いつ使うか
| シナリオ | おすすめ |
|---|---|
| 正確に判明している NodeId | 文字列 DarraUa_Session_ReadNode |
| 標準ノード(サーバーの状態など) | で WellKnownNodes 宏 |
| ツリーの閲覧 / 一回限りのスクリプトの作成 | DarraUa_Session_BrowseNode |
| 複数のノードを一括処理 | DarraUa_Session_ReadNodes / _BrowseNodes、ループは不要 |
| 複数のパスの一括初期化 | DarraUa_Session_TranslateBrowsePaths |