From 9034cd07e95320a37a0f788ee6eaa546af5f4df1 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 1 Oct 2015 10:16:42 +1300 Subject: Implemenet future message filtering --- src/org/traccar/FilterHandler.java | 15 ++++++++++++++- test/org/traccar/FilterHandlerTest.java | 9 +++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/org/traccar/FilterHandler.java b/src/org/traccar/FilterHandler.java index c601016b4..e24de487f 100644 --- a/src/org/traccar/FilterHandler.java +++ b/src/org/traccar/FilterHandler.java @@ -19,18 +19,24 @@ import org.traccar.helper.DistanceCalculator; import org.traccar.helper.Log; import org.traccar.model.Position; +import java.util.Date; + public class FilterHandler extends BaseDataHandler { + private static final long FILTER_FUTURE_LIMIT = 5 * 60 * 1000; + private final boolean filterInvalid; private final boolean filterZero; private final boolean filterDuplicate; + private final boolean filterFuture; private final int filterDistance; private final long filterLimit; - + public FilterHandler( boolean filterInvalid, boolean filterZero, boolean filterDuplicate, + boolean filterFuture, int filterDistance, long filterLimit) { @@ -38,6 +44,7 @@ public class FilterHandler extends BaseDataHandler { this.filterZero = filterZero; this.filterDuplicate = filterDuplicate; this.filterDistance = filterDistance; + this.filterFuture = filterFuture; this.filterLimit = filterLimit; } @@ -47,6 +54,7 @@ public class FilterHandler extends BaseDataHandler { filterInvalid = config.getBoolean("filter.invalid"); filterZero = config.getBoolean("filter.zero"); filterDuplicate = config.getBoolean("filter.duplicate"); + filterFuture = config.getBoolean("filter.future"); filterDistance = config.getInteger("filter.distance"); filterLimit = config.getLong("filter.limit") * 1000; } @@ -80,6 +88,10 @@ public class FilterHandler extends BaseDataHandler { return false; } } + + private boolean filterFuture(Position position) { + return filterFuture && (position.getFixTime().getTime() > System.currentTimeMillis() + FILTER_FUTURE_LIMIT); + } private boolean filterDistance(Position position) { if (filterDistance != 0) { @@ -116,6 +128,7 @@ public class FilterHandler extends BaseDataHandler { filterInvalid(p) || filterZero(p) || filterDuplicate(p) || + filterFuture(p) || filterDistance(p); if (filterLimit(p)) { diff --git a/test/org/traccar/FilterHandlerTest.java b/test/org/traccar/FilterHandlerTest.java index ee5ddd396..f860c489f 100644 --- a/test/org/traccar/FilterHandlerTest.java +++ b/test/org/traccar/FilterHandlerTest.java @@ -15,8 +15,8 @@ public class FilterHandlerTest { @Before public void setUp() { - filtingHandler = new FilterHandler(true, true, true, 10, 10); - passingHandler = new FilterHandler(false, false, false, 0, 0); + filtingHandler = new FilterHandler(true, true, true, true, 10, 10); + passingHandler = new FilterHandler(false, false, false, false, 0, 0); } @After @@ -55,6 +55,11 @@ public class FilterHandlerTest { assertNotNull(filtingHandler.decode(null, null, position)); assertNotNull(passingHandler.decode(null, null, position)); + position = createPosition(0, new Date(Long.MAX_VALUE), true, 10, 10, 10, 10, 10); + + assertNull(filtingHandler.decode(null, null, position)); + assertNotNull(passingHandler.decode(null, null, position)); + position = createPosition(0, new Date(), false, 10, 10, 10, 10, 10); assertNull(filtingHandler.decode(null, null, position)); -- cgit v1.2.3