Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#3465): Add support for variable string length in PLC4X adapters #3466

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,30 @@ public Map<String, String> getNodeInformationFromCodeProperty(String codePropert
* @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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
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].
Expand All @@ -52,53 +52,72 @@ public void testGetNodeInformationFromCodePropertyWithComments() {
}

@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);

assertEquals(Datatypes.Integer, result);
}

@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);
}
Expand Down
Loading