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/11/21 08:58:18 UTC

[incubator-plc4x] branch develop updated: - Unified the way the examples are configured even more.

This is an automated email from the ASF dual-hosted git repository.

cdutz pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new 6bf1908  - Unified the way the examples are configured even more.
6bf1908 is described below

commit 6bf1908d0523abb861a1d9d23cb61589a4ed1e82
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Wed Nov 21 09:58:15 2018 +0100

    - Unified the way the examples are configured even more.
---
 .../java/examples/cloud/azure/CliOptions.java      | 12 ++++--
 .../cloud/azure/S7PlcToAzureIoTHubSample.java      |  3 +-
 .../java/examples/cloud/google/CliOptions.java     | 12 ++++--
 .../cloud/google/S7PlcToGoogleIoTCoreSample.java   |  3 +-
 examples/hello-integration-edgent/pom.xml          |  6 +++
 .../examples/integration/edgent}/CliOptions.java   | 43 ++++++++++++++++------
 .../examples/integration/edgent/PlcLogger.java     | 18 ++++-----
 .../plc4x/java/examples/helloplc4x/CliOptions.java | 12 ++++--
 .../plc4x/java/examples/helloplc4x/HelloPlc4x.java |  3 +-
 9 files changed, 79 insertions(+), 33 deletions(-)

