Namespaces — ua.namespaces
OPC UA 노드의 NodeId는 NamespaceIndex + Identifier로 구성되지만, NamespaceIndex는 불안정 — 서버 재시작 시 변경될 수 있습니다. 프로덕션 코드에서는 인덱스를 NamespaceUri로 역조회해야 하며, ns=2를 하드코딩해서는 안 됩니다.
사전 읽기
- 표준 = ns0, 노드 상수에 대해서는 다음을 참조하십시오. WellKnownNodes.
- 노드 Browse / 경로 분석에 대해서는 다음을 참조하십시오. Nodes.
NamespaceCollection 일반 회원
OpcUaSession.namespaces는 NamespaceCollection이며, connect() 시 서버 NamespaceArray (i=2255)에서 자동으로 로드됩니다.
진짜 서명:
class NamespaceCollection:
def __init__(self, session: OpcUaSession): ...
def __len__(self) -> int: ... # 네임스페이스 수
def __getitem__(self, index: int) -> Optional[str]: ... # Index → URI, 범위를 벗어날 경우 None을 반환합니다.
def index_of(self, uri: str) -> int: ... # URI → 인덱스, 찾을 수 없으면 -1을 반환
def reload(self) -> StatusCode: ... # 서버 재시작 NamespaceArray
def __iter__(self): ... # 하나씩 yield URI
| 분류 | 속성 | 유형 | 접근 | 설명 |
|---|---|---|---|---|
| 프로토콜 수준 | len(ns) | int | R | 네임스페이스 수 (Python 내장 len() 함수를 사용) |
ns[i] | Optional[str] | R | 인덱스 접근, 해당 Index에 대응하는 URI를 반환 (범위를 벗어나면 None) |
방법
index_of(uri)(int, 못 찾으면 -1)— 역조회 URI → Indexreload()(StatusCode)— 서버의 능동적 재읽기 NamespaceArray (서버가 동적으로 변경될 때)
반복 지원: for uri in ua.namespaces: ...
일반적인 사용법
역조회 Index 철자 NodeId
ua.connect()
idx = ua.namespaces.index_of("urn:my-company:plc1")
if idx < 0:
raise RuntimeError("Server does not have my namespace")
node_id = f"ns={idx};s=Temperature"
with ua.read(node_id) as dv:
print(dv.value)
모든 네임스페이스 나열 (디버깅)
for i, uri in enumerate(ua.namespaces):
print(f" [{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()를 호출하여 다시 읽어오십시오:
ua.namespaces.reload()
idx = ua.namespaces.index_of("urn:plugin:loaded-after-startup")
표준 네임스페이스 (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 | 서버 측 배열 |
또는 상수를 사용합니다 (권장):
from darra_opcua import well_known_nodes as wkn # 모듈명 darra_opcua
with ua.read(wkn.SERVER_SERVER_STATUS_CURRENT_TIME) as dv:
print(dv.value)
자세한 내용은 WellKnownNodes.