diff options
author | Abyss777 <abyss@fox5.ru> | 2017-02-21 15:20:25 +0500 |
---|---|---|
committer | Abyss777 <abyss@fox5.ru> | 2017-02-21 15:20:25 +0500 |
commit | bd2a8efaddacbd8d8386ae38f000b633845639ab (patch) | |
tree | 80d4454311d9656fe79850ebcdb4131e3cc32634 /src/org/traccar/notification/NotificationFormatter.java | |
parent | b6af1e997c125336e2ce9f5056d9502743f22034 (diff) | |
download | trackermap-server-bd2a8efaddacbd8d8386ae38f000b633845639ab.tar.gz trackermap-server-bd2a8efaddacbd8d8386ae38f000b633845639ab.tar.bz2 trackermap-server-bd2a8efaddacbd8d8386ae38f000b633845639ab.zip |
Implement SMS notifications with help smpp protocol
Diffstat (limited to 'src/org/traccar/notification/NotificationFormatter.java')
-rw-r--r-- | src/org/traccar/notification/NotificationFormatter.java | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java index b68d53ee0..1e3072d53 100644 --- a/src/org/traccar/notification/NotificationFormatter.java +++ b/src/org/traccar/notification/NotificationFormatter.java @@ -1,5 +1,6 @@ /* - * Copyright 2016 Anton Tananaev (anton@traccar.org) + * 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. @@ -33,7 +34,7 @@ public final class NotificationFormatter { private NotificationFormatter() { } - public static MailMessage formatMessage(long userId, Event event, Position position) { + private static VelocityContext prepareContext(long userId, Event event, Position position) { Device device = Context.getIdentityManager().getDeviceById(event.getDeviceId()); VelocityContext velocityContext = new VelocityContext(); @@ -47,13 +48,20 @@ public final class NotificationFormatter { velocityContext.put("geofence", Context.getGeofenceManager().getGeofence(event.getGeofenceId())); } velocityContext.put("webUrl", Context.getVelocityEngine().getProperty("web.url")); + return velocityContext; + } + public static MailMessage formatMailMessage(long userId, Event event, Position position) { + VelocityContext velocityContext = prepareContext(userId, event, position); + String mailTemplatesPath = Context.getConfig().getString("mail.templatesPath", "mail") + "/"; Template template = null; try { - template = Context.getVelocityEngine().getTemplate(event.getType() + ".vm", StandardCharsets.UTF_8.name()); + template = Context.getVelocityEngine().getTemplate(mailTemplatesPath + event.getType() + ".vm", + StandardCharsets.UTF_8.name()); } catch (ResourceNotFoundException error) { Log.warning(error); - template = Context.getVelocityEngine().getTemplate("unknown.vm", StandardCharsets.UTF_8.name()); + template = Context.getVelocityEngine().getTemplate(mailTemplatesPath + "unknown.vm", + StandardCharsets.UTF_8.name()); } StringWriter writer = new StringWriter(); @@ -61,4 +69,21 @@ public final class NotificationFormatter { String subject = (String) velocityContext.get("subject"); return new MailMessage(subject, writer.toString()); } + + public static String formatSmsMessage(long userId, Event event, Position position) { + VelocityContext velocityContext = prepareContext(userId, event, position); + String smsTemplatesPath = Context.getConfig().getString("sms.templatesPath", "sms") + "/"; + Template template = null; + try { + template = Context.getVelocityEngine().getTemplate(smsTemplatesPath + event.getType() + ".vm", + StandardCharsets.UTF_8.name()); + } catch (ResourceNotFoundException error) { + Log.warning(error); + template = Context.getVelocityEngine().getTemplate(smsTemplatesPath + "unknown.vm", + StandardCharsets.UTF_8.name()); + } + StringWriter writer = new StringWriter(); + template.merge(velocityContext, writer); + return writer.toString(); + } } |