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/09 11:27:13 UTC

[incubator-plc4x] branch master updated: Added byte[] and Byte[] implementation to ads.

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


The following commit(s) were added to refs/heads/master by this push:
     new 8c87fca  Added byte[] and Byte[] implementation to ads.
8c87fca is described below

commit 8c87fca926bcddbcc97dcf669bd4774be7d9fc31
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Thu Aug 9 13:27:08 2018 +0200

    Added byte[] and Byte[] implementation to ads.
---
 .../plc4x/java/ads/protocol/util/LittleEndianDecoder.java    |  6 ++++++
 .../plc4x/java/ads/protocol/util/LittleEndianEncoder.java    | 12 ++++++++++++
 .../plc4x/java/ads/protocol/Plc4x2AdsProtocolTest.java       |  6 +++---
 3 files changed, 21 insertions(+), 3 deletions(-)

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 dcea26f..6481952 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
@@ -57,6 +57,12 @@ public class LittleEndianDecoder {
 
     @SuppressWarnings("unchecked")
     public static <T> List<T> decodeData(Class<T> datatype, byte[] adsData) throws PlcProtocolException {
+        if (datatype == byte[].class) {
+            return (List<T>) Collections.singletonList(adsData);
+        }
+        if (datatype == Byte[].class) {
+            return (List<T>) Collections.singletonList(ArrayUtils.toObject(adsData));
+        }
         List<Object> result = new LinkedList<>();
         int i = 0;
         final int length = adsData.length;
diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/util/LittleEndianEncoder.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/util/LittleEndianEncoder.java
index 6b147c5..c477c5f 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/util/LittleEndianEncoder.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/util/LittleEndianEncoder.java
@@ -63,6 +63,10 @@ public class LittleEndianEncoder {
             result = encodeDouble(Arrays.stream(values).map(Double.class::cast));
         } else if (valueType == String.class) {
             result = encodeString(Arrays.stream(values).map(String.class::cast));
+        } else if (valueType == byte[].class) {
+            result = encodeByteArray(Arrays.stream(values).map(byte[].class::cast));
+        } else if (valueType == Byte[].class) {
+            result = encodeBigByteArray(Arrays.stream(values).map(Byte[].class::cast));
         } else {
             throw new PlcProtocolException("Unsupported datatype " + valueType);
         }
@@ -93,6 +97,14 @@ public class LittleEndianEncoder {
             .map(bytes -> ArrayUtils.add(bytes, (byte) 0x0));
     }
 
+    private static Stream<byte[]> encodeByteArray(Stream<byte[]> byteArrayStream) {
+        return byteArrayStream;
+    }
+
+    private static Stream<byte[]> encodeBigByteArray(Stream<Byte[]> byteArrayStream) {
+        return byteArrayStream.map(ArrayUtils::toPrimitive);
+    }
+
     private static Stream<byte[]> encodeFloat(Stream<Float> floatStream) {
         return floatStream
             // TODO: check how ads expects this data
diff --git a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/protocol/Plc4x2AdsProtocolTest.java b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/protocol/Plc4x2AdsProtocolTest.java
index 1d97a02..ebfff15 100644
--- a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/protocol/Plc4x2AdsProtocolTest.java
+++ b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/protocol/Plc4x2AdsProtocolTest.java
@@ -18,6 +18,7 @@
  */
 package org.apache.plc4x.java.ads.protocol;
 
+import org.apache.commons.lang3.NotImplementedException;
 import org.apache.commons.lang3.tuple.ImmutablePair;
 import org.apache.plc4x.java.ads.api.commands.AdsReadResponse;
 import org.apache.plc4x.java.ads.api.commands.AdsWriteRequest;
@@ -68,10 +69,9 @@ public class Plc4x2AdsProtocolTest {
 
     @Rule
     public ExpectedException expectedException = ExpectedException.none();
-    // TODO: implement these types
     private List<String> notYetSupportedDataType = Stream.of(
-        byte[].class,
-        Byte[].class
+        // We can add types which are not implemented in this protocol. The exception is currently a placeholder (empty list).
+        NotImplementedException.class
     ).map(Class::getSimpleName).collect(Collectors.toList());
 
     @Parameterized.Parameter