From 7e4ba0f57de50ddd5150f6553b51af040f2a420e Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sun, 5 Jul 2015 12:10:33 +1200 Subject: Use connection manager for filtering --- src/org/traccar/FilterHandler.java | 20 ++++++++------------ src/org/traccar/database/ConnectionManager.java | 5 ++++- test/org/traccar/FilterHandlerTest.java | 14 +++----------- 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/src/org/traccar/FilterHandler.java b/src/org/traccar/FilterHandler.java index 08621dce2..69f9a55b1 100644 --- a/src/org/traccar/FilterHandler.java +++ b/src/org/traccar/FilterHandler.java @@ -15,17 +15,14 @@ */ package org.traccar; -import java.util.HashMap; import java.util.Iterator; import java.util.List; -import java.util.Map; import java.util.Properties; import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.handler.codec.oneone.OneToOneDecoder; import org.traccar.helper.DistanceCalculator; import org.traccar.helper.Log; -import org.traccar.model.Event; import org.traccar.model.Position; public class FilterHandler extends OneToOneDecoder { @@ -36,8 +33,6 @@ public class FilterHandler extends OneToOneDecoder { private int filterDistance; private long filterLimit; - private final Map lastPositions = new HashMap(); - public FilterHandler( boolean filterInvalid, boolean filterZero, @@ -71,6 +66,10 @@ public class FilterHandler extends OneToOneDecoder { if (value != null) filterLimit = Long.valueOf(value) * 1000; } + private Position getLastPosition(long deviceId) { + return Context.getConnectionManager().getLastPosition(deviceId); + } + private boolean filterInvalid(Position position) { return filterInvalid && !position.getValid(); } @@ -83,7 +82,7 @@ public class FilterHandler extends OneToOneDecoder { private boolean filterDuplicate(Position position) { if (filterDuplicate) { - Position last = lastPositions.get(position.getDeviceId()); + Position last = getLastPosition(position.getDeviceId()); if (last != null) { return position.getFixTime().equals(last.getFixTime()); } else { @@ -96,7 +95,7 @@ public class FilterHandler extends OneToOneDecoder { private boolean filterDistance(Position position) { if (filterDistance != 0) { - Position last = lastPositions.get(position.getDeviceId()); + Position last = getLastPosition(position.getDeviceId()); if (last != null) { double distance = DistanceCalculator.distance( position.getLatitude(), position.getLongitude(), @@ -112,7 +111,7 @@ public class FilterHandler extends OneToOneDecoder { private boolean filterLimit(Position position) { if (filterLimit != 0) { - Position last = lastPositions.get(position.getDeviceId()); + Position last = getLastPosition(position.getDeviceId()); if (last != null) { return (position.getFixTime().getTime() - last.getFixTime().getTime()) > filterLimit; } else { @@ -135,10 +134,7 @@ public class FilterHandler extends OneToOneDecoder { result = false; } - if (!result) { - lastPositions.put(p.getDeviceId(), p); - } else { - StringBuilder s = new StringBuilder(); + if (result) { Log.info("Position filtered from " + p.getDeviceId()); } diff --git a/src/org/traccar/database/ConnectionManager.java b/src/org/traccar/database/ConnectionManager.java index 7ef190fcf..eb22379da 100644 --- a/src/org/traccar/database/ConnectionManager.java +++ b/src/org/traccar/database/ConnectionManager.java @@ -24,7 +24,6 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; - import org.jboss.netty.channel.Channel; import org.traccar.Protocol; import org.traccar.helper.Log; @@ -65,6 +64,10 @@ public class ConnectionManager { } } + public Position getLastPosition(long deviceId) { + return positions.get(deviceId); + } + public synchronized Collection getInitialState(Collection devices) { List result = new LinkedList(); diff --git a/test/org/traccar/FilterHandlerTest.java b/test/org/traccar/FilterHandlerTest.java index 053681b71..ee5ddd396 100644 --- a/test/org/traccar/FilterHandlerTest.java +++ b/test/org/traccar/FilterHandlerTest.java @@ -1,20 +1,12 @@ package org.traccar; -import org.jboss.netty.buffer.ChannelBuffers; +import java.util.Date; import org.junit.After; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import org.junit.Before; import org.junit.Test; -import org.traccar.helper.ChannelBufferTools; -import org.traccar.helper.TestIdentityManager; -import org.traccar.model.Event; import org.traccar.model.Position; -import org.traccar.protocol.AtrackProtocolDecoder; - -import java.util.Date; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.traccar.helper.DecoderVerifier.verify; public class FilterHandlerTest { -- cgit v1.2.3