6. OPC 服务
通过 client.opc() 访问。OPC 表示数据采集通道(Data Source),即 OPC UA 服务器、PLC、Modbus、RDBMS、REST API/文件、虚拟通道等外部数据的接入点。
ID 规则:必须使用
OPC_或API_前缀。详细内容参见 域 ID 规则。
6.1 方法一览
| 方法 | 返回类型 | HTTP | 端点 |
|---|---|---|---|
create(OPCRequestV5) | OPCResponseV5 或 null | POST | /api/v5/opc |
update(opc_id, OPCRequestV5) | OPCResponseV5 或 null | PUT | /api/v5/opc/{id} |
delete(opc_id) | boolean | DELETE | /api/v5/opc/{id} |
get(opc_id) | OPCResponseV5 或 null | GET | /api/v5/opc/{id} |
list() | List<OPCResponseV5> | GET | /api/v5/opc |
listBySite(site_id) | List<OPCResponseV5> | GET | /api/v5/opc?site_id=... |
listByType(opc_type) | List<OPCResponseV5> | GET | /api/v5/opc?opc_type=... |
exists(opc_id) | boolean | GET | /api/v5/opc/{id}/exists |
count() | long | GET | /api/v5/opc/count |
countBySite(site_id) | long | GET | /api/v5/opc/count?site_id=... |
6.2 OPC Type 取值
| 取值 | 用途 |
|---|---|
OPC | OPC UA / OPC DA 服务器 |
PLC | Siemens、Mitsubishi、Allen-Bradley 等 PLC 直连 |
MODBUS | Modbus TCP/RTU 设备 |
DATABASE | 外部 RDBMS(Oracle、MSSQL 等) |
FILE | REST API / 文件导入 |
VIRTUAL | 虚拟通道(用于仿真与测试) |
6.3 OPCRequestV5 / OPCResponseV5
| 字段 | 类型 | 说明 |
|---|---|---|
opc_id | String | OPC ID(PK,必须为 OPC_ 或 API_) |
opc_name | String | OPC 显示名称 |
opc_type | String | OPC/PLC/MODBUS/DATABASE/FILE/VIRTUAL |
opc_sub_type | String | 细分类型(例如:PLC 厂商) |
opc_server_ip | String | OPC 服务器 IP |
opc_agent_ip | String | 数据采集代理 IP |
opc_agent_port | int | 代理端口 |
site_id | String | 所属站点 |
description | String | 说明 |
6.4 使用示例
创建 OPC(PLC 直连)
import plantpulse.api.v5.dto.request.OPCRequestV5;
import plantpulse.api.v5.dto.response.OPCResponseV5;
OPCRequestV5 req = new OPCRequestV5();
req.setOpc_id("OPC_PLC_L1_001");
req.setOpc_name("Line1 메인 PLC");
req.setOpc_type("PLC");
req.setOpc_sub_type("SIEMENS_S7");
req.setOpc_server_ip("192.168.10.50");
req.setSite_id("SITE_DJ");
req.setOpc_agent_ip("192.168.10.5");
req.setOpc_agent_port(60000);
OPCResponseV5 created = client.opc().create(req);
创建 OPC(外部 REST API)
OPCRequestV5 api = new OPCRequestV5();
api.setOpc_id("API_ERP_001"); // 관례상 API_ 접두사
api.setOpc_name("ERP 생산 실적 API");
api.setOpc_type("FILE");
api.setSite_id("SITE_DJ");
api.setDescription("매시간 ERP에서 생산 카운트 폴링");
client.opc().create(api);
创建 OPC(虚拟 — 测试用)
OPCRequestV5 v = new OPCRequestV5();
v.setOpc_id("OPC_VIRTUAL_001");
v.setOpc_name("테스트용 가상 OPC");
v.setOpc_type("VIRTUAL");
v.setSite_id("SITE_00001");
v.setOpc_agent_ip("127.0.0.1");
v.setOpc_agent_port(0);
client.opc().create(v);
按站点查询 OPC
import java.util.List;
List<OPCResponseV5> djOpcs = client.opc().listBySite("SITE_DJ");
for (OPCResponseV5 opc : djOpcs) {
System.out.println(opc.getOpc_id() + " (" + opc.getOpc_type() + ")");
}
按类型查询 OPC
// 모든 사이트의 PLC만
List<OPCResponseV5> plcs = client.opc().listByType("PLC");
// 모든 사이트의 Modbus만
List<OPCResponseV5> modbus = client.opc().listByType("MODBUS");
计数
long total = client.opc().count();
long djCount = client.opc().countBySite("SITE_DJ");
System.out.printf("전체 OPC: %d, 대전공장: %d%n", total, djCount);
CUD 生命周期
String opcId = "OPC_PLC_L1_001";
// 생성
OPCRequestV5 req = new OPCRequestV5();
req.setOpc_id(opcId);
req.setOpc_name("초기 이름");
req.setOpc_type("PLC");
req.setSite_id("SITE_DJ");
client.opc().create(req);
// 존재 여부 확인
assert client.opc().exists(opcId);
// 수정
req.setOpc_name("수정된 이름");
client.opc().update(opcId, req);
// 삭제
boolean deleted = client.opc().delete(opcId);
assert !client.opc().exists(opcId);
6.5 应用场景
场景 — 新产线 PLC 注册后等待标签采集
// 1) OPC 등록
OPCRequestV5 plc = new OPCRequestV5();
plc.setOpc_id("OPC_PLC_L2_001");
plc.setOpc_type("PLC");
plc.setSite_id("SITE_DJ");
plc.setOpc_server_ip("192.168.10.60");
plc.setOpc_agent_ip("192.168.10.5");
plc.setOpc_agent_port(60000);
client.opc().create(plc);
// 2) 에이전트가 자동으로 태그를 수집하면 — 이후 Tag 서비스에서 확인
List<TagResponseV5> tags = client.tag().listByOpc("OPC_PLC_L2_001");
System.out.println("수집된 태그 수: " + tags.size());