diff options
Diffstat (limited to 'src/main/java/org/traccar/notificators')
9 files changed, 83 insertions, 80 deletions
diff --git a/src/main/java/org/traccar/notificators/Notificator.java b/src/main/java/org/traccar/notificators/Notificator.java index cf71141c0..59216b1b6 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 - 2023 Anton Tananaev (anton@traccar.org) + * Copyright 2018 - 2024 Anton Tananaev (anton@traccar.org) * Copyright 2018 Andrey Kunitsyn (andrey@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,9 +21,26 @@ 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; -public interface Notificator { +public abstract class Notificator { - void send(Notification notification, User user, Event event, Position position) throws MessageException; + private final NotificationFormatter notificationFormatter; + private final String templatePath; + + public Notificator(NotificationFormatter notificationFormatter, String templatePath) { + this.notificationFormatter = notificationFormatter; + this.templatePath = templatePath; + } + + public void send(Notification notification, User user, Event event, Position position) throws MessageException { + var message = notificationFormatter.formatMessage(notification, user, event, position, templatePath); + send(user, message, event, position); + } + + public void send(User user, NotificationMessage message, Event event, Position position) throws MessageException { + throw new UnsupportedOperationException(); + } } diff --git a/src/main/java/org/traccar/notificators/NotificatorCommand.java b/src/main/java/org/traccar/notificators/NotificatorCommand.java index 5dd3313d4..712dda274 100644 --- a/src/main/java/org/traccar/notificators/NotificatorCommand.java +++ b/src/main/java/org/traccar/notificators/NotificatorCommand.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 Anton Tananaev (anton@traccar.org) + * Copyright 2023 - 2024 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. @@ -31,13 +31,14 @@ import jakarta.inject.Inject; import jakarta.inject.Singleton; @Singleton -public class NotificatorCommand implements Notificator { +public class NotificatorCommand extends Notificator { private final Storage storage; private final CommandsManager commandsManager; @Inject public NotificatorCommand(Storage storage, CommandsManager commandsManager) { + super(null, null); this.storage = storage; this.commandsManager = commandsManager; } diff --git a/src/main/java/org/traccar/notificators/NotificatorFirebase.java b/src/main/java/org/traccar/notificators/NotificatorFirebase.java index 89031ba26..a4171a1d4 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 - 2023 Anton Tananaev (anton@traccar.org) + * Copyright 2018 - 2024 Anton Tananaev (anton@traccar.org) * Copyright 2018 Andrey Kunitsyn (andrey@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -26,25 +26,25 @@ import com.google.firebase.messaging.Aps; import com.google.firebase.messaging.FirebaseMessaging; import com.google.firebase.messaging.MessagingErrorCode; import com.google.firebase.messaging.MulticastMessage; +import jakarta.inject.Inject; +import jakarta.inject.Singleton; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.traccar.model.ObjectOperation; import org.traccar.config.Config; import org.traccar.config.Keys; import org.traccar.model.Event; -import org.traccar.model.Notification; +import org.traccar.model.ObjectOperation; 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.session.cache.CacheManager; 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; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -54,11 +54,9 @@ import java.util.LinkedList; import java.util.List; @Singleton -public class NotificatorFirebase implements Notificator { +public class NotificatorFirebase extends Notificator { private static final Logger LOGGER = LoggerFactory.getLogger(NotificatorFirebase.class); - - private final NotificationFormatter notificationFormatter; private final Storage storage; private final CacheManager cacheManager; @@ -67,7 +65,7 @@ public class NotificatorFirebase implements Notificator { Config config, NotificationFormatter notificationFormatter, Storage storage, CacheManager cacheManager) throws IOException { - this.notificationFormatter = notificationFormatter; + super(notificationFormatter, "short"); this.storage = storage; this.cacheManager = cacheManager; @@ -82,18 +80,16 @@ public class NotificatorFirebase implements Notificator { } @Override - public void send(Notification notification, User user, Event event, Position position) throws MessageException { + public void send(User user, NotificationMessage message, Event event, Position position) throws MessageException { if (user.hasAttribute("notificationTokens")) { - var shortMessage = notificationFormatter.formatMessage(notification, user, event, position, "short"); - List<String> registrationTokens = new ArrayList<>( Arrays.asList(user.getString("notificationTokens").split("[, ]"))); - MulticastMessage message = MulticastMessage.builder() + var messageBuilder = MulticastMessage.builder() .setNotification(com.google.firebase.messaging.Notification.builder() - .setTitle(shortMessage.getSubject()) - .setBody(shortMessage.getBody()) + .setTitle(message.getSubject()) + .setBody(message.getBody()) .build()) .setAndroidConfig(AndroidConfig.builder() .setNotification(AndroidNotification.builder() @@ -105,12 +101,14 @@ public class NotificatorFirebase implements Notificator { .setSound("default") .build()) .build()) - .addAllTokens(registrationTokens) - .putData("eventId", String.valueOf(event.getId())) - .build(); + .addAllTokens(registrationTokens); + + if (event != null) { + messageBuilder.putData("eventId", String.valueOf(event.getId())); + } try { - var result = FirebaseMessaging.getInstance().sendMulticast(message); + var result = FirebaseMessaging.getInstance().sendMulticast(messageBuilder.build()); List<String> failedTokens = new LinkedList<>(); var iterator = result.getResponses().listIterator(); while (iterator.hasNext()) { diff --git a/src/main/java/org/traccar/notificators/NotificatorMail.java b/src/main/java/org/traccar/notificators/NotificatorMail.java index 11d4c5bae..e7ca10157 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 - 2023 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2024 Anton Tananaev (anton@traccar.org) * Copyright 2017 - 2018 Andrey Kunitsyn (andrey@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,35 +16,32 @@ */ package org.traccar.notificators; +import jakarta.inject.Inject; +import jakarta.inject.Singleton; +import jakarta.mail.MessagingException; import org.traccar.mail.MailManager; 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 jakarta.inject.Inject; -import jakarta.inject.Singleton; -import jakarta.mail.MessagingException; +import org.traccar.notification.NotificationMessage; @Singleton -public class NotificatorMail implements Notificator { +public class NotificatorMail extends Notificator { private final MailManager mailManager; - private final NotificationFormatter notificationFormatter; @Inject public NotificatorMail(MailManager mailManager, NotificationFormatter notificationFormatter) { + super(notificationFormatter, "full"); this.mailManager = mailManager; - this.notificationFormatter = notificationFormatter; } @Override - public void send(Notification notification, User user, Event event, Position position) throws MessageException { + public void send(User user, NotificationMessage message, Event event, Position position) throws MessageException { try { - var fullMessage = notificationFormatter.formatMessage(notification, user, event, position, "full"); - mailManager.sendMessage(user, false, fullMessage.getSubject(), fullMessage.getBody()); + mailManager.sendMessage(user, false, message.getSubject(), message.getBody()); } catch (MessagingException e) { throw new MessageException(e); } diff --git a/src/main/java/org/traccar/notificators/NotificatorPushover.java b/src/main/java/org/traccar/notificators/NotificatorPushover.java index cf4c4026b..a9708818b 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 - 2023 Anton Tananaev (anton@traccar.org) + * Copyright 2020 - 2024 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,23 +16,21 @@ package org.traccar.notificators; import com.fasterxml.jackson.annotation.JsonProperty; +import jakarta.inject.Inject; +import jakarta.inject.Singleton; +import jakarta.ws.rs.client.Client; +import jakarta.ws.rs.client.Entity; 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 jakarta.inject.Inject; -import jakarta.inject.Singleton; -import jakarta.ws.rs.client.Client; -import jakarta.ws.rs.client.Entity; +import org.traccar.notification.NotificationMessage; @Singleton -public class NotificatorPushover implements Notificator { +public class NotificatorPushover extends Notificator { - private final NotificationFormatter notificationFormatter; private final Client client; private final String url; @@ -54,7 +52,7 @@ public class NotificatorPushover implements Notificator { @Inject public NotificatorPushover(Config config, NotificationFormatter notificationFormatter, Client client) { - this.notificationFormatter = notificationFormatter; + super(notificationFormatter, "short"); this.client = client; url = "https://api.pushover.net/1/messages.json"; token = config.getString(Keys.NOTIFICATOR_PUSHOVER_TOKEN); @@ -62,8 +60,7 @@ public class NotificatorPushover implements Notificator { } @Override - public void send(Notification notification, User user, Event event, Position position) { - var shortMessage = notificationFormatter.formatMessage(notification, user, event, position, "short"); + public void send(User user, NotificationMessage shortMessage, Event event, Position position) { Message message = new Message(); message.token = token; diff --git a/src/main/java/org/traccar/notificators/NotificatorSms.java b/src/main/java/org/traccar/notificators/NotificatorSms.java index ce362290e..a8086adcc 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 - 2023 Anton Tananaev (anton@traccar.org) + * Copyright 2017 - 2024 Anton Tananaev (anton@traccar.org) * Copyright 2017 - 2018 Andrey Kunitsyn (andrey@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,39 +16,36 @@ */ package org.traccar.notificators; +import jakarta.inject.Inject; +import jakarta.inject.Singleton; 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; -import jakarta.inject.Inject; -import jakarta.inject.Singleton; - @Singleton -public class NotificatorSms implements Notificator { +public class NotificatorSms extends Notificator { private final SmsManager smsManager; - private final NotificationFormatter notificationFormatter; private final StatisticsManager statisticsManager; @Inject public NotificatorSms( SmsManager smsManager, NotificationFormatter notificationFormatter, StatisticsManager statisticsManager) { + super(notificationFormatter, "short"); this.smsManager = smsManager; - this.notificationFormatter = notificationFormatter; this.statisticsManager = statisticsManager; } @Override - public void send(Notification notification, User user, Event event, Position position) throws MessageException { + public void send(User user, NotificationMessage message, Event event, Position position) throws MessageException { if (user.getPhone() != null) { - var shortMessage = notificationFormatter.formatMessage(notification, user, event, position, "short"); statisticsManager.registerSms(); - smsManager.sendMessage(user.getPhone(), shortMessage.getBody(), false); + smsManager.sendMessage(user.getPhone(), message.getBody(), false); } } diff --git a/src/main/java/org/traccar/notificators/NotificatorTelegram.java b/src/main/java/org/traccar/notificators/NotificatorTelegram.java index eaee32810..4c8d7b8aa 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 - 2023 Anton Tananaev (anton@traccar.org) + * Copyright 2019 - 2024 Anton Tananaev (anton@traccar.org) * Copyright 2021 Rafael Miquelino (rafaelmiquelino@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,23 +17,21 @@ package org.traccar.notificators; import com.fasterxml.jackson.annotation.JsonProperty; +import jakarta.inject.Inject; +import jakarta.inject.Singleton; +import jakarta.ws.rs.client.Client; +import jakarta.ws.rs.client.Entity; 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 jakarta.inject.Inject; -import jakarta.inject.Singleton; -import jakarta.ws.rs.client.Client; -import jakarta.ws.rs.client.Entity; +import org.traccar.notification.NotificationMessage; @Singleton -public class NotificatorTelegram implements Notificator { +public class NotificatorTelegram extends Notificator { - private final NotificationFormatter notificationFormatter; private final Client client; private final String urlSendText; @@ -65,7 +63,7 @@ public class NotificatorTelegram implements Notificator { @Inject public NotificatorTelegram(Config config, NotificationFormatter notificationFormatter, Client client) { - this.notificationFormatter = notificationFormatter; + super(notificationFormatter, "short"); this.client = client; urlSendText = String.format( "https://api.telegram.org/bot%s/sendMessage", config.getString(Keys.NOTIFICATOR_TELEGRAM_KEY)); @@ -86,8 +84,7 @@ public class NotificatorTelegram implements Notificator { } @Override - public void send(Notification notification, User user, Event event, Position position) { - var shortMessage = notificationFormatter.formatMessage(notification, user, event, position, "short"); + public void send(User user, NotificationMessage shortMessage, Event event, Position position) { TextMessage message = new TextMessage(); message.chatId = user.getString("telegramChatId"); diff --git a/src/main/java/org/traccar/notificators/NotificatorTraccar.java b/src/main/java/org/traccar/notificators/NotificatorTraccar.java index c00e3e029..983c4cda6 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 - 2023 Anton Tananaev (anton@traccar.org) + * Copyright 2020 - 2024 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. @@ -25,6 +25,7 @@ import org.traccar.model.Event; import org.traccar.model.Position; import org.traccar.model.User; import org.traccar.notification.NotificationFormatter; +import org.traccar.notification.NotificationMessage; import org.traccar.session.cache.CacheManager; import org.traccar.storage.Storage; import org.traccar.storage.query.Columns; @@ -43,11 +44,10 @@ import java.util.LinkedList; import java.util.List; @Singleton -public class NotificatorTraccar implements Notificator { +public class NotificatorTraccar extends 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; @@ -75,7 +75,7 @@ public class NotificatorTraccar implements Notificator { public NotificatorTraccar( Config config, NotificationFormatter notificationFormatter, Client client, Storage storage, CacheManager cacheManager) { - this.notificationFormatter = notificationFormatter; + super(notificationFormatter, "short"); this.client = client; this.storage = storage; this.cacheManager = cacheManager; @@ -84,11 +84,9 @@ public class NotificatorTraccar implements Notificator { } @Override - public void send(org.traccar.model.Notification notification, User user, Event event, Position position) { + public void send(User user, NotificationMessage shortMessage, Event event, Position position) { if (user.hasAttribute("notificationTokens")) { - var shortMessage = notificationFormatter.formatMessage(notification, user, event, position, "short"); - NotificationObject item = new NotificationObject(); item.title = shortMessage.getSubject(); item.body = shortMessage.getBody(); diff --git a/src/main/java/org/traccar/notificators/NotificatorWeb.java b/src/main/java/org/traccar/notificators/NotificatorWeb.java index 2b9030226..b7bbdac1b 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 - 2023 Anton Tananaev (anton@traccar.org) + * Copyright 2018 - 2024 Anton Tananaev (anton@traccar.org) * Copyright 2018 Andrey Kunitsyn (andrey@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -27,13 +27,14 @@ import jakarta.inject.Inject; import jakarta.inject.Singleton; @Singleton -public final class NotificatorWeb implements Notificator { +public final class NotificatorWeb extends Notificator { private final ConnectionManager connectionManager; private final NotificationFormatter notificationFormatter; @Inject public NotificatorWeb(ConnectionManager connectionManager, NotificationFormatter notificationFormatter) { + super(null, null); this.connectionManager = connectionManager; this.notificationFormatter = notificationFormatter; } |