From 8852f1176ac32a065db734079c295783811c192f Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 16 Apr 2019 23:06:45 -0700 Subject: Support binary serial data --- .../org/traccar/protocol/TeltonikaProtocolDecoder.java | 16 +++++++++++++++- .../traccar/protocol/TeltonikaProtocolDecoderTest.java | 3 +++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/traccar/protocol/TeltonikaProtocolDecoder.java b/src/main/java/org/traccar/protocol/TeltonikaProtocolDecoder.java index 974d2c106..485df833a 100644 --- a/src/main/java/org/traccar/protocol/TeltonikaProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/TeltonikaProtocolDecoder.java @@ -145,8 +145,22 @@ public class TeltonikaProtocolDecoder extends BaseProtocolDecoder { } else { position.set(Position.KEY_TYPE, type); - position.set(Position.KEY_RESULT, buf.readSlice(buf.readInt()).toString(StandardCharsets.US_ASCII)); + int length = buf.readInt(); + boolean readable = true; + for (int i = 0; i < length; i++) { + byte b = buf.getByte(buf.readerIndex() + i); + if (b < 32 && b != '\r' && b != '\n') { + readable = false; + break; + } + } + + if (readable) { + position.set(Position.KEY_RESULT, buf.readSlice(length).toString(StandardCharsets.US_ASCII)); + } else { + position.set(Position.KEY_RESULT, ByteBufUtil.hexDump(buf.readSlice(length))); + } } } diff --git a/src/test/java/org/traccar/protocol/TeltonikaProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/TeltonikaProtocolDecoderTest.java index f94cd9460..b515fbdc6 100644 --- a/src/test/java/org/traccar/protocol/TeltonikaProtocolDecoderTest.java +++ b/src/test/java/org/traccar/protocol/TeltonikaProtocolDecoderTest.java @@ -15,6 +15,9 @@ public class TeltonikaProtocolDecoderTest extends ProtocolTest { verifyNull(decoder, binary( "000F313233343536373839303132333435")); + verifyPositions(decoder, false, binary( + "00000000000000100C010600000008010300010015D5C5010000D988")); + verifyPositions(decoder, binary( "000000000000004c08010000016818d500580009c28d9f1cb3757a00be00c60f0053000f06f0011503c80001011d00fc0007423799180053cdf80dce426f430f88190bb8560bb802f100005aa110002887e000010000ee8d")); -- cgit v1.2.3