From 32a0fee0b092e488662ed29b7b7f1f303ae57e0f Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Fri, 11 Aug 2017 15:06:09 +0500 Subject: Make Overspeed and Motion events handlers singleton and move some code to function --- src/org/traccar/events/MotionEventHandler.java | 34 +++++++++++++------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'src/org/traccar/events/MotionEventHandler.java') diff --git a/src/org/traccar/events/MotionEventHandler.java b/src/org/traccar/events/MotionEventHandler.java index 1a8cb0ef8..b20a11999 100644 --- a/src/org/traccar/events/MotionEventHandler.java +++ b/src/org/traccar/events/MotionEventHandler.java @@ -32,11 +32,20 @@ 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, 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(); @@ -45,16 +54,13 @@ public class MotionEventHandler extends BaseEventHandler { long motionTime = motionPosition.getFixTime().getTime() + (newMotion ? tripsConfig.getMinimalTripDuration() : tripsConfig.getMinimalParkingDuration()); if (motionTime <= currentTime) { - String eventType = newMotion ? Event.TYPE_DEVICE_MOVING : Event.TYPE_DEVICE_STOPPED; - result = new Event(eventType, motionPosition.getDeviceId(), motionPosition.getId()); - deviceState.setMotionState(newMotion); - deviceState.setMotionPosition(null); + result = newEvent(deviceState, newMotion); } } return result; } - public static Event updateMotionState(DeviceState deviceState, Position position, TripsConfig tripsConfig) { + public Event updateMotionState(DeviceState deviceState, Position position) { Event result = null; Boolean oldMotion = deviceState.getMotionState(); @@ -75,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); } } } @@ -110,7 +110,7 @@ public class MotionEventHandler extends BaseEventHandler { 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) { -- cgit v1.2.3