mirror of
https://github.com/traccar/traccar.git
synced 2025-01-08 11:47:49 +08:00
Inject velocity engine
This commit is contained in:
parent
5b269c0e30
commit
a401b40ee3
@ -79,7 +79,9 @@
|
||||
<!--<module name="MethodLength">
|
||||
<property name="max" value="200"/>
|
||||
</module>-->
|
||||
<module name="ParameterNumber"/>
|
||||
<module name="ParameterNumber">
|
||||
<property name="tokens" value="METHOD_DEF"/>
|
||||
</module>
|
||||
|
||||
<!-- Checks for whitespace -->
|
||||
<!-- See http://checkstyle.sf.net/config_whitespace.html -->
|
||||
|
@ -18,8 +18,6 @@ package org.traccar;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.datatype.jsr353.JSR353Module;
|
||||
import org.apache.velocity.app.VelocityEngine;
|
||||
import org.eclipse.jetty.util.URIUtil;
|
||||
import org.traccar.config.Config;
|
||||
import org.traccar.config.Keys;
|
||||
import org.traccar.database.BaseObjectManager;
|
||||
@ -46,9 +44,6 @@ import org.traccar.session.cache.CacheManager;
|
||||
import javax.ws.rs.client.Client;
|
||||
import javax.ws.rs.client.ClientBuilder;
|
||||
import javax.ws.rs.ext.ContextResolver;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Properties;
|
||||
|
||||
public final class Context {
|
||||
|
||||
@ -115,12 +110,6 @@ public final class Context {
|
||||
return notificationManager;
|
||||
}
|
||||
|
||||
private static VelocityEngine velocityEngine;
|
||||
|
||||
public static VelocityEngine getVelocityEngine() {
|
||||
return velocityEngine;
|
||||
}
|
||||
|
||||
private static Client client = ClientBuilder.newClient();
|
||||
|
||||
public static Client getClient() {
|
||||
@ -185,25 +174,6 @@ public final class Context {
|
||||
Main.getInjector().getInstance(EventForwarder.class),
|
||||
Main.getInjector().getInstance(NotificatorManager.class),
|
||||
Main.getInjector().getInstance(Geocoder.class));
|
||||
Properties velocityProperties = new Properties();
|
||||
velocityProperties.setProperty("file.resource.loader.path",
|
||||
Context.getConfig().getString("templates.rootPath", "templates") + "/");
|
||||
velocityProperties.setProperty("runtime.log.logsystem.class",
|
||||
"org.apache.velocity.runtime.log.NullLogChute");
|
||||
|
||||
String address;
|
||||
try {
|
||||
address = config.getString(Keys.WEB_ADDRESS, InetAddress.getLocalHost().getHostAddress());
|
||||
} catch (UnknownHostException e) {
|
||||
address = "localhost";
|
||||
}
|
||||
|
||||
String webUrl = URIUtil.newURI("http", address, config.getInteger(Keys.WEB_PORT), "", "");
|
||||
webUrl = Context.getConfig().getString("web.url", webUrl);
|
||||
velocityProperties.setProperty("web.url", webUrl);
|
||||
|
||||
velocityEngine = new VelocityEngine();
|
||||
velocityEngine.init(velocityProperties);
|
||||
}
|
||||
|
||||
public static <T extends BaseModel> BaseObjectManager<T> getManager(Class<T> clazz) {
|
||||
|
@ -21,6 +21,9 @@ import com.google.inject.Provides;
|
||||
import com.google.inject.Scopes;
|
||||
import io.netty.util.HashedWheelTimer;
|
||||
import io.netty.util.Timer;
|
||||
import org.apache.velocity.app.VelocityEngine;
|
||||
import org.apache.velocity.runtime.log.NullLogChute;
|
||||
import org.eclipse.jetty.util.URIUtil;
|
||||
import org.traccar.broadcast.BroadcastService;
|
||||
import org.traccar.config.Config;
|
||||
import org.traccar.config.Keys;
|
||||
@ -70,6 +73,9 @@ import javax.annotation.Nullable;
|
||||
import javax.inject.Singleton;
|
||||
import javax.ws.rs.client.Client;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Properties;
|
||||
|
||||
public class MainModule extends AbstractModule {
|
||||
|
||||
@ -278,4 +284,27 @@ public class MainModule extends AbstractModule {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Singleton
|
||||
@Provides
|
||||
public static VelocityEngine provideVelocityEngine(Config config) {
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("file.resource.loader.path", config.getString(Keys.TEMPLATES_ROOT) + "/");
|
||||
properties.setProperty("runtime.log.logsystem.class", NullLogChute.class.getName());
|
||||
|
||||
String address;
|
||||
try {
|
||||
address = config.getString(Keys.WEB_ADDRESS, InetAddress.getLocalHost().getHostAddress());
|
||||
} catch (UnknownHostException e) {
|
||||
address = "localhost";
|
||||
}
|
||||
|
||||
String url = config.getString(
|
||||
Keys.WEB_URL, URIUtil.newURI("http", address, config.getInteger(Keys.WEB_PORT), "", ""));
|
||||
properties.setProperty("web.url", url);
|
||||
|
||||
VelocityEngine velocityEngine = new VelocityEngine();
|
||||
velocityEngine.init(properties);
|
||||
return velocityEngine;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2021 Anton Tananaev (anton@traccar.org)
|
||||
* Copyright 2021 - 2022 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.
|
||||
@ -15,12 +15,10 @@
|
||||
*/
|
||||
package org.traccar.api.resource;
|
||||
|
||||
import org.apache.velocity.VelocityContext;
|
||||
import org.traccar.Context;
|
||||
import org.traccar.api.BaseResource;
|
||||
import org.traccar.database.MailManager;
|
||||
import org.traccar.model.User;
|
||||
import org.traccar.notification.NotificationMessage;
|
||||
import org.traccar.notification.TextTemplateFormatter;
|
||||
import org.traccar.storage.StorageException;
|
||||
|
||||
@ -46,6 +44,9 @@ public class PasswordResource extends BaseResource {
|
||||
@Inject
|
||||
private MailManager mailManager;
|
||||
|
||||
@Inject
|
||||
private TextTemplateFormatter textTemplateFormatter;
|
||||
|
||||
@Path("reset")
|
||||
@PermitAll
|
||||
@POST
|
||||
@ -56,11 +57,9 @@ public class PasswordResource extends BaseResource {
|
||||
String token = UUID.randomUUID().toString().replaceAll("-", "");
|
||||
user.set(PASSWORD_RESET_TOKEN, token);
|
||||
Context.getUsersManager().updateItem(user);
|
||||
VelocityContext velocityContext = TextTemplateFormatter.prepareContext(
|
||||
permissionsService.getServer(), user);
|
||||
var velocityContext = textTemplateFormatter.prepareContext(permissionsService.getServer(), user);
|
||||
velocityContext.put("token", token);
|
||||
NotificationMessage fullMessage =
|
||||
TextTemplateFormatter.formatMessage(velocityContext, "passwordReset", "full");
|
||||
var fullMessage = textTemplateFormatter.formatMessage(velocityContext, "passwordReset", "full");
|
||||
mailManager.sendMessage(user, fullMessage.getSubject(), fullMessage.getBody());
|
||||
break;
|
||||
}
|
||||
|
@ -687,6 +687,14 @@ public final class Keys {
|
||||
"commands.queueing",
|
||||
Collections.singletonList(KeyType.GLOBAL));
|
||||
|
||||
/**
|
||||
* Root folder for all template files.
|
||||
*/
|
||||
public static final ConfigKey<String> TEMPLATES_ROOT = new ConfigKey<>(
|
||||
"templates.root",
|
||||
Collections.singletonList(KeyType.GLOBAL),
|
||||
"templates");
|
||||
|
||||
/**
|
||||
* SMS API service full URL. Enables SMS commands and notifications.
|
||||
*/
|
||||
@ -1245,6 +1253,15 @@ public final class Keys {
|
||||
"web.persistSession",
|
||||
Collections.singletonList(KeyType.GLOBAL));
|
||||
|
||||
/**
|
||||
* Public URL for the web app. Used for notification and report link.
|
||||
*
|
||||
* If not provided, Traccar will attempt to get a URL from the server IP address, but it might be a local address.
|
||||
*/
|
||||
public static final ConfigKey<String> WEB_URL = new ConfigKey<>(
|
||||
"web.url",
|
||||
Collections.singletonList(KeyType.GLOBAL));
|
||||
|
||||
/**
|
||||
* Output logging to the standard terminal output instead of a log file.
|
||||
*/
|
||||
|
@ -27,18 +27,26 @@ import org.traccar.model.Server;
|
||||
import org.traccar.model.User;
|
||||
import org.traccar.session.cache.CacheManager;
|
||||
|
||||
public final class NotificationFormatter {
|
||||
import javax.inject.Inject;
|
||||
|
||||
private NotificationFormatter() {
|
||||
public class NotificationFormatter {
|
||||
|
||||
private final CacheManager cacheManager;
|
||||
private final TextTemplateFormatter textTemplateFormatter;
|
||||
|
||||
@Inject
|
||||
public NotificationFormatter(
|
||||
CacheManager cacheManager, TextTemplateFormatter textTemplateFormatter) {
|
||||
this.cacheManager = cacheManager;
|
||||
this.textTemplateFormatter = textTemplateFormatter;
|
||||
}
|
||||
|
||||
public static NotificationMessage formatMessage(
|
||||
CacheManager cacheManager, User user, Event event, Position position, String templatePath) {
|
||||
public NotificationMessage formatMessage(User user, Event event, Position position, String templatePath) {
|
||||
|
||||
Server server = cacheManager.getServer();
|
||||
Device device = cacheManager.getObject(Device.class, event.getDeviceId());
|
||||
|
||||
VelocityContext velocityContext = TextTemplateFormatter.prepareContext(server, user);
|
||||
VelocityContext velocityContext = textTemplateFormatter.prepareContext(server, user);
|
||||
|
||||
velocityContext.put("device", device);
|
||||
velocityContext.put("event", event);
|
||||
@ -59,7 +67,7 @@ public final class NotificationFormatter {
|
||||
velocityContext.put("driver", cacheManager.findDriverByUniqueId(device.getId(), driverUniqueId));
|
||||
}
|
||||
|
||||
return TextTemplateFormatter.formatMessage(velocityContext, event.getType(), templatePath);
|
||||
return textTemplateFormatter.formatMessage(velocityContext, event.getType(), templatePath);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -17,29 +17,34 @@ package org.traccar.notification;
|
||||
|
||||
import org.apache.velocity.Template;
|
||||
import org.apache.velocity.VelocityContext;
|
||||
import org.apache.velocity.app.VelocityEngine;
|
||||
import org.apache.velocity.exception.ResourceNotFoundException;
|
||||
import org.apache.velocity.tools.generic.DateTool;
|
||||
import org.apache.velocity.tools.generic.NumberTool;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.traccar.Context;
|
||||
import org.traccar.helper.model.UserUtil;
|
||||
import org.traccar.model.Server;
|
||||
import org.traccar.model.User;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Locale;
|
||||
|
||||
public final class TextTemplateFormatter {
|
||||
public class TextTemplateFormatter {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(TextTemplateFormatter.class);
|
||||
|
||||
private TextTemplateFormatter() {
|
||||
private final VelocityEngine velocityEngine;
|
||||
|
||||
@Inject
|
||||
public TextTemplateFormatter(VelocityEngine velocityEngine) {
|
||||
this.velocityEngine = velocityEngine;
|
||||
}
|
||||
|
||||
public static VelocityContext prepareContext(Server server, User user) {
|
||||
public VelocityContext prepareContext(Server server, User user) {
|
||||
|
||||
VelocityContext velocityContext = new VelocityContext();
|
||||
|
||||
@ -48,7 +53,7 @@ public final class TextTemplateFormatter {
|
||||
velocityContext.put("timezone", UserUtil.getTimezone(server, user));
|
||||
}
|
||||
|
||||
velocityContext.put("webUrl", Context.getVelocityEngine().getProperty("web.url"));
|
||||
velocityContext.put("webUrl", velocityEngine.getProperty("web.url"));
|
||||
velocityContext.put("dateTool", new DateTool());
|
||||
velocityContext.put("numberTool", new NumberTool());
|
||||
velocityContext.put("locale", Locale.getDefault());
|
||||
@ -56,23 +61,23 @@ public final class TextTemplateFormatter {
|
||||
return velocityContext;
|
||||
}
|
||||
|
||||
public static Template getTemplate(String name, String path) {
|
||||
public Template getTemplate(String name, String path) {
|
||||
|
||||
String templateFilePath;
|
||||
Template template;
|
||||
|
||||
try {
|
||||
templateFilePath = Paths.get(path, name + ".vm").toString();
|
||||
template = Context.getVelocityEngine().getTemplate(templateFilePath, StandardCharsets.UTF_8.name());
|
||||
template = velocityEngine.getTemplate(templateFilePath, StandardCharsets.UTF_8.name());
|
||||
} catch (ResourceNotFoundException error) {
|
||||
LOGGER.warn("Notification template error", error);
|
||||
templateFilePath = Paths.get(path, "unknown.vm").toString();
|
||||
template = Context.getVelocityEngine().getTemplate(templateFilePath, StandardCharsets.UTF_8.name());
|
||||
template = velocityEngine.getTemplate(templateFilePath, StandardCharsets.UTF_8.name());
|
||||
}
|
||||
return template;
|
||||
}
|
||||
|
||||
public static NotificationMessage formatMessage(VelocityContext velocityContext, String name, String templatePath) {
|
||||
public NotificationMessage formatMessage(VelocityContext velocityContext, String name, String templatePath) {
|
||||
StringWriter writer = new StringWriter();
|
||||
getTemplate(name, templatePath).merge(velocityContext, writer);
|
||||
return new NotificationMessage((String) velocityContext.get("subject"), writer.toString());
|
||||
|
@ -23,7 +23,6 @@ import org.traccar.model.Event;
|
||||
import org.traccar.model.Position;
|
||||
import org.traccar.model.User;
|
||||
import org.traccar.notification.NotificationFormatter;
|
||||
import org.traccar.session.cache.CacheManager;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.ws.rs.client.Client;
|
||||
@ -31,7 +30,7 @@ import javax.ws.rs.client.Entity;
|
||||
|
||||
public class NotificatorFirebase implements Notificator {
|
||||
|
||||
private final CacheManager cacheManager;
|
||||
private final NotificationFormatter notificationFormatter;
|
||||
private final Client client;
|
||||
|
||||
private final String url;
|
||||
@ -54,14 +53,15 @@ public class NotificatorFirebase implements Notificator {
|
||||
}
|
||||
|
||||
@Inject
|
||||
public NotificatorFirebase(Config config, CacheManager cacheManager, Client client) {
|
||||
public NotificatorFirebase(Config config, NotificationFormatter notificationFormatter, Client client) {
|
||||
this(
|
||||
cacheManager, client, "https://fcm.googleapis.com/fcm/send",
|
||||
notificationFormatter, client, "https://fcm.googleapis.com/fcm/send",
|
||||
config.getString(Keys.NOTIFICATOR_FIREBASE_KEY));
|
||||
}
|
||||
|
||||
protected NotificatorFirebase(CacheManager cacheManager, Client client, String url, String key) {
|
||||
this.cacheManager = cacheManager;
|
||||
protected NotificatorFirebase(
|
||||
NotificationFormatter notificationFormatter, Client client, String url, String key) {
|
||||
this.notificationFormatter = notificationFormatter;
|
||||
this.client = client;
|
||||
this.url = url;
|
||||
this.key = key;
|
||||
@ -71,7 +71,7 @@ public class NotificatorFirebase implements Notificator {
|
||||
public void send(User user, Event event, Position position) {
|
||||
if (user.getAttributes().containsKey("notificationTokens")) {
|
||||
|
||||
var shortMessage = NotificationFormatter.formatMessage(cacheManager, user, event, position, "short");
|
||||
var shortMessage = notificationFormatter.formatMessage(user, event, position, "short");
|
||||
|
||||
Notification notification = new Notification();
|
||||
notification.title = shortMessage.getSubject();
|
||||
|
@ -20,10 +20,8 @@ import org.traccar.database.MailManager;
|
||||
import org.traccar.model.Event;
|
||||
import org.traccar.model.Position;
|
||||
import org.traccar.model.User;
|
||||
import org.traccar.notification.NotificationMessage;
|
||||
import org.traccar.notification.MessageException;
|
||||
import org.traccar.notification.NotificationFormatter;
|
||||
import org.traccar.session.cache.CacheManager;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.mail.MessagingException;
|
||||
@ -31,19 +29,18 @@ import javax.mail.MessagingException;
|
||||
public class NotificatorMail implements Notificator {
|
||||
|
||||
private final MailManager mailManager;
|
||||
private final CacheManager cacheManager;
|
||||
private final NotificationFormatter notificationFormatter;
|
||||
|
||||
@Inject
|
||||
public NotificatorMail(MailManager mailManager, CacheManager cacheManager) {
|
||||
public NotificatorMail(MailManager mailManager, NotificationFormatter notificationFormatter) {
|
||||
this.mailManager = mailManager;
|
||||
this.cacheManager = cacheManager;
|
||||
this.notificationFormatter = notificationFormatter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(User user, Event event, Position position) throws MessageException {
|
||||
try {
|
||||
NotificationMessage fullMessage = NotificationFormatter.formatMessage(
|
||||
cacheManager, user, event, position, "full");
|
||||
var fullMessage = notificationFormatter.formatMessage(user, event, position, "full");
|
||||
mailManager.sendMessage(user, fullMessage.getSubject(), fullMessage.getBody());
|
||||
} catch (MessagingException e) {
|
||||
throw new MessageException(e);
|
||||
|
@ -22,7 +22,6 @@ import org.traccar.model.Event;
|
||||
import org.traccar.model.Position;
|
||||
import org.traccar.model.User;
|
||||
import org.traccar.notification.NotificationFormatter;
|
||||
import org.traccar.session.cache.CacheManager;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.ws.rs.client.Client;
|
||||
@ -30,7 +29,7 @@ import javax.ws.rs.client.Entity;
|
||||
|
||||
public class NotificatorPushover implements Notificator {
|
||||
|
||||
private final CacheManager cacheManager;
|
||||
private final NotificationFormatter notificationFormatter;
|
||||
private final Client client;
|
||||
|
||||
private final String url;
|
||||
@ -51,8 +50,8 @@ public class NotificatorPushover implements Notificator {
|
||||
}
|
||||
|
||||
@Inject
|
||||
public NotificatorPushover(Config config, CacheManager cacheManager, Client client) {
|
||||
this.cacheManager = cacheManager;
|
||||
public NotificatorPushover(Config config, NotificationFormatter notificationFormatter, Client client) {
|
||||
this.notificationFormatter = notificationFormatter;
|
||||
this.client = client;
|
||||
url = "https://api.pushover.net/1/messages.json";
|
||||
token = config.getString(Keys.NOTIFICATOR_PUSHOVER_TOKEN);
|
||||
@ -70,7 +69,7 @@ public class NotificatorPushover implements Notificator {
|
||||
device = user.getString("notificator.pushover.device").replaceAll(" *, *", ",");
|
||||
}
|
||||
|
||||
var shortMessage = NotificationFormatter.formatMessage(cacheManager, user, event, position, "short");
|
||||
var shortMessage = notificationFormatter.formatMessage(user, event, position, "short");
|
||||
|
||||
Message message = new Message();
|
||||
message.token = token;
|
||||
|
@ -22,8 +22,6 @@ import org.traccar.model.Position;
|
||||
import org.traccar.model.User;
|
||||
import org.traccar.notification.MessageException;
|
||||
import org.traccar.notification.NotificationFormatter;
|
||||
import org.traccar.notification.NotificationMessage;
|
||||
import org.traccar.session.cache.CacheManager;
|
||||
import org.traccar.sms.SmsManager;
|
||||
|
||||
import javax.inject.Inject;
|
||||
@ -31,21 +29,21 @@ import javax.inject.Inject;
|
||||
public class NotificatorSms implements Notificator {
|
||||
|
||||
private final SmsManager smsManager;
|
||||
private final CacheManager cacheManager;
|
||||
private final NotificationFormatter notificationFormatter;
|
||||
private final StatisticsManager statisticsManager;
|
||||
|
||||
@Inject
|
||||
public NotificatorSms(SmsManager smsManager, CacheManager cacheManager, StatisticsManager statisticsManager) {
|
||||
public NotificatorSms(
|
||||
SmsManager smsManager, NotificationFormatter notificationFormatter, StatisticsManager statisticsManager) {
|
||||
this.smsManager = smsManager;
|
||||
this.cacheManager = cacheManager;
|
||||
this.notificationFormatter = notificationFormatter;
|
||||
this.statisticsManager = statisticsManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(User user, Event event, Position position) throws MessageException, InterruptedException {
|
||||
if (user.getPhone() != null) {
|
||||
NotificationMessage shortMessage = NotificationFormatter.formatMessage(
|
||||
cacheManager, user, event, position, "short");
|
||||
var shortMessage = notificationFormatter.formatMessage(user, event, position, "short");
|
||||
statisticsManager.registerSms();
|
||||
smsManager.sendMessage(user.getPhone(), shortMessage.getBody(), false);
|
||||
}
|
||||
|
@ -23,7 +23,6 @@ import org.traccar.model.Event;
|
||||
import org.traccar.model.Position;
|
||||
import org.traccar.model.User;
|
||||
import org.traccar.notification.NotificationFormatter;
|
||||
import org.traccar.session.cache.CacheManager;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.ws.rs.client.Client;
|
||||
@ -31,7 +30,7 @@ import javax.ws.rs.client.Entity;
|
||||
|
||||
public class NotificatorTelegram implements Notificator {
|
||||
|
||||
private final CacheManager cacheManager;
|
||||
private final NotificationFormatter notificationFormatter;
|
||||
private final Client client;
|
||||
|
||||
private final String urlSendText;
|
||||
@ -62,8 +61,8 @@ public class NotificatorTelegram implements Notificator {
|
||||
}
|
||||
|
||||
@Inject
|
||||
public NotificatorTelegram(Config config, CacheManager cacheManager, Client client) {
|
||||
this.cacheManager = cacheManager;
|
||||
public NotificatorTelegram(Config config, NotificationFormatter notificationFormatter, Client client) {
|
||||
this.notificationFormatter = notificationFormatter;
|
||||
this.client = client;
|
||||
urlSendText = String.format(
|
||||
"https://api.telegram.org/bot%s/sendMessage", config.getString(Keys.NOTIFICATOR_TELEGRAM_KEY));
|
||||
@ -85,7 +84,7 @@ public class NotificatorTelegram implements Notificator {
|
||||
|
||||
@Override
|
||||
public void send(User user, Event event, Position position) {
|
||||
var shortMessage = NotificationFormatter.formatMessage(cacheManager, user, event, position, "short");
|
||||
var shortMessage = notificationFormatter.formatMessage(user, event, position, "short");
|
||||
|
||||
TextMessage message = new TextMessage();
|
||||
message.chatId = user.getString("telegramChatId");
|
||||
|
@ -17,7 +17,7 @@ package org.traccar.notificators;
|
||||
|
||||
import org.traccar.config.Config;
|
||||
import org.traccar.config.Keys;
|
||||
import org.traccar.session.cache.CacheManager;
|
||||
import org.traccar.notification.NotificationFormatter;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.ws.rs.client.Client;
|
||||
@ -25,9 +25,9 @@ import javax.ws.rs.client.Client;
|
||||
public class NotificatorTraccar extends NotificatorFirebase {
|
||||
|
||||
@Inject
|
||||
public NotificatorTraccar(Config config, CacheManager cacheManager, Client client) {
|
||||
public NotificatorTraccar(Config config, NotificationFormatter notificationFormatter, Client client) {
|
||||
super(
|
||||
cacheManager, client, "https://www.traccar.org/push/",
|
||||
notificationFormatter, client, "https://www.traccar.org/push/",
|
||||
config.getString(Keys.NOTIFICATOR_TRACCAR_KEY));
|
||||
}
|
||||
|
||||
|
@ -21,19 +21,18 @@ import org.traccar.model.Position;
|
||||
import org.traccar.model.User;
|
||||
import org.traccar.notification.NotificationFormatter;
|
||||
import org.traccar.session.ConnectionManager;
|
||||
import org.traccar.session.cache.CacheManager;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public final class NotificatorWeb implements Notificator {
|
||||
|
||||
private final ConnectionManager connectionManager;
|
||||
private final CacheManager cacheManager;
|
||||
private final NotificationFormatter notificationFormatter;
|
||||
|
||||
@Inject
|
||||
public NotificatorWeb(ConnectionManager connectionManager, CacheManager cacheManager) {
|
||||
public NotificatorWeb(ConnectionManager connectionManager, NotificationFormatter notificationFormatter) {
|
||||
this.connectionManager = connectionManager;
|
||||
this.cacheManager = cacheManager;
|
||||
this.notificationFormatter = notificationFormatter;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -49,7 +48,7 @@ public final class NotificatorWeb implements Notificator {
|
||||
copy.setMaintenanceId(event.getMaintenanceId());
|
||||
copy.getAttributes().putAll(event.getAttributes());
|
||||
|
||||
var message = NotificationFormatter.formatMessage(cacheManager, user, event, position, "short");
|
||||
var message = notificationFormatter.formatMessage(user, event, position, "short");
|
||||
copy.set("message", message.getBody());
|
||||
|
||||
connectionManager.updateEvent(user.getId(), copy);
|
||||
|
@ -16,6 +16,7 @@
|
||||
*/
|
||||
package org.traccar.reports.common;
|
||||
|
||||
import org.apache.velocity.app.VelocityEngine;
|
||||
import org.apache.velocity.tools.generic.DateTool;
|
||||
import org.apache.velocity.tools.generic.NumberTool;
|
||||
import org.jxls.area.Area;
|
||||
@ -74,18 +75,21 @@ public class ReportUtils {
|
||||
private final IdentityManager identityManager;
|
||||
private final DeviceManager deviceManager;
|
||||
private final TripsConfig tripsConfig;
|
||||
private final VelocityEngine velocityEngine;
|
||||
private final Geocoder geocoder;
|
||||
|
||||
@Inject
|
||||
public ReportUtils(
|
||||
Config config, Storage storage, PermissionsService permissionsService, IdentityManager identityManager,
|
||||
DeviceManager deviceManager, TripsConfig tripsConfig, @Nullable Geocoder geocoder) {
|
||||
DeviceManager deviceManager, TripsConfig tripsConfig, VelocityEngine velocityEngine,
|
||||
@Nullable Geocoder geocoder) {
|
||||
this.config = config;
|
||||
this.storage = storage;
|
||||
this.permissionsService = permissionsService;
|
||||
this.identityManager = identityManager;
|
||||
this.deviceManager = deviceManager;
|
||||
this.tripsConfig = tripsConfig;
|
||||
this.velocityEngine = velocityEngine;
|
||||
this.geocoder = geocoder;
|
||||
}
|
||||
|
||||
@ -153,7 +157,7 @@ public class ReportUtils {
|
||||
context.putVar("distanceUnit", UserUtil.getDistanceUnit(server, user));
|
||||
context.putVar("speedUnit", UserUtil.getSpeedUnit(server, user));
|
||||
context.putVar("volumeUnit", UserUtil.getVolumeUnit(server, user));
|
||||
context.putVar("webUrl", Context.getVelocityEngine().getProperty("web.url"));
|
||||
context.putVar("webUrl", velocityEngine.getProperty("web.url"));
|
||||
context.putVar("dateTool", new DateTool());
|
||||
context.putVar("numberTool", new NumberTool());
|
||||
context.putVar("timezone", UserUtil.getTimezone(server, user));
|
||||
|
@ -1,5 +1,6 @@
|
||||
package org.traccar.reports;
|
||||
|
||||
import org.apache.velocity.app.VelocityEngine;
|
||||
import org.junit.Test;
|
||||
import org.traccar.BaseTest;
|
||||
import org.traccar.api.security.PermissionsService;
|
||||
@ -76,8 +77,8 @@ public class ReportUtilsTest extends BaseTest {
|
||||
@Test
|
||||
public void testCalculateSpentFuel() {
|
||||
ReportUtils reportUtils = new ReportUtils(
|
||||
mock(Config.class), mock(Storage.class), mock(PermissionsService.class),
|
||||
mockIdentityManager(), mock(DeviceManager.class), mock(TripsConfig.class), null);
|
||||
mock(Config.class), mock(Storage.class), mock(PermissionsService.class), mockIdentityManager(),
|
||||
mock(DeviceManager.class), mock(TripsConfig.class), mock(VelocityEngine.class), null);
|
||||
Position startPosition = new Position();
|
||||
Position endPosition = new Position();
|
||||
assertEquals(reportUtils.calculateFuel(startPosition, endPosition), 0.0, 0.01);
|
||||
@ -101,8 +102,8 @@ public class ReportUtilsTest extends BaseTest {
|
||||
|
||||
TripsConfig tripsConfig = new TripsConfig(500, 300000, 180000, 900000, false, false, 0.01);
|
||||
ReportUtils reportUtils = new ReportUtils(
|
||||
mock(Config.class), mock(Storage.class), mock(PermissionsService.class),
|
||||
mockIdentityManager(), mock(DeviceManager.class), tripsConfig, null);
|
||||
mock(Config.class), mock(Storage.class), mock(PermissionsService.class), mockIdentityManager(),
|
||||
mock(DeviceManager.class), tripsConfig, mock(VelocityEngine.class), null);
|
||||
|
||||
Collection<TripReportItem> trips = reportUtils.detectTripsAndStops(data, false, TripReportItem.class);
|
||||
|
||||
@ -156,8 +157,8 @@ public class ReportUtilsTest extends BaseTest {
|
||||
|
||||
TripsConfig tripsConfig = new TripsConfig(500, 300000, 180000, 900000, true, false, 0.01);
|
||||
ReportUtils reportUtils = new ReportUtils(
|
||||
mock(Config.class), mock(Storage.class), mock(PermissionsService.class),
|
||||
mockIdentityManager(), mock(DeviceManager.class), tripsConfig, null);
|
||||
mock(Config.class), mock(Storage.class), mock(PermissionsService.class), mockIdentityManager(),
|
||||
mock(DeviceManager.class), tripsConfig, mock(VelocityEngine.class), null);
|
||||
|
||||
Collection<TripReportItem> trips = reportUtils.detectTripsAndStops(data, false, TripReportItem.class);
|
||||
|
||||
@ -227,8 +228,8 @@ public class ReportUtilsTest extends BaseTest {
|
||||
|
||||
TripsConfig tripsConfig = new TripsConfig(500, 300000, 180000, 900000, false, false, 0.01);
|
||||
ReportUtils reportUtils = new ReportUtils(
|
||||
mock(Config.class), mock(Storage.class), mock(PermissionsService.class),
|
||||
mockIdentityManager(), mock(DeviceManager.class), tripsConfig, null);
|
||||
mock(Config.class), mock(Storage.class), mock(PermissionsService.class), mockIdentityManager(),
|
||||
mock(DeviceManager.class), tripsConfig, mock(VelocityEngine.class), null);
|
||||
|
||||
Collection<TripReportItem> trips = reportUtils.detectTripsAndStops(data, false, TripReportItem.class);
|
||||
|
||||
@ -278,8 +279,8 @@ public class ReportUtilsTest extends BaseTest {
|
||||
|
||||
TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, 900000, false, false, 0.01);
|
||||
ReportUtils reportUtils = new ReportUtils(
|
||||
mock(Config.class), mock(Storage.class), mock(PermissionsService.class),
|
||||
mockIdentityManager(), mock(DeviceManager.class), tripsConfig, null);
|
||||
mock(Config.class), mock(Storage.class), mock(PermissionsService.class), mockIdentityManager(),
|
||||
mock(DeviceManager.class), tripsConfig, mock(VelocityEngine.class), null);
|
||||
|
||||
Collection<StopReportItem> result = reportUtils.detectTripsAndStops(data, false, StopReportItem.class);
|
||||
|
||||
@ -307,8 +308,8 @@ public class ReportUtilsTest extends BaseTest {
|
||||
|
||||
TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, 900000, false, false, 0.01);
|
||||
ReportUtils reportUtils = new ReportUtils(
|
||||
mock(Config.class), mock(Storage.class), mock(PermissionsService.class),
|
||||
mockIdentityManager(), mock(DeviceManager.class), tripsConfig, null);
|
||||
mock(Config.class), mock(Storage.class), mock(PermissionsService.class), mockIdentityManager(),
|
||||
mock(DeviceManager.class), tripsConfig, mock(VelocityEngine.class), null);
|
||||
|
||||
Collection<StopReportItem> result = reportUtils.detectTripsAndStops(data, false, StopReportItem.class);
|
||||
|
||||
@ -336,8 +337,8 @@ public class ReportUtilsTest extends BaseTest {
|
||||
|
||||
TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, 900000, false, false, 0.01);
|
||||
ReportUtils reportUtils = new ReportUtils(
|
||||
mock(Config.class), mock(Storage.class), mock(PermissionsService.class),
|
||||
mockIdentityManager(), mock(DeviceManager.class), tripsConfig, null);
|
||||
mock(Config.class), mock(Storage.class), mock(PermissionsService.class), mockIdentityManager(),
|
||||
mock(DeviceManager.class), tripsConfig, mock(VelocityEngine.class), null);
|
||||
|
||||
Collection<StopReportItem> result = reportUtils.detectTripsAndStops(data, false, StopReportItem.class);
|
||||
|
||||
@ -365,8 +366,8 @@ public class ReportUtilsTest extends BaseTest {
|
||||
|
||||
TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, 900000, false, false, 0.01);
|
||||
ReportUtils reportUtils = new ReportUtils(
|
||||
mock(Config.class), mock(Storage.class), mock(PermissionsService.class),
|
||||
mockIdentityManager(), mock(DeviceManager.class), tripsConfig, null);
|
||||
mock(Config.class), mock(Storage.class), mock(PermissionsService.class), mockIdentityManager(),
|
||||
mock(DeviceManager.class), tripsConfig, mock(VelocityEngine.class), null);
|
||||
|
||||
Collection<StopReportItem> result = reportUtils.detectTripsAndStops(data, false, StopReportItem.class);
|
||||
|
||||
@ -390,8 +391,8 @@ public class ReportUtilsTest extends BaseTest {
|
||||
|
||||
TripsConfig tripsConfig = new TripsConfig(500, 200000, 200000, 900000, false, false, 0.01);
|
||||
ReportUtils reportUtils = new ReportUtils(
|
||||
mock(Config.class), mock(Storage.class), mock(PermissionsService.class),
|
||||
mockIdentityManager(), mock(DeviceManager.class), tripsConfig, null);
|
||||
mock(Config.class), mock(Storage.class), mock(PermissionsService.class), mockIdentityManager(),
|
||||
mock(DeviceManager.class), tripsConfig, mock(VelocityEngine.class), null);
|
||||
|
||||
Collection<TripReportItem> trips = reportUtils.detectTripsAndStops(data, false, TripReportItem.class);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user