From bb289a69fa4d292378c5c534e10985be65b2e392 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Sun, 25 Mar 2018 22:16:09 -0300 Subject: generalization for notifications processing --- src/org/traccar/notification/NotificationSms.java | 27 +++++++++-------------- 1 file changed, 11 insertions(+), 16 deletions(-) (limited to 'src/org/traccar/notification/NotificationSms.java') diff --git a/src/org/traccar/notification/NotificationSms.java b/src/org/traccar/notification/NotificationSms.java index 8c0265af4..34d68e174 100644 --- a/src/org/traccar/notification/NotificationSms.java +++ b/src/org/traccar/notification/NotificationSms.java @@ -20,32 +20,27 @@ import org.traccar.Context; import org.traccar.model.Event; import org.traccar.model.Position; import org.traccar.model.User; +import org.traccar.sms.SMSException; -import com.cloudhopper.smpp.type.RecoverablePduException; -import com.cloudhopper.smpp.type.SmppChannelException; -import com.cloudhopper.smpp.type.SmppTimeoutException; -import com.cloudhopper.smpp.type.UnrecoverablePduException; +public final class NotificationSms extends Notificator { -public final class NotificationSms { - - private NotificationSms() { - } - - public static void sendSmsAsync(long userId, Event event, Position position) { + @Override + public void sendAsync(long userId, Event event, Position position) { User user = Context.getPermissionsManager().getUser(userId); - if (Context.getSmppManager() != null && user.getPhone() != null) { + if (Context.getSmsManager() != null && user.getPhone() != null) { Context.getStatisticsManager().registerSms(); - Context.getSmppManager().sendMessageAsync(user.getPhone(), + Context.getSmsManager().sendMessageAsync(user.getPhone(), NotificationFormatter.formatSmsMessage(userId, event, position), false); } } - public static void sendSmsSync(long userId, Event event, Position position) throws RecoverablePduException, - UnrecoverablePduException, SmppTimeoutException, SmppChannelException, InterruptedException { + @Override + public void sendSync(long userId, Event event, Position position) throws SMSException, + InterruptedException { User user = Context.getPermissionsManager().getUser(userId); - if (Context.getSmppManager() != null && user.getPhone() != null) { + if (Context.getSmsManager() != null && user.getPhone() != null) { Context.getStatisticsManager().registerSms(); - Context.getSmppManager().sendMessageSync(user.getPhone(), + Context.getSmsManager().sendMessageSync(user.getPhone(), NotificationFormatter.formatSmsMessage(userId, event, position), false); } } -- cgit v1.2.3 From 2891a1a4eee21e6c80b65915d35558c033503047 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Sun, 1 Apr 2018 18:22:29 -0300 Subject: generalization for notifications processing --- src/org/traccar/notification/NotificationSms.java | 4 ++-- src/org/traccar/notification/NotificatorManager.java | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) (limited to 'src/org/traccar/notification/NotificationSms.java') diff --git a/src/org/traccar/notification/NotificationSms.java b/src/org/traccar/notification/NotificationSms.java index 34d68e174..a8eaff798 100644 --- a/src/org/traccar/notification/NotificationSms.java +++ b/src/org/traccar/notification/NotificationSms.java @@ -27,7 +27,7 @@ public final class NotificationSms extends Notificator { @Override public void sendAsync(long userId, Event event, Position position) { User user = Context.getPermissionsManager().getUser(userId); - if (Context.getSmsManager() != null && user.getPhone() != null) { + if (user.getPhone() != null) { Context.getStatisticsManager().registerSms(); Context.getSmsManager().sendMessageAsync(user.getPhone(), NotificationFormatter.formatSmsMessage(userId, event, position), false); @@ -38,7 +38,7 @@ public final class NotificationSms extends Notificator { public void sendSync(long userId, Event event, Position position) throws SMSException, InterruptedException { User user = Context.getPermissionsManager().getUser(userId); - if (Context.getSmsManager() != null && user.getPhone() != null) { + if (user.getPhone() != null) { Context.getStatisticsManager().registerSms(); Context.getSmsManager().sendMessageSync(user.getPhone(), NotificationFormatter.formatSmsMessage(userId, event, position), false); diff --git a/src/org/traccar/notification/NotificatorManager.java b/src/org/traccar/notification/NotificatorManager.java index 814c8dc1a..1aa76beff 100644 --- a/src/org/traccar/notification/NotificatorManager.java +++ b/src/org/traccar/notification/NotificatorManager.java @@ -19,6 +19,8 @@ package org.traccar.notification; import java.util.HashMap; import java.util.Map; +import org.traccar.Context; + public class NotificatorManager { protected NotificatorManager() { @@ -29,7 +31,9 @@ public class NotificatorManager { private static final Notificator NULL_NOTIFICATOR = new NotificationNull(); static { - NOTIFICATORS.put("sms", new NotificationSms()); + if (Context.getSmsManager() != null) { + NOTIFICATORS.put("sms", new NotificationSms()); + } NOTIFICATORS.put("mail", new NotificationMail()); NOTIFICATORS.put("web", new NotificationWeb()); } -- cgit v1.2.3 From 50a80d68516890aa3ec5c2826f929474927def30 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Fri, 1 Jun 2018 07:34:35 -0300 Subject: generic template names --- src/org/traccar/notification/NotificationFormatter.java | 8 ++++---- src/org/traccar/notification/NotificationMail.java | 2 +- src/org/traccar/notification/NotificationSms.java | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) (limited to 'src/org/traccar/notification/NotificationSms.java') diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java index 114825a83..d1f6c3903 100644 --- a/src/org/traccar/notification/NotificationFormatter.java +++ b/src/org/traccar/notification/NotificationFormatter.java @@ -85,16 +85,16 @@ public final class NotificationFormatter { return template; } - public static MailMessage formatMailMessage(long userId, Event event, Position position) { - String templatePath = Context.getConfig().getString("mail.templatesPath", "mail"); + public static MailMessage formatFullMessage(long userId, Event event, Position position) { + String templatePath = Context.getConfig().getString("message.full.templatesPath", "full"); VelocityContext velocityContext = prepareContext(userId, event, position); String formattedMessage = formatMessage(velocityContext, userId, event, position, templatePath); return new MailMessage((String) velocityContext.get("subject"), formattedMessage); } - public static String formatSmsMessage(long userId, Event event, Position position) { - String templatePath = Context.getConfig().getString("sms.templatesPath", "sms"); + public static String formatShortMessage(long userId, Event event, Position position) { + String templatePath = Context.getConfig().getString("message.short.templatesPath", "short"); return formatMessage(null, userId, event, position, templatePath); } diff --git a/src/org/traccar/notification/NotificationMail.java b/src/org/traccar/notification/NotificationMail.java index 3a2a19337..fb89bf5bd 100644 --- a/src/org/traccar/notification/NotificationMail.java +++ b/src/org/traccar/notification/NotificationMail.java @@ -108,7 +108,7 @@ public final class NotificationMail extends Notificator { } message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail())); - MailMessage mailMessage = NotificationFormatter.formatMailMessage(userId, event, position); + MailMessage mailMessage = NotificationFormatter.formatFullMessage(userId, event, position); message.setSubject(mailMessage.getSubject()); message.setSentDate(new Date()); message.setContent(mailMessage.getBody(), "text/html; charset=utf-8"); diff --git a/src/org/traccar/notification/NotificationSms.java b/src/org/traccar/notification/NotificationSms.java index a8eaff798..804fa8527 100644 --- a/src/org/traccar/notification/NotificationSms.java +++ b/src/org/traccar/notification/NotificationSms.java @@ -30,7 +30,7 @@ public final class NotificationSms extends Notificator { if (user.getPhone() != null) { Context.getStatisticsManager().registerSms(); Context.getSmsManager().sendMessageAsync(user.getPhone(), - NotificationFormatter.formatSmsMessage(userId, event, position), false); + NotificationFormatter.formatShortMessage(userId, event, position), false); } } @@ -41,7 +41,7 @@ public final class NotificationSms extends Notificator { if (user.getPhone() != null) { Context.getStatisticsManager().registerSms(); Context.getSmsManager().sendMessageSync(user.getPhone(), - NotificationFormatter.formatSmsMessage(userId, event, position), false); + NotificationFormatter.formatShortMessage(userId, event, position), false); } } } -- cgit v1.2.3 From 575deac9e2df1cbd0601328f7ea7a9f22029fa43 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Fri, 1 Jun 2018 11:20:39 -0300 Subject: option to instantiate a new SMSNotifier using a different SMSManager --- src/org/traccar/Context.java | 18 +++++++++--------- src/org/traccar/notification/NotificationSms.java | 22 ++++++++++++++++++---- 2 files changed, 27 insertions(+), 13 deletions(-) (limited to 'src/org/traccar/notification/NotificationSms.java') diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index a7c1817cf..abf9f7a8a 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -372,6 +372,15 @@ public final class Context { tripsConfig = initTripsConfig(); + if (config.getBoolean("sms.smpp.enable")) { + final String smsManagerClass = config.getString("sms.manager.class", "org.traccar.smpp.SmppClient"); + try { + smsManager = (SMSManager) Class.forName(smsManagerClass).newInstance(); + } catch (ClassNotFoundException e) { + Log.warning("Error loading SMS Manager class : " + smsManagerClass, e); + } + } + if (config.getBoolean("event.enable")) { initEventsModule(); } @@ -394,15 +403,6 @@ public final class Context { statisticsManager = new StatisticsManager(); - if (config.getBoolean("sms.smpp.enable")) { - final String smsManagerClass = config.getString("sms.manager.class", "org.traccar.smpp.SmppClient"); - try { - smsManager = (SMSManager) Class.forName(smsManagerClass).newInstance(); - } catch (ClassNotFoundException e) { - Log.warning("Error loading SMS Manager class : " + smsManagerClass, e); - } - } - } private static void initGeolocationModule() { diff --git a/src/org/traccar/notification/NotificationSms.java b/src/org/traccar/notification/NotificationSms.java index 804fa8527..63fd57895 100644 --- a/src/org/traccar/notification/NotificationSms.java +++ b/src/org/traccar/notification/NotificationSms.java @@ -20,16 +20,30 @@ import org.traccar.Context; import org.traccar.model.Event; import org.traccar.model.Position; import org.traccar.model.User; +import org.traccar.sms.SMSManager; import org.traccar.sms.SMSException; public final class NotificationSms extends Notificator { + private final SMSManager sms; + + public NotificationSms(String methodId) throws ClassNotFoundException, + InstantiationException, IllegalAccessException { + final String smsClass = Context.getConfig().getString("notificator." + methodId + ".sms.class", ""); + if (smsClass.length() > 0) { + sms = (SMSManager) Class.forName(smsClass).newInstance(); + } else { + sms = Context.getSmsManager(); + } + } + + @Override public void sendAsync(long userId, Event event, Position position) { - User user = Context.getPermissionsManager().getUser(userId); + final User user = Context.getPermissionsManager().getUser(userId); if (user.getPhone() != null) { Context.getStatisticsManager().registerSms(); - Context.getSmsManager().sendMessageAsync(user.getPhone(), + sms.sendMessageAsync(user.getPhone(), NotificationFormatter.formatShortMessage(userId, event, position), false); } } @@ -37,10 +51,10 @@ public final class NotificationSms extends Notificator { @Override public void sendSync(long userId, Event event, Position position) throws SMSException, InterruptedException { - User user = Context.getPermissionsManager().getUser(userId); + final User user = Context.getPermissionsManager().getUser(userId); if (user.getPhone() != null) { Context.getStatisticsManager().registerSms(); - Context.getSmsManager().sendMessageSync(user.getPhone(), + sms.sendMessageSync(user.getPhone(), NotificationFormatter.formatShortMessage(userId, event, position), false); } } -- cgit v1.2.3