diff options
author | Vitaly Litvak <vitavaque@gmail.com> | 2016-10-01 09:14:33 +0300 |
---|---|---|
committer | Vitaly Litvak <vitavaque@gmail.com> | 2016-10-01 09:14:33 +0300 |
commit | 69d0cc47d63d9052218a8d87cf436e46165e716c (patch) | |
tree | 24f44abee2c380cf48442140542a8bba4a23ff88 | |
parent | 82909a81eb01cfd76f38fec7cccff5a5c87b5232 (diff) | |
download | trackermap-server-69d0cc47d63d9052218a8d87cf436e46165e716c.tar.gz trackermap-server-69d0cc47d63d9052218a8d87cf436e46165e716c.tar.bz2 trackermap-server-69d0cc47d63d9052218a8d87cf436e46165e716c.zip |
For JT600c - added checksum verification and replying for alarms/positions messages
-rw-r--r-- | src/org/traccar/protocol/Jt600ProtocolDecoder.java | 75 | ||||
-rw-r--r-- | test/org/traccar/protocol/Jt600ProtocolDecoderTest.java | 14 |
2 files changed, 65 insertions, 24 deletions
diff --git a/src/org/traccar/protocol/Jt600ProtocolDecoder.java b/src/org/traccar/protocol/Jt600ProtocolDecoder.java index fd2448c0d..0c235a4b5 100644 --- a/src/org/traccar/protocol/Jt600ProtocolDecoder.java +++ b/src/org/traccar/protocol/Jt600ProtocolDecoder.java @@ -201,24 +201,25 @@ public class Jt600ProtocolDecoder extends BaseProtocolDecoder { private static final Pattern PATTERN_U01 = new PatternBuilder() .text("(") - .number("(d+),") // id - .number("Udd,") // type - .number("d+,").optional() // alarm - .number("(dd)(dd)(dd),") // date (ddmmyy) - .number("(dd)(dd)(dd),") // time - .expression("([TF]),") // validity - .number("(d+.d+),([NS]),") // latitude - .number("(d+.d+),([EW]),") // longitude - .number("(d+.?d*),") // speed - .number("(d+),") // course - .number("(d+),") // satellites - .number("(d+%),") // battery - .expression("([01]+),") // status - .number("(d+),") // cid - .number("(d+),") // lac - .number("(d+),") // gsm signal - .number("(d+),") // odometer - .number("(d+)") // index + .number("(d+),") // id + .number("(Udd),") // type + .number("d+,").optional() // alarm + .number("(dd)(dd)(dd),") // date (ddmmyy) + .number("(dd)(dd)(dd),") // time + .expression("([TF]),") // validity + .number("(d+.d+),([NS]),") // latitude + .number("(d+.d+),([EW]),") // longitude + .number("(d+.?d*),") // speed + .number("(d+),") // course + .number("(d+),") // satellites + .number("(d+%),") // battery + .expression("([01]+),") // status + .number("(d+),") // cid + .number("(d+),") // lac + .number("(d+),") // gsm signal + .number("(d+),") // odometer + .number("(d+),") // serial number + .expression("([0-9A-F][0-9A-F])").optional() // checksum .any() .compile(); @@ -234,6 +235,8 @@ public class Jt600ProtocolDecoder extends BaseProtocolDecoder { return null; } + String messageType = parser.next(); + Position position = new Position(); position.setProtocol(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); @@ -256,11 +259,29 @@ public class Jt600ProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_CID, parser.nextInt()); position.set(Position.KEY_LAC, parser.nextInt()); position.set(Position.KEY_GSM, parser.nextInt()); - position.set(Position.KEY_ODOMETER, parser.nextLong() * 1000); + position.set(Position.KEY_ODOMETER, parser.nextLong() * 100); position.set(Position.KEY_INDEX, parser.nextInt()); - return position; + switch (messageType) { + case "U01": + case "U02": + case "U03": + String checkSum = parser.next(); + int calculatedCheckSum = checkSum(sentence.substring(1, sentence.length() - checkSum.length() - 1)); + if (Integer.parseInt(checkSum, 16) == calculatedCheckSum) { + sendResponse(channel, "(S39)"); + return position; + } else { + return null; + } + case "U06": + sendResponse(channel, "(S20)"); + return position; + default: + return null; + } } + @Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { @@ -282,4 +303,18 @@ public class Jt600ProtocolDecoder extends BaseProtocolDecoder { return null; } + private byte checkSum(String sentence) { + byte[] bytes = sentence.getBytes(StandardCharsets.US_ASCII); + byte sum = 0; + for (byte b : bytes) { + sum ^= b; + } + return sum; + } + + 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..6899418d9 100644 --- a/test/org/traccar/protocol/Jt600ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/Jt600ProtocolDecoderTest.java @@ -18,13 +18,13 @@ public class Jt600ProtocolDecoderTest extends ProtocolTest { "2475605035891613002328091601152806086750106533350c00000000000a000000000000e1ff4f97007f1607")); verifyPosition(decoder, buffer( - "(3301210003,U01,040812,185302,T,22.564025,N,113.242329,E,5.21,152,9,32%,00000000000011,10133,5173,22,100,1)")); + "(3301210003,U01,040812,185302,T,22.564025,N,113.242329,E,5.21,152,9,32%,00000000000011,10133,5173,22,100,1,18)")); verifyPosition(decoder, buffer( - "(3301210003,U02,040812,185302,T,22.564025,N,113.242329,E,5,152,9,32%,00000000000011,10133,5173,22,100,1)")); + "(3301210003,U02,040812,185302,T,22.564025,N,113.242329,E,5,152,9,32%,00000000000011,10133,5173,22,100,1,36)")); verifyPosition(decoder, buffer( - "(3301210003,U03,040812,185302,T,22.564025,N,113.242329,E,5,152,9,32%,00000000000011,10133,5173,22,100,1)")); + "(3301210003,U03,040812,185302,T,22.564025,N,113.242329,E,5,152,9,32%,00000000000011,10133,5173,22,100,1,37)")); verifyNothing(decoder, buffer( "(3301210003,U04)")); @@ -68,12 +68,18 @@ public class Jt600ProtocolDecoderTest extends ProtocolTest { "(3120820029,W01,02553.3555,E,2438.0997,S,A,171012,053339,0,8,20,6,31,5,20,20)")); verifyPosition(decoder, buffer( - "(3330104377,U01,010100,010228,F,00.000000,N,000.000000,E,0,0,0,0%,00001000000000,741,14,22,0,206)")); + "(3330104377,U01,010100,010228,F,00.000000,N,000.000000,E,0,0,0,0%,00001000000000,741,14,22,0,206,1E)")); verifyNothing(decoder, buffer( "(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)")); } |