anganing opened a new issue, #1027:
URL: https://github.com/apache/plc4x/issues/1027
### What happened?
hello,
I want to replace milo with plc4j.
the plc4j is very slow than milo:
milo:539ms, plc4j:22s.
The gap is very large.
but, if I use the same connection(no close it, use it again), it cost 83 ms.
what can I do for cost less time?
use plc4j code:
```
private List<StationStateDto> selectStationStateByPlc4x(List<String>
stationCodeList, String deviceCode) throws Exception {
// 根据设备编码查询设备信息
DcDeviceInfo dcDeviceInfo = new
LambdaQueryChainWrapper<>(dcDeviceInfoMapper)
.eq(DcDeviceInfo::getDeviceCode, deviceCode)
.one();
// 查询S7头表信息
DcDeviceS7Header dcDeviceS7Header = new
LambdaQueryChainWrapper<>(dcDeviceS7HeaderMapper)
.eq(DcDeviceS7Header::getDeviceId,
dcDeviceInfo.getDeviceId())
.one();
// 查询S7明细表信息
List<DcDeviceS7Detail> dcDeviceS7Details = new
LambdaQueryChainWrapper<>(dcDeviceS7DetailMapper)
.eq(DcDeviceS7Detail::getS7HeaderId,
dcDeviceS7Header.getS7HeaderId())
.list();
// 将dcDeviceS7Details转换为Map便于查找
Map<String, DcDeviceS7Detail> dcDeviceS7DetailMap =
dcDeviceS7Details.stream()
.filter(dcDeviceS7Detail ->
dcDeviceS7Detail.getMatchingCode() != null &&
!dcDeviceS7Detail.getMatchingCode().isEmpty())
.collect(Collectors.toMap(DcDeviceS7Detail::getMatchingCode,
dcDeviceS7Detail -> dcDeviceS7Detail));
// 获取OPC_UA连接
log.info("连接设备:{}", dcDeviceInfo.getDeviceName());
PlcConnection plcConnection = new
PlcDriverManager().getConnection(OpcUaUtils.getPlc4xConnectionString(dcDeviceS7Header.getEndPointUrl()));
boolean canRead = plcConnection.getMetadata().canRead();
if (!canRead) {
log.error("该plc4x连接不支持读取数据");
throw new BizException("该连接不支持读取数据");
}
List<StationStateDto> stationStateDtoList = new ArrayList<>();
PlcReadRequest.Builder builder = plcConnection.readRequestBuilder();
for (String stationCode : stationCodeList) {
DcDeviceS7Detail dcDeviceS7Detail =
dcDeviceS7DetailMap.get(stationCode);
String fieldQuery = "ns="+dcDeviceS7Detail.getNamespaceIndex() +
";s=" + dcDeviceS7Detail.getIdentifier() + ";" + dcDeviceS7Detail.getDataType();
builder.addItem(stationCode, fieldQuery);
}
PlcReadRequest readRequest = builder.build();
PlcReadResponse response = readRequest.execute().get();
for (String fieldName : response.getFieldNames()) {
if (response.getResponseCode(fieldName) == PlcResponseCode.OK) {
StationStateDto stationStateDto = new StationStateDto();
stationStateDto.setStationCode(fieldName);
stationStateDto.setState((Short)
response.getObject(fieldName));
stationStateDtoList.add(stationStateDto);
} else {
log.error("Error[" + fieldName + "]: " +
response.getResponseCode(fieldName).name());
}
}
// 关闭连接
plcConnection.close();
log.info("工位占用状态集合:{}",
JSONUtil.toJsonPrettyStr(stationStateDtoList));
return stationStateDtoList;
}
```
use milo code:
```
public List<StationStateDto> selectStationState(List<String>
stationCodeList, String deviceCode) throws Exception {
// 根据设备编码查询设备信息
DcDeviceInfo dcDeviceInfo = new
LambdaQueryChainWrapper<>(dcDeviceInfoMapper)
.eq(DcDeviceInfo::getDeviceCode, deviceCode)
.one();
// 查询S7头表信息
DcDeviceS7Header dcDeviceS7Header = new
LambdaQueryChainWrapper<>(dcDeviceS7HeaderMapper)
.eq(DcDeviceS7Header::getDeviceId,
dcDeviceInfo.getDeviceId())
.one();
// 查询S7明细表信息
List<DcDeviceS7Detail> dcDeviceS7Details = new
LambdaQueryChainWrapper<>(dcDeviceS7DetailMapper)
.eq(DcDeviceS7Detail::getS7HeaderId,
dcDeviceS7Header.getS7HeaderId())
.list();
// 获取OPC_UA连接
log.info("连接设备:{}", dcDeviceInfo.getDeviceName());
OpcUaClient opcUaClient =
OpcUaUtils.createClient(dcDeviceS7Header.getEndPointUrl(),
dcDeviceS7Header.getApplicationName(), dcDeviceS7Header.getApplicationUri());
// 开启连接
opcUaClient.connect().get();
List<StationStateDto> stationStateDtoList = new ArrayList<>();
for (String stationCode : stationCodeList) {
OpcUaReadSubscribeVo opcUaReadSubscribeVo = new
OpcUaReadSubscribeVo();
opcUaReadSubscribeVo.setNamespaceIndex(dcDeviceS7Details.stream().filter(p ->
Objects.equals(p.getMatchingCode(),
stationCode)).collect(Collectors.toList()).get(0).getNamespaceIndex());
opcUaReadSubscribeVo.setIdentifier(dcDeviceS7Details.stream().filter(p ->
Objects.equals(p.getMatchingCode(),
stationCode)).collect(Collectors.toList()).get(0).getIdentifier());
OpcNodeId opcNodeId = OpcUaUtils.readNode(opcUaClient,
opcUaReadSubscribeVo);
StationStateDto stationStateDto = new StationStateDto();
stationStateDto.setStationCode(stationCode);
stationStateDto.setState((Short) opcNodeId.getValue());
stationStateDtoList.add(stationStateDto);
}
// 关闭连接
opcUaClient.disconnect().get();
log.info("工位占用状态集合:{}",
JSONUtil.toJsonPrettyStr(stationStateDtoList));
return stationStateDtoList;
// return selectStationStateByPlc4x(stationCodeList, deviceCode);
}
```
### Version
v0.10.0
### Programming Languages
- [X] plc4j
- [ ] plc4go
- [ ] plc4c
- [ ] plc4net
### Protocols
- [ ] AB-Ethernet
- [ ] ADS /AMS
- [ ] BACnet/IP
- [ ] CANopen
- [ ] DeltaV
- [ ] DF1
- [ ] EtherNet/IP
- [ ] Firmata
- [ ] KNXnet/IP
- [ ] Modbus
- [X] OPC-UA
- [ ] S7
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]