본문으로 건너뛰기

Namespaces — s.namespaces()

OPC UA 노드의 NodeId는 NamespaceIndex + Identifier로 구성되지만, NamespaceIndex는 불안정 — 서버 재시작 시 변경될 수 있습니다. 프로덕션 코드에서는 인덱스를 NamespaceUri로 역조회해야 하며, ns=2를 하드코딩해서는 안 됩니다.

사전 읽기
  • 표준 = ns0, 노드 상수에 대해서는 다음을 참조하십시오. WellKnownNodes.
  • 노드 Browse / 경로 분석에 대해서는 다음을 참조하십시오. Nodes.

NamespaceCollection 일반 회원​

s.namespaces() NamespaceCollection 을 반환합니다 , Connect 시 서버 측 NamespaceArray (i=2255 )에서 자동으로 로드되었습니다.

분류속성유형접근설명
조회count()u32R네임스페이스 수
get(index)Option<String>R인덱스로 URI를 가져오며, 범위를 벗어나면 None을 반환합니다
index_of(uri)i32R역조회 URI → Index, 찾을 수 없을 경우 -1을 반환
iter()NamespaceIterR모든 URI를 순회하며, Iterator<Item=String>을 구현합니다
새로 고침reload()StatusCodeRW서버의 NamespaceArray를 수동으로 다시 읽기 (Server가 동적으로 변경될 때)

일반적인 사용법​

역조회 Index 철자 NodeId​

s.connect()?;
let ns = s.namespaces();
let idx = ns.index_of("urn:my-company:plc1");
if idx < 0 {
return Err("Server does not have my namespace".into());
}

let node_id = format!("ns={};s=Temperature", idx);
let dv = s.read(&node_id)?;

모든 네임스페이스 나열 (디버깅)​

let ns = s.namespaces();
for (i, uri) in ns.iter().enumerate() {
println!(" [{}] {}", i, uri);
}

출력 예시:

[0] http://opcfoundation.org/UA/
[1] urn:server-host
[2] urn:my-company:plc1
[3] http://my-company.com/UA/

동적 네임스페이스 변경​

일부 서버는 시작된 후에야 네임스페이스를 등록하므로(플러그인 방식), Connect가 가져오는 NamespaceArray가 불완전할 수 있습니다. 이때 reload()를 호출하여 다시 읽으십시오:

let ns = s.namespaces();
ns.reload();
let idx = ns.index_of("urn:plugin:loaded-after-startup");

NodeCollection 자동 생성 NodeId​

NodeCollection::make_numeric(uri, identifier)와 make_string(uri, identifier)는 네임스페이스 URI를 직접 전달하므로, 직접 인덱스를 역조회할 필요가 없습니다:

// 숫자 NodeId: "ns=2;i=1234"
let node = s.nodes().make_numeric("urn:my-company:plc1", 1234)?;

// 문자열 NodeId: "ns=2;s=Temperature"
let node = s.nodes().make_string("urn:my-company:plc1", "Temperature")?;

네임스페이스가 존재하지 않을 때 반환합니다 Err(OpcUaError::with_message(StatusCode::INVALID_ARGUMENT, "Namespace URI 'xxx' not found")).

표준 네임스페이스 (ns=0)​

OPC Foundation은 Index 0을 항상 http://opcfoundation.org/UA/로 규정하며, 모든 표준 노드는 ns=0에 있습니다:

노드NodeId설명
Rooti=84뿌리
Objectsi=85비즈니스 데이터 루트
Typesi=86유형 정의 루트
Serveri=2253Server 자체 객체
ServerStatusi=2256서버 상태 구조
ServerStatus.CurrentTimei=2258서버의 현재 시간
NamespaceArrayi=2255네임스페이스 배열
ServerArrayi=2254서버 측 배열

자세한 내용은 WellKnownNodes.

use darra_opcua::well_known_nodes;

let dv = s.read(well_known_nodes::SERVER_NAMESPACE_ARRAY)?;
// dv.variant()는 String[] 배열이며, 그 내용은 s.namespaces().iter()와 동일합니다.

다음 단계​