aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-06-16 18:48:08 -0700
committerAnton Tananaev <anton@traccar.org>2022-06-16 18:48:08 -0700
commit15c4b6b21a31c63bc0f7e624e26d032d531c459d (patch)
tree524ca1dd9aca6ef0ea8db2910a16a3e227e8c1fb /src/main/java/org
parentee9abd94265eb704b5f5dd2262f1a3460bbe2a8f (diff)
downloadtrackermap-server-15c4b6b21a31c63bc0f7e624e26d032d531c459d.tar.gz
trackermap-server-15c4b6b21a31c63bc0f7e624e26d032d531c459d.tar.bz2
trackermap-server-15c4b6b21a31c63bc0f7e624e26d032d531c459d.zip
Refactor geofence classes
Diffstat (limited to 'src/main/java/org')
-rw-r--r--src/main/java/org/traccar/MainModule.java1
-rw-r--r--src/main/java/org/traccar/geofence/GeofenceCircle.java4
-rw-r--r--src/main/java/org/traccar/geofence/GeofenceGeometry.java5
-rw-r--r--src/main/java/org/traccar/geofence/GeofencePolygon.java4
-rw-r--r--src/main/java/org/traccar/geofence/GeofencePolyline.java23
-rw-r--r--src/main/java/org/traccar/handler/events/GeofenceEventHandler.java7
-rw-r--r--src/main/java/org/traccar/helper/model/GeofenceUtil.java6
-rw-r--r--src/main/java/org/traccar/model/Geofence.java22
-rw-r--r--src/main/java/org/traccar/session/cache/CacheManager.java7
9 files changed, 43 insertions, 36 deletions
diff --git a/src/main/java/org/traccar/MainModule.java b/src/main/java/org/traccar/MainModule.java
index a29c2c910..a9337a3a2 100644
--- a/src/main/java/org/traccar/MainModule.java
+++ b/src/main/java/org/traccar/MainModule.java
@@ -75,7 +75,6 @@ import org.traccar.web.WebServer;
import javax.annotation.Nullable;
import javax.inject.Singleton;
-import javax.sql.DataSource;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.ext.ContextResolver;
diff --git a/src/main/java/org/traccar/geofence/GeofenceCircle.java b/src/main/java/org/traccar/geofence/GeofenceCircle.java
index 5d566f84e..59feb1730 100644
--- a/src/main/java/org/traccar/geofence/GeofenceCircle.java
+++ b/src/main/java/org/traccar/geofence/GeofenceCircle.java
@@ -18,7 +18,9 @@ package org.traccar.geofence;
import java.text.DecimalFormat;
import java.text.ParseException;
+import org.traccar.config.Config;
import org.traccar.helper.DistanceCalculator;
+import org.traccar.model.Geofence;
public class GeofenceCircle extends GeofenceGeometry {
@@ -44,7 +46,7 @@ public class GeofenceCircle extends GeofenceGeometry {
}
@Override
- public boolean containsPoint(double latitude, double longitude) {
+ public boolean containsPoint(Config config, Geofence geofence, double latitude, double longitude) {
return distanceFromCenter(latitude, longitude) <= radius;
}
diff --git a/src/main/java/org/traccar/geofence/GeofenceGeometry.java b/src/main/java/org/traccar/geofence/GeofenceGeometry.java
index 2c45c22af..fadabab1c 100644
--- a/src/main/java/org/traccar/geofence/GeofenceGeometry.java
+++ b/src/main/java/org/traccar/geofence/GeofenceGeometry.java
@@ -15,11 +15,14 @@
*/
package org.traccar.geofence;
+import org.traccar.config.Config;
+import org.traccar.model.Geofence;
+
import java.text.ParseException;
public abstract class GeofenceGeometry {
- public abstract boolean containsPoint(double latitude, double longitude);
+ public abstract boolean containsPoint(Config config, Geofence geofence, double latitude, double longitude);
public abstract double calculateArea();
diff --git a/src/main/java/org/traccar/geofence/GeofencePolygon.java b/src/main/java/org/traccar/geofence/GeofencePolygon.java
index cd2cbf16a..13f6658ef 100644
--- a/src/main/java/org/traccar/geofence/GeofencePolygon.java
+++ b/src/main/java/org/traccar/geofence/GeofencePolygon.java
@@ -19,6 +19,8 @@ import org.locationtech.spatial4j.context.SpatialContext;
import org.locationtech.spatial4j.context.jts.JtsSpatialContextFactory;
import org.locationtech.spatial4j.shape.ShapeFactory;
import org.locationtech.spatial4j.shape.jts.JtsShapeFactory;
+import org.traccar.config.Config;
+import org.traccar.model.Geofence;
import java.text.ParseException;
import java.util.ArrayList;
@@ -95,7 +97,7 @@ public class GeofencePolygon extends GeofenceGeometry {
}
@Override
- public boolean containsPoint(double latitude, double longitude) {
+ public boolean containsPoint(Config config, Geofence geofence, double latitude, double longitude) {
int polyCorners = coordinates.size();
int i;
diff --git a/src/main/java/org/traccar/geofence/GeofencePolyline.java b/src/main/java/org/traccar/geofence/GeofencePolyline.java
index 370bf99bb..d9c280ae4 100644
--- a/src/main/java/org/traccar/geofence/GeofencePolyline.java
+++ b/src/main/java/org/traccar/geofence/GeofencePolyline.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2016 - 2020 Anton Tananaev (anton@traccar.org)
+ * Copyright 2016 - 2022 Anton Tananaev (anton@traccar.org)
* Copyright 2016 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -19,23 +19,28 @@ package org.traccar.geofence;
import java.text.ParseException;
import java.util.ArrayList;
+import org.traccar.config.Config;
+import org.traccar.config.Keys;
import org.traccar.helper.DistanceCalculator;
+import org.traccar.model.Geofence;
public class GeofencePolyline extends GeofenceGeometry {
private ArrayList<Coordinate> coordinates;
- private double distance;
public GeofencePolyline() {
}
- public GeofencePolyline(String wkt, double distance) throws ParseException {
+ public GeofencePolyline(String wkt) throws ParseException {
fromWkt(wkt);
- this.distance = distance;
}
@Override
- public boolean containsPoint(double latitude, double longitude) {
+ public boolean containsPoint(Config config, Geofence geofence, double latitude, double longitude) {
+ double distance = geofence.getDouble("polylineDistance");
+ if (distance == 0) {
+ distance = config.getDouble(Keys.GEOFENCE_POLYLINE_DISTANCE);
+ }
for (int i = 1; i < coordinates.size(); i++) {
if (DistanceCalculator.distanceToLine(
latitude, longitude, coordinates.get(i - 1).getLat(), coordinates.get(i - 1).getLon(),
@@ -56,9 +61,9 @@ public class GeofencePolyline extends GeofenceGeometry {
StringBuilder buf = new StringBuilder();
buf.append("LINESTRING (");
for (Coordinate coordinate : coordinates) {
- buf.append(String.valueOf(coordinate.getLat()));
+ buf.append(coordinate.getLat());
buf.append(" ");
- buf.append(String.valueOf(coordinate.getLon()));
+ buf.append(coordinate.getLon());
buf.append(", ");
}
return buf.substring(0, buf.length() - 2) + ")";
@@ -105,8 +110,4 @@ public class GeofencePolyline extends GeofenceGeometry {
}
- public void setDistance(double distance) {
- this.distance = distance;
- }
-
}
diff --git a/src/main/java/org/traccar/handler/events/GeofenceEventHandler.java b/src/main/java/org/traccar/handler/events/GeofenceEventHandler.java
index 724f8f0d0..0a924cfc3 100644
--- a/src/main/java/org/traccar/handler/events/GeofenceEventHandler.java
+++ b/src/main/java/org/traccar/handler/events/GeofenceEventHandler.java
@@ -16,6 +16,7 @@
package org.traccar.handler.events;
import io.netty.channel.ChannelHandler;
+import org.traccar.config.Config;
import org.traccar.helper.model.GeofenceUtil;
import org.traccar.helper.model.PositionUtil;
import org.traccar.model.Calendar;
@@ -35,11 +36,13 @@ import java.util.Map;
@ChannelHandler.Sharable
public class GeofenceEventHandler extends BaseEventHandler {
+ private final Config config;
private final CacheManager cacheManager;
private final ConnectionManager connectionManager;
@Inject
- public GeofenceEventHandler(CacheManager cacheManager, ConnectionManager connectionManager) {
+ public GeofenceEventHandler(Config config, CacheManager cacheManager, ConnectionManager connectionManager) {
+ this.config = config;
this.cacheManager = cacheManager;
this.connectionManager = connectionManager;
}
@@ -54,7 +57,7 @@ public class GeofenceEventHandler extends BaseEventHandler {
return null;
}
- List<Long> currentGeofences = GeofenceUtil.getCurrentGeofences(cacheManager, position);
+ List<Long> currentGeofences = GeofenceUtil.getCurrentGeofences(config, cacheManager, position);
List<Long> oldGeofences = new ArrayList<>();
if (device.getGeofenceIds() != null) {
oldGeofences.addAll(device.getGeofenceIds());
diff --git a/src/main/java/org/traccar/helper/model/GeofenceUtil.java b/src/main/java/org/traccar/helper/model/GeofenceUtil.java
index f56bf4224..9f063a8b4 100644
--- a/src/main/java/org/traccar/helper/model/GeofenceUtil.java
+++ b/src/main/java/org/traccar/helper/model/GeofenceUtil.java
@@ -15,6 +15,7 @@
*/
package org.traccar.helper.model;
+import org.traccar.config.Config;
import org.traccar.model.Geofence;
import org.traccar.model.Position;
import org.traccar.session.cache.CacheManager;
@@ -27,10 +28,11 @@ public final class GeofenceUtil {
private GeofenceUtil() {
}
- public static List<Long> getCurrentGeofences(CacheManager cacheManager, Position position) {
+ public static List<Long> getCurrentGeofences(Config config, CacheManager cacheManager, Position position) {
List<Long> result = new ArrayList<>();
for (Geofence geofence : cacheManager.getDeviceObjects(position.getDeviceId(), Geofence.class)) {
- if (geofence.getGeometry().containsPoint(position.getLatitude(), position.getLongitude())) {
+ if (geofence.getGeometry().containsPoint(
+ config, geofence, position.getLatitude(), position.getLongitude())) {
result.add(geofence.getId());
}
}
diff --git a/src/main/java/org/traccar/model/Geofence.java b/src/main/java/org/traccar/model/Geofence.java
index 5b857580d..9259028fb 100644
--- a/src/main/java/org/traccar/model/Geofence.java
+++ b/src/main/java/org/traccar/model/Geofence.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2016 Anton Tananaev (anton@traccar.org)
+ * Copyright 2016 - 2022 Anton Tananaev (anton@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,26 +15,19 @@
*/
package org.traccar.model;
-import java.text.ParseException;
-
-import org.traccar.Context;
-import org.traccar.config.Keys;
-import org.traccar.storage.QueryIgnore;
+import com.fasterxml.jackson.annotation.JsonIgnore;
import org.traccar.geofence.GeofenceCircle;
import org.traccar.geofence.GeofenceGeometry;
import org.traccar.geofence.GeofencePolygon;
import org.traccar.geofence.GeofencePolyline;
-
-import com.fasterxml.jackson.annotation.JsonIgnore;
+import org.traccar.storage.QueryIgnore;
import org.traccar.storage.StorageName;
+import java.text.ParseException;
+
@StorageName("tc_geofences")
public class Geofence extends ScheduledModel {
- public static final String TYPE_GEOFENCE_CILCLE = "geofenceCircle";
- public static final String TYPE_GEOFENCE_POLYGON = "geofencePolygon";
- public static final String TYPE_GEOFENCE_POLYLINE = "geofencePolyline";
-
private String name;
public String getName() {
@@ -68,9 +61,7 @@ public class Geofence extends ScheduledModel {
} else if (area.startsWith("POLYGON")) {
geometry = new GeofencePolygon(area);
} else if (area.startsWith("LINESTRING")) {
- final double distance = getDouble("polylineDistance");
- geometry = new GeofencePolyline(area, distance > 0 ? distance
- : Context.getConfig().getDouble(Keys.GEOFENCE_POLYLINE_DISTANCE));
+ geometry = new GeofencePolyline(area);
} else {
throw new ParseException("Unknown geometry type", 0);
}
@@ -92,4 +83,5 @@ public class Geofence extends ScheduledModel {
area = geometry.toWkt();
this.geometry = geometry;
}
+
}
diff --git a/src/main/java/org/traccar/session/cache/CacheManager.java b/src/main/java/org/traccar/session/cache/CacheManager.java
index 18daeae15..14034f3d6 100644
--- a/src/main/java/org/traccar/session/cache/CacheManager.java
+++ b/src/main/java/org/traccar/session/cache/CacheManager.java
@@ -15,6 +15,7 @@
*/
package org.traccar.session.cache;
+import org.traccar.config.Config;
import org.traccar.helper.model.GeofenceUtil;
import org.traccar.model.Attribute;
import org.traccar.model.BaseModel;
@@ -53,6 +54,7 @@ public class CacheManager {
private static final Collection<Class<? extends BaseModel>> CLASSES = Arrays.asList(
Attribute.class, Driver.class, Geofence.class, Maintenance.class, Notification.class);
+ private final Config config;
private final Storage storage;
private final ReadWriteLock lock = new ReentrantReadWriteLock();
@@ -65,7 +67,8 @@ public class CacheManager {
private final Map<Long, List<User>> notificationUsers = new HashMap<>();
@Inject
- public CacheManager(Storage storage) throws StorageException {
+ public CacheManager(Config config, Storage storage) throws StorageException {
+ this.config = config;
this.storage = storage;
invalidateServer();
invalidateUsers();
@@ -315,7 +318,7 @@ public class CacheManager {
private void invalidateDeviceGeofences(Device device) {
Position position = getPosition(device.getId());
if (position != null) {
- device.setGeofenceIds(GeofenceUtil.getCurrentGeofences(this, position));
+ device.setGeofenceIds(GeofenceUtil.getCurrentGeofences(config, this, position));
}
}