aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAbyss777 <abyss@fox5.ru>2018-06-26 09:22:22 +0500
committerAbyss777 <abyss@fox5.ru>2018-06-26 09:22:22 +0500
commitaba402226403f8b3eb58cc01e8f536eb4104d35a (patch)
treeec884994ad90365fa9b385465a80391606f6d970 /src
parentc700bfdb66071ba224ddf01e9827e721562fed7a (diff)
parent575deac9e2df1cbd0601328f7ea7a9f22029fa43 (diff)
downloadtraccar-server-aba402226403f8b3eb58cc01e8f536eb4104d35a.tar.gz
traccar-server-aba402226403f8b3eb58cc01e8f536eb4104d35a.tar.bz2
traccar-server-aba402226403f8b3eb58cc01e8f536eb4104d35a.zip
Merge remote-tracking branch 'ivanfmartinez/notifications'
# Conflicts: # src/org/traccar/Context.java
Diffstat (limited to 'src')
-rw-r--r--src/org/traccar/BaseProtocol.java8
-rw-r--r--src/org/traccar/Context.java29
-rw-r--r--src/org/traccar/api/resource/NotificationResource.java32
-rw-r--r--src/org/traccar/database/CommandsManager.java6
-rw-r--r--src/org/traccar/database/NotificationManager.java25
-rw-r--r--src/org/traccar/model/Notification.java35
-rw-r--r--src/org/traccar/notification/NotificationException.java25
-rw-r--r--src/org/traccar/notification/NotificationFormatter.java8
-rw-r--r--src/org/traccar/notification/NotificationMail.java62
-rw-r--r--src/org/traccar/notification/NotificationNull.java34
-rw-r--r--src/org/traccar/notification/NotificationSms.java43
-rw-r--r--src/org/traccar/notification/NotificationWeb.java31
-rw-r--r--src/org/traccar/notification/Notificator.java40
-rw-r--r--src/org/traccar/notification/NotificatorManager.java71
-rw-r--r--src/org/traccar/smpp/SmppClient.java60
-rw-r--r--src/org/traccar/sms/SMSException.java28
-rw-r--r--src/org/traccar/sms/SMSManager.java8
17 files changed, 396 insertions, 149 deletions
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..80be1ddc6 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.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();
}
@@ -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..2347b43fa 100644
--- a/src/org/traccar/api/resource/NotificationResource.java
+++ b/src/org/traccar/api/resource/NotificationResource.java
@@ -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.NotificationException;
-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,11 +51,26 @@ public class NotificationResource extends ExtendedObjectResource<Notification> {
@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 {
+ 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<String> getNotificators() {
+ return Context.getNotificatorManager().getNotificatorTypes();
+ }
+
+
}
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..2c1ffc09c 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> 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.getTransportMethods());
}
}
+ for (String nm : notificationMethods) {
+ Context.getNotificatorManager().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..0b632c861 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;
@@ -37,33 +40,25 @@ 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 transports;
- public boolean getMail() {
- return mail;
+ public String getTransports() {
+ return transports;
}
- public void setMail(boolean mail) {
- this.mail = mail;
+ public void setTransports(String transports) {
+ this.transports = transports;
}
- private boolean sms;
- public boolean getSms() {
- return sms;
+ public Set<String> getTransportMethods() {
+ final Set<String> set = new HashSet<>();
+ final String[] tmp = transports.split(",");
+ for (String t : tmp) {
+ set.add(t.trim());
+ }
+ return set;
}
- public void setSms(boolean sms) {
- this.sms = sms;
- }
}
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/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java
index 524153721..ddc35227e 100644
--- a/src/org/traccar/notification/NotificationFormatter.java
+++ b/src/org/traccar/notification/NotificationFormatter.java
@@ -88,16 +88,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 6c81ecc79..fb89bf5bd 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.formatFullMessage(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..fd7950a80
--- /dev/null
+++ b/src/org/traccar/notification/NotificationNull.java
@@ -0,0 +1,34 @@
+/*
+ * 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 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..63fd57895 100644
--- a/src/org/traccar/notification/NotificationSms.java
+++ b/src/org/traccar/notification/NotificationSms.java
@@ -20,33 +20,42 @@ 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 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 sms;
- private NotificationSms() {
+ 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();
+ }
}
- 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);
+ sms.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 SMSException,
+ 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);
+ sms.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..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..20c7749d2
--- /dev/null
+++ b/src/org/traccar/notification/NotificatorManager.java
@@ -0,0 +1,71 @@
+/*
+ * 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.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;
+
+public final class 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", "");
+ if (className.length() > 0) {
+ try {
+ final Class<Notificator> c = (Class<Notificator>) Class.forName(className);
+ try {
+ final Constructor<Notificator> 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());
+ }
+ }
+ }
+ }
+
+ private final Map<String, Notificator> notificators = new HashMap<>();
+ private static final Notificator NULL_NOTIFICATOR = new NotificationNull();
+
+ 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;
+ }
+ return n;
+ }
+
+
+ public Set<String> getNotificatorTypes() {
+ return notificators.keySet();
+ }
+
+
+}
+
diff --git a/src/org/traccar/smpp/SmppClient.java b/src/org/traccar/smpp/SmppClient.java
index 967eed82c..d5e6820ab 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);
+
+}