aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar
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 /src/org/traccar
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
Diffstat (limited to 'src/org/traccar')
-rw-r--r--src/org/traccar/helper/Checksum.java9
-rw-r--r--src/org/traccar/protocol/Jt600ProtocolDecoder.java39
2 files changed, 45 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));
+ }
+ }
}