totalDistance wrong calculation

makos8 years ago

Hi,

Sometimes totalDistance attribute is calculated wrong.
Please take a look at attributes of three sequential positions:
20308 fixtime: 2016-12-16 23:36:35 {"priority":0,"sat":9,"event":0,"ignition":true,"io22":1,"battery":9360,
"power":28124,"io24":78,"distance":128,"ip":"","totalDistance":6244514.41}

20309 fixtime: 2016-12-16 23:37:00 {"priority":0,"sat":10,"event":0,"ignition":true,"io22":1,"battery":9360,
"power":28071,"io24":70,"distance":508,"ip":"","totalDistance":6245022.41}

20310 fixtime: 2016-12-16 23:37:05 {"priority":0,"sat":11,"event":0,"ignition":true,"io22":1,"battery":9360,
"power":28139,"io24":70,"distance":98,"ip":"","totalDistance":6244612.41}

In position id 20309 totalDistance calculated correctly, but in position id 20310 totalDistance difference is negative -410 instead of 98.

I have done some logging and as you can see the problem arises sometimes:

2016-12-16 23:40:31  INFO: Position ID:20307 distance mismatch. TotalDist = 104 TrackerDist = 104
2016-12-16 23:40:31  INFO: Position ID:20308 distance mismatch. TotalDist = 128 TrackerDist = 128
2016-12-16 23:40:31  INFO: Position ID:20309 distance mismatch. TotalDist = 508 TrackerDist = 508
2016-12-16 23:40:31  INFO: Position ID:20310 distance mismatch. TotalDist = -410 TrackerDist = 98
2016-12-16 23:40:31  INFO: Position ID:20311 distance mismatch. TotalDist = -19 TrackerDist = 79
2016-12-16 23:40:31  INFO: Position ID:20312 distance mismatch. TotalDist = 119 TrackerDist = 100
2016-12-16 23:40:31  INFO: Position ID:20313 distance mismatch. TotalDist = 103 TrackerDist = 103
2016-12-16 23:40:31  INFO: Position ID:20314 distance mismatch. TotalDist = 104 TrackerDist = 104
2016-12-16 23:40:31  INFO: Position ID:20315 distance mismatch. TotalDist = 506 TrackerDist = 506

Looks like the positions are stored correctly according to fixtime.
If I have followed the logic correctly - "distance" value of current position is added to totalDistance of position returned by getLastPosition() which is last position from device table.
So could it be that last position in device table is stored incorrectly? Or this is not the cause?

Thank you.

Anton Tananaev8 years ago

That can happen if your device sends data out of order.

makos8 years ago

Does it means that device last position rely on time of position being parsed by protocol decoded instead of time reported by device (fixtime)?

Thank you.

Anton Tananaev8 years ago

It is not directly based on it, but it's based on the order of received positions.

abyss8 years ago

I want to add, that another reason is multi-threading.
If two packets comes in almost same time, they can be parsed in different threads in parallel.
Both threads will use same lastPosition for reference (distance calculation, events generating and so on).

makos8 years ago

My both test devices using teltonika protocol and none of them got the correct distance in reports.
However if I calculate the sum of distance attributes for the same set of positions - the results are correct.
What I am doing wrong?

I am not the software engineer, so my approach could be incorrect.
But maybe it will be better if lastPosition setter call will determine last device position according to fixtime?
I think it could be a method that executes a single query. Also it seems there is the device cache that should be updated as well.

Anton Tananaev8 years ago

Teltonika is one of the devices that can upload positions in wrong order or in batches. Traccar does not recalculate distance every time, it's calculated once when server receives data from device.