From 70f684945754b6bdadd2bf038ab955d93a95977d Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 28 Jul 2016 13:44:05 +0500 Subject: Organized server cache Moved lookupAttribute function to DeviceManager --- src/org/traccar/api/resource/ServerResource.java | 5 ++- src/org/traccar/database/DeviceManager.java | 32 ++++++++++++++++++ src/org/traccar/database/PermissionsManager.java | 11 ++++++- src/org/traccar/events/OverspeedEventHandler.java | 40 +++-------------------- 4 files changed, 49 insertions(+), 39 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/api/resource/ServerResource.java b/src/org/traccar/api/resource/ServerResource.java index 9e42687ab..0ca0d62aa 100644 --- a/src/org/traccar/api/resource/ServerResource.java +++ b/src/org/traccar/api/resource/ServerResource.java @@ -37,14 +37,13 @@ public class ServerResource extends BaseResource { @PermitAll @GET public Server get() throws SQLException { - return Context.getDataManager().getServer(); + return Context.getPermissionsManager().getServer(); } @PUT public Response update(Server entity) throws SQLException { Context.getPermissionsManager().checkAdmin(getUserId()); - Context.getDataManager().updateServer(entity); - Context.getPermissionsManager().refresh(); + Context.getPermissionsManager().updateServer(entity); return Response.ok(entity).build(); } diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index 5a0c87985..3476139f2 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -32,6 +32,7 @@ import org.traccar.helper.Log; import org.traccar.model.Device; import org.traccar.model.Group; import org.traccar.model.Position; +import org.traccar.model.Server; public class DeviceManager implements IdentityManager { @@ -40,6 +41,7 @@ public class DeviceManager implements IdentityManager { private final Config config; private final DataManager dataManager; private final long dataRefreshDelay; + private boolean lookupGroupsAttribute; private Map devicesById; private Map devicesByUniqueId; @@ -54,6 +56,7 @@ public class DeviceManager implements IdentityManager { this.dataManager = dataManager; this.config = Context.getConfig(); dataRefreshDelay = config.getLong("database.refreshDelay", DEFAULT_REFRESH_DELAY) * 1000; + lookupGroupsAttribute = config.getBoolean("deviceManager.lookupGroupsAttribute"); if (dataManager != null) { try { updateGroupCache(true); @@ -312,4 +315,33 @@ public class DeviceManager implements IdentityManager { dataManager.removeGroup(groupId); groupsById.remove(groupId); } + + public String lookupAttribute(long deviceId, String attributeName) { + String result = null; + Device device = getDeviceById(deviceId); + if (device != null) { + if (device.getAttributes().containsKey(attributeName)) { + result = (String) device.getAttributes().get(attributeName); + } + if (result == null && lookupGroupsAttribute) { + long groupId = device.getGroupId(); + while (groupId != 0) { + if (getGroupById(groupId) != null) { + result = (String) getGroupById(groupId).getAttributes().get(attributeName); + if (result != null) { + break; + } + groupId = getGroupById(groupId).getGroupId(); + } else { + groupId = 0; + } + } + } + if (result == null) { + Server server = Context.getPermissionsManager().getServer(); + result = (String) server.getAttributes().get(attributeName); + } + } + return result; + } } diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index d786dcc4e..92fcc3ebd 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -34,7 +34,7 @@ public class PermissionsManager { private final DataManager dataManager; - private Server server; + private volatile Server server; private final Map users = new HashMap<>(); @@ -153,4 +153,13 @@ public class PermissionsManager { } } + public Server getServer() { + return server; + } + + public void updateServer(Server server) throws SQLException { + dataManager.updateServer(server); + this.server = server; + } + } diff --git a/src/org/traccar/events/OverspeedEventHandler.java b/src/org/traccar/events/OverspeedEventHandler.java index 0328b9109..a1efbde48 100644 --- a/src/org/traccar/events/OverspeedEventHandler.java +++ b/src/org/traccar/events/OverspeedEventHandler.java @@ -15,7 +15,6 @@ */ package org.traccar.events; -import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; @@ -24,19 +23,15 @@ import org.traccar.Context; import org.traccar.model.Device; import org.traccar.model.Event; import org.traccar.model.Position; -import org.traccar.model.Server; -import org.traccar.helper.Log; import org.traccar.helper.UnitsConverter; public class OverspeedEventHandler extends BaseEventHandler { public static final String ATTRIBUTE_SPEED_LIMIT = "speedLimit"; - private boolean checkGroups; private boolean riseOnce; public OverspeedEventHandler() { - checkGroups = Context.getConfig().getBoolean("event.overspeed.groupsEnabled"); riseOnce = Context.getConfig().getBoolean("event.overspeed.riseOnce"); } @@ -54,37 +49,12 @@ public class OverspeedEventHandler extends BaseEventHandler { Collection events = new ArrayList<>(); double speed = position.getSpeed(); double speedLimit = 0; - - if (device.getAttributes().containsKey(ATTRIBUTE_SPEED_LIMIT)) { - speedLimit = Double.parseDouble((String) device.getAttributes().get(ATTRIBUTE_SPEED_LIMIT)); - } - if (speedLimit == 0 && checkGroups) { - long groupId = device.getGroupId(); - while (groupId != 0) { - if (Context.getDeviceManager().getGroupById(groupId).getAttributes() - .containsKey(ATTRIBUTE_SPEED_LIMIT)) { - speedLimit = Double.parseDouble((String) Context.getDeviceManager().getGroupById(groupId) - .getAttributes().get(ATTRIBUTE_SPEED_LIMIT)); - if (speedLimit != 0) { - break; - } - } - if (Context.getDeviceManager().getGroupById(groupId) != null) { - groupId = Context.getDeviceManager().getGroupById(groupId).getGroupId(); - } else { - groupId = 0; - } - } + String speedLimitAttribute = Context.getDeviceManager().lookupAttribute(device.getId(), ATTRIBUTE_SPEED_LIMIT); + if (speedLimitAttribute != null) { + speedLimit = Double.parseDouble(speedLimitAttribute); } if (speedLimit == 0) { - try { - Server server = Context.getDataManager().getServer(); - if (server.getAttributes().containsKey(ATTRIBUTE_SPEED_LIMIT)) { - speedLimit = Double.parseDouble((String) server.getAttributes().get(ATTRIBUTE_SPEED_LIMIT)); - } - } catch (SQLException error) { - Log.warning(error); - } + return null; } double oldSpeed = 0; if (riseOnce) { @@ -94,7 +64,7 @@ public class OverspeedEventHandler extends BaseEventHandler { } } speedLimit = UnitsConverter.knotsFromKph(speedLimit); - if (speedLimit != 0 && speed > speedLimit && oldSpeed <= speedLimit) { + if (speed > speedLimit && oldSpeed <= speedLimit) { events.add(new Event(Event.TYPE_DEVICE_OVERSPEED, position.getDeviceId(), position.getId())); } return events; -- cgit v1.2.3