diff options
author | Jan-Piet Mens <jpmens@gmail.com> | 2017-06-14 17:11:04 +0200 |
---|---|---|
committer | Jan-Piet Mens <jpmens@gmail.com> | 2017-06-14 22:58:09 +0200 |
commit | c432acb0fbc43433c2ada22de50bfdb86c1dcb03 (patch) | |
tree | 7eb911c92b22c8168ab7e7ec51d5df2b1b409a8a | |
parent | ce9f1791ff8d10434392ea5853ccacf0cb2548f1 (diff) | |
download | trackermap-server-c432acb0fbc43433c2ada22de50bfdb86c1dcb03.tar.gz trackermap-server-c432acb0fbc43433c2ada22de50bfdb86c1dcb03.tar.bz2 trackermap-server-c432acb0fbc43433c2ada22de50bfdb86c1dcb03.zip |
Solidify owntracks protocol decoder
-rw-r--r-- | src/org/traccar/protocol/OwnTracksProtocolDecoder.java | 33 | ||||
-rw-r--r-- | test/org/traccar/protocol/OwnTracksProtocolDecoderTest.java | 2 |
2 files changed, 31 insertions, 4 deletions
diff --git a/src/org/traccar/protocol/OwnTracksProtocolDecoder.java b/src/org/traccar/protocol/OwnTracksProtocolDecoder.java index f664d113b..4efed2a35 100644 --- a/src/org/traccar/protocol/OwnTracksProtocolDecoder.java +++ b/src/org/traccar/protocol/OwnTracksProtocolDecoder.java @@ -58,6 +58,13 @@ 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; + } + + long timestamp; + String deviceId = new String(); Position position = new Position(); position.setProtocol(getProtocolName()); position.setValid(true); @@ -80,16 +87,34 @@ public class OwnTracksProtocolDecoder extends BaseProtocolDecoder { if (root.containsKey("t")) { position.set("t", root.getString("t")); } + if (root.containsKey("p")) { + position.set("pb", root.getInt("p")); + } if (root.containsKey("batt")) { position.set(Position.KEY_BATTERY, root.getInt("batt")); } - if (root.containsKey("topic")) { - position.set("topic", root.getString("topic")); + + if (root.containsKey("tst")) { + timestamp = root.getJsonNumber("tst").longValue(); + if (timestamp < Integer.MAX_VALUE) { + timestamp *= 1000; + } + } else { + timestamp = new Date().getTime(); } + position.setTime(new Date(timestamp)); - position.setTime(new Date(root.getJsonNumber("tst").longValue() * 1000)); + if (root.containsKey("tid")) { + deviceId = root.getString("tid"); + } + if (root.containsKey("topic")) { + deviceId = root.getString("topic"); + if (root.containsKey("tid")) { + position.set("tid", root.getString("tid")); + } + } - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, root.getString("tid")); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, deviceId); if (deviceSession == null) { sendResponse(channel, HttpResponseStatus.BAD_REQUEST); return null; diff --git a/test/org/traccar/protocol/OwnTracksProtocolDecoderTest.java b/test/org/traccar/protocol/OwnTracksProtocolDecoderTest.java index 75d9af041..57e5d7eca 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(text("{\"lat\":48.85,\"lon\":2.295,\"_type\":\"location\",\"tid\":\"JJ\"}")))); } } |