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/BaseProtocol.java | 8 +-- src/org/traccar/Context.java | 15 ++++-- .../traccar/api/resource/NotificationResource.java | 16 ++---- src/org/traccar/database/CommandsManager.java | 6 +-- src/org/traccar/database/NotificationManager.java | 26 +++------ src/org/traccar/model/Notification.java | 18 +++++++ .../notification/NotificationException.java | 25 +++++++++ src/org/traccar/notification/NotificationMail.java | 62 +++++++++------------- src/org/traccar/notification/NotificationNull.java | 31 +++++++++++ src/org/traccar/notification/NotificationSms.java | 27 ++++------ src/org/traccar/notification/NotificationWeb.java | 31 +++++++++++ src/org/traccar/notification/Notificator.java | 40 ++++++++++++++ .../traccar/notification/NotificatorManager.java | 51 ++++++++++++++++++ src/org/traccar/smpp/SmppClient.java | 60 ++++++++++++--------- src/org/traccar/sms/SMSException.java | 28 ++++++++++ src/org/traccar/sms/SMSManager.java | 8 +++ 16 files changed, 331 insertions(+), 121 deletions(-) create mode 100644 src/org/traccar/notification/NotificationException.java create mode 100644 src/org/traccar/notification/NotificationNull.java create mode 100644 src/org/traccar/notification/NotificationWeb.java create mode 100644 src/org/traccar/notification/Notificator.java create mode 100644 src/org/traccar/notification/NotificatorManager.java create mode 100644 src/org/traccar/sms/SMSException.java create mode 100644 src/org/traccar/sms/SMSManager.java (limited to 'src/org/traccar') diff --git a/src/org/traccar/BaseProtocol.java b/src/org/traccar/BaseProtocol.java index 07adbea5e..7265bd9c1 100644 --- a/src/org/traccar/BaseProtocol.java +++ b/src/org/traccar/BaseProtocol.java @@ -92,18 +92,18 @@ public abstract class BaseProtocol implements Protocol { @Override public void sendTextCommand(String destAddress, Command command) throws Exception { - if (Context.getSmppManager() != null) { + if (Context.getSmsManager() != null) { if (command.getType().equals(Command.TYPE_CUSTOM)) { - Context.getSmppManager().sendMessageSync(destAddress, command.getString(Command.KEY_DATA), true); + Context.getSmsManager().sendMessageSync(destAddress, command.getString(Command.KEY_DATA), true); } else if (supportedTextCommands.contains(command.getType()) && textCommandEncoder != null) { - Context.getSmppManager().sendMessageSync(destAddress, + Context.getSmsManager().sendMessageSync(destAddress, (String) textCommandEncoder.encodeCommand(command), true); } else { throw new RuntimeException( "Command " + command.getType() + " is not supported in protocol " + getName()); } } else { - throw new RuntimeException("SMPP client is not enabled"); + throw new RuntimeException("SMS is not enabled"); } } diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 09e3c619b..c9ac4ec12 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -74,7 +74,7 @@ import org.traccar.notification.EventForwarder; import org.traccar.notification.JsonTypeEventForwarder; import org.traccar.notification.MultiPartEventForwarder; import org.traccar.reports.model.TripsConfig; -import org.traccar.smpp.SmppClient; +import org.traccar.sms.SMSManager; import org.traccar.web.WebServer; public final class Context { @@ -238,10 +238,10 @@ public final class Context { return statisticsManager; } - private static SmppClient smppClient; + private static SMSManager smsManager; - public static SmppClient getSmppManager() { - return smppClient; + public static SMSManager getSmsManager() { + return smsManager; } private static MotionEventHandler motionEventHandler; @@ -388,7 +388,12 @@ public final class Context { statisticsManager = new StatisticsManager(); if (config.getBoolean("sms.smpp.enable")) { - smppClient = new SmppClient(); + 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); + } } } diff --git a/src/org/traccar/api/resource/NotificationResource.java b/src/org/traccar/api/resource/NotificationResource.java index 540f02926..2a398f40d 100644 --- a/src/org/traccar/api/resource/NotificationResource.java +++ b/src/org/traccar/api/resource/NotificationResource.java @@ -17,7 +17,6 @@ package org.traccar.api.resource; import java.util.Collection; -import javax.mail.MessagingException; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; @@ -31,13 +30,9 @@ import org.traccar.api.ExtendedObjectResource; import org.traccar.model.Event; import org.traccar.model.Notification; import org.traccar.model.Typed; -import org.traccar.notification.NotificationMail; -import org.traccar.notification.NotificationSms; +import org.traccar.notification.NotificationException; +import org.traccar.notification.NotificatorManager; -import com.cloudhopper.smpp.type.RecoverablePduException; -import com.cloudhopper.smpp.type.SmppChannelException; -import com.cloudhopper.smpp.type.SmppTimeoutException; -import com.cloudhopper.smpp.type.UnrecoverablePduException; @Path("notifications") @Produces(MediaType.APPLICATION_JSON) @@ -56,10 +51,9 @@ public class NotificationResource extends ExtendedObjectResource { @POST @Path("test") - public Response testMessage() throws MessagingException, RecoverablePduException, - UnrecoverablePduException, SmppTimeoutException, SmppChannelException, InterruptedException { - NotificationMail.sendMailSync(getUserId(), new Event("test", 0), null); - NotificationSms.sendSmsSync(getUserId(), new Event("test", 0), null); + public Response testMessage() throws NotificationException, InterruptedException { + NotificatorManager.getMail().sendSync(getUserId(), new Event("test", 0), null); + NotificatorManager.getSms().sendSync(getUserId(), new Event("test", 0), null); return Response.noContent().build(); } diff --git a/src/org/traccar/database/CommandsManager.java b/src/org/traccar/database/CommandsManager.java index 9ceb995ef..2e68fae5a 100644 --- a/src/org/traccar/database/CommandsManager.java +++ b/src/org/traccar/database/CommandsManager.java @@ -58,10 +58,10 @@ public class CommandsManager extends ExtendedObjectManager { BaseProtocol protocol = Context.getServerManager().getProtocol(lastPosition.getProtocol()); protocol.sendTextCommand(phone, command); } else if (command.getType().equals(Command.TYPE_CUSTOM)) { - if (Context.getSmppManager() != null) { - Context.getSmppManager().sendMessageSync(phone, command.getString(Command.KEY_DATA), true); + if (Context.getSmsManager() != null) { + Context.getSmsManager().sendMessageSync(phone, command.getString(Command.KEY_DATA), true); } else { - throw new RuntimeException("SMPP client is not enabled"); + throw new RuntimeException("SMS is not enabled"); } } else { throw new RuntimeException("Command " + command.getType() + " is not supported"); diff --git a/src/org/traccar/database/NotificationManager.java b/src/org/traccar/database/NotificationManager.java index 1c59a8666..37c6b720c 100644 --- a/src/org/traccar/database/NotificationManager.java +++ b/src/org/traccar/database/NotificationManager.java @@ -32,8 +32,7 @@ import org.traccar.model.Event; import org.traccar.model.Notification; import org.traccar.model.Position; import org.traccar.model.Typed; -import org.traccar.notification.NotificationMail; -import org.traccar.notification.NotificationSms; +import org.traccar.notification.NotificatorManager; public class NotificationManager extends ExtendedObjectManager { @@ -83,29 +82,16 @@ public class NotificationManager extends ExtendedObjectManager { if (usersToForward != null) { usersToForward.add(userId); } - boolean sentWeb = false; - boolean sentMail = false; - boolean sentSms = Context.getSmppManager() == null; + final Set notificationMethods = new HashSet<>(); for (long notificationId : getEffectiveNotifications(userId, deviceId, event.getServerTime())) { Notification notification = getById(notificationId); if (getById(notificationId).getType().equals(event.getType())) { - if (!sentWeb && notification.getWeb()) { - Context.getConnectionManager().updateEvent(userId, event); - sentWeb = true; - } - if (!sentMail && notification.getMail()) { - NotificationMail.sendMailAsync(userId, event, position); - sentMail = true; - } - if (!sentSms && notification.getSms()) { - NotificationSms.sendSmsAsync(userId, event, position); - sentSms = true; - } - } - if (sentWeb && sentMail && sentSms) { - break; + notificationMethods.addAll(notification.getMethods()); } } + for (String nm : notificationMethods) { + NotificatorManager.getNotificator(nm).sendAsync(userId, event, position); + } } } if (Context.getEventForwarder() != null) { diff --git a/src/org/traccar/model/Notification.java b/src/org/traccar/model/Notification.java index cc80f2ae2..0d0b34cd4 100644 --- a/src/org/traccar/model/Notification.java +++ b/src/org/traccar/model/Notification.java @@ -15,6 +15,9 @@ */ package org.traccar.model; +import java.util.HashSet; +import java.util.Set; + public class Notification extends ScheduledModel { private boolean always; @@ -66,4 +69,19 @@ public class Notification extends ScheduledModel { public void setSms(boolean sms) { this.sms = sms; } + + public Set getMethods() { + final Set set = new HashSet<>(); + if (web) { + set.add("web"); + } + if (mail) { + set.add("mail"); + } + if (sms) { + set.add("sms"); + } + return set; + } + } diff --git a/src/org/traccar/notification/NotificationException.java b/src/org/traccar/notification/NotificationException.java new file mode 100644 index 000000000..34d5a80f7 --- /dev/null +++ b/src/org/traccar/notification/NotificationException.java @@ -0,0 +1,25 @@ +/* + * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 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.notification; + +public class NotificationException extends Exception { + + public NotificationException(Throwable cause) { + super(cause); + } + +} diff --git a/src/org/traccar/notification/NotificationMail.java b/src/org/traccar/notification/NotificationMail.java index 6c81ecc79..3a2a19337 100644 --- a/src/org/traccar/notification/NotificationMail.java +++ b/src/org/traccar/notification/NotificationMail.java @@ -32,10 +32,7 @@ import org.traccar.model.Event; import org.traccar.model.Position; import org.traccar.model.User; -public final class NotificationMail { - - private NotificationMail() { - } +public final class NotificationMail extends Notificator { private static Properties getProperties(PropertiesProvider provider) { Properties properties = new Properties(); @@ -84,7 +81,8 @@ public final class NotificationMail { return properties; } - public static void sendMailSync(long userId, Event event, Position position) throws MessagingException { + @Override + public void sendSync(long userId, Event event, Position position) throws NotificationException { User user = Context.getPermissionsManager().getUser(userId); Properties properties = null; @@ -103,40 +101,32 @@ public final class NotificationMail { MimeMessage message = new MimeMessage(session); - String from = properties.getProperty("mail.smtp.from"); - if (from != null) { - message.setFrom(new InternetAddress(from)); - } - - message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail())); - MailMessage mailMessage = NotificationFormatter.formatMailMessage(userId, event, position); - message.setSubject(mailMessage.getSubject()); - message.setSentDate(new Date()); - message.setContent(mailMessage.getBody(), "text/html; charset=utf-8"); - - Transport transport = session.getTransport(); try { - Context.getStatisticsManager().registerMail(); - transport.connect( - properties.getProperty("mail.smtp.host"), - properties.getProperty("mail.smtp.username"), - properties.getProperty("mail.smtp.password")); - transport.sendMessage(message, message.getAllRecipients()); - } finally { - transport.close(); - } - } + String from = properties.getProperty("mail.smtp.from"); + if (from != null) { + message.setFrom(new InternetAddress(from)); + } - public static void sendMailAsync(final long userId, final Event event, final Position position) { - new Thread(new Runnable() { - public void run() { - try { - sendMailSync(userId, event, position); - } catch (MessagingException error) { - Log.warning(error); - } + message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail())); + MailMessage mailMessage = NotificationFormatter.formatMailMessage(userId, event, position); + message.setSubject(mailMessage.getSubject()); + message.setSentDate(new Date()); + message.setContent(mailMessage.getBody(), "text/html; charset=utf-8"); + + Transport transport = session.getTransport(); + try { + Context.getStatisticsManager().registerMail(); + transport.connect( + properties.getProperty("mail.smtp.host"), + properties.getProperty("mail.smtp.username"), + properties.getProperty("mail.smtp.password")); + transport.sendMessage(message, message.getAllRecipients()); + } finally { + transport.close(); } - }).start(); + } catch (MessagingException e) { + throw new NotificationException(e); + } } } diff --git a/src/org/traccar/notification/NotificationNull.java b/src/org/traccar/notification/NotificationNull.java new file mode 100644 index 000000000..afe160561 --- /dev/null +++ b/src/org/traccar/notification/NotificationNull.java @@ -0,0 +1,31 @@ +/* + * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 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.notification; + +import org.traccar.model.Event; +import org.traccar.model.Position; + +public final class NotificationNull extends Notificator { + + @Override + public void sendAsync(long userId, Event event, Position position) { + } + + @Override + public void sendSync(long userId, Event event, Position position) { + } +} 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); } } diff --git a/src/org/traccar/notification/NotificationWeb.java b/src/org/traccar/notification/NotificationWeb.java new file mode 100644 index 000000000..c6a1ae281 --- /dev/null +++ b/src/org/traccar/notification/NotificationWeb.java @@ -0,0 +1,31 @@ +/* + * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 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.notification; + +import org.traccar.Context; +import org.traccar.model.Event; +import org.traccar.model.Position; + +public final class NotificationWeb extends Notificator { + + @Override + public void sendSync(long userId, Event event, Position position) throws NotificationException, + InterruptedException { + Context.getConnectionManager().updateEvent(userId, event); + } + +} diff --git a/src/org/traccar/notification/Notificator.java b/src/org/traccar/notification/Notificator.java new file mode 100644 index 000000000..7192d25c0 --- /dev/null +++ b/src/org/traccar/notification/Notificator.java @@ -0,0 +1,40 @@ +/* + * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 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.notification; + +import org.traccar.helper.Log; +import org.traccar.model.Event; +import org.traccar.model.Position; + +public abstract class Notificator { + + public void sendAsync(final long userId, final Event event, final Position position) { + new Thread(new Runnable() { + public void run() { + try { + sendSync(userId, event, position); + } catch (NotificationException | InterruptedException error) { + Log.warning(error); + } + } + }).start(); + } + + + public abstract void sendSync(long userId, Event event, Position position) + throws NotificationException, InterruptedException; +} diff --git a/src/org/traccar/notification/NotificatorManager.java b/src/org/traccar/notification/NotificatorManager.java new file mode 100644 index 000000000..814c8dc1a --- /dev/null +++ b/src/org/traccar/notification/NotificatorManager.java @@ -0,0 +1,51 @@ +/* + * Copyright 2016 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 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.notification; + +import java.util.HashMap; +import java.util.Map; + +public class NotificatorManager { + + protected NotificatorManager() { + //not called + } + + private static final Map NOTIFICATORS = new HashMap<>(); + private static final Notificator NULL_NOTIFICATOR = new NotificationNull(); + + static { + NOTIFICATORS.put("sms", new NotificationSms()); + NOTIFICATORS.put("mail", new NotificationMail()); + NOTIFICATORS.put("web", new NotificationWeb()); + } + + public static Notificator getNotificator(String type) { + return NOTIFICATORS.getOrDefault(type, NULL_NOTIFICATOR); + } + + public static Notificator getSms() { + return getNotificator("sms"); + } + + public static Notificator getMail() { + return getNotificator("mail"); + } + + +} + diff --git a/src/org/traccar/smpp/SmppClient.java b/src/org/traccar/smpp/SmppClient.java index bf395f90e..97830dd00 100644 --- a/src/org/traccar/smpp/SmppClient.java +++ b/src/org/traccar/smpp/SmppClient.java @@ -25,6 +25,8 @@ import java.util.concurrent.TimeUnit; import org.traccar.Context; import org.traccar.helper.Log; +import org.traccar.sms.SMSException; +import org.traccar.sms.SMSManager; import com.cloudhopper.commons.charset.CharsetUtil; import com.cloudhopper.smpp.SmppBindType; @@ -42,7 +44,7 @@ import com.cloudhopper.smpp.type.SmppChannelException; import com.cloudhopper.smpp.type.SmppTimeoutException; import com.cloudhopper.smpp.type.UnrecoverablePduException; -public class SmppClient { +public class SmppClient implements SMSManager { private SmppSessionConfiguration sessionConfig = new SmppSessionConfiguration(); private SmppSession smppSession; @@ -212,46 +214,52 @@ public class SmppClient { } } + @Override public synchronized void sendMessageSync(String destAddress, String message, boolean command) - throws RecoverablePduException, UnrecoverablePduException, SmppTimeoutException, SmppChannelException, - InterruptedException, IllegalStateException { + throws SMSException, InterruptedException, IllegalStateException { if (getSession() != null && getSession().isBound()) { - SubmitSm submit = new SubmitSm(); - byte[] textBytes; - textBytes = CharsetUtil.encode(message, command ? commandsCharsetName : notificationsCharsetName); - submit.setDataCoding(command ? commandsDataCoding : notificationsDataCoding); - if (requestDlr) { - submit.setRegisteredDelivery(SmppConstants.REGISTERED_DELIVERY_SMSC_RECEIPT_REQUESTED); - } + try { + SubmitSm submit = new SubmitSm(); + byte[] textBytes; + textBytes = CharsetUtil.encode(message, command ? commandsCharsetName : notificationsCharsetName); + submit.setDataCoding(command ? commandsDataCoding : notificationsDataCoding); + if (requestDlr) { + submit.setRegisteredDelivery(SmppConstants.REGISTERED_DELIVERY_SMSC_RECEIPT_REQUESTED); + } - if (textBytes != null && textBytes.length > 255) { - submit.addOptionalParameter(new Tlv(SmppConstants.TAG_MESSAGE_PAYLOAD, textBytes, "message_payload")); - } else { - submit.setShortMessage(textBytes); - } + if (textBytes != null && textBytes.length > 255) { + submit.addOptionalParameter(new Tlv(SmppConstants.TAG_MESSAGE_PAYLOAD, textBytes, + "message_payload")); + } else { + submit.setShortMessage(textBytes); + } - submit.setSourceAddress(command ? new Address(commandSourceTon, commandSourceNpi, commandSourceAddress) - : new Address(sourceTon, sourceNpi, sourceAddress)); - submit.setDestAddress(new Address(destTon, destNpi, destAddress)); - SubmitSmResp submitResponce = getSession().submit(submit, submitTimeout); - if (submitResponce.getCommandStatus() == SmppConstants.STATUS_OK) { - Log.debug("SMS submitted, message id: " + submitResponce.getMessageId()); - } else { - throw new IllegalStateException(submitResponce.getResultMessage()); + submit.setSourceAddress(command ? new Address(commandSourceTon, commandSourceNpi, commandSourceAddress) + : new Address(sourceTon, sourceNpi, sourceAddress)); + submit.setDestAddress(new Address(destTon, destNpi, destAddress)); + SubmitSmResp submitResponce = getSession().submit(submit, submitTimeout); + if (submitResponce.getCommandStatus() == SmppConstants.STATUS_OK) { + Log.debug("SMS submitted, message id: " + submitResponce.getMessageId()); + } else { + throw new IllegalStateException(submitResponce.getResultMessage()); + } + } catch (SmppChannelException | RecoverablePduException + | SmppTimeoutException | UnrecoverablePduException error) { + throw new SMSException(error); } } else { - throw new SmppChannelException("SMPP session is not connected"); + throw new SMSException(new SmppChannelException("SMPP session is not connected")); } } + @Override public void sendMessageAsync(final String destAddress, final String message, final boolean command) { executorService.execute(new Runnable() { @Override public void run() { try { sendMessageSync(destAddress, message, command); - } catch (InterruptedException | RecoverablePduException | UnrecoverablePduException - | SmppTimeoutException | SmppChannelException | IllegalStateException error) { + } catch (SMSException | InterruptedException | IllegalStateException error) { Log.warning(error); } } diff --git a/src/org/traccar/sms/SMSException.java b/src/org/traccar/sms/SMSException.java new file mode 100644 index 000000000..c9c0b3128 --- /dev/null +++ b/src/org/traccar/sms/SMSException.java @@ -0,0 +1,28 @@ +/* + * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 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.sms; + + +import org.traccar.notification.NotificationException; + +public class SMSException extends NotificationException { + + public SMSException(Throwable cause) { + super(cause); + } + +} diff --git a/src/org/traccar/sms/SMSManager.java b/src/org/traccar/sms/SMSManager.java new file mode 100644 index 000000000..b40dae867 --- /dev/null +++ b/src/org/traccar/sms/SMSManager.java @@ -0,0 +1,8 @@ +package org.traccar.sms; + +public interface SMSManager { + + void sendMessageSync(String destAddress, String message, boolean command) throws InterruptedException, SMSException; + void sendMessageAsync(final String destAddress, final String message, final boolean command); + +} -- 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') 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 9339c709e5a6d521fd0bdd4b1221b909c0a888d6 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Mon, 9 Apr 2018 21:20:15 -0300 Subject: configuration support for notificators --- setup/default.xml | 5 +++ .../traccar/notification/NotificatorManager.java | 41 +++++++++++++++------- 2 files changed, 33 insertions(+), 13 deletions(-) (limited to 'src/org/traccar') diff --git a/setup/default.xml b/setup/default.xml index 1788cb901..123f67572 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -27,6 +27,11 @@ ./media + web,mail + org.traccar.notification.NotificationWeb + org.traccar.notification.NotificationSms + org.traccar.notification.NotificationMail + https://www.traccar.org/analytics/ diff --git a/src/org/traccar/notification/NotificatorManager.java b/src/org/traccar/notification/NotificatorManager.java index 1aa76beff..4353b7914 100644 --- a/src/org/traccar/notification/NotificatorManager.java +++ b/src/org/traccar/notification/NotificatorManager.java @@ -16,30 +16,45 @@ */ package org.traccar.notification; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Map; import org.traccar.Context; +import org.traccar.helper.Log; -public class NotificatorManager { +public final class NotificatorManager { - protected NotificatorManager() { - //not called - } - - private static final Map NOTIFICATORS = new HashMap<>(); - private static final Notificator NULL_NOTIFICATOR = new NotificationNull(); + private static final NotificatorManager INSTANCE = new NotificatorManager(); - static { - if (Context.getSmsManager() != null) { - NOTIFICATORS.put("sms", new NotificationSms()); + private NotificatorManager() { + final String[] types = Context.getConfig().getString("notificator.types", "").split(","); + for (String type : types) { + final String className = Context.getConfig().getString("notificator." + type + ".class", ""); + if (className.length() > 0) { + try { + final Class c = (Class) Class.forName(className); + try { + final Constructor constructor = c.getConstructor(new Class[]{String.class}); + notificators.put(type, constructor.newInstance(type)); + } catch (NoSuchMethodException e) { + // No constructor with String argument + notificators.put(type, c.newInstance()); + } + } catch (ClassNotFoundException | InstantiationException + | IllegalAccessException | InvocationTargetException e) { + Log.error("Unable to load notificator class for " + type + " " + className + " " + e.getMessage()); + } + } } - NOTIFICATORS.put("mail", new NotificationMail()); - NOTIFICATORS.put("web", new NotificationWeb()); } + private final Map notificators = new HashMap<>(); + private static final Notificator NULL_NOTIFICATOR = new NotificationNull(); + public static Notificator getNotificator(String type) { - return NOTIFICATORS.getOrDefault(type, NULL_NOTIFICATOR); + return INSTANCE.notificators.getOrDefault(type, NULL_NOTIFICATOR); } public static Notificator getSms() { -- cgit v1.2.3 From 25eb3d86b97f8a0eb806f3e0c572d80c495257d6 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Fri, 1 Jun 2018 07:29:09 -0300 Subject: replace database definition with comma separated list --- schema/changelog-3.18.xml | 57 +++++++++++++++++++++++ src/org/traccar/database/NotificationManager.java | 2 +- src/org/traccar/model/Notification.java | 41 ++++------------ 3 files changed, 67 insertions(+), 33 deletions(-) create mode 100644 schema/changelog-3.18.xml (limited to 'src/org/traccar') diff --git a/schema/changelog-3.18.xml b/schema/changelog-3.18.xml new file mode 100644 index 000000000..85c3b0c33 --- /dev/null +++ b/schema/changelog-3.18.xml @@ -0,0 +1,57 @@ + + + + + + + + + + + + web = 1 AND mail = 1 AND sms = 1 + + + + + web = 1 AND mail = 1 AND sms = 0 + + + + + web = 1 AND mail = 0 AND sms = 0 + + + + + web = 1 AND mail = 0 AND sms = 1 + + + + + web = 0 AND mail = 1 AND sms = 1 + + + + + web = 0 AND mail = 1 AND sms = 0 + + + + + web = 0 AND mail = 0 AND sms = 1 + + + + + + diff --git a/src/org/traccar/database/NotificationManager.java b/src/org/traccar/database/NotificationManager.java index 37c6b720c..10c76181a 100644 --- a/src/org/traccar/database/NotificationManager.java +++ b/src/org/traccar/database/NotificationManager.java @@ -86,7 +86,7 @@ public class NotificationManager extends ExtendedObjectManager { for (long notificationId : getEffectiveNotifications(userId, deviceId, event.getServerTime())) { Notification notification = getById(notificationId); if (getById(notificationId).getType().equals(event.getType())) { - notificationMethods.addAll(notification.getMethods()); + notificationMethods.addAll(notification.getTransportMethods()); } } for (String nm : notificationMethods) { diff --git a/src/org/traccar/model/Notification.java b/src/org/traccar/model/Notification.java index 0d0b34cd4..0b632c861 100644 --- a/src/org/traccar/model/Notification.java +++ b/src/org/traccar/model/Notification.java @@ -40,46 +40,23 @@ public class Notification extends ScheduledModel { this.type = type; } - private boolean web; - public boolean getWeb() { - return web; - } - - public void setWeb(boolean web) { - this.web = web; - } - - private boolean mail; - - public boolean getMail() { - return mail; - } + private String transports; - public void setMail(boolean mail) { - this.mail = mail; + public String getTransports() { + return transports; } - private boolean sms; - - public boolean getSms() { - return sms; + public void setTransports(String transports) { + this.transports = transports; } - public void setSms(boolean sms) { - this.sms = sms; - } - public Set getMethods() { + public Set getTransportMethods() { final Set set = new HashSet<>(); - if (web) { - set.add("web"); - } - if (mail) { - set.add("mail"); - } - if (sms) { - set.add("sms"); + final String[] tmp = transports.split(","); + for (String t : tmp) { + set.add(t.trim()); } return set; } -- 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') 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 633835e8e28a70fddc252e0ee92dcf7131ba167b Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Fri, 1 Jun 2018 07:38:54 -0300 Subject: added warning when using the null notificator --- src/org/traccar/notification/NotificationNull.java | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/org/traccar') diff --git a/src/org/traccar/notification/NotificationNull.java b/src/org/traccar/notification/NotificationNull.java index afe160561..fd7950a80 100644 --- a/src/org/traccar/notification/NotificationNull.java +++ b/src/org/traccar/notification/NotificationNull.java @@ -16,6 +16,7 @@ */ package org.traccar.notification; +import org.traccar.helper.Log; import org.traccar.model.Event; import org.traccar.model.Position; @@ -23,9 +24,11 @@ public final class NotificationNull extends Notificator { @Override public void sendAsync(long userId, Event event, Position position) { + Log.warning("You are using null notificatior, please check your configuration, notification not sent"); } @Override public void sendSync(long userId, Event event, Position position) { + Log.warning("You are using null notificatior, please check your configuration, notification not sent"); } } -- cgit v1.2.3 From 8ca7b2d0d9b5e18dd7e96a2cf5b1b8d8d751051a Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Fri, 1 Jun 2018 07:50:10 -0300 Subject: make compatible with java 7 --- src/org/traccar/notification/NotificatorManager.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/notification/NotificatorManager.java b/src/org/traccar/notification/NotificatorManager.java index 4353b7914..0ffef5d05 100644 --- a/src/org/traccar/notification/NotificatorManager.java +++ b/src/org/traccar/notification/NotificatorManager.java @@ -54,7 +54,12 @@ public final class NotificatorManager { private static final Notificator NULL_NOTIFICATOR = new NotificationNull(); public static Notificator getNotificator(String type) { - return INSTANCE.notificators.getOrDefault(type, NULL_NOTIFICATOR); + final Notificator n = INSTANCE.notificators.get(type); + if (n == null) { + Log.error("No notificator configured for type : " + type); + return NULL_NOTIFICATOR; + } + return n; } public static Notificator getSms() { -- cgit v1.2.3 From 4fc750b585dd6b2953b16408dd57a8ef93fdeee9 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Fri, 1 Jun 2018 09:48:07 -0300 Subject: move NotificatorManager instance to Context --- src/org/traccar/Context.java | 8 ++++++++ src/org/traccar/api/resource/NotificationResource.java | 5 ++--- src/org/traccar/database/NotificationManager.java | 3 +-- src/org/traccar/notification/NotificatorManager.java | 12 +++++------- 4 files changed, 16 insertions(+), 12 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index c9ac4ec12..a7c1817cf 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -73,6 +73,7 @@ import org.traccar.geolocation.OpenCellIdGeolocationProvider; import org.traccar.notification.EventForwarder; import org.traccar.notification.JsonTypeEventForwarder; import org.traccar.notification.MultiPartEventForwarder; +import org.traccar.notification.NotificatorManager; import org.traccar.reports.model.TripsConfig; import org.traccar.sms.SMSManager; import org.traccar.web.WebServer; @@ -196,6 +197,12 @@ public final class Context { return notificationManager; } + private static NotificatorManager notificatorManager; + + public static NotificatorManager getNotificatorManager() { + return notificatorManager; + } + private static VelocityEngine velocityEngine; public static VelocityEngine getVelocityEngine() { @@ -425,6 +432,7 @@ public final class Context { geofenceManager = new GeofenceManager(dataManager); calendarManager = new CalendarManager(dataManager); notificationManager = new NotificationManager(dataManager); + notificatorManager = new NotificatorManager(); Properties velocityProperties = new Properties(); velocityProperties.setProperty("file.resource.loader.path", Context.getConfig().getString("templates.rootPath", "templates") + "/"); diff --git a/src/org/traccar/api/resource/NotificationResource.java b/src/org/traccar/api/resource/NotificationResource.java index 2a398f40d..830e34fc0 100644 --- a/src/org/traccar/api/resource/NotificationResource.java +++ b/src/org/traccar/api/resource/NotificationResource.java @@ -31,7 +31,6 @@ import org.traccar.model.Event; import org.traccar.model.Notification; import org.traccar.model.Typed; import org.traccar.notification.NotificationException; -import org.traccar.notification.NotificatorManager; @Path("notifications") @@ -52,8 +51,8 @@ public class NotificationResource extends ExtendedObjectResource { @POST @Path("test") public Response testMessage() throws NotificationException, InterruptedException { - NotificatorManager.getMail().sendSync(getUserId(), new Event("test", 0), null); - NotificatorManager.getSms().sendSync(getUserId(), new Event("test", 0), null); + Context.getNotificatorManager().getMail().sendSync(getUserId(), new Event("test", 0), null); + Context.getNotificatorManager().getSms().sendSync(getUserId(), new Event("test", 0), null); return Response.noContent().build(); } diff --git a/src/org/traccar/database/NotificationManager.java b/src/org/traccar/database/NotificationManager.java index 10c76181a..a10fbf69a 100644 --- a/src/org/traccar/database/NotificationManager.java +++ b/src/org/traccar/database/NotificationManager.java @@ -32,7 +32,6 @@ import org.traccar.model.Event; import org.traccar.model.Notification; import org.traccar.model.Position; import org.traccar.model.Typed; -import org.traccar.notification.NotificatorManager; public class NotificationManager extends ExtendedObjectManager { @@ -90,7 +89,7 @@ public class NotificationManager extends ExtendedObjectManager { } } for (String nm : notificationMethods) { - NotificatorManager.getNotificator(nm).sendAsync(userId, event, position); + Context.getNotificatorManager().getNotificator(nm).sendAsync(userId, event, position); } } } diff --git a/src/org/traccar/notification/NotificatorManager.java b/src/org/traccar/notification/NotificatorManager.java index 0ffef5d05..c58149847 100644 --- a/src/org/traccar/notification/NotificatorManager.java +++ b/src/org/traccar/notification/NotificatorManager.java @@ -26,9 +26,7 @@ import org.traccar.helper.Log; public final class NotificatorManager { - private static final NotificatorManager INSTANCE = new NotificatorManager(); - - private NotificatorManager() { + public NotificatorManager() { final String[] types = Context.getConfig().getString("notificator.types", "").split(","); for (String type : types) { final String className = Context.getConfig().getString("notificator." + type + ".class", ""); @@ -53,8 +51,8 @@ public final class NotificatorManager { private final Map notificators = new HashMap<>(); private static final Notificator NULL_NOTIFICATOR = new NotificationNull(); - public static Notificator getNotificator(String type) { - final Notificator n = INSTANCE.notificators.get(type); + public Notificator getNotificator(String type) { + final Notificator n = notificators.get(type); if (n == null) { Log.error("No notificator configured for type : " + type); return NULL_NOTIFICATOR; @@ -62,11 +60,11 @@ public final class NotificatorManager { return n; } - public static Notificator getSms() { + public Notificator getSms() { return getNotificator("sms"); } - public static Notificator getMail() { + public Notificator getMail() { return getNotificator("mail"); } -- cgit v1.2.3 From 294c399e5b260313a2d49a0fed1516116a23fbd5 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Fri, 1 Jun 2018 10:43:16 -0300 Subject: update api to support notificators --- .../traccar/api/resource/NotificationResource.java | 21 +++++++++++++++++++-- .../traccar/notification/NotificatorManager.java | 8 +++----- 2 files changed, 22 insertions(+), 7 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/api/resource/NotificationResource.java b/src/org/traccar/api/resource/NotificationResource.java index 830e34fc0..2347b43fa 100644 --- a/src/org/traccar/api/resource/NotificationResource.java +++ b/src/org/traccar/api/resource/NotificationResource.java @@ -21,6 +21,7 @@ import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @@ -51,9 +52,25 @@ public class NotificationResource extends ExtendedObjectResource { @POST @Path("test") public Response testMessage() throws NotificationException, InterruptedException { - Context.getNotificatorManager().getMail().sendSync(getUserId(), new Event("test", 0), null); - Context.getNotificatorManager().getSms().sendSync(getUserId(), new Event("test", 0), null); + for (String method : Context.getNotificatorManager().getNotificatorTypes()) { + Context.getNotificatorManager().getNotificator(method).sendSync(getUserId(), new Event("test", 0), null); + } return Response.noContent().build(); } + @POST + @Path("test/{method}") + public Response testMessage(@PathParam("method") String method) throws NotificationException, InterruptedException { + Context.getNotificatorManager().getNotificator(method).sendSync(getUserId(), new Event("test", 0), null); + return Response.noContent().build(); + } + + + @GET + @Path("notificators") + public Collection getNotificators() { + return Context.getNotificatorManager().getNotificatorTypes(); + } + + } diff --git a/src/org/traccar/notification/NotificatorManager.java b/src/org/traccar/notification/NotificatorManager.java index c58149847..20c7749d2 100644 --- a/src/org/traccar/notification/NotificatorManager.java +++ b/src/org/traccar/notification/NotificatorManager.java @@ -20,6 +20,7 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Map; +import java.util.Set; import org.traccar.Context; import org.traccar.helper.Log; @@ -60,12 +61,9 @@ public final class NotificatorManager { return n; } - public Notificator getSms() { - return getNotificator("sms"); - } - public Notificator getMail() { - return getNotificator("mail"); + public Set getNotificatorTypes() { + return notificators.keySet(); } -- 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') 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