aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2017-06-15 18:28:28 +1200
committerGitHub <noreply@github.com>2017-06-15 18:28:28 +1200
commitf4b7d595334a12bd612575e36c8e2ebf7253c8a4 (patch)
treef15ba4c96dcb9f8bdcb515f3bfb64c17dd3799ac
parentf06a4a99f0457cb385dd3465b5be592551a3530b (diff)
parentbb9dac7fb639788cb210c27274a9481d37374eed (diff)
downloadtrackermap-server-f4b7d595334a12bd612575e36c8e2ebf7253c8a4.tar.gz
trackermap-server-f4b7d595334a12bd612575e36c8e2ebf7253c8a4.tar.bz2
trackermap-server-f4b7d595334a12bd612575e36c8e2ebf7253c8a4.zip
Merge pull request #3249 from jpmens/owntracks2
Solidify OwnTracks protocol decoder
-rw-r--r--src/org/traccar/protocol/OwnTracksProtocolDecoder.java22
-rw-r--r--test/org/traccar/protocol/OwnTracksProtocolDecoderTest.java2
2 files changed, 19 insertions, 5 deletions
diff --git a/src/org/traccar/protocol/OwnTracksProtocolDecoder.java b/src/org/traccar/protocol/OwnTracksProtocolDecoder.java
index f664d113b..ea604f4ac 100644
--- a/src/org/traccar/protocol/OwnTracksProtocolDecoder.java
+++ b/src/org/traccar/protocol/OwnTracksProtocolDecoder.java
@@ -58,6 +58,11 @@ public class OwnTracksProtocolDecoder extends BaseProtocolDecoder {
JsonObject root = Json.createReader(
new StringReader(request.getContent().toString(StandardCharsets.US_ASCII))).readObject();
+ if (!root.containsKey("_type") || !root.getString("_type").equals("location")) {
+ sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
+ return null;
+ }
+
Position position = new Position();
position.setProtocol(getProtocolName());
position.setValid(true);
@@ -83,13 +88,21 @@ public class OwnTracksProtocolDecoder extends BaseProtocolDecoder {
if (root.containsKey("batt")) {
position.set(Position.KEY_BATTERY, root.getInt("batt"));
}
- if (root.containsKey("topic")) {
- position.set("topic", root.getString("topic"));
- }
position.setTime(new Date(root.getJsonNumber("tst").longValue() * 1000));
- DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, root.getString("tid"));
+ String uniqueId;
+
+ if (root.containsKey("topic")) {
+ uniqueId = root.getString("topic");
+ if (root.containsKey("tid")) {
+ position.set("tid", root.getString("tid"));
+ }
+ } else {
+ uniqueId = root.getString("tid");
+ }
+
+ DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, uniqueId);
if (deviceSession == null) {
sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
return null;
@@ -99,5 +112,4 @@ public class OwnTracksProtocolDecoder extends BaseProtocolDecoder {
sendResponse(channel, HttpResponseStatus.OK);
return position;
}
-
}
diff --git a/test/org/traccar/protocol/OwnTracksProtocolDecoderTest.java b/test/org/traccar/protocol/OwnTracksProtocolDecoderTest.java
index 75d9af041..89b994fbe 100644
--- a/test/org/traccar/protocol/OwnTracksProtocolDecoderTest.java
+++ b/test/org/traccar/protocol/OwnTracksProtocolDecoderTest.java
@@ -17,6 +17,8 @@ public class OwnTracksProtocolDecoderTest extends ProtocolTest {
verifyPosition(decoder, request(HttpMethod.POST, "/",
buffer("{\"cog\":271,\"lon\":2.29513,\"acc\":5,\"vel\":61,\"vac\":21,\"lat\":48.85833,\"tst\":1497349316,\"alt\":167,\"_type\":\"location\",\"tid\":\"JJ\",\"t\":\"u\",\"batt\":67}")));
+ verifyPosition(decoder, request(HttpMethod.POST, "/",
+ buffer("{\"lat\":48.85,\"lon\":2.295,\"_type\":\"location\",\"tid\":\"JJ\",\"tst\":1497476456}")));
}
}