aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/notificators
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/traccar/notificators')
-rw-r--r--src/main/java/org/traccar/notificators/Notificator.java23
-rw-r--r--src/main/java/org/traccar/notificators/NotificatorCommand.java62
-rw-r--r--src/main/java/org/traccar/notificators/NotificatorFirebase.java169
-rw-r--r--src/main/java/org/traccar/notificators/NotificatorMail.java29
-rw-r--r--src/main/java/org/traccar/notificators/NotificatorNull.java38
-rw-r--r--src/main/java/org/traccar/notificators/NotificatorPushover.java75
-rw-r--r--src/main/java/org/traccar/notificators/NotificatorSms.java42
-rw-r--r--src/main/java/org/traccar/notificators/NotificatorTelegram.java66
-rw-r--r--src/main/java/org/traccar/notificators/NotificatorTraccar.java124
-rw-r--r--src/main/java/org/traccar/notificators/NotificatorWeb.java40
10 files changed, 420 insertions, 248 deletions
diff --git a/src/main/java/org/traccar/notificators/Notificator.java b/src/main/java/org/traccar/notificators/Notificator.java
index dd888bae9..cf71141c0 100644
--- a/src/main/java/org/traccar/notificators/Notificator.java
+++ b/src/main/java/org/traccar/notificators/Notificator.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2018 Anton Tananaev (anton@traccar.org)
+ * Copyright 2018 - 2023 Anton Tananaev (anton@traccar.org)
* Copyright 2018 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,27 +16,14 @@
*/
package org.traccar.notificators;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import org.traccar.model.Event;
+import org.traccar.model.Notification;
import org.traccar.model.Position;
+import org.traccar.model.User;
import org.traccar.notification.MessageException;
-public abstract class Notificator {
+public interface Notificator {
- private static final Logger LOGGER = LoggerFactory.getLogger(Notificator.class);
-
- public void sendAsync(final long userId, final Event event, final Position position) {
- new Thread(() -> {
- try {
- sendSync(userId, event, position);
- } catch (MessageException | InterruptedException error) {
- LOGGER.warn("Event send error", error);
- }
- }).start();
- }
-
- public abstract void sendSync(long userId, Event event, Position position)
- throws MessageException, InterruptedException;
+ void send(Notification notification, User user, Event event, Position position) throws MessageException;
}
diff --git a/src/main/java/org/traccar/notificators/NotificatorCommand.java b/src/main/java/org/traccar/notificators/NotificatorCommand.java
new file mode 100644
index 000000000..5dd3313d4
--- /dev/null
+++ b/src/main/java/org/traccar/notificators/NotificatorCommand.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2023 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.notificators;
+
+import org.traccar.database.CommandsManager;
+import org.traccar.model.Command;
+import org.traccar.model.Event;
+import org.traccar.model.Notification;
+import org.traccar.model.Position;
+import org.traccar.model.User;
+import org.traccar.notification.MessageException;
+import org.traccar.storage.Storage;
+import org.traccar.storage.query.Columns;
+import org.traccar.storage.query.Condition;
+import org.traccar.storage.query.Request;
+
+import jakarta.inject.Inject;
+import jakarta.inject.Singleton;
+
+@Singleton
+public class NotificatorCommand implements Notificator {
+
+ private final Storage storage;
+ private final CommandsManager commandsManager;
+
+ @Inject
+ public NotificatorCommand(Storage storage, CommandsManager commandsManager) {
+ this.storage = storage;
+ this.commandsManager = commandsManager;
+ }
+
+ @Override
+ public void send(Notification notification, User user, Event event, Position position) throws MessageException {
+
+ if (notification == null || notification.getCommandId() <= 0) {
+ throw new MessageException("Saved command not provided");
+ }
+
+ try {
+ Command command = storage.getObject(Command.class, new Request(
+ new Columns.All(), new Condition.Equals("id", notification.getCommandId())));
+ command.setDeviceId(event.getDeviceId());
+ commandsManager.sendCommand(command);
+ } catch (Exception e) {
+ throw new MessageException(e);
+ }
+ }
+
+}
diff --git a/src/main/java/org/traccar/notificators/NotificatorFirebase.java b/src/main/java/org/traccar/notificators/NotificatorFirebase.java
index f91ec25a0..be95fb28e 100644
--- a/src/main/java/org/traccar/notificators/NotificatorFirebase.java
+++ b/src/main/java/org/traccar/notificators/NotificatorFirebase.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2018 - 2020 Anton Tananaev (anton@traccar.org)
+ * Copyright 2018 - 2023 Anton Tananaev (anton@traccar.org)
* Copyright 2018 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,88 +16,131 @@
*/
package org.traccar.notificators;
-import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.auth.oauth2.GoogleCredentials;
+import com.google.firebase.FirebaseApp;
+import com.google.firebase.FirebaseOptions;
+import com.google.firebase.messaging.AndroidConfig;
+import com.google.firebase.messaging.AndroidNotification;
+import com.google.firebase.messaging.ApnsConfig;
+import com.google.firebase.messaging.Aps;
+import com.google.firebase.messaging.FirebaseMessaging;
+import com.google.firebase.messaging.FirebaseMessagingException;
+import com.google.firebase.messaging.MessagingErrorCode;
+import com.google.firebase.messaging.MulticastMessage;
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.model.Event;
+import org.traccar.model.Notification;
import org.traccar.model.Position;
import org.traccar.model.User;
-import org.traccar.notification.NotificationMessage;
+import org.traccar.notification.MessageException;
import org.traccar.notification.NotificationFormatter;
-
-import javax.ws.rs.client.Entity;
-import javax.ws.rs.client.InvocationCallback;
-
-public class NotificatorFirebase extends Notificator {
+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 jakarta.inject.Inject;
+import jakarta.inject.Singleton;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.List;
+
+@Singleton
+public class NotificatorFirebase implements Notificator {
private static final Logger LOGGER = LoggerFactory.getLogger(NotificatorFirebase.class);
- private final String url;
- private final String key;
+ private final NotificationFormatter notificationFormatter;
+ private final Storage storage;
+ private final CacheManager cacheManager;
- public static class Notification {
- @JsonProperty("title")
- private String title;
- @JsonProperty("body")
- private String body;
- @JsonProperty("sound")
- private String sound;
- }
+ @Inject
+ public NotificatorFirebase(
+ Config config, NotificationFormatter notificationFormatter,
+ Storage storage, CacheManager cacheManager) throws IOException {
- public static class Message {
- @JsonProperty("registration_ids")
- private String[] tokens;
- @JsonProperty("notification")
- private Notification notification;
- }
+ this.notificationFormatter = notificationFormatter;
+ this.storage = storage;
+ this.cacheManager = cacheManager;
- public NotificatorFirebase() {
- this(
- "https://fcm.googleapis.com/fcm/send",
- Context.getConfig().getString(Keys.NOTIFICATOR_FIREBASE_KEY));
- }
+ InputStream serviceAccount = new ByteArrayInputStream(
+ config.getString(Keys.NOTIFICATOR_FIREBASE_SERVICE_ACCOUNT).getBytes());
- protected NotificatorFirebase(String url, String key) {
- this.url = url;
- this.key = key;
+ FirebaseOptions options = FirebaseOptions.builder()
+ .setCredentials(GoogleCredentials.fromStream(serviceAccount))
+ .build();
+
+ FirebaseApp.initializeApp(options);
}
@Override
- public void sendSync(long userId, Event event, Position position) {
- final User user = Context.getPermissionsManager().getUser(userId);
- if (user.getAttributes().containsKey("notificationTokens")) {
-
- NotificationMessage shortMessage = NotificationFormatter.formatMessage(userId, event, position, "short");
-
- Notification notification = new Notification();
- notification.title = shortMessage.getSubject();
- notification.body = shortMessage.getBody();
- notification.sound = "default";
-
- Message message = new Message();
- message.tokens = user.getString("notificationTokens").split("[, ]");
- message.notification = notification;
-
- Context.getClient().target(url).request()
- .header("Authorization", "key=" + key)
- .async().post(Entity.json(message), new InvocationCallback<Object>() {
- @Override
- public void completed(Object o) {
+ public void send(Notification notification, User user, Event event, Position position) throws MessageException {
+ if (user.hasAttribute("notificationTokens")) {
+
+ var shortMessage = notificationFormatter.formatMessage(user, event, position, "short");
+
+ List<String> registrationTokens = new ArrayList<>(
+ Arrays.asList(user.getString("notificationTokens").split("[, ]")));
+
+ MulticastMessage message = MulticastMessage.builder()
+ .setNotification(com.google.firebase.messaging.Notification.builder()
+ .setTitle(shortMessage.getSubject())
+ .setBody(shortMessage.getBody())
+ .build())
+ .setAndroidConfig(AndroidConfig.builder()
+ .setNotification(AndroidNotification.builder()
+ .setSound("default")
+ .build())
+ .build())
+ .setApnsConfig(ApnsConfig.builder()
+ .setAps(Aps.builder()
+ .setSound("default")
+ .build())
+ .build())
+ .addAllTokens(registrationTokens)
+ .putData("eventId", String.valueOf(event.getId()))
+ .build();
+
+ try {
+ var result = FirebaseMessaging.getInstance().sendMulticast(message);
+ List<String> failedTokens = new LinkedList<>();
+ var iterator = result.getResponses().listIterator();
+ while (iterator.hasNext()) {
+ int index = iterator.nextIndex();
+ var response = iterator.next();
+ if (!response.isSuccessful()) {
+ MessagingErrorCode error = response.getException().getMessagingErrorCode();
+ if (error == MessagingErrorCode.INVALID_ARGUMENT || error == MessagingErrorCode.UNREGISTERED) {
+ failedTokens.add(registrationTokens.get(index));
+ }
+ LOGGER.warn("Firebase user {} error", user.getId(), response.getException());
+ }
}
-
- @Override
- public void failed(Throwable throwable) {
- LOGGER.warn("Firebase notification error", throwable);
+ if (!failedTokens.isEmpty()) {
+ registrationTokens.removeAll(failedTokens);
+ if (registrationTokens.isEmpty()) {
+ user.getAttributes().remove("notificationTokens");
+ } else {
+ user.set("notificationTokens", String.join(",", registrationTokens));
+ }
+ storage.updateObject(user, new Request(
+ new Columns.Include("attributes"),
+ new Condition.Equals("id", user.getId())));
+ cacheManager.updateOrInvalidate(true, user);
}
- });
+ } catch (FirebaseMessagingException | StorageException e) {
+ LOGGER.warn("Firebase error", e);
+ }
}
}
- @Override
- public void sendAsync(long userId, Event event, Position position) {
- sendSync(userId, event, position);
- }
-
}
diff --git a/src/main/java/org/traccar/notificators/NotificatorMail.java b/src/main/java/org/traccar/notificators/NotificatorMail.java
index 9b5637ed8..3ab050686 100644
--- a/src/main/java/org/traccar/notificators/NotificatorMail.java
+++ b/src/main/java/org/traccar/notificators/NotificatorMail.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2016 - 2018 Anton Tananaev (anton@traccar.org)
+ * Copyright 2016 - 2023 Anton Tananaev (anton@traccar.org)
* Copyright 2017 - 2018 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,22 +16,35 @@
*/
package org.traccar.notificators;
-import org.traccar.Context;
+import org.traccar.mail.MailManager;
import org.traccar.model.Event;
+import org.traccar.model.Notification;
import org.traccar.model.Position;
-import org.traccar.notification.NotificationMessage;
+import org.traccar.model.User;
import org.traccar.notification.MessageException;
import org.traccar.notification.NotificationFormatter;
-import javax.mail.MessagingException;
+import jakarta.inject.Inject;
+import jakarta.inject.Singleton;
+import jakarta.mail.MessagingException;
-public final class NotificatorMail extends Notificator {
+@Singleton
+public class NotificatorMail implements Notificator {
+
+ private final MailManager mailManager;
+ private final NotificationFormatter notificationFormatter;
+
+ @Inject
+ public NotificatorMail(MailManager mailManager, NotificationFormatter notificationFormatter) {
+ this.mailManager = mailManager;
+ this.notificationFormatter = notificationFormatter;
+ }
@Override
- public void sendSync(long userId, Event event, Position position) throws MessageException {
+ public void send(Notification notification, User user, Event event, Position position) throws MessageException {
try {
- NotificationMessage fullMessage = NotificationFormatter.formatMessage(userId, event, position, "full");
- Context.getMailManager().sendMessage(userId, fullMessage.getSubject(), fullMessage.getBody());
+ var fullMessage = notificationFormatter.formatMessage(user, event, position, "full");
+ mailManager.sendMessage(user, false, fullMessage.getSubject(), fullMessage.getBody());
} catch (MessagingException e) {
throw new MessageException(e);
}
diff --git a/src/main/java/org/traccar/notificators/NotificatorNull.java b/src/main/java/org/traccar/notificators/NotificatorNull.java
deleted file mode 100644
index 9364336be..000000000
--- a/src/main/java/org/traccar/notificators/NotificatorNull.java
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * Copyright 2018 Anton Tananaev (anton@traccar.org)
- * Copyright 2018 Andrey Kunitsyn (andrey@traccar.org)
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.traccar.notificators;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.traccar.model.Event;
-import org.traccar.model.Position;
-
-public final class NotificatorNull extends Notificator {
-
- private static final Logger LOGGER = LoggerFactory.getLogger(NotificatorNull.class);
-
- @Override
- public void sendAsync(long userId, Event event, Position position) {
- LOGGER.warn("You are using null notificatior, please check your configuration, notification not sent");
- }
-
- @Override
- public void sendSync(long userId, Event event, Position position) {
- LOGGER.warn("You are using null notificatior, please check your configuration, notification not sent");
- }
-
-}
diff --git a/src/main/java/org/traccar/notificators/NotificatorPushover.java b/src/main/java/org/traccar/notificators/NotificatorPushover.java
index 456c2fe4f..9f2a8c94d 100644
--- a/src/main/java/org/traccar/notificators/NotificatorPushover.java
+++ b/src/main/java/org/traccar/notificators/NotificatorPushover.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2020 Anton Tananaev (anton@traccar.org)
+ * Copyright 2020 - 2023 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.
@@ -16,22 +16,24 @@
package org.traccar.notificators;
import com.fasterxml.jackson.annotation.JsonProperty;
-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.model.Event;
+import org.traccar.model.Notification;
import org.traccar.model.Position;
import org.traccar.model.User;
import org.traccar.notification.NotificationFormatter;
-import org.traccar.notification.NotificationMessage;
-import javax.ws.rs.client.Entity;
-import javax.ws.rs.client.InvocationCallback;
+import jakarta.inject.Inject;
+import jakarta.inject.Singleton;
+import jakarta.ws.rs.client.Client;
+import jakarta.ws.rs.client.Entity;
-public class NotificatorPushover extends Notificator {
+@Singleton
+public class NotificatorPushover implements Notificator {
- private static final Logger LOGGER = LoggerFactory.getLogger(NotificatorPushover.class);
+ private final NotificationFormatter notificationFormatter;
+ private final Client client;
private final String url;
private final String token;
@@ -50,58 +52,35 @@ public class NotificatorPushover extends Notificator {
private String message;
}
- public NotificatorPushover() {
+ @Inject
+ public NotificatorPushover(Config config, NotificationFormatter notificationFormatter, Client client) {
+ this.notificationFormatter = notificationFormatter;
+ this.client = client;
url = "https://api.pushover.net/1/messages.json";
- token = Context.getConfig().getString(Keys.NOTIFICATOR_PUSHOVER_TOKEN);
- user = Context.getConfig().getString(Keys.NOTIFICATOR_PUSHOVER_USER);
+ token = config.getString(Keys.NOTIFICATOR_PUSHOVER_TOKEN);
+ user = config.getString(Keys.NOTIFICATOR_PUSHOVER_USER);
}
@Override
- public void sendSync(long userId, Event event, Position position) {
+ public void send(Notification notification, User user, Event event, Position position) {
+ var shortMessage = notificationFormatter.formatMessage(user, event, position, "short");
- final User user = Context.getPermissionsManager().getUser(userId);
-
- String device = "";
-
- if (user.getAttributes().containsKey("notificator.pushover.device")) {
- device = user.getString("notificator.pushover.device").replaceAll(" *, *", ",");
- }
+ Message message = new Message();
+ message.token = token;
- if (token == null) {
- LOGGER.warn("Pushover token not found");
- return;
+ message.user = user.getString("pushoverUserKey");
+ if (message.user == null) {
+ message.user = this.user;
}
- if (this.user == null) {
- LOGGER.warn("Pushover user not found");
- return;
+ if (user.hasAttribute("pushoverDeviceNames")) {
+ message.device = user.getString("pushoverDeviceNames").replaceAll(" *, *", ",");
}
- NotificationMessage shortMessage = NotificationFormatter.formatMessage(userId, event, position, "short");
-
- Message message = new Message();
- message.token = token;
- message.user = this.user;
- message.device = device;
message.title = shortMessage.getSubject();
message.message = shortMessage.getBody();
- Context.getClient().target(url).request()
- .async().post(Entity.json(message), new InvocationCallback<Object>() {
- @Override
- public void completed(Object o) {
- }
-
- @Override
- public void failed(Throwable throwable) {
- LOGGER.warn("Pushover API error", throwable);
- }
- });
- }
-
- @Override
- public void sendAsync(long userId, Event event, Position position) {
- sendSync(userId, event, position);
+ client.target(url).request().post(Entity.json(message)).close();
}
}
diff --git a/src/main/java/org/traccar/notificators/NotificatorSms.java b/src/main/java/org/traccar/notificators/NotificatorSms.java
index fb817b112..2b6b20b1b 100644
--- a/src/main/java/org/traccar/notificators/NotificatorSms.java
+++ b/src/main/java/org/traccar/notificators/NotificatorSms.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2017 - 2020 Anton Tananaev (anton@traccar.org)
+ * Copyright 2017 - 2023 Anton Tananaev (anton@traccar.org)
* Copyright 2017 - 2018 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,37 +16,39 @@
*/
package org.traccar.notificators;
-import org.traccar.Context;
-import org.traccar.Main;
import org.traccar.database.StatisticsManager;
import org.traccar.model.Event;
+import org.traccar.model.Notification;
import org.traccar.model.Position;
import org.traccar.model.User;
import org.traccar.notification.MessageException;
import org.traccar.notification.NotificationFormatter;
-import org.traccar.notification.NotificationMessage;
+import org.traccar.sms.SmsManager;
-public final class NotificatorSms extends Notificator {
+import jakarta.inject.Inject;
+import jakarta.inject.Singleton;
- @Override
- public void sendAsync(long userId, Event event, Position position) {
- final User user = Context.getPermissionsManager().getUser(userId);
- if (user.getPhone() != null) {
- NotificationMessage shortMessage = NotificationFormatter.formatMessage(userId, event, position, "short");
- Main.getInjector().getInstance(StatisticsManager.class).registerSms();
- Context.getSmsManager().sendMessageAsync(user.getPhone(),
- shortMessage.getBody(), false);
- }
+@Singleton
+public class NotificatorSms implements Notificator {
+
+ private final SmsManager smsManager;
+ private final NotificationFormatter notificationFormatter;
+ private final StatisticsManager statisticsManager;
+
+ @Inject
+ public NotificatorSms(
+ SmsManager smsManager, NotificationFormatter notificationFormatter, StatisticsManager statisticsManager) {
+ this.smsManager = smsManager;
+ this.notificationFormatter = notificationFormatter;
+ this.statisticsManager = statisticsManager;
}
@Override
- public void sendSync(long userId, Event event, Position position) throws MessageException, InterruptedException {
- final User user = Context.getPermissionsManager().getUser(userId);
+ public void send(Notification notification, User user, Event event, Position position) throws MessageException {
if (user.getPhone() != null) {
- NotificationMessage shortMessage = NotificationFormatter.formatMessage(userId, event, position, "short");
- Main.getInjector().getInstance(StatisticsManager.class).registerSms();
- Context.getSmsManager().sendMessageSync(user.getPhone(),
- shortMessage.getBody(), false);
+ var shortMessage = notificationFormatter.formatMessage(user, event, position, "short");
+ statisticsManager.registerSms();
+ smsManager.sendMessage(user.getPhone(), shortMessage.getBody(), false);
}
}
diff --git a/src/main/java/org/traccar/notificators/NotificatorTelegram.java b/src/main/java/org/traccar/notificators/NotificatorTelegram.java
index 70148110c..c91aaa4ff 100644
--- a/src/main/java/org/traccar/notificators/NotificatorTelegram.java
+++ b/src/main/java/org/traccar/notificators/NotificatorTelegram.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2019 - 2021 Anton Tananaev (anton@traccar.org)
+ * Copyright 2019 - 2023 Anton Tananaev (anton@traccar.org)
* Copyright 2021 Rafael Miquelino (rafaelmiquelino@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,22 +17,24 @@
package org.traccar.notificators;
import com.fasterxml.jackson.annotation.JsonProperty;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.traccar.Context;
-import org.traccar.model.User;
+import org.traccar.config.Config;
import org.traccar.config.Keys;
import org.traccar.model.Event;
+import org.traccar.model.Notification;
import org.traccar.model.Position;
+import org.traccar.model.User;
import org.traccar.notification.NotificationFormatter;
-import org.traccar.notification.NotificationMessage;
-import javax.ws.rs.client.Entity;
-import javax.ws.rs.client.InvocationCallback;
+import jakarta.inject.Inject;
+import jakarta.inject.Singleton;
+import jakarta.ws.rs.client.Client;
+import jakarta.ws.rs.client.Entity;
-public class NotificatorTelegram extends Notificator {
+@Singleton
+public class NotificatorTelegram implements Notificator {
- private static final Logger LOGGER = LoggerFactory.getLogger(NotificatorTelegram.class);
+ private final NotificationFormatter notificationFormatter;
+ private final Client client;
private final String urlSendText;
private final String urlSendLocation;
@@ -61,29 +63,16 @@ public class NotificatorTelegram extends Notificator {
private int bearing;
}
- public NotificatorTelegram() {
+ @Inject
+ public NotificatorTelegram(Config config, NotificationFormatter notificationFormatter, Client client) {
+ this.notificationFormatter = notificationFormatter;
+ this.client = client;
urlSendText = String.format(
- "https://api.telegram.org/bot%s/sendMessage",
- Context.getConfig().getString(Keys.NOTIFICATOR_TELEGRAM_KEY));
+ "https://api.telegram.org/bot%s/sendMessage", config.getString(Keys.NOTIFICATOR_TELEGRAM_KEY));
urlSendLocation = String.format(
- "https://api.telegram.org/bot%s/sendLocation",
- Context.getConfig().getString(Keys.NOTIFICATOR_TELEGRAM_KEY));
- chatId = Context.getConfig().getString(Keys.NOTIFICATOR_TELEGRAM_CHAT_ID);
- sendLocation = Context.getConfig().getBoolean(Keys.NOTIFICATOR_TELEGRAM_SEND_LOCATION);
- }
-
- private void executeRequest(String url, Object message) {
- Context.getClient().target(url).request()
- .async().post(Entity.json(message), new InvocationCallback<Object>() {
- @Override
- public void completed(Object o) {
- }
-
- @Override
- public void failed(Throwable throwable) {
- LOGGER.warn("Telegram API error", throwable);
- }
- });
+ "https://api.telegram.org/bot%s/sendLocation", config.getString(Keys.NOTIFICATOR_TELEGRAM_KEY));
+ chatId = config.getString(Keys.NOTIFICATOR_TELEGRAM_CHAT_ID);
+ sendLocation = config.getBoolean(Keys.NOTIFICATOR_TELEGRAM_SEND_LOCATION);
}
private LocationMessage createLocationMessage(String messageChatId, Position position) {
@@ -97,9 +86,8 @@ public class NotificatorTelegram extends Notificator {
}
@Override
- public void sendSync(long userId, Event event, Position position) {
- User user = Context.getPermissionsManager().getUser(userId);
- NotificationMessage shortMessage = NotificationFormatter.formatMessage(userId, event, position, "short");
+ public void send(Notification notification, User user, Event event, Position position) {
+ var shortMessage = notificationFormatter.formatMessage(user, event, position, "short");
TextMessage message = new TextMessage();
message.chatId = user.getString("telegramChatId");
@@ -107,15 +95,11 @@ public class NotificatorTelegram extends Notificator {
message.chatId = chatId;
}
message.text = shortMessage.getBody();
- executeRequest(urlSendText, message);
+ client.target(urlSendText).request().post(Entity.json(message)).close();
if (sendLocation && position != null) {
- executeRequest(urlSendLocation, createLocationMessage(message.chatId, position));
+ client.target(urlSendLocation).request().post(
+ Entity.json(createLocationMessage(message.chatId, position))).close();
}
}
- @Override
- public void sendAsync(long userId, Event event, Position position) {
- sendSync(userId, event, position);
- }
-
}
diff --git a/src/main/java/org/traccar/notificators/NotificatorTraccar.java b/src/main/java/org/traccar/notificators/NotificatorTraccar.java
index 5bcd18b5e..e354adccb 100644
--- a/src/main/java/org/traccar/notificators/NotificatorTraccar.java
+++ b/src/main/java/org/traccar/notificators/NotificatorTraccar.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2020 Anton Tananaev (anton@traccar.org)
+ * Copyright 2020 - 2023 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.
@@ -15,15 +15,125 @@
*/
package org.traccar.notificators;
-import org.traccar.Context;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.traccar.config.Config;
import org.traccar.config.Keys;
+import org.traccar.model.Event;
+import org.traccar.model.Position;
+import org.traccar.model.User;
+import org.traccar.notification.NotificationFormatter;
+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;
-public class NotificatorTraccar extends NotificatorFirebase {
+import jakarta.inject.Inject;
+import jakarta.inject.Singleton;
+import jakarta.json.JsonObject;
+import jakarta.ws.rs.client.Client;
+import jakarta.ws.rs.client.Entity;
+import jakarta.ws.rs.core.Response;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.List;
- public NotificatorTraccar() {
- super(
- "https://www.traccar.org/push/",
- Context.getConfig().getString(Keys.NOTIFICATOR_TRACCAR_KEY));
+@Singleton
+public class NotificatorTraccar implements Notificator {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(NotificatorTraccar.class);
+
+ private final NotificationFormatter notificationFormatter;
+ private final Client client;
+ private final Storage storage;
+ private final CacheManager cacheManager;
+
+ private final String url;
+ private final String key;
+
+ public static class NotificationObject {
+ @JsonProperty("title")
+ private String title;
+ @JsonProperty("body")
+ private String body;
+ @JsonProperty("sound")
+ private String sound;
+ }
+
+ public static class Message {
+ @JsonProperty("registration_ids")
+ private String[] tokens;
+ @JsonProperty("notification")
+ private NotificationObject notification;
+ }
+
+ @Inject
+ public NotificatorTraccar(
+ Config config, NotificationFormatter notificationFormatter, Client client,
+ Storage storage, CacheManager cacheManager) {
+ this.notificationFormatter = notificationFormatter;
+ this.client = client;
+ this.storage = storage;
+ this.cacheManager = cacheManager;
+ this.url = "https://www.traccar.org/push/";
+ this.key = config.getString(Keys.NOTIFICATOR_TRACCAR_KEY);
+ }
+
+ @Override
+ public void send(org.traccar.model.Notification notification, User user, Event event, Position position) {
+ if (user.hasAttribute("notificationTokens")) {
+
+ var shortMessage = notificationFormatter.formatMessage(user, event, position, "short");
+
+ NotificationObject item = new NotificationObject();
+ item.title = shortMessage.getSubject();
+ item.body = shortMessage.getBody();
+ item.sound = "default";
+
+ String[] tokenArray = user.getString("notificationTokens").split("[, ]");
+ List<String> registrationTokens = new ArrayList<>(Arrays.asList(tokenArray));
+
+ Message message = new Message();
+ message.tokens = user.getString("notificationTokens").split("[, ]");
+ message.notification = item;
+
+ var request = client.target(url).request().header("Authorization", "key=" + key);
+ try (Response result = request.post(Entity.json(message))) {
+ var json = result.readEntity(JsonObject.class);
+ List<String> failedTokens = new LinkedList<>();
+ var responses = json.getJsonArray("responses");
+ for (int i = 0; i < responses.size(); i++) {
+ var response = responses.getJsonObject(i);
+ if (!response.getBoolean("success")) {
+ var error = response.getJsonObject("error");
+ String errorCode = error.getString("code");
+ if (errorCode.equals("messaging/invalid-argument")
+ || errorCode.equals("messaging/registration-token-not-registered")) {
+ failedTokens.add(registrationTokens.get(i));
+ }
+ LOGGER.warn("Push user {} error - {}", user.getId(), error.getString("message"));
+ }
+ }
+ if (!failedTokens.isEmpty()) {
+ registrationTokens.removeAll(failedTokens);
+ if (registrationTokens.isEmpty()) {
+ user.getAttributes().remove("notificationTokens");
+ } else {
+ user.set("notificationTokens", String.join(",", registrationTokens));
+ }
+ storage.updateObject(user, new Request(
+ new Columns.Include("attributes"),
+ new Condition.Equals("id", user.getId())));
+ cacheManager.updateOrInvalidate(true, user);
+ }
+ } catch (StorageException e) {
+ LOGGER.warn("Push error", e);
+ }
+ }
}
}
diff --git a/src/main/java/org/traccar/notificators/NotificatorWeb.java b/src/main/java/org/traccar/notificators/NotificatorWeb.java
index 1d11c0b46..3a125db3c 100644
--- a/src/main/java/org/traccar/notificators/NotificatorWeb.java
+++ b/src/main/java/org/traccar/notificators/NotificatorWeb.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2018 Anton Tananaev (anton@traccar.org)
+ * Copyright 2018 - 2023 Anton Tananaev (anton@traccar.org)
* Copyright 2018 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,15 +16,45 @@
*/
package org.traccar.notificators;
-import org.traccar.Context;
import org.traccar.model.Event;
+import org.traccar.model.Notification;
import org.traccar.model.Position;
+import org.traccar.model.User;
+import org.traccar.notification.NotificationFormatter;
+import org.traccar.session.ConnectionManager;
-public final class NotificatorWeb extends Notificator {
+import jakarta.inject.Inject;
+import jakarta.inject.Singleton;
+
+@Singleton
+public final class NotificatorWeb implements Notificator {
+
+ private final ConnectionManager connectionManager;
+ private final NotificationFormatter notificationFormatter;
+
+ @Inject
+ public NotificatorWeb(ConnectionManager connectionManager, NotificationFormatter notificationFormatter) {
+ this.connectionManager = connectionManager;
+ this.notificationFormatter = notificationFormatter;
+ }
@Override
- public void sendSync(long userId, Event event, Position position) {
- Context.getConnectionManager().updateEvent(userId, event);
+ public void send(Notification notification, User user, Event event, Position position) {
+
+ Event copy = new Event();
+ copy.setId(event.getId());
+ copy.setDeviceId(event.getDeviceId());
+ copy.setType(event.getType());
+ copy.setEventTime(event.getEventTime());
+ copy.setPositionId(event.getPositionId());
+ copy.setGeofenceId(event.getGeofenceId());
+ copy.setMaintenanceId(event.getMaintenanceId());
+ copy.getAttributes().putAll(event.getAttributes());
+
+ var message = notificationFormatter.formatMessage(user, event, position, "short");
+ copy.set("message", message.getBody());
+
+ connectionManager.updateEvent(true, user.getId(), copy);
}
}