in hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/plc/AbstractPlcCollectImpl.java [61:109]
public void preCheck(Metrics metrics) throws IllegalArgumentException {
if (metrics == null || metrics.getPlc() == null) {
throw new IllegalArgumentException("PLC collect must have PLC params");
}
// check driver name
if (metrics.getPlc().getDriverName() == null || !ArrayUtils.contains(DRIVER_LIST, metrics.getPlc().getDriverName())) {
throw new IllegalArgumentException("PLC collect must have valid driver name");
}
// check address syntax
if (!ArrayUtils.contains(ADDRESS_SYNTAX, metrics.getPlc().getAddressSyntax())) {
throw new IllegalArgumentException("PLC collect must have valid address syntax");
}
// check register address
if (metrics.getPlc().getRegisterAddresses() == null || metrics.getPlc().getRegisterAddresses().isEmpty()) {
throw new IllegalArgumentException("PLC collect must have register address");
}
// check timeout is legal
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));
} else {
addressCount.addAndGet(1);
}
});
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());
}
}