From 45a98c81bc5c9510dfe25f91b0e84d7608eaf7eb Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 28 Dec 2020 14:34:05 -0800 Subject: Refactor double keys --- src/main/java/org/traccar/Context.java | 2 +- src/main/java/org/traccar/config/Config.java | 20 +++++++++++++------- src/main/java/org/traccar/config/ConfigKey.java | 4 ++++ src/main/java/org/traccar/config/Keys.java | 6 ++++-- src/main/java/org/traccar/model/Geofence.java | 2 +- 5 files changed, 23 insertions(+), 11 deletions(-) (limited to 'src/main') diff --git a/src/main/java/org/traccar/Context.java b/src/main/java/org/traccar/Context.java index 340cdb6a5..7e38a0e41 100644 --- a/src/main/java/org/traccar/Context.java +++ b/src/main/java/org/traccar/Context.java @@ -258,7 +258,7 @@ public final class Context { config.getLong("report.trip.minimalNoDataDuration", 3600) * 1000, config.getBoolean("report.trip.useIgnition"), config.getBoolean(Keys.EVENT_MOTION_PROCESS_INVALID_POSITIONS), - config.getDouble(Keys.EVENT_MOTION_SPEED_THRESHOLD, 0.01)); + config.getDouble(Keys.EVENT_MOTION_SPEED_THRESHOLD)); } private static class ObjectMapperContextResolver implements ContextResolver { diff --git a/src/main/java/org/traccar/config/Config.java b/src/main/java/org/traccar/config/Config.java index 1231918c6..c6d87bfd6 100644 --- a/src/main/java/org/traccar/config/Config.java +++ b/src/main/java/org/traccar/config/Config.java @@ -134,16 +134,22 @@ public class Config { return hasKey(key) ? Long.parseLong(getString(key)) : defaultValue; } - public double getDouble(ConfigKey key) { - return getDouble(key, 0.0); - } - - public double getDouble(ConfigKey key, double defaultValue) { - return hasKey(key.getKey()) ? Double.parseDouble(getString(key.getKey())) : defaultValue; + public double getDouble(ConfigKey key) { + String value = getString(key.getKey()); + if (value != null) { + return Double.parseDouble(value); + } else { + Double defaultValue = key.getDefaultValue(); + if (defaultValue != null) { + return defaultValue; + } else { + return 0; + } + } } @VisibleForTesting - public void setString(ConfigKey key, String value) { + public void setString(ConfigKey key, String value) { properties.put(key.getKey(), value); } diff --git a/src/main/java/org/traccar/config/ConfigKey.java b/src/main/java/org/traccar/config/ConfigKey.java index 926b46851..cc095c087 100644 --- a/src/main/java/org/traccar/config/ConfigKey.java +++ b/src/main/java/org/traccar/config/ConfigKey.java @@ -37,4 +37,8 @@ public class ConfigKey { return key; } + public T getDefaultValue() { + return defaultValue; + } + } diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java index b6b78d4d7..dc204b2f5 100644 --- a/src/main/java/org/traccar/config/Keys.java +++ b/src/main/java/org/traccar/config/Keys.java @@ -91,7 +91,8 @@ public final class Keys { */ public static final ConfigKey EVENT_MOTION_SPEED_THRESHOLD = new ConfigKey<>( "event.motion.speedThreshold", - Collections.singletonList(KeyType.FILE)); + Collections.singletonList(KeyType.FILE), + 0.01); /** * Global polyline geofence distance. Within that distance from the polyline, point is considered within the @@ -99,7 +100,8 @@ public final class Keys { */ public static final ConfigKey GEOFENCE_POLYLINE_DISTANCE = new ConfigKey<>( "geofence.polylineDistance", - Collections.singletonList(KeyType.FILE)); + Collections.singletonList(KeyType.FILE), + 25.0); /** * Path to the database driver JAR file. Traccar includes drivers for MySQL, PostgreSQL and H2 databases. If you use diff --git a/src/main/java/org/traccar/model/Geofence.java b/src/main/java/org/traccar/model/Geofence.java index 835ea7c22..85f392f99 100644 --- a/src/main/java/org/traccar/model/Geofence.java +++ b/src/main/java/org/traccar/model/Geofence.java @@ -68,7 +68,7 @@ public class Geofence extends ScheduledModel { } else if (area.startsWith("LINESTRING")) { final double distance = getDouble("polylineDistance"); geometry = new GeofencePolyline(area, distance > 0 ? distance - : Context.getConfig().getDouble(Keys.GEOFENCE_POLYLINE_DISTANCE, 25)); + : Context.getConfig().getDouble(Keys.GEOFENCE_POLYLINE_DISTANCE)); } else { throw new ParseException("Unknown geometry type", 0); } -- cgit v1.2.3