diff --git a/examples/hello-cloud-azure/src/main/java/org/apache/plc4x/java/examples/cloud/azure/CliOptions.java b/examples/hello-cloud-azure/src/main/java/org/apache/plc4x/java/examples/cloud/azure/CliOptions.java
index 42fb547..c8f7739 100644
--- a/examples/hello-cloud-azure/src/main/java/org/apache/plc4x/java/examples/cloud/azure/CliOptions.java
+++ b/examples/hello-cloud-azure/src/main/java/org/apache/plc4x/java/examples/cloud/azure/CliOptions.java
@@ -23,13 +23,14 @@ import org.apache.commons.cli.*;
 
 public class CliOptions {
 
+    private static Options options;
+
     private final String plc4xConnectionString;
     private final String plc4xFieldAddress;
     private final String iotHubConnectionString;
 
-    /** Construct an MqttExampleOptions class from command line flags. */
-    public static CliOptions fromFlags(String[] args) {
-        org.apache.commons.cli.Options options = new org.apache.commons.cli.Options();
+    public static CliOptions fromArgs(String[] args) {
+        options = new Options();
         // Required arguments
         options.addOption(
             Option.builder()
@@ -72,6 +73,11 @@ public class CliOptions {
         }
     }
 
+    public static void printHelp() {
+        HelpFormatter formatter = new HelpFormatter();
+        formatter.printHelp("S7PlcToAzureIoTHubSample", options);
+    }
+
     public CliOptions(String plc4xConnectionString, String plc4xFieldAddress, String iotHubConnectionString) {
         this.plc4xConnectionString = plc4xConnectionString;
         this.plc4xFieldAddress = plc4xFieldAddress;
diff --git a/examples/hello-cloud-azure/src/main/java/org/apache/plc4x/java/examples/cloud/azure/S7PlcToAzureIoTHubSample.java b/examples/hello-cloud-azure/src/main/java/org/apache/plc4x/java/examples/cloud/azure/S7PlcToAzureIoTHubSample.java
index 34f8c3c..6eb625a 100644
--- a/examples/hello-cloud-azure/src/main/java/org/apache/plc4x/java/examples/cloud/azure/S7PlcToAzureIoTHubSample.java
+++ b/examples/hello-cloud-azure/src/main/java/org/apache/plc4x/java/examples/cloud/azure/S7PlcToAzureIoTHubSample.java
@@ -42,8 +42,9 @@ public class S7PlcToAzureIoTHubSample {
      * @param args Expected: [plc4x connection string, plc4x field address, IoT-Hub connection string].
      */
     public static void main(String[] args) throws Exception {
-        CliOptions options = CliOptions.fromFlags(args);
+        CliOptions options = CliOptions.fromArgs(args);
         if (options == null) {
+            CliOptions.printHelp();
             // Could not parse.
             System.exit(1);
         }
diff --git a/examples/hello-cloud-google/src/main/java/org/apache/plc4x/java/examples/cloud/google/CliOptions.java b/examples/hello-cloud-google/src/main/java/org/apache/plc4x/java/examples/cloud/google/CliOptions.java
index ae72ff0..82c1b6e 100644
--- a/examples/hello-cloud-google/src/main/java/org/apache/plc4x/java/examples/cloud/google/CliOptions.java
+++ b/examples/hello-cloud-google/src/main/java/org/apache/plc4x/java/examples/cloud/google/CliOptions.java
@@ -24,6 +24,8 @@ import org.apache.commons.cli.*;
 /** Command line options for the MQTT example. */
 public class CliOptions {
 
+    private static Options options;
+
     private final String projectId;
     private final String registryId;
     private final String deviceId;
@@ -35,9 +37,8 @@ public class CliOptions {
     private final short mqttBridgePort;
     private final String messageType;
 
-    /** Construct an MqttExampleOptions class from command line flags. */
-    public static CliOptions fromFlags(String[] args) {
-        org.apache.commons.cli.Options options = new org.apache.commons.cli.Options();
+    public static CliOptions fromArgs(String[] args) {
+        options = new Options();
         // Required arguments
         options.addOption(
             Option.builder()
@@ -158,6 +159,11 @@ public class CliOptions {
         }
     }
 
+    public static void printHelp() {
+        HelpFormatter formatter = new HelpFormatter();
+        formatter.printHelp("S7PlcToGoogleIoTCoreSample", options);
+    }
+
     public CliOptions(String projectId, String registryId, String deviceId, String privateKeyFile, String algorithm,
                       String cloudRegion, int tokenExpMins, String mqttBridgeHostname, short mqttBridgePort,
                       String messageType) {
diff --git a/examples/hello-cloud-google/src/main/java/org/apache/plc4x/java/examples/cloud/google/S7PlcToGoogleIoTCoreSample.java b/examples/hello-cloud-google/src/main/java/org/apache/plc4x/java/examples/cloud/google/S7PlcToGoogleIoTCoreSample.java
index 2301ae2..61484e6 100644
--- a/examples/hello-cloud-google/src/main/java/org/apache/plc4x/java/examples/cloud/google/S7PlcToGoogleIoTCoreSample.java
+++ b/examples/hello-cloud-google/src/main/java/org/apache/plc4x/java/examples/cloud/google/S7PlcToGoogleIoTCoreSample.java
@@ -152,8 +152,9 @@ public class S7PlcToGoogleIoTCoreSample {
     public static void main(String[] args) throws Exception {
 
         // [START iot_mqtt_configuremqtt]
-        CliOptions options = CliOptions.fromFlags(args);
+        CliOptions options = CliOptions.fromArgs(args);
         if (options == null) {
+            CliOptions.printHelp();
             // Could not parse.
             System.exit(1);
         }
diff --git a/examples/hello-integration-edgent/pom.xml b/examples/hello-integration-edgent/pom.xml
index b5b6925..e627ef2 100644
--- a/examples/hello-integration-edgent/pom.xml
+++ b/examples/hello-integration-edgent/pom.xml
@@ -58,6 +58,12 @@
       <version>1.2.0</version>
     </dependency>
 
+    <dependency>
+      <groupId>commons-cli</groupId>
+      <artifactId>commons-cli</artifactId>
+      <version>1.4</version>
+    </dependency>
+
     <!-- Required driver implementation -->
     <dependency>
       <groupId>org.apache.plc4x</groupId>
diff --git a/examples/hello-world-plc4x/src/main/java/org/apache/plc4x/java/examples/helloplc4x/CliOptions.java b/examples/hello-integration-edgent/src/main/java/org/apache/plc4x/java/examples/integration/edgent/CliOptions.java
similarity index 62%
copy from examples/hello-world-plc4x/src/main/java/org/apache/plc4x/java/examples/helloplc4x/CliOptions.java
copy to examples/hello-integration-edgent/src/main/java/org/apache/plc4x/java/examples/integration/edgent/CliOptions.java
index 7efbda4..906f861 100644
--- a/examples/hello-world-plc4x/src/main/java/org/apache/plc4x/java/examples/helloplc4x/CliOptions.java
+++ b/examples/hello-integration-edgent/src/main/java/org/apache/plc4x/java/examples/integration/edgent/CliOptions.java
@@ -17,18 +17,20 @@
  under the License.
  */
 
-package org.apache.plc4x.java.examples.helloplc4x;
+package org.apache.plc4x.java.examples.integration.edgent;
 
 import org.apache.commons.cli.*;
 
 public class CliOptions {
 
+    private static Options options;
+
     private final String connectionString;
-    private final String[] fieldAddress;
+    private final String fieldAddress;
+    private final int pollingInterval;
 
-    /** Construct an MqttExampleOptions class from command line flags. */
-    public static CliOptions fromFlags(String[] args) {
-        org.apache.commons.cli.Options options = new org.apache.commons.cli.Options();
+    public static CliOptions fromArgs(String[] args) {
+        options = new Options();
         // Required arguments
         options.addOption(
             Option.builder()
@@ -41,9 +43,17 @@ public class CliOptions {
         options.addOption(
             Option.builder()
                 .type(String.class)
-                .longOpt("field-addresses")
+                .longOpt("field-address")
+                .hasArg()
+                .desc("Field Address.")
+                .required()
+                .build());
+        options.addOption(
+            Option.builder()
+                .type(Integer.class)
+                .longOpt("polling-interval")
                 .hasArg()
-                .desc("Field Addresses (Space separated).")
+                .desc("Polling Interval (milliseconds).")
                 .required()
                 .build());
 
@@ -53,26 +63,37 @@ public class CliOptions {
             commandLine = parser.parse(options, args);
 
             String connectionString = commandLine.getOptionValue("connection-string");
-            String[] fieldAddress = commandLine.getOptionValues("field-addresses");
+            String fieldAddress = commandLine.getOptionValue("field-address");
+            int pollingInterval = Integer.valueOf(commandLine.getOptionValue("polling-interval"));
 
-            return new CliOptions(connectionString, fieldAddress);
+            return new CliOptions(connectionString, fieldAddress, pollingInterval);
         } catch (ParseException e) {
             System.err.println(e.getMessage());
             return null;
         }
     }
 
-    public CliOptions(String connectionString, String[] fieldAddress) {
+    public static void printHelp() {
+        HelpFormatter formatter = new HelpFormatter();
+        formatter.printHelp("PlcLogger", options);
+    }
+
+    public CliOptions(String connectionString, String fieldAddress, int pollingInterval) {
         this.connectionString = connectionString;
         this.fieldAddress = fieldAddress;
+        this.pollingInterval = pollingInterval;
     }
 
     public String getConnectionString() {
         return connectionString;
     }
 
-    public String[] getFieldAddress() {
+    public String getFieldAddress() {
         return fieldAddress;
     }
 
+    public int getPollingInterval() {
+        return pollingInterval;
+    }
+
 }
diff --git a/examples/hello-integration-edgent/src/main/java/org/apache/plc4x/java/examples/integration/edgent/PlcLogger.java b/examples/hello-integration-edgent/src/main/java/org/apache/plc4x/java/examples/integration/edgent/PlcLogger.java
index a63ab95..b1b5a13 100644
--- a/examples/hello-integration-edgent/src/main/java/org/apache/plc4x/java/examples/integration/edgent/PlcLogger.java
+++ b/examples/hello-integration-edgent/src/main/java/org/apache/plc4x/java/examples/integration/edgent/PlcLogger.java
@@ -30,26 +30,24 @@ import java.util.concurrent.TimeUnit;
 public class PlcLogger {
 
     public static void main(String[] args) throws Exception {
-        if(args.length != 3) {
-            System.out.println("Usage: PlcLogger {connection-string} {resource-address-string} {interval-ms}");
-            System.out.println("Example: PlcLogger s7://10.10.64.20/0/0 INPUTS/0 10");
+        CliOptions options = CliOptions.fromArgs(args);
+        if (options == null) {
+            CliOptions.printHelp();
+            // Could not parse.
+            System.exit(1);
         }
 
-        String connectionString = args[0];
-        String addressString = args[1];
-        Integer interval = Integer.valueOf(args[2]);
-
         // Get a plc connection.
-        try (PlcConnectionAdapter plcAdapter = new PlcConnectionAdapter(connectionString)) {
+        try (PlcConnectionAdapter plcAdapter = new PlcConnectionAdapter(options.getConnectionString())) {
             // Initialize the Edgent core.
             DirectProvider dp = new DirectProvider();
             Topology top = dp.newTopology();
 
             // Define the event stream.
             // 1) PLC4X source generating a stream of bytes.
-            Supplier<Byte> plcSupplier = PlcFunctions.byteSupplier(plcAdapter, addressString);
+            Supplier<Byte> plcSupplier = PlcFunctions.byteSupplier(plcAdapter, options.getFieldAddress());
             // 2) Use polling to get an item from the byte-stream in regular intervals.
-            TStream<Byte> source = top.poll(plcSupplier, interval, TimeUnit.MILLISECONDS);
+            TStream<Byte> source = top.poll(plcSupplier, options.getPollingInterval(), TimeUnit.MILLISECONDS);
             // 3) Output the events in the stream on the console.
             source.print();
 
diff --git a/examples/hello-world-plc4x/src/main/java/org/apache/plc4x/java/examples/helloplc4x/CliOptions.java b/examples/hello-world-plc4x/src/main/java/org/apache/plc4x/java/examples/helloplc4x/CliOptions.java
index 7efbda4..4f1326b 100644
--- a/examples/hello-world-plc4x/src/main/java/org/apache/plc4x/java/examples/helloplc4x/CliOptions.java
+++ b/examples/hello-world-plc4x/src/main/java/org/apache/plc4x/java/examples/helloplc4x/CliOptions.java
@@ -23,12 +23,13 @@ import org.apache.commons.cli.*;
 
 public class CliOptions {
 
+    private static Options options;
+
     private final String connectionString;
     private final String[] fieldAddress;
 
-    /** Construct an MqttExampleOptions class from command line flags. */
-    public static CliOptions fromFlags(String[] args) {
-        org.apache.commons.cli.Options options = new org.apache.commons.cli.Options();
+    public static CliOptions fromArgs(String[] args) {
+        options = new Options();
         // Required arguments
         options.addOption(
             Option.builder()
@@ -62,6 +63,11 @@ public class CliOptions {
         }
     }
 
+    public static void printHelp() {
+        HelpFormatter formatter = new HelpFormatter();
+        formatter.printHelp("HelloPlc4x", options);
+    }
+
     public CliOptions(String connectionString, String[] fieldAddress) {
         this.connectionString = connectionString;
         this.fieldAddress = fieldAddress;
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 47fa44d..46a0832 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
@@ -38,8 +38,9 @@ public class HelloPlc4x {
      * @param args ignored.
      */
     public static void main(String[] args) throws Exception {
-        CliOptions options = CliOptions.fromFlags(args);
+        CliOptions options = CliOptions.fromArgs(args);
         if (options == null) {
+            CliOptions.printHelp();
             // Could not parse.
             System.exit(1);
         }