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 From 825ee0d178a24620f075cb4ffb8d49c75b046484 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 26 Jun 2018 12:05:16 +0500 Subject: Cleanup and some adjustments --- schema/changelog-3.18.xml | 57 ---------------------- schema/changelog-4.0.xml | 47 ++++++++++++++++++ setup/default.xml | 2 +- .../traccar/api/resource/NotificationResource.java | 9 ++-- src/org/traccar/database/NotificationManager.java | 5 +- src/org/traccar/model/Notification.java | 18 +++++-- .../notification/NotificationException.java | 4 +- src/org/traccar/notification/NotificationMail.java | 4 +- src/org/traccar/notification/NotificationNull.java | 4 +- src/org/traccar/notification/NotificationSms.java | 4 +- src/org/traccar/notification/NotificationWeb.java | 4 +- src/org/traccar/notification/Notificator.java | 4 +- .../traccar/notification/NotificatorManager.java | 26 ++++++---- src/org/traccar/sms/SMSException.java | 4 +- src/org/traccar/sms/SMSManager.java | 16 ++++++ templates/full/alarm.vm | 10 ++++ templates/full/commandResult.vm | 10 ++++ templates/full/deviceFuelDrop.vm | 9 ++++ templates/full/deviceMoving.vm | 10 ++++ templates/full/deviceOffline.vm | 10 ++++ templates/full/deviceOnline.vm | 10 ++++ templates/full/deviceOverspeed.vm | 19 ++++++++ templates/full/deviceStopped.vm | 10 ++++ templates/full/deviceUnknown.vm | 10 ++++ templates/full/driverChanged.vm | 10 ++++ templates/full/geofenceEnter.vm | 10 ++++ templates/full/geofenceExit.vm | 10 ++++ templates/full/ignitionOff.vm | 10 ++++ templates/full/ignitionOn.vm | 10 ++++ templates/full/maintenance.vm | 10 ++++ templates/full/test.vm | 7 +++ templates/full/textMessage.vm | 9 ++++ templates/full/unknown.vm | 7 +++ templates/mail/alarm.vm | 10 ---- templates/mail/commandResult.vm | 10 ---- templates/mail/deviceFuelDrop.vm | 9 ---- templates/mail/deviceMoving.vm | 10 ---- templates/mail/deviceOffline.vm | 10 ---- templates/mail/deviceOnline.vm | 10 ---- templates/mail/deviceOverspeed.vm | 19 -------- templates/mail/deviceStopped.vm | 10 ---- templates/mail/deviceUnknown.vm | 10 ---- templates/mail/driverChanged.vm | 10 ---- templates/mail/geofenceEnter.vm | 10 ---- templates/mail/geofenceExit.vm | 10 ---- templates/mail/ignitionOff.vm | 10 ---- templates/mail/ignitionOn.vm | 10 ---- templates/mail/maintenance.vm | 10 ---- templates/mail/test.vm | 7 --- templates/mail/textMessage.vm | 9 ---- templates/mail/unknown.vm | 7 --- templates/short/alarm.vm | 1 + templates/short/commandResult.vm | 1 + templates/short/deviceFuelDrop.vm | 1 + templates/short/deviceMoving.vm | 1 + templates/short/deviceOffline.vm | 1 + templates/short/deviceOnline.vm | 1 + templates/short/deviceOverspeed.vm | 10 ++++ templates/short/deviceStopped.vm | 1 + templates/short/deviceUnknown.vm | 1 + templates/short/driverChanged.vm | 6 +++ templates/short/geofenceEnter.vm | 1 + templates/short/geofenceExit.vm | 1 + templates/short/ignitionOff.vm | 1 + templates/short/ignitionOn.vm | 1 + templates/short/maintenance.vm | 1 + templates/short/test.vm | 1 + templates/short/textMessage.vm | 1 + templates/short/unknown.vm | 1 + templates/sms/alarm.vm | 1 - templates/sms/commandResult.vm | 1 - templates/sms/deviceFuelDrop.vm | 1 - templates/sms/deviceMoving.vm | 1 - templates/sms/deviceOffline.vm | 1 - templates/sms/deviceOnline.vm | 1 - templates/sms/deviceOverspeed.vm | 10 ---- templates/sms/deviceStopped.vm | 1 - templates/sms/deviceUnknown.vm | 1 - templates/sms/driverChanged.vm | 6 --- templates/sms/geofenceEnter.vm | 1 - templates/sms/geofenceExit.vm | 1 - templates/sms/ignitionOff.vm | 1 - templates/sms/ignitionOn.vm | 1 - templates/sms/maintenance.vm | 1 - templates/sms/test.vm | 1 - templates/sms/textMessage.vm | 1 - templates/sms/unknown.vm | 1 - 87 files changed, 328 insertions(+), 306 deletions(-) delete mode 100644 schema/changelog-3.18.xml create mode 100644 templates/full/alarm.vm create mode 100644 templates/full/commandResult.vm create mode 100644 templates/full/deviceFuelDrop.vm create mode 100644 templates/full/deviceMoving.vm create mode 100644 templates/full/deviceOffline.vm create mode 100644 templates/full/deviceOnline.vm create mode 100644 templates/full/deviceOverspeed.vm create mode 100644 templates/full/deviceStopped.vm create mode 100644 templates/full/deviceUnknown.vm create mode 100644 templates/full/driverChanged.vm create mode 100644 templates/full/geofenceEnter.vm create mode 100644 templates/full/geofenceExit.vm create mode 100644 templates/full/ignitionOff.vm create mode 100644 templates/full/ignitionOn.vm create mode 100644 templates/full/maintenance.vm create mode 100644 templates/full/test.vm create mode 100644 templates/full/textMessage.vm create mode 100644 templates/full/unknown.vm delete mode 100644 templates/mail/alarm.vm delete mode 100644 templates/mail/commandResult.vm delete mode 100644 templates/mail/deviceFuelDrop.vm delete mode 100644 templates/mail/deviceMoving.vm delete mode 100644 templates/mail/deviceOffline.vm delete mode 100644 templates/mail/deviceOnline.vm delete mode 100644 templates/mail/deviceOverspeed.vm delete mode 100644 templates/mail/deviceStopped.vm delete mode 100644 templates/mail/deviceUnknown.vm delete mode 100644 templates/mail/driverChanged.vm delete mode 100644 templates/mail/geofenceEnter.vm delete mode 100644 templates/mail/geofenceExit.vm delete mode 100644 templates/mail/ignitionOff.vm delete mode 100644 templates/mail/ignitionOn.vm delete mode 100644 templates/mail/maintenance.vm delete mode 100644 templates/mail/test.vm delete mode 100644 templates/mail/textMessage.vm delete mode 100644 templates/mail/unknown.vm create mode 100644 templates/short/alarm.vm create mode 100644 templates/short/commandResult.vm create mode 100644 templates/short/deviceFuelDrop.vm create mode 100644 templates/short/deviceMoving.vm create mode 100644 templates/short/deviceOffline.vm create mode 100644 templates/short/deviceOnline.vm create mode 100644 templates/short/deviceOverspeed.vm create mode 100644 templates/short/deviceStopped.vm create mode 100644 templates/short/deviceUnknown.vm create mode 100644 templates/short/driverChanged.vm create mode 100644 templates/short/geofenceEnter.vm create mode 100644 templates/short/geofenceExit.vm create mode 100644 templates/short/ignitionOff.vm create mode 100644 templates/short/ignitionOn.vm create mode 100644 templates/short/maintenance.vm create mode 100644 templates/short/test.vm create mode 100644 templates/short/textMessage.vm create mode 100644 templates/short/unknown.vm delete mode 100644 templates/sms/alarm.vm delete mode 100644 templates/sms/commandResult.vm delete mode 100644 templates/sms/deviceFuelDrop.vm delete mode 100644 templates/sms/deviceMoving.vm delete mode 100644 templates/sms/deviceOffline.vm delete mode 100644 templates/sms/deviceOnline.vm delete mode 100644 templates/sms/deviceOverspeed.vm delete mode 100644 templates/sms/deviceStopped.vm delete mode 100644 templates/sms/deviceUnknown.vm delete mode 100644 templates/sms/driverChanged.vm delete mode 100644 templates/sms/geofenceEnter.vm delete mode 100644 templates/sms/geofenceExit.vm delete mode 100644 templates/sms/ignitionOff.vm delete mode 100644 templates/sms/ignitionOn.vm delete mode 100644 templates/sms/maintenance.vm delete mode 100644 templates/sms/test.vm delete mode 100644 templates/sms/textMessage.vm delete mode 100644 templates/sms/unknown.vm (limited to 'src/org/traccar') diff --git a/schema/changelog-3.18.xml b/schema/changelog-3.18.xml deleted file mode 100644 index 85c3b0c33..000000000 --- a/schema/changelog-3.18.xml +++ /dev/null @@ -1,57 +0,0 @@ - - - - - - - - - - - - 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/schema/changelog-4.0.xml b/schema/changelog-4.0.xml index 04e9fe0c4..82403c83c 100644 --- a/schema/changelog-4.0.xml +++ b/schema/changelog-4.0.xml @@ -53,4 +53,51 @@ + + + + + + + + + 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/setup/default.xml b/setup/default.xml index 353ba36a7..d7c1e5f0f 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -30,8 +30,8 @@ web,mail org.traccar.notification.NotificationWeb - org.traccar.notification.NotificationSms org.traccar.notification.NotificationMail + org.traccar.notification.NotificationSms https://www.traccar.org/analytics/ diff --git a/src/org/traccar/api/resource/NotificationResource.java b/src/org/traccar/api/resource/NotificationResource.java index 2347b43fa..9046569a0 100644 --- a/src/org/traccar/api/resource/NotificationResource.java +++ b/src/org/traccar/api/resource/NotificationResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2018 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. @@ -52,8 +52,9 @@ public class NotificationResource extends ExtendedObjectResource { @POST @Path("test") public Response testMessage() throws NotificationException, InterruptedException { - for (String method : Context.getNotificatorManager().getNotificatorTypes()) { - Context.getNotificatorManager().getNotificator(method).sendSync(getUserId(), new Event("test", 0), null); + for (Typed method : Context.getNotificatorManager().getNotificatorTypes()) { + Context.getNotificatorManager() + .getNotificator(method.getType()).sendSync(getUserId(), new Event("test", 0), null); } return Response.noContent().build(); } @@ -68,7 +69,7 @@ public class NotificationResource extends ExtendedObjectResource { @GET @Path("notificators") - public Collection getNotificators() { + public Collection getNotificators() { return Context.getNotificatorManager().getNotificatorTypes(); } diff --git a/src/org/traccar/database/NotificationManager.java b/src/org/traccar/database/NotificationManager.java index 2c1ffc09c..b2a30e4f1 100644 --- a/src/org/traccar/database/NotificationManager.java +++ b/src/org/traccar/database/NotificationManager.java @@ -90,8 +90,9 @@ public class NotificationManager extends ExtendedObjectManager { notificationMethods.addAll(notification.getTransportMethods()); } } - for (String nm : notificationMethods) { - Context.getNotificatorManager().getNotificator(nm).sendAsync(userId, event, position); + for (String notificationMethod : notificationMethods) { + Context.getNotificatorManager() + .getNotificator(notificationMethod).sendAsync(userId, event, position); } } } diff --git a/src/org/traccar/model/Notification.java b/src/org/traccar/model/Notification.java index 0b632c861..4923798c2 100644 --- a/src/org/traccar/model/Notification.java +++ b/src/org/traccar/model/Notification.java @@ -18,6 +18,10 @@ package org.traccar.model; import java.util.HashSet; import java.util.Set; +import org.traccar.database.QueryIgnore; + +import com.fasterxml.jackson.annotation.JsonIgnore; + public class Notification extends ScheduledModel { private boolean always; @@ -52,13 +56,17 @@ public class Notification extends ScheduledModel { } + @JsonIgnore + @QueryIgnore public Set getTransportMethods() { - final Set set = new HashSet<>(); - final String[] tmp = transports.split(","); - for (String t : tmp) { - set.add(t.trim()); + final Set result = new HashSet<>(); + if (transports != null) { + final String[] transportsList = transports.split(","); + for (String transport : transportsList) { + result.add(transport.trim()); + } } - return set; + return result; } } diff --git a/src/org/traccar/notification/NotificationException.java b/src/org/traccar/notification/NotificationException.java index 34d5a80f7..525e5ee37 100644 --- a/src/org/traccar/notification/NotificationException.java +++ b/src/org/traccar/notification/NotificationException.java @@ -1,6 +1,6 @@ /* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) + * 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. diff --git a/src/org/traccar/notification/NotificationMail.java b/src/org/traccar/notification/NotificationMail.java index fb89bf5bd..76387c73b 100644 --- a/src/org/traccar/notification/NotificationMail.java +++ b/src/org/traccar/notification/NotificationMail.java @@ -1,6 +1,6 @@ /* - * Copyright 2016 - 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) + * Copyright 2016 - 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2017 - 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. diff --git a/src/org/traccar/notification/NotificationNull.java b/src/org/traccar/notification/NotificationNull.java index fd7950a80..3ee954c24 100644 --- a/src/org/traccar/notification/NotificationNull.java +++ b/src/org/traccar/notification/NotificationNull.java @@ -1,6 +1,6 @@ /* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) + * 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. diff --git a/src/org/traccar/notification/NotificationSms.java b/src/org/traccar/notification/NotificationSms.java index 63fd57895..dd3304d85 100644 --- a/src/org/traccar/notification/NotificationSms.java +++ b/src/org/traccar/notification/NotificationSms.java @@ -1,6 +1,6 @@ /* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) + * Copyright 2017 - 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2017 - 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. diff --git a/src/org/traccar/notification/NotificationWeb.java b/src/org/traccar/notification/NotificationWeb.java index c6a1ae281..8e04fc2c4 100644 --- a/src/org/traccar/notification/NotificationWeb.java +++ b/src/org/traccar/notification/NotificationWeb.java @@ -1,6 +1,6 @@ /* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) + * 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. diff --git a/src/org/traccar/notification/Notificator.java b/src/org/traccar/notification/Notificator.java index 7192d25c0..09f98d3ad 100644 --- a/src/org/traccar/notification/Notificator.java +++ b/src/org/traccar/notification/Notificator.java @@ -1,6 +1,6 @@ /* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) + * 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. diff --git a/src/org/traccar/notification/NotificatorManager.java b/src/org/traccar/notification/NotificatorManager.java index 20c7749d2..87a294345 100644 --- a/src/org/traccar/notification/NotificatorManager.java +++ b/src/org/traccar/notification/NotificatorManager.java @@ -1,6 +1,6 @@ /* - * Copyright 2016 - 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) + * 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. @@ -19,11 +19,13 @@ package org.traccar.notification; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; import java.util.Set; import org.traccar.Context; import org.traccar.helper.Log; +import org.traccar.model.Typed; public final class NotificatorManager { @@ -33,13 +35,13 @@ public final class NotificatorManager { final String className = Context.getConfig().getString("notificator." + type + ".class", ""); if (className.length() > 0) { try { - final Class c = (Class) Class.forName(className); + final Class clazz = (Class) Class.forName(className); try { - final Constructor constructor = c.getConstructor(new Class[]{String.class}); + final Constructor constructor = clazz.getConstructor(new Class[]{String.class}); notificators.put(type, constructor.newInstance(type)); } catch (NoSuchMethodException e) { // No constructor with String argument - notificators.put(type, c.newInstance()); + notificators.put(type, clazz.newInstance()); } } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | InvocationTargetException e) { @@ -53,17 +55,21 @@ public final class NotificatorManager { private static final Notificator NULL_NOTIFICATOR = new NotificationNull(); public Notificator getNotificator(String type) { - final Notificator n = notificators.get(type); - if (n == null) { + final Notificator notificator = notificators.get(type); + if (notificator == null) { Log.error("No notificator configured for type : " + type); return NULL_NOTIFICATOR; } - return n; + return notificator; } - public Set getNotificatorTypes() { - return notificators.keySet(); + public Set getNotificatorTypes() { + Set result = new HashSet<>(); + for (String notificator : notificators.keySet()) { + result.add(new Typed(notificator)); + } + return result; } diff --git a/src/org/traccar/sms/SMSException.java b/src/org/traccar/sms/SMSException.java index c9c0b3128..22ca95296 100644 --- a/src/org/traccar/sms/SMSException.java +++ b/src/org/traccar/sms/SMSException.java @@ -1,6 +1,6 @@ /* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) + * 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. diff --git a/src/org/traccar/sms/SMSManager.java b/src/org/traccar/sms/SMSManager.java index b40dae867..c4cb68fb7 100644 --- a/src/org/traccar/sms/SMSManager.java +++ b/src/org/traccar/sms/SMSManager.java @@ -1,3 +1,19 @@ +/* + * 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.sms; public interface SMSManager { diff --git a/templates/full/alarm.vm b/templates/full/alarm.vm new file mode 100644 index 000000000..8f1164291 --- /dev/null +++ b/templates/full/alarm.vm @@ -0,0 +1,10 @@ +#set($subject = "$device.name: alarm!") + + + +Device: $device.name
+Alarm: $position.getString("alarm")
+Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
+Point: #{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}
+ + diff --git a/templates/full/commandResult.vm b/templates/full/commandResult.vm new file mode 100644 index 000000000..5b6d8ef3e --- /dev/null +++ b/templates/full/commandResult.vm @@ -0,0 +1,10 @@ +#set($subject = "$device.name: command result received") + + + +Device: $device.name
+Result: $position.getString("result")
+Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
+Link: $webUrl?eventId=$event.id + + diff --git a/templates/full/deviceFuelDrop.vm b/templates/full/deviceFuelDrop.vm new file mode 100644 index 000000000..c30ec3a1c --- /dev/null +++ b/templates/full/deviceFuelDrop.vm @@ -0,0 +1,9 @@ +#set($subject = "$device.name: fuel drop") + + + +Device: $device.name
+Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
+Point: #{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}
+ + diff --git a/templates/full/deviceMoving.vm b/templates/full/deviceMoving.vm new file mode 100644 index 000000000..6e98af1de --- /dev/null +++ b/templates/full/deviceMoving.vm @@ -0,0 +1,10 @@ +#set($subject = "$device.name: moving") + + + +Device: $device.name
+Moving
+Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
+Point: #{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}
+ + diff --git a/templates/full/deviceOffline.vm b/templates/full/deviceOffline.vm new file mode 100644 index 000000000..1f6d02fb2 --- /dev/null +++ b/templates/full/deviceOffline.vm @@ -0,0 +1,10 @@ +#set($subject = "$device.name: offline") + + + +Device: $device.name
+Offline
+Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
+Link: $webUrl?eventId=$event.id + + diff --git a/templates/full/deviceOnline.vm b/templates/full/deviceOnline.vm new file mode 100644 index 000000000..9211eff11 --- /dev/null +++ b/templates/full/deviceOnline.vm @@ -0,0 +1,10 @@ +#set($subject = "$device.name: online") + + + +Device: $device.name
+Online
+Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
+Link: $webUrl?eventId=$event.id + + diff --git a/templates/full/deviceOverspeed.vm b/templates/full/deviceOverspeed.vm new file mode 100644 index 000000000..e5e576f3a --- /dev/null +++ b/templates/full/deviceOverspeed.vm @@ -0,0 +1,19 @@ +#set($subject = "$device.name: exceeds the speed") +#if($speedUnit == 'kmh') +#set($speedValue = $position.speed * 1.852) +#set($speedString = $numberTool.format("0.0 km/h", $speedValue)) +#elseif($speedUnit == 'mph') +#set($speedValue = $position.speed * 1.15078) +#set($speedString = $numberTool.format("0.0 mph", $speedValue)) +#else +#set($speedString = $numberTool.format("0.0 kn", $position.speed)) +#end + + + +Device: $device.name
+Exceeds the speed: $speedString#{if}($geofence) in $geofence.name#{else}#{end}
+Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
+Point: #{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}
+ + diff --git a/templates/full/deviceStopped.vm b/templates/full/deviceStopped.vm new file mode 100644 index 000000000..2ec0f8db9 --- /dev/null +++ b/templates/full/deviceStopped.vm @@ -0,0 +1,10 @@ +#set($subject = "$device.name: stopped") + + + +Device: $device.name
+Stopped
+Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
+Point: #{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}
+ + diff --git a/templates/full/deviceUnknown.vm b/templates/full/deviceUnknown.vm new file mode 100644 index 000000000..34fa01a8a --- /dev/null +++ b/templates/full/deviceUnknown.vm @@ -0,0 +1,10 @@ +#set($subject = "$device.name: status is unknown") + + + +Device: $device.name
+Status is unknown
+Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
+Link: $webUrl?eventId=$event.id + + diff --git a/templates/full/driverChanged.vm b/templates/full/driverChanged.vm new file mode 100644 index 000000000..ba1e68661 --- /dev/null +++ b/templates/full/driverChanged.vm @@ -0,0 +1,10 @@ +#set($subject = "$device.name: driver has changed") + + + +Device: $device.name
+Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
+Point: #{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}
+Driver: #{if}($driver)$driver.name#{else}$event.getString("driverUniqueId")#{end} + + diff --git a/templates/full/geofenceEnter.vm b/templates/full/geofenceEnter.vm new file mode 100644 index 000000000..e83a0de62 --- /dev/null +++ b/templates/full/geofenceEnter.vm @@ -0,0 +1,10 @@ +#set($subject = "$device.name: has entered geofence") + + + +Device: $device.name
+Has entered geofence: $geofence.name
+Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
+Point: #{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}
+ + diff --git a/templates/full/geofenceExit.vm b/templates/full/geofenceExit.vm new file mode 100644 index 000000000..3557f6eb2 --- /dev/null +++ b/templates/full/geofenceExit.vm @@ -0,0 +1,10 @@ +#set($subject = "$device.name: has exited geofence") + + + +Device: $device.name
+Has exited geofence: $geofence.name
+Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
+Point: #{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}
+ + diff --git a/templates/full/ignitionOff.vm b/templates/full/ignitionOff.vm new file mode 100644 index 000000000..0281eef02 --- /dev/null +++ b/templates/full/ignitionOff.vm @@ -0,0 +1,10 @@ +#set($subject = "$device.name: ignition OFF") + + + +Device: $device.name
+Ignition OFF
+Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
+Point: #{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}
+ + diff --git a/templates/full/ignitionOn.vm b/templates/full/ignitionOn.vm new file mode 100644 index 000000000..27135a7f0 --- /dev/null +++ b/templates/full/ignitionOn.vm @@ -0,0 +1,10 @@ +#set($subject = "$device.name: ignition ON") + + + +Device: $device.name
+Ignition ON
+Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
+Point: #{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}
+ + diff --git a/templates/full/maintenance.vm b/templates/full/maintenance.vm new file mode 100644 index 000000000..c6973f97a --- /dev/null +++ b/templates/full/maintenance.vm @@ -0,0 +1,10 @@ +#set($subject = "$device.name: maintenance is required") + + + +Device: $device.name
+Maintenance is required: $maintenance.name
+Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
+Point: #{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}
+ + diff --git a/templates/full/test.vm b/templates/full/test.vm new file mode 100644 index 000000000..93cbdc549 --- /dev/null +++ b/templates/full/test.vm @@ -0,0 +1,7 @@ +#set($subject = "Test message") + + + +Test message + + diff --git a/templates/full/textMessage.vm b/templates/full/textMessage.vm new file mode 100644 index 000000000..174173c85 --- /dev/null +++ b/templates/full/textMessage.vm @@ -0,0 +1,9 @@ +#set($subject = "$device.name: text message received") + + + +Device: $device.name
+Message: $event.getString("message")
+Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
+ + diff --git a/templates/full/unknown.vm b/templates/full/unknown.vm new file mode 100644 index 000000000..566ce0d42 --- /dev/null +++ b/templates/full/unknown.vm @@ -0,0 +1,7 @@ +#set($subject = "Unknown type") + + + +Unknown type + + diff --git a/templates/mail/alarm.vm b/templates/mail/alarm.vm deleted file mode 100644 index 8f1164291..000000000 --- a/templates/mail/alarm.vm +++ /dev/null @@ -1,10 +0,0 @@ -#set($subject = "$device.name: alarm!") - - - -Device: $device.name
-Alarm: $position.getString("alarm")
-Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
-Point: #{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}
- - diff --git a/templates/mail/commandResult.vm b/templates/mail/commandResult.vm deleted file mode 100644 index 5b6d8ef3e..000000000 --- a/templates/mail/commandResult.vm +++ /dev/null @@ -1,10 +0,0 @@ -#set($subject = "$device.name: command result received") - - - -Device: $device.name
-Result: $position.getString("result")
-Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
-Link: $webUrl?eventId=$event.id - - diff --git a/templates/mail/deviceFuelDrop.vm b/templates/mail/deviceFuelDrop.vm deleted file mode 100644 index c30ec3a1c..000000000 --- a/templates/mail/deviceFuelDrop.vm +++ /dev/null @@ -1,9 +0,0 @@ -#set($subject = "$device.name: fuel drop") - - - -Device: $device.name
-Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
-Point: #{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}
- - diff --git a/templates/mail/deviceMoving.vm b/templates/mail/deviceMoving.vm deleted file mode 100644 index 6e98af1de..000000000 --- a/templates/mail/deviceMoving.vm +++ /dev/null @@ -1,10 +0,0 @@ -#set($subject = "$device.name: moving") - - - -Device: $device.name
-Moving
-Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
-Point: #{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}
- - diff --git a/templates/mail/deviceOffline.vm b/templates/mail/deviceOffline.vm deleted file mode 100644 index 1f6d02fb2..000000000 --- a/templates/mail/deviceOffline.vm +++ /dev/null @@ -1,10 +0,0 @@ -#set($subject = "$device.name: offline") - - - -Device: $device.name
-Offline
-Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
-Link: $webUrl?eventId=$event.id - - diff --git a/templates/mail/deviceOnline.vm b/templates/mail/deviceOnline.vm deleted file mode 100644 index 9211eff11..000000000 --- a/templates/mail/deviceOnline.vm +++ /dev/null @@ -1,10 +0,0 @@ -#set($subject = "$device.name: online") - - - -Device: $device.name
-Online
-Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
-Link: $webUrl?eventId=$event.id - - diff --git a/templates/mail/deviceOverspeed.vm b/templates/mail/deviceOverspeed.vm deleted file mode 100644 index e5e576f3a..000000000 --- a/templates/mail/deviceOverspeed.vm +++ /dev/null @@ -1,19 +0,0 @@ -#set($subject = "$device.name: exceeds the speed") -#if($speedUnit == 'kmh') -#set($speedValue = $position.speed * 1.852) -#set($speedString = $numberTool.format("0.0 km/h", $speedValue)) -#elseif($speedUnit == 'mph') -#set($speedValue = $position.speed * 1.15078) -#set($speedString = $numberTool.format("0.0 mph", $speedValue)) -#else -#set($speedString = $numberTool.format("0.0 kn", $position.speed)) -#end - - - -Device: $device.name
-Exceeds the speed: $speedString#{if}($geofence) in $geofence.name#{else}#{end}
-Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
-Point: #{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}
- - diff --git a/templates/mail/deviceStopped.vm b/templates/mail/deviceStopped.vm deleted file mode 100644 index 2ec0f8db9..000000000 --- a/templates/mail/deviceStopped.vm +++ /dev/null @@ -1,10 +0,0 @@ -#set($subject = "$device.name: stopped") - - - -Device: $device.name
-Stopped
-Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
-Point: #{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}
- - diff --git a/templates/mail/deviceUnknown.vm b/templates/mail/deviceUnknown.vm deleted file mode 100644 index 34fa01a8a..000000000 --- a/templates/mail/deviceUnknown.vm +++ /dev/null @@ -1,10 +0,0 @@ -#set($subject = "$device.name: status is unknown") - - - -Device: $device.name
-Status is unknown
-Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
-Link: $webUrl?eventId=$event.id - - diff --git a/templates/mail/driverChanged.vm b/templates/mail/driverChanged.vm deleted file mode 100644 index ba1e68661..000000000 --- a/templates/mail/driverChanged.vm +++ /dev/null @@ -1,10 +0,0 @@ -#set($subject = "$device.name: driver has changed") - - - -Device: $device.name
-Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
-Point: #{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}
-Driver: #{if}($driver)$driver.name#{else}$event.getString("driverUniqueId")#{end} - - diff --git a/templates/mail/geofenceEnter.vm b/templates/mail/geofenceEnter.vm deleted file mode 100644 index e83a0de62..000000000 --- a/templates/mail/geofenceEnter.vm +++ /dev/null @@ -1,10 +0,0 @@ -#set($subject = "$device.name: has entered geofence") - - - -Device: $device.name
-Has entered geofence: $geofence.name
-Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
-Point: #{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}
- - diff --git a/templates/mail/geofenceExit.vm b/templates/mail/geofenceExit.vm deleted file mode 100644 index 3557f6eb2..000000000 --- a/templates/mail/geofenceExit.vm +++ /dev/null @@ -1,10 +0,0 @@ -#set($subject = "$device.name: has exited geofence") - - - -Device: $device.name
-Has exited geofence: $geofence.name
-Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
-Point: #{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}
- - diff --git a/templates/mail/ignitionOff.vm b/templates/mail/ignitionOff.vm deleted file mode 100644 index 0281eef02..000000000 --- a/templates/mail/ignitionOff.vm +++ /dev/null @@ -1,10 +0,0 @@ -#set($subject = "$device.name: ignition OFF") - - - -Device: $device.name
-Ignition OFF
-Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
-Point: #{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}
- - diff --git a/templates/mail/ignitionOn.vm b/templates/mail/ignitionOn.vm deleted file mode 100644 index 27135a7f0..000000000 --- a/templates/mail/ignitionOn.vm +++ /dev/null @@ -1,10 +0,0 @@ -#set($subject = "$device.name: ignition ON") - - - -Device: $device.name
-Ignition ON
-Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
-Point: #{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}
- - diff --git a/templates/mail/maintenance.vm b/templates/mail/maintenance.vm deleted file mode 100644 index c6973f97a..000000000 --- a/templates/mail/maintenance.vm +++ /dev/null @@ -1,10 +0,0 @@ -#set($subject = "$device.name: maintenance is required") - - - -Device: $device.name
-Maintenance is required: $maintenance.name
-Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
-Point: #{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}
- - diff --git a/templates/mail/test.vm b/templates/mail/test.vm deleted file mode 100644 index 93cbdc549..000000000 --- a/templates/mail/test.vm +++ /dev/null @@ -1,7 +0,0 @@ -#set($subject = "Test message") - - - -Test message - - diff --git a/templates/mail/textMessage.vm b/templates/mail/textMessage.vm deleted file mode 100644 index 174173c85..000000000 --- a/templates/mail/textMessage.vm +++ /dev/null @@ -1,9 +0,0 @@ -#set($subject = "$device.name: text message received") - - - -Device: $device.name
-Message: $event.getString("message")
-Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
- - diff --git a/templates/mail/unknown.vm b/templates/mail/unknown.vm deleted file mode 100644 index 566ce0d42..000000000 --- a/templates/mail/unknown.vm +++ /dev/null @@ -1,7 +0,0 @@ -#set($subject = "Unknown type") - - - -Unknown type - - diff --git a/templates/short/alarm.vm b/templates/short/alarm.vm new file mode 100644 index 000000000..389341cf1 --- /dev/null +++ b/templates/short/alarm.vm @@ -0,0 +1 @@ +$device.name alarm: $position.getString("alarm") at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/short/commandResult.vm b/templates/short/commandResult.vm new file mode 100644 index 000000000..4a327d850 --- /dev/null +++ b/templates/short/commandResult.vm @@ -0,0 +1 @@ +$device.name command result received: $position.getString("result") at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/short/deviceFuelDrop.vm b/templates/short/deviceFuelDrop.vm new file mode 100644 index 000000000..5e51396d4 --- /dev/null +++ b/templates/short/deviceFuelDrop.vm @@ -0,0 +1 @@ +$device.name fuel drop at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/short/deviceMoving.vm b/templates/short/deviceMoving.vm new file mode 100644 index 000000000..aaa72ab0d --- /dev/null +++ b/templates/short/deviceMoving.vm @@ -0,0 +1 @@ +$device.name moving at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/short/deviceOffline.vm b/templates/short/deviceOffline.vm new file mode 100644 index 000000000..8c4a02335 --- /dev/null +++ b/templates/short/deviceOffline.vm @@ -0,0 +1 @@ +$device.name offline at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/short/deviceOnline.vm b/templates/short/deviceOnline.vm new file mode 100644 index 000000000..62854feb0 --- /dev/null +++ b/templates/short/deviceOnline.vm @@ -0,0 +1 @@ +$device.name online at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/short/deviceOverspeed.vm b/templates/short/deviceOverspeed.vm new file mode 100644 index 000000000..5e92f662c --- /dev/null +++ b/templates/short/deviceOverspeed.vm @@ -0,0 +1,10 @@ +#if($speedUnit == 'kmh') +#set($speedValue = $position.speed * 1.852) +#set($speedString = $numberTool.format("0.0 km/h", $speedValue)) +#elseif($speedUnit == 'mph') +#set($speedValue = $position.speed * 1.15078) +#set($speedString = $numberTool.format("0.0 mph", $speedValue)) +#else +#set($speedString = $numberTool.format("0.0 kn", $position.speed)) +#end +$device.name exceeds the speed $speedString#{if}($geofence) in $geofence.name#{else}#{end} at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/short/deviceStopped.vm b/templates/short/deviceStopped.vm new file mode 100644 index 000000000..c2437b5f6 --- /dev/null +++ b/templates/short/deviceStopped.vm @@ -0,0 +1 @@ +$device.name stopped at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/short/deviceUnknown.vm b/templates/short/deviceUnknown.vm new file mode 100644 index 000000000..07f39f3a9 --- /dev/null +++ b/templates/short/deviceUnknown.vm @@ -0,0 +1 @@ +$device.name status is unknown at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/short/driverChanged.vm b/templates/short/driverChanged.vm new file mode 100644 index 000000000..50849df7c --- /dev/null +++ b/templates/short/driverChanged.vm @@ -0,0 +1,6 @@ +#if($driver) +#set($driverName = $driver.name) +#else +#set($driverName = $event.getString("driverUniqueId")) +#end +Driver $driverName has changed in $device.name at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/short/geofenceEnter.vm b/templates/short/geofenceEnter.vm new file mode 100644 index 000000000..debdbf356 --- /dev/null +++ b/templates/short/geofenceEnter.vm @@ -0,0 +1 @@ +$device.name has entered geofence $geofence.name at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/short/geofenceExit.vm b/templates/short/geofenceExit.vm new file mode 100644 index 000000000..8553458f7 --- /dev/null +++ b/templates/short/geofenceExit.vm @@ -0,0 +1 @@ +$device.name has exited geofence $geofence.name at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/short/ignitionOff.vm b/templates/short/ignitionOff.vm new file mode 100644 index 000000000..ddf860413 --- /dev/null +++ b/templates/short/ignitionOff.vm @@ -0,0 +1 @@ +$device.name ignition OFF at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/short/ignitionOn.vm b/templates/short/ignitionOn.vm new file mode 100644 index 000000000..00b365be9 --- /dev/null +++ b/templates/short/ignitionOn.vm @@ -0,0 +1 @@ +$device.name ignition ON at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/short/maintenance.vm b/templates/short/maintenance.vm new file mode 100644 index 000000000..621beae46 --- /dev/null +++ b/templates/short/maintenance.vm @@ -0,0 +1 @@ +$device.name maintenance $maintenance.name is required at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/short/test.vm b/templates/short/test.vm new file mode 100644 index 000000000..d25f218ce --- /dev/null +++ b/templates/short/test.vm @@ -0,0 +1 @@ +Test message diff --git a/templates/short/textMessage.vm b/templates/short/textMessage.vm new file mode 100644 index 000000000..dc42f29f1 --- /dev/null +++ b/templates/short/textMessage.vm @@ -0,0 +1 @@ +Text message received from $device.name at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/short/unknown.vm b/templates/short/unknown.vm new file mode 100644 index 000000000..fd20b50cc --- /dev/null +++ b/templates/short/unknown.vm @@ -0,0 +1 @@ +Unknown type diff --git a/templates/sms/alarm.vm b/templates/sms/alarm.vm deleted file mode 100644 index 389341cf1..000000000 --- a/templates/sms/alarm.vm +++ /dev/null @@ -1 +0,0 @@ -$device.name alarm: $position.getString("alarm") at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/sms/commandResult.vm b/templates/sms/commandResult.vm deleted file mode 100644 index 4a327d850..000000000 --- a/templates/sms/commandResult.vm +++ /dev/null @@ -1 +0,0 @@ -$device.name command result received: $position.getString("result") at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/sms/deviceFuelDrop.vm b/templates/sms/deviceFuelDrop.vm deleted file mode 100644 index 5e51396d4..000000000 --- a/templates/sms/deviceFuelDrop.vm +++ /dev/null @@ -1 +0,0 @@ -$device.name fuel drop at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/sms/deviceMoving.vm b/templates/sms/deviceMoving.vm deleted file mode 100644 index aaa72ab0d..000000000 --- a/templates/sms/deviceMoving.vm +++ /dev/null @@ -1 +0,0 @@ -$device.name moving at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/sms/deviceOffline.vm b/templates/sms/deviceOffline.vm deleted file mode 100644 index 8c4a02335..000000000 --- a/templates/sms/deviceOffline.vm +++ /dev/null @@ -1 +0,0 @@ -$device.name offline at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/sms/deviceOnline.vm b/templates/sms/deviceOnline.vm deleted file mode 100644 index 62854feb0..000000000 --- a/templates/sms/deviceOnline.vm +++ /dev/null @@ -1 +0,0 @@ -$device.name online at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/sms/deviceOverspeed.vm b/templates/sms/deviceOverspeed.vm deleted file mode 100644 index 5e92f662c..000000000 --- a/templates/sms/deviceOverspeed.vm +++ /dev/null @@ -1,10 +0,0 @@ -#if($speedUnit == 'kmh') -#set($speedValue = $position.speed * 1.852) -#set($speedString = $numberTool.format("0.0 km/h", $speedValue)) -#elseif($speedUnit == 'mph') -#set($speedValue = $position.speed * 1.15078) -#set($speedString = $numberTool.format("0.0 mph", $speedValue)) -#else -#set($speedString = $numberTool.format("0.0 kn", $position.speed)) -#end -$device.name exceeds the speed $speedString#{if}($geofence) in $geofence.name#{else}#{end} at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/sms/deviceStopped.vm b/templates/sms/deviceStopped.vm deleted file mode 100644 index c2437b5f6..000000000 --- a/templates/sms/deviceStopped.vm +++ /dev/null @@ -1 +0,0 @@ -$device.name stopped at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/sms/deviceUnknown.vm b/templates/sms/deviceUnknown.vm deleted file mode 100644 index 07f39f3a9..000000000 --- a/templates/sms/deviceUnknown.vm +++ /dev/null @@ -1 +0,0 @@ -$device.name status is unknown at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/sms/driverChanged.vm b/templates/sms/driverChanged.vm deleted file mode 100644 index 50849df7c..000000000 --- a/templates/sms/driverChanged.vm +++ /dev/null @@ -1,6 +0,0 @@ -#if($driver) -#set($driverName = $driver.name) -#else -#set($driverName = $event.getString("driverUniqueId")) -#end -Driver $driverName has changed in $device.name at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/sms/geofenceEnter.vm b/templates/sms/geofenceEnter.vm deleted file mode 100644 index debdbf356..000000000 --- a/templates/sms/geofenceEnter.vm +++ /dev/null @@ -1 +0,0 @@ -$device.name has entered geofence $geofence.name at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/sms/geofenceExit.vm b/templates/sms/geofenceExit.vm deleted file mode 100644 index 8553458f7..000000000 --- a/templates/sms/geofenceExit.vm +++ /dev/null @@ -1 +0,0 @@ -$device.name has exited geofence $geofence.name at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/sms/ignitionOff.vm b/templates/sms/ignitionOff.vm deleted file mode 100644 index ddf860413..000000000 --- a/templates/sms/ignitionOff.vm +++ /dev/null @@ -1 +0,0 @@ -$device.name ignition OFF at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/sms/ignitionOn.vm b/templates/sms/ignitionOn.vm deleted file mode 100644 index 00b365be9..000000000 --- a/templates/sms/ignitionOn.vm +++ /dev/null @@ -1 +0,0 @@ -$device.name ignition ON at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/sms/maintenance.vm b/templates/sms/maintenance.vm deleted file mode 100644 index 621beae46..000000000 --- a/templates/sms/maintenance.vm +++ /dev/null @@ -1 +0,0 @@ -$device.name maintenance $maintenance.name is required at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/sms/test.vm b/templates/sms/test.vm deleted file mode 100644 index d25f218ce..000000000 --- a/templates/sms/test.vm +++ /dev/null @@ -1 +0,0 @@ -Test message diff --git a/templates/sms/textMessage.vm b/templates/sms/textMessage.vm deleted file mode 100644 index dc42f29f1..000000000 --- a/templates/sms/textMessage.vm +++ /dev/null @@ -1 +0,0 @@ -Text message received from $device.name at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/sms/unknown.vm b/templates/sms/unknown.vm deleted file mode 100644 index fd20b50cc..000000000 --- a/templates/sms/unknown.vm +++ /dev/null @@ -1 +0,0 @@ -Unknown type -- cgit v1.2.3 From 253f11afa0e31d97d332ca3269111eff36ee347b Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 26 Jun 2018 12:29:08 +0500 Subject: Rename transports to notificators --- schema/changelog-4.0.xml | 16 +++++++-------- .../traccar/api/resource/NotificationResource.java | 23 +++++++++++----------- src/org/traccar/database/NotificationManager.java | 9 ++++----- src/org/traccar/model/Notification.java | 16 +++++++-------- .../traccar/notification/NotificatorManager.java | 2 +- 5 files changed, 32 insertions(+), 34 deletions(-) (limited to 'src/org/traccar') diff --git a/schema/changelog-4.0.xml b/schema/changelog-4.0.xml index 82403c83c..9a5cd1526 100644 --- a/schema/changelog-4.0.xml +++ b/schema/changelog-4.0.xml @@ -56,41 +56,41 @@ - + - + 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/api/resource/NotificationResource.java b/src/org/traccar/api/resource/NotificationResource.java index 9046569a0..bec1ce3ab 100644 --- a/src/org/traccar/api/resource/NotificationResource.java +++ b/src/org/traccar/api/resource/NotificationResource.java @@ -48,11 +48,17 @@ public class NotificationResource extends ExtendedObjectResource { public Collection get() { return Context.getNotificationManager().getAllNotificationTypes(); } + + @GET + @Path("notificators") + public Collection getNotificators() { + return Context.getNotificatorManager().getAllNotificatorTypes(); + } @POST @Path("test") public Response testMessage() throws NotificationException, InterruptedException { - for (Typed method : Context.getNotificatorManager().getNotificatorTypes()) { + for (Typed method : Context.getNotificatorManager().getAllNotificatorTypes()) { Context.getNotificatorManager() .getNotificator(method.getType()).sendSync(getUserId(), new Event("test", 0), null); } @@ -60,18 +66,11 @@ public class NotificationResource extends ExtendedObjectResource { } @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); + @Path("test/{notificator}") + public Response testMessage(@PathParam("notificator") String notificator) + throws NotificationException, InterruptedException { + Context.getNotificatorManager().getNotificator(notificator).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/database/NotificationManager.java b/src/org/traccar/database/NotificationManager.java index b2a30e4f1..5dcb1d317 100644 --- a/src/org/traccar/database/NotificationManager.java +++ b/src/org/traccar/database/NotificationManager.java @@ -83,16 +83,15 @@ public class NotificationManager extends ExtendedObjectManager { if (usersToForward != null) { usersToForward.add(userId); } - final Set notificationMethods = new HashSet<>(); + final Set notificators = new HashSet<>(); for (long notificationId : getEffectiveNotifications(userId, deviceId, event.getServerTime())) { Notification notification = getById(notificationId); if (getById(notificationId).getType().equals(event.getType())) { - notificationMethods.addAll(notification.getTransportMethods()); + notificators.addAll(notification.getNotificatorsTypes()); } } - for (String notificationMethod : notificationMethods) { - Context.getNotificatorManager() - .getNotificator(notificationMethod).sendAsync(userId, event, position); + for (String notificator : notificators) { + Context.getNotificatorManager().getNotificator(notificator).sendAsync(userId, event, position); } } } diff --git a/src/org/traccar/model/Notification.java b/src/org/traccar/model/Notification.java index 4923798c2..f1983a03a 100644 --- a/src/org/traccar/model/Notification.java +++ b/src/org/traccar/model/Notification.java @@ -45,23 +45,23 @@ public class Notification extends ScheduledModel { } - private String transports; + private String notificators; - public String getTransports() { - return transports; + public String getNotificators() { + return notificators; } - public void setTransports(String transports) { - this.transports = transports; + public void setNotificators(String transports) { + this.notificators = transports; } @JsonIgnore @QueryIgnore - public Set getTransportMethods() { + public Set getNotificatorsTypes() { final Set result = new HashSet<>(); - if (transports != null) { - final String[] transportsList = transports.split(","); + if (notificators != null) { + final String[] transportsList = notificators.split(","); for (String transport : transportsList) { result.add(transport.trim()); } diff --git a/src/org/traccar/notification/NotificatorManager.java b/src/org/traccar/notification/NotificatorManager.java index 87a294345..bb55844f3 100644 --- a/src/org/traccar/notification/NotificatorManager.java +++ b/src/org/traccar/notification/NotificatorManager.java @@ -64,7 +64,7 @@ public final class NotificatorManager { } - public Set getNotificatorTypes() { + public Set getAllNotificatorTypes() { Set result = new HashSet<>(); for (String notificator : notificators.keySet()) { result.add(new Typed(notificator)); -- cgit v1.2.3 From b70e46560359a181b0136fa1c6b0400615bfc904 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 26 Jun 2018 14:15:24 +0500 Subject: Rename MailMessage to FullMessage --- .../traccar/api/resource/NotificationResource.java | 2 +- src/org/traccar/notification/FullMessage.java | 36 ++++++++++++++++++++++ src/org/traccar/notification/MailMessage.java | 36 ---------------------- .../notification/NotificationFormatter.java | 4 +-- src/org/traccar/notification/NotificationMail.java | 6 ++-- 5 files changed, 42 insertions(+), 42 deletions(-) create mode 100644 src/org/traccar/notification/FullMessage.java delete mode 100644 src/org/traccar/notification/MailMessage.java (limited to 'src/org/traccar') diff --git a/src/org/traccar/api/resource/NotificationResource.java b/src/org/traccar/api/resource/NotificationResource.java index bec1ce3ab..0d7a7982d 100644 --- a/src/org/traccar/api/resource/NotificationResource.java +++ b/src/org/traccar/api/resource/NotificationResource.java @@ -48,7 +48,7 @@ public class NotificationResource extends ExtendedObjectResource { public Collection get() { return Context.getNotificationManager().getAllNotificationTypes(); } - + @GET @Path("notificators") public Collection getNotificators() { diff --git a/src/org/traccar/notification/FullMessage.java b/src/org/traccar/notification/FullMessage.java new file mode 100644 index 000000000..f66537c6e --- /dev/null +++ b/src/org/traccar/notification/FullMessage.java @@ -0,0 +1,36 @@ +/* + * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 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 FullMessage { + + private String subject; + private String body; + + public FullMessage(String subject, String body) { + this.subject = subject; + this.body = body; + } + + public String getSubject() { + return subject; + } + + public String getBody() { + return body; + } +} diff --git a/src/org/traccar/notification/MailMessage.java b/src/org/traccar/notification/MailMessage.java deleted file mode 100644 index 0fce43740..000000000 --- a/src/org/traccar/notification/MailMessage.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2016 Anton Tananaev (anton@traccar.org) - * Copyright 2016 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 MailMessage { - - private String subject; - private String body; - - public MailMessage(String subject, String body) { - this.subject = subject; - this.body = body; - } - - public String getSubject() { - return subject; - } - - public String getBody() { - return body; - } -} diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java index ddc35227e..a1abdd0cc 100644 --- a/src/org/traccar/notification/NotificationFormatter.java +++ b/src/org/traccar/notification/NotificationFormatter.java @@ -88,12 +88,12 @@ public final class NotificationFormatter { return template; } - public static MailMessage formatFullMessage(long userId, Event event, Position position) { + public static FullMessage 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); + return new FullMessage((String) velocityContext.get("subject"), formattedMessage); } public static String formatShortMessage(long userId, Event event, Position position) { diff --git a/src/org/traccar/notification/NotificationMail.java b/src/org/traccar/notification/NotificationMail.java index 76387c73b..808acca76 100644 --- a/src/org/traccar/notification/NotificationMail.java +++ b/src/org/traccar/notification/NotificationMail.java @@ -108,10 +108,10 @@ public final class NotificationMail extends Notificator { } message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail())); - MailMessage mailMessage = NotificationFormatter.formatFullMessage(userId, event, position); - message.setSubject(mailMessage.getSubject()); + FullMessage fullMessage = NotificationFormatter.formatFullMessage(userId, event, position); + message.setSubject(fullMessage.getSubject()); message.setSentDate(new Date()); - message.setContent(mailMessage.getBody(), "text/html; charset=utf-8"); + message.setContent(fullMessage.getBody(), "text/html; charset=utf-8"); Transport transport = session.getTransport(); try { -- cgit v1.2.3 From 14840af2abd9976b8f5634af8e77f4a7126dfac1 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 27 Jun 2018 15:55:19 +0500 Subject: - Rename NotificationException to MessageException - Simplify Notificator instantiation - Make sms configuration more clear - Move some defaults in code - Some cleanup --- setup/default.xml | 3 -- src/org/traccar/Context.java | 12 +++--- .../traccar/api/resource/NotificationResource.java | 6 +-- src/org/traccar/notification/MessageException.java | 25 +++++++++++ .../notification/NotificationException.java | 25 ----------- .../notification/NotificationFormatter.java | 7 +-- src/org/traccar/notification/NotificationMail.java | 4 +- src/org/traccar/notification/NotificationSms.java | 22 ++++------ src/org/traccar/notification/NotificationWeb.java | 3 +- src/org/traccar/notification/Notificator.java | 6 +-- .../traccar/notification/NotificatorManager.java | 50 ++++++++++++---------- src/org/traccar/smpp/SmppClient.java | 14 +++--- src/org/traccar/sms/SMSException.java | 28 ------------ src/org/traccar/sms/SMSManager.java | 24 ----------- src/org/traccar/sms/SmsManager.java | 27 ++++++++++++ 15 files changed, 112 insertions(+), 144 deletions(-) create mode 100644 src/org/traccar/notification/MessageException.java delete mode 100644 src/org/traccar/notification/NotificationException.java delete mode 100644 src/org/traccar/sms/SMSException.java delete mode 100644 src/org/traccar/sms/SMSManager.java create mode 100644 src/org/traccar/sms/SmsManager.java (limited to 'src/org/traccar') diff --git a/setup/default.xml b/setup/default.xml index d7c1e5f0f..83447ef83 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -29,9 +29,6 @@ ./media web,mail - org.traccar.notification.NotificationWeb - org.traccar.notification.NotificationMail - org.traccar.notification.NotificationSms https://www.traccar.org/analytics/ diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 80be1ddc6..2afdde3b9 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -79,7 +79,7 @@ import org.traccar.notification.EventForwarder; import org.traccar.notification.JsonTypeEventForwarder; import org.traccar.notification.NotificatorManager; import org.traccar.reports.model.TripsConfig; -import org.traccar.sms.SMSManager; +import org.traccar.sms.SmsManager; import org.traccar.web.WebServer; import javax.ws.rs.client.Client; @@ -261,9 +261,9 @@ public final class Context { return statisticsManager; } - private static SMSManager smsManager; + private static SmsManager smsManager; - public static SMSManager getSmsManager() { + public static SmsManager getSmsManager() { return smsManager; } @@ -396,11 +396,11 @@ public final class Context { tripsConfig = initTripsConfig(); - if (config.getBoolean("sms.smpp.enable")) { + if (config.getBoolean("sms.enable")) { final String smsManagerClass = config.getString("sms.manager.class", "org.traccar.smpp.SmppClient"); try { - smsManager = (SMSManager) Class.forName(smsManagerClass).newInstance(); - } catch (ClassNotFoundException e) { + smsManager = (SmsManager) Class.forName(smsManagerClass).newInstance(); + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException 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 0d7a7982d..9631a52b7 100644 --- a/src/org/traccar/api/resource/NotificationResource.java +++ b/src/org/traccar/api/resource/NotificationResource.java @@ -31,7 +31,7 @@ import org.traccar.api.ExtendedObjectResource; import org.traccar.model.Event; import org.traccar.model.Notification; import org.traccar.model.Typed; -import org.traccar.notification.NotificationException; +import org.traccar.notification.MessageException; @Path("notifications") @@ -57,7 +57,7 @@ public class NotificationResource extends ExtendedObjectResource { @POST @Path("test") - public Response testMessage() throws NotificationException, InterruptedException { + public Response testMessage() throws MessageException, InterruptedException { for (Typed method : Context.getNotificatorManager().getAllNotificatorTypes()) { Context.getNotificatorManager() .getNotificator(method.getType()).sendSync(getUserId(), new Event("test", 0), null); @@ -68,7 +68,7 @@ public class NotificationResource extends ExtendedObjectResource { @POST @Path("test/{notificator}") public Response testMessage(@PathParam("notificator") String notificator) - throws NotificationException, InterruptedException { + throws MessageException, InterruptedException { Context.getNotificatorManager().getNotificator(notificator).sendSync(getUserId(), new Event("test", 0), null); return Response.noContent().build(); } diff --git a/src/org/traccar/notification/MessageException.java b/src/org/traccar/notification/MessageException.java new file mode 100644 index 000000000..ce4b9f6ee --- /dev/null +++ b/src/org/traccar/notification/MessageException.java @@ -0,0 +1,25 @@ +/* + * 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.notification; + +public class MessageException extends Exception { + + public MessageException(Throwable cause) { + super(cause); + } + +} diff --git a/src/org/traccar/notification/NotificationException.java b/src/org/traccar/notification/NotificationException.java deleted file mode 100644 index 525e5ee37..000000000 --- a/src/org/traccar/notification/NotificationException.java +++ /dev/null @@ -1,25 +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.notification; - -public class NotificationException extends Exception { - - public NotificationException(Throwable cause) { - super(cause); - } - -} diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java index a1abdd0cc..c011403c5 100644 --- a/src/org/traccar/notification/NotificationFormatter.java +++ b/src/org/traccar/notification/NotificationFormatter.java @@ -89,17 +89,14 @@ public final class NotificationFormatter { } public static FullMessage 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); + String formattedMessage = formatMessage(velocityContext, userId, event, position, "full"); return new FullMessage((String) velocityContext.get("subject"), formattedMessage); } 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); + return formatMessage(null, userId, event, position, "short"); } private static String formatMessage(VelocityContext vc, Long userId, Event event, Position position, diff --git a/src/org/traccar/notification/NotificationMail.java b/src/org/traccar/notification/NotificationMail.java index 808acca76..c2ee67299 100644 --- a/src/org/traccar/notification/NotificationMail.java +++ b/src/org/traccar/notification/NotificationMail.java @@ -82,7 +82,7 @@ public final class NotificationMail extends Notificator { } @Override - public void sendSync(long userId, Event event, Position position) throws NotificationException { + public void sendSync(long userId, Event event, Position position) throws MessageException { User user = Context.getPermissionsManager().getUser(userId); Properties properties = null; @@ -125,7 +125,7 @@ public final class NotificationMail extends Notificator { transport.close(); } } catch (MessagingException e) { - throw new NotificationException(e); + throw new MessageException(e); } } diff --git a/src/org/traccar/notification/NotificationSms.java b/src/org/traccar/notification/NotificationSms.java index dd3304d85..ed651ac11 100644 --- a/src/org/traccar/notification/NotificationSms.java +++ b/src/org/traccar/notification/NotificationSms.java @@ -20,41 +20,37 @@ 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; +import org.traccar.sms.SmsManager; public final class NotificationSms extends Notificator { - private final SMSManager sms; + private final SmsManager smsManager; - public NotificationSms(String methodId) throws ClassNotFoundException, - InstantiationException, IllegalAccessException { - final String smsClass = Context.getConfig().getString("notificator." + methodId + ".sms.class", ""); + public NotificationSms() throws ClassNotFoundException, InstantiationException, IllegalAccessException { + final String smsClass = Context.getConfig().getString("notificator.sms.manager.class", ""); if (smsClass.length() > 0) { - sms = (SMSManager) Class.forName(smsClass).newInstance(); + smsManager = (SmsManager) Class.forName(smsClass).newInstance(); } else { - sms = Context.getSmsManager(); + smsManager = Context.getSmsManager(); } } - @Override public void sendAsync(long userId, Event event, Position position) { final User user = Context.getPermissionsManager().getUser(userId); if (user.getPhone() != null) { Context.getStatisticsManager().registerSms(); - sms.sendMessageAsync(user.getPhone(), + smsManager.sendMessageAsync(user.getPhone(), NotificationFormatter.formatShortMessage(userId, event, position), false); } } @Override - public void sendSync(long userId, Event event, Position position) throws SMSException, - InterruptedException { + public void sendSync(long userId, Event event, Position position) throws MessageException, InterruptedException { final User user = Context.getPermissionsManager().getUser(userId); if (user.getPhone() != null) { Context.getStatisticsManager().registerSms(); - sms.sendMessageSync(user.getPhone(), + smsManager.sendMessageSync(user.getPhone(), NotificationFormatter.formatShortMessage(userId, event, position), false); } } diff --git a/src/org/traccar/notification/NotificationWeb.java b/src/org/traccar/notification/NotificationWeb.java index 8e04fc2c4..afc401d24 100644 --- a/src/org/traccar/notification/NotificationWeb.java +++ b/src/org/traccar/notification/NotificationWeb.java @@ -23,8 +23,7 @@ import org.traccar.model.Position; public final class NotificationWeb extends Notificator { @Override - public void sendSync(long userId, Event event, Position position) throws NotificationException, - InterruptedException { + public void sendSync(long userId, Event event, Position position) { Context.getConnectionManager().updateEvent(userId, event); } diff --git a/src/org/traccar/notification/Notificator.java b/src/org/traccar/notification/Notificator.java index 09f98d3ad..d912b445d 100644 --- a/src/org/traccar/notification/Notificator.java +++ b/src/org/traccar/notification/Notificator.java @@ -27,14 +27,14 @@ public abstract class Notificator { public void run() { try { sendSync(userId, event, position); - } catch (NotificationException | InterruptedException error) { + } catch (MessageException | InterruptedException error) { Log.warning(error); } } }).start(); } - public abstract void sendSync(long userId, Event event, Position position) - throws NotificationException, InterruptedException; + throws MessageException, InterruptedException; + } diff --git a/src/org/traccar/notification/NotificatorManager.java b/src/org/traccar/notification/NotificatorManager.java index bb55844f3..147de47d3 100644 --- a/src/org/traccar/notification/NotificatorManager.java +++ b/src/org/traccar/notification/NotificatorManager.java @@ -16,8 +16,6 @@ */ package org.traccar.notification; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -29,31 +27,40 @@ import org.traccar.model.Typed; public final class NotificatorManager { + private static final String DEFAULT_WEB_NOTIFICATOR = "org.traccar.notification.NotificationWeb"; + private static final String DEFAULT_MAIL_NOTIFICATOR = "org.traccar.notification.NotificationMail"; + private static final String DEFAULT_SMS_NOTIFICATOR = "org.traccar.notification.NotificationSms"; + + private final Map notificators = new HashMap<>(); + private static final Notificator NULL_NOTIFICATOR = new NotificationNull(); + public 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 clazz = (Class) Class.forName(className); - try { - final Constructor constructor = clazz.getConstructor(new Class[]{String.class}); - notificators.put(type, constructor.newInstance(type)); - } catch (NoSuchMethodException e) { - // No constructor with String argument - notificators.put(type, clazz.newInstance()); - } - } catch (ClassNotFoundException | InstantiationException - | IllegalAccessException | InvocationTargetException e) { - Log.error("Unable to load notificator class for " + type + " " + className + " " + e.getMessage()); - } + String defaultNotificator = ""; + switch (type) { + case "web": + defaultNotificator = DEFAULT_WEB_NOTIFICATOR; + break; + case "mail": + defaultNotificator = DEFAULT_MAIL_NOTIFICATOR; + break; + case "sms": + defaultNotificator = DEFAULT_SMS_NOTIFICATOR; + break; + default: + break; + } + final String className = Context.getConfig() + .getString("notificator." + type + ".class", defaultNotificator); + try { + notificators.put(type, (Notificator) Class.forName(className).newInstance()); + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { + Log.error("Unable to load notificator class for " + type + " " + className + " " + e.getMessage()); } } } - private final Map notificators = new HashMap<>(); - private static final Notificator NULL_NOTIFICATOR = new NotificationNull(); - public Notificator getNotificator(String type) { final Notificator notificator = notificators.get(type); if (notificator == null) { @@ -63,7 +70,6 @@ public final class NotificatorManager { return notificator; } - public Set getAllNotificatorTypes() { Set result = new HashSet<>(); for (String notificator : notificators.keySet()) { @@ -72,6 +78,4 @@ public final class NotificatorManager { return result; } - } - diff --git a/src/org/traccar/smpp/SmppClient.java b/src/org/traccar/smpp/SmppClient.java index d5e6820ab..ddda4cb4f 100644 --- a/src/org/traccar/smpp/SmppClient.java +++ b/src/org/traccar/smpp/SmppClient.java @@ -25,8 +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 org.traccar.notification.MessageException; +import org.traccar.sms.SmsManager; import com.cloudhopper.commons.charset.CharsetUtil; import com.cloudhopper.smpp.SmppBindType; @@ -44,7 +44,7 @@ import com.cloudhopper.smpp.type.SmppChannelException; import com.cloudhopper.smpp.type.SmppTimeoutException; import com.cloudhopper.smpp.type.UnrecoverablePduException; -public class SmppClient implements SMSManager { +public class SmppClient implements SmsManager { private SmppSessionConfiguration sessionConfig = new SmppSessionConfiguration(); private SmppSession smppSession; @@ -216,7 +216,7 @@ public class SmppClient implements SMSManager { @Override public synchronized void sendMessageSync(String destAddress, String message, boolean command) - throws SMSException, InterruptedException, IllegalStateException { + throws MessageException, InterruptedException, IllegalStateException { if (getSession() != null && getSession().isBound()) { try { SubmitSm submit = new SubmitSm(); @@ -245,10 +245,10 @@ public class SmppClient implements SMSManager { } } catch (SmppChannelException | RecoverablePduException | SmppTimeoutException | UnrecoverablePduException error) { - throw new SMSException(error); + throw new MessageException(error); } } else { - throw new SMSException(new SmppChannelException("SMPP session is not connected")); + throw new MessageException(new SmppChannelException("SMPP session is not connected")); } } @@ -259,7 +259,7 @@ public class SmppClient implements SMSManager { public void run() { try { sendMessageSync(destAddress, message, command); - } catch (SMSException | InterruptedException | IllegalStateException error) { + } catch (MessageException | InterruptedException | IllegalStateException error) { Log.warning(error); } } diff --git a/src/org/traccar/sms/SMSException.java b/src/org/traccar/sms/SMSException.java deleted file mode 100644 index 22ca95296..000000000 --- a/src/org/traccar/sms/SMSException.java +++ /dev/null @@ -1,28 +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.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 deleted file mode 100644 index c4cb68fb7..000000000 --- a/src/org/traccar/sms/SMSManager.java +++ /dev/null @@ -1,24 +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.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); - -} diff --git a/src/org/traccar/sms/SmsManager.java b/src/org/traccar/sms/SmsManager.java new file mode 100644 index 000000000..4bc4bd009 --- /dev/null +++ b/src/org/traccar/sms/SmsManager.java @@ -0,0 +1,27 @@ +/* + * 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.sms; + +import org.traccar.notification.MessageException; + +public interface SmsManager { + + void sendMessageSync(String destAddress, String message, boolean command) + throws InterruptedException, MessageException; + void sendMessageAsync(final String destAddress, final String message, final boolean command); + +} -- cgit v1.2.3