aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2020-11-08 16:12:48 -0800
committerAnton Tananaev <anton.tananaev@gmail.com>2020-11-08 16:12:48 -0800
commit9f8505ebf73bba5e329fc5467fe00e43f989fd0d (patch)
tree049702f0964d3708d35821889253a64d5f4a37f0 /src
parent42af749b78de2d83a3b8ccba6974c8a8c990f77e (diff)
downloadtrackermap-server-9f8505ebf73bba5e329fc5467fe00e43f989fd0d.tar.gz
trackermap-server-9f8505ebf73bba5e329fc5467fe00e43f989fd0d.tar.bz2
trackermap-server-9f8505ebf73bba5e329fc5467fe00e43f989fd0d.zip
Handle Cantrack heartbeat
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/traccar/protocol/H02ProtocolDecoder.java37
-rw-r--r--src/test/java/org/traccar/protocol/H02ProtocolDecoderTest.java4
2 files changed, 38 insertions, 3 deletions
diff --git a/src/main/java/org/traccar/protocol/H02ProtocolDecoder.java b/src/main/java/org/traccar/protocol/H02ProtocolDecoder.java
index 320fe991d..a5dc1860a 100644
--- a/src/main/java/org/traccar/protocol/H02ProtocolDecoder.java
+++ b/src/main/java/org/traccar/protocol/H02ProtocolDecoder.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2012 - 2019 Anton Tananaev (anton@traccar.org)
+ * Copyright 2012 - 2020 Anton Tananaev (anton@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -286,6 +286,14 @@ public class H02ProtocolDecoder extends BaseProtocolDecoder {
.any()
.compile();
+ private static final Pattern PATTERN_HTBT = new PatternBuilder()
+ .text("*HQ,")
+ .number("(d{15}),") // imei
+ .text("HTBT,")
+ .number("(d+)") // battery
+ .any()
+ .compile();
+
private void sendResponse(Channel channel, SocketAddress remoteAddress, String id, String type) {
if (channel != null && id != null) {
String response;
@@ -551,6 +559,28 @@ public class H02ProtocolDecoder extends BaseProtocolDecoder {
return position;
}
+ private Position decodeHeartbeat(String sentence, Channel channel, SocketAddress remoteAddress) {
+
+ Parser parser = new Parser(PATTERN_HTBT, sentence);
+ if (!parser.matches()) {
+ return null;
+ }
+
+ DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
+ if (deviceSession == null) {
+ return null;
+ }
+
+ Position position = new Position(getProtocolName());
+ position.setDeviceId(deviceSession.getDeviceId());
+
+ getLastLocation(position, null);
+
+ position.set(Position.KEY_BATTERY_LEVEL, parser.nextInt());
+
+ return position;
+ }
+
@Override
protected Object decode(
Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
@@ -572,9 +602,10 @@ public class H02ProtocolDecoder extends BaseProtocolDecoder {
case "V0":
case "HTBT":
if (channel != null) {
- channel.writeAndFlush(new NetworkMessage(sentence, remoteAddress));
+ String response = sentence.substring(0, typeEnd) + "#";
+ channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
}
- return null;
+ return decodeHeartbeat(sentence, channel, remoteAddress);
case "NBR":
return decodeLbs(sentence, channel, remoteAddress);
case "LINK":
diff --git a/src/test/java/org/traccar/protocol/H02ProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/H02ProtocolDecoderTest.java
index fe4988b5a..07411b55f 100644
--- a/src/test/java/org/traccar/protocol/H02ProtocolDecoderTest.java
+++ b/src/test/java/org/traccar/protocol/H02ProtocolDecoderTest.java
@@ -11,6 +11,10 @@ public class H02ProtocolDecoderTest extends ProtocolTest {
H02ProtocolDecoder decoder = new H02ProtocolDecoder(null);
+ verifyAttribute(decoder, buffer(
+ "*HQ,135790246811220,HTBT,100#"),
+ Position.KEY_BATTERY_LEVEL, 100);
+
verifyPosition(decoder, buffer(
"*HQ,9180271064,V5,091233,V,2348.8912,N,09021.3302,E,000.00,000,051219,FFFFBBFF,470,01,21019,2033,2921283#"));