Skip to content

Commit

Permalink
Merge pull request traccar#3950 from Abyss777/notification_refactor
Browse files Browse the repository at this point in the history
Notification refactor to allow custom "notificators"
  • Loading branch information
tananaev authored Jun 28, 2018
2 parents 4d6229c + 14840af commit e849d3a
Show file tree
Hide file tree
Showing 55 changed files with 457 additions and 160 deletions.
47 changes: 47 additions & 0 deletions schema/changelog-4.0.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
2 changes: 2 additions & 0 deletions setup/default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
8 changes: 4 additions & 4 deletions src/org/traccar/BaseProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,18 @@ public void setTextCommandEncoder(StringProtocolEncoder textCommandEncoder) {

@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");
}
}

Expand Down
29 changes: 21 additions & 8 deletions src/org/traccar/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@
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;
Expand Down Expand Up @@ -206,6 +207,12 @@ public static NotificationManager getNotificationManager() {
return notificationManager;
}

private static NotificatorManager notificatorManager;

public static NotificatorManager getNotificatorManager() {
return notificatorManager;
}

private static VelocityEngine velocityEngine;

public static VelocityEngine getVelocityEngine() {
Expand Down Expand Up @@ -254,10 +261,10 @@ public static StatisticsManager getStatisticsManager() {
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;
Expand Down Expand Up @@ -389,6 +396,15 @@ public static void init(String[] arguments) throws Exception {

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();
}
Expand All @@ -407,10 +423,6 @@ public static void init(String[] arguments) throws Exception {

statisticsManager = new StatisticsManager();

if (config.getBoolean("sms.smpp.enable")) {
smppClient = new SmppClient();
}

}

private static void initGeolocationModule() {
Expand Down Expand Up @@ -441,6 +453,7 @@ private static void initEventsModule() {
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") + "/");
Expand Down
34 changes: 22 additions & 12 deletions src/org/traccar/api/resource/NotificationResource.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 - 2017 Anton Tananaev ([email protected])
* Copyright 2016 - 2018 Anton Tananaev ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,11 +17,11 @@

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;
Expand All @@ -31,13 +31,8 @@
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)
Expand All @@ -54,12 +49,27 @@ public Collection<Typed> get() {
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();
}

Expand Down
6 changes: 3 additions & 3 deletions src/org/traccar/database/CommandsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ public boolean sendCommand(Command command) throws Exception {
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");
Expand Down
25 changes: 5 additions & 20 deletions src/org/traccar/database/NotificationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
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> {

Expand Down Expand Up @@ -85,29 +83,16 @@ public void updateEvent(Event event, Position position) {
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) {
Expand Down
43 changes: 23 additions & 20 deletions src/org/traccar/model/Notification.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,33 +44,29 @@ public void setType(String type) {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
25 changes: 25 additions & 0 deletions src/org/traccar/notification/MessageException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2018 Anton Tananaev ([email protected])
* Copyright 2018 Andrey Kunitsyn ([email protected])
*
* 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);
}

}
Loading

0 comments on commit e849d3a

Please sign in to comment.