본문으로 건너뛰기

NodeId — 강타입 NodeId

개요​

OPC UA 노드 ID는 기본적으로 문자열로 인코딩됩니다 ("ns=2;s=Foo").NodeId 일반 값 유형(struct, 공용 필드는 직접 읽기/쓰기 가능)이며, namespace_index / type / identifier 세 가지 요소를 명시적으로 포함하고, 문자열 인코딩과 상호 변환이 가능합니다.

해당 규격 조항: Part 6 §5.2.2.

헤더 파일​

#include <darra/opcua/node_id.hpp>
using namespace darra::opcua;

API​

구성원분류읽기 및 쓰기설명
NodeId::Null()정적 팩토리—구조체 빈 NodeId (i=0), 함수이지 상수가 아닙니다.
NodeId::FromNumeric(uint16_t ns, uint32_t id)정적 팩토리—디지털형
NodeId::FromString(uint16_t ns, std::string id)정적 팩토리—문자열형
NodeId::FromGuid(uint16_t ns, std::arraystd::array<uint8_t,16> guid_bytes)정적 팩토리—GUID 형 (16바이트 원시 데이터, UUID 문자열 아님)
NodeId::FromOpaque(uint16_t ns, std::vectorstd::vector<uint8_t> bytes)정적 팩토리—불투명형
NodeId::Parse(std::string const& s)정적—분석, 실패 시 중단 std::invalid_argument
NodeId::TryParse(std::string const& s)정적—보안 분석, 반환 std::optional<NodeId>, bool + out 매개변수가 아닙니다
nid.ToString()방법—표준 문자열로 직렬화
nid.IsNull()방법—비어 있는지 여부 NodeId
nid.namespace_index필드읽기 및 쓰기uint16_t
nid.type필드읽기 및 쓰기NodeIdType (필드 이름은 type, 이 아니라 id_type)
nid.identifier필드읽기 및 쓰기std::variant<uint32_t, std::string, std::array<uint8_t,16>, std::vector<uint8_t>>

관련 구조 — NodeIdType​

NodeIdType값코딩 접두사identifier에 저장된 유형
Numeric0i=uint32_t
String1s=std::string (UTF-8)
Guid2g=std::array<uint8_t,16>
Opaque3b=std::vector<uint8_t> (base64)

문자열 인코딩 규칙 (표기 ToString / TryParse 실제 동작):

  • namespace_index == 0 때 ToString() 생략ns=0; 접두사
  • Guid 출력 결과는 g= + 36자 십육진수이며, 중괄호 없이; Parse 시간 괄호는 있어도 되고 없어도 된다
  • Parse / TryParse 의 ns=N 접두사는 생략할 수 있으며(생략 시 ns=0), 번호 접두사의 대소문자는 구분하지 않습니다.

알려진 제한 사항: operator< /에 대한 NodeId 정의가 node_id.hpp 없으며 operator==, std::hash 특화(C++17 집합 struct)도 없으므로, 직접 std::map /의 std::unordered_map 키로 사용할 수 없습니다. —— 키로 ToString() 를 사용하십시오.

내장된 well-known 래퍼: 로 돌아가는 정적 NodeId const& 메서드(Well::RootFolder() / Well::ObjectsFolder() / Well::Server() / Well::ServerStatus() / Well::CurrentTime() / Well::Organizes() 등)를 NodeId::Well 제공하며, 자세한 내용은 Well-Known Nodes.

코드 예시​

#include <darra/opcua/node_id.hpp>
#include <darra/opcua/client.hpp>
#include <array>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace darra::opcua;

// 1) 공장 (ns 는 uint16_t, 디지털 ID는 uint32_t)
NodeId n1 = NodeId::FromNumeric(0, 84); // i=84
NodeId n2 = NodeId::FromString(2, "Boiler1.Temp"); // ns=2;s=Boiler1.Temp
NodeId n3 = NodeId::FromGuid(3, std::array<uint8_t, 16>{ // 순수 16바이트, UUID가 아닌 문자열
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10});
NodeId n4 = NodeId::FromOpaque(4, std::vector<uint8_t>{1, 2, 3});

// 2) 분석 — namespace_index / type / identifier는 공용 필드이므로 값을 직접 가져옵니다.
NodeId n5 = NodeId::Parse("ns=2;s=Demo.Tag");
std::cout << "ns=" << n5.namespace_index
<< ", type=" << static_cast<int>(n5.type) << "\n";

// TryParse 은 std::optional<NodeId> 를 반환, bool + out 매개변수가 아닙니다
if (auto n6 = NodeId::TryParse("ns=99;i=foo"); !n6)
std::cout << "invalid\n";

// 3) 시리얼라이즈 (ns=0일 경우 "ns=0;" 접두사를 생략)
std::string s = n2.ToString(); // "ns=2;s=Boiler1.Temp"

// 4) Session과의 상호 운용성 — Session::Read/Write 문자열만 처리 NodeId
Stack::Init();
Session ua("opc.tcp://localhost:4840");
ua.Connect();
DataValue dv = ua.Read(n2.ToString());

// 5) map 키로 사용 — operator< / std::hash가 NodeId 없으므로, 키로 ToString() 사용
std::map<std::string, std::string> dict;
dict[n2.ToString()] = "锅炉一温度";

// 6) null NodeId — Null()은 정적 함수이며, 상수가 아닙니다.
std::cout << NodeId::Null().IsNull() << "\n"; // 1

모범 사례​

  • 프로필에 문자열 저장, 불러올 때 사용 Parse
  • 코드 내부에서는 강타입을 사용하고, Session::Read / 함수 호출 Write 시에는 ToString()
  • 숫자 우선 (식별자 생략 uint32_t, 가장 일반적)
  • 사용자 입력은 항상 먼저 처리됩니다 TryParse (반환되며 std::optional, 예외를 발생시키지 않음)
  • map/set의 키를 지정할 때는 쉼표(,)를 사용하되 ToString(), NodeId 내장된 비교 기능이 작동할 것이라고 기대하지 마십시오.

다국어 대조​

C#PythonJavaC++RustC
OpcUaNodeIdOpcUaNodeIdOpcUaNodeIdNodeIdOpcUaNodeIdDarraUa_OwnedNodeId
ParseparseparseNodeId::ParseparseDarraUa_NodeId_Parse
FromNumericfrom_numericfromNumericNodeId::FromNumericfrom_numericDarraUa_NodeId_FromNumeric

다음 단계​