aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-06-18 11:08:32 -0700
committerAnton Tananaev <anton@traccar.org>2022-06-18 11:08:32 -0700
commitddbe4d7de6ae7590e2b927066312597efa129393 (patch)
tree777882e24ce5488b4cfc8a4d6f11119abcbeb7a3 /src/main/java/org
parent182656b6dc1fb5d167bb752c16ecf633add001a8 (diff)
downloadtrackermap-server-ddbe4d7de6ae7590e2b927066312597efa129393.tar.gz
trackermap-server-ddbe4d7de6ae7590e2b927066312597efa129393.tar.bz2
trackermap-server-ddbe4d7de6ae7590e2b927066312597efa129393.zip
Remove positions from manager
Diffstat (limited to 'src/main/java/org')
-rw-r--r--src/main/java/org/traccar/BaseProtocolDecoder.java2
-rw-r--r--src/main/java/org/traccar/Context.java4
-rw-r--r--src/main/java/org/traccar/MainEventHandler.java28
-rw-r--r--src/main/java/org/traccar/MainModule.java4
-rw-r--r--src/main/java/org/traccar/api/AsyncSocket.java14
-rw-r--r--src/main/java/org/traccar/api/AsyncSocketServlet.java7
-rw-r--r--src/main/java/org/traccar/api/resource/PositionResource.java14
-rw-r--r--src/main/java/org/traccar/database/DataManager.java24
-rw-r--r--src/main/java/org/traccar/database/DeviceManager.java93
-rw-r--r--src/main/java/org/traccar/database/IdentityManager.java5
-rw-r--r--src/main/java/org/traccar/handler/DistanceHandler.java10
-rw-r--r--src/main/java/org/traccar/handler/EngineHoursHandler.java10
-rw-r--r--src/main/java/org/traccar/handler/GeocoderHandler.java11
-rw-r--r--src/main/java/org/traccar/handler/events/AlertEventHandler.java10
-rw-r--r--src/main/java/org/traccar/handler/events/BehaviorEventHandler.java10
-rw-r--r--src/main/java/org/traccar/handler/events/DriverEventHandler.java18
-rw-r--r--src/main/java/org/traccar/handler/events/IgnitionEventHandler.java15
-rw-r--r--src/main/java/org/traccar/handler/events/MaintenanceEventHandler.java3
-rw-r--r--src/main/java/org/traccar/handler/events/MotionEventHandler.java12
-rw-r--r--src/main/java/org/traccar/helper/model/PositionUtil.java17
-rw-r--r--src/main/java/org/traccar/reports/common/ReportUtils.java2
21 files changed, 115 insertions, 198 deletions
diff --git a/src/main/java/org/traccar/BaseProtocolDecoder.java b/src/main/java/org/traccar/BaseProtocolDecoder.java
index 076b52e96..f7e726f02 100644
--- a/src/main/java/org/traccar/BaseProtocolDecoder.java
+++ b/src/main/java/org/traccar/BaseProtocolDecoder.java
@@ -156,7 +156,7 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder {
if (position.getDeviceId() != 0) {
position.setOutdated(true);
- Position last = identityManager.getLastPosition(position.getDeviceId());
+ Position last = cacheManager.getPosition(position.getDeviceId());
if (last != null) {
position.setFixTime(last.getFixTime());
position.setValid(last.getValid());
diff --git a/src/main/java/org/traccar/Context.java b/src/main/java/org/traccar/Context.java
index 854af97c0..c549b20c2 100644
--- a/src/main/java/org/traccar/Context.java
+++ b/src/main/java/org/traccar/Context.java
@@ -24,7 +24,6 @@ import org.traccar.database.PermissionsManager;
import org.traccar.helper.Log;
import org.traccar.model.BaseModel;
import org.traccar.model.Device;
-import org.traccar.session.ConnectionManager;
import org.traccar.storage.Storage;
public final class Context {
@@ -69,8 +68,7 @@ public final class Context {
deviceManager = new DeviceManager(
config,
- Main.getInjector().getInstance(DataManager.class),
- Main.getInjector().getInstance(ConnectionManager.class));
+ Main.getInjector().getInstance(DataManager.class));
identityManager = deviceManager;
diff --git a/src/main/java/org/traccar/MainEventHandler.java b/src/main/java/org/traccar/MainEventHandler.java
index 7fff2e13f..e1aee3cc8 100644
--- a/src/main/java/org/traccar/MainEventHandler.java
+++ b/src/main/java/org/traccar/MainEventHandler.java
@@ -28,10 +28,16 @@ import org.traccar.config.Keys;
import org.traccar.database.StatisticsManager;
import org.traccar.helper.DateUtil;
import org.traccar.helper.NetworkUtil;
+import org.traccar.helper.model.PositionUtil;
+import org.traccar.model.Device;
import org.traccar.model.Position;
import org.traccar.session.ConnectionManager;
import org.traccar.session.cache.CacheManager;
+import org.traccar.storage.Storage;
import org.traccar.storage.StorageException;
+import org.traccar.storage.query.Columns;
+import org.traccar.storage.query.Condition;
+import org.traccar.storage.query.Request;
import javax.inject.Inject;
import java.util.Arrays;
@@ -46,10 +52,15 @@ public class MainEventHandler extends ChannelInboundHandlerAdapter {
private final Set<String> connectionlessProtocols = new HashSet<>();
private final Set<String> logAttributes = new LinkedHashSet<>();
+ private final CacheManager cacheManager;
+ private final Storage storage;
private final ConnectionManager connectionManager;
@Inject
- public MainEventHandler(Config config, ConnectionManager connectionManager) {
+ public MainEventHandler(
+ Config config, CacheManager cacheManager, Storage storage, ConnectionManager connectionManager) {
+ this.cacheManager = cacheManager;
+ this.storage = storage;
this.connectionManager = connectionManager;
String connectionlessProtocolList = config.getString(Keys.STATUS_IGNORE_OFFLINE);
if (connectionlessProtocolList != null) {
@@ -64,8 +75,19 @@ public class MainEventHandler extends ChannelInboundHandlerAdapter {
Position position = (Position) msg;
try {
- Context.getDeviceManager().updateLatestPosition(position);
- Main.getInjector().getInstance(CacheManager.class).updatePosition(position);
+ if (PositionUtil.isLatest(cacheManager, position)) {
+ Device device = new Device();
+ device.setId(position.getDeviceId());
+ device.setPositionId(position.getId());
+ storage.updateObject(device, new Request(
+ new Columns.Include("positionId"),
+ new Condition.Equals("id", "id")));
+
+ cacheManager.updatePosition(position);
+ cacheManager.getObject(Device.class, position.getDeviceId()).setPositionId(position.getId());
+
+ connectionManager.updatePosition(position);
+ }
} catch (StorageException error) {
LOGGER.warn("Failed to update device", error);
}
diff --git a/src/main/java/org/traccar/MainModule.java b/src/main/java/org/traccar/MainModule.java
index a9337a3a2..a7e531808 100644
--- a/src/main/java/org/traccar/MainModule.java
+++ b/src/main/java/org/traccar/MainModule.java
@@ -252,9 +252,9 @@ public class MainModule extends AbstractModule {
@Provides
public static GeocoderHandler provideGeocoderHandler(
- Config config, @Nullable Geocoder geocoder, IdentityManager identityManager) {
+ Config config, @Nullable Geocoder geocoder, CacheManager cacheManager) {
if (geocoder != null) {
- return new GeocoderHandler(config, geocoder, identityManager);
+ return new GeocoderHandler(config, geocoder, cacheManager);
}
return null;
}
diff --git a/src/main/java/org/traccar/api/AsyncSocket.java b/src/main/java/org/traccar/api/AsyncSocket.java
index 3239d36c4..40aa68e88 100644
--- a/src/main/java/org/traccar/api/AsyncSocket.java
+++ b/src/main/java/org/traccar/api/AsyncSocket.java
@@ -21,11 +21,13 @@ import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.WebSocketAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.traccar.Context;
+import org.traccar.helper.model.PositionUtil;
import org.traccar.session.ConnectionManager;
import org.traccar.model.Device;
import org.traccar.model.Event;
import org.traccar.model.Position;
+import org.traccar.storage.Storage;
+import org.traccar.storage.StorageException;
import java.util.Collection;
import java.util.Collections;
@@ -42,11 +44,13 @@ public class AsyncSocket extends WebSocketAdapter implements ConnectionManager.U
private final ObjectMapper objectMapper;
private final ConnectionManager connectionManager;
+ private final Storage storage;
private final long userId;
- public AsyncSocket(ObjectMapper objectMapper, ConnectionManager connectionManager, long userId) {
+ public AsyncSocket(ObjectMapper objectMapper, ConnectionManager connectionManager, Storage storage, long userId) {
this.objectMapper = objectMapper;
this.connectionManager = connectionManager;
+ this.storage = storage;
this.userId = userId;
}
@@ -55,7 +59,11 @@ public class AsyncSocket extends WebSocketAdapter implements ConnectionManager.U
super.onWebSocketConnect(session);
Map<String, Collection<?>> data = new HashMap<>();
- data.put(KEY_POSITIONS, Context.getDeviceManager().getInitialState(userId));
+ try {
+ data.put(KEY_POSITIONS, PositionUtil.getLatestPositions(storage, userId));
+ } catch (StorageException e) {
+ throw new RuntimeException(e);
+ }
sendData(data);
connectionManager.addListener(userId, this);
diff --git a/src/main/java/org/traccar/api/AsyncSocketServlet.java b/src/main/java/org/traccar/api/AsyncSocketServlet.java
index 7c532179b..7d9fdf0ed 100644
--- a/src/main/java/org/traccar/api/AsyncSocketServlet.java
+++ b/src/main/java/org/traccar/api/AsyncSocketServlet.java
@@ -22,6 +22,7 @@ import org.traccar.Context;
import org.traccar.api.resource.SessionResource;
import org.traccar.config.Keys;
import org.traccar.session.ConnectionManager;
+import org.traccar.storage.Storage;
import javax.inject.Inject;
import javax.inject.Singleton;
@@ -33,11 +34,13 @@ public class AsyncSocketServlet extends JettyWebSocketServlet {
private final ObjectMapper objectMapper;
private final ConnectionManager connectionManager;
+ private final Storage storage;
@Inject
- public AsyncSocketServlet(ObjectMapper objectMapper, ConnectionManager connectionManager) {
+ public AsyncSocketServlet(ObjectMapper objectMapper, ConnectionManager connectionManager, Storage storage) {
this.objectMapper = objectMapper;
this.connectionManager = connectionManager;
+ this.storage = storage;
}
@Override
@@ -46,7 +49,7 @@ public class AsyncSocketServlet extends JettyWebSocketServlet {
factory.setCreator((req, resp) -> {
if (req.getSession() != null) {
long userId = (Long) ((HttpSession) req.getSession()).getAttribute(SessionResource.USER_ID_KEY);
- return new AsyncSocket(objectMapper, connectionManager, userId);
+ return new AsyncSocket(objectMapper, connectionManager, storage, userId);
} else {
return null;
}
diff --git a/src/main/java/org/traccar/api/resource/PositionResource.java b/src/main/java/org/traccar/api/resource/PositionResource.java
index 099d97632..cac64feb1 100644
--- a/src/main/java/org/traccar/api/resource/PositionResource.java
+++ b/src/main/java/org/traccar/api/resource/PositionResource.java
@@ -17,10 +17,8 @@ package org.traccar.api.resource;
import org.traccar.api.BaseResource;
import org.traccar.helper.model.PositionUtil;
-import org.traccar.model.BaseModel;
import org.traccar.model.Device;
import org.traccar.model.Position;
-import org.traccar.model.User;
import org.traccar.model.UserRestrictions;
import org.traccar.storage.StorageException;
import org.traccar.storage.query.Columns;
@@ -37,7 +35,6 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
-import java.util.stream.Collectors;
@Path("positions")
@Produces(MediaType.APPLICATION_JSON)
@@ -68,16 +65,7 @@ public class PositionResource extends BaseResource {
new Columns.All(), new Condition.LatestPositions(deviceId)));
}
} else {
- var devices = storage.getObjects(Device.class, new Request(
- new Columns.Include("id"),
- new Condition.Permission(User.class, getUserId(), Device.class)));
- var deviceIds = devices.stream().map(BaseModel::getId).collect(Collectors.toUnmodifiableSet());
-
- var positions = storage.getObjects(Position.class, new Request(
- new Columns.All(), new Condition.LatestPositions()));
- return positions.stream()
- .filter(position -> deviceIds.contains(position.getDeviceId()))
- .collect(Collectors.toList());
+ return PositionUtil.getLatestPositions(storage, getUserId());
}
}
diff --git a/src/main/java/org/traccar/database/DataManager.java b/src/main/java/org/traccar/database/DataManager.java
index aa600e375..f80f429e1 100644
--- a/src/main/java/org/traccar/database/DataManager.java
+++ b/src/main/java/org/traccar/database/DataManager.java
@@ -24,20 +24,17 @@ import org.traccar.storage.Storage;
import org.traccar.storage.StorageException;
import org.traccar.storage.query.Columns;
import org.traccar.storage.query.Condition;
-import org.traccar.storage.query.Limit;
-import org.traccar.storage.query.Order;
import org.traccar.storage.query.Request;
import javax.inject.Inject;
import java.util.Collection;
-import java.util.Date;
public class DataManager {
private final Storage storage;
@Inject
- public DataManager(Storage storage) throws Exception {
+ public DataManager(Storage storage) {
this.storage = storage;
}
@@ -47,25 +44,6 @@ public class DataManager {
new Condition.Equals("id", "id")));
}
- public Position getPrecedingPosition(long deviceId, Date date) throws StorageException {
- return storage.getObject(Position.class, new Request(
- new Columns.All(),
- new Condition.And(
- new Condition.Equals("deviceId", "deviceId", deviceId),
- new Condition.Compare("fixTime", "<=", "time", date)),
- new Order(true, "fixTime"),
- new Limit(1)));
- }
-
- public void updateLatestPosition(Position position) throws StorageException {
- Device device = new Device();
- device.setId(position.getDeviceId());
- device.setPositionId(position.getId());
- storage.updateObject(device, new Request(
- new Columns.Include("positionId"),
- new Condition.Equals("id", "id")));
- }
-
public Collection<Position> getLatestPositions() throws StorageException {
return storage.getObjects(Position.class, new Request(
new Columns.All(),
diff --git a/src/main/java/org/traccar/database/DeviceManager.java b/src/main/java/org/traccar/database/DeviceManager.java
index 841915f1c..4516255c1 100644
--- a/src/main/java/org/traccar/database/DeviceManager.java
+++ b/src/main/java/org/traccar/database/DeviceManager.java
@@ -15,21 +15,13 @@
*/
package org.traccar.database;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.traccar.Context;
import org.traccar.config.Config;
import org.traccar.config.Keys;
import org.traccar.model.Device;
-import org.traccar.model.Position;
-import org.traccar.session.ConnectionManager;
import org.traccar.session.DeviceState;
import org.traccar.storage.StorageException;
import java.util.Collection;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@@ -37,23 +29,15 @@ import java.util.concurrent.atomic.AtomicLong;
public class DeviceManager extends BaseObjectManager<Device> implements IdentityManager {
- private static final Logger LOGGER = LoggerFactory.getLogger(DeviceManager.class);
-
- private final ConnectionManager connectionManager;
private final long dataRefreshDelay;
private final AtomicLong devicesLastUpdate = new AtomicLong();
- private final Map<Long, Position> positions = new ConcurrentHashMap<>();
-
private final Map<Long, DeviceState> deviceStates = new ConcurrentHashMap<>();
- public DeviceManager(
- Config config, DataManager dataManager, ConnectionManager connectionManager) {
+ public DeviceManager(Config config, DataManager dataManager) {
super(dataManager, Device.class);
- this.connectionManager = connectionManager;
dataRefreshDelay = config.getLong(Keys.DATABASE_REFRESH_DELAY) * 1000;
- refreshLastPositions();
}
public void updateDeviceCache(boolean force) {
@@ -78,25 +62,6 @@ public class DeviceManager extends BaseObjectManager<Device> implements Identity
return getItems(getAllItems());
}
- public Set<Long> getAllUserItems(long userId) {
- return Context.getPermissionsManager().getDevicePermissions(userId);
- }
-
- public Set<Long> getUserItems(long userId) {
- if (Context.getPermissionsManager() != null) {
- Set<Long> result = new HashSet<>();
- for (long deviceId : Context.getPermissionsManager().getDevicePermissions(userId)) {
- Device device = getById(deviceId);
- if (device != null && !device.getDisabled()) {
- result.add(deviceId);
- }
- }
- return result;
- } else {
- return new HashSet<>();
- }
- }
-
@Override
protected void updateCachedItem(Device device) {
Device cachedDevice = getById(device.getId());
@@ -117,7 +82,6 @@ public class DeviceManager extends BaseObjectManager<Device> implements Identity
if (cachedDevice != null) {
super.removeCachedItem(deviceId);
}
- positions.remove(deviceId);
}
public void updateDeviceStatus(Device device) throws StorageException {
@@ -128,61 +92,6 @@ public class DeviceManager extends BaseObjectManager<Device> implements Identity
}
}
- private void refreshLastPositions() {
- if (getDataManager() != null) {
- try {
- for (Position position : getDataManager().getLatestPositions()) {
- positions.put(position.getDeviceId(), position);
- }
- } catch (StorageException error) {
- LOGGER.warn("Load latest positions error", error);
- }
- }
- }
-
- public boolean isLatestPosition(Position position) {
- Position lastPosition = getLastPosition(position.getDeviceId());
- return lastPosition == null || position.getFixTime().compareTo(lastPosition.getFixTime()) >= 0;
- }
-
- public void updateLatestPosition(Position position) throws StorageException {
-
- if (isLatestPosition(position)) {
-
- getDataManager().updateLatestPosition(position);
-
- Device device = getById(position.getDeviceId());
- if (device != null) {
- device.setPositionId(position.getId());
- }
-
- positions.put(position.getDeviceId(), position);
-
- connectionManager.updatePosition(position);
- }
- }
-
- @Override
- public Position getLastPosition(long deviceId) {
- return positions.get(deviceId);
- }
-
- public Collection<Position> getInitialState(long userId) {
-
- List<Position> result = new LinkedList<>();
-
- if (Context.getPermissionsManager() != null) {
- for (long deviceId : Context.getPermissionsManager().getUserAdmin(userId)
- ? getAllUserItems(userId) : getUserItems(userId)) {
- if (positions.containsKey(deviceId)) {
- result.add(positions.get(deviceId));
- }
- }
- }
-
- return result;
- }
-
public DeviceState getDeviceState(long deviceId) {
DeviceState deviceState = deviceStates.get(deviceId);
if (deviceState == null) {
diff --git a/src/main/java/org/traccar/database/IdentityManager.java b/src/main/java/org/traccar/database/IdentityManager.java
index b42d4c292..1e0eb00c5 100644
--- a/src/main/java/org/traccar/database/IdentityManager.java
+++ b/src/main/java/org/traccar/database/IdentityManager.java
@@ -16,14 +16,9 @@
package org.traccar.database;
import org.traccar.model.Device;
-import org.traccar.model.Position;
public interface IdentityManager {
Device getById(long id);
- Position getLastPosition(long deviceId);
-
- boolean isLatestPosition(Position position);
-
}
diff --git a/src/main/java/org/traccar/handler/DistanceHandler.java b/src/main/java/org/traccar/handler/DistanceHandler.java
index 08c8c068d..fb82b5d8e 100644
--- a/src/main/java/org/traccar/handler/DistanceHandler.java
+++ b/src/main/java/org/traccar/handler/DistanceHandler.java
@@ -20,9 +20,9 @@ import io.netty.channel.ChannelHandler;
import org.traccar.BaseDataHandler;
import org.traccar.config.Config;
import org.traccar.config.Keys;
-import org.traccar.database.IdentityManager;
import org.traccar.helper.DistanceCalculator;
import org.traccar.model.Position;
+import org.traccar.session.cache.CacheManager;
import javax.inject.Inject;
import java.math.BigDecimal;
@@ -31,15 +31,15 @@ import java.math.RoundingMode;
@ChannelHandler.Sharable
public class DistanceHandler extends BaseDataHandler {
- private final IdentityManager identityManager;
+ private final CacheManager cacheManager;
private final boolean filter;
private final int coordinatesMinError;
private final int coordinatesMaxError;
@Inject
- public DistanceHandler(Config config, IdentityManager identityManager) {
- this.identityManager = identityManager;
+ public DistanceHandler(Config config, CacheManager cacheManager) {
+ this.cacheManager = cacheManager;
this.filter = config.getBoolean(Keys.COORDINATES_FILTER);
this.coordinatesMinError = config.getInteger(Keys.COORDINATES_MIN_ERROR);
this.coordinatesMaxError = config.getInteger(Keys.COORDINATES_MAX_ERROR);
@@ -54,7 +54,7 @@ public class DistanceHandler extends BaseDataHandler {
}
double totalDistance = 0.0;
- Position last = identityManager != null ? identityManager.getLastPosition(position.getDeviceId()) : null;
+ Position last = cacheManager.getPosition(position.getDeviceId());
if (last != null) {
totalDistance = last.getDouble(Position.KEY_TOTAL_DISTANCE);
if (!position.getAttributes().containsKey(Position.KEY_DISTANCE)) {
diff --git a/src/main/java/org/traccar/handler/EngineHoursHandler.java b/src/main/java/org/traccar/handler/EngineHoursHandler.java
index be2a46ade..bfffdcb0c 100644
--- a/src/main/java/org/traccar/handler/EngineHoursHandler.java
+++ b/src/main/java/org/traccar/handler/EngineHoursHandler.java
@@ -18,25 +18,25 @@ package org.traccar.handler;
import io.netty.channel.ChannelHandler;
import org.traccar.BaseDataHandler;
-import org.traccar.database.IdentityManager;
import org.traccar.model.Position;
+import org.traccar.session.cache.CacheManager;
import javax.inject.Inject;
@ChannelHandler.Sharable
public class EngineHoursHandler extends BaseDataHandler {
- private final IdentityManager identityManager;
+ private final CacheManager cacheManager;
@Inject
- public EngineHoursHandler(IdentityManager identityManager) {
- this.identityManager = identityManager;
+ public EngineHoursHandler(CacheManager cacheManager) {
+ this.cacheManager = cacheManager;
}
@Override
protected Position handlePosition(Position position) {
if (!position.getAttributes().containsKey(Position.KEY_HOURS)) {
- Position last = identityManager.getLastPosition(position.getDeviceId());
+ Position last = cacheManager.getPosition(position.getDeviceId());
if (last != null) {
long hours = last.getLong(Position.KEY_HOURS);
if (last.getBoolean(Position.KEY_IGNITION) && position.getBoolean(Position.KEY_IGNITION)) {
diff --git a/src/main/java/org/traccar/handler/GeocoderHandler.java b/src/main/java/org/traccar/handler/GeocoderHandler.java
index 075bdf815..0248fca05 100644
--- a/src/main/java/org/traccar/handler/GeocoderHandler.java
+++ b/src/main/java/org/traccar/handler/GeocoderHandler.java
@@ -22,9 +22,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.traccar.config.Config;
import org.traccar.config.Keys;
-import org.traccar.database.IdentityManager;
import org.traccar.geocoder.Geocoder;
import org.traccar.model.Position;
+import org.traccar.session.cache.CacheManager;
@ChannelHandler.Sharable
public class GeocoderHandler extends ChannelInboundHandlerAdapter {
@@ -32,15 +32,14 @@ public class GeocoderHandler extends ChannelInboundHandlerAdapter {
private static final Logger LOGGER = LoggerFactory.getLogger(GeocoderHandler.class);
private final Geocoder geocoder;
- private final IdentityManager identityManager;
+ private final CacheManager cacheManager;
private final boolean ignorePositions;
private final boolean processInvalidPositions;
private final int geocoderReuseDistance;
- public GeocoderHandler(
- Config config, Geocoder geocoder, IdentityManager identityManager) {
+ public GeocoderHandler(Config config, Geocoder geocoder, CacheManager cacheManager) {
this.geocoder = geocoder;
- this.identityManager = identityManager;
+ this.cacheManager = cacheManager;
ignorePositions = config.getBoolean(Keys.GEOCODER_IGNORE_POSITIONS);
processInvalidPositions = config.getBoolean(Keys.GEOCODER_PROCESS_INVALID_POSITIONS);
geocoderReuseDistance = config.getInteger(Keys.GEOCODER_REUSE_DISTANCE, 0);
@@ -52,7 +51,7 @@ public class GeocoderHandler extends ChannelInboundHandlerAdapter {
final Position position = (Position) message;
if (processInvalidPositions || position.getValid()) {
if (geocoderReuseDistance != 0) {
- Position lastPosition = identityManager.getLastPosition(position.getDeviceId());
+ Position lastPosition = cacheManager.getPosition(position.getDeviceId());
if (lastPosition != null && lastPosition.getAddress() != null
&& position.getDouble(Position.KEY_DISTANCE) <= geocoderReuseDistance) {
position.setAddress(lastPosition.getAddress());
diff --git a/src/main/java/org/traccar/handler/events/AlertEventHandler.java b/src/main/java/org/traccar/handler/events/AlertEventHandler.java
index 6e7b0b16e..75626ca6c 100644
--- a/src/main/java/org/traccar/handler/events/AlertEventHandler.java
+++ b/src/main/java/org/traccar/handler/events/AlertEventHandler.java
@@ -21,21 +21,21 @@ import java.util.Map;
import io.netty.channel.ChannelHandler;
import org.traccar.config.Config;
import org.traccar.config.Keys;
-import org.traccar.database.IdentityManager;
import org.traccar.model.Event;
import org.traccar.model.Position;
+import org.traccar.session.cache.CacheManager;
import javax.inject.Inject;
@ChannelHandler.Sharable
public class AlertEventHandler extends BaseEventHandler {
- private final IdentityManager identityManager;
+ private final CacheManager cacheManager;
private final boolean ignoreDuplicateAlerts;
@Inject
- public AlertEventHandler(Config config, IdentityManager identityManager) {
- this.identityManager = identityManager;
+ public AlertEventHandler(Config config, CacheManager cacheManager) {
+ this.cacheManager = cacheManager;
ignoreDuplicateAlerts = config.getBoolean(Keys.EVENT_IGNORE_DUPLICATE_ALERTS);
}
@@ -45,7 +45,7 @@ public class AlertEventHandler extends BaseEventHandler {
if (alarm != null) {
boolean ignoreAlert = false;
if (ignoreDuplicateAlerts) {
- Position lastPosition = identityManager.getLastPosition(position.getDeviceId());
+ Position lastPosition = cacheManager.getPosition(position.getDeviceId());
if (lastPosition != null && alarm.equals(lastPosition.getAttributes().get(Position.KEY_ALARM))) {
ignoreAlert = true;
}
diff --git a/src/main/java/org/traccar/handler/events/BehaviorEventHandler.java b/src/main/java/org/traccar/handler/events/BehaviorEventHandler.java
index bbf749cdc..3c2fa6a97 100644
--- a/src/main/java/org/traccar/handler/events/BehaviorEventHandler.java
+++ b/src/main/java/org/traccar/handler/events/BehaviorEventHandler.java
@@ -18,10 +18,10 @@ package org.traccar.handler.events;
import io.netty.channel.ChannelHandler;
import org.traccar.config.Config;
import org.traccar.config.Keys;
-import org.traccar.database.IdentityManager;
import org.traccar.helper.UnitsConverter;
import org.traccar.model.Event;
import org.traccar.model.Position;
+import org.traccar.session.cache.CacheManager;
import javax.inject.Inject;
import java.util.Collections;
@@ -33,19 +33,19 @@ public class BehaviorEventHandler extends BaseEventHandler {
private final double accelerationThreshold;
private final double brakingThreshold;
- private final IdentityManager identityManager;
+ private final CacheManager cacheManager;
@Inject
- public BehaviorEventHandler(Config config, IdentityManager identityManager) {
+ public BehaviorEventHandler(Config config, CacheManager cacheManager) {
accelerationThreshold = config.getDouble(Keys.EVENT_BEHAVIOR_ACCELERATION_THRESHOLD);
brakingThreshold = config.getDouble(Keys.EVENT_BEHAVIOR_BRAKING_THRESHOLD);
- this.identityManager = identityManager;
+ this.cacheManager = cacheManager;
}
@Override
protected Map<Event, Position> analyzePosition(Position position) {
- Position lastPosition = identityManager.getLastPosition(position.getDeviceId());
+ Position lastPosition = cacheManager.getPosition(position.getDeviceId());
if (lastPosition != null && position.getFixTime().equals(lastPosition.getFixTime())) {
double acceleration = UnitsConverter.mpsFromKnots(position.getSpeed() - lastPosition.getSpeed()) * 1000
/ (position.getFixTime().getTime() - lastPosition.getFixTime().getTime());
diff --git a/src/main/java/org/traccar/handler/events/DriverEventHandler.java b/src/main/java/org/traccar/handler/events/DriverEventHandler.java
index 510ac3465..1ad66ba64 100644
--- a/src/main/java/org/traccar/handler/events/DriverEventHandler.java
+++ b/src/main/java/org/traccar/handler/events/DriverEventHandler.java
@@ -16,35 +16,35 @@
*/
package org.traccar.handler.events;
-import java.util.Collections;
-import java.util.Map;
-
import io.netty.channel.ChannelHandler;
-import org.traccar.database.IdentityManager;
+import org.traccar.helper.model.PositionUtil;
import org.traccar.model.Event;
import org.traccar.model.Position;
+import org.traccar.session.cache.CacheManager;
import javax.inject.Inject;
+import java.util.Collections;
+import java.util.Map;
@ChannelHandler.Sharable
public class DriverEventHandler extends BaseEventHandler {
- private final IdentityManager identityManager;
+ private final CacheManager cacheManager;
@Inject
- public DriverEventHandler(IdentityManager identityManager) {
- this.identityManager = identityManager;
+ public DriverEventHandler(CacheManager cacheManager) {
+ this.cacheManager = cacheManager;
}
@Override
protected Map<Event, Position> analyzePosition(Position position) {
- if (!identityManager.isLatestPosition(position)) {
+ if (!PositionUtil.isLatest(cacheManager, position)) {
return null;
}
String driverUniqueId = position.getString(Position.KEY_DRIVER_UNIQUE_ID);
if (driverUniqueId != null) {
String oldDriverUniqueId = null;
- Position lastPosition = identityManager.getLastPosition(position.getDeviceId());
+ Position lastPosition = cacheManager.getPosition(position.getDeviceId());
if (lastPosition != null) {
oldDriverUniqueId = lastPosition.getString(Position.KEY_DRIVER_UNIQUE_ID);
}
diff --git a/src/main/java/org/traccar/handler/events/IgnitionEventHandler.java b/src/main/java/org/traccar/handler/events/IgnitionEventHandler.java
index 9887c9db6..6e411539c 100644
--- a/src/main/java/org/traccar/handler/events/IgnitionEventHandler.java
+++ b/src/main/java/org/traccar/handler/events/IgnitionEventHandler.java
@@ -20,27 +20,28 @@ import java.util.Collections;
import java.util.Map;
import io.netty.channel.ChannelHandler;
-import org.traccar.database.IdentityManager;
+import org.traccar.helper.model.PositionUtil;
import org.traccar.model.Device;
import org.traccar.model.Event;
import org.traccar.model.Position;
+import org.traccar.session.cache.CacheManager;
import javax.inject.Inject;
@ChannelHandler.Sharable
public class IgnitionEventHandler extends BaseEventHandler {
- private final IdentityManager identityManager;
+ private final CacheManager cacheManager;
@Inject
- public IgnitionEventHandler(IdentityManager identityManager) {
- this.identityManager = identityManager;
+ public IgnitionEventHandler(CacheManager cacheManager) {
+ this.cacheManager = cacheManager;
}
@Override
protected Map<Event, Position> analyzePosition(Position position) {
- Device device = identityManager.getById(position.getDeviceId());
- if (device == null || !identityManager.isLatestPosition(position)) {
+ Device device = cacheManager.getObject(Device.class, position.getDeviceId());
+ if (device == null || !PositionUtil.isLatest(cacheManager, position)) {
return null;
}
@@ -49,7 +50,7 @@ public class IgnitionEventHandler extends BaseEventHandler {
if (position.getAttributes().containsKey(Position.KEY_IGNITION)) {
boolean ignition = position.getBoolean(Position.KEY_IGNITION);
- Position lastPosition = identityManager.getLastPosition(position.getDeviceId());
+ Position lastPosition = cacheManager.getPosition(position.getDeviceId());
if (lastPosition != null && lastPosition.getAttributes().containsKey(Position.KEY_IGNITION)) {
boolean oldIgnition = lastPosition.getBoolean(Position.KEY_IGNITION);
diff --git a/src/main/java/org/traccar/handler/events/MaintenanceEventHandler.java b/src/main/java/org/traccar/handler/events/MaintenanceEventHandler.java
index f85aab043..be3e9bf8d 100644
--- a/src/main/java/org/traccar/handler/events/MaintenanceEventHandler.java
+++ b/src/main/java/org/traccar/handler/events/MaintenanceEventHandler.java
@@ -20,7 +20,6 @@ import java.util.HashMap;
import java.util.Map;
import io.netty.channel.ChannelHandler;
-import org.traccar.Context;
import org.traccar.model.Event;
import org.traccar.model.Maintenance;
import org.traccar.model.Position;
@@ -40,7 +39,7 @@ public class MaintenanceEventHandler extends BaseEventHandler {
@Override
protected Map<Event, Position> analyzePosition(Position position) {
- Position lastPosition = Context.getIdentityManager().getLastPosition(position.getDeviceId());
+ Position lastPosition = cacheManager.getPosition(position.getDeviceId());
if (lastPosition == null || position.getFixTime().compareTo(lastPosition.getFixTime()) < 0) {
return null;
}
diff --git a/src/main/java/org/traccar/handler/events/MotionEventHandler.java b/src/main/java/org/traccar/handler/events/MotionEventHandler.java
index 2c381e530..724e9bf15 100644
--- a/src/main/java/org/traccar/handler/events/MotionEventHandler.java
+++ b/src/main/java/org/traccar/handler/events/MotionEventHandler.java
@@ -18,13 +18,13 @@ package org.traccar.handler.events;
import io.netty.channel.ChannelHandler;
import org.traccar.database.DeviceManager;
-import org.traccar.database.IdentityManager;
import org.traccar.helper.model.PositionUtil;
import org.traccar.model.Device;
import org.traccar.model.Event;
import org.traccar.model.Position;
import org.traccar.reports.common.TripsConfig;
import org.traccar.session.DeviceState;
+import org.traccar.session.cache.CacheManager;
import javax.inject.Inject;
import java.util.Collections;
@@ -33,13 +33,13 @@ import java.util.Map;
@ChannelHandler.Sharable
public class MotionEventHandler extends BaseEventHandler {
- private final IdentityManager identityManager;
+ private final CacheManager cacheManager;
private final DeviceManager deviceManager;
private final TripsConfig tripsConfig;
@Inject
- public MotionEventHandler(IdentityManager identityManager, DeviceManager deviceManager, TripsConfig tripsConfig) {
- this.identityManager = identityManager;
+ public MotionEventHandler(CacheManager cacheManager, DeviceManager deviceManager, TripsConfig tripsConfig) {
+ this.cacheManager = cacheManager;
this.deviceManager = deviceManager;
this.tripsConfig = tripsConfig;
}
@@ -113,11 +113,11 @@ public class MotionEventHandler extends BaseEventHandler {
protected Map<Event, Position> analyzePosition(Position position) {
long deviceId = position.getDeviceId();
- Device device = identityManager.getById(deviceId);
+ Device device = cacheManager.getObject(Device.class, deviceId);
if (device == null) {
return null;
}
- if (!identityManager.isLatestPosition(position)
+ if (!PositionUtil.isLatest(cacheManager, position)
|| !tripsConfig.getProcessInvalidPositions() && !position.getValid()) {
return null;
}
diff --git a/src/main/java/org/traccar/helper/model/PositionUtil.java b/src/main/java/org/traccar/helper/model/PositionUtil.java
index 566e31bc5..31f828947 100644
--- a/src/main/java/org/traccar/helper/model/PositionUtil.java
+++ b/src/main/java/org/traccar/helper/model/PositionUtil.java
@@ -15,7 +15,10 @@
*/
package org.traccar.helper.model;
+import org.traccar.model.BaseModel;
+import org.traccar.model.Device;
import org.traccar.model.Position;
+import org.traccar.model.User;
import org.traccar.session.cache.CacheManager;
import org.traccar.storage.Storage;
import org.traccar.storage.StorageException;
@@ -26,6 +29,7 @@ import org.traccar.storage.query.Request;
import java.util.Date;
import java.util.List;
+import java.util.stream.Collectors;
public final class PositionUtil {
@@ -60,4 +64,17 @@ public final class PositionUtil {
new Order("fixTime")));
}
+ public static List<Position> getLatestPositions(Storage storage, long userId) throws StorageException {
+ var devices = storage.getObjects(Device.class, new Request(
+ new Columns.Include("id"),
+ new Condition.Permission(User.class, userId, Device.class)));
+ var deviceIds = devices.stream().map(BaseModel::getId).collect(Collectors.toUnmodifiableSet());
+
+ var positions = storage.getObjects(Position.class, new Request(
+ new Columns.All(), new Condition.LatestPositions()));
+ return positions.stream()
+ .filter(position -> deviceIds.contains(position.getDeviceId()))
+ .collect(Collectors.toList());
+ }
+
}
diff --git a/src/main/java/org/traccar/reports/common/ReportUtils.java b/src/main/java/org/traccar/reports/common/ReportUtils.java
index 84866a67b..bb37bfa9c 100644
--- a/src/main/java/org/traccar/reports/common/ReportUtils.java
+++ b/src/main/java/org/traccar/reports/common/ReportUtils.java
@@ -346,7 +346,7 @@ public class ReportUtils {
ArrayList<Position> positions = new ArrayList<>(positionCollection);
if (!positions.isEmpty()) {
boolean trips = reportClass.equals(TripReportItem.class);
- MotionEventHandler motionHandler = new MotionEventHandler(identityManager, deviceManager, tripsConfig);
+ MotionEventHandler motionHandler = new MotionEventHandler(null, deviceManager, tripsConfig);
DeviceState deviceState = new DeviceState();
deviceState.setMotionState(isMoving(positions, 0, tripsConfig));
int startEventIndex = trips == deviceState.getMotionState() ? 0 : -1;