diff options
author | Anton Tananaev <anton@traccar.org> | 2022-06-14 18:50:48 -0700 |
---|---|---|
committer | Anton Tananaev <anton@traccar.org> | 2022-06-14 18:50:48 -0700 |
commit | 0d5c7606c73d84aab44f69936ab62afa0b68939b (patch) | |
tree | 4e9cbebe1ddcd3e3b75a15bee8e23d05374ca434 /src/main/java/org/traccar/notification | |
parent | 415ba3ddb0f770b829c997beb3e575ffb6e195ec (diff) | |
download | trackermap-server-0d5c7606c73d84aab44f69936ab62afa0b68939b.tar.gz trackermap-server-0d5c7606c73d84aab44f69936ab62afa0b68939b.tar.bz2 trackermap-server-0d5c7606c73d84aab44f69936ab62afa0b68939b.zip |
Refactor notification manager
Diffstat (limited to 'src/main/java/org/traccar/notification')
-rw-r--r-- | src/main/java/org/traccar/notification/EventForwarder.java | 14 | ||||
-rw-r--r-- | src/main/java/org/traccar/notification/NotificatorManager.java | 51 |
2 files changed, 28 insertions, 37 deletions
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()); } } |