From e776aa3351535f78c60ab9502dd1154e2b973227 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 13 Jul 2017 10:05:57 +0500 Subject: - Save both driver name and driver unique id in trip report - Map user and driver objects to notification templates - Add driverUnauthorized templates --- src/org/traccar/notification/NotificationFormatter.java | 7 +++++++ src/org/traccar/reports/ReportUtils.java | 17 +++++++++++------ src/org/traccar/reports/model/TripReport.java | 10 ++++++++++ templates/mail/driverUnauthorized.vm | 10 ++++++++++ templates/sms/driverUnauthorized.vm | 6 ++++++ 5 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 templates/mail/driverUnauthorized.vm create mode 100644 templates/sms/driverUnauthorized.vm diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java index 96337ecaa..a30023fdc 100644 --- a/src/org/traccar/notification/NotificationFormatter.java +++ b/src/org/traccar/notification/NotificationFormatter.java @@ -30,6 +30,7 @@ import org.traccar.helper.Log; import org.traccar.model.Device; import org.traccar.model.Event; import org.traccar.model.Position; +import org.traccar.model.User; import org.traccar.reports.ReportUtils; public final class NotificationFormatter { @@ -38,9 +39,11 @@ public final class NotificationFormatter { } public static VelocityContext prepareContext(long userId, Event event, Position position) { + User user = Context.getPermissionsManager().getUser(userId); Device device = Context.getIdentityManager().getDeviceById(event.getDeviceId()); VelocityContext velocityContext = new VelocityContext(); + velocityContext.put("user", user); velocityContext.put("device", device); velocityContext.put("event", event); if (position != null) { @@ -50,6 +53,10 @@ public final class NotificationFormatter { if (event.getGeofenceId() != 0) { velocityContext.put("geofence", Context.getGeofenceManager().getGeofence(event.getGeofenceId())); } + String driverUniqueId = event.getString(Position.KEY_DRIVER_UNIQUE_ID); + if (driverUniqueId != null) { + velocityContext.put("driver", Context.getDriversManager().getDriverByUniqueId(driverUniqueId)); + } velocityContext.put("webUrl", Context.getVelocityEngine().getProperty("web.url")); velocityContext.put("dateTool", new DateTool()); velocityContext.put("numberTool", new NumberTool()); diff --git a/src/org/traccar/reports/ReportUtils.java b/src/org/traccar/reports/ReportUtils.java index 92918b861..3a177300b 100644 --- a/src/org/traccar/reports/ReportUtils.java +++ b/src/org/traccar/reports/ReportUtils.java @@ -102,16 +102,19 @@ public final class ReportUtils { return 0; } - public static String findDriverName(Position firstPosition, Position lastPosition) { - String driverUniqueId = null; + public static String findDriver(Position firstPosition, Position lastPosition) { if (firstPosition.getAttributes().containsKey(Position.KEY_DRIVER_UNIQUE_ID)) { - driverUniqueId = firstPosition.getString(Position.KEY_DRIVER_UNIQUE_ID); + return firstPosition.getString(Position.KEY_DRIVER_UNIQUE_ID); } else if (lastPosition.getAttributes().containsKey(Position.KEY_DRIVER_UNIQUE_ID)) { - driverUniqueId = lastPosition.getString(Position.KEY_DRIVER_UNIQUE_ID); + return lastPosition.getString(Position.KEY_DRIVER_UNIQUE_ID); } + return null; + } + + public static String findDriverName(String driverUniqueId) { if (driverUniqueId != null && Context.getDriversManager() != null) { Driver driver = Context.getDriversManager().getDriverByUniqueId(driverUniqueId); - return driver != null ? driver.getName() : null; + return driver != null ? driver.getName() : driverUniqueId; } return null; } @@ -190,7 +193,9 @@ public final class ReportUtils { trip.setAverageSpeed(speedSum / (endIndex - startIndex)); trip.setMaxSpeed(speedMax); trip.setSpentFuel(calculateFuel(startTrip, endTrip)); - trip.setDriverName(findDriverName(startTrip, endTrip)); + + trip.setDriverUniqueId(findDriver(startTrip, endTrip)); + trip.setDriverName(findDriverName(trip.getDriverUniqueId())); return trip; } diff --git a/src/org/traccar/reports/model/TripReport.java b/src/org/traccar/reports/model/TripReport.java index 0f23581b2..42a4240b7 100644 --- a/src/org/traccar/reports/model/TripReport.java +++ b/src/org/traccar/reports/model/TripReport.java @@ -146,6 +146,16 @@ public class TripReport extends BaseReport { this.duration = duration; } + private String driverUniqueId; + + public String getDriverUniqueId() { + return driverUniqueId; + } + + public void setDriverUniqueId(String driverUniqueId) { + this.driverUniqueId = driverUniqueId; + } + private String driverName; public String getDriverName() { diff --git a/templates/mail/driverUnauthorized.vm b/templates/mail/driverUnauthorized.vm new file mode 100644 index 000000000..3d28266cd --- /dev/null +++ b/templates/mail/driverUnauthorized.vm @@ -0,0 +1,10 @@ +#set($subject = "$device.name: driver unauthorized") + + + +Device: $device.name
+Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)
+Point: #{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}
+Driver: #{if}($driver)$driver.name#{else}$event.getString("driverUniqueId")#{end} + + diff --git a/templates/sms/driverUnauthorized.vm b/templates/sms/driverUnauthorized.vm new file mode 100644 index 000000000..52c819eb8 --- /dev/null +++ b/templates/sms/driverUnauthorized.vm @@ -0,0 +1,6 @@ +#if($driver) +#set($driverName = $driver.name) +#else +#set($driverName = $event.getString("driverUniqueId")) +#end +$driverName is unauthorized for $device.name at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) -- cgit v1.2.3