Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solidify OwnTracks protocol decoder #3249

Merged
merged 5 commits into from Jun 15, 2017
Merged

Conversation

jpmens
Copy link
Contributor

@jpmens jpmens commented Jun 14, 2017

You kindly merged, the new OwnTracks Protocol Decoder, and this pull request attempts to address some things I omitted the first time around. In particular:

  1. Ensures most attributes in OwnTracks JSON are optional: only lat, lon, and _type are mandatory.
  2. Checks for payloads of "_type" : "location" and uses only those. (OwnTracks has other payload types, e.g. "transition" which probably shouldn't be used in Traccar.)
  3. Addresses an originally too simplistic method of creating a Traccar deviceId using our tracking ID (tid). Starting now, if the payload contains our topic (e.g. owntracks/jpmens/iphone5s) that will be used in preference to avoid collisions.
  4. Adds another unit test.

I hope this is OK.

We're updating our documentation to include Traccar, and that could be the landing for "device type OwnTracks" on your https://www.traccar.org/devices/ page, for example. Just a thought.

@tananaev
Copy link
Member

Please fix merge issues. I did some cleanup.

@jpmens
Copy link
Contributor Author

jpmens commented Jun 14, 2017

I think you added the line

position.setTime(new Date(root.getJsonNumber("tst").longValue() * 1000));

which I've removed again because that would cause the code to fail in the (unlikely) event that the JSON doesn't contain tst; that's the reason for the block above it.

@@ -17,6 +17,8 @@ public void testDecode() throws Exception {
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\"}"))));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove text here.

}

long timestamp;
String deviceId = new String();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there a new String() here. That's just wrong. Also, the name should be uniqueId. We use deviceId for internal device identifier. Also, why are these variables at the top?


if (root.containsKey("tst")) {
timestamp = root.getJsonNumber("tst").longValue();
if (timestamp < Integer.MAX_VALUE) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed this code. I believe it's unnecessary.

}
if (root.containsKey("topic")) {
deviceId = root.getString("topic");
if (root.containsKey("tid")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already do this check. You can move it to the code above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really. If topic exists, we want tid as an attribute, in addition to uniqueId set to topic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sure there is a better way to write this code without checking twice.

@jpmens
Copy link
Contributor Author

jpmens commented Jun 14, 2017

I have, I hope, addressed all your comments.

Copy link
Member

@tananaev tananaev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just two more small comments. Everything else looks good.


position.setTime(new Date(root.getJsonNumber("tst").longValue() * 1000));

DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, root.getString("tid"));
Boolean haveTopic = root.containsKey("topic");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't use boxed primitives. Also, there is no need for variables here at all.

DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, root.getString("tid"));
Boolean haveTopic = root.containsKey("topic");
Boolean haveTid = root.containsKey("tid");
String uniqueId = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to assign null. here.

DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, root.getString("tid"));
Boolean haveTopic = root.containsKey("topic");
Boolean haveTid = root.containsKey("tid");
String uniqueId = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to assign null. here.

@@ -80,16 +85,29 @@ protected Object decode(
if (root.containsKey("t")) {
position.set("t", root.getString("t"));
}
if (root.containsKey("p")) {
position.set("pb", root.getInt("p"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, I just noticed this. What is "pb"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pb was atmospheric pressure; it is reported by our iOS app on iPhone >= 6. I've removed it for the time being; it's not so important.

@tananaev
Copy link
Member

tananaev commented Jun 15, 2017

It seems like you haven't read my comments carefully enough. Why do you need variables for something that you are using once?

@jpmens
Copy link
Contributor Author

jpmens commented Jun 15, 2017

Apologies: I thought the code looked clearer.

@tananaev tananaev merged commit f4b7d59 into traccar:master Jun 15, 2017
@tananaev
Copy link
Member

Merged, thanks.

@jpmens
Copy link
Contributor Author

jpmens commented Jun 15, 2017

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants