From c432acb0fbc43433c2ada22de50bfdb86c1dcb03 Mon Sep 17 00:00:00 2001 From: Jan-Piet Mens Date: Wed, 14 Jun 2017 17:11:04 +0200 Subject: Solidify owntracks protocol decoder --- .../traccar/protocol/OwnTracksProtocolDecoder.java | 33 +++++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) (limited to 'src/org') 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; -- cgit v1.2.3 From d01591f1c0de50b6aa399b0ece00d422897a48e5 Mon Sep 17 00:00:00 2001 From: Jan-Piet Mens Date: Wed, 14 Jun 2017 23:43:01 +0200 Subject: changes for review --- .../traccar/protocol/OwnTracksProtocolDecoder.java | 19 +++++-------------- .../protocol/OwnTracksProtocolDecoderTest.java | 2 +- 2 files changed, 6 insertions(+), 15 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/protocol/OwnTracksProtocolDecoder.java b/src/org/traccar/protocol/OwnTracksProtocolDecoder.java index 4efed2a35..4bff3c7ed 100644 --- a/src/org/traccar/protocol/OwnTracksProtocolDecoder.java +++ b/src/org/traccar/protocol/OwnTracksProtocolDecoder.java @@ -63,8 +63,6 @@ public class OwnTracksProtocolDecoder extends BaseProtocolDecoder { return null; } - long timestamp; - String deviceId = new String(); Position position = new Position(); position.setProtocol(getProtocolName()); position.setValid(true); @@ -94,27 +92,20 @@ public class OwnTracksProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_BATTERY, root.getInt("batt")); } - 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)); + String uniqueId = ""; if (root.containsKey("tid")) { - deviceId = root.getString("tid"); + uniqueId = root.getString("tid"); } if (root.containsKey("topic")) { - deviceId = root.getString("topic"); + uniqueId = root.getString("topic"); if (root.containsKey("tid")) { position.set("tid", root.getString("tid")); } } - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, deviceId); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, uniqueId); 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 57e5d7eca..89b994fbe 100644 --- a/test/org/traccar/protocol/OwnTracksProtocolDecoderTest.java +++ b/test/org/traccar/protocol/OwnTracksProtocolDecoderTest.java @@ -18,7 +18,7 @@ public class OwnTracksProtocolDecoderTest extends ProtocolTest { 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\"}")))); + buffer("{\"lat\":48.85,\"lon\":2.295,\"_type\":\"location\",\"tid\":\"JJ\",\"tst\":1497476456}"))); } } -- cgit v1.2.3 From b52688409736837d952f10865be754e23120f42c Mon Sep 17 00:00:00 2001 From: Jan-Piet Mens Date: Thu, 15 Jun 2017 00:43:47 +0200 Subject: more review --- src/org/traccar/protocol/OwnTracksProtocolDecoder.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/protocol/OwnTracksProtocolDecoder.java b/src/org/traccar/protocol/OwnTracksProtocolDecoder.java index 4bff3c7ed..f1dd2401a 100644 --- a/src/org/traccar/protocol/OwnTracksProtocolDecoder.java +++ b/src/org/traccar/protocol/OwnTracksProtocolDecoder.java @@ -94,15 +94,17 @@ public class OwnTracksProtocolDecoder extends BaseProtocolDecoder { position.setTime(new Date(root.getJsonNumber("tst").longValue() * 1000)); - String uniqueId = ""; - if (root.containsKey("tid")) { - uniqueId = root.getString("tid"); - } - if (root.containsKey("topic")) { + Boolean haveTopic = root.containsKey("topic"); + Boolean haveTid = root.containsKey("tid"); + String uniqueId = null; + + if (haveTopic) { uniqueId = root.getString("topic"); - if (root.containsKey("tid")) { + if (haveTid) { position.set("tid", root.getString("tid")); } + } else { + uniqueId = root.getString("tid"); } DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, uniqueId); @@ -115,5 +117,4 @@ public class OwnTracksProtocolDecoder extends BaseProtocolDecoder { sendResponse(channel, HttpResponseStatus.OK); return position; } - } -- cgit v1.2.3 From 8a5f318ed92e4bb6411c0883d1ce73dd41e9a7bc Mon Sep 17 00:00:00 2001 From: Jan-Piet Mens Date: Thu, 15 Jun 2017 08:05:19 +0200 Subject: remove null asignment and "pb", and use simplel booleans --- src/org/traccar/protocol/OwnTracksProtocolDecoder.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/protocol/OwnTracksProtocolDecoder.java b/src/org/traccar/protocol/OwnTracksProtocolDecoder.java index f1dd2401a..34a61da89 100644 --- a/src/org/traccar/protocol/OwnTracksProtocolDecoder.java +++ b/src/org/traccar/protocol/OwnTracksProtocolDecoder.java @@ -85,18 +85,15 @@ 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")); } position.setTime(new Date(root.getJsonNumber("tst").longValue() * 1000)); - Boolean haveTopic = root.containsKey("topic"); - Boolean haveTid = root.containsKey("tid"); - String uniqueId = null; + boolean haveTopic = root.containsKey("topic"); + boolean haveTid = root.containsKey("tid"); + String uniqueId; if (haveTopic) { uniqueId = root.getString("topic"); -- cgit v1.2.3 From bb9dac7fb639788cb210c27274a9481d37374eed Mon Sep 17 00:00:00 2001 From: Jan-Piet Mens Date: Thu, 15 Jun 2017 08:23:00 +0200 Subject: omit booleans --- src/org/traccar/protocol/OwnTracksProtocolDecoder.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/protocol/OwnTracksProtocolDecoder.java b/src/org/traccar/protocol/OwnTracksProtocolDecoder.java index 34a61da89..ea604f4ac 100644 --- a/src/org/traccar/protocol/OwnTracksProtocolDecoder.java +++ b/src/org/traccar/protocol/OwnTracksProtocolDecoder.java @@ -91,13 +91,11 @@ public class OwnTracksProtocolDecoder extends BaseProtocolDecoder { position.setTime(new Date(root.getJsonNumber("tst").longValue() * 1000)); - boolean haveTopic = root.containsKey("topic"); - boolean haveTid = root.containsKey("tid"); String uniqueId; - if (haveTopic) { + if (root.containsKey("topic")) { uniqueId = root.getString("topic"); - if (haveTid) { + if (root.containsKey("tid")) { position.set("tid", root.getString("tid")); } } else { -- cgit v1.2.3