You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by hu...@apache.org on 2020/11/15 11:44:59 UTC
[plc4x] branch bug/simulated_device_data_types updated: Add back
support for existing Java class types.
This is an automated email from the ASF dual-hosted git repository.
hutcheb pushed a commit to branch bug/simulated_device_data_types
in repository https://gitbox.apache.org/repos/asf/plc4x.git
The following commit(s) were added to refs/heads/bug/simulated_device_data_types by this push:
new 079259f Add back support for existing Java class types.
079259f is described below
commit 079259f63f2ec38345edf946bebae4154d71ccbf
Author: hutcheb <be...@gmail.com>
AuthorDate: Sun Nov 15 06:44:49 2020 -0500
Add back support for existing Java class types.
---
.../language/java/JavaLanguageTemplateHelper.java | 4 +-
.../java/simulated/connection/SimulatedDevice.java | 11 ++++
.../plc4x/java/simulated/field/SimulatedField.java | 29 +++++++++-
.../plc4x/java/simulated/utils/StaticHelper.java | 66 ++++++++++++++++++++++
.../simulated/connection/SimulatedDeviceTest.java | 4 +-
.../simulated/field/SimularedFieldHandlerTest.java | 2 +-
.../java/simulated/field/SimulatedFieldTest.java | 8 +--
.../java/org/apache/plc4x/DriverManagerTest.java | 2 +-
.../apache-calcite/src/test/resources/example.yml | 4 +-
.../plc4x/java/spi/generation/ReadBuffer.java | 1 +
.../resources/protocols/simulated/simulated.mspec | 10 +++-
src/site/asciidoc/users/protocols/simulated.adoc | 3 +-
12 files changed, 126 insertions(+), 18 deletions(-)
diff --git a/build-utils/language-java/src/main/java/org/apache/plc4x/language/java/JavaLanguageTemplateHelper.java b/build-utils/language-java/src/main/java/org/apache/plc4x/language/java/JavaLanguageTemplateHelper.java
index 2a99c7f..e2906ca 100644
--- a/build-utils/language-java/src/main/java/org/apache/plc4x/language/java/JavaLanguageTemplateHelper.java
+++ b/build-utils/language-java/src/main/java/org/apache/plc4x/language/java/JavaLanguageTemplateHelper.java
@@ -292,8 +292,8 @@ public class JavaLanguageTemplateHelper extends BaseFreemarkerLanguageTemplateHe
return floatTypeReference.getSizeInBits();
}
case STRING: {
- IntegerTypeReference integerTypeReference = (IntegerTypeReference) simpleTypeReference;
- return integerTypeReference.getSizeInBits();
+ StringTypeReference stringTypeReference = (StringTypeReference) simpleTypeReference;
+ return stringTypeReference.getSizeInBits();
}
default: {
return 0;
diff --git a/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/connection/SimulatedDevice.java b/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/connection/SimulatedDevice.java
index fd6932b..80003a9 100644
--- a/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/connection/SimulatedDevice.java
+++ b/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/connection/SimulatedDevice.java
@@ -91,6 +91,17 @@ public class SimulatedDevice {
System.out.printf("TEST PLC STDOUT [%s]: %s%n", field.getName(), value.getString());
return;
case RANDOM:
+ switch (field.getPlcDataType()) {
+ case "IEC61131_STRING":
+ case "IEC61131_WSTRING":
+ break;
+ default:
+ try {
+ DataItemIO.staticSerialize(value, field.getPlcDataType(), 1, false);
+ } catch (ParseException e) {
+ System.out.printf("Write failed");
+ }
+ }
System.out.printf("TEST PLC RANDOM [%s]: %s%n", field.getName(), value.getString());
return;
}
diff --git a/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/field/SimulatedField.java b/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/field/SimulatedField.java
index e9d3e23..f71e5e6 100644
--- a/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/field/SimulatedField.java
+++ b/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/field/SimulatedField.java
@@ -42,7 +42,7 @@ public class SimulatedField implements PlcField {
* - {@code RANDOM/foo:INTEGER}
* - {@code STDOUT/foo:STRING}
*/
- private static final Pattern ADDRESS_PATTERN = Pattern.compile("^(?<type>\\w+)/(?<name>\\w+):(?<dataType>[a-zA-Z0-9\\.]++)(\\[(?<numElements>\\d+)])?$");
+ private static final Pattern ADDRESS_PATTERN = Pattern.compile("^(?<type>\\w+)/(?<name>[a-zA-Z0-9_\\.]+):(?<dataType>[a-zA-Z0-9]++)(\\[(?<numElements>\\d+)])?$");
private final SimulatedFieldType type;
private final String name;
@@ -61,7 +61,32 @@ public class SimulatedField implements PlcField {
if (matcher.matches()) {
SimulatedFieldType type = SimulatedFieldType.valueOf(matcher.group("type"));
String name = matcher.group("name");
- String dataType = "IEC61131_" + matcher.group("dataType").toUpperCase();
+ String dataType;
+ switch (matcher.group("dataType").toUpperCase()) {
+ case "INTEGER":
+ dataType = "IEC61131_DINT";
+ break;
+ case "BYTE":
+ dataType = "IEC61131_BYTE";
+ break;
+ case "SHORT":
+ dataType = "IEC61131_INT";
+ break;
+ case "LONG":
+ dataType = "IEC61131_LINT";
+ break;
+ case "FLOAT":
+ dataType = "IEC61131_REAL";
+ break;
+ case "DOUBLE":
+ dataType = "IEC61131_LREAL";
+ break;
+ case "BOOLEAN":
+ dataType = "IEC61131_BOOL";
+ break;
+ default:
+ dataType = "IEC61131_" + matcher.group("dataType").toUpperCase();
+ }
try {
SimulatedDataTypeSizes.enumForValue(dataType).getDataTypeSize();
} catch (NullPointerException e) {
diff --git a/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/utils/StaticHelper.java b/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/utils/StaticHelper.java
new file mode 100644
index 0000000..8f34fad
--- /dev/null
+++ b/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/utils/StaticHelper.java
@@ -0,0 +1,66 @@
+/*
+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.simulated.utils;
+
+import org.apache.commons.lang3.NotImplementedException;
+import org.apache.plc4x.java.api.value.PlcValue;
+import org.apache.plc4x.java.spi.generation.ParseException;
+import org.apache.plc4x.java.spi.generation.ReadBuffer;
+import org.apache.plc4x.java.spi.generation.WriteBuffer;
+
+import java.io.IOException;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.temporal.ChronoUnit;
+import java.nio.charset.Charset;
+
+
+public class StaticHelper {
+
+ public static String parsePascalString(ReadBuffer io, String encoding) {
+ try {
+ // This is the maximum number of bytes a string can be long.
+ short stringLength = io.readUnsignedShort(8);
+ // Read the full size of the string.
+ String str = io.readString(stringLength * 8, (String) encoding);
+ // Cut off the parts that don't belong to it.
+ return str;
+ } catch (ParseException e) {
+ return null;
+ }
+ }
+
+ public static void serializePascalString(WriteBuffer io, PlcValue value, String encoding) throws ParseException {
+ final byte[] bytes = value.getString().getBytes(Charset.forName(encoding));
+ try {
+ if (bytes.length < 256) {
+ io.writeByte(8, (byte) bytes.length);
+ for (byte aByte : bytes) {
+ io.writeByte(8, aByte);
+ }
+ } else {
+ throw new ParseException("Error writing string, string > 255 bytes");
+ }
+ } catch (ParseException e) {
+ throw new ParseException("Error writing string", e);
+ }
+ }
+
+}
diff --git a/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/connection/SimulatedDeviceTest.java b/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/connection/SimulatedDeviceTest.java
index 9bc22d8..b2d0b1e 100644
--- a/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/connection/SimulatedDeviceTest.java
+++ b/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/connection/SimulatedDeviceTest.java
@@ -32,7 +32,7 @@ public class SimulatedDeviceTest {
@Test
public void random() {
SimulatedDevice device = new SimulatedDevice("foobar");
- SimulatedField field = SimulatedField.of("RANDOM/foo:INT");
+ SimulatedField field = SimulatedField.of("RANDOM/foo:Integer");
Optional<PlcValue> value = device.get(field);
@@ -42,7 +42,7 @@ public class SimulatedDeviceTest {
@Test
public void read() {
SimulatedDevice device = new SimulatedDevice("foobar");
- SimulatedField field = SimulatedField.of("STATE/bar:INT");
+ SimulatedField field = SimulatedField.of("STATE/bar:Integer");
Optional<PlcValue> value = device.get(field);
assertFalse(value.isPresent());
diff --git a/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/field/SimularedFieldHandlerTest.java b/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/field/SimularedFieldHandlerTest.java
index dd0a27d..c65b395 100644
--- a/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/field/SimularedFieldHandlerTest.java
+++ b/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/field/SimularedFieldHandlerTest.java
@@ -44,7 +44,7 @@ class SimularedFieldHandlerTest implements WithAssertions {
@Test
void createField() {
- assertThat(SUT.createField("STATE/bar:INT")).isNotNull();
+ assertThat(SUT.createField("STATE/bar:Integer")).isNotNull();
}
/*@Test
diff --git a/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/field/SimulatedFieldTest.java b/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/field/SimulatedFieldTest.java
index 16b1d55..eb0bb66 100644
--- a/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/field/SimulatedFieldTest.java
+++ b/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/field/SimulatedFieldTest.java
@@ -32,14 +32,14 @@ public class SimulatedFieldTest {
@Test
public void constructor() {
- assertThat(SimulatedField.matches("RANDOM/test:Int[2]"), equalTo(true));
- SimulatedField field = SimulatedField.of("RANDOM/test:Int[2]");
+ assertThat(SimulatedField.matches("RANDOM/test:Integer[2]"), equalTo(true));
+ SimulatedField field = SimulatedField.of("RANDOM/test:Integer[2]");
assertThat(field.getType(), equalTo(SimulatedFieldType.RANDOM));
assertThat(field.getName(), equalTo("test"));
- assertThat(field.getPlcDataType(), equalTo("IEC61131_INT"));
+ assertThat(field.getPlcDataType(), equalTo("IEC61131_DINT"));
assertThat(field.getNumberOfElements(), equalTo(2));
assertThat(field.toString(),
- equalTo("TestField{type=RANDOM, name='test', dataType='IEC61131_INT', numElements=2}"));
+ equalTo("TestField{type=RANDOM, name='test', dataType='IEC61131_DINT', numElements=2}"));
}
/*[TODO] Add support for Full Java Type Names back in after plc4go changes have merged
diff --git a/plc4j/integrations/apache-calcite/src/test/java/org/apache/plc4x/DriverManagerTest.java b/plc4j/integrations/apache-calcite/src/test/java/org/apache/plc4x/DriverManagerTest.java
index f0fcace..c06c3ad 100644
--- a/plc4j/integrations/apache-calcite/src/test/java/org/apache/plc4x/DriverManagerTest.java
+++ b/plc4j/integrations/apache-calcite/src/test/java/org/apache/plc4x/DriverManagerTest.java
@@ -73,7 +73,7 @@ public class DriverManagerTest implements WithAssertions {
// Column types
assertThat(metadata.getColumnTypeName(1)).isEqualTo("TIMESTAMP");
assertThat(metadata.getColumnTypeName(2)).isEqualTo("VARCHAR");
- assertThat(metadata.getColumnTypeName(3)).isEqualTo("SMALLINT");
+ assertThat(metadata.getColumnTypeName(3)).isEqualTo("INTEGER");
assertThat(metadata.getColumnTypeName(4)).isEqualTo("VARCHAR");
int rowCount = 0;
diff --git a/plc4j/integrations/apache-calcite/src/test/resources/example.yml b/plc4j/integrations/apache-calcite/src/test/resources/example.yml
index a38f1cb..7d7bb8e 100644
--- a/plc4j/integrations/apache-calcite/src/test/resources/example.yml
+++ b/plc4j/integrations/apache-calcite/src/test/resources/example.yml
@@ -28,5 +28,5 @@ jobs:
- test
- test2
fields:
- test: 'RANDOM/test:INT'
- test2: 'RANDOM/test:STRING'
+ test: 'RANDOM/test:Integer'
+ test2: 'RANDOM/test:String'
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/ReadBuffer.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/ReadBuffer.java
index d890f7b..7652d93 100644
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/ReadBuffer.java
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/ReadBuffer.java
@@ -300,6 +300,7 @@ public class ReadBuffer {
throw new PlcRuntimeException(e);
}
}
+ //replaceAll function removes and leading ' char or hypens.
return new String(strBytes, Charset.forName(encoding.replaceAll("[^a-zA-Z0-9]","")));
}
diff --git a/protocols/simulated/src/main/resources/protocols/simulated/simulated.mspec b/protocols/simulated/src/main/resources/protocols/simulated/simulated.mspec
index a8afe1b..433ec99 100644
--- a/protocols/simulated/src/main/resources/protocols/simulated/simulated.mspec
+++ b/protocols/simulated/src/main/resources/protocols/simulated/simulated.mspec
@@ -124,6 +124,12 @@
['IEC61131_WCHAR' List
[array uint 16 'value' count 'numberOfValues']
]
+ ['IEC61131_STRING' STRING
+ [manual string 'UTF-8' 'value' 'STATIC_CALL("org.apache.plc4x.java.simulated.utils.StaticHelper.parsePascalString", io, _type.encoding)' 'STATIC_CALL("org.apache.plc4x.java.simulated.utils.StaticHelper.serializePascalString", io, _value, _type.encoding)' '_value.length + 2']
+ ]
+ ['IEC61131_WSTRING' STRING
+ [manual string 'UTF-16' 'value''STATIC_CALL("org.apache.plc4x.java.simulated.utils.StaticHelper.parsePascalString", io, _type.encoding)' 'STATIC_CALL("org.apache.plc4x.java.simulated.utils.StaticHelper.serializePascalString", io, _value, _type.encoding)' '(_value.length * 2) + 2']
+ ]
]
]
@@ -153,6 +159,6 @@
['IEC61131_LDATE_AND_TIME' LDATE_AND_TIME ['8']]
['IEC61131_CHAR' CHAR ['1']]
['IEC61131_WCHAR' WCHAR ['2']]
- ['IEC61131_STRING' STRING ['1']]
- ['IEC61131_WSTRING' WSTRING ['2']]
+ ['IEC61131_STRING' STRING ['256']]
+ ['IEC61131_WSTRING' WSTRING ['127']]
]
diff --git a/src/site/asciidoc/users/protocols/simulated.adoc b/src/site/asciidoc/users/protocols/simulated.adoc
index 81ced0d..66f72bc 100644
--- a/src/site/asciidoc/users/protocols/simulated.adoc
+++ b/src/site/asciidoc/users/protocols/simulated.adoc
@@ -89,7 +89,7 @@ If the data-type part is omitted, it defaults to STRING.
The simulation device supports 3 different simulation types
-- STATE - This holds in memory a value for athe given alias. This value can be read or written to, however this
+- STATE - This holds in memory a value for a given alias. This value can be read or written to, however this
should only be used in conjunction with a persistent connection. Once the connection is closed the memory area is cleared.
- RANDOM - This provides a new random value for each read. When writing, a log message is recorded and the value is discarded.
- STDOUT - Always returns a null value when reading. When writing, a log message is recorded and the value is discarded.
@@ -138,4 +138,3 @@ All of these address formats are valid:-
- RANDOM/device1.machineA.sensor1:STRING
- STATE/device1.machineA.sensor1:INT
- STDOUT/device1.machineA.sensor1:INT
-