From 51704590a835057bf4850a5eaf8c5037d20f648a Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 28 Dec 2020 17:02:23 -0800 Subject: Migrate long config keys --- src/main/java/org/traccar/Context.java | 10 +-- .../java/org/traccar/api/AsyncSocketServlet.java | 7 +- src/main/java/org/traccar/config/Config.java | 22 +++--- src/main/java/org/traccar/config/Keys.java | 86 ++++++++++++++++++++++ .../org/traccar/database/ConnectionManager.java | 5 +- .../java/org/traccar/database/DeviceManager.java | 9 +-- src/main/java/org/traccar/reports/ReportUtils.java | 3 +- 7 files changed, 113 insertions(+), 29 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/traccar/Context.java b/src/main/java/org/traccar/Context.java index 7e38a0e41..8492e4230 100644 --- a/src/main/java/org/traccar/Context.java +++ b/src/main/java/org/traccar/Context.java @@ -252,11 +252,11 @@ public final class Context { public static TripsConfig initTripsConfig() { return new TripsConfig( - config.getLong("report.trip.minimalTripDistance", 500), - config.getLong("report.trip.minimalTripDuration", 300) * 1000, - config.getLong("report.trip.minimalParkingDuration", 300) * 1000, - config.getLong("report.trip.minimalNoDataDuration", 3600) * 1000, - config.getBoolean("report.trip.useIgnition"), + config.getLong(Keys.REPORT_TRIP_MINIMAL_TRIP_DISTANCE), + config.getLong(Keys.REPORT_TRIP_MINIMAL_TRIP_DURATION) * 1000, + config.getLong(Keys.REPORT_TRIP_MINIMAL_PARKING_DURATION) * 1000, + config.getLong(Keys.REPORT_TRIP_MINIMAL_NO_DATA_DURATION) * 1000, + config.getBoolean(Keys.REPORT_TRIP_USE_IGNITION), config.getBoolean(Keys.EVENT_MOTION_PROCESS_INVALID_POSITIONS), config.getDouble(Keys.EVENT_MOTION_SPEED_THRESHOLD)); } diff --git a/src/main/java/org/traccar/api/AsyncSocketServlet.java b/src/main/java/org/traccar/api/AsyncSocketServlet.java index 51fbfb478..490fc89fc 100644 --- a/src/main/java/org/traccar/api/AsyncSocketServlet.java +++ b/src/main/java/org/traccar/api/AsyncSocketServlet.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2020 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. @@ -19,14 +19,13 @@ import org.eclipse.jetty.websocket.servlet.WebSocketServlet; import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory; import org.traccar.Context; import org.traccar.api.resource.SessionResource; +import org.traccar.config.Keys; public class AsyncSocketServlet extends WebSocketServlet { - private static final long ASYNC_TIMEOUT = 10 * 60 * 1000; - @Override public void configure(WebSocketServletFactory factory) { - factory.getPolicy().setIdleTimeout(Context.getConfig().getLong("web.timeout", ASYNC_TIMEOUT)); + factory.getPolicy().setIdleTimeout(Context.getConfig().getLong(Keys.WEB_TIMEOUT)); factory.setCreator((req, resp) -> { if (req.getSession() != null) { long userId = (Long) req.getSession().getAttribute(SessionResource.USER_ID_KEY); diff --git a/src/main/java/org/traccar/config/Config.java b/src/main/java/org/traccar/config/Config.java index 6cc21cbeb..713f5c607 100644 --- a/src/main/java/org/traccar/config/Config.java +++ b/src/main/java/org/traccar/config/Config.java @@ -117,17 +117,17 @@ public class Config { } public long getLong(ConfigKey key) { - return getLong(key.getKey()); - } - - @Deprecated - public long getLong(String key) { - return getLong(key, 0); - } - - @Deprecated - public long getLong(String key, long defaultValue) { - return hasKey(key) ? Long.parseLong(getString(key)) : defaultValue; + String value = getString(key.getKey()); + if (value != null) { + return Long.parseLong(value); + } else { + Long defaultValue = key.getDefaultValue(); + if (defaultValue != null) { + return defaultValue; + } else { + return 0; + } + } } public double getDouble(ConfigKey key) { diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java index 3a42a66a1..ea3176afd 100644 --- a/src/main/java/org/traccar/config/Keys.java +++ b/src/main/java/org/traccar/config/Keys.java @@ -161,6 +161,45 @@ public final class Keys { "database.saveOriginal", Collections.singletonList(KeyType.GLOBAL)); + /** + * Default category for auto-registered devices. + */ + public static final ConfigKey DATABASE_REGISTER_UNKNOWN_DEFAULT_CATEGORY = new ConfigKey<>( + "database.registerUnknown.defaultCategory", + Collections.singletonList(KeyType.GLOBAL)); + + /** + * The group id assigned to auto-registered devices. + */ + public static final ConfigKey DATABASE_REGISTER_UNKNOWN_DEFAULT_GROUP_ID = new ConfigKey<>( + "database.registerUnknown.defaultGroupId", + Collections.singletonList(KeyType.GLOBAL)); + + /** + * Minimum device refresh timeout in seconds. Default timeout is 5 minutes. + */ + public static final ConfigKey DATABASE_REFRESH_DELAY = new ConfigKey<>( + "database.refreshDelay", + Collections.singletonList(KeyType.GLOBAL), + 300L); + + /** + * If no data is reported by a device for the given amount of time, status changes from online to unknown. Value is + * in seconds. Default timeout is 10 minutes. + */ + public static final ConfigKey STATUS_TIMEOUT = new ConfigKey<>( + "status.timeout", + Collections.singletonList(KeyType.GLOBAL), + 600L); + + /** + * WebSocket connection timeout in milliseconds. Default timeout is 10 minutes. + */ + public static final ConfigKey WEB_TIMEOUT = new ConfigKey<>( + "web.timeout", + Collections.singletonList(KeyType.GLOBAL), + 60000L); + /** * Enable positions forwarding to other web server. */ @@ -233,6 +272,53 @@ public final class Keys { "forward.retry.limit", Collections.singletonList(KeyType.GLOBAL)); + /** + * Maximum time period for reports in seconds. Can be useful to prevent users to request unreasonably long reports. + * By default there is no limit. + */ + public static final ConfigKey REPORT_PERIOD_LIMIT = new ConfigKey<>( + "report.periodLimit", + Collections.singletonList(KeyType.GLOBAL)); + + /** + * Trips less than minimal duration and minimal distance are ignored. 300 seconds and 500 meters are default. + */ + public static final ConfigKey REPORT_TRIP_MINIMAL_TRIP_DISTANCE = new ConfigKey<>( + "report.trip.minimalTripDistance", + Collections.singletonList(KeyType.GLOBAL), + 500L); + + /** + * Trips less than minimal duration and minimal distance are ignored. 300 seconds and 500 meters are default. + */ + public static final ConfigKey REPORT_TRIP_MINIMAL_TRIP_DURATION = new ConfigKey<>( + "report.trip.minimalTripDuration", + Collections.singletonList(KeyType.GLOBAL), + 300L); + + /** + * Parking less than minimal duration does not cut trip. Default 300 seconds. + */ + public static final ConfigKey REPORT_TRIP_MINIMAL_PARKING_DURATION = new ConfigKey<>( + "report.trip.minimalParkingDuration", + Collections.singletonList(KeyType.GLOBAL), + 300L); + + /** + * Gaps of more than specified time are counted as stops. Default value is one hour. + */ + public static final ConfigKey REPORT_TRIP_MINIMAL_NO_DATA_DURATION = new ConfigKey<>( + "report.trip.minimalNoDataDuration", + Collections.singletonList(KeyType.GLOBAL), + 3600L); + + /** + * Flag to enable ignition use for trips calculation. + */ + public static final ConfigKey REPORT_TRIP_USE_IGNITION = new ConfigKey<>( + "report.trip.useIgnition", + Collections.singletonList(KeyType.GLOBAL)); + /** * Boolean flag to enable or disable position filtering. */ diff --git a/src/main/java/org/traccar/database/ConnectionManager.java b/src/main/java/org/traccar/database/ConnectionManager.java index 9342fd3de..8278baa7c 100644 --- a/src/main/java/org/traccar/database/ConnectionManager.java +++ b/src/main/java/org/traccar/database/ConnectionManager.java @@ -23,6 +23,7 @@ import org.traccar.Context; import org.traccar.GlobalTimer; import org.traccar.Main; import org.traccar.Protocol; +import org.traccar.config.Keys; import org.traccar.handler.events.MotionEventHandler; import org.traccar.handler.events.OverspeedEventHandler; import org.traccar.model.Device; @@ -44,8 +45,6 @@ public class ConnectionManager { private static final Logger LOGGER = LoggerFactory.getLogger(ConnectionManager.class); - private static final long DEFAULT_TIMEOUT = 600; - private final long deviceTimeout; private final boolean enableStatusEvents; private final boolean updateDeviceState; @@ -55,7 +54,7 @@ public class ConnectionManager { private final Map timeouts = new ConcurrentHashMap<>(); public ConnectionManager() { - deviceTimeout = Context.getConfig().getLong("status.timeout", DEFAULT_TIMEOUT) * 1000; + deviceTimeout = Context.getConfig().getLong(Keys.STATUS_TIMEOUT) * 1000; enableStatusEvents = Context.getConfig().getBoolean("event.enable"); updateDeviceState = Context.getConfig().getBoolean("status.updateDeviceState"); } diff --git a/src/main/java/org/traccar/database/DeviceManager.java b/src/main/java/org/traccar/database/DeviceManager.java index c78526b3e..d238e9229 100644 --- a/src/main/java/org/traccar/database/DeviceManager.java +++ b/src/main/java/org/traccar/database/DeviceManager.java @@ -29,6 +29,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.traccar.config.Config; import org.traccar.Context; +import org.traccar.config.Keys; import org.traccar.model.Command; import org.traccar.model.Device; import org.traccar.model.DeviceState; @@ -41,8 +42,6 @@ public class DeviceManager extends BaseObjectManager implements Identity private static final Logger LOGGER = LoggerFactory.getLogger(DeviceManager.class); - public static final long DEFAULT_REFRESH_DELAY = 300; - private final Config config; private final long dataRefreshDelay; @@ -68,7 +67,7 @@ public class DeviceManager extends BaseObjectManager implements Identity } finally { writeUnlock(); } - dataRefreshDelay = config.getLong("database.refreshDelay", DEFAULT_REFRESH_DELAY) * 1000; + dataRefreshDelay = config.getLong(Keys.DATABASE_REFRESH_DELAY) * 1000; refreshLastPositions(); } @@ -77,9 +76,9 @@ public class DeviceManager extends BaseObjectManager implements Identity Device device = new Device(); device.setName(uniqueId); device.setUniqueId(uniqueId); - device.setCategory(Context.getConfig().getString("database.registerUnknown.defaultCategory")); + device.setCategory(Context.getConfig().getString(Keys.DATABASE_REGISTER_UNKNOWN_DEFAULT_CATEGORY)); - long defaultGroupId = Context.getConfig().getLong("database.registerUnknown.defaultGroupId"); + long defaultGroupId = Context.getConfig().getLong(Keys.DATABASE_REGISTER_UNKNOWN_DEFAULT_GROUP_ID); if (defaultGroupId != 0) { device.setGroupId(defaultGroupId); } diff --git a/src/main/java/org/traccar/reports/ReportUtils.java b/src/main/java/org/traccar/reports/ReportUtils.java index 8dd77606c..d8dfeed4c 100644 --- a/src/main/java/org/traccar/reports/ReportUtils.java +++ b/src/main/java/org/traccar/reports/ReportUtils.java @@ -26,6 +26,7 @@ import org.jxls.transform.Transformer; import org.jxls.transform.poi.PoiTransformer; import org.jxls.util.TransformerFactory; import org.traccar.Context; +import org.traccar.config.Keys; import org.traccar.database.DeviceManager; import org.traccar.database.IdentityManager; import org.traccar.handler.events.MotionEventHandler; @@ -58,7 +59,7 @@ public final class ReportUtils { } public static void checkPeriodLimit(Date from, Date to) { - long limit = Context.getConfig().getLong("report.periodLimit") * 1000; + long limit = Context.getConfig().getLong(Keys.REPORT_PERIOD_LIMIT) * 1000; if (limit > 0 && to.getTime() - from.getTime() > limit) { throw new IllegalArgumentException("Time period exceeds the limit"); } -- cgit v1.2.3