Skip to content

Commit

Permalink
fix(plc4j/examples): fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Sep 2, 2022
1 parent 36881ba commit 326739e
Showing 1 changed file with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.plc4x.java.examples.helloplc4x.discoverandbrowse;

import org.apache.commons.lang3.StringUtils;
import org.apache.plc4x.java.PlcDriverManager;
import org.apache.plc4x.java.api.PlcConnection;
import org.apache.plc4x.java.api.PlcDriver;
Expand All @@ -37,18 +38,18 @@ public static void main(String[] args) throws Exception {
PlcDriverManager driverManager = new PlcDriverManager();
for (String protocolCode : driverManager.listDrivers()) {
PlcDriver driver = driverManager.getDriver(protocolCode);
if(driver.getMetadata().canDiscover()) {
if (driver.getMetadata().canDiscover()) {
logger.info("Performing discovery for {} protocol", driver.getProtocolName());

PlcDiscoveryRequest discoveryRequest = driver.discoveryRequestBuilder().build();

discoveryRequest.executeWithHandler(discoveryItem -> {
logger.info(" - Found device with connection-url {}", discoveryItem.getConnectionUrl());
try (PlcConnection connection = driverManager.getConnection(discoveryItem.getConnectionUrl())) {
if(connection.getMetadata().canBrowse()) {
if (connection.getMetadata().canBrowse()) {
PlcBrowseRequest browseRequest = connection.browseRequestBuilder().build();
browseRequest.execute().whenComplete((browseResponse, throwable) -> {
if(throwable != null) {
if (throwable != null) {
throwable.printStackTrace();
} else {
for (PlcBrowseItem value : browseResponse.getValues()) {
Expand All @@ -57,8 +58,6 @@ public static void main(String[] args) throws Exception {
}
});
}
} catch (PlcConnectionException e) {
throw new RuntimeException(e);
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand All @@ -68,14 +67,14 @@ public static void main(String[] args) throws Exception {
}

protected static void outputBrowseItem(PlcBrowseItem browseItem, int indent) {
StringBuilder sb = new StringBuilder();
sb.append(" ".repeat(Math.max(0, indent)));
System.out.println(String.format(sb + "%s : %s (%s %s %s)", browseItem.getAddress(),
System.out.printf("%s%s : %s (%s %s %s)%n",
StringUtils.repeat(" ", Math.max(0, indent)),
browseItem.getAddress(),
browseItem.getPlcValueType().name(),
browseItem.isReadable() ? "R" : " ",
browseItem.isReadable() ? "W" : " ",
browseItem.isReadable() ? "S" : " "));
if(!browseItem.getChildren().isEmpty()) {
browseItem.isReadable() ? "S" : " ");
if (!browseItem.getChildren().isEmpty()) {
for (PlcBrowseItem child : browseItem.getChildren()) {
outputBrowseItem(child, indent + 1);
}
Expand Down

0 comments on commit 326739e

Please sign in to comment.