kopia lustrzana https://github.com/bertrik/ttnhabbridge
Refactor CayenneItem / CayenneMessage a bit.
rodzic
bcbe5daa41
commit
3e4577b69f
|
@ -1,5 +1,7 @@
|
|||
package nl.sikken.bertrik.cayenne;
|
||||
|
||||
import java.nio.BufferUnderflowException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Arrays;
|
||||
import java.util.Locale;
|
||||
|
||||
|
@ -41,6 +43,28 @@ public final class CayenneItem {
|
|||
return type.format(values);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses one item from the byte buffer and returns it.
|
||||
*
|
||||
* @param bb the byte buffer
|
||||
* @return a new cayenne item
|
||||
* @throws CayenneException if an error occurs during parsing
|
||||
*/
|
||||
public static CayenneItem parse(ByteBuffer bb) throws CayenneException {
|
||||
try {
|
||||
final int channel = bb.get();
|
||||
final int type = bb.get() & 0xFF;
|
||||
final ECayenneItem ct = ECayenneItem.parse(type);
|
||||
if (ct == null) {
|
||||
throw new CayenneException("Invalid cayenne type " + type);
|
||||
}
|
||||
final Double[] values = ct.parse(bb);
|
||||
return new CayenneItem(channel, ct, values);
|
||||
} catch (BufferUnderflowException e) {
|
||||
throw new CayenneException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format(Locale.US, "{chan=%d,type=%s,value=%s}", channel, type, Arrays.toString(format()));
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package nl.sikken.bertrik.cayenne;
|
||||
|
||||
import java.nio.BufferUnderflowException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
@ -23,21 +22,10 @@ public final class CayenneMessage {
|
|||
*/
|
||||
public static CayenneMessage parse(byte[] data) throws CayenneException {
|
||||
final CayenneMessage message = new CayenneMessage();
|
||||
try {
|
||||
final ByteBuffer bb = ByteBuffer.wrap(data);
|
||||
while (bb.hasRemaining()) {
|
||||
final int channel = bb.get();
|
||||
final int type = bb.get() & 0xFF;
|
||||
final ECayenneItem ct = ECayenneItem.parse(type);
|
||||
if (ct == null) {
|
||||
throw new CayenneException("Invalid cayenne type " + type);
|
||||
}
|
||||
final Double[] values = ct.parse(bb);
|
||||
final CayenneItem item = new CayenneItem(channel, ct, values);
|
||||
message.items.add(item);
|
||||
}
|
||||
} catch (BufferUnderflowException e) {
|
||||
throw new CayenneException(e);
|
||||
final ByteBuffer bb = ByteBuffer.wrap(data);
|
||||
while (bb.hasRemaining()) {
|
||||
final CayenneItem item = CayenneItem.parse(bb);
|
||||
message.items.add(item);
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
|
|
@ -64,7 +64,6 @@ public final class CayenneMessageTest {
|
|||
final byte[] data = {1, 104, 100};
|
||||
final CayenneMessage payload = CayenneMessage.parse(data);
|
||||
|
||||
// final Map<Integer, String[]> items = payload.getItems();
|
||||
Assert.assertArrayEquals(new String[] {"50.0"}, payload.ofChannel(1).format());
|
||||
}
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue