From cf619556709df6d5481d2e4f712ddd6eeebab89d Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 27 Mar 2018 00:55:44 +1300 Subject: Improve Watch data decoding --- src/org/traccar/protocol/WatchFrameDecoder.java | 2 +- src/org/traccar/protocol/WatchProtocolDecoder.java | 10 ++++++---- test/org/traccar/protocol/WatchProtocolDecoderTest.java | 3 +++ 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/org/traccar/protocol/WatchFrameDecoder.java b/src/org/traccar/protocol/WatchFrameDecoder.java index f6b5ef12c..0009ef30f 100644 --- a/src/org/traccar/protocol/WatchFrameDecoder.java +++ b/src/org/traccar/protocol/WatchFrameDecoder.java @@ -45,7 +45,7 @@ public class WatchFrameDecoder extends FrameDecoder { } if (payloadIndex + 5 < buf.writerIndex() && buf.getByte(payloadIndex + 5) == '*' - && buf.toString(payloadIndex + 1, 4, StandardCharsets.US_ASCII).matches("[0-9A-F]+")) { + && buf.toString(payloadIndex + 1, 4, StandardCharsets.US_ASCII).matches("\\p{XDigit}+")) { lengthIndex = payloadIndex + 1; payloadIndex = buf.indexOf(lengthIndex, buf.writerIndex(), (byte) '*'); if (payloadIndex < 0) { diff --git a/src/org/traccar/protocol/WatchProtocolDecoder.java b/src/org/traccar/protocol/WatchProtocolDecoder.java index c57279296..41cd957ae 100644 --- a/src/org/traccar/protocol/WatchProtocolDecoder.java +++ b/src/org/traccar/protocol/WatchProtocolDecoder.java @@ -171,8 +171,8 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { String manufacturer = buf.readBytes(2).toString(StandardCharsets.US_ASCII); buf.skipBytes(1); // delimiter - int idLength = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*') - buf.readerIndex(); - String id = buf.readBytes(idLength).toString(StandardCharsets.US_ASCII); + int idIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); + String id = buf.readBytes(idIndex - buf.readerIndex()).toString(StandardCharsets.US_ASCII); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id); if (deviceSession == null) { return null; @@ -181,7 +181,9 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { buf.skipBytes(1); // delimiter String index = null; - if (idLength > 10) { + int contentIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); + if (contentIndex + 5 < buf.writerIndex() && buf.getByte(contentIndex + 5) == '*' + && buf.toString(contentIndex + 1, 4, StandardCharsets.US_ASCII).matches("\\p{XDigit}+")) { int indexLength = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*') - buf.readerIndex(); index = buf.readBytes(indexLength).toString(StandardCharsets.US_ASCII); buf.skipBytes(1); // delimiter @@ -192,7 +194,7 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { buf.writerIndex(buf.writerIndex() - 1); // ignore ending - int contentIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ','); + contentIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ','); if (contentIndex < 0) { contentIndex = buf.writerIndex(); } diff --git a/test/org/traccar/protocol/WatchProtocolDecoderTest.java b/test/org/traccar/protocol/WatchProtocolDecoderTest.java index 6ba04bf8e..50f4c2ec0 100644 --- a/test/org/traccar/protocol/WatchProtocolDecoderTest.java +++ b/test/org/traccar/protocol/WatchProtocolDecoderTest.java @@ -10,6 +10,9 @@ public class WatchProtocolDecoderTest extends ProtocolTest { WatchProtocolDecoder decoder = new WatchProtocolDecoder(new WatchProtocol()); + verifyPosition(decoder, buffer( + "[SG*352661090143150*006C*UD,150817,132115,V,28.435142,N,81.354333,W,2.2038,000,99,00,70,100,0,50,00000000,1,1,310,260,1091,30082,139,,00]")); + verifyAttributes(decoder, buffer( "[3G*4700609403*0013*bphrt,120,79,73,,,,]")); -- cgit v1.2.3