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/04 18:04:04 UTC

[streampipes] branch 1980-arrays-are-not-supported-for-s7-plcs updated: feat(#1980): Add array support for S7 PLC registers

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 3c9fe1c27 feat(#1980): Add array support for S7 PLC registers
3c9fe1c27 is described below

commit 3c9fe1c27aa164d2e128b502b62bf8cb81e34d70
Author: Philipp Zehnder <te...@users.noreply.github.com>
AuthorDate: Wed Oct 4 20:03:54 2023 +0200

    feat(#1980): Add array support for S7 PLC registers
---
 .../iiot/adapters/plc4x/s7/Plc4xS7Adapter.java     | 41 +++++++++++++++++-----
 .../plc4x/s7/config/ConfigurationParser.java       | 14 ++++++++
 .../plc4x/s7/config/ConfigurationParserTest.java   | 35 ++++++++++++++++++
 3 files changed, 82 insertions(+), 8 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 612d9da21..4122f2ccd 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
@@ -34,6 +34,7 @@ import org.apache.streampipes.extensions.management.connect.adapter.util.Polling
 import org.apache.streampipes.model.AdapterType;
 import org.apache.streampipes.model.connect.guess.GuessSchema;
 import org.apache.streampipes.model.schema.EventProperty;
+import org.apache.streampipes.model.schema.EventPropertyList;
 import org.apache.streampipes.model.staticproperty.CollectionStaticProperty;
 import org.apache.streampipes.model.staticproperty.StaticProperty;
 import org.apache.streampipes.model.staticproperty.StaticPropertyGroup;
@@ -55,6 +56,7 @@ import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
 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.apache.plc4x.java.api.value.PlcValue;
 import org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -214,7 +216,6 @@ public class Plc4xS7Adapter implements StreamPipesAdapter, IPullAdapter, PlcRead
   }
 
 
-
   @Override
   public void onReadResult(PlcReadResponse response, Throwable throwable) {
     if (throwable != null) {
@@ -232,7 +233,19 @@ public class Plc4xS7Adapter implements StreamPipesAdapter, IPullAdapter, PlcRead
 
     for (String key : this.nodes.keySet()) {
       if (response.getResponseCode(key) == PlcResponseCode.OK) {
-        event.put(key, response.getObject(key));
+
+        // if the response is a list, add each element to the result
+        if (response.getObject(key) instanceof List) {
+          event.put(key,
+              response.getAsPlcValue()
+                  .getValue(key)
+                  .getList().stream()
+                  .map(PlcValue::getObject)
+                  .toList()
+                  .toArray());
+        } else {
+          event.put(key, response.getObject(key));
+        }
       } else {
         LOG.error("Error[" + key + "]: "
                   + response.getResponseCode(key).name());
@@ -303,12 +316,24 @@ public class Plc4xS7Adapter implements StreamPipesAdapter, IPullAdapter, PlcRead
       for (Map.Entry<String, String> entry : this.nodes.entrySet()) {
         var datatype = new ConfigurationParser().getStreamPipesDataType(entry.getValue());
 
-        allProperties.add(
-            PrimitivePropertyBuilder
-                .create(datatype, entry.getKey())
-                .label(entry.getKey())
-                .description("")
-                .build());
+        var primitiveProperty = PrimitivePropertyBuilder
+            .create(datatype, entry.getKey())
+            .label(entry.getKey())
+            .description("")
+            .build();
+
+        // Check if the address configuration is an array
+        var isArray = new ConfigurationParser().isPLCArray(entry.getValue());
+
+        if (isArray) {
+          var propertyList = new EventPropertyList();
+          propertyList.setRuntimeName(entry.getKey());
+          propertyList.setLabel(entry.getKey());
+          propertyList.setEventProperty(primitiveProperty);
+          allProperties.add(propertyList);
+        } else {
+          allProperties.add(primitiveProperty);
+        }
       }
 
       this.before(extractor.getStaticPropertyExtractor());
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
index aac461369..7b73a372b 100644
--- 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
@@ -33,6 +33,7 @@ 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
    */
@@ -74,6 +75,9 @@ public class ConfigurationParser {
 
     String type = plcType.substring(plcType.lastIndexOf(":") + 1);
 
+    // replace array information from type
+    type = type.replaceAll("\\[.*?\\]", "");
+
     return switch (type) {
       case "BOOL" -> Datatypes.Boolean;
       case "BYTE", "REAL" -> Datatypes.Float;
@@ -83,5 +87,15 @@ public class ConfigurationParser {
     };
   }
 
+  /**
+   * Takes the PLC4X address description and validates if it describes an array
+   *
+   * @param plcType address of the register that should be read
+   * @return whether the address describes an array or not
+   */
+  public boolean isPLCArray(String plcType) {
+    return plcType.matches(".*\\[.*\\].*");
+  }
+
 
 }
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
index 90f4ea23f..e780f7bcb 100644
--- 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
@@ -18,11 +18,16 @@
 
 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 org.junit.Test;
 
 import java.util.Set;
 
 import static org.junit.Assert.assertEquals;
+import static org.wildfly.common.Assert.assertFalse;
+import static org.wildfly.common.Assert.assertTrue;
 
 public class ConfigurationParserTest {
 
@@ -46,6 +51,7 @@ public class ConfigurationParserTest {
     assertEquals(Set.of("temperature"), result.keySet());
     assertEquals("%I0.0:INT", result.get("temperature"));
   }
+
   @Test
   public void testGetNodeInformationFromCodePropertyMultipleEntries() {
     var configBlock = """
@@ -60,6 +66,23 @@ public class ConfigurationParserTest {
     assertEquals("%I0.0:BOOL", result.get("v2"));
   }
 
+  @Test
+  public void testGetStreamPipesDataTypeArray() throws AdapterException {
+    var plcType = "INT[100]";
+    var result = new ConfigurationParser().getStreamPipesDataType(plcType);
+
+    assertEquals(Datatypes.Integer, result);
+  }
+
+  @Test
+  public void testGetStreamPipesDataTypeBasic() throws AdapterException {
+    var plcType = "INT";
+    var result = new ConfigurationParser().getStreamPipesDataType(plcType);
+
+    assertEquals(Datatypes.Integer, result);
+  }
+
+
   @Test
   public void testGetNodeInformationFromCodePropertyNoEntries() {
     var configBlock = "";
@@ -68,4 +91,16 @@ public class ConfigurationParserTest {
     assertEquals(0, result.size());
   }
 
+  @Test
+  public void testIsPLCArray() {
+    var result = new ConfigurationParser().isPLCArray("%DB3.DB0:BOOL[100]");
+    assertTrue(result);
+  }
+
+
+  @Test
+  public void testIsNoPLCArray() {
+    var result = new ConfigurationParser().isPLCArray("%DB3.DB0:BOOL");
+    assertFalse(result);
+  }
 }
\ No newline at end of file