본문으로 건너뛰기

OpcUaVariant

OpcUaVariant 이는 범용 값 컨테이너로 OPC UA , 모든 OPC UA 내장 데이터형을 포괄합니다. Java와 네이티브 간에 값을 전달할 때 SDK 사용됩니다.

구현 AutoCloseable — 네이티브 핸들을 보유하며, 반드시 close.

세트
  • 패키지 값 + 타임스탬프 + 상태는 다음을 참조하십시오 OpcUaDataValue.
  • 표준 노드 상수에 대해서는 다음을 참조하십시오. WellKnownNodes.

공용 메서드​

분류속성유형접근설명
상태handle()PointerR내부 네이티브 핸들 (디버깅용)
getDataType()Enums.BuiltinTypeR내장 데이터 유형
구축 / 설정setBoolean / setSByte / setByte / setInt16 / setUInt16 / setInt32 / setUInt32 / setInt64 / setFloat / setDouble / setString / setDateTimeOpcUaVariant (this)W연쇄 설정, 자체 반환
값 가져오기asBoolean()BooleanR타입이 일치하지 않으면 null을 반환합니다
asInt32()IntegerR타입이 일치하지 않으면 null을 반환합니다
asUInt32()LongRLong으로 부호 없는 값을 저장
asInt64()LongR타입이 일치하지 않으면 null을 반환합니다
asDouble()DoubleR타입이 일치하지 않으면 null을 반환합니다
asString()StringR타입이 일치하지 않으면 null을 반환합니다
asFileTime()longRDateTime Windows에서 가져옴 FileTime, 0 = 빈 값
tryGet 스타일tryGetBoolean(IntByReference)booleanR성공 시 true를 반환하며, 결과는 IntByReference에 저장됨
tryGetInt32 / tryGetUInt32 / tryGetInt64 / tryGetDoublebooleanR위와 동일, 해당 유형을 ByReference 방식으로 출력
해방close()void—네이티브 메모리 해제 (owns=true일 때)

구축 및 설정 (연쇄형)​

new OpcUaVariant().setBoolean(true)            // Boolean
new OpcUaVariant().setSByte((byte) -1) // SByte
new OpcUaVariant().setByte(255) // Byte
new OpcUaVariant().setInt16((short) 42) // Int16
new OpcUaVariant().setUInt16(42) // UInt16
new OpcUaVariant().setInt32(42) // Int32
new OpcUaVariant().setUInt32(42) // UInt32
new OpcUaVariant().setInt64(42L) // Int64
new OpcUaVariant().setFloat(3.14f) // Float
new OpcUaVariant().setDouble(3.14) // Double
new OpcUaVariant().setString("hello") // String
new OpcUaVariant().setDateTime(fileTimeLong) // DateTime (Windows FileTime)

읽기 (강타입)​

try (OpcUaDataValue dv = ua.read("ns=2;s=T1")) {
OpcUaVariant v = dv.variant(); // 보유하지 않음, close하지 마세요
Double d = v.asDouble(); // null = 유형 불일치
Integer i = v.asInt32();
String s = v.asString();
Boolean b = v.asBoolean();
long ft = v.asFileTime(); // DateTime
}

유형 불일치 반환 null (Java SDK 스타일), 예외를 던지지 않음 — 상위 레벨에서 명시적으로 null 여부를 확인.

tryGet (out 스타일)​

import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.DoubleByReference;

OpcUaVariant v = dv.variant();

IntByReference outI = new IntByReference();
if (v.tryGetInt32(outI)) {
int n = outI.getValue();
}

DoubleByReference outD = new DoubleByReference();
if (v.tryGetDouble(outD)) {
double x = outD.getValue();
}

DateTime ↔ Instant​

import java.time.Instant;

// FileTime → Instant
long ft = v.asFileTime();
Instant t = (ft > 0)
? Instant.ofEpochMilli((ft - 116444736000000000L) / 10000L)
: Instant.now();

// Instant → FileTime
long ft2 = t.toEpochMilli() * 10000L + 116444736000000000L;
v.setDateTime(ft2);

toString​

OpcUaVariant.toString() '스마트 DataType 포맷'을 선택:

DataType출력 예시
Double / Float3.14
Int32 / UInt3242
Stringhello
DateTimeft=132456... (FileTime 원본, 직접 Instant 형식으로 변환하여 표시)
기타<TypeName>

메모리 소유권​

OpcUaVariant 네이티브 핸들 래핑, 구현 AutoCloseable:

출처owns닫을까요?
new OpcUaVariant() 생성자true네
ua.read(...) 간접 보유 (을 통해 OpcUaDataValue)—~에 의해 OpcUaDataValue 인수됨
ua.call(...) 반환된 출력값true예 (항목별로 닫기)
OpcUaDataValue.variant() 반환false아니요 (와 DataValue 함께 해제됨)

모범 사례:

try (OpcUaVariant v = new OpcUaVariant().setDouble(42.0)) {
ua.write("ns=2;s=X", v);
} // 자동 닫기
close 생략 시 결과

close를 생략하면 네이티브 메모리가 누출됩니다(정기적으로 100k회 이상 실행하면 메모리 사용량이 증가합니다). 항상 try-with-resources로 감싼 OpcUaVariant를 사용하십시오.

BuiltinType 전체 목록 (Enums.BuiltinType)​

Null (0), Boolean (1), SByte (2), Byte (3),
Int16 (4), UInt16 (5), Int32 (6), UInt32 (7), Int64 (8), UInt64 (9),
Float (10), Double (11), String (12), DateTime (13), Guid (14),
ByteString (15), XmlElement (16), NodeId (17), ExpandedNodeId (18),
StatusCode (19), QualifiedName (20), LocalizedText (21),
ExtensionObject (22), DataValue (23), Variant (24), DiagnosticInfo (25)

배열 유형​

현재는 스칼라에 중점을 setXxx두고asXxx Java SDK Variant 있습니다. 배열의 읽기/쓰기 기능은 현재 제공되지 않으며, 향후 버전에서 확장될 예정입니다.

ExtensionObject (복잡한 구조)​

OPC UA Server 사용자 정의 구조체(Struct)는 ExtensionObject 래핑을 통해 DataType 구현됩니다. 현재는 기본 ExtensionObject 컨테이너만 SDK 제공되며, 전체 스키마 구문 분석 기능은 향후 버전에서 제공될 예정입니다.

다음 단계​