You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streampipes.apache.org by ze...@apache.org on 2023/10/02 14:28:57 UTC

[streampipes] branch 1980-arrays-are-not-supported-for-s7-plcs updated: feat(#1980): Support advanced register input mode for plc s7 adapter

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

zehnder pushed a commit to branch 1980-arrays-are-not-supported-for-s7-plcs
in repository https://gitbox.apache.org/repos/asf/streampipes.git


The following commit(s) were added to refs/heads/1980-arrays-are-not-supported-for-s7-plcs by this push:
     new 027274bd4 feat(#1980): Support advanced register input mode for plc s7 adapter
027274bd4 is described below

commit 027274bd4b0f7aa5c7c153425f16eb4aa53c55f5
Author: Philipp Zehnder <te...@users.noreply.github.com>
AuthorDate: Mon Oct 2 16:28:48 2023 +0200

    feat(#1980): Support advanced register input mode for plc s7 adapter
---
 .../iiot/adapters/plc4x/s7/Plc4xS7Adapter.java     | 142 ++++++++++++---------
 .../plc4x/s7/config/ConfigurationParser.java       |  87 +++++++++++++
 .../strings.en                                     |  16 +--
 .../plc4x/s7/config/ConfigurationParserTest.java   |  71 +++++++++++
 4 files changed, 250 insertions(+), 66 deletions(-)

diff --git a/streampipes-extensions/streampipes-connect-adapters-iiot/src/main/java/org/apache/streampipes/connect/iiot/adapters/plc4x/s7/Plc4xS7Adapter.java b/streampipes-extensions/streampipes-connect-adapters-iiot/src/main/java/org/apache/streampipes/connect/iiot/adapters/plc4x/s7/Plc4xS7Adapter.java
index af52ed841..612d9da21 100644
--- a/streampipes-extensions/streampipes-connect-adapters-iiot/src/main/java/org/apache/streampipes/connect/iiot/adapters/plc4x/s7/Plc4xS7Adapter.java
+++ b/streampipes-extensions/streampipes-connect-adapters-iiot/src/main/java/org/apache/streampipes/connect/iiot/adapters/plc4x/s7/Plc4xS7Adapter.java
@@ -20,6 +20,7 @@ package org.apache.streampipes.connect.iiot.adapters.plc4x.s7;
 
 
 import org.apache.streampipes.commons.exceptions.connect.AdapterException;
+import org.apache.streampipes.connect.iiot.adapters.plc4x.s7.config.ConfigurationParser;
 import org.apache.streampipes.extensions.api.connect.IAdapterConfiguration;
 import org.apache.streampipes.extensions.api.connect.IEventCollector;
 import org.apache.streampipes.extensions.api.connect.IPullAdapter;
@@ -47,7 +48,6 @@ import org.apache.streampipes.sdk.helpers.Labels;
 import org.apache.streampipes.sdk.helpers.Locales;
 import org.apache.streampipes.sdk.helpers.Options;
 import org.apache.streampipes.sdk.utils.Assets;
-import org.apache.streampipes.sdk.utils.Datatypes;
 
 import org.apache.plc4x.java.PlcDriverManager;
 import org.apache.plc4x.java.api.PlcConnection;
@@ -85,17 +85,28 @@ public class Plc4xS7Adapter implements StreamPipesAdapter, IPullAdapter, PlcRead
   private static final String PLC_NODE_RUNTIME_NAME = "plc_node_runtime_name";
   private static final String PLC_NODE_TYPE = "plc_node_type";
 
-  private static final String NODE_INPUT_CODE_BLOCK_ALTIVE = "node_input_code_block_altive";
-  private static final String CODE_BLOCK = "code_block";
-  private static final String NODE_INPUT_ALTERNATIVES = "node_input_alternatives";
-  private static final String NODE_INPUT_COLLECTION_ALTERNATIVE = "node_input_collection_alternative";
+  private static final String PLC_NODE_INPUT_CODE_BLOCK_ALTIVE = "plc_node_input_code_block_altive";
+  private static final String PLC_CODE_BLOCK = "plc_code_block";
+  private static final String PLC_NODE_INPUT_ALTERNATIVES = "plc_node_input_alternatives";
+  private static final String PLC_NODE_INPUT_COLLECTION_ALTERNATIVE = "plc_node_input_collection_alternative";
+
+  private static final String CODE_TEMPLATE = """
+      // This code block can be used to manually specify the addresses of the PLC registers.
+      // The syntax is based on the PLC4X syntax, see [1].
+      // Address Pattern:
+      // propertyName=%{Memory-Area}{start-address}:{Data-Type}[{array-size}]
+
+      temperature=%I0.0:INT
+
+      // [1] https://plc4x.apache.org/users/protocols/s7.html
+      """;
 
   /**
    * Values of user configuration parameters
    */
   private String ip;
   private int pollingInterval;
-  private List<Map<String, String>> nodes;
+  private Map<String, String> nodes;
 
   private PlcDriverManager driverManager;
 
@@ -108,10 +119,8 @@ public class Plc4xS7Adapter implements StreamPipesAdapter, IPullAdapter, PlcRead
 
   /**
    * This method is executed when the adapter is started. A connection to the PLC is initialized
-   *
-   * @throws AdapterException
    */
-  private void before(IStaticPropertyExtractor extractor) throws AdapterException {
+  private void before(IStaticPropertyExtractor extractor) {
     // Extract user input
     getConfigurations(extractor);
 
@@ -141,17 +150,18 @@ public class Plc4xS7Adapter implements StreamPipesAdapter, IPullAdapter, PlcRead
     }
   }
 
-  private PlcReadRequest makeReadRequest(PlcConnection plcConnection) throws PlcConnectionException {
+  private PlcReadRequest makeReadRequest(PlcConnection plcConnection) {
     PlcReadRequest.Builder builder = plcConnection.readRequestBuilder();
-    for (Map<String, String> node : this.nodes) {
-      builder.addItem(node.get(PLC_NODE_NAME),
-          node.get(PLC_NODE_NAME) + ":" + node.get(PLC_NODE_TYPE).toUpperCase().replaceAll(" ", "_"));
+
+    for (Map.Entry<String, String> entry : this.nodes.entrySet()) {
+      builder.addItem(entry.getKey(), entry.getValue());
     }
+
     return builder.build();
   }
 
   private void readPlcData(
-      PlcConnection plcConnection, PlcReadResponseHandler handler) throws PlcConnectionException {
+      PlcConnection plcConnection, PlcReadResponseHandler handler) {
     var readRequest = makeReadRequest(plcConnection);
     // Execute the request
     CompletableFuture<? extends PlcReadResponse> asyncResponse = readRequest.execute();
@@ -182,43 +192,28 @@ public class Plc4xS7Adapter implements StreamPipesAdapter, IPullAdapter, PlcRead
    *
    * @param extractor StaticPropertyExtractor
    */
-  private void getConfigurations(IStaticPropertyExtractor extractor) throws AdapterException {
+  private void getConfigurations(IStaticPropertyExtractor extractor) {
 
     this.ip = extractor.singleValueParameter(PLC_IP, String.class);
     this.pollingInterval = extractor.singleValueParameter(PLC_POLLING_INTERVAL, Integer.class);
 
-    this.nodes = new ArrayList<>();
-    CollectionStaticProperty sp = (CollectionStaticProperty) extractor.getStaticPropertyByName(PLC_NODES);
+    this.nodes = new HashMap<>();
 
-    for (StaticProperty member : sp.getMembers()) {
-      StaticPropertyExtractor memberExtractor =
-          StaticPropertyExtractor.from(((StaticPropertyGroup) member).getStaticProperties(), new ArrayList<>());
-      Map<String, String> map = new HashMap<>();
-      map.put(PLC_NODE_RUNTIME_NAME, memberExtractor.textParameter(PLC_NODE_RUNTIME_NAME));
-      map.put(PLC_NODE_NAME, memberExtractor.textParameter(PLC_NODE_NAME));
-      map.put(PLC_NODE_TYPE, memberExtractor.selectedSingleValue(PLC_NODE_TYPE, String.class));
-      this.nodes.add(map);
+    var selectedAlternative = extractor.selectedAlternativeInternalId(PLC_NODE_INPUT_ALTERNATIVES);
+
+    if (selectedAlternative.equals(PLC_NODE_INPUT_COLLECTION_ALTERNATIVE)) {
+      // Alternative Simple
+      var csp = (CollectionStaticProperty) extractor.getStaticPropertyByName(PLC_NODES);
+      this.nodes = getNodeInformationFromCollectionStaticProperty(csp);
+
+    } else {
+      // Alternative Advanced
+      var codePropertyInput = extractor.codeblockValue(PLC_CODE_BLOCK);
+      this.nodes = new ConfigurationParser().getNodeInformationFromCodeProperty(codePropertyInput);
     }
   }
 
-  /**
-   * Transforms PLC4X data types to datatypes supported in StreamPipes
-   *
-   * @param plcType String
-   * @return Datatypes
-   */
-  private Datatypes getStreamPipesDataType(String plcType) throws AdapterException {
 
-    String type = plcType.substring(plcType.lastIndexOf(":") + 1);
-
-    return switch (type) {
-      case "BOOL" -> Datatypes.Boolean;
-      case "BYTE", "REAL" -> Datatypes.Float;
-      case "INT" -> Datatypes.Integer;
-      case "WORD", "TIME_OF_DAY", "DATE", "DATE_AND_TIME", "STRING", "CHAR" -> Datatypes.String;
-      default -> throw new AdapterException("Datatype " + plcType + " is not supported");
-    };
-  }
 
   @Override
   public void onReadResult(PlcReadResponse response, Throwable throwable) {
@@ -233,13 +228,14 @@ public class Plc4xS7Adapter implements StreamPipesAdapter, IPullAdapter, PlcRead
   }
 
   private Map<String, Object> makeEvent(PlcReadResponse response) {
-    Map<String, Object> event = new HashMap<>();
-    for (Map<String, String> node : this.nodes) {
-      if (response.getResponseCode(node.get(PLC_NODE_NAME)) == PlcResponseCode.OK) {
-        event.put(node.get(PLC_NODE_RUNTIME_NAME), response.getObject(node.get(PLC_NODE_NAME)));
+    var event = new HashMap<String, Object>();
+
+    for (String key : this.nodes.keySet()) {
+      if (response.getResponseCode(key) == PlcResponseCode.OK) {
+        event.put(key, response.getObject(key));
       } else {
-        LOG.error("Error[" + node.get(PLC_NODE_NAME) + "]: "
-                  + response.getResponseCode(node.get(PLC_NODE_NAME)).name());
+        LOG.error("Error[" + key + "]: "
+                  + response.getResponseCode(key).name());
       }
     }
     return event;
@@ -260,8 +256,8 @@ public class Plc4xS7Adapter implements StreamPipesAdapter, IPullAdapter, PlcRead
         .requiredTextParameter(Labels.withId(PLC_IP))
         .requiredIntegerParameter(Labels.withId(PLC_POLLING_INTERVAL), 1000)
         .requiredAlternatives(
-            Labels.withId(NODE_INPUT_ALTERNATIVES),
-            Alternatives.from(Labels.withId(NODE_INPUT_COLLECTION_ALTERNATIVE),
+            Labels.withId(PLC_NODE_INPUT_ALTERNATIVES),
+            Alternatives.from(Labels.withId(PLC_NODE_INPUT_COLLECTION_ALTERNATIVE),
                 StaticProperties.collection(Labels.withId(PLC_NODES),
                     StaticProperties.stringFreeTextProperty(Labels.withId(PLC_NODE_RUNTIME_NAME)),
                     StaticProperties.stringFreeTextProperty(Labels.withId(PLC_NODE_NAME)),
@@ -269,8 +265,8 @@ public class Plc4xS7Adapter implements StreamPipesAdapter, IPullAdapter, PlcRead
                         Options.from("Bool", "Byte", "Int", "Word", "Real", "Char", "String", "Date", "Time of day",
                             "Date and Time"))),
                 true),
-            Alternatives.from(Labels.withId(NODE_INPUT_CODE_BLOCK_ALTIVE),
-                StaticProperties.codeStaticProperty(Labels.withId(CODE_BLOCK), CodeLanguage.None, "")))
+            Alternatives.from(Labels.withId(PLC_NODE_INPUT_CODE_BLOCK_ALTIVE),
+                StaticProperties.codeStaticProperty(Labels.withId(PLC_CODE_BLOCK), CodeLanguage.None, CODE_TEMPLATE)))
         .buildConfiguration();
   }
 
@@ -304,15 +300,13 @@ public class Plc4xS7Adapter implements StreamPipesAdapter, IPullAdapter, PlcRead
       GuessSchemaBuilder builder = GuessSchemaBuilder.create();
       List<EventProperty> allProperties = new ArrayList<>();
 
-      for (Map<String, String> node : this.nodes) {
-        Datatypes datatype = getStreamPipesDataType(node.get(PLC_NODE_TYPE)
-            .toUpperCase()
-            .replaceAll(" ", "_"));
+      for (Map.Entry<String, String> entry : this.nodes.entrySet()) {
+        var datatype = new ConfigurationParser().getStreamPipesDataType(entry.getValue());
 
         allProperties.add(
             PrimitivePropertyBuilder
-                .create(datatype, node.get(PLC_NODE_RUNTIME_NAME))
-                .label(node.get(PLC_NODE_RUNTIME_NAME))
+                .create(datatype, entry.getKey())
+                .label(entry.getKey())
                 .description("")
                 .build());
       }
@@ -328,4 +322,36 @@ public class Plc4xS7Adapter implements StreamPipesAdapter, IPullAdapter, PlcRead
       throw new AdapterException(e.getMessage(), e);
     }
   }
+
+
+  private Map<String, String> getNodeInformationFromCollectionStaticProperty(CollectionStaticProperty csp) {
+    var result = new HashMap<String, String>();
+
+    for (StaticProperty member : csp.getMembers()) {
+      StaticPropertyExtractor memberExtractor =
+          StaticPropertyExtractor.from(((StaticPropertyGroup) member).getStaticProperties(), new ArrayList<>());
+
+      result.put(
+          memberExtractor.textParameter(PLC_NODE_RUNTIME_NAME),
+          getNodeAddress(memberExtractor));
+
+    }
+
+    return result;
+  }
+
+  /**
+   * Takes the members of the static property collection from the UI and creates the PLC4X node address
+   *
+   * @param memberExtractor member of the static property node collection
+   * @return string in the format of PLC4X node address
+   */
+  private String getNodeAddress(StaticPropertyExtractor memberExtractor) {
+    return "%s:%s".formatted(
+        memberExtractor.textParameter(PLC_NODE_NAME),
+        memberExtractor.selectedSingleValue(PLC_NODE_TYPE, String.class)
+            .toUpperCase()
+            .replaceAll(" ", "_"));
+  }
+
 }
diff --git a/streampipes-extensions/streampipes-connect-adapters-iiot/src/main/java/org/apache/streampipes/connect/iiot/adapters/plc4x/s7/config/ConfigurationParser.java b/streampipes-extensions/streampipes-connect-adapters-iiot/src/main/java/org/apache/streampipes/connect/iiot/adapters/plc4x/s7/config/ConfigurationParser.java
new file mode 100644
index 000000000..aac461369
--- /dev/null
+++ b/streampipes-extensions/streampipes-connect-adapters-iiot/src/main/java/org/apache/streampipes/connect/iiot/adapters/plc4x/s7/config/ConfigurationParser.java
@@ -0,0 +1,87 @@
+/*
+ * 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.streampipes.connect.iiot.adapters.plc4x.s7.config;
+
+import org.apache.streampipes.commons.exceptions.connect.AdapterException;
+import org.apache.streampipes.sdk.utils.Datatypes;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+/**
+ * This is a helper class to parse the user input for the PLC4X S7 adapter
+ */
+public class ConfigurationParser {
+
+  /**
+   * This method takes a string with the PLC configuration and parses the configuration accorting to this pattern:
+   * variableName=value
+   * @param codePropertyValue code block with the PLC configuration
+   * @return returns a list of Maps, with the variable name as the Key and value as value
+   */
+  public Map<String, String> getNodeInformationFromCodeProperty(String codePropertyValue) {
+    var result = new HashMap<String, String>();
+
+    var lines = codePropertyValue.split("\n");
+
+    // pattern to match "variableName=value"
+    var pattern = Pattern.compile("(\\w+)=(.+)");
+
+    for (String line : lines) {
+      // Remove leading and trailing whitespace
+      line = line.trim();
+
+      // Skip comments
+      if (line.startsWith("//")) {
+        continue;
+      }
+
+      // Check if macher matches and add to results list
+      var matcher = pattern.matcher(line);
+      if (matcher.find()) {
+        result.put(matcher.group(1), matcher.group(2));
+      }
+    }
+
+    return result;
+  }
+
+
+  /**
+   * Transforms PLC4X data types to datatypes supported in StreamPipes
+   *
+   * @param plcType String
+   * @return Datatypes
+   */
+  public Datatypes getStreamPipesDataType(String plcType) throws AdapterException {
+
+    String type = plcType.substring(plcType.lastIndexOf(":") + 1);
+
+    return switch (type) {
+      case "BOOL" -> Datatypes.Boolean;
+      case "BYTE", "REAL" -> Datatypes.Float;
+      case "INT" -> Datatypes.Integer;
+      case "WORD", "TIME_OF_DAY", "DATE", "DATE_AND_TIME", "STRING", "CHAR" -> Datatypes.String;
+      default -> throw new AdapterException("Datatype " + plcType + " is not supported");
+    };
+  }
+
+
+}
diff --git a/streampipes-extensions/streampipes-connect-adapters-iiot/src/main/resources/org.apache.streampipes.connect.iiot.adapters.plc4x.s7/strings.en b/streampipes-extensions/streampipes-connect-adapters-iiot/src/main/resources/org.apache.streampipes.connect.iiot.adapters.plc4x.s7/strings.en
index 507caf1cb..5aebb1485 100644
--- a/streampipes-extensions/streampipes-connect-adapters-iiot/src/main/resources/org.apache.streampipes.connect.iiot.adapters.plc4x.s7/strings.en
+++ b/streampipes-extensions/streampipes-connect-adapters-iiot/src/main/resources/org.apache.streampipes.connect.iiot.adapters.plc4x.s7/strings.en
@@ -43,14 +43,14 @@ plc_node_name.description=example: %Q0.4
 plc_node_type.title=Data Type
 plc_node_type.description=example: bool
 
-node_input_code_block_altive.title=Advanced
-node_input_code_block_altive.description=
+plc_node_input_code_block_altive.title=Advanced
+plc_node_input_code_block_altive.description=
 
-code_block.title=Advanced
-code_block.description=Enter the nodes in the code block below, acording to the described format
+plc_code_block.title=Advanced
+plc_code_block.description=Enter the nodes in the code block below, acording to the described format
 
-node_input_alternatives.title=Register Input
-node_input_alternatives.description=The information about the registers can be entered in simple or advanced mode
+plc_node_input_alternatives.title=Register Input
+plc_node_input_alternatives.description=The information about the registers can be entered in simple or advanced mode
 
-node_input_collection_alternative.title=Simple
-node_input_collection_alternative.description=
+plc_node_input_collection_alternative.title=Simple
+plc_node_input_collection_alternative.description=
diff --git a/streampipes-extensions/streampipes-connect-adapters-iiot/src/test/java/org/apache/streampipes/connect/iiot/adapters/plc4x/s7/config/ConfigurationParserTest.java b/streampipes-extensions/streampipes-connect-adapters-iiot/src/test/java/org/apache/streampipes/connect/iiot/adapters/plc4x/s7/config/ConfigurationParserTest.java
new file mode 100644
index 000000000..90f4ea23f
--- /dev/null
+++ b/streampipes-extensions/streampipes-connect-adapters-iiot/src/test/java/org/apache/streampipes/connect/iiot/adapters/plc4x/s7/config/ConfigurationParserTest.java
@@ -0,0 +1,71 @@
+/*
+ * 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.streampipes.connect.iiot.adapters.plc4x.s7.config;
+
+import org.junit.Test;
+
+import java.util.Set;
+
+import static org.junit.Assert.assertEquals;
+
+public class ConfigurationParserTest {
+
+  @Test
+  public void testGetNodeInformationFromCodePropertyWithComments() {
+    var configBlock = """
+        // This code block can be used to manually specify the addresses of the PLC registers.
+        // The syntax is based on the PLC4X syntax, see [1].
+        // Address Pattern:
+        // propertyName=%{Memory-Area}{start-address}:{Data-Type}[{array-size}]
+        //
+        // Sample:
+        temperature=%I0.0:INT
+
+
+        // [1] https://plc4x.apache.org/users/protocols/s7.html
+        """;
+    var result = new ConfigurationParser().getNodeInformationFromCodeProperty(configBlock);
+
+    assertEquals(1, result.size());
+    assertEquals(Set.of("temperature"), result.keySet());
+    assertEquals("%I0.0:INT", result.get("temperature"));
+  }
+  @Test
+  public void testGetNodeInformationFromCodePropertyMultipleEntries() {
+    var configBlock = """
+        v1=%I0.0:INT
+        v2=%I0.0:BOOL
+        """;
+    var result = new ConfigurationParser().getNodeInformationFromCodeProperty(configBlock);
+
+    assertEquals(2, result.size());
+    assertEquals(Set.of("v1", "v2"), result.keySet());
+    assertEquals("%I0.0:INT", result.get("v1"));
+    assertEquals("%I0.0:BOOL", result.get("v2"));
+  }
+
+  @Test
+  public void testGetNodeInformationFromCodePropertyNoEntries() {
+    var configBlock = "";
+    var result = new ConfigurationParser().getNodeInformationFromCodeProperty(configBlock);
+
+    assertEquals(0, result.size());
+  }
+
+}
\ No newline at end of file