You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2018/08/23 12:46:13 UTC

[incubator-plc4x] 02/02: [ADS] use LittleEndianDecoder for determination of length of types.

This is an automated email from the ASF dual-hosted git repository.

sruehl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git

commit bad9ad041519505000d176f3d72ba1a9728cb302
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Thu Aug 23 14:45:51 2018 +0200

    [ADS] use LittleEndianDecoder for determination of length of types.
---
 .../java/ads/connection/AdsTcpPlcConnection.java   |  2 +-
 .../plc4x/java/ads/protocol/Plc4x2AdsProtocol.java | 42 ++--------------------
 .../ads/protocol/util/LittleEndianDecoder.java     |  7 ++--
 .../ads/protocol/util/LittleEndianDecoderTest.java | 17 +++++----
 4 files changed, 14 insertions(+), 54 deletions(-)

diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/connection/AdsTcpPlcConnection.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/connection/AdsTcpPlcConnection.java
index e9032d8..4cc69dd 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/connection/AdsTcpPlcConnection.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/connection/AdsTcpPlcConnection.java
@@ -195,7 +195,7 @@ public class AdsTcpPlcConnection extends AdsAbstractPlcConnection implements Plc
             Invoke.NONE,
             indexGroup,
             indexOffset,
-            LittleEndianDecoder.getLengthFor(datatype, 1),
+            Length.of(LittleEndianDecoder.getLengthFor(datatype, 1)),
             transmissionMode,
             MaxDelay.of(0),
             CycleTime.of(4000000)
diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/Plc4x2AdsProtocol.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/Plc4x2AdsProtocol.java
index b72d471..b4a3a0d 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/Plc4x2AdsProtocol.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/Plc4x2AdsProtocol.java
@@ -29,10 +29,10 @@ import org.apache.plc4x.java.ads.api.generic.types.Invoke;
 import org.apache.plc4x.java.ads.model.AdsAddress;
 import org.apache.plc4x.java.ads.model.SymbolicAdsAddress;
 import org.apache.plc4x.java.ads.protocol.exception.AdsException;
+import org.apache.plc4x.java.ads.protocol.util.LittleEndianDecoder;
 import org.apache.plc4x.java.api.exceptions.PlcException;
 import org.apache.plc4x.java.api.exceptions.PlcIoException;
 import org.apache.plc4x.java.api.exceptions.PlcProtocolException;
-import org.apache.plc4x.java.api.exceptions.PlcUnsupportedDataTypeException;
 import org.apache.plc4x.java.api.messages.*;
 import org.apache.plc4x.java.api.messages.items.ReadRequestItem;
 import org.apache.plc4x.java.api.messages.items.ReadResponseItem;
@@ -49,8 +49,6 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.io.IOException;
-import java.math.BigInteger;
-import java.util.Calendar;
 import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
