This is an automated email from the ASF dual-hosted git repository.
zehnder pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/streampipes.git
The following commit(s) were added to refs/heads/dev by this push:
new 8642ef0b1d fix(#3465): Add support for variable string length in PLC4X
adapters (#3466)
8642ef0b1d is described below
commit 8642ef0b1d1d596c809f59657b933412b17430ac
Author: Philipp Zehnder <[email protected]>
AuthorDate: Fri Feb 7 12:42:22 2025 +0100
fix(#3465): Add support for variable string length in PLC4X adapters (#3466)
---
.../plc/adapter/s7/config/ConfigurationParser.java | 24 +++++++++--
.../adapter/s7/config/ConfigurationParserTest.java | 47 +++++++++++++++-------
2 files changed, 54 insertions(+), 17 deletions(-)
diff --git
a/streampipes-extensions/streampipes-connectors-plc/src/main/java/org/apache/streampipes/extensions/connectors/plc/adapter/s7/config/ConfigurationParser.java
b/streampipes-extensions/streampipes-connectors-plc/src/main/java/org/apache/streampipes/extensions/connectors/plc/adapter/s7/config/ConfigurationParser.java
index 395a11115d..2325714936 100644
---
a/streampipes-extensions/streampipes-connectors-plc/src/main/java/org/apache/streampipes/extensions/connectors/plc/adapter/s7/config/ConfigurationParser.java
+++
b/streampipes-extensions/streampipes-connectors-plc/src/main/java/org/apache/streampipes/extensions/connectors/plc/adapter/s7/config/ConfigurationParser.java
@@ -72,12 +72,30 @@ public class ConfigurationParser {
* @return Datatypes
*/
public Datatypes getStreamPipesDataType(String plcType) throws
AdapterException {
+ var type = extractType(plcType);
- String type = plcType.substring(plcType.lastIndexOf(":") + 1);
+ type = removeArrayInformation(type);
- // replace array information from type
- type = type.replaceAll("\\[.*?\\]", "");
+ if (isStringWithLengthLimit(type)) {
+ return Datatypes.String;
+ }
+
+ return mapTypeToDatatype(type, plcType);
+ }
+
+ private String extractType(String plcType) {
+ return plcType.substring(plcType.lastIndexOf(":") + 1);
+ }
+
+ private String removeArrayInformation(String type) {
+ return type.replaceAll("\\[.*?\\]", "");
+ }
+
+ private boolean isStringWithLengthLimit(String type) {
+ return type.startsWith("STRING(");
+ }
+ private Datatypes mapTypeToDatatype(String type, String plcType) throws
AdapterException {
return switch (type) {
case "BOOL" -> Datatypes.Boolean;
case "BYTE", "REAL" -> Datatypes.Float;
diff --git
a/streampipes-extensions/streampipes-connectors-plc/src/test/java/org/apache/streampipes/extensions/connectors/plc/adapter/s7/config/ConfigurationParserTest.java
b/streampipes-extensions/streampipes-connectors-plc/src/test/java/org/apache/streampipes/extensions/connectors/plc/adapter/s7/config/ConfigurationParserTest.java
index 50a9c03a58..0fe0cc4f12 100644
---
a/streampipes-extensions/streampipes-connectors-plc/src/test/java/org/apache/streampipes/extensions/connectors/plc/adapter/s7/config/ConfigurationParserTest.java
+++
b/streampipes-extensions/streampipes-connectors-plc/src/test/java/org/apache/streampipes/extensions/connectors/plc/adapter/s7/config/ConfigurationParserTest.java
@@ -31,7 +31,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
public class ConfigurationParserTest {
@Test
- public void testGetNodeInformationFromCodePropertyWithComments() {
+ public void getNodeInformationFromCodeProperty_WithComments() {
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].
@@ -52,21 +52,33 @@ public class ConfigurationParserTest {
}
@Test
- public void testGetNodeInformationFromCodePropertyMultipleEntries() {
+ public void getNodeInformationFromCodeProperty_MultipleEntries() {
var configBlock = """
v1=%I0.0:INT
- v2=%I0.0:BOOL
+ v2=%I0.1:BOOL
+ v3=%I0.2:STRING(10)
+ v4=%I0.3:STRING(10)[100]
""";
var result = new
ConfigurationParser().getNodeInformationFromCodeProperty(configBlock);
- assertEquals(2, result.size());
- assertEquals(Set.of("v1", "v2"), result.keySet());
+ assertEquals(4, result.size());
+ assertEquals(Set.of("v1", "v2", "v3", "v4"), result.keySet());
assertEquals("%I0.0:INT", result.get("v1"));
- assertEquals("%I0.0:BOOL", result.get("v2"));
+ assertEquals("%I0.1:BOOL", result.get("v2"));
+ assertEquals("%I0.2:STRING(10)", result.get("v3"));
+ assertEquals("%I0.3:STRING(10)[100]", result.get("v4"));
}
@Test
- public void testGetStreamPipesDataTypeArray() throws AdapterException {
+ public void getNodeInformationFromCodeProperty_NoEntries() {
+ var configBlock = "";
+ var result = new
ConfigurationParser().getNodeInformationFromCodeProperty(configBlock);
+
+ assertEquals(0, result.size());
+ }
+
+ @Test
+ public void getStreamPipesDataType_Array() throws AdapterException {
var plcType = "INT[100]";
var result = new ConfigurationParser().getStreamPipesDataType(plcType);
@@ -74,31 +86,38 @@ public class ConfigurationParserTest {
}
@Test
- public void testGetStreamPipesDataTypeBasic() throws AdapterException {
+ public void getStreamPipesDataType_Basic() throws AdapterException {
var plcType = "INT";
var result = new ConfigurationParser().getStreamPipesDataType(plcType);
assertEquals(Datatypes.Integer, result);
}
+ @Test
+ public void getStreamPipesDataType_StringWithLenghtLimit() throws
AdapterException {
+ var plcType = "STRING(10)";
+ var result = new ConfigurationParser().getStreamPipesDataType(plcType);
+
+ assertEquals(Datatypes.String, result);
+ }
@Test
- public void testGetNodeInformationFromCodePropertyNoEntries() {
- var configBlock = "";
- var result = new
ConfigurationParser().getNodeInformationFromCodeProperty(configBlock);
+ public void getStreamPipesDataType_ArrayOfStringsWithLenghtLimit() throws
AdapterException {
+ var plcType = "STRING(10)[100]";
+ var result = new ConfigurationParser().getStreamPipesDataType(plcType);
- assertEquals(0, result.size());
+ assertEquals(Datatypes.String, result);
}
@Test
- public void testIsPLCArray() {
+ public void isPLCArray_True() {
var result = new ConfigurationParser().isPLCArray("%DB3.DB0:BOOL[100]");
Assertions.assertTrue(result);
}
@Test
- public void testIsNoPLCArray() {
+ public void isNoPLCArray_False() {
var result = new ConfigurationParser().isPLCArray("%DB3.DB0:BOOL");
Assertions.assertFalse(result);
}