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/27 14:21:20 UTC
[incubator-plc4x] branch master updated: - Get rid of an annoying
sonar report ...
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
The following commit(s) were added to refs/heads/master by this push:
new 2ce3b2f - Get rid of an annoying sonar report ...
2ce3b2f is described below
commit 2ce3b2fcdcba4492e13006765a48415a846725e1
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Sat Oct 27 16:21:15 2018 +0200
- Get rid of an annoying sonar report ...
---
examples/hello-world-plc4x/pom.xml | 7 ++
.../plc4x/java/examples/helloplc4x/HelloPlc4x.java | 119 +++++++++++----------
2 files changed, 68 insertions(+), 58 deletions(-)
diff --git a/examples/hello-world-plc4x/pom.xml b/examples/hello-world-plc4x/pom.xml
index 127da48..a965de9 100644
--- a/examples/hello-world-plc4x/pom.xml
+++ b/examples/hello-world-plc4x/pom.xml
@@ -54,6 +54,12 @@
<version>0.2.0-SNAPSHOT</version>
<scope>runtime</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.plc4x</groupId>
+ <artifactId>plc4j-protocol-test</artifactId>
+ <version>0.2.0-SNAPSHOT</version>
+ <scope>runtime</scope>
+ </dependency>
<dependency>
<groupId>org.slf4j</groupId>
@@ -78,6 +84,7 @@
<configuration>
<usedDependencies combine.children="append">
<usedDependency>org.apache.plc4x:plc4j-protocol-s7</usedDependency>
+ <usedDependency>org.apache.plc4x:plc4j-protocol-test</usedDependency>
<usedDependency>org.slf4j:log4j-over-slf4j</usedDependency>
</usedDependencies>
</configuration>
diff --git a/examples/hello-world-plc4x/src/main/java/org/apache/plc4x/java/examples/helloplc4x/HelloPlc4x.java b/examples/hello-world-plc4x/src/main/java/org/apache/plc4x/java/examples/helloplc4x/HelloPlc4x.java
index 20253e4..6fedf8b 100644
--- a/examples/hello-world-plc4x/src/main/java/org/apache/plc4x/java/examples/helloplc4x/HelloPlc4x.java
+++ b/examples/hello-world-plc4x/src/main/java/org/apache/plc4x/java/examples/helloplc4x/HelloPlc4x.java
@@ -23,86 +23,89 @@ 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.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) 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.
- if (!plcConnection.getMetadata().canRead()) {
- System.err.println("This connection doesn't support reading.");
+ public static void main(String[] args) throws Exception {
+ if (args.length < 2) {
+ logger.info("Usage: HelloPlc4x {connection-string} {address-string}+");
+ logger.info("Example: HelloPlc4x test:plc4x-example-mqtt RANDOM/foo:INTEGER RANDOM/bar:INTEGER");
return;
}
- // Create a new read request:
- // - Give the single item requested the alias name "value"
- PlcReadRequest.Builder builder = plcConnection.readRequestBuilder();
- for (int i = 1; i < args.length; i++) {
- builder.addItem("value-" + i, args[i]);
- }
- PlcReadRequest readRequest = builder.build();
+ // Establish a connection to the plc using the url provided as first argument
+ try (PlcConnection plcConnection = new PlcDriverManager().getConnection(args[0])) {
- //////////////////////////////////////////////////////////
- // 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);
+ // Check if this connection support reading of data.
+ if (!plcConnection.getMetadata().canRead()) {
+ logger.error("This connection doesn't support reading.");
+ return;
+ }
- //////////////////////////////////////////////////////////
- // 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);
- } else {
- System.err.println("An error occurred: " + throwable.getMessage());
- throwable.printStackTrace();
+ // Create a new read request:
+ // - Give the single item requested the alias name "value"
+ PlcReadRequest.Builder builder = plcConnection.readRequestBuilder();
+ for (int i = 1; i < args.length; i++) {
+ builder.addItem("value-" + i, args[i]);
}
- });
+ PlcReadRequest readRequest = builder.build();
+
+ //////////////////////////////////////////////////////////
+ // Read synchronously ...
+ // NOTICE: the ".get()" immediately lets this thread pause until
+ // the response is processed and available.
+ logger.info("Synchronous request ...");
+ PlcReadResponse syncResponse = readRequest.execute().get();
+ // Simply iterating over the field names returned in the response.
+ printResponse(syncResponse);
+
+ //////////////////////////////////////////////////////////
+ // Read asynchronously ...
+ // Register a callback executed as soon as a response arrives.
+ logger.info("Asynchronous request ...");
+ CompletableFuture<? extends PlcReadResponse> asyncResponse = readRequest.execute();
+ asyncResponse.whenComplete((readResponse, throwable) -> {
+ if (readResponse != null) {
+ printResponse(syncResponse);
+ } else {
+ logger.error("An error occurred: " + throwable.getMessage(), throwable);
+ }
+ });
+ }
}
-}
-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));
+ 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) {
+ logger.info("Value[" + fieldName + "]: " + response.getObject(fieldName));
+ }
+ // If it's more than one element, output each in a single row.
+ else {
+ logger.info("Value[" + fieldName + "]:");
+ for(int i = 0; i < numValues; i++) {
+ logger.info(" - " + response.getObject(fieldName, i));
+ }
+ }
}
- // If it's more than one element, output each in a single row.
+ // Something went wrong, to output an error message instead.
else {
- System.out.println("Value[" + fieldName + "]:");
- for(int i = 0; i < numValues; i++) {
- System.out.println(" - " + response.getObject(fieldName, i));
- }
+ logger.error("Error[" + fieldName + "]: " + response.getResponseCode(fieldName).name());
}
}
- // Something went wrong, to output an error message instead.
- else {
- System.out.println("Error[" + fieldName + "]: " + response.getResponseCode(fieldName).name());
- }
}
-}
}