aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-07-12 17:31:36 -0700
committerAnton Tananaev <anton@traccar.org>2022-07-12 17:31:36 -0700
commit493ff1068ea3e4d96f2475234b265b47cce8691f (patch)
tree6de7dbc5ac202ba576bc04ecf45dab751aee7bfd
parent98e2db95f73cb85a2623e2902741bcb4e73683e5 (diff)
downloadtrackermap-server-493ff1068ea3e4d96f2475234b265b47cce8691f.tar.gz
trackermap-server-493ff1068ea3e4d96f2475234b265b47cce8691f.tar.bz2
trackermap-server-493ff1068ea3e4d96f2475234b265b47cce8691f.zip
Persist device status (fix #4890, fix #4891)
-rw-r--r--schema/changelog-5.2.xml21
-rw-r--r--schema/changelog-master.xml1
-rw-r--r--src/main/java/org/traccar/handler/events/GeofenceEventHandler.java21
-rw-r--r--src/main/java/org/traccar/model/Device.java4
-rw-r--r--src/main/java/org/traccar/session/ConnectionManager.java13
-rw-r--r--src/main/java/org/traccar/session/cache/CacheManager.java14
6 files changed, 49 insertions, 25 deletions
diff --git a/schema/changelog-5.2.xml b/schema/changelog-5.2.xml
new file mode 100644
index 000000000..1ac9eedc5
--- /dev/null
+++ b/schema/changelog-5.2.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<databaseChangeLog
+ xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
+ http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd"
+ logicalFilePath="changelog-5.2">
+
+ <changeSet author="author" id="changelog-5.2">
+
+ <addColumn tableName="tc_devices">
+ <column name="status" type="CHAR(8)" />
+ </addColumn>
+
+ <addColumn tableName="tc_devices">
+ <column name="geofenceIds" type="VARCHAR(128)" />
+ </addColumn>
+
+ </changeSet>
+
+</databaseChangeLog>
diff --git a/schema/changelog-master.xml b/schema/changelog-master.xml
index 471b0fe5d..83a1ac865 100644
--- a/schema/changelog-master.xml
+++ b/schema/changelog-master.xml
@@ -32,5 +32,6 @@
<include file="changelog-5.0.xml" relativeToChangelogFile="true" />
<include file="changelog-5.1.xml" relativeToChangelogFile="true" />
+ <include file="changelog-5.2.xml" relativeToChangelogFile="true" />
</databaseChangeLog>
diff --git a/src/main/java/org/traccar/handler/events/GeofenceEventHandler.java b/src/main/java/org/traccar/handler/events/GeofenceEventHandler.java
index ca3fc3f89..70bdb84cb 100644
--- a/src/main/java/org/traccar/handler/events/GeofenceEventHandler.java
+++ b/src/main/java/org/traccar/handler/events/GeofenceEventHandler.java
@@ -26,6 +26,11 @@ import org.traccar.model.Geofence;
import org.traccar.model.Position;
import org.traccar.session.ConnectionManager;
import org.traccar.session.cache.CacheManager;
+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.ArrayList;
@@ -39,12 +44,15 @@ public class GeofenceEventHandler extends BaseEventHandler {
private final Config config;
private final CacheManager cacheManager;
private final ConnectionManager connectionManager;
+ private final Storage storage;
@Inject
- public GeofenceEventHandler(Config config, CacheManager cacheManager, ConnectionManager connectionManager) {
+ public GeofenceEventHandler(
+ Config config, CacheManager cacheManager, ConnectionManager connectionManager, Storage storage) {
this.config = config;
this.cacheManager = cacheManager;
this.connectionManager = connectionManager;
+ this.storage = storage;
}
@Override
@@ -66,8 +74,17 @@ public class GeofenceEventHandler extends BaseEventHandler {
newGeofences.removeAll(oldGeofences);
oldGeofences.removeAll(currentGeofences);
- device.setGeofenceIds(currentGeofences);
+
if (!oldGeofences.isEmpty() || !newGeofences.isEmpty()) {
+ device.setGeofenceIds(currentGeofences.isEmpty() ? null : currentGeofences);
+
+ try {
+ storage.updateObject(device, new Request(
+ new Columns.Include("geofenceIds"), new Condition.Equals("id", "id")));
+ } catch (StorageException e) {
+ throw new RuntimeException("Update device geofences error", e);
+ }
+
connectionManager.updateDevice(true, device);
}
diff --git a/src/main/java/org/traccar/model/Device.java b/src/main/java/org/traccar/model/Device.java
index 35eb757f2..6593c59b0 100644
--- a/src/main/java/org/traccar/model/Device.java
+++ b/src/main/java/org/traccar/model/Device.java
@@ -50,14 +50,13 @@ public class Device extends GroupedModel {
private String status;
- @QueryIgnore
public String getStatus() {
return status != null ? status : STATUS_OFFLINE;
}
@QueryIgnore
public void setStatus(String status) {
- this.status = status;
+ this.status = status.trim();
}
private Date lastUpdate;
@@ -85,7 +84,6 @@ public class Device extends GroupedModel {
private List<Long> geofenceIds;
- @QueryIgnore
public List<Long> getGeofenceIds() {
return geofenceIds;
}
diff --git a/src/main/java/org/traccar/session/ConnectionManager.java b/src/main/java/org/traccar/session/ConnectionManager.java
index d4bb9b4b1..62fdd833b 100644
--- a/src/main/java/org/traccar/session/ConnectionManager.java
+++ b/src/main/java/org/traccar/session/ConnectionManager.java
@@ -52,6 +52,7 @@ import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
@@ -256,15 +257,15 @@ public class ConnectionManager implements BroadcastInterface {
notificationManager.updateEvents(events);
}
+ if (time != null) {
+ device.setLastUpdate(time);
+ }
+
Timeout timeout = timeouts.remove(deviceId);
if (timeout != null) {
timeout.cancel();
}
- if (time != null) {
- device.setLastUpdate(time);
- }
-
if (status.equals(Device.STATUS_ONLINE)) {
timeouts.put(deviceId, timer.newTimeout(timeout1 -> {
if (!timeout1.isCancelled()) {
@@ -275,7 +276,7 @@ public class ConnectionManager implements BroadcastInterface {
try {
storage.updateObject(device, new Request(
- new Columns.Include("lastUpdate"),
+ new Columns.Include("status", "lastUpdate"),
new Condition.Equals("id", "id")));
} catch (StorageException e) {
LOGGER.warn("Update device status error", e);
@@ -368,7 +369,7 @@ public class ConnectionManager implements BroadcastInterface {
if (clazz1.equals(User.class) && clazz2.equals(Device.class)) {
if (listeners.containsKey(id1)) {
userDevices.get(id1).add(id2);
- deviceUsers.put(id2, Set.of(id1));
+ deviceUsers.put(id2, new HashSet<>(List.of(id1)));
}
}
}
diff --git a/src/main/java/org/traccar/session/cache/CacheManager.java b/src/main/java/org/traccar/session/cache/CacheManager.java
index c1697faf9..4e99161dd 100644
--- a/src/main/java/org/traccar/session/cache/CacheManager.java
+++ b/src/main/java/org/traccar/session/cache/CacheManager.java
@@ -18,7 +18,6 @@ package org.traccar.session.cache;
import org.traccar.broadcast.BroadcastInterface;
import org.traccar.broadcast.BroadcastService;
import org.traccar.config.Config;
-import org.traccar.helper.model.GeofenceUtil;
import org.traccar.model.Attribute;
import org.traccar.model.BaseModel;
import org.traccar.model.Device;
@@ -224,10 +223,6 @@ public class CacheManager implements BroadcastInterface {
} finally {
lock.writeLock().unlock();
}
-
- if (object instanceof Device) {
- invalidateDeviceGeofences((Device) object);
- }
}
}
@@ -304,8 +299,6 @@ public class CacheManager implements BroadcastInterface {
devicePositions.put(deviceId, storage.getObject(Position.class, new Request(
new Columns.All(), new Condition.Equals("id", "id", device.getPositionId()))));
}
-
- invalidateDeviceGeofences(device);
}
}
@@ -359,11 +352,4 @@ public class CacheManager implements BroadcastInterface {
}
}
- private void invalidateDeviceGeofences(Device device) {
- Position position = getPosition(device.getId());
- if (position != null) {
- device.setGeofenceIds(GeofenceUtil.getCurrentGeofences(config, this, position));
- }
- }
-
}