You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by jm...@apache.org on 2018/02/27 06:53:54 UTC

[incubator-plc4x] branch master updated (d488e98 -> a0a4ef6)

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

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


    from d488e98  Merge pull request #7
     new 0863a31  unnneeded brackets
     new d6c75e0  use diamond operator
     new a0a4ef6  refactor to make simplier

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../apache/plc4x/edgent/PlcConnectionAdapter.java  |   2 +-
 .../java/ads/netty/util/LittleEndianDecoder.java   | 131 +++++++++++++--------
 2 files changed, 80 insertions(+), 53 deletions(-)

-- 
To stop receiving notification emails like this one, please contact
jmclean@apache.org.

[incubator-plc4x] 03/03: refactor to make simplier

Posted by jm...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit a0a4ef6197707c80cb13b21705939163e5626c80
Author: Justin Mclean <jm...@apache.org>
AuthorDate: Tue Feb 27 17:53:43 2018 +1100

    refactor to make simplier
---
 .../java/ads/netty/util/LittleEndianDecoder.java   | 130 +++++++++++++--------
 1 file changed, 79 insertions(+), 51 deletions(-)

diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/netty/util/LittleEndianDecoder.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/netty/util/LittleEndianDecoder.java
index 6d58d0b..00ad4ce 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/netty/util/LittleEndianDecoder.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/netty/util/LittleEndianDecoder.java
@@ -38,69 +38,97 @@ public class LittleEndianDecoder {
         List<Object> result = new LinkedList<>();
         int i = 0;
         final int length = adsData.length;
+
         while (i < length) {
             byte byteOne = adsData[i];
             if (datatype == String.class) {
-                StringBuilder builder = new StringBuilder();
-                while (adsData[i] != (byte) 0x0 && i < length) {
-                    builder.append((char) adsData[i]);
-                    i++;
-                }
-                i++; // skip terminating character
-                result.add(builder.toString());
+                i = decodeString(adsData, i, length, result);
             } else if (datatype == Boolean.class) {
                 result.add((byteOne & 0x01) == 0x01);
                 i += 1;
             } else if (datatype == Byte.class) {
                 result.add(byteOne);
                 i += 1;
+            } else  if (datatype == Short.class) {
+                decodeShort(adsData, i, result);
+                i += 2;
+            } else if (datatype == Integer.class) {
+                decodeInteger(adsData, i, result);
+                i += 4;
+            } else if (datatype == Float.class) {
+                decodeFloat(adsData, i, result);
+                i += 4;
+            } else if (datatype == Calendar.class || Calendar.class.isAssignableFrom(datatype)) {
+                extractCalendar(adsData, i, result);
+                i += 8;
             } else {
-                byte byteTwo = adsData[i + 1];
-                if (datatype == Short.class) {
-                    result.add((short) ((byteOne & 0xff) | ((byteTwo & 0xff) << 8)));
-                    i += 2;
-                } else {
-                    byte byteThree = adsData[i + 2];
-                    byte byteFour = adsData[i + 3];
-                    if (datatype == Integer.class) {
-                        result.add((byteOne & 0xff) | ((byteTwo & 0xff) << 8) | ((byteThree & 0xff) << 16) | (byteFour & 0xff) << 24);
-                        i += 4;
-                    } else if (datatype == Float.class) {
-                        // TODO: check how ads expects this data
-                        // Description of the Real number format:
-                        // https://www.sps-lehrgang.de/zahlenformate-step7/#c144
-                        // https://de.wikipedia.org/wiki/IEEE_754
-                        int intValue = (byteOne & 0xff) | ((byteTwo & 0xff) << 8) | ((byteThree & 0xff) << 16) | ((byteFour & 0xff) << 24);
-                        result.add(Float.intBitsToFloat(intValue));
-                        i += 4;
-                    } else {
-                        if (datatype == Calendar.class || Calendar.class.isAssignableFrom(datatype)) {
-                            byte byteFive = adsData[i + 4];
-                            byte byteSix = adsData[i + 5];
-                            byte byteSeven = adsData[i + 6];
-                            byte byteEight = adsData[i + 7];
-                            Calendar calendar = Calendar.getInstance();
-                            calendar.setTime(new Date(TimeStamp.winTimeToJava(
-                                new BigInteger(new byte[]{
-                                    // LE
-                                    byteEight,
-                                    byteSeven,
-                                    byteSix,
-                                    byteFive,
-                                    byteFour,
-                                    byteThree,
-                                    byteTwo,
-                                    byteOne,
-                                })).longValue()));
-                            result.add(calendar);
-                            i += 8;
-                        } else {
-                            throw new PlcProtocolException("Unsupported datatype " + datatype.getSimpleName());
-                        }
-                    }
-                }
+                throw new PlcProtocolException("Unsupported datatype " + datatype.getSimpleName());
             }
         }
         return (List<T>) result;
     }
