You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by jf...@apache.org on 2019/03/07 09:50:31 UTC
[incubator-plc4x] branch extend-Field-with-type-information
updated: Added Implementation for S7 Driver.
This is an automated email from the ASF dual-hosted git repository.
jfeinauer pushed a commit to branch extend-Field-with-type-information
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git
The following commit(s) were added to refs/heads/extend-Field-with-type-information by this push:
new 39c9b47 Added Implementation for S7 Driver.
39c9b47 is described below
commit 39c9b4738fd86b002a660dc9ba7adc4b4b24a26e
Author: Julian Feinauer <j....@pragmaticminds.de>
AuthorDate: Thu Mar 7 10:38:25 2019 +0100
Added Implementation for S7 Driver.
---
.../org/apache/plc4x/java/api/PlcConnection.java | 6 ++++--
.../org/apache/plc4x/java/api/model/PlcField.java | 7 +++++++
.../plc4x/java/s7/connection/S7PlcConnection.java | 8 ++++++++
.../java/s7/connection/S7PlcConnectionTests.java | 15 ++++++++++++++
.../org/apache/plc4x/java/s7/model/S7Field.java | 23 ++++++++++++++++++++++
.../apache/plc4x/java/s7/model/S7FieldTests.java | 8 ++++++++
6 files changed, 65 insertions(+), 2 deletions(-)
diff --git a/plc4j/api/src/main/java/org/apache/plc4x/java/api/PlcConnection.java b/plc4j/api/src/main/java/org/apache/plc4x/java/api/PlcConnection.java
index ea8525c..e7d9a83 100644
--- a/plc4j/api/src/main/java/org/apache/plc4x/java/api/PlcConnection.java
+++ b/plc4j/api/src/main/java/org/apache/plc4x/java/api/PlcConnection.java
@@ -19,6 +19,7 @@ under the License.
package org.apache.plc4x.java.api;
import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
+import org.apache.plc4x.java.api.exceptions.PlcInvalidFieldException;
import org.apache.plc4x.java.api.exceptions.PlcRuntimeException;
import org.apache.plc4x.java.api.exceptions.PlcUnsupportedOperationException;
import org.apache.plc4x.java.api.messages.PlcReadRequest;
@@ -55,10 +56,11 @@ public interface PlcConnection extends AutoCloseable {
void close() throws Exception;
/**
- * Parse a fieldQuery for the given connection type
+ * Parse a fieldQuery for the given connection type.
+ *
* @throws PlcRuntimeException If the string cannot be parsed
*/
- default PlcField parse(String fieldQuery) throws PlcRuntimeException {
+ default PlcField parse(String fieldQuery) throws PlcInvalidFieldException {
throw new PlcRuntimeException("Parse method is not implemented for this connection / driver");
}
diff --git a/plc4j/api/src/main/java/org/apache/plc4x/java/api/model/PlcField.java b/plc4j/api/src/main/java/org/apache/plc4x/java/api/model/PlcField.java
index 48216e8..6851667 100644
--- a/plc4j/api/src/main/java/org/apache/plc4x/java/api/model/PlcField.java
+++ b/plc4j/api/src/main/java/org/apache/plc4x/java/api/model/PlcField.java
@@ -35,6 +35,13 @@ public interface PlcField {
/**
* Returns the "default" type of the response one can expect from this field as java type.
* I.e., a call to getXXX for the ResponseItem should succeed.
+ *
+ * The contract is not to return the exact type which is internally hold by PLC4X but to
+ * return a "casteable" type, i.e., a type whiches getXXX method succeeds.
+ * So for example if a type is internally a short, but the getInt() method is implemented, it
+ * is allowed to return Integer.class by this method.
+ *
+ * <b>This method should always return the BOXED type for primitives. E.g. not bool.class but Boolean.class</b>
* @return Either specific type or Object.class if no specific type is known.
*/
default Class<?> getDefaultJavaType() {
diff --git a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/connection/S7PlcConnection.java b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/connection/S7PlcConnection.java
index da65b9a..e3e9aec 100644
--- a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/connection/S7PlcConnection.java
+++ b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/connection/S7PlcConnection.java
@@ -24,10 +24,13 @@ import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.SystemConfiguration;
import org.apache.commons.lang3.StringUtils;
import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
+import org.apache.plc4x.java.api.exceptions.PlcInvalidFieldException;
+import org.apache.plc4x.java.api.exceptions.PlcRuntimeException;
import org.apache.plc4x.java.api.messages.PlcReadRequest;
import org.apache.plc4x.java.api.messages.PlcReadResponse;
import org.apache.plc4x.java.api.messages.PlcWriteRequest;
import org.apache.plc4x.java.api.messages.PlcWriteResponse;
+import org.apache.plc4x.java.api.model.PlcField;
import org.apache.plc4x.java.base.connection.ChannelFactory;
import org.apache.plc4x.java.base.connection.NettyPlcConnection;
import org.apache.plc4x.java.base.connection.TcpSocketChannelFactory;
@@ -40,6 +43,7 @@ import org.apache.plc4x.java.isotp.protocol.model.tpdus.DisconnectRequestTpdu;
import org.apache.plc4x.java.isotp.protocol.model.types.DeviceGroup;
import org.apache.plc4x.java.isotp.protocol.model.types.DisconnectReason;
import org.apache.plc4x.java.isotp.protocol.model.types.TpduSize;
+import org.apache.plc4x.java.s7.model.S7Field;
import org.apache.plc4x.java.s7.netty.Plc4XS7Protocol;
import org.apache.plc4x.java.s7.netty.S7Protocol;
import org.apache.plc4x.java.s7.netty.model.types.MemoryArea;
@@ -200,6 +204,10 @@ public class S7PlcConnection extends NettyPlcConnection implements PlcReader, Pl
channel.pipeline().fireUserEventTriggered(new ConnectEvent());
}
+ @Override public PlcField parse(String fieldQuery) throws PlcInvalidFieldException {
+ return S7Field.of(fieldQuery);
+ }
+
public int getRack() {
return rack;
}
diff --git a/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/connection/S7PlcConnectionTests.java b/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/connection/S7PlcConnectionTests.java
index 65d37ba..c0d938d 100644
--- a/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/connection/S7PlcConnectionTests.java
+++ b/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/connection/S7PlcConnectionTests.java
@@ -19,7 +19,11 @@
package org.apache.plc4x.java.s7.connection;
+import org.apache.plc4x.java.api.exceptions.PlcInvalidFieldException;
import org.apache.plc4x.java.api.exceptions.PlcUnsupportedOperationException;
+import org.apache.plc4x.java.api.model.PlcField;
+import org.apache.plc4x.java.s7.model.S7Field;
+import org.apache.plc4x.java.s7.netty.model.types.TransportSize;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -75,4 +79,15 @@ public class S7PlcConnectionTests {
assertThrows(PlcUnsupportedOperationException.class, () -> SUT.unsubscriptionRequestBuilder());
}
+ @Test
+ public void parse() {
+ final PlcField field = SUT.parse("%DB1.DBX38.1:BOOL");
+ assertThat(field.getClass(), equalTo(S7Field.class));
+ assertThat(((S7Field) field).getDataType(), equalTo(TransportSize.BOOL));
+ }
+
+ @Test(expected = PlcInvalidFieldException.class)
+ public void parseFails() {
+ SUT.parse("this is a bad field query");
+ }
}
\ No newline at end of file
diff --git a/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/model/S7Field.java b/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/model/S7Field.java
index 8f7e555..e0d78d6 100644
--- a/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/model/S7Field.java
+++ b/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/model/S7Field.java
@@ -18,6 +18,7 @@ under the License.
*/
package org.apache.plc4x.java.s7.model;
+import org.apache.commons.lang3.NotImplementedException;
import org.apache.plc4x.java.api.exceptions.PlcInvalidFieldException;
import org.apache.plc4x.java.api.model.PlcField;
import org.apache.plc4x.java.s7.netty.model.types.MemoryArea;
@@ -89,6 +90,28 @@ public class S7Field implements PlcField {
ADDRESS_PATTERN.matcher(fieldString).matches();
}
+ /**
+ * @return Java type of expected response.
+ *
+ * TODO implement all Methods
+ */
+ @Override public Class<?> getDefaultJavaType() {
+ switch (dataType){
+ case STRING:
+ return String.class;
+ case INT:
+ case DINT:
+ case UINT:
+ return Long.class;
+ case BOOL:
+ return Boolean.class;
+ case REAL:
+ return Double.class;
+ default:
+ throw new NotImplementedException("The response type for datatype " + dataType + " is not yet implemented");
+ }
+ }
+
public static S7Field of(String fieldString) {
Matcher matcher = DATA_BLOCK_ADDRESS_PATTERN.matcher(fieldString);
if(matcher.matches()) {
diff --git a/plc4j/protocols/s7/src/test/java/org/apache/plc4x/java/s7/model/S7FieldTests.java b/plc4j/protocols/s7/src/test/java/org/apache/plc4x/java/s7/model/S7FieldTests.java
index 015562d..a412b67 100644
--- a/plc4j/protocols/s7/src/test/java/org/apache/plc4x/java/s7/model/S7FieldTests.java
+++ b/plc4j/protocols/s7/src/test/java/org/apache/plc4x/java/s7/model/S7FieldTests.java
@@ -20,6 +20,7 @@ under the License.
package org.apache.plc4x.java.s7.model;
import org.apache.plc4x.java.api.exceptions.PlcRuntimeException;
+import org.apache.plc4x.java.api.model.PlcField;
import org.apache.plc4x.java.s7.netty.model.types.MemoryArea;
import org.apache.plc4x.java.s7.netty.model.types.TransportSize;
import org.apache.plc4x.test.FastTests;
@@ -86,6 +87,13 @@ class S7FieldTests {
}
@Test
+ void getDefaultJavaType() {
+ final PlcField field = S7Field.of("%DB1.DBX38.1:BOOL");
+
+ assertThat(field.getDefaultJavaType(), equalTo(Boolean.class));
+ }
+
+ @Test
void checkGreedyNumFieldsParsing() {
S7Field field = S7Field.of("%DB56.DBB100:SINT[25]");