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() | u32 | R | 네임스페이스 수 |
get(index) | Option<String> | R | 인덱스로 URI를 가져오며, 범위를 벗어나면 None을 반환합니다 | |
index_of(uri) | i32 | R | 역조회 URI → Index, 찾을 수 없을 경우 -1을 반환 | |
iter() | NamespaceIter | R | 모든 URI를 순회하며, Iterator<Item=String>을 구현합니다 | |
| 새로 고침 | reload() | StatusCode | RW | 서버의 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 | 설명 |
|---|---|---|
| Root | i=84 | 뿌리 |
| Objects | i=85 | 비즈니스 데이터 루트 |
| Types | i=86 | 유형 정의 루트 |
| Server | i=2253 | Server 자체 객체 |
| ServerStatus | i=2256 | 서버 상태 구조 |
| ServerStatus.CurrentTime | i=2258 | 서버의 현재 시간 |
| NamespaceArray | i=2255 | 네임스페이스 배열 |
| ServerArray | i=2254 | 서버 측 배열 |
자세한 내용은 WellKnownNodes.
use darra_opcua::well_known_nodes;
let dv = s.read(well_known_nodes::SERVER_NAMESPACE_ARRAY)?;
// dv.variant()는 String[] 배열이며, 그 내용은 s.namespaces().iter()와 동일합니다.