+
+    private static int decodeString(byte[] adsData, int i, int length, List<Object> result) {
+        int pos = i;
+        StringBuilder builder = new StringBuilder();
+        while (adsData[pos] != (byte) 0x0 && pos < length) {
+            builder.append((char) adsData[pos]);
+            pos++;
+        }
+        pos++; // skip terminating character
+        result.add(builder.toString());
+        return pos;
+    }
+
+    private static void decodeShort(byte[] adsData, int i, List<Object> result) {
+        byte byteOne = adsData[i];
+        byte byteTwo = adsData[i+1];
+        result.add((short) ((byteOne & 0xff) | ((byteTwo & 0xff) << 8)));
+    }
+
+    private static void decodeInteger(byte[] adsData, int i, List<Object> result) {
+        byte byteOne = adsData[i];
+        byte byteTwo = adsData[i + 1];
+        byte byteThree = adsData[i + 2];
+        byte byteFour = adsData[i + 3];
+        result.add((byteOne & 0xff) | ((byteTwo & 0xff) << 8) | ((byteThree & 0xff) << 16) | (byteFour & 0xff) << 24);
+    }
+
+    private static void decodeFloat(byte[] adsData, int i, List<Object> result) {
+        byte byteOne = adsData[i];
+        byte byteTwo = adsData[i + 1];
+        byte byteThree = adsData[i + 2];
+        byte byteFour = adsData[i + 3];
+        // TODO: check how ads expects this data
+        // Description of the Real number format:
+        // https://www.sps-lehrgang.de/zahlenformate-step7/#c144
+        // https://de.wikipedia.org/wiki/IEEE_754
+        int intValue = (byteOne & 0xff) | ((byteTwo & 0xff) << 8) | ((byteThree & 0xff) << 16) | ((byteFour & 0xff) << 24);
+        result.add(Float.intBitsToFloat(intValue));
+    }
+
+    private static void extractCalendar(byte[] adsData, int i, List<Object> result) {
+        byte byteOne = adsData[i];
+        byte byteTwo = adsData[i + 1];
+        byte byteThree = adsData[i + 2];
+        byte byteFour = adsData[i + 3];
+        byte byteFive = adsData[i + 4];
+        byte byteSix = adsData[i + 5];
+        byte byteSeven = adsData[i + 6];
+        byte byteEight = adsData[i + 7];
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTime(new Date(TimeStamp.winTimeToJava(
+            new BigInteger(new byte[]{
+                // LE
+                byteEight,
+                byteSeven,
+                byteSix,
+                byteFive,
+                byteFour,
+                byteThree,
+                byteTwo,
+                byteOne,
+            })).longValue()));
+        result.add(calendar);
+    }
 }

-- 
To stop receiving notification emails like this one, please contact
jmclean@apache.org.

[incubator-plc4x] 01/03: unnneeded brackets

Posted by jm...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 0863a31979916d2c2771c5a2a36ffe161c2e3fab
Author: Justin Mclean <jm...@apache.org>
AuthorDate: Tue Feb 27 17:31:13 2018 +1100

    unnneeded brackets
