aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2016-10-02 20:38:03 +1300
committerGitHub <noreply@github.com>2016-10-02 20:38:03 +1300
commit8862d4f45712f6bee956c27380800d89105034e8 (patch)
tree869b17f6563ab3495f5cc092371a24f5d74b4acb
parent8e265f9c030496f5672460379bcb3fc2d0d78a3b (diff)
parentbb211eea5f62014c0700138e922cb1adcdc9d9dd (diff)
downloadtraccar-server-8862d4f45712f6bee956c27380800d89105034e8.tar.gz
traccar-server-8862d4f45712f6bee956c27380800d89105034e8.tar.bz2
traccar-server-8862d4f45712f6bee956c27380800d89105034e8.zip
Merge pull request #2387 from vitalidze/jt600c_checksum_replies
For JT600c - added checksum verification and replying for alarms/positions
-rw-r--r--src/org/traccar/helper/Checksum.java9
-rw-r--r--src/org/traccar/protocol/Jt600ProtocolDecoder.java39
-rw-r--r--test/org/traccar/protocol/Jt600ProtocolDecoderTest.java6
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)"));
}