You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2018/10/18 12:59:38 UTC
[incubator-plc4x] 02/02: - Fine tuned the example
This is an automated email from the ASF dual-hosted git repository.
cdutz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git
commit 5903b53528739953202451ed3560cd74f5420035
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Thu Oct 18 14:59:33 2018 +0200
- Fine tuned the example
---
.../plc4x/java/examples/helloplc4x/HelloPlc4x.java | 126 ++++++++++-----------
1 file changed, 58 insertions(+), 68 deletions(-)
diff --git a/examples/hello-plc4x/src/main/java/org/apache/plc4x/java/examples/helloplc4x/HelloPlc4x.java b/examples/hello-plc4x/src/main/java/org/apache/plc4x/java/examples/helloplc4x/HelloPlc4x.java
index f8928dc..f321248 100644
--- a/examples/hello-plc4x/src/main/java/org/apache/plc4x/java/examples/helloplc4x/HelloPlc4x.java
+++ b/examples/hello-plc4x/src/main/java/org/apache/plc4x/java/examples/helloplc4x/HelloPlc4x.java
@@ -23,98 +23,88 @@ import org.apache.plc4x.java.api.PlcConnection;
import org.apache.plc4x.java.api.messages.PlcReadRequest;
import org.apache.plc4x.java.api.messages.PlcReadResponse;
import org.apache.plc4x.java.api.types.PlcResponseCode;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import java.util.Optional;
import java.util.concurrent.CompletableFuture;
public class HelloPlc4x {
- private static final Logger logger = LoggerFactory.getLogger(HelloPlc4x.class);
-
/**
* Example code do demonstrate using PLC4X.
*
* @param args ignored.
*/
- public static void main(String[] args) {
- if (args.length < 2) {
- System.out.println("Usage: HelloPlc4x {connection-string} {address-string}+");
- System.out.println("Example: HelloPlc4x s7://10.10.64.20/1/1 %Q0.0:BOOL %Q0:BYTE");
+public static void main(String[] args) throws Exception {
+ if (args.length < 2) {
+ System.out.println("Usage: HelloPlc4x {connection-string} {address-string}+");
+ System.out.println("Example: HelloPlc4x s7://10.10.64.20/1/1 %Q0.0:BOOL %Q0:BYTE");
+ return;
+ }
+
+ // Establish a connection to the plc using the url provided as first argument
+ try (PlcConnection plcConnection = new PlcDriverManager().getConnection(args[0])) {
+
+ // Check if this connection support reading of data.
+ Optional<PlcReadRequest.Builder> builderOptional = plcConnection.readRequestBuilder();
+ if (!builderOptional.isPresent()) {
+ System.err.println("This connection doesn't support reading.");
return;
}
- // Establish a connection to the plc using the url provided as first argument
- try (PlcConnection plcConnection = new PlcDriverManager().getConnection(args[0])) {
-
- // Check if this connection support reading of data.
- if (plcConnection.readRequestBuilder().isPresent()) {
+ // Create a new read request:
+ // - Give the single item requested the alias name "value"
+ PlcReadRequest.Builder builder = builderOptional.get();
+ for (int i = 1; i < args.length; i++) {
+ builder.addItem("value-" + i, args[i]);
+ }
+ PlcReadRequest readRequest = builder.build();
- // Create a new read request:
- // - Give the single item requested the alias name "value"
- PlcReadRequest.Builder syncBuilder = plcConnection.readRequestBuilder().get();
- for (int i = 1; i < args.length; i++) {
- syncBuilder.addItem("value-" + i, args[i]);
- }
- PlcReadRequest syncPlcReadRequest = syncBuilder.build();
+ //////////////////////////////////////////////////////////
+ // Read synchronously ...
+ // NOTICE: the ".get()" immediately lets this thread pause until
+ // the response is processed and available.
+ System.out.println("Synchronous request ...");
+ PlcReadResponse syncResponse = readRequest.execute().get();
+ // Simply iterating over the field names returned in the response.
+ printResponse(syncResponse);
- //////////////////////////////////////////////////////////
- // Read synchronously ...
- // NOTICE: the ".get()" immediately lets this thread pause till
- // the response is processed and available.
- System.out.println("\nSynchronous request ...");
- PlcReadResponse syncResponse = syncPlcReadRequest.execute().get();
- // Simply iterating over the field names returned in the response.
+ //////////////////////////////////////////////////////////
+ // Read asynchronously ...
+ // Register a callback executed as soon as a response arrives.
+ System.out.println("Asynchronous request ...");
+ CompletableFuture<? extends PlcReadResponse> asyncResponse = readRequest.execute();
+ asyncResponse.whenComplete((readResponse, throwable) -> {
+ if (readResponse != null) {
printResponse(syncResponse);
-
- //////////////////////////////////////////////////////////
- // Read asynchronously ...
- // Register a callback executed as soon as a response arives.
- PlcReadRequest.Builder asyncBuilder = plcConnection.readRequestBuilder().get();
- for (int i = 1; i < args.length; i++) {
- asyncBuilder.addItem("value-" + i, args[i]);
- }
- PlcReadRequest asyncPlcReadRequest = asyncBuilder.build();
- System.out.println("\n\nAsynchronous request ...");
- CompletableFuture<? extends PlcReadResponse> asyncResponse = asyncPlcReadRequest.execute();
- asyncResponse.whenComplete((readResponse, throwable) -> {
- if (readResponse != null) {
- printResponse(syncResponse);
- } else {
- logger.error("An error occurred", throwable);
- }
- });
} else {
- logger.info("This connection doesn't support reading.");
+ System.err.println("An error occurred: " + throwable.getMessage());
+ throwable.printStackTrace();
}
- }
- // Catch any exception or the application won't be able to finish if something goes wrong.
- catch (Exception e) {
- logger.error("An error occurred", e);
- }
+ });
}
+}
- private static void printResponse(PlcReadResponse syncResponse) {
- for (String fieldName : syncResponse.getFieldNames()) {
- if(syncResponse.getResponseCode(fieldName) == PlcResponseCode.OK) {
- int numValues = syncResponse.getNumberOfValues(fieldName);
- // If it's just one element, output just one single line.
- if(numValues == 1) {
- System.out.println("Value[" + fieldName + "]: " + syncResponse.getObject(fieldName));
- }
- // If it's more than one element, output each in a single row.
- else {
- System.out.println("Value[" + fieldName + "]:");
- for(int i = 0; i < numValues; i++) {
- System.out.println(" - " + syncResponse.getObject(fieldName, i));
- }
- }
+private static void printResponse(PlcReadResponse response) {
+ for (String fieldName : response.getFieldNames()) {
+ if(response.getResponseCode(fieldName) == PlcResponseCode.OK) {
+ int numValues = response.getNumberOfValues(fieldName);
+ // If it's just one element, output just one single line.
+ if(numValues == 1) {
+ System.out.println("Value[" + fieldName + "]: " + response.getObject(fieldName));
}
- // Something went wrong, to output an error message instead.
+ // If it's more than one element, output each in a single row.
else {
- System.out.println("Error[" + fieldName + "]: " + syncResponse.getResponseCode(fieldName).name());
+ System.out.println("Value[" + fieldName + "]:");
+ for(int i = 0; i < numValues; i++) {
+ System.out.println(" - " + response.getObject(fieldName, i));
+ }
}
}
+ // Something went wrong, to output an error message instead.
+ else {
+ System.out.println("Error[" + fieldName + "]: " + response.getResponseCode(fieldName).name());
+ }
}
+}
}