---
 .../org/apache/plc4x/java/ads/netty/util/LittleEndianDecoder.java  | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/netty/util/LittleEndianDecoder.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/netty/util/LittleEndianDecoder.java
index e37f2f2..6d58d0b 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/netty/util/LittleEndianDecoder.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/netty/util/LittleEndianDecoder.java
@@ -57,20 +57,20 @@ public class LittleEndianDecoder {
             } else {
                 byte byteTwo = adsData[i + 1];
                 if (datatype == Short.class) {
-                    result.add((short) (((byteOne & 0xff) & 0xff) | ((byteTwo & 0xff) << 8)));
+                    result.add((short) ((byteOne & 0xff) | ((byteTwo & 0xff) << 8)));
                     i += 2;
                 } else {
                     byte byteThree = adsData[i + 2];
                     byte byteFour = adsData[i + 3];
                     if (datatype == Integer.class) {
-                        result.add(((byteOne & 0xff)) | ((byteTwo & 0xff) << 8) | ((byteThree & 0xff) << 16) | ((byteFour & 0xff)) << 24);
+                        result.add((byteOne & 0xff) | ((byteTwo & 0xff) << 8) | ((byteThree & 0xff) << 16) | (byteFour & 0xff) << 24);
                         i += 4;
                     } else if (datatype == Float.class) {
                         // TODO: check how ads expects this data
                         // Description of the Real number format:
                         // https://www.sps-lehrgang.de/zahlenformate-step7/#c144
                         // https://de.wikipedia.org/wiki/IEEE_754
-                        int intValue = ((byteOne & 0xff)) | ((byteTwo & 0xff) << 8) | ((byteThree & 0xff) << 16) | ((byteFour & 0xff) << 24);
+                        int intValue = (byteOne & 0xff) | ((byteTwo & 0xff) << 8) | ((byteThree & 0xff) << 16) | ((byteFour & 0xff) << 24);
                         result.add(Float.intBitsToFloat(intValue));
                         i += 4;
                     } else {
@@ -87,7 +87,6 @@ public class LittleEndianDecoder {
                                     byteSeven,
                                     byteSix,
                                     byteFive,
-
                                     byteFour,
                                     byteThree,
                                     byteTwo,

-- 
To stop receiving notification emails like this one, please contact
jmclean@apache.org.

[incubator-plc4x] 02/03: use diamond operator

Posted by jm...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit d6c75e0e28cad1a3c4323a6a000371246456cb55
Author: Justin Mclean <jm...@apache.org>
AuthorDate: Tue Feb 27 17:31:57 2018 +1100

    use diamond operator
---
 .../src/main/java/org/apache/plc4x/edgent/PlcConnectionAdapter.java     | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/integrations/apache-edgent/src/main/java/org/apache/plc4x/edgent/PlcConnectionAdapter.java b/integrations/apache-edgent/src/main/java/org/apache/plc4x/edgent/PlcConnectionAdapter.java
index 3908099..9a6d1c3 100644
--- a/integrations/apache-edgent/src/main/java/org/apache/plc4x/edgent/PlcConnectionAdapter.java
+++ b/integrations/apache-edgent/src/main/java/org/apache/plc4x/edgent/PlcConnectionAdapter.java
@@ -109,7 +109,7 @@ public class PlcConnectionAdapter implements AutoCloseable {
     <T> Supplier<T> newSupplier(Class<T> datatype, String addressStr) {
         PlcConnectionAdapter.checkDatatype(datatype);
         // satisfy sonar's "Reduce number of anonymous class lines" code smell
-        return new MySupplier<T>(datatype, addressStr);
+        return new MySupplier<>(datatype, addressStr);
     }
     
     private class MySupplier<T> implements Supplier<T> {

-- 
To stop receiving notification emails like this one, please contact
jmclean@apache.org.