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:57 UTC
[incubator-plc4x] 03/03: refactor to make simplier
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.