aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/events
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/traccar/events')
-rw-r--r--src/org/traccar/events/MotionEventHandler.java52
-rw-r--r--src/org/traccar/events/OverspeedEventHandler.java88
2 files changed, 104 insertions, 36 deletions
diff --git a/src/org/traccar/events/MotionEventHandler.java b/src/org/traccar/events/MotionEventHandler.java
index 228b43c0f..b20a11999 100644
--- a/src/org/traccar/events/MotionEventHandler.java
+++ b/src/org/traccar/events/MotionEventHandler.java
@@ -32,11 +32,35 @@ public class MotionEventHandler extends BaseEventHandler {
private TripsConfig tripsConfig;
- public MotionEventHandler() {
- tripsConfig = ReportUtils.initTripsConfig();
+ public MotionEventHandler(TripsConfig tripsConfig) {
+ this.tripsConfig = tripsConfig;
}
- public static Event updateMotionState(DeviceState deviceState, Position position, TripsConfig tripsConfig) {
+ private Event newEvent(DeviceState deviceState, boolean newMotion) {
+ String eventType = newMotion ? Event.TYPE_DEVICE_MOVING : Event.TYPE_DEVICE_STOPPED;
+ Event event = new Event(eventType, deviceState.getMotionPosition().getDeviceId(),
+ deviceState.getMotionPosition().getId());
+ deviceState.setMotionState(newMotion);
+ deviceState.setMotionPosition(null);
+ return event;
+ }
+
+ public Event updateMotionState(DeviceState deviceState) {
+ Event result = null;
+ if (deviceState.getMotionState() != null && deviceState.getMotionPosition() != null) {
+ boolean newMotion = !deviceState.getMotionState();
+ Position motionPosition = deviceState.getMotionPosition();
+ long currentTime = System.currentTimeMillis();
+ long motionTime = motionPosition.getFixTime().getTime()
+ + (newMotion ? tripsConfig.getMinimalTripDuration() : tripsConfig.getMinimalParkingDuration());
+ if (motionTime <= currentTime) {
+ result = newEvent(deviceState, newMotion);
+ }
+ }
+ return result;
+ }
+
+ public Event updateMotionState(DeviceState deviceState, Position position) {
Event result = null;
Boolean oldMotion = deviceState.getMotionState();
@@ -57,17 +81,11 @@ public class MotionEventHandler extends BaseEventHandler {
if (newMotion) {
if (motionTime + tripsConfig.getMinimalTripDuration() <= currentTime
|| distance >= tripsConfig.getMinimalTripDistance()) {
- result = new Event(Event.TYPE_DEVICE_MOVING, motionPosition.getDeviceId(),
- motionPosition.getId());
- deviceState.setMotionState(true);
- deviceState.setMotionPosition(null);
+ result = newEvent(deviceState, newMotion);
}
} else {
if (motionTime + tripsConfig.getMinimalParkingDuration() <= currentTime) {
- result = new Event(Event.TYPE_DEVICE_STOPPED, motionPosition.getDeviceId(),
- motionPosition.getId());
- deviceState.setMotionState(false);
- deviceState.setMotionPosition(null);
+ result = newEvent(deviceState, newMotion);
}
}
}
@@ -77,7 +95,8 @@ public class MotionEventHandler extends BaseEventHandler {
@Override
protected Collection<Event> analyzePosition(Position position) {
- Device device = Context.getIdentityManager().getById(position.getDeviceId());
+ long deviceId = position.getDeviceId();
+ Device device = Context.getIdentityManager().getById(deviceId);
if (device == null) {
return null;
}
@@ -86,17 +105,12 @@ public class MotionEventHandler extends BaseEventHandler {
}
Event result = null;
-
- long deviceId = position.getDeviceId();
DeviceState deviceState = Context.getDeviceManager().getDeviceState(deviceId);
- if (deviceState == null) {
- deviceState = new DeviceState();
- deviceState.setMotionState(position.getBoolean(Position.KEY_MOTION));
- } else if (deviceState.getMotionState() == null) {
+ if (deviceState.getMotionState() == null) {
deviceState.setMotionState(position.getBoolean(Position.KEY_MOTION));
} else {
- result = updateMotionState(deviceState, position, tripsConfig);
+ result = updateMotionState(deviceState, position);
}
Context.getDeviceManager().setDeviceState(deviceId, deviceState);
if (result != null) {
diff --git a/src/org/traccar/events/OverspeedEventHandler.java b/src/org/traccar/events/OverspeedEventHandler.java
index 795892f40..953af6b33 100644
--- a/src/org/traccar/events/OverspeedEventHandler.java
+++ b/src/org/traccar/events/OverspeedEventHandler.java
@@ -21,6 +21,7 @@ import java.util.Collections;
import org.traccar.BaseEventHandler;
import org.traccar.Context;
import org.traccar.model.Device;
+import org.traccar.model.DeviceState;
import org.traccar.model.Event;
import org.traccar.model.Position;
@@ -29,15 +30,69 @@ public class OverspeedEventHandler extends BaseEventHandler {
public static final String ATTRIBUTE_SPEED_LIMIT = "speedLimit";
private boolean notRepeat;
+ private long minimalDuration;
- public OverspeedEventHandler() {
- notRepeat = Context.getConfig().getBoolean("event.overspeed.notRepeat");
+ public OverspeedEventHandler(long minimalDuration, boolean notRepeat) {
+ this.notRepeat = notRepeat;
+ this.minimalDuration = minimalDuration;
+ }
+
+ private Event newEvent(DeviceState deviceState, double speedLimit) {
+ Event event = new Event(Event.TYPE_DEVICE_OVERSPEED, deviceState.getOverspeedPosition().getDeviceId(),
+ deviceState.getOverspeedPosition().getId());
+ event.set("speed", deviceState.getOverspeedPosition().getSpeed());
+ event.set(ATTRIBUTE_SPEED_LIMIT, speedLimit);
+ deviceState.setOverspeedState(notRepeat);
+ deviceState.setOverspeedPosition(null);
+ return event;
+ }
+
+ public Event updateOverspeedState(DeviceState deviceState, double speedLimit) {
+ Event result = null;
+ if (deviceState.getOverspeedState() != null && !deviceState.getOverspeedState()
+ && deviceState.getOverspeedPosition() != null && speedLimit != 0) {
+ long currentTime = System.currentTimeMillis();
+ Position overspeedPosition = deviceState.getOverspeedPosition();
+ long overspeedTime = overspeedPosition.getFixTime().getTime();
+ if (overspeedTime + minimalDuration <= currentTime) {
+ result = newEvent(deviceState, speedLimit);
+ }
+ }
+ return result;
+ }
+
+ public Event updateOverspeedState(DeviceState deviceState, Position position, double speedLimit) {
+ Event result = null;
+
+ Boolean oldOverspeed = deviceState.getOverspeedState();
+
+ long currentTime = position.getFixTime().getTime();
+ boolean newOverspeed = position.getSpeed() > speedLimit;
+ if (newOverspeed && !oldOverspeed) {
+ if (deviceState.getOverspeedPosition() == null) {
+ deviceState.setOverspeedPosition(position);
+ }
+ } else if (oldOverspeed && !newOverspeed) {
+ deviceState.setOverspeedState(false);
+ deviceState.setOverspeedPosition(null);
+ } else {
+ deviceState.setOverspeedPosition(null);
+ }
+ Position overspeedPosition = deviceState.getOverspeedPosition();
+ if (overspeedPosition != null) {
+ long overspeedTime = overspeedPosition.getFixTime().getTime();
+ if (newOverspeed && overspeedTime + minimalDuration <= currentTime) {
+ result = newEvent(deviceState, speedLimit);
+ }
+ }
+ return result;
}
@Override
protected Collection<Event> analyzePosition(Position position) {
- Device device = Context.getIdentityManager().getById(position.getDeviceId());
+ long deviceId = position.getDeviceId();
+ Device device = Context.getIdentityManager().getById(deviceId);
if (device == null) {
return null;
}
@@ -45,24 +100,23 @@ public class OverspeedEventHandler extends BaseEventHandler {
return null;
}
- double speed = position.getSpeed();
- double speedLimit = Context.getDeviceManager()
- .lookupAttributeDouble(device.getId(), ATTRIBUTE_SPEED_LIMIT, 0, false);
+ double speedLimit = Context.getDeviceManager().lookupAttributeDouble(deviceId, ATTRIBUTE_SPEED_LIMIT, 0, false);
if (speedLimit == 0) {
return null;
}
- double oldSpeed = 0;
- if (notRepeat) {
- Position lastPosition = Context.getIdentityManager().getLastPosition(position.getDeviceId());
- if (lastPosition != null) {
- oldSpeed = lastPosition.getSpeed();
- }
+
+ Event result = null;
+ DeviceState deviceState = Context.getDeviceManager().getDeviceState(deviceId);
+
+ if (deviceState.getOverspeedState() == null) {
+ deviceState.setOverspeedState(position.getSpeed() > speedLimit);
+ } else {
+ result = updateOverspeedState(deviceState, position, speedLimit);
}
- if (speed > speedLimit && oldSpeed <= speedLimit) {
- Event event = new Event(Event.TYPE_DEVICE_OVERSPEED, position.getDeviceId(), position.getId());
- event.set("speed", speed);
- event.set(ATTRIBUTE_SPEED_LIMIT, speedLimit);
- return Collections.singleton(event);
+
+ Context.getDeviceManager().setDeviceState(deviceId, deviceState);
+ if (result != null) {
+ return Collections.singleton(result);
}
return null;
}