6. OPC 服务
通过 client.opc() 进行访问。OPC 表示源连接(Source Connection)。它是外部数据进入的入口点,例如 OPC UA 服务器、PLC、Modbus 设备、数据库、消息代理、REST API 等。
ID 规则: 必须带有
OPC_前缀。详情请参考 域名 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_source_type) | List<OPCResponseV5> | GET | /api/v5/opc?opc_source_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_protocol, 例如: S7·LS·MELSEC·MODBUS·OPCUA·MQTT_CLIENT·API)。
源类型和采集主体由协议派生,无需直接填写。协议词汇的正本位于 plantpulse-event 的协议目录中。
| 源类型 | 用途 |
|---|---|
OPC | OPC UA 服务器 |
PLC | Siemens, LS, Mitsubishi, Allen-Bradley 等 PLC 直连 |
MODBUS | Modbus TCP/UDP/RTU 设备 |
DATABASE | 外部数据库轮询 |
MESSAGING | 消息代理·推送协议 (MQTT, Kafka, HTTP, WebSocket, CoAP, ROS 2) |
API | 进入 PlantPulse REST API 的数据 |
SYSTEM | Edge 网关自监控 (OPC_SYSTEM_<edge_id>, 由 Edge 声明) |
6.3 OPCRequestV5 / OPCResponseV5
| 字段 | 类型 | 说明 |
|---|---|---|
opc_id | String | OPC ID (PK, OPC_ 必填) |
opc_name | String | OPC 显示名称 |
opc_protocol | String | 协议代码 (例如: S7, LS, OPCUA, API) |
opc_source_ip | String | 实际源(PLC·设备·OPC UA 服务器·DB·代理)的 IP 或主机 |
opc_source_port | int | 实际源的端口 |
opc_edge_id | String | 采集此连接的 Edge。若由 Platform 直接采集则留空 |
opc_source_type | String | (仅响应) 从协议派生的源类型 |
opc_collected_by | String | (仅响应) EDGE / PLATFORM — 从协议派生 |
site_id | String | 所属站点 |
description | String | 说明 |
Edge 采集连接(具有
opc_edge_id的连接)的协议与地址仅通过 Edge sync(PUT /api/v5/opc/{id}/sync)写入。在 Platform 界面中为只读。
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_protocol("S7");
req.setSite_id("SITE_DJ");
req.setOpc_source_ip("192.168.10.50");
req.setOpc_source_port(102);
OPCResponseV5 created = client.opc().create(req);
创建 OPC (外部 REST API)
OPCRequestV5 api = new OPCRequestV5();
api.setOpc_id("OPC_API_ERP_001"); // OPC_ 접두사 필수
api.setOpc_name("ERP 생산 실적 API");
api.setOpc_protocol("API");
api.setSite_id("SITE_DJ");
api.setDescription("매시간 ERP에서 생산 카운트 폴링");
client.opc().create(api);
创建 OPC (测试用 — API 源)
OPCRequestV5 v = new OPCRequestV5();
v.setOpc_id("OPC_TEST_001");
v.setOpc_name("테스트용 OPC");
v.setOpc_protocol("API");
v.setSite_id("SITE_000001");
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_protocol() + ", " + opc.getOpc_source_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_protocol("S7");
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_protocol("S7");
plc.setSite_id("SITE_DJ");
plc.setOpc_source_ip("192.168.10.60");
plc.setOpc_source_port(102);
client.opc().create(plc);
// 2) 수집이 시작되면 — 이후 Tag 서비스에서 확인
List<TagResponseV5> tags = client.tag().listByOpc("OPC_PLC_L2_001");
System.out.println("수집된 태그 수: " + tags.size());