aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2016-08-25 03:33:36 +1200
committerGitHub <noreply@github.com>2016-08-25 03:33:36 +1200
commit43033e0bd19dc99d9e571881aadee3b0285f4574 (patch)
tree166d62dbab0554e5d21924c195a90ffceb34368b /src/org/traccar
parent516d1f157a762c3bd54e0ffc2f3c71547d49773c (diff)
parentd2e89a2c1aa8f787bc0a296f588295ad1a5f76f2 (diff)
downloadtraccar-server-43033e0bd19dc99d9e571881aadee3b0285f4574.tar.gz
traccar-server-43033e0bd19dc99d9e571881aadee3b0285f4574.tar.bz2
traccar-server-43033e0bd19dc99d9e571881aadee3b0285f4574.zip
Merge pull request #2248 from Abyss777/convert_speed2
Convert speed units in mail notifications
Diffstat (limited to 'src/org/traccar')
-rw-r--r--src/org/traccar/helper/UnitsConverter.java21
-rw-r--r--src/org/traccar/notification/NotificationFormatter.java22
2 files changed, 37 insertions, 6 deletions
diff --git a/src/org/traccar/helper/UnitsConverter.java b/src/org/traccar/helper/UnitsConverter.java
index fe42d9ee2..4bc7348db 100644
--- a/src/org/traccar/helper/UnitsConverter.java
+++ b/src/org/traccar/helper/UnitsConverter.java
@@ -17,23 +17,36 @@ package org.traccar.helper;
public final class UnitsConverter {
+ private static final double KNOTS_TO_KPH_RATIO = 0.539957;
+ private static final double KNOTS_TO_MPH_RATIO = 0.868976;
+ private static final double KNOTS_TO_MPS_RATIO = 1.94384;
+ private static final double KNOTS_TO_CPS_RATIO = 0.0194384449;
+
private UnitsConverter() {
}
public static double knotsFromKph(double value) { // km/h
- return value * 0.539957;
+ return value * KNOTS_TO_KPH_RATIO;
+ }
+
+ public static double kphFromKnots(double value) {
+ return value / KNOTS_TO_KPH_RATIO;
}
public static double knotsFromMph(double value) {
- return value * 0.868976;
+ return value * KNOTS_TO_MPH_RATIO;
+ }
+
+ public static double mphFromKnots(double value) {
+ return value / KNOTS_TO_MPH_RATIO;
}
public static double knotsFromMps(double value) { // m/s
- return value * 1.94384;
+ return value * KNOTS_TO_MPS_RATIO;
}
public static double knotsFromCps(double value) { // cm/s
- return value * 0.0194384449;
+ return value * KNOTS_TO_CPS_RATIO;
}
}
diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java
index 1d5257b4f..fb8019900 100644
--- a/src/org/traccar/notification/NotificationFormatter.java
+++ b/src/org/traccar/notification/NotificationFormatter.java
@@ -15,10 +15,12 @@
*/
package org.traccar.notification;
+import java.text.DecimalFormat;
import java.util.Formatter;
import java.util.Locale;
import org.traccar.Context;
+import org.traccar.helper.UnitsConverter;
import org.traccar.model.Device;
import org.traccar.model.Event;
import org.traccar.model.Position;
@@ -55,7 +57,7 @@ public final class NotificationFormatter {
public static final String TITLE_TEMPLATE_TYPE_DEVICE_OVERSPEED = "%1$s: exceeds the speed";
public static final String MESSAGE_TEMPLATE_TYPE_DEVICE_OVERSPEED = "Device: %1$s%n"
- + "Exceeds the speed: %5$f%n"
+ + "Exceeds the speed: %5$s%n"
+ "Point: http://www.openstreetmap.org/?mlat=%3$f&mlon=%4$f#map=16/%3$f/%4$f%n"
+ "Time: %2$tc%n";
@@ -161,7 +163,7 @@ public final class NotificationFormatter {
break;
case Event.TYPE_DEVICE_OVERSPEED:
formatter.format(MESSAGE_TEMPLATE_TYPE_DEVICE_OVERSPEED, device.getName(), position.getFixTime(),
- position.getLatitude(), position.getLongitude(), position.getSpeed());
+ position.getLatitude(), position.getLongitude(), formatSpeed(userId, position.getSpeed()));
break;
case Event.TYPE_GEOFENCE_ENTER:
formatter.format(MESSAGE_TEMPLATE_TYPE_GEOFENCE_ENTER, device.getName(), position.getFixTime(),
@@ -194,4 +196,20 @@ public final class NotificationFormatter {
formatter.close();
return result;
}
+
+ private static String formatSpeed(long userId, double speed) {
+ DecimalFormat df = new DecimalFormat("#.##");
+ String result = df.format(speed) + " kn";
+ switch (Context.getPermissionsManager().getUser(userId).getSpeedUnit()) {
+ case "kmh":
+ result = df.format(UnitsConverter.kphFromKnots(speed)) + " km/h";
+ break;
+ case "mph":
+ result = df.format(UnitsConverter.mphFromKnots(speed)) + " mph";
+ break;
+ default:
+ break;
+ }
+ return result;
+ }
}