Skip to content

Commit

Permalink
[improve] Improve the preCheck of PlcCollectImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
ZY945 committed Dec 1, 2024
1 parent 7c9fb0f commit 00b57b3
Showing 1 changed file with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,17 @@ public void preCheck(Metrics metrics) throws IllegalArgumentException {
throw new IllegalArgumentException("PLC collect must have register address");
}
// check timeout is legal
try {
Long.parseLong(metrics.getPlc().getTimeout());
} catch (NumberFormatException e) {
throw new IllegalArgumentException("PLC collect must have valid timeout");
if (Objects.nonNull(metrics.getPlc().getTimeout())) {
try {
Long.parseLong(metrics.getPlc().getTimeout());
} catch (NumberFormatException e) {
throw new IllegalArgumentException("PLC collect must have valid timeout");
}
}

AtomicInteger addressCount = new AtomicInteger();
metrics.getPlc().getRegisterAddresses().forEach(address -> {
if (address.contains("[") && address.contains("]")) {

String[] addressArray = address.split("\\[");
String num = addressArray[1].replace("]", "");
addressCount.addAndGet(Integer.parseInt(num));
Expand All @@ -77,10 +78,14 @@ public void preCheck(Metrics metrics) throws IllegalArgumentException {
}
});
List<String> aliasFields = metrics.getAliasFields();
if (Objects.isNull(aliasFields)) {
throw new IllegalArgumentException("Please ensure that the number of aliasFields (tagName) in yml matches the number of registered addresses" +
"Number of AliasFields(tagList): 0 ,but Number of addresses:" + addressCount.get());
}
int tagListCount = aliasFields.size() - 1;
if (aliasFields.size() - 1 != addressCount.get()) {
throw new IllegalArgumentException("Please ensure that the number of aliasFields (tagName) in yml matches the number of registered addresses" +
"Number of AliasFields(tagList):" + tagListCount + "but Number of addresses:" + addressCount.get());
"Number of AliasFields(tagList): " + tagListCount + " ,but Number of addresses:" + addressCount.get());
}
}

Expand All @@ -90,11 +95,13 @@ public void collect(CollectRep.MetricsData.Builder builder, long monitorId, Stri
long startTime = System.currentTimeMillis();
PlcProtocol plcProtocol = metrics.getPlc();
// check slaveId
if( !StringUtils.hasText(metrics.getPlc().getSlaveId())){
if (!StringUtils.hasText(metrics.getPlc().getSlaveId())) {
plcProtocol.setSlaveId("1");
}
if (!StringUtils.hasText(plcProtocol.getTimeout())) {
plcProtocol.setTimeout("5000");
}
String connectionString = "modbus-tcp:tcp://" + plcProtocol.getHost() + ":" + plcProtocol.getPort() + "?unit-identifier=" + plcProtocol.getSlaveId();
// String connectionString = "modbus-tcp:tcp://127.0.0.1:502?request-timeout=5000&unit-identifier=1";
PlcConnection plcConnection = null;
List<String> registerAddressList = plcProtocol.getRegisterAddresses();
try {
Expand Down Expand Up @@ -140,7 +147,7 @@ public void collect(CollectRep.MetricsData.Builder builder, long monitorId, Stri
if (COIL.equals(plcProtocol.getAddressSyntax())) {
resultMap = resultMap.entrySet()
.stream()
.peek(obj -> obj.setValue(String.valueOf(Boolean.TRUE.equals(Boolean.valueOf(obj.getValue()))?1:0)))
.peek(obj -> obj.setValue(String.valueOf(Boolean.TRUE.equals(Boolean.valueOf(obj.getValue())) ? 1 : 0)))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

}
Expand Down

0 comments on commit 00b57b3

Please sign in to comment.