aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/database
diff options
context:
space:
mode:
authorAbyss777 <abyss@fox5.ru>2017-08-08 09:53:31 +0500
committerAbyss777 <abyss@fox5.ru>2017-08-09 11:04:29 +0500
commit94a922eaf167a73a0188255398fb1c6799767e18 (patch)
treed1f23e82db216a1c859a096a2c4f7f8676d50a6b /src/org/traccar/database
parentc76acd69275ae2d6612bb731758df8a5b91e6508 (diff)
downloadtrackermap-server-94a922eaf167a73a0188255398fb1c6799767e18.tar.gz
trackermap-server-94a922eaf167a73a0188255398fb1c6799767e18.tar.bz2
trackermap-server-94a922eaf167a73a0188255398fb1c6799767e18.zip
Implement motion detection similar to trips detection
Diffstat (limited to 'src/org/traccar/database')
-rw-r--r--src/org/traccar/database/ConnectionManager.java45
-rw-r--r--src/org/traccar/database/DeviceManager.java12
2 files changed, 54 insertions, 3 deletions
diff --git a/src/org/traccar/database/ConnectionManager.java b/src/org/traccar/database/ConnectionManager.java
index 1445fb785..b3b00fefa 100644
--- a/src/org/traccar/database/ConnectionManager.java
+++ b/src/org/traccar/database/ConnectionManager.java
@@ -23,8 +23,11 @@ import org.traccar.GlobalTimer;
import org.traccar.Protocol;
import org.traccar.helper.Log;
import org.traccar.model.Device;
+import org.traccar.model.DeviceState;
import org.traccar.model.Event;
import org.traccar.model.Position;
+import org.traccar.reports.ReportUtils;
+import org.traccar.reports.model.TripsConfig;
import java.net.SocketAddress;
import java.sql.SQLException;
@@ -41,6 +44,7 @@ public class ConnectionManager {
private final long deviceTimeout;
private final boolean enableStatusEvents;
+ private TripsConfig tripsConfig = null;
private final Map<Long, ActiveDevice> activeDevices = new ConcurrentHashMap<>();
private final Map<Long, Set<UpdateListener>> listeners = new ConcurrentHashMap<>();
@@ -49,6 +53,9 @@ public class ConnectionManager {
public ConnectionManager() {
deviceTimeout = Context.getConfig().getLong("status.timeout", DEFAULT_TIMEOUT) * 1000;
enableStatusEvents = Context.getConfig().getBoolean("event.enable");
+ if (Context.getConfig().getBoolean("status.updateDeviceState")) {
+ tripsConfig = ReportUtils.initTripsConfig();
+ }
}
public void addActiveDevice(long deviceId, Protocol protocol, Channel channel, SocketAddress remoteAddress) {
@@ -80,21 +87,30 @@ public class ConnectionManager {
if (enableStatusEvents && !status.equals(oldStatus)) {
String eventType;
+ Event stateEvent = null;
switch (status) {
case Device.STATUS_ONLINE:
eventType = Event.TYPE_DEVICE_ONLINE;
break;
case Device.STATUS_UNKNOWN:
eventType = Event.TYPE_DEVICE_UNKNOWN;
+ if (tripsConfig != null) {
+ stateEvent = updateDeviceState(deviceId);
+ }
break;
default:
eventType = Event.TYPE_DEVICE_OFFLINE;
+ if (tripsConfig != null) {
+ stateEvent = updateDeviceState(deviceId);
+ }
break;
}
- Event event = new Event(eventType, deviceId);
- if (Context.getNotificationManager() != null) {
- Context.getNotificationManager().updateEvent(event, null);
+ Set<Event> events = new HashSet<>();
+ if (stateEvent != null) {
+ events.add(stateEvent);
}
+ events.add(new Event(eventType, deviceId));
+ Context.getNotificationManager().updateEvents(events, null);
}
Timeout timeout = timeouts.remove(deviceId);
@@ -126,6 +142,29 @@ public class ConnectionManager {
updateDevice(device);
}
+ public Event updateDeviceState(long deviceId) {
+ DeviceState deviceState = Context.getDeviceManager().getDeviceState(deviceId);
+ if (deviceState == null || deviceState.getMotionState() == null) {
+ return null;
+ }
+ Event result = null;
+ Boolean oldMotion = deviceState.getMotionState();
+ long currentTime = new Date().getTime();
+ boolean newMotion = !oldMotion;
+ Position potentialPosition = deviceState.getMotionPosition();
+ if (potentialPosition != null) {
+ long potentialTime = potentialPosition.getFixTime().getTime()
+ + (newMotion ? tripsConfig.getMinimalTripDuration() : tripsConfig.getMinimalParkingDuration());
+ if (potentialTime <= currentTime) {
+ String eventType = newMotion ? Event.TYPE_DEVICE_MOVING : Event.TYPE_DEVICE_STOPPED;
+ result = new Event(eventType, potentialPosition.getDeviceId(), potentialPosition.getId());
+ deviceState.setMotionState(newMotion);
+ deviceState.setMotionPosition(null);
+ }
+ }
+ return result;
+ }
+
public synchronized void updateDevice(Device device) {
for (long userId : Context.getPermissionsManager().getDeviceUsers(device.getId())) {
if (listeners.containsKey(userId)) {
diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java
index 5d123f9b8..3b7e5c617 100644
--- a/src/org/traccar/database/DeviceManager.java
+++ b/src/org/traccar/database/DeviceManager.java
@@ -33,6 +33,7 @@ import org.traccar.helper.Log;
import org.traccar.model.Command;
import org.traccar.model.CommandType;
import org.traccar.model.Device;
+import org.traccar.model.DeviceState;
import org.traccar.model.DeviceTotalDistance;
import org.traccar.model.Group;
import org.traccar.model.Position;
@@ -52,6 +53,8 @@ public class DeviceManager extends BaseObjectManager<Device> implements Identity
private final Map<Long, Position> positions = new ConcurrentHashMap<>();
+ private final Map<Long, DeviceState> deviceStates = new ConcurrentHashMap<>();
+
private boolean fallbackToText;
public DeviceManager(DataManager dataManager) {
@@ -387,4 +390,13 @@ public class DeviceManager extends BaseObjectManager<Device> implements Identity
}
return result;
}
+
+ public DeviceState getDeviceState(long deviceId) {
+ return deviceStates.get(deviceId);
+ }
+
+ public void setDeviceState(long deviceId, DeviceState deviceState) {
+ deviceStates.put(deviceId, deviceState);
+ }
+
}