diff options
-rw-r--r-- | schema/changelog-4.0.xml | 47 | ||||
-rw-r--r-- | setup/default.xml | 2 | ||||
-rw-r--r-- | src/org/traccar/BaseProtocol.java | 8 | ||||
-rw-r--r-- | src/org/traccar/Context.java | 29 | ||||
-rw-r--r-- | src/org/traccar/api/resource/NotificationResource.java | 34 | ||||
-rw-r--r-- | src/org/traccar/database/CommandsManager.java | 6 | ||||
-rw-r--r-- | src/org/traccar/database/NotificationManager.java | 25 | ||||
-rw-r--r-- | src/org/traccar/model/Notification.java | 43 | ||||
-rw-r--r-- | src/org/traccar/notification/FullMessage.java (renamed from src/org/traccar/notification/MailMessage.java) | 4 | ||||
-rw-r--r-- | src/org/traccar/notification/MessageException.java | 25 | ||||
-rw-r--r-- | src/org/traccar/notification/NotificationFormatter.java | 13 | ||||
-rw-r--r-- | src/org/traccar/notification/NotificationMail.java | 66 | ||||
-rw-r--r-- | src/org/traccar/notification/NotificationNull.java | 34 | ||||
-rw-r--r-- | src/org/traccar/notification/NotificationSms.java | 43 | ||||
-rw-r--r-- | src/org/traccar/notification/NotificationWeb.java | 30 | ||||
-rw-r--r-- | src/org/traccar/notification/Notificator.java | 40 | ||||
-rw-r--r-- | src/org/traccar/notification/NotificatorManager.java | 81 | ||||
-rw-r--r-- | src/org/traccar/smpp/SmppClient.java | 60 | ||||
-rw-r--r-- | src/org/traccar/sms/SmsManager.java | 27 | ||||
-rw-r--r-- | templates/full/alarm.vm (renamed from templates/mail/alarm.vm) | 0 | ||||
-rw-r--r-- | templates/full/commandResult.vm (renamed from templates/mail/commandResult.vm) | 0 | ||||
-rw-r--r-- | templates/full/deviceFuelDrop.vm (renamed from templates/mail/deviceFuelDrop.vm) | 0 | ||||
-rw-r--r-- | templates/full/deviceMoving.vm (renamed from templates/mail/deviceMoving.vm) | 0 | ||||
-rw-r--r-- | templates/full/deviceOffline.vm (renamed from templates/mail/deviceOffline.vm) | 0 | ||||
-rw-r--r-- | templates/full/deviceOnline.vm (renamed from templates/mail/deviceOnline.vm) | 0 | ||||
-rw-r--r-- | templates/full/deviceOverspeed.vm (renamed from templates/mail/deviceOverspeed.vm) | 0 | ||||
-rw-r--r-- | templates/full/deviceStopped.vm (renamed from templates/mail/deviceStopped.vm) | 0 | ||||
-rw-r--r-- | templates/full/deviceUnknown.vm (renamed from templates/mail/deviceUnknown.vm) | 0 | ||||
-rw-r--r-- | templates/full/driverChanged.vm (renamed from templates/mail/driverChanged.vm) | 0 | ||||
-rw-r--r-- | templates/full/geofenceEnter.vm (renamed from templates/mail/geofenceEnter.vm) | 0 | ||||
-rw-r--r-- | templates/full/geofenceExit.vm (renamed from templates/mail/geofenceExit.vm) | 0 | ||||
-rw-r--r-- | templates/full/ignitionOff.vm (renamed from templates/mail/ignitionOff.vm) | 0 | ||||
-rw-r--r-- | templates/full/ignitionOn.vm (renamed from templates/mail/ignitionOn.vm) | 0 | ||||
-rw-r--r-- | templates/full/maintenance.vm (renamed from templates/mail/maintenance.vm) | 0 | ||||
-rw-r--r-- | templates/full/test.vm (renamed from templates/mail/test.vm) | 0 | ||||
-rw-r--r-- | templates/full/textMessage.vm (renamed from templates/mail/textMessage.vm) | 0 | ||||
-rw-r--r-- | templates/full/unknown.vm (renamed from templates/mail/unknown.vm) | 0 | ||||
-rw-r--r-- | templates/short/alarm.vm (renamed from templates/sms/alarm.vm) | 0 | ||||
-rw-r--r-- | templates/short/commandResult.vm (renamed from templates/sms/commandResult.vm) | 0 | ||||
-rw-r--r-- | templates/short/deviceFuelDrop.vm (renamed from templates/sms/deviceFuelDrop.vm) | 0 | ||||
-rw-r--r-- | templates/short/deviceMoving.vm (renamed from templates/sms/deviceMoving.vm) | 0 | ||||
-rw-r--r-- | templates/short/deviceOffline.vm (renamed from templates/sms/deviceOffline.vm) | 0 | ||||
-rw-r--r-- | templates/short/deviceOnline.vm (renamed from templates/sms/deviceOnline.vm) | 0 | ||||
-rw-r--r-- | templates/short/deviceOverspeed.vm (renamed from templates/sms/deviceOverspeed.vm) | 0 | ||||
-rw-r--r-- | templates/short/deviceStopped.vm (renamed from templates/sms/deviceStopped.vm) | 0 | ||||
-rw-r--r-- | templates/short/deviceUnknown.vm (renamed from templates/sms/deviceUnknown.vm) | 0 | ||||
-rw-r--r-- | templates/short/driverChanged.vm (renamed from templates/sms/driverChanged.vm) | 0 | ||||
-rw-r--r-- | templates/short/geofenceEnter.vm (renamed from templates/sms/geofenceEnter.vm) | 0 | ||||
-rw-r--r-- | templates/short/geofenceExit.vm (renamed from templates/sms/geofenceExit.vm) | 0 | ||||
-rw-r--r-- | templates/short/ignitionOff.vm (renamed from templates/sms/ignitionOff.vm) | 0 | ||||
-rw-r--r-- | templates/short/ignitionOn.vm (renamed from templates/sms/ignitionOn.vm) | 0 | ||||
-rw-r--r-- | templates/short/maintenance.vm (renamed from templates/sms/maintenance.vm) | 0 | ||||
-rw-r--r-- | templates/short/test.vm (renamed from templates/sms/test.vm) | 0 | ||||
-rw-r--r-- | templates/short/textMessage.vm (renamed from templates/sms/textMessage.vm) | 0 | ||||
-rw-r--r-- | templates/short/unknown.vm (renamed from templates/sms/unknown.vm) | 0 |
55 files changed, 457 insertions, 160 deletions
diff --git a/schema/changelog-4.0.xml b/schema/changelog-4.0.xml index 04e9fe0c4..9a5cd1526 100644 --- a/schema/changelog-4.0.xml +++ b/schema/changelog-4.0.xml @@ -53,4 +53,51 @@ </changeSet> + <changeSet author="author" id="changelog-4.0"> + + <addColumn tableName="tc_notifications"> + <column name="notificators" type="VARCHAR(128)" /> + </addColumn> + + <update tableName="tc_notifications"> + <column name="notificators" value="web,mail,sms" /> + <where>web = 1 AND mail = 1 AND sms = 1</where> + </update> + + <update tableName="tc_notifications"> + <column name="notificators" value="web,mail" /> + <where>web = 1 AND mail = 1 AND sms = 0</where> + </update> + + <update tableName="tc_notifications"> + <column name="notificators" value="web" /> + <where>web = 1 AND mail = 0 AND sms = 0</where> + </update> + + <update tableName="tc_notifications"> + <column name="notificators" value="web,sms" /> + <where>web = 1 AND mail = 0 AND sms = 1</where> + </update> + + <update tableName="tc_notifications"> + <column name="notificators" value="mail,sms" /> + <where>web = 0 AND mail = 1 AND sms = 1</where> + </update> + + <update tableName="tc_notifications"> + <column name="notificators" value="mail" /> + <where>web = 0 AND mail = 1 AND sms = 0</where> + </update> + + <update tableName="tc_notifications"> + <column name="notificators" value="sms" /> + <where>web = 0 AND mail = 0 AND sms = 1</where> + </update> + + <dropColumn tableName="tc_notifications" columnName="web" /> + <dropColumn tableName="tc_notifications" columnName="mail" /> + <dropColumn tableName="tc_notifications" columnName="sms" /> + + </changeSet> + </databaseChangeLog> diff --git a/setup/default.xml b/setup/default.xml index 326ff7b93..83447ef83 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -28,6 +28,8 @@ <entry key='media.path'>./media</entry> + <entry key='notificator.types'>web,mail</entry> + <entry key='server.statistics'>https://www.traccar.org/analytics/</entry> <entry key='commands.queueing'>true</entry> diff --git a/src/org/traccar/BaseProtocol.java b/src/org/traccar/BaseProtocol.java index 1729edd54..62ee99946 100644 --- a/src/org/traccar/BaseProtocol.java +++ b/src/org/traccar/BaseProtocol.java @@ -91,18 +91,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 7224fd742..2afdde3b9 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -77,8 +77,9 @@ import org.traccar.geolocation.MozillaGeolocationProvider; import org.traccar.geolocation.OpenCellIdGeolocationProvider; import org.traccar.notification.EventForwarder; import org.traccar.notification.JsonTypeEventForwarder; +import org.traccar.notification.NotificatorManager; import org.traccar.reports.model.TripsConfig; -import org.traccar.smpp.SmppClient; +import org.traccar.sms.SmsManager; import org.traccar.web.WebServer; import javax.ws.rs.client.Client; @@ -206,6 +207,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() { @@ -254,10 +261,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; @@ -389,6 +396,15 @@ public final class Context { tripsConfig = initTripsConfig(); + 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 | InstantiationException | IllegalAccessException e) { + Log.warning("Error loading SMS Manager class : " + smsManagerClass, e); + } + } + if (config.getBoolean("event.enable")) { initEventsModule(); } @@ -407,10 +423,6 @@ public final class Context { statisticsManager = new StatisticsManager(); - if (config.getBoolean("sms.smpp.enable")) { - smppClient = new SmppClient(); - } - } private static void initGeolocationModule() { @@ -441,6 +453,7 @@ public final class Context { calendarManager = new CalendarManager(dataManager); maintenancesManager = new MaintenancesManager(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 540f02926..9631a52b7 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. @@ -17,11 +17,11 @@ 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; 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; @@ -31,13 +31,8 @@ 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.MessageException; -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) @@ -54,12 +49,27 @@ public class NotificationResource extends ExtendedObjectResource<Notification> { return Context.getNotificationManager().getAllNotificationTypes(); } + @GET + @Path("notificators") + public Collection<Typed> getNotificators() { + return Context.getNotificatorManager().getAllNotificatorTypes(); + } + @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 MessageException, InterruptedException { + for (Typed method : Context.getNotificatorManager().getAllNotificatorTypes()) { + Context.getNotificatorManager() + .getNotificator(method.getType()).sendSync(getUserId(), new Event("test", 0), null); + } + return Response.noContent().build(); + } + + @POST + @Path("test/{notificator}") + public Response testMessage(@PathParam("notificator") String notificator) + throws MessageException, InterruptedException { + Context.getNotificatorManager().getNotificator(notificator).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 8ddced5f7..c11f05f9b 100644 --- a/src/org/traccar/database/CommandsManager.java +++ b/src/org/traccar/database/CommandsManager.java @@ -61,10 +61,10 @@ public class CommandsManager extends ExtendedObjectManager<Command> { 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 3bc048356..5dcb1d317 100644 --- a/src/org/traccar/database/NotificationManager.java +++ b/src/org/traccar/database/NotificationManager.java @@ -32,8 +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.NotificationMail; -import org.traccar.notification.NotificationSms; public class NotificationManager extends ExtendedObjectManager<Notification> { @@ -85,29 +83,16 @@ public class NotificationManager extends ExtendedObjectManager<Notification> { if (usersToForward != null) { usersToForward.add(userId); } - boolean sentWeb = false; - boolean sentMail = false; - boolean sentSms = Context.getSmppManager() == null; + final Set<String> notificators = 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; + notificators.addAll(notification.getNotificatorsTypes()); } } + for (String notificator : notificators) { + Context.getNotificatorManager().getNotificator(notificator).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..f1983a03a 100644 --- a/src/org/traccar/model/Notification.java +++ b/src/org/traccar/model/Notification.java @@ -15,6 +15,13 @@ */ 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; @@ -37,33 +44,29 @@ 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; + private String notificators; - public boolean getMail() { - return mail; + public String getNotificators() { + return notificators; } - public void setMail(boolean mail) { - this.mail = mail; + public void setNotificators(String transports) { + this.notificators = transports; } - private boolean sms; - public boolean getSms() { - return sms; + @JsonIgnore + @QueryIgnore + public Set<String> getNotificatorsTypes() { + final Set<String> result = new HashSet<>(); + if (notificators != null) { + final String[] transportsList = notificators.split(","); + for (String transport : transportsList) { + result.add(transport.trim()); + } + } + return result; } - public void setSms(boolean sms) { - this.sms = sms; - } } diff --git a/src/org/traccar/notification/MailMessage.java b/src/org/traccar/notification/FullMessage.java index 0fce43740..f66537c6e 100644 --- a/src/org/traccar/notification/MailMessage.java +++ b/src/org/traccar/notification/FullMessage.java @@ -16,12 +16,12 @@ */ package org.traccar.notification; -public class MailMessage { +public class FullMessage { private String subject; private String body; - public MailMessage(String subject, String body) { + public FullMessage(String subject, String body) { this.subject = subject; this.body = body; } 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/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java index 524153721..c011403c5 100644 --- a/src/org/traccar/notification/NotificationFormatter.java +++ b/src/org/traccar/notification/NotificationFormatter.java @@ -88,18 +88,15 @@ 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 FullMessage formatFullMessage(long userId, Event event, Position position) { VelocityContext velocityContext = prepareContext(userId, event, position); - String formattedMessage = formatMessage(velocityContext, userId, event, position, templatePath); + String formattedMessage = formatMessage(velocityContext, userId, event, position, "full"); - return new MailMessage((String) velocityContext.get("subject"), formattedMessage); + return new FullMessage((String) velocityContext.get("subject"), formattedMessage); } - public static String formatSmsMessage(long userId, Event event, Position position) { - String templatePath = Context.getConfig().getString("sms.templatesPath", "sms"); - - return formatMessage(null, userId, event, position, templatePath); + public static String formatShortMessage(long userId, Event event, Position position) { + 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 6c81ecc79..c2ee67299 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. @@ -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 MessageException { 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())); + FullMessage fullMessage = NotificationFormatter.formatFullMessage(userId, event, position); + message.setSubject(fullMessage.getSubject()); + message.setSentDate(new Date()); + message.setContent(fullMessage.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 MessageException(e); + } } } diff --git a/src/org/traccar/notification/NotificationNull.java b/src/org/traccar/notification/NotificationNull.java new file mode 100644 index 000000000..3ee954c24 --- /dev/null +++ b/src/org/traccar/notification/NotificationNull.java @@ -0,0 +1,34 @@ +/* + * 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; + +import org.traccar.helper.Log; +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) { + 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"); + } +} diff --git a/src/org/traccar/notification/NotificationSms.java b/src/org/traccar/notification/NotificationSms.java index 8c0265af4..ed651ac11 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. @@ -20,33 +20,38 @@ 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 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 final SmsManager smsManager; - private NotificationSms() { + public NotificationSms() throws ClassNotFoundException, InstantiationException, IllegalAccessException { + final String smsClass = Context.getConfig().getString("notificator.sms.manager.class", ""); + if (smsClass.length() > 0) { + smsManager = (SmsManager) Class.forName(smsClass).newInstance(); + } else { + smsManager = Context.getSmsManager(); + } } - public static void sendSmsAsync(long userId, Event event, Position position) { - User user = Context.getPermissionsManager().getUser(userId); - if (Context.getSmppManager() != null && user.getPhone() != null) { + @Override + public void sendAsync(long userId, Event event, Position position) { + final User user = Context.getPermissionsManager().getUser(userId); + if (user.getPhone() != null) { Context.getStatisticsManager().registerSms(); - Context.getSmppManager().sendMessageAsync(user.getPhone(), - NotificationFormatter.formatSmsMessage(userId, event, position), false); + smsManager.sendMessageAsync(user.getPhone(), + NotificationFormatter.formatShortMessage(userId, event, position), false); } } - public static void sendSmsSync(long userId, Event event, Position position) throws RecoverablePduException, - UnrecoverablePduException, SmppTimeoutException, SmppChannelException, InterruptedException { - User user = Context.getPermissionsManager().getUser(userId); - if (Context.getSmppManager() != null && user.getPhone() != null) { + @Override + 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(); - Context.getSmppManager().sendMessageSync(user.getPhone(), - NotificationFormatter.formatSmsMessage(userId, event, position), false); + 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 new file mode 100644 index 000000000..afc401d24 --- /dev/null +++ b/src/org/traccar/notification/NotificationWeb.java @@ -0,0 +1,30 @@ +/* + * 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; + +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) { + 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..d912b445d --- /dev/null +++ b/src/org/traccar/notification/Notificator.java @@ -0,0 +1,40 @@ +/* + * 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; + +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 (MessageException | InterruptedException error) { + Log.warning(error); + } + } + }).start(); + } + + public abstract void sendSync(long userId, Event event, Position position) + throws MessageException, InterruptedException; + +} diff --git a/src/org/traccar/notification/NotificatorManager.java b/src/org/traccar/notification/NotificatorManager.java new file mode 100644 index 000000000..147de47d3 --- /dev/null +++ b/src/org/traccar/notification/NotificatorManager.java @@ -0,0 +1,81 @@ +/* + * 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; + +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 { + + 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<String, Notificator> 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) { + 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()); + } + } + } + + public Notificator getNotificator(String type) { + final Notificator notificator = notificators.get(type); + if (notificator == null) { + Log.error("No notificator configured for type : " + type); + return NULL_NOTIFICATOR; + } + return notificator; + } + + public Set<Typed> getAllNotificatorTypes() { + Set<Typed> result = new HashSet<>(); + for (String notificator : notificators.keySet()) { + result.add(new Typed(notificator)); + } + return result; + } + +} diff --git a/src/org/traccar/smpp/SmppClient.java b/src/org/traccar/smpp/SmppClient.java index 967eed82c..ddda4cb4f 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.notification.MessageException; +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 MessageException, 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 MessageException(error); } } else { - throw new SmppChannelException("SMPP session is not connected"); + throw new MessageException(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 (MessageException | InterruptedException | IllegalStateException error) { Log.warning(error); } } 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); + +} diff --git a/templates/mail/alarm.vm b/templates/full/alarm.vm index 8f1164291..8f1164291 100644 --- a/templates/mail/alarm.vm +++ b/templates/full/alarm.vm diff --git a/templates/mail/commandResult.vm b/templates/full/commandResult.vm index 5b6d8ef3e..5b6d8ef3e 100644 --- a/templates/mail/commandResult.vm +++ b/templates/full/commandResult.vm diff --git a/templates/mail/deviceFuelDrop.vm b/templates/full/deviceFuelDrop.vm index c30ec3a1c..c30ec3a1c 100644 --- a/templates/mail/deviceFuelDrop.vm +++ b/templates/full/deviceFuelDrop.vm diff --git a/templates/mail/deviceMoving.vm b/templates/full/deviceMoving.vm index 6e98af1de..6e98af1de 100644 --- a/templates/mail/deviceMoving.vm +++ b/templates/full/deviceMoving.vm diff --git a/templates/mail/deviceOffline.vm b/templates/full/deviceOffline.vm index 1f6d02fb2..1f6d02fb2 100644 --- a/templates/mail/deviceOffline.vm +++ b/templates/full/deviceOffline.vm diff --git a/templates/mail/deviceOnline.vm b/templates/full/deviceOnline.vm index 9211eff11..9211eff11 100644 --- a/templates/mail/deviceOnline.vm +++ b/templates/full/deviceOnline.vm diff --git a/templates/mail/deviceOverspeed.vm b/templates/full/deviceOverspeed.vm index e5e576f3a..e5e576f3a 100644 --- a/templates/mail/deviceOverspeed.vm +++ b/templates/full/deviceOverspeed.vm diff --git a/templates/mail/deviceStopped.vm b/templates/full/deviceStopped.vm index 2ec0f8db9..2ec0f8db9 100644 --- a/templates/mail/deviceStopped.vm +++ b/templates/full/deviceStopped.vm diff --git a/templates/mail/deviceUnknown.vm b/templates/full/deviceUnknown.vm index 34fa01a8a..34fa01a8a 100644 --- a/templates/mail/deviceUnknown.vm +++ b/templates/full/deviceUnknown.vm diff --git a/templates/mail/driverChanged.vm b/templates/full/driverChanged.vm index ba1e68661..ba1e68661 100644 --- a/templates/mail/driverChanged.vm +++ b/templates/full/driverChanged.vm diff --git a/templates/mail/geofenceEnter.vm b/templates/full/geofenceEnter.vm index e83a0de62..e83a0de62 100644 --- a/templates/mail/geofenceEnter.vm +++ b/templates/full/geofenceEnter.vm diff --git a/templates/mail/geofenceExit.vm b/templates/full/geofenceExit.vm index 3557f6eb2..3557f6eb2 100644 --- a/templates/mail/geofenceExit.vm +++ b/templates/full/geofenceExit.vm diff --git a/templates/mail/ignitionOff.vm b/templates/full/ignitionOff.vm index 0281eef02..0281eef02 100644 --- a/templates/mail/ignitionOff.vm +++ b/templates/full/ignitionOff.vm diff --git a/templates/mail/ignitionOn.vm b/templates/full/ignitionOn.vm index 27135a7f0..27135a7f0 100644 --- a/templates/mail/ignitionOn.vm +++ b/templates/full/ignitionOn.vm diff --git a/templates/mail/maintenance.vm b/templates/full/maintenance.vm index c6973f97a..c6973f97a 100644 --- a/templates/mail/maintenance.vm +++ b/templates/full/maintenance.vm diff --git a/templates/mail/test.vm b/templates/full/test.vm index 93cbdc549..93cbdc549 100644 --- a/templates/mail/test.vm +++ b/templates/full/test.vm diff --git a/templates/mail/textMessage.vm b/templates/full/textMessage.vm index 174173c85..174173c85 100644 --- a/templates/mail/textMessage.vm +++ b/templates/full/textMessage.vm diff --git a/templates/mail/unknown.vm b/templates/full/unknown.vm index 566ce0d42..566ce0d42 100644 --- a/templates/mail/unknown.vm +++ b/templates/full/unknown.vm diff --git a/templates/sms/alarm.vm b/templates/short/alarm.vm index 389341cf1..389341cf1 100644 --- a/templates/sms/alarm.vm +++ b/templates/short/alarm.vm diff --git a/templates/sms/commandResult.vm b/templates/short/commandResult.vm index 4a327d850..4a327d850 100644 --- a/templates/sms/commandResult.vm +++ b/templates/short/commandResult.vm diff --git a/templates/sms/deviceFuelDrop.vm b/templates/short/deviceFuelDrop.vm index 5e51396d4..5e51396d4 100644 --- a/templates/sms/deviceFuelDrop.vm +++ b/templates/short/deviceFuelDrop.vm diff --git a/templates/sms/deviceMoving.vm b/templates/short/deviceMoving.vm index aaa72ab0d..aaa72ab0d 100644 --- a/templates/sms/deviceMoving.vm +++ b/templates/short/deviceMoving.vm diff --git a/templates/sms/deviceOffline.vm b/templates/short/deviceOffline.vm index 8c4a02335..8c4a02335 100644 --- a/templates/sms/deviceOffline.vm +++ b/templates/short/deviceOffline.vm diff --git a/templates/sms/deviceOnline.vm b/templates/short/deviceOnline.vm index 62854feb0..62854feb0 100644 --- a/templates/sms/deviceOnline.vm +++ b/templates/short/deviceOnline.vm diff --git a/templates/sms/deviceOverspeed.vm b/templates/short/deviceOverspeed.vm index 5e92f662c..5e92f662c 100644 --- a/templates/sms/deviceOverspeed.vm +++ b/templates/short/deviceOverspeed.vm diff --git a/templates/sms/deviceStopped.vm b/templates/short/deviceStopped.vm index c2437b5f6..c2437b5f6 100644 --- a/templates/sms/deviceStopped.vm +++ b/templates/short/deviceStopped.vm diff --git a/templates/sms/deviceUnknown.vm b/templates/short/deviceUnknown.vm index 07f39f3a9..07f39f3a9 100644 --- a/templates/sms/deviceUnknown.vm +++ b/templates/short/deviceUnknown.vm diff --git a/templates/sms/driverChanged.vm b/templates/short/driverChanged.vm index 50849df7c..50849df7c 100644 --- a/templates/sms/driverChanged.vm +++ b/templates/short/driverChanged.vm diff --git a/templates/sms/geofenceEnter.vm b/templates/short/geofenceEnter.vm index debdbf356..debdbf356 100644 --- a/templates/sms/geofenceEnter.vm +++ b/templates/short/geofenceEnter.vm diff --git a/templates/sms/geofenceExit.vm b/templates/short/geofenceExit.vm index 8553458f7..8553458f7 100644 --- a/templates/sms/geofenceExit.vm +++ b/templates/short/geofenceExit.vm diff --git a/templates/sms/ignitionOff.vm b/templates/short/ignitionOff.vm index ddf860413..ddf860413 100644 --- a/templates/sms/ignitionOff.vm +++ b/templates/short/ignitionOff.vm diff --git a/templates/sms/ignitionOn.vm b/templates/short/ignitionOn.vm index 00b365be9..00b365be9 100644 --- a/templates/sms/ignitionOn.vm +++ b/templates/short/ignitionOn.vm diff --git a/templates/sms/maintenance.vm b/templates/short/maintenance.vm index 621beae46..621beae46 100644 --- a/templates/sms/maintenance.vm +++ b/templates/short/maintenance.vm diff --git a/templates/sms/test.vm b/templates/short/test.vm index d25f218ce..d25f218ce 100644 --- a/templates/sms/test.vm +++ b/templates/short/test.vm diff --git a/templates/sms/textMessage.vm b/templates/short/textMessage.vm index dc42f29f1..dc42f29f1 100644 --- a/templates/sms/textMessage.vm +++ b/templates/short/textMessage.vm diff --git a/templates/sms/unknown.vm b/templates/short/unknown.vm index fd20b50cc..fd20b50cc 100644 --- a/templates/sms/unknown.vm +++ b/templates/short/unknown.vm |