aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/FilterHandler.java
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2014-09-03 00:09:42 +1200
committerAnton Tananaev <anton.tananaev@gmail.com>2014-09-03 00:09:42 +1200
commit66b62de750e75e1081ac8eeccca17f11b297d01a (patch)
tree080e3748138f31e5dd8ab9fba6a61993256acb27 /src/org/traccar/FilterHandler.java
parent1f56f881365fbe31736c26c3b5ba848c2fda4f64 (diff)
downloadtrackermap-server-66b62de750e75e1081ac8eeccca17f11b297d01a.tar.gz
trackermap-server-66b62de750e75e1081ac8eeccca17f11b297d01a.tar.bz2
trackermap-server-66b62de750e75e1081ac8eeccca17f11b297d01a.zip
Implement position filtering (fix #259)
Diffstat (limited to 'src/org/traccar/FilterHandler.java')
-rw-r--r--src/org/traccar/FilterHandler.java133
1 files changed, 133 insertions, 0 deletions
diff --git a/src/org/traccar/FilterHandler.java b/src/org/traccar/FilterHandler.java
new file mode 100644
index 000000000..18ae2ecae
--- /dev/null
+++ b/src/org/traccar/FilterHandler.java
@@ -0,0 +1,133 @@
+/*
+ * Copyright 2014 Anton Tananaev (anton.tananaev@gmail.com)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+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.Position;
+
+public class FilterHandler extends OneToOneDecoder {
+
+ private boolean filterInvalid;
+ private boolean filterZero;
+ private boolean filterDuplicate;
+ private int filterDistance;
+
+ private final Map<Long, Position> lastPositions = new HashMap<Long, Position>();
+
+ public FilterHandler(ServerManager serverManager) {
+ Properties properties = serverManager.getProperties();
+
+ String value = properties.getProperty("filter.invalid");
+ if (value != null) filterInvalid = Boolean.valueOf(value);
+
+ value = properties.getProperty("filter.zero");
+ if (value != null) filterZero = Boolean.valueOf(value);
+
+ value = properties.getProperty("filter.duplicate");
+ if (value != null) filterDuplicate = Boolean.valueOf(value);
+
+ value = properties.getProperty("filter.distance");
+ if (value != null) filterDistance = Integer.valueOf(value);
+ }
+
+ private boolean filterInvalid(Position position) {
+ return filterInvalid && !position.getValid();
+ }
+
+ private boolean filterZero(Position position) {
+ return filterZero &&
+ (position.getLatitude() == 0.0) &&
+ (position.getLongitude() == 0.0);
+ }
+
+ private boolean filterDuplicate(Position position) {
+ if (filterDuplicate) {
+ Position last = lastPositions.get(position.getDeviceId());
+ if (last != null) {
+ return position.getTime().equals(last.getTime());
+ } else {
+ return false;
+ }
+ } else {
+ return false;
+ }
+ }
+
+ private boolean filterDistance(Position position) {
+ if (filterDistance != 0) {
+ Position last = lastPositions.get(position.getDeviceId());
+ if (last != null) {
+ double distance = DistanceCalculator.distance(
+ position.getLatitude(), position.getLongitude(),
+ last.getLatitude(), last.getLongitude());
+ return distance < filterDistance;
+ } else {
+ return false;
+ }
+ } else {
+ return false;
+ }
+ }
+
+ private boolean filter(Position p) {
+
+ boolean result =
+ filterInvalid(p) ||
+ filterZero(p) ||
+ filterDuplicate(p) ||
+ filterDistance(p);
+
+ if (!result) {
+ lastPositions.put(p.getDeviceId(), p);
+ } else {
+ StringBuilder s = new StringBuilder();
+ Log.info("Position filtered from " + p.getDeviceId());
+ }
+
+ return result;
+ }
+
+ @Override
+ protected Object decode(
+ ChannelHandlerContext ctx, Channel channel, Object msg)
+ throws Exception {
+
+ if (msg instanceof Position) {
+ if (filter((Position) msg)) {
+ return null;
+ }
+ } else if (msg instanceof List) {
+ Iterator<Position> i = ((List<Position>) msg).iterator();
+ while (i.hasNext()) {
+ if (filter(i.next())) {
+ i.remove();
+ }
+ }
+ }
+
+ return msg;
+ }
+
+}