You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by jf...@apache.org on 2019/08/29 05:32:10 UTC
[plc4x] 01/02: Adding some degree of flexibility to
ElasticsearchStorage example
This is an automated email from the ASF dual-hosted git repository.
jfeinauer pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git
commit a439b504b3ee26132ffbfb89be54641c3cd4808f
Author: Roman Shaposhnik <rv...@zededa.com>
AuthorDate: Wed Aug 28 14:53:45 2019 -0700
Adding some degree of flexibility to ElasticsearchStorage example
---
plc4j/examples/hello-storage-elasticsearch/pom.xml | 6 ++
.../examples/storage/elasticsearch/CliOptions.java | 82 ++++++++++++++++++++++
.../elasticsearch/ElasticsearchStorage.java | 17 ++++-
3 files changed, 103 insertions(+), 2 deletions(-)
diff --git a/plc4j/examples/hello-storage-elasticsearch/pom.xml b/plc4j/examples/hello-storage-elasticsearch/pom.xml
index 3cea262..a0df236 100644
--- a/plc4j/examples/hello-storage-elasticsearch/pom.xml
+++ b/plc4j/examples/hello-storage-elasticsearch/pom.xml
@@ -48,6 +48,12 @@
</dependency>
<dependency>
+ <groupId>commons-cli</groupId>
+ <artifactId>commons-cli</artifactId>
+ <version>1.4</version>
+ </dependency>
+
+ <dependency>
<groupId>org.apache.edgent</groupId>
<artifactId>edgent-api-function</artifactId>
<version>1.2.0</version>
diff --git a/plc4j/examples/hello-storage-elasticsearch/src/main/java/org/apache/plc4x/java/examples/storage/elasticsearch/CliOptions.java b/plc4j/examples/hello-storage-elasticsearch/src/main/java/org/apache/plc4x/java/examples/storage/elasticsearch/CliOptions.java
new file mode 100644
index 0000000..77b3107
--- /dev/null
+++ b/plc4j/examples/hello-storage-elasticsearch/src/main/java/org/apache/plc4x/java/examples/storage/elasticsearch/CliOptions.java
@@ -0,0 +1,82 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+
+package org.apache.plc4x.java.examples.storage.elasticsearch;
+
+
+import org.apache.commons.cli.*;
+
+public class CliOptions {
+
+ private static Options options;
+
+ private final String plc4xConnectionString;
+ private final String plc4xFieldAddress;
+
+ public static CliOptions fromArgs(String[] args) {
+ options = new Options();
+ // Required arguments
+ options.addOption(
+ Option.builder()
+ .type(String.class)
+ .longOpt("plc4x-connection-string")
+ .hasArg()
+ .desc("Connection String")
+ .build());
+ options.addOption(
+ Option.builder()
+ .type(String.class)
+ .longOpt("plc4x-field-address")
+ .hasArg()
+ .desc("Field Address")
+ .build());
+
+ CommandLineParser parser = new DefaultParser();
+ CommandLine commandLine;
+ try {
+ commandLine = parser.parse(options, args);
+
+ String plc4xConnectionString = commandLine.getOptionValue("plc4x-connection-string");
+ String plc4xFieldAddress = commandLine.getOptionValue("plc4x-field-address");
+
+ return new CliOptions(plc4xConnectionString, plc4xFieldAddress);
+ } catch (ParseException e) {
+ System.err.println(e.getMessage());
+ return null;
+ }
+ }
+
+ public static void printHelp() {
+ HelpFormatter formatter = new HelpFormatter();
+ formatter.printHelp("S7PlcToElasticSearchSample", options);
+ }
+
+ public CliOptions(String plc4xConnectionString, String plc4xFieldAddress) {
+ this.plc4xConnectionString = plc4xConnectionString != null ? plc4xConnectionString : "s7://10.10.64.20/1/1";
+ this.plc4xFieldAddress = plc4xFieldAddress != null ? plc4xFieldAddress : "%Q0:BYTE";
+ }
+
+ public String getPlc4xConnectionString() {
+ return plc4xConnectionString;
+ }
+
+ public String getPlc4xFieldAddress() {
+ return plc4xFieldAddress;
+ }
+}
\ No newline at end of file
diff --git a/plc4j/examples/hello-storage-elasticsearch/src/main/java/org/apache/plc4x/java/examples/storage/elasticsearch/ElasticsearchStorage.java b/plc4j/examples/hello-storage-elasticsearch/src/main/java/org/apache/plc4x/java/examples/storage/elasticsearch/ElasticsearchStorage.java
index 18ca35e..88e66f5 100644
--- a/plc4j/examples/hello-storage-elasticsearch/src/main/java/org/apache/plc4x/java/examples/storage/elasticsearch/ElasticsearchStorage.java
+++ b/plc4j/examples/hello-storage-elasticsearch/src/main/java/org/apache/plc4x/java/examples/storage/elasticsearch/ElasticsearchStorage.java
@@ -50,6 +50,7 @@ import java.util.List;
import java.util.concurrent.TimeUnit;
public class ElasticsearchStorage {
+ private static CliOptions options;
public enum ConveyorState {
STOPPED,
@@ -82,6 +83,7 @@ public class ElasticsearchStorage {
.put("http.type", "netty4")
.put("http.cors.enabled", "true")
.put("path.home", "elasticsearch-data")
+ .put("network.host", "0.0.0.0")
.build(), Collections.singletonList(Netty4Plugin.class));
node.start();
return node;
@@ -155,14 +157,15 @@ public class ElasticsearchStorage {
prepareIndexes(esClient);
// Get a plc connection.
- try (PlcConnectionAdapter plcAdapter = new PlcConnectionAdapter("s7://10.10.64.20/1/1")) {
+ try (PlcConnectionAdapter plcAdapter = new PlcConnectionAdapter(options.getPlc4xConnectionString())) {
// 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<List<Boolean>> plcSupplier = PlcFunctions.booleanListSupplier(plcAdapter, "%Q0:BYTE");
+ Supplier<List<Boolean>> plcSupplier = PlcFunctions.booleanListSupplier(plcAdapter,
+ options.getPlc4xFieldAddress());
// 2) Use polling to get an item from the byte-stream in regular intervals.
TStream<List<Boolean>> plcOutputStates = top.poll(plcSupplier, 100, TimeUnit.MILLISECONDS);
@@ -247,6 +250,16 @@ public class ElasticsearchStorage {
}
public static void main(String[] args) {
+ options = CliOptions.fromArgs(args);
+ if (options == null) {
+ CliOptions.printHelp();
+ // Could not parse.
+ System.exit(1);
+ }
+
+ System.out.println("Connecting to " + options.getPlc4xConnectionString()+ " with field address " +
+ options.getPlc4xFieldAddress());
+
ElasticsearchStorage factory = new ElasticsearchStorage();
factory.runFactory();
}