diff options
-rw-r--r-- | src/org/traccar/helper/Checksum.java | 9 | ||||
-rw-r--r-- | src/org/traccar/protocol/Jt600ProtocolDecoder.java | 39 | ||||
-rw-r--r-- | test/org/traccar/protocol/Jt600ProtocolDecoderTest.java | 6 |
3 files changed, 51 insertions, 3 deletions
diff --git a/src/org/traccar/helper/Checksum.java b/src/org/traccar/helper/Checksum.java index 2a4b1191d..dde174545 100644 --- a/src/org/traccar/helper/Checksum.java +++ b/src/org/traccar/helper/Checksum.java @@ -194,6 +194,15 @@ public final class Checksum { return checksum; } + public static int xor(String string) { + byte[] bytes = string.getBytes(StandardCharsets.US_ASCII); + byte sum = 0; + for (byte b : bytes) { + sum ^= b; + } + return sum; + } + public static String nmea(String msg) { int checksum = 0; for (byte b : msg.getBytes(StandardCharsets.US_ASCII)) { diff --git a/src/org/traccar/protocol/Jt600ProtocolDecoder.java b/src/org/traccar/protocol/Jt600ProtocolDecoder.java index fd2448c0d..6d066299a 100644 --- a/src/org/traccar/protocol/Jt600ProtocolDecoder.java +++ b/src/org/traccar/protocol/Jt600ProtocolDecoder.java @@ -21,6 +21,7 @@ import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; import org.traccar.helper.BcdUtil; +import org.traccar.helper.Checksum; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -202,7 +203,7 @@ public class Jt600ProtocolDecoder extends BaseProtocolDecoder { private static final Pattern PATTERN_U01 = new PatternBuilder() .text("(") .number("(d+),") // id - .number("Udd,") // type + .number("(Udd),") // type .number("d+,").optional() // alarm .number("(dd)(dd)(dd),") // date (ddmmyy) .number("(dd)(dd)(dd),") // time @@ -218,7 +219,8 @@ public class Jt600ProtocolDecoder extends BaseProtocolDecoder { .number("(d+),") // lac .number("(d+),") // gsm signal .number("(d+),") // odometer - .number("(d+)") // index + .number("(d+),?") // serial number + .number("(xx)").optional() // checksum .any() .compile(); @@ -234,6 +236,8 @@ public class Jt600ProtocolDecoder extends BaseProtocolDecoder { return null; } + String messageType = parser.next(); + Position position = new Position(); position.setProtocol(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); @@ -259,8 +263,32 @@ public class Jt600ProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_ODOMETER, parser.nextLong() * 1000); position.set(Position.KEY_INDEX, parser.nextInt()); - return position; + switch (messageType) { + case "U01": + case "U02": + case "U03": + // support protocol with check sum + if (parser.hasNext(2)) { + int checkSum = parser.nextInt(16); + int calculatedCheckSum = Checksum.xor(sentence.substring(1, sentence.length() - 3)); + if (checkSum == calculatedCheckSum) { + sendResponse(channel, "(S39)"); + return position; + } else { + return null; + } + // support protocol without check sum + } else { + return position; + } + case "U06": + sendResponse(channel, "(S20)"); + return position; + default: + return null; + } } + @Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { @@ -282,4 +310,9 @@ public class Jt600ProtocolDecoder extends BaseProtocolDecoder { return null; } + private void sendResponse(Channel channel, String response) { + if (channel != null) { + channel.write(ChannelBuffers.copiedBuffer(response, StandardCharsets.US_ASCII)); + } + } } diff --git a/test/org/traccar/protocol/Jt600ProtocolDecoderTest.java b/test/org/traccar/protocol/Jt600ProtocolDecoderTest.java index 64db2a124..3fc74081f 100644 --- a/test/org/traccar/protocol/Jt600ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/Jt600ProtocolDecoderTest.java @@ -74,6 +74,12 @@ public class Jt600ProtocolDecoderTest extends ProtocolTest { "(6221107674,2,U09,129,2,A,280513113036,E,02711.0500,S,1721.0876,A,030613171243,E,02756.7618,S,2300.0325,3491,538200,14400,1)")); verifyPosition(decoder, buffer( + "(3301210003,U02,040812,185302,T,00.000000,N,000.000000,E,0,0,0,0%,00000000000011,741,51,22,0,1,05)")); + + verifyPosition(decoder, buffer( + "(3301210003,U06,4,250916,133207,T,7.011013,N,25.060708,W,27.61,102,10,0%,00101011000000,0,1,0,448,0,126,1,30)")); + + verifyPosition(decoder, buffer( "(3551001012,U01,010100,000032,F,0.000000,N,0.000000,E,0.00,0,0,10%,00000000010000,15748,7923,23,0,0,3E)")); } |