@@ -194,43 +192,7 @@ public class Plc4x2AdsProtocol extends MessageToMessageCodec<AmsPacket, PlcReque
     }
 
     private long calculateLength(Class<?> dataType, int size) {
-        if (dataType == Boolean.class) {
-            // Boolean is one byte
-            return size;
-        } else if (dataType == Byte.class) {
-            // Byte is one byte
-            return size;
-        } else if (dataType == Short.class) {
-            return (Short.SIZE / Byte.SIZE) * size;
-        } else if (dataType == Float.class) {
-            return (Float.SIZE / Byte.SIZE) * size;
-        } else if (dataType == Integer.class) {
-            return (Integer.SIZE / Byte.SIZE) * size;
-        } else if (dataType == Double.class) {
-            return (Double.SIZE / Byte.SIZE) * size;
-        } else if (dataType == BigInteger.class) {
-            // TODO: how to calculate size for this?
-            //return (BigInteger.SIZE / Byte.SIZE) * size;
-            return 4 + size;
-        } else if (dataType == Calendar.class || Calendar.class.isAssignableFrom(dataType)) {
-            // TODO: how to calculate size for this?
-            //return (Calendar.SIZE / Byte.SIZE) * size;
-            return 4 + size;
-        } else if (dataType == String.class) {
-            // TODO: how to calculate size for this?
-            //return (String.SIZE / Byte.SIZE) * size;
-            return 4 + size;
-        } else if (dataType == byte[].class) {
-            // TODO: how to calculate size for this?
-            //return (byte[].SIZE / Byte.SIZE) * size;
-            return 4 + size;
-        } else if (dataType == Byte[].class) {
-            // TODO: how to calculate size for this?
-            //return (Byte[].SIZE / Byte.SIZE) * size;
-            return 4 + size;
-        } else {
-            throw new PlcUnsupportedDataTypeException(dataType);
-        }
+        return LittleEndianDecoder.getLengthFor(dataType, 4) * size;
     }
 
     private void encodeProprietaryRequest(PlcRequestContainer<PlcRequest, PlcResponse> msg, List<Object> out) throws PlcProtocolException {
diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/util/LittleEndianDecoder.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/util/LittleEndianDecoder.java
index 50f53ce..ec5deec 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/util/LittleEndianDecoder.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/util/LittleEndianDecoder.java
@@ -19,7 +19,6 @@
 package org.apache.plc4x.java.ads.protocol.util;
 
 import org.apache.commons.lang3.ArrayUtils;
-import org.apache.plc4x.java.ads.api.commands.types.Length;
 import org.apache.plc4x.java.ads.api.commands.types.TimeStamp;
 import org.apache.plc4x.java.api.exceptions.PlcProtocolException;
 import org.apache.plc4x.java.api.exceptions.PlcUnsupportedDataTypeException;
@@ -49,11 +48,11 @@ public class LittleEndianDecoder {
         // Utility class
     }
 
-    public static Length getLengthFor(Class<?> clazz, long defaultValue) {
+    public static long getLengthFor(Class<?> clazz, long defaultValue) {
         if (Calendar.class.isAssignableFrom(clazz)) {
-            return Length.of(8);
+            return 8;
         }
-        return Length.of(LENGTH_MAP.getOrDefault(clazz, defaultValue));
+        return LENGTH_MAP.getOrDefault(clazz, defaultValue);
     }
 
     @SuppressWarnings("unchecked")
diff --git a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/protocol/util/LittleEndianDecoderTest.java b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/protocol/util/LittleEndianDecoderTest.java
index 12e5841..ae9319f 100644
--- a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/protocol/util/LittleEndianDecoderTest.java
+++ b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/protocol/util/LittleEndianDecoderTest.java
@@ -18,7 +18,6 @@
  */
 package org.apache.plc4x.java.ads.protocol.util;
 
-import org.apache.plc4x.java.ads.api.commands.types.Length;
 import org.apache.plc4x.java.api.exceptions.PlcProtocolException;
 import org.apache.plc4x.java.api.exceptions.PlcUnsupportedDataTypeException;
 import org.junit.Test;
@@ -35,14 +34,14 @@ public class LittleEndianDecoderTest {
 
     @Test
     public void getLengthFor() {
-        assertEquals(LittleEndianDecoder.getLengthFor(Boolean.class, 0), Length.of(1));
-        assertEquals(LittleEndianDecoder.getLengthFor(Byte.class, 0), Length.of(1));
-        assertEquals(LittleEndianDecoder.getLengthFor(Short.class, 0), Length.of(2));
-        assertEquals(LittleEndianDecoder.getLengthFor(Integer.class, 0), Length.of(4));
-        assertEquals(LittleEndianDecoder.getLengthFor(Float.class, 0), Length.of(4));
-        assertEquals(LittleEndianDecoder.getLengthFor(Double.class, 0), Length.of(8));
-        assertEquals(LittleEndianDecoder.getLengthFor(Calendar.class, 0), Length.of(8));
-        assertEquals(LittleEndianDecoder.getLengthFor(LittleEndianDecoderTest.class, 666), Length.of(666));
+        assertEquals(LittleEndianDecoder.getLengthFor(Boolean.class, 0), 1);
+        assertEquals(LittleEndianDecoder.getLengthFor(Byte.class, 0), 1);
+        assertEquals(LittleEndianDecoder.getLengthFor(Short.class, 0), 2);
+        assertEquals(LittleEndianDecoder.getLengthFor(Integer.class, 0), 4);
+        assertEquals(LittleEndianDecoder.getLengthFor(Float.class, 0), 4);
+        assertEquals(LittleEndianDecoder.getLengthFor(Double.class, 0), 8);
+        assertEquals(LittleEndianDecoder.getLengthFor(Calendar.class, 0), 8);
+        assertEquals(LittleEndianDecoder.getLengthFor(LittleEndianDecoderTest.class, 666), 666);
     }
 
     @Test