파일 전송 (OpcUaFile)
개요
OPC UA Part 5 §10 FileType (i=11575) 6개의 메서드를 노출합니다.SDK 클래스로 OpcUaFile 캡슐화되어 펌웨어 업그레이드 및 레시피 전송에 사용됩니다.
API
| 메서드 / 속성 | 분류 | 읽기 및 쓰기 | 설명 |
|---|---|---|---|
| new OpcUaFile(ua, fileNodeId) | 구조 | — | FileType 인스턴스에 바인딩 |
| file.size() | 속성 | 읽기 | 파일 크기 (long) |
| file.writable() | 속성 | 읽기 | 쓰기 가능 여부 (부울) |
| file.userWritable() | 속성 | 읽기 | 현재 사용자가 쓰기 가능 (boolean) |
| file.openCount() | 속성 | 읽기 | 현재 열람 횟수 (int) |
| file.open(mode) | 방법 | 쓰기 | 열기, mode가 OpcUaFile.Mode.* 인 byte 플래그 |
| file.read(length) | 방법 | 읽기 | N 바이트를 읽고, 반환 Map.Entry<Enums.StatusCode, byte[]> |
| file.write(bytes) | 방법 | 쓰기 | 쓰기, Enums.StatusCode를 반환합니다 |
| file.closeFile() | 방법 | 쓰기 | FileType.Close를 호출하여 핸들을 닫습니다. |
| file.close() | 방법 | 쓰기 | AutoCloseable.close(), 내부 메서드 closeFile() |
| file.downloadTo(localPath, progress) | 고층 | 읽기 | 원클릭 다운로드, localPath가 Path 이거나 인 경우 String, 반환 Enums.StatusCode |
| file.uploadFrom(localPath, progress) | 고층 | 쓰기 | 원클릭 업로드, localPath가 Path 이거나 인 경우 String, 반환 Enums.StatusCode |
관련 구조:
OpcUaFile.Mode 는정적 상수 클래스 (enum이 아님), 4개의 byte 플래그 비트를 비트별 OR 연산으로 조합할 수 있음 (Part 5 §10.4.1):
public static final class Mode {
public static final byte READ = 0x01; // 읽기
public static final byte WRITE = 0x02; // 쓰기
public static final byte ERASE_EXISTING = 0x04; // 비우기
public static final byte APPEND = 0x08; // 추가
}
예: f.open((byte) (OpcUaFile.Mode.WRITE | OpcUaFile.Mode.ERASE_EXISTING));
코드 예시
import com.darra.opcua.*;
import java.util.Map;
import java.util.function.BiConsumer;
try (OpcUaSession ua = new OpcUaSession("opc.tcp://device:4840")) {
ua.connect();
// 1) 원클릭 펌웨어 다운로드 (localPath String 또는 사용 가능 java.nio.file.Path)
try (OpcUaFile file = new OpcUaFile(ua, "ns=2;s=Firmware.Image")) {
System.out.println("Size = " + file.size());
BiConsumer<Long, Long> progress = (done, total) ->
System.out.printf("\r%d%% (%d/%d)", done * 100 / total, done, total);
Enums.StatusCode rc = file.downloadTo("C:/dl/firmware.bin", progress);
if (rc != Enums.StatusCode.Good)
System.out.println("\n下载失败: " + rc);
}
// 2) 한 번의 클릭으로 레시피 업로드
try (OpcUaFile f2 = new OpcUaFile(ua, "ns=2;s=Recipe.Current")) {
f2.uploadFrom("C:/recipes/new.csv", null);
}
// 3) 수동 스트리밍 읽기
try (OpcUaFile f3 = new OpcUaFile(ua, "ns=2;s=Logs.Current")) {
f3.open(OpcUaFile.Mode.READ);
while (true) {
Map.Entry<Enums.StatusCode, byte[]> r = f3.read(4096);
byte[] chunk = r.getValue();
if (r.getKey() != Enums.StatusCode.Good || chunk == null || chunk.length == 0) break;
System.out.println("chunk " + chunk.length + " bytes");
}
f3.closeFile();
}
}
구현 설명
- 블록 크기 기본값 4 KB
- ByteString ISO-8859-1 문자열 연결, 손실 없이 유지 0x00-0xFF
- Method NodeId 분석: 우선순위 TranslateBrowsePaths, 대체(fallback) 클래스 수준 NodeId
모범 사례
- 항상
try-with-resources,를 사용해야 하며, close를 생략하면 서버 핸들이 누수될 수 있습니다. - 대용량 파일 분할 재시도
- 동일한 fileHandle을 동시에 사용하지 마십시오.
다국어 대조
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| OpcUaFile | OpcUaFile | OpcUaFile | OpcUaFile | OpcUaFile | DarraUa_OpcUaFile_* |
| DownloadTo | download_to | downloadTo | DownloadTo | download_to | DarraUa_OpcUaFile_DownloadTo |
C 측면 핸들 계열은 DarraUa_OpcUaFile_*: _New / _Free / _Open / _Close / _Read / _Write / _GetPosition / _SetPosition / _Size / _OpenCount / _Writable / _UserWritable / 이며 _IsOpen, 파일 전체에 대한 간편한 접근 경로 DarraUa_OpcUaFile_DownloadTo / 도 있습니다 _UploadFrom.