aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/traccar/Context.java4
-rw-r--r--src/main/java/org/traccar/config/Keys.java8
-rw-r--r--src/main/java/org/traccar/database/DataManager.java7
-rw-r--r--src/main/java/org/traccar/database/DeviceManager.java41
-rw-r--r--src/main/java/org/traccar/handler/events/MotionEventHandler.java13
-rw-r--r--src/main/java/org/traccar/handler/events/OverspeedEventHandler.java12
-rw-r--r--src/main/java/org/traccar/reports/common/ReportUtils.java8
-rw-r--r--src/main/java/org/traccar/schedule/TaskDeviceInactivityCheck.java30
-rw-r--r--src/main/java/org/traccar/session/ConnectionManager.java17
9 files changed, 53 insertions, 87 deletions
diff --git a/src/main/java/org/traccar/Context.java b/src/main/java/org/traccar/Context.java
index 246ed9f02..c89d39f4c 100644
--- a/src/main/java/org/traccar/Context.java
+++ b/src/main/java/org/traccar/Context.java
@@ -51,9 +51,7 @@ public final class Context {
throw e;
}
- deviceManager = new DeviceManager(
- config,
- Main.getInjector().getInstance(DataManager.class));
+ deviceManager = new DeviceManager(Main.getInjector().getInstance(DataManager.class));
}
diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java
index 5909ae517..6eb61607e 100644
--- a/src/main/java/org/traccar/config/Keys.java
+++ b/src/main/java/org/traccar/config/Keys.java
@@ -477,14 +477,6 @@ public final class Keys {
List.of(KeyType.CONFIG));
/**
- * Minimum device refresh timeout in seconds. Default timeout is 5 minutes.
- */
- public static final ConfigKey<Long> DATABASE_REFRESH_DELAY = new LongConfigKey(
- "database.refreshDelay",
- List.of(KeyType.CONFIG),
- 300L);
-
- /**
* Store empty messages as positions. For example, heartbeats.
*/
public static final ConfigKey<Boolean> DATABASE_SAVE_EMPTY = new BooleanConfigKey(
diff --git a/src/main/java/org/traccar/database/DataManager.java b/src/main/java/org/traccar/database/DataManager.java
index bfc79eb77..398c63892 100644
--- a/src/main/java/org/traccar/database/DataManager.java
+++ b/src/main/java/org/traccar/database/DataManager.java
@@ -16,7 +16,6 @@
package org.traccar.database;
import org.traccar.model.BaseModel;
-import org.traccar.model.Device;
import org.traccar.model.Server;
import org.traccar.storage.Storage;
import org.traccar.storage.StorageException;
@@ -36,12 +35,6 @@ public class DataManager {
this.storage = storage;
}
- public void updateDeviceStatus(Device device) throws StorageException {
- storage.updateObject(device, new Request(
- new Columns.Include("lastUpdate"),
- new Condition.Equals("id", "id")));
- }
-
public Server getServer() throws StorageException {
return storage.getObject(Server.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 d6c442f10..7a472c2d4 100644
--- a/src/main/java/org/traccar/database/DeviceManager.java
+++ b/src/main/java/org/traccar/database/DeviceManager.java
@@ -15,34 +15,22 @@
*/
package org.traccar.database;
-import org.traccar.config.Config;
-import org.traccar.config.Keys;
import org.traccar.model.Device;
-import org.traccar.session.DeviceState;
-import org.traccar.storage.StorageException;
-import java.util.Collection;
-import java.util.Map;
import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
public class DeviceManager extends BaseObjectManager<Device> {
- private final long dataRefreshDelay;
-
private final AtomicLong devicesLastUpdate = new AtomicLong();
- private final Map<Long, DeviceState> deviceStates = new ConcurrentHashMap<>();
-
- public DeviceManager(Config config, DataManager dataManager) {
+ public DeviceManager(DataManager dataManager) {
super(dataManager, Device.class);
- dataRefreshDelay = config.getLong(Keys.DATABASE_REFRESH_DELAY) * 1000;
}
public void updateDeviceCache(boolean force) {
long lastUpdate = devicesLastUpdate.get();
- if ((force || System.currentTimeMillis() - lastUpdate > dataRefreshDelay)
+ if ((force || System.currentTimeMillis() - lastUpdate > 300000L)
&& devicesLastUpdate.compareAndSet(lastUpdate, System.currentTimeMillis())) {
refreshItems();
}
@@ -58,10 +46,6 @@ public class DeviceManager extends BaseObjectManager<Device> {
return result;
}
- public Collection<Device> getAllDevices() {
- return getItems(getAllItems());
- }
-
@Override
protected void updateCachedItem(Device device) {
Device cachedDevice = getById(device.getId());
@@ -84,25 +68,4 @@ public class DeviceManager extends BaseObjectManager<Device> {
}
}
- public void updateDeviceStatus(Device device) throws StorageException {
- getDataManager().updateDeviceStatus(device);
- Device cachedDevice = getById(device.getId());
- if (cachedDevice != null) {
- cachedDevice.setStatus(device.getStatus());
- }
- }
-
- public DeviceState getDeviceState(long deviceId) {
- DeviceState deviceState = deviceStates.get(deviceId);
- if (deviceState == null) {
- deviceState = new DeviceState();
- deviceStates.put(deviceId, deviceState);
- }
- return deviceState;
- }
-
- public void setDeviceState(long deviceId, DeviceState deviceState) {
- deviceStates.put(deviceId, deviceState);
- }
-
}
diff --git a/src/main/java/org/traccar/handler/events/MotionEventHandler.java b/src/main/java/org/traccar/handler/events/MotionEventHandler.java
index 724e9bf15..bc9d5f722 100644
--- a/src/main/java/org/traccar/handler/events/MotionEventHandler.java
+++ b/src/main/java/org/traccar/handler/events/MotionEventHandler.java
@@ -17,12 +17,12 @@
package org.traccar.handler.events;
import io.netty.channel.ChannelHandler;
-import org.traccar.database.DeviceManager;
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.ConnectionManager;
import org.traccar.session.DeviceState;
import org.traccar.session.cache.CacheManager;
@@ -34,13 +34,14 @@ import java.util.Map;
public class MotionEventHandler extends BaseEventHandler {
private final CacheManager cacheManager;
- private final DeviceManager deviceManager;
+ private final ConnectionManager connectionManager;
private final TripsConfig tripsConfig;
@Inject
- public MotionEventHandler(CacheManager cacheManager, DeviceManager deviceManager, TripsConfig tripsConfig) {
+ public MotionEventHandler(
+ CacheManager cacheManager, ConnectionManager connectionManager, TripsConfig tripsConfig) {
this.cacheManager = cacheManager;
- this.deviceManager = deviceManager;
+ this.connectionManager = connectionManager;
this.tripsConfig = tripsConfig;
}
@@ -123,14 +124,14 @@ public class MotionEventHandler extends BaseEventHandler {
}
Map<Event, Position> result = null;
- DeviceState deviceState = deviceManager.getDeviceState(deviceId);
+ DeviceState deviceState = connectionManager.getDeviceState(deviceId);
if (deviceState.getMotionState() == null) {
deviceState.setMotionState(position.getBoolean(Position.KEY_MOTION));
} else {
result = updateMotionState(deviceState, position);
}
- deviceManager.setDeviceState(deviceId, deviceState);
+ connectionManager.setDeviceState(deviceId, deviceState);
return result;
}
diff --git a/src/main/java/org/traccar/handler/events/OverspeedEventHandler.java b/src/main/java/org/traccar/handler/events/OverspeedEventHandler.java
index 6de56d11e..7f3675308 100644
--- a/src/main/java/org/traccar/handler/events/OverspeedEventHandler.java
+++ b/src/main/java/org/traccar/handler/events/OverspeedEventHandler.java
@@ -22,10 +22,10 @@ import java.util.Map;
import io.netty.channel.ChannelHandler;
import org.traccar.config.Config;
import org.traccar.config.Keys;
-import org.traccar.database.DeviceManager;
import org.traccar.helper.model.AttributeUtil;
import org.traccar.helper.model.PositionUtil;
import org.traccar.model.Device;
+import org.traccar.session.ConnectionManager;
import org.traccar.session.DeviceState;
import org.traccar.model.Event;
import org.traccar.model.Geofence;
@@ -39,7 +39,7 @@ public class OverspeedEventHandler extends BaseEventHandler {
public static final String ATTRIBUTE_SPEED = "speed";
- private final DeviceManager deviceManager;
+ private final ConnectionManager connectionManager;
private final CacheManager cacheManager;
private final boolean notRepeat;
@@ -47,8 +47,8 @@ public class OverspeedEventHandler extends BaseEventHandler {
private final boolean preferLowest;
@Inject
- public OverspeedEventHandler(Config config, DeviceManager deviceManager, CacheManager cacheManager) {
- this.deviceManager = deviceManager;
+ public OverspeedEventHandler(Config config, ConnectionManager connectionManager, CacheManager cacheManager) {
+ this.connectionManager = connectionManager;
this.cacheManager = cacheManager;
notRepeat = config.getBoolean(Keys.EVENT_OVERSPEED_NOT_REPEAT);
minimalDuration = config.getLong(Keys.EVENT_OVERSPEED_MINIMAL_DURATION) * 1000;
@@ -157,7 +157,7 @@ public class OverspeedEventHandler extends BaseEventHandler {
}
Map<Event, Position> result = null;
- DeviceState deviceState = deviceManager.getDeviceState(deviceId);
+ DeviceState deviceState = connectionManager.getDeviceState(deviceId);
if (deviceState.getOverspeedState() == null) {
deviceState.setOverspeedState(position.getSpeed() > speedLimit);
@@ -166,7 +166,7 @@ public class OverspeedEventHandler extends BaseEventHandler {
result = updateOverspeedState(deviceState, position, speedLimit, overspeedGeofenceId);
}
- deviceManager.setDeviceState(deviceId, deviceState);
+ connectionManager.setDeviceState(deviceId, deviceState);
return result;
}
diff --git a/src/main/java/org/traccar/reports/common/ReportUtils.java b/src/main/java/org/traccar/reports/common/ReportUtils.java
index 3960546e9..f5f2cd3df 100644
--- a/src/main/java/org/traccar/reports/common/ReportUtils.java
+++ b/src/main/java/org/traccar/reports/common/ReportUtils.java
@@ -29,7 +29,6 @@ import org.jxls.util.TransformerFactory;
import org.traccar.api.security.PermissionsService;
import org.traccar.config.Config;
import org.traccar.config.Keys;
-import org.traccar.database.DeviceManager;
import org.traccar.geocoder.Geocoder;
import org.traccar.handler.events.MotionEventHandler;
import org.traccar.helper.UnitsConverter;
@@ -75,7 +74,6 @@ public class ReportUtils {
private final Config config;
private final Storage storage;
private final PermissionsService permissionsService;
- private final DeviceManager deviceManager;
private final TripsConfig tripsConfig;
private final VelocityEngine velocityEngine;
private final Geocoder geocoder;
@@ -83,12 +81,10 @@ public class ReportUtils {
@Inject
public ReportUtils(
Config config, Storage storage, PermissionsService permissionsService,
- DeviceManager deviceManager, TripsConfig tripsConfig, VelocityEngine velocityEngine,
- @Nullable Geocoder geocoder) {
+ TripsConfig tripsConfig, VelocityEngine velocityEngine, @Nullable Geocoder geocoder) {
this.config = config;
this.storage = storage;
this.permissionsService = permissionsService;
- this.deviceManager = deviceManager;
this.tripsConfig = tripsConfig;
this.velocityEngine = velocityEngine;
this.geocoder = geocoder;
@@ -369,7 +365,7 @@ public class ReportUtils {
ArrayList<Position> positions = new ArrayList<>(positionCollection);
if (!positions.isEmpty()) {
boolean trips = reportClass.equals(TripReportItem.class);
- MotionEventHandler motionHandler = new MotionEventHandler(null, deviceManager, tripsConfig);
+ MotionEventHandler motionHandler = new MotionEventHandler(null, null, tripsConfig);
DeviceState deviceState = new DeviceState();
deviceState.setMotionState(isMoving(positions, 0, tripsConfig));
int startEventIndex = trips == deviceState.getMotionState() ? 0 : -1;
diff --git a/src/main/java/org/traccar/schedule/TaskDeviceInactivityCheck.java b/src/main/java/org/traccar/schedule/TaskDeviceInactivityCheck.java
index f2ed3c3b3..57c64dc5b 100644
--- a/src/main/java/org/traccar/schedule/TaskDeviceInactivityCheck.java
+++ b/src/main/java/org/traccar/schedule/TaskDeviceInactivityCheck.java
@@ -15,11 +15,16 @@
*/
package org.traccar.schedule;
-import org.traccar.database.DeviceManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.traccar.database.NotificationManager;
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 org.traccar.storage.query.Columns;
+import org.traccar.storage.query.Request;
import javax.inject.Inject;
import java.util.HashMap;
@@ -29,18 +34,20 @@ import java.util.concurrent.TimeUnit;
public class TaskDeviceInactivityCheck implements ScheduleTask {
+ private static final Logger LOGGER = LoggerFactory.getLogger(TaskDeviceInactivityCheck.class);
+
public static final String ATTRIBUTE_DEVICE_INACTIVITY_START = "deviceInactivityStart";
public static final String ATTRIBUTE_DEVICE_INACTIVITY_PERIOD = "deviceInactivityPeriod";
public static final String ATTRIBUTE_LAST_UPDATE = "lastUpdate";
private static final long CHECK_PERIOD_MINUTES = 15;
- private final DeviceManager deviceManager;
+ private final Storage storage;
private final NotificationManager notificationManager;
@Inject
- public TaskDeviceInactivityCheck(DeviceManager deviceManager, NotificationManager notificationManager) {
- this.deviceManager = deviceManager;
+ public TaskDeviceInactivityCheck(Storage storage, NotificationManager notificationManager) {
+ this.storage = storage;
this.notificationManager = notificationManager;
}
@@ -55,12 +62,17 @@ public class TaskDeviceInactivityCheck implements ScheduleTask {
long checkPeriod = TimeUnit.MINUTES.toMillis(CHECK_PERIOD_MINUTES);
Map<Event, Position> events = new HashMap<>();
- for (Device device : deviceManager.getAllDevices()) {
- if (device.getLastUpdate() != null && checkDevice(device, currentTime, checkPeriod)) {
- Event event = new Event(Event.TYPE_DEVICE_INACTIVE, device.getId());
- event.set(ATTRIBUTE_LAST_UPDATE, device.getLastUpdate().getTime());
- events.put(event, null);
+
+ try {
+ for (Device device : storage.getObjects(Device.class, new Request(new Columns.All()))) {
+ if (device.getLastUpdate() != null && checkDevice(device, currentTime, checkPeriod)) {
+ Event event = new Event(Event.TYPE_DEVICE_INACTIVE, device.getId());
+ event.set(ATTRIBUTE_LAST_UPDATE, device.getLastUpdate().getTime());
+ events.put(event, null);
+ }
}
+ } catch (StorageException e) {
+ LOGGER.warn("Get devices error", e);
}
notificationManager.updateEvents(events);
diff --git a/src/main/java/org/traccar/session/ConnectionManager.java b/src/main/java/org/traccar/session/ConnectionManager.java
index 38d82e848..cead771c9 100644
--- a/src/main/java/org/traccar/session/ConnectionManager.java
+++ b/src/main/java/org/traccar/session/ConnectionManager.java
@@ -20,7 +20,6 @@ import io.netty.util.Timeout;
import io.netty.util.Timer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.traccar.Context;
import org.traccar.Main;
import org.traccar.Protocol;
import org.traccar.config.Config;
@@ -63,6 +62,8 @@ public class ConnectionManager {
private final Map<Long, DeviceSession> sessionsByDeviceId = new ConcurrentHashMap<>();
private final Map<Endpoint, Map<String, DeviceSession>> sessionsByEndpoint = new ConcurrentHashMap<>();
+ private final Map<Long, DeviceState> deviceStates = new ConcurrentHashMap<>();
+
private final Config config;
private final CacheManager cacheManager;
private final Storage storage;
@@ -256,7 +257,9 @@ public class ConnectionManager {
}
try {
- Context.getDeviceManager().updateDeviceStatus(device);
+ storage.updateObject(device, new Request(
+ new Columns.Include("lastUpdate"),
+ new Condition.Equals("id", "id")));
} catch (StorageException e) {
LOGGER.warn("Update device status error", e);
}
@@ -264,8 +267,16 @@ public class ConnectionManager {
updateDevice(device);
}
+ public DeviceState getDeviceState(long deviceId) {
+ return deviceStates.computeIfAbsent(deviceId, x -> new DeviceState());
+ }
+
+ public void setDeviceState(long deviceId, DeviceState deviceState) {
+ deviceStates.put(deviceId, deviceState);
+ }
+
public Map<Event, Position> updateDeviceState(long deviceId) {
- DeviceState deviceState = Context.getDeviceManager().getDeviceState(deviceId);
+ DeviceState deviceState = getDeviceState(deviceId);
Map<Event, Position> result = new HashMap<>();
Map<Event, Position> event = Main.getInjector()