Skip to content

Commit

Permalink
refactor to make simplier
Browse files Browse the repository at this point in the history
  • Loading branch information
justinmclean committed Feb 27, 2018
1 parent d6c75e0 commit a0a4ef6
Showing 1 changed file with 79 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,69 +38,97 @@ public static <T> List<T> decodeData(Class<T> datatype, byte[] adsData) throws P
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);
}
}

0 comments on commit a0a4ef6

Please sign in to comment.