aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-06-14 18:50:48 -0700
committerAnton Tananaev <anton@traccar.org>2022-06-14 18:50:48 -0700
commit0d5c7606c73d84aab44f69936ab62afa0b68939b (patch)
tree4e9cbebe1ddcd3e3b75a15bee8e23d05374ca434
parent415ba3ddb0f770b829c997beb3e575ffb6e195ec (diff)
downloadtrackermap-server-0d5c7606c73d84aab44f69936ab62afa0b68939b.tar.gz
trackermap-server-0d5c7606c73d84aab44f69936ab62afa0b68939b.tar.bz2
trackermap-server-0d5c7606c73d84aab44f69936ab62afa0b68939b.zip
Refactor notification manager
-rw-r--r--src/main/java/org/traccar/Context.java26
-rw-r--r--src/main/java/org/traccar/MainModule.java8
-rw-r--r--src/main/java/org/traccar/api/BaseObjectResource.java10
-rw-r--r--src/main/java/org/traccar/api/resource/NotificationResource.java22
-rw-r--r--src/main/java/org/traccar/database/DeviceManager.java1
-rw-r--r--src/main/java/org/traccar/database/ExtendedObjectManager.java30
-rw-r--r--src/main/java/org/traccar/database/NotificationManager.java126
-rw-r--r--src/main/java/org/traccar/database/PermissionsManager.java51
-rw-r--r--src/main/java/org/traccar/handler/events/BaseEventHandler.java17
-rw-r--r--src/main/java/org/traccar/notification/EventForwarder.java14
-rw-r--r--src/main/java/org/traccar/notification/NotificatorManager.java51
-rw-r--r--src/main/java/org/traccar/session/ConnectionManager.java9
12 files changed, 116 insertions, 249 deletions
diff --git a/src/main/java/org/traccar/Context.java b/src/main/java/org/traccar/Context.java
index a66cde464..51c420390 100644
--- a/src/main/java/org/traccar/Context.java
+++ b/src/main/java/org/traccar/Context.java
@@ -22,20 +22,14 @@ import org.traccar.database.DataManager;
import org.traccar.database.DeviceManager;
import org.traccar.database.GroupsManager;
import org.traccar.database.IdentityManager;
-import org.traccar.database.NotificationManager;
import org.traccar.database.PermissionsManager;
import org.traccar.database.UsersManager;
-import org.traccar.geocoder.Geocoder;
import org.traccar.helper.Log;
import org.traccar.model.BaseModel;
import org.traccar.model.Device;
import org.traccar.model.Group;
-import org.traccar.model.Notification;
import org.traccar.model.User;
-import org.traccar.notification.EventForwarder;
-import org.traccar.notification.NotificatorManager;
import org.traccar.session.ConnectionManager;
-import org.traccar.session.cache.CacheManager;
public final class Context {
@@ -84,12 +78,6 @@ public final class Context {
return permissionsManager;
}
- private static NotificationManager notificationManager;
-
- public static NotificationManager getNotificationManager() {
- return notificationManager;
- }
-
public static void init(String configFile) throws Exception {
try {
@@ -116,18 +104,6 @@ public final class Context {
permissionsManager = new PermissionsManager(dataManager, usersManager);
- initEventsModule();
-
- }
-
- private static void initEventsModule() {
-
- notificationManager = new NotificationManager(
- dataManager,
- Main.getInjector().getInstance(CacheManager.class),
- Main.getInjector().getInstance(EventForwarder.class),
- Main.getInjector().getInstance(NotificatorManager.class),
- Main.getInjector().getInstance(Geocoder.class));
}
public static <T extends BaseModel> BaseObjectManager<T> getManager(Class<T> clazz) {
@@ -137,8 +113,6 @@ public final class Context {
return (BaseObjectManager<T>) groupsManager;
} else if (clazz.equals(User.class)) {
return (BaseObjectManager<T>) usersManager;
- } else if (clazz.equals(Notification.class)) {
- return (BaseObjectManager<T>) notificationManager;
}
return null;
}
diff --git a/src/main/java/org/traccar/MainModule.java b/src/main/java/org/traccar/MainModule.java
index 306bc9e7b..dd496e5a4 100644
--- a/src/main/java/org/traccar/MainModule.java
+++ b/src/main/java/org/traccar/MainModule.java
@@ -31,7 +31,6 @@ import org.traccar.broadcast.BroadcastService;
import org.traccar.config.Config;
import org.traccar.config.Keys;
import org.traccar.database.LdapProvider;
-import org.traccar.database.NotificationManager;
import org.traccar.database.UsersManager;
import org.traccar.helper.SanitizerModule;
import org.traccar.notification.EventForwarder;
@@ -125,11 +124,6 @@ public class MainModule extends AbstractModule {
}
@Provides
- public static NotificationManager provideNotificationManager() {
- return Context.getNotificationManager();
- }
-
- @Provides
public static IdentityManager provideIdentityManager() {
return Context.getIdentityManager();
}
@@ -301,7 +295,7 @@ public class MainModule extends AbstractModule {
public static EventForwarder provideEventForwarder(
Config config, Client client, CacheManager cacheManager, UsersManager usersManager) {
if (config.hasKey(Keys.EVENT_FORWARD_URL)) {
- return new EventForwarder(config, client, cacheManager, usersManager);
+ return new EventForwarder(config, client, cacheManager);
}
return null;
}
diff --git a/src/main/java/org/traccar/api/BaseObjectResource.java b/src/main/java/org/traccar/api/BaseObjectResource.java
index cc930c591..aa777f3f6 100644
--- a/src/main/java/org/traccar/api/BaseObjectResource.java
+++ b/src/main/java/org/traccar/api/BaseObjectResource.java
@@ -22,7 +22,6 @@ import org.traccar.database.ExtendedObjectManager;
import org.traccar.database.SimpleObjectManager;
import org.traccar.helper.LogAction;
import org.traccar.model.BaseModel;
-import org.traccar.model.Calendar;
import org.traccar.model.Device;
import org.traccar.model.Group;
import org.traccar.model.Permission;
@@ -87,7 +86,6 @@ public abstract class BaseObjectResource<T extends BaseModel> extends BaseResour
((SimpleObjectManager<T>) manager).refreshUserItems();
} else if (baseClass.equals(Group.class) || baseClass.equals(Device.class)) {
Context.getPermissionsManager().refreshDeviceAndGroupPermissions();
- Context.getPermissionsManager().refreshAllExtendedPermissions();
}
return Response.ok(entity).build();
}
@@ -112,7 +110,6 @@ public abstract class BaseObjectResource<T extends BaseModel> extends BaseResour
if (baseClass.equals(Group.class) || baseClass.equals(Device.class)) {
Context.getPermissionsManager().refreshDeviceAndGroupPermissions();
- Context.getPermissionsManager().refreshAllExtendedPermissions();
}
return Response.ok(entity).build();
}
@@ -145,13 +142,6 @@ public abstract class BaseObjectResource<T extends BaseModel> extends BaseResour
Context.getDeviceManager().updateDeviceCache(true);
}
Context.getPermissionsManager().refreshDeviceAndGroupPermissions();
- if (baseClass.equals(User.class)) {
- Context.getPermissionsManager().refreshAllUsersPermissions();
- } else {
- Context.getPermissionsManager().refreshAllExtendedPermissions();
- }
- } else if (baseClass.equals(Calendar.class)) {
- Context.getNotificationManager().refreshItems();
}
return Response.noContent().build();
}
diff --git a/src/main/java/org/traccar/api/resource/NotificationResource.java b/src/main/java/org/traccar/api/resource/NotificationResource.java
index 0a95b257a..a42de687d 100644
--- a/src/main/java/org/traccar/api/resource/NotificationResource.java
+++ b/src/main/java/org/traccar/api/resource/NotificationResource.java
@@ -15,7 +15,11 @@
*/
package org.traccar.api.resource;
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
@@ -27,7 +31,8 @@ import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
-import org.traccar.Context;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.traccar.api.ExtendedObjectResource;
import org.traccar.model.Event;
import org.traccar.model.Notification;
@@ -42,6 +47,8 @@ import org.traccar.storage.StorageException;
@Consumes(MediaType.APPLICATION_JSON)
public class NotificationResource extends ExtendedObjectResource<Notification> {
+ private static final Logger LOGGER = LoggerFactory.getLogger(NotificationResource.class);
+
@Inject
private NotificatorManager notificatorManager;
@@ -52,7 +59,18 @@ public class NotificationResource extends ExtendedObjectResource<Notification> {
@GET
@Path("types")
public Collection<Typed> get() {
- return Context.getNotificationManager().getAllNotificationTypes();
+ Set<Typed> types = new HashSet<>();
+ Field[] fields = Event.class.getDeclaredFields();
+ for (Field field : fields) {
+ if (Modifier.isStatic(field.getModifiers()) && field.getName().startsWith("TYPE_")) {
+ try {
+ types.add(new Typed(field.get(null).toString()));
+ } catch (IllegalArgumentException | IllegalAccessException error) {
+ LOGGER.warn("Get event types error", error);
+ }
+ }
+ }
+ return types;
}
@GET
diff --git a/src/main/java/org/traccar/database/DeviceManager.java b/src/main/java/org/traccar/database/DeviceManager.java
index 973cf3f68..e1d6ad1dd 100644
--- a/src/main/java/org/traccar/database/DeviceManager.java
+++ b/src/main/java/org/traccar/database/DeviceManager.java
@@ -91,7 +91,6 @@ public class DeviceManager extends BaseObjectManager<Device> implements Identity
if (defaultGroupId != 0) {
Context.getPermissionsManager().refreshDeviceAndGroupPermissions();
- Context.getPermissionsManager().refreshAllExtendedPermissions();
}
return device;
diff --git a/src/main/java/org/traccar/database/ExtendedObjectManager.java b/src/main/java/org/traccar/database/ExtendedObjectManager.java
index 006ed47b2..fe0ebb96e 100644
--- a/src/main/java/org/traccar/database/ExtendedObjectManager.java
+++ b/src/main/java/org/traccar/database/ExtendedObjectManager.java
@@ -44,34 +44,6 @@ public abstract class ExtendedObjectManager<T extends BaseModel> extends SimpleO
refreshExtendedPermissions();
}
- public final Set<Long> getGroupItems(long groupId) {
- try {
- readLock();
- Set<Long> result = groupItems.get(groupId);
- if (result != null) {
- return new HashSet<>(result);
- } else {
- return new HashSet<>();
- }
- } finally {
- readUnlock();
- }
- }
-
- public final Set<Long> getDeviceItems(long deviceId) {
- try {
- readLock();
- Set<Long> result = deviceItems.get(deviceId);
- if (result != null) {
- return new HashSet<>(result);
- } else {
- return new HashSet<>();
- }
- } finally {
- readUnlock();
- }
- }
-
public Set<Long> getAllDeviceItems(long deviceId) {
try {
readLock();
@@ -122,7 +94,7 @@ public abstract class ExtendedObjectManager<T extends BaseModel> extends SimpleO
.add(devicePermission.getPropertyId());
}
- for (Device device : Context.getDeviceManager().getAllDevices()) {
+ for (Device device : getDataManager().getObjects(Device.class)) {
long groupId = device.getGroupId();
while (groupId > 0) {
deviceItemsWithGroups
diff --git a/src/main/java/org/traccar/database/NotificationManager.java b/src/main/java/org/traccar/database/NotificationManager.java
index 3c87aacb2..46b58dd75 100644
--- a/src/main/java/org/traccar/database/NotificationManager.java
+++ b/src/main/java/org/traccar/database/NotificationManager.java
@@ -16,39 +16,36 @@
*/
package org.traccar.database;
-import java.lang.reflect.Field;
-import java.lang.reflect.Modifier;
-import java.util.Arrays;
-import java.util.Date;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Set;
-
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.traccar.Context;
+import org.traccar.config.Config;
import org.traccar.config.Keys;
import org.traccar.geocoder.Geocoder;
import org.traccar.model.Calendar;
import org.traccar.model.Event;
import org.traccar.model.Notification;
import org.traccar.model.Position;
-import org.traccar.model.Typed;
-import org.traccar.model.User;
import org.traccar.notification.EventForwarder;
import org.traccar.notification.MessageException;
import org.traccar.notification.NotificatorManager;
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.Request;
import javax.annotation.Nullable;
+import javax.inject.Inject;
+import java.util.Arrays;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.stream.Collectors;
-public class NotificationManager extends ExtendedObjectManager<Notification> {
+public class NotificationManager {
private static final Logger LOGGER = LoggerFactory.getLogger(NotificationManager.class);
+ private final Storage storage;
private final CacheManager cacheManager;
private final EventForwarder eventForwarder;
private final NotificatorManager notificatorManager;
@@ -56,81 +53,67 @@ public class NotificationManager extends ExtendedObjectManager<Notification> {
private final boolean geocodeOnRequest;
+ @Inject
public NotificationManager(
- DataManager dataManager, CacheManager cacheManager, @Nullable EventForwarder eventForwarder,
+ Config config, Storage storage, CacheManager cacheManager, @Nullable EventForwarder eventForwarder,
NotificatorManager notificatorManager, @Nullable Geocoder geocoder) {
- super(dataManager, Notification.class);
+ this.storage = storage;
this.cacheManager = cacheManager;
this.eventForwarder = eventForwarder;
this.notificatorManager = notificatorManager;
this.geocoder = geocoder;
- geocodeOnRequest = Context.getConfig().getBoolean(Keys.GEOCODER_ON_REQUEST);
- }
-
- private Set<Long> getEffectiveNotifications(long userId, long deviceId, Date time) {
- Set<Long> result = new HashSet<>();
- Set<Long> deviceNotifications = getAllDeviceItems(deviceId);
- for (long itemId : getUserItems(userId)) {
- if (getById(itemId).getAlways() || deviceNotifications.contains(itemId)) {
- long calendarId = getById(itemId).getCalendarId();
- Calendar calendar = calendarId != 0 ? cacheManager.getObject(Calendar.class, calendarId) : null;
- if (calendar == null || calendar.checkMoment(time)) {
- result.add(itemId);
- }
- }
- }
- return result;
+ geocodeOnRequest = config.getBoolean(Keys.GEOCODER_ON_REQUEST);
}
public void updateEvent(Event event, Position position) {
try {
- getDataManager().addObject(event);
+ event.setId(storage.addObject(event, new Request(new Columns.Exclude("id"))));
} catch (StorageException error) {
LOGGER.warn("Event save error", error);
}
- long deviceId = event.getDeviceId();
- Set<Long> users = Context.getPermissionsManager().getDeviceUsers(deviceId);
- Set<Long> usersToForward = new HashSet<>();
- for (long userId : users) {
- usersToForward.add(userId);
- final Set<String> notificators = new HashSet<>();
- for (long notificationId : getEffectiveNotifications(userId, deviceId, event.getEventTime())) {
- Notification notification = getById(notificationId);
- if (getById(notificationId).getType().equals(event.getType())) {
- boolean filter = false;
+ var notifications = cacheManager.getDeviceObjects(event.getDeviceId(), Notification.class).stream()
+ .filter(notification -> notification.getType().equals(event.getType()))
+ .filter(notification -> {
if (event.getType().equals(Event.TYPE_ALARM)) {
String alarmsAttribute = notification.getString("alarms");
- if (alarmsAttribute == null) {
- filter = true;
- } else {
- List<String> alarms = Arrays.asList(alarmsAttribute.split(","));
- filter = !alarms.contains(event.getString(Position.KEY_ALARM));
+ if (alarmsAttribute != null) {
+ return Arrays.asList(alarmsAttribute.split(","))
+ .contains(event.getString(Position.KEY_ALARM));
}
+ return false;
}
- if (!filter) {
- notificators.addAll(notification.getNotificatorsTypes());
- }
- }
- }
+ return true;
+ })
+ .filter(notification -> {
+ long calendarId = notification.getCalendarId();
+ Calendar calendar = calendarId != 0 ? cacheManager.getObject(Calendar.class, calendarId) : null;
+ return calendar == null || calendar.checkMoment(event.getEventTime());
+ })
+ .collect(Collectors.toUnmodifiableList());
+ if (!notifications.isEmpty()) {
if (position != null && position.getAddress() == null && geocodeOnRequest && geocoder != null) {
position.setAddress(geocoder.getAddress(position.getLatitude(), position.getLongitude(), null));
}
- User user = Context.getUsersManager().getById(userId);
- new Thread(() -> {
- for (String notificator : notificators) {
- try {
- notificatorManager.getNotificator(notificator).send(user, event, position);
- } catch (MessageException | InterruptedException exception) {
- LOGGER.warn("Notification failed", exception);
- }
- }
- }).start();
+ notifications.forEach(notification -> {
+ cacheManager.getNotificationUsers(notification.getId()).forEach(user -> {
+ new Thread(() -> {
+ for (String notificator : notification.getNotificatorsTypes()) {
+ try {
+ notificatorManager.getNotificator(notificator).send(user, event, position);
+ } catch (MessageException | InterruptedException exception) {
+ LOGGER.warn("Notification failed", exception);
+ }
+ }
+ }).start();
+ });
+ });
}
+
if (eventForwarder != null) {
- eventForwarder.forwardEvent(event, position, usersToForward);
+ eventForwarder.forwardEvent(event, position);
}
}
@@ -139,19 +122,4 @@ public class NotificationManager extends ExtendedObjectManager<Notification> {
updateEvent(event.getKey(), event.getValue());
}
}
-
- public Set<Typed> getAllNotificationTypes() {
- Set<Typed> types = new HashSet<>();
- Field[] fields = Event.class.getDeclaredFields();
- for (Field field : fields) {
- if (Modifier.isStatic(field.getModifiers()) && field.getName().startsWith("TYPE_")) {
- try {
- types.add(new Typed(field.get(null).toString()));
- } catch (IllegalArgumentException | IllegalAccessException error) {
- LOGGER.warn("Get event types error", error);
- }
- }
- }
- return types;
- }
}
diff --git a/src/main/java/org/traccar/database/PermissionsManager.java b/src/main/java/org/traccar/database/PermissionsManager.java
index 3c74c0049..f34810439 100644
--- a/src/main/java/org/traccar/database/PermissionsManager.java
+++ b/src/main/java/org/traccar/database/PermissionsManager.java
@@ -22,7 +22,6 @@ import org.traccar.model.BaseModel;
import org.traccar.model.Device;
import org.traccar.model.Group;
import org.traccar.model.ManagedUser;
-import org.traccar.model.Notification;
import org.traccar.model.Permission;
import org.traccar.model.Server;
import org.traccar.model.User;
@@ -237,31 +236,11 @@ public class PermissionsManager {
}
}
- public void checkDeviceLimit(long userId) throws SecurityException {
- int deviceLimit = getUser(userId).getDeviceLimit();
- if (deviceLimit != -1) {
- int deviceCount;
- if (getUserManager(userId)) {
- deviceCount = Context.getDeviceManager().getAllManagedItems(userId).size();
- } else {
- deviceCount = Context.getDeviceManager().getAllUserItems(userId).size();
- }
- if (deviceCount >= deviceLimit) {
- throw new SecurityException("User device limit reached");
- }
- }
- }
-
public boolean getUserReadonly(long userId) {
User user = getUser(userId);
return user != null && user.getReadonly();
}
- public boolean getUserLimitCommands(long userId) {
- User user = getUser(userId);
- return user != null && user.getLimitCommands();
- }
-
public void checkReadonly(long userId) throws SecurityException {
if (!getUserAdmin(userId) && (server.getReadonly() || getUserReadonly(userId))) {
throw new SecurityException("Account is readonly");
@@ -313,18 +292,6 @@ public class PermissionsManager {
}
}
- public void checkGroup(long userId, long groupId) throws SecurityException {
- if (!getGroupPermissions(userId).contains(groupId) && !getUserAdmin(userId)) {
- checkManager(userId);
- for (long managedUserId : usersManager.getUserItems(userId)) {
- if (getGroupPermissions(managedUserId).contains(groupId)) {
- return;
- }
- }
- throw new SecurityException("Group access denied");
- }
- }
-
public void checkDevice(long userId, long deviceId) throws SecurityException {
if (!Context.getDeviceManager().getUserItems(userId).contains(deviceId) && !getUserAdmin(userId)) {
checkManager(userId);
@@ -354,31 +321,13 @@ public class PermissionsManager {
}
}
- public void refreshAllUsersPermissions() {
- if (Context.getNotificationManager() != null) {
- Context.getNotificationManager().refreshUserItems();
- }
- }
-
- public void refreshAllExtendedPermissions() {
- }
-
public void refreshPermissions(Permission permission) {
if (permission.getOwnerClass().equals(User.class)) {
if (permission.getPropertyClass().equals(Device.class)
|| permission.getPropertyClass().equals(Group.class)) {
refreshDeviceAndGroupPermissions();
- refreshAllExtendedPermissions();
} else if (permission.getPropertyClass().equals(ManagedUser.class)) {
usersManager.refreshUserItems();
- } else if (permission.getPropertyClass().equals(Notification.class)
- && Context.getNotificationManager() != null) {
- Context.getNotificationManager().refreshUserItems();
- }
- } else if (permission.getOwnerClass().equals(Device.class) || permission.getOwnerClass().equals(Group.class)) {
- if (permission.getPropertyClass().equals(Notification.class)
- && Context.getNotificationManager() != null) {
- Context.getNotificationManager().refreshExtendedPermissions();
}
}
}
diff --git a/src/main/java/org/traccar/handler/events/BaseEventHandler.java b/src/main/java/org/traccar/handler/events/BaseEventHandler.java
index 41f677f6c..f7199f6dc 100644
--- a/src/main/java/org/traccar/handler/events/BaseEventHandler.java
+++ b/src/main/java/org/traccar/handler/events/BaseEventHandler.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2016 Anton Tananaev (anton@traccar.org)
+ * Copyright 2016 - 2022 Anton Tananaev (anton@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,17 +18,26 @@ package org.traccar.handler.events;
import java.util.Map;
import org.traccar.BaseDataHandler;
-import org.traccar.Context;
+import org.traccar.database.NotificationManager;
import org.traccar.model.Event;
import org.traccar.model.Position;
+import javax.inject.Inject;
+
public abstract class BaseEventHandler extends BaseDataHandler {
+ private NotificationManager notificationManager;
+
+ @Inject
+ public void setNotificationManager(NotificationManager notificationManager) {
+ this.notificationManager = notificationManager;
+ }
+
@Override
protected Position handlePosition(Position position) {
Map<Event, Position> events = analyzePosition(position);
- if (events != null && Context.getNotificationManager() != null) {
- Context.getNotificationManager().updateEvents(events);
+ if (events != null) {
+ notificationManager.updateEvents(events);
}
return position;
}
diff --git a/src/main/java/org/traccar/notification/EventForwarder.java b/src/main/java/org/traccar/notification/EventForwarder.java
index 5afff1b7b..279d5e678 100644
--- a/src/main/java/org/traccar/notification/EventForwarder.java
+++ b/src/main/java/org/traccar/notification/EventForwarder.java
@@ -19,7 +19,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.traccar.config.Config;
import org.traccar.config.Keys;
-import org.traccar.database.UsersManager;
import org.traccar.model.Device;
import org.traccar.model.Event;
import org.traccar.model.Geofence;
@@ -33,7 +32,6 @@ import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.InvocationCallback;
import java.util.HashMap;
import java.util.Map;
-import java.util.Set;
public class EventForwarder {
@@ -44,12 +42,10 @@ public class EventForwarder {
private final Client client;
private final CacheManager cacheManager;
- private final UsersManager usersManager;
- public EventForwarder(Config config, Client client, CacheManager cacheManager, UsersManager usersManager) {
+ public EventForwarder(Config config, Client client, CacheManager cacheManager) {
this.client = client;
this.cacheManager = cacheManager;
- this.usersManager = usersManager;
url = config.getString(Keys.EVENT_FORWARD_URL);
header = config.getString(Keys.EVENT_FORWARD_HEADERS);
}
@@ -59,9 +55,8 @@ public class EventForwarder {
private static final String KEY_GEOFENCE = "geofence";
private static final String KEY_DEVICE = "device";
private static final String KEY_MAINTENANCE = "maintenance";
- private static final String KEY_USERS = "users";
- public final void forwardEvent(Event event, Position position, Set<Long> users) {
+ public final void forwardEvent(Event event, Position position) {
Invocation.Builder requestBuilder = client.target(url).request();
@@ -74,7 +69,7 @@ public class EventForwarder {
LOGGER.debug("Event forwarding initiated");
requestBuilder.async().post(
- Entity.json(preparePayload(event, position, users)), new InvocationCallback<Object>() {
+ Entity.json(preparePayload(event, position)), new InvocationCallback<Object>() {
@Override
public void completed(Object o) {
LOGGER.debug("Event forwarding succeeded");
@@ -87,7 +82,7 @@ public class EventForwarder {
});
}
- protected Map<String, Object> preparePayload(Event event, Position position, Set<Long> users) {
+ protected Map<String, Object> preparePayload(Event event, Position position) {
Map<String, Object> data = new HashMap<>();
data.put(KEY_EVENT, event);
if (position != null) {
@@ -109,7 +104,6 @@ public class EventForwarder {
data.put(KEY_MAINTENANCE, maintenance);
}
}
- data.put(KEY_USERS, usersManager.getItems(users));
return data;
}
diff --git a/src/main/java/org/traccar/notification/NotificatorManager.java b/src/main/java/org/traccar/notification/NotificatorManager.java
index d6ebb2c4a..1d9f4f423 100644
--- a/src/main/java/org/traccar/notification/NotificatorManager.java
+++ b/src/main/java/org/traccar/notification/NotificatorManager.java
@@ -16,29 +16,29 @@
*/
package org.traccar.notification;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-
+import com.google.inject.Injector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.traccar.Main;
import org.traccar.config.Config;
import org.traccar.config.Keys;
import org.traccar.model.Typed;
+import org.traccar.notificators.Notificator;
import org.traccar.notificators.NotificatorFirebase;
import org.traccar.notificators.NotificatorMail;
import org.traccar.notificators.NotificatorNull;
-import org.traccar.notificators.Notificator;
+import org.traccar.notificators.NotificatorPushover;
import org.traccar.notificators.NotificatorSms;
+import org.traccar.notificators.NotificatorTelegram;
import org.traccar.notificators.NotificatorTraccar;
import org.traccar.notificators.NotificatorWeb;
-import org.traccar.notificators.NotificatorTelegram;
-import org.traccar.notificators.NotificatorPushover;
import javax.inject.Inject;
import javax.inject.Singleton;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
@Singleton
public class NotificatorManager {
@@ -54,36 +54,33 @@ public class NotificatorManager {
"telegram", NotificatorTelegram.class,
"pushover", NotificatorPushover.class);
- private final Map<String, Notificator> notificators = new HashMap<>();
+ private final Injector injector;
+
+ private final Set<String> types = new HashSet<>();
@Inject
- public NotificatorManager(Config config) {
+ public NotificatorManager(Injector injector, Config config) {
+ this.injector = injector;
String types = config.getString(Keys.NOTIFICATOR_TYPES);
if (types != null) {
- for (String type : types.split(",")) {
- var notificatorClass = NOTIFICATORS_ALL.get(type);
- if (notificatorClass != null) {
- notificators.put(type, Main.getInjector().getInstance(notificatorClass));
- }
- }
+ this.types.addAll(Arrays.asList(types.split(",")));
}
}
public Notificator getNotificator(String type) {
- final Notificator notificator = notificators.get(type);
- if (notificator == null) {
- LOGGER.warn("No notificator configured for type : " + type);
- return new NotificatorNull();
+ var clazz = NOTIFICATORS_ALL.get(type);
+ if (clazz != null) {
+ var notificator = injector.getInstance(clazz);
+ if (notificator != null) {
+ return notificator;
+ }
}
- return notificator;
+ LOGGER.warn("Failed to get notificator {}", type);
+ return new NotificatorNull();
}
public Set<Typed> getAllNotificatorTypes() {
- Set<Typed> result = new HashSet<>();
- for (String notificator : notificators.keySet()) {
- result.add(new Typed(notificator));
- }
- return result;
+ return types.stream().map(Typed::new).collect(Collectors.toUnmodifiableSet());
}
}
diff --git a/src/main/java/org/traccar/session/ConnectionManager.java b/src/main/java/org/traccar/session/ConnectionManager.java
index ab3c36734..dc714379b 100644
--- a/src/main/java/org/traccar/session/ConnectionManager.java
+++ b/src/main/java/org/traccar/session/ConnectionManager.java
@@ -25,6 +25,7 @@ import org.traccar.Main;
import org.traccar.Protocol;
import org.traccar.config.Config;
import org.traccar.config.Keys;
+import org.traccar.database.NotificationManager;
import org.traccar.handler.events.MotionEventHandler;
import org.traccar.handler.events.OverspeedEventHandler;
import org.traccar.model.Device;
@@ -63,6 +64,7 @@ public class ConnectionManager {
private final Config config;
private final CacheManager cacheManager;
private final Storage storage;
+ private final NotificationManager notificationManager;
private final Timer timer;
private final Map<Long, Set<UpdateListener>> listeners = new ConcurrentHashMap<>();
@@ -70,10 +72,12 @@ public class ConnectionManager {
@Inject
public ConnectionManager(
- Config config, CacheManager cacheManager, Storage storage, Timer timer) {
+ Config config, CacheManager cacheManager, Storage storage,
+ NotificationManager notificationManager, Timer timer) {
this.config = config;
this.cacheManager = cacheManager;
this.storage = storage;
+ this.notificationManager = notificationManager;
this.timer = timer;
deviceTimeout = config.getLong(Keys.STATUS_TIMEOUT) * 1000;
updateDeviceState = config.getBoolean(Keys.STATUS_UPDATE_DEVICE_STATE);
@@ -163,7 +167,6 @@ public class ConnectionManager {
if (defaultGroupId != 0) {
Context.getPermissionsManager().refreshDeviceAndGroupPermissions();
- Context.getPermissionsManager().refreshAllExtendedPermissions();
}
return device;
@@ -236,7 +239,7 @@ public class ConnectionManager {
break;
}
events.put(new Event(eventType, deviceId), null);
- Context.getNotificationManager().updateEvents(events);
+ notificationManager.updateEvents(events);
}
Timeout timeout = timeouts.remove(deviceId);