aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/handler
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-09-25 10:35:20 -0700
committerAnton Tananaev <anton@traccar.org>2022-09-25 10:35:20 -0700
commitfe3d9995cceb2f1530a7c2549ae9a4cf457cb7f0 (patch)
tree36e7d1b457f7c1e8d3b867cd106033d000a59ddf /src/main/java/org/traccar/handler
parentabb26e80a5617424d960a0f7d0b98fcb379a5224 (diff)
downloadtrackermap-server-fe3d9995cceb2f1530a7c2549ae9a4cf457cb7f0.tar.gz
trackermap-server-fe3d9995cceb2f1530a7c2549ae9a4cf457cb7f0.tar.bz2
trackermap-server-fe3d9995cceb2f1530a7c2549ae9a4cf457cb7f0.zip
Persist device state
Diffstat (limited to 'src/main/java/org/traccar/handler')
-rw-r--r--src/main/java/org/traccar/handler/events/MotionEventHandler.java84
-rw-r--r--src/main/java/org/traccar/handler/events/OverspeedEventHandler.java80
2 files changed, 56 insertions, 108 deletions
diff --git a/src/main/java/org/traccar/handler/events/MotionEventHandler.java b/src/main/java/org/traccar/handler/events/MotionEventHandler.java
index 234899785..0777f353a 100644
--- a/src/main/java/org/traccar/handler/events/MotionEventHandler.java
+++ b/src/main/java/org/traccar/handler/events/MotionEventHandler.java
@@ -17,14 +17,21 @@
package org.traccar.handler.events;
import io.netty.channel.ChannelHandler;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
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;
+import org.traccar.session.state.MotionProcessor;
+import org.traccar.session.state.MotionState;
+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.Collections;
@@ -33,66 +40,20 @@ import java.util.Map;
@ChannelHandler.Sharable
public class MotionEventHandler extends BaseEventHandler {
+ private static final Logger LOGGER = LoggerFactory.getLogger(MotionEventHandler.class);
+
private final CacheManager cacheManager;
- private final ConnectionManager connectionManager;
+ private final Storage storage;
private final TripsConfig tripsConfig;
@Inject
public MotionEventHandler(
- CacheManager cacheManager, ConnectionManager connectionManager, TripsConfig tripsConfig) {
+ CacheManager cacheManager, Storage storage, TripsConfig tripsConfig) {
this.cacheManager = cacheManager;
- this.connectionManager = connectionManager;
+ this.storage = storage;
this.tripsConfig = tripsConfig;
}
- public Map<Event, Position> updateMotionState(DeviceState deviceState, Position position, boolean newState) {
-
- boolean oldState = deviceState.getMotionState();
- if (oldState == newState) {
- if (deviceState.getMotionTime() != null) {
- long oldTime = deviceState.getMotionTime().getTime();
- long newTime = position.getFixTime().getTime();
-
- double distance = position.getDouble(Position.KEY_TOTAL_DISTANCE) - deviceState.getMotionDistance();
- Boolean ignition = null;
- if (tripsConfig.getUseIgnition() && position.hasAttribute(Position.KEY_IGNITION)) {
- ignition = position.getBoolean(Position.KEY_IGNITION);
- }
-
- boolean generateEvent = false;
- if (newState) {
- if (newTime - oldTime >= tripsConfig.getMinimalTripDuration()
- || distance >= tripsConfig.getMinimalTripDistance()) {
- generateEvent = true;
- }
- } else {
- if (newTime - oldTime >= tripsConfig.getMinimalParkingDuration()
- || ignition != null && !ignition) {
- generateEvent = true;
- }
- }
-
- if (generateEvent) {
-
- String eventType = newState ? Event.TYPE_DEVICE_MOVING : Event.TYPE_DEVICE_STOPPED;
- Event event = new Event(eventType, position);
-
- deviceState.setMotionTime(null);
- deviceState.setMotionDistance(0);
-
- return Collections.singletonMap(event, position);
-
- }
- }
- } else {
- deviceState.setMotionState(newState);
- deviceState.setMotionTime(position.getFixTime());
- deviceState.setMotionDistance(position.getDouble(Position.KEY_TOTAL_DISTANCE));
- }
-
- return null;
- }
-
@Override
protected Map<Event, Position> analyzePosition(Position position) {
@@ -106,10 +67,19 @@ public class MotionEventHandler extends BaseEventHandler {
return null;
}
- DeviceState deviceState = connectionManager.getDeviceState(deviceId);
- var result = updateMotionState(deviceState, position, position.getBoolean(Position.KEY_MOTION));
- connectionManager.setDeviceState(deviceId, deviceState);
- return result;
+ MotionState state = MotionState.fromDevice(device);
+ MotionProcessor.updateState(state, position, position.getBoolean(Position.KEY_MOTION), tripsConfig);
+ if (state.isChanged()) {
+ state.toDevice(device);
+ try {
+ storage.updateObject(device, new Request(
+ new Columns.Include("motionState", "motionTime", "motionDistance"),
+ new Condition.Equals("id", "id")));
+ } catch (StorageException e) {
+ LOGGER.warn("Update device motion error", e);
+ }
+ }
+ return state.getEvent() != null ? Collections.singletonMap(state.getEvent(), position) : null;
}
}
diff --git a/src/main/java/org/traccar/handler/events/OverspeedEventHandler.java b/src/main/java/org/traccar/handler/events/OverspeedEventHandler.java
index 3984299d7..c03b8eb7b 100644
--- a/src/main/java/org/traccar/handler/events/OverspeedEventHandler.java
+++ b/src/main/java/org/traccar/handler/events/OverspeedEventHandler.java
@@ -16,81 +16,50 @@
*/
package org.traccar.handler.events;
-import java.util.Collections;
-import java.util.Map;
-
import io.netty.channel.ChannelHandler;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.traccar.config.Config;
import org.traccar.config.Keys;
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;
import org.traccar.model.Position;
import org.traccar.session.cache.CacheManager;
+import org.traccar.session.state.OverspeedProcessor;
+import org.traccar.session.state.OverspeedState;
+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.Collections;
+import java.util.Map;
@ChannelHandler.Sharable
public class OverspeedEventHandler extends BaseEventHandler {
- public static final String ATTRIBUTE_SPEED = "speed";
+ private static final Logger LOGGER = LoggerFactory.getLogger(OverspeedEventHandler.class);
- private final ConnectionManager connectionManager;
private final CacheManager cacheManager;
+ private final Storage storage;
private final long minimalDuration;
private final boolean preferLowest;
@Inject
- public OverspeedEventHandler(Config config, ConnectionManager connectionManager, CacheManager cacheManager) {
- this.connectionManager = connectionManager;
+ public OverspeedEventHandler(
+ Config config, CacheManager cacheManager, Storage storage) {
this.cacheManager = cacheManager;
+ this.storage = storage;
minimalDuration = config.getLong(Keys.EVENT_OVERSPEED_MINIMAL_DURATION) * 1000;
preferLowest = config.getBoolean(Keys.EVENT_OVERSPEED_PREFER_LOWEST);
}
- public Map<Event, Position> updateOverspeedState(
- DeviceState deviceState, Position position, double speedLimit, long geofenceId) {
-
- boolean oldState = deviceState.getOverspeedState();
- if (oldState) {
- boolean newState = position.getSpeed() > speedLimit;
- if (newState) {
- if (deviceState.getOverspeedTime() != null) {
- long oldTime = deviceState.getOverspeedTime().getTime();
- long newTime = position.getFixTime().getTime();
- if (newTime - oldTime > minimalDuration) {
-
- Event event = new Event(Event.TYPE_DEVICE_OVERSPEED, position);
- event.set(ATTRIBUTE_SPEED, position.getSpeed());
- event.set(Position.KEY_SPEED_LIMIT, speedLimit);
- event.setGeofenceId(deviceState.getOverspeedGeofenceId());
-
- deviceState.setOverspeedTime(null);
- deviceState.setOverspeedGeofenceId(0);
-
- return Collections.singletonMap(event, position);
-
- }
- }
- } else {
- deviceState.setOverspeedState(false);
- deviceState.setOverspeedTime(null);
- deviceState.setOverspeedGeofenceId(0);
- }
- } else if (position != null && position.getSpeed() > speedLimit) {
- deviceState.setOverspeedState(true);
- deviceState.setOverspeedTime(position.getFixTime());
- deviceState.setOverspeedGeofenceId(geofenceId);
- }
-
- return null;
- }
-
@Override
protected Map<Event, Position> analyzePosition(Position position) {
@@ -135,10 +104,19 @@ public class OverspeedEventHandler extends BaseEventHandler {
return null;
}
- DeviceState deviceState = connectionManager.getDeviceState(deviceId);
- var result = updateOverspeedState(deviceState, position, speedLimit, overspeedGeofenceId);
- connectionManager.setDeviceState(deviceId, deviceState);
- return result;
+ OverspeedState state = OverspeedState.fromDevice(device);
+ OverspeedProcessor.updateState(state, position, speedLimit, minimalDuration, overspeedGeofenceId);
+ if (state.isChanged()) {
+ state.toDevice(device);
+ try {
+ storage.updateObject(device, new Request(
+ new Columns.Include("overspeedState", "overspeedTime", "overspeedGeofenceId"),
+ new Condition.Equals("id", "id")));
+ } catch (StorageException e) {
+ LOGGER.warn("Update device overspeed error", e);
+ }
+ }
+ return state.getEvent() != null ? Collections.singletonMap(state.getEvent(), position) : null;
}
}