aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-06-10 07:56:49 -0700
committerAnton Tananaev <anton@traccar.org>2022-06-10 07:56:49 -0700
commitbbe84d6a751fdc840e4201ef9027a96527006049 (patch)
tree4165d09a134eb1c1c61438b17fe44f5a1fcd0654 /src
parentc03b4a2ace925e6a0d7c43ce59e14ddb9cbf18a9 (diff)
downloadtrackermap-server-bbe84d6a751fdc840e4201ef9027a96527006049.tar.gz
trackermap-server-bbe84d6a751fdc840e4201ef9027a96527006049.tar.bz2
trackermap-server-bbe84d6a751fdc840e4201ef9027a96527006049.zip
Inject report utils
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/traccar/Context.java7
-rw-r--r--src/main/java/org/traccar/api/resource/ServerResource.java10
-rw-r--r--src/main/java/org/traccar/database/NotificationManager.java14
-rw-r--r--src/main/java/org/traccar/handler/events/MotionEventHandler.java11
-rw-r--r--src/main/java/org/traccar/helper/model/PositionUtil.java13
-rw-r--r--src/main/java/org/traccar/reports/EventsReportProvider.java64
-rw-r--r--src/main/java/org/traccar/reports/RouteReportProvider.java43
-rw-r--r--src/main/java/org/traccar/reports/StopsReportProvider.java62
-rw-r--r--src/main/java/org/traccar/reports/SummaryReportProvider.java43
-rw-r--r--src/main/java/org/traccar/reports/TripsReportProvider.java54
-rw-r--r--src/main/java/org/traccar/reports/common/ReportUtils.java165
-rw-r--r--src/test/java/org/traccar/reports/ReportUtilsTest.java78
12 files changed, 282 insertions, 282 deletions
diff --git a/src/main/java/org/traccar/Context.java b/src/main/java/org/traccar/Context.java
index cba4c0a58..471416926 100644
--- a/src/main/java/org/traccar/Context.java
+++ b/src/main/java/org/traccar/Context.java
@@ -109,10 +109,6 @@ public final class Context {
return permissionsManager;
}
- public static Geocoder getGeocoder() {
- return Main.getInjector() != null ? Main.getInjector().getInstance(Geocoder.class) : null;
- }
-
private static NotificationManager notificationManager;
public static NotificationManager getNotificationManager() {
@@ -196,7 +192,8 @@ public final class Context {
notificationManager = new NotificationManager(
dataManager,
Main.getInjector().getInstance(CacheManager.class),
- Main.getInjector().getInstance(NotificatorManager.class));
+ Main.getInjector().getInstance(NotificatorManager.class),
+ Main.getInjector().getInstance(Geocoder.class));
Properties velocityProperties = new Properties();
velocityProperties.setProperty("file.resource.loader.path",
Context.getConfig().getString("templates.rootPath", "templates") + "/");
diff --git a/src/main/java/org/traccar/api/resource/ServerResource.java b/src/main/java/org/traccar/api/resource/ServerResource.java
index 51a26825b..18230a2b3 100644
--- a/src/main/java/org/traccar/api/resource/ServerResource.java
+++ b/src/main/java/org/traccar/api/resource/ServerResource.java
@@ -18,6 +18,7 @@ package org.traccar.api.resource;
import org.traccar.Context;
import org.traccar.api.BaseResource;
import org.traccar.database.MailManager;
+import org.traccar.geocoder.Geocoder;
import org.traccar.helper.LogAction;
import org.traccar.model.Server;
import org.traccar.storage.Storage;
@@ -25,6 +26,7 @@ import org.traccar.storage.StorageException;
import org.traccar.storage.query.Columns;
import org.traccar.storage.query.Request;
+import javax.annotation.Nullable;
import javax.annotation.security.PermitAll;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
@@ -50,6 +52,10 @@ public class ServerResource extends BaseResource {
@Inject
private MailManager mailManager;
+ @Inject
+ @Nullable
+ private Geocoder geocoder;
+
@PermitAll
@GET
public Server get() throws StorageException {
@@ -69,8 +75,8 @@ public class ServerResource extends BaseResource {
@Path("geocode")
@GET
public String geocode(@QueryParam("latitude") double latitude, @QueryParam("longitude") double longitude) {
- if (Context.getGeocoder() != null) {
- return Context.getGeocoder().getAddress(latitude, longitude, null);
+ if (geocoder != null) {
+ return geocoder.getAddress(latitude, longitude, null);
} else {
throw new RuntimeException("Reverse geocoding is not enabled");
}
diff --git a/src/main/java/org/traccar/database/NotificationManager.java b/src/main/java/org/traccar/database/NotificationManager.java
index eddd7e2b4..1f4a48ac0 100644
--- a/src/main/java/org/traccar/database/NotificationManager.java
+++ b/src/main/java/org/traccar/database/NotificationManager.java
@@ -30,6 +30,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.traccar.Context;
import org.traccar.config.Keys;
+import org.traccar.geocoder.Geocoder;
import org.traccar.model.Calendar;
import org.traccar.model.Event;
import org.traccar.model.Notification;
@@ -41,20 +42,25 @@ import org.traccar.notification.NotificatorManager;
import org.traccar.session.cache.CacheManager;
import org.traccar.storage.StorageException;
+import javax.annotation.Nullable;
+
public class NotificationManager extends ExtendedObjectManager<Notification> {
private static final Logger LOGGER = LoggerFactory.getLogger(NotificationManager.class);
private final CacheManager cacheManager;
private final NotificatorManager notificatorManager;
+ private final Geocoder geocoder;
private final boolean geocodeOnRequest;
public NotificationManager(
- DataManager dataManager, CacheManager cacheManager, NotificatorManager notificatorManager) {
+ DataManager dataManager, CacheManager cacheManager,
+ NotificatorManager notificatorManager, @Nullable Geocoder geocoder) {
super(dataManager, Notification.class);
this.cacheManager = cacheManager;
this.notificatorManager = notificatorManager;
+ this.geocoder = geocoder;
geocodeOnRequest = Context.getConfig().getBoolean(Keys.GEOCODER_ON_REQUEST);
}
@@ -110,10 +116,8 @@ public class NotificationManager extends ExtendedObjectManager<Notification> {
}
}
- if (position != null && position.getAddress() == null
- && geocodeOnRequest && Context.getGeocoder() != null) {
- position.setAddress(Context.getGeocoder()
- .getAddress(position.getLatitude(), position.getLongitude(), null));
+ if (position != null && position.getAddress() == null && geocodeOnRequest && geocoder != null) {
+ position.setAddress(geocoder.getAddress(position.getLatitude(), position.getLongitude(), null));
}
User user = Context.getUsersManager().getById(userId);
diff --git a/src/main/java/org/traccar/handler/events/MotionEventHandler.java b/src/main/java/org/traccar/handler/events/MotionEventHandler.java
index 57d2bc1c5..2c381e530 100644
--- a/src/main/java/org/traccar/handler/events/MotionEventHandler.java
+++ b/src/main/java/org/traccar/handler/events/MotionEventHandler.java
@@ -16,20 +16,19 @@
*/
package org.traccar.handler.events;
-import java.util.Collections;
-import java.util.Map;
-
import io.netty.channel.ChannelHandler;
import org.traccar.database.DeviceManager;
import org.traccar.database.IdentityManager;
+import org.traccar.helper.model.PositionUtil;
import org.traccar.model.Device;
-import org.traccar.session.DeviceState;
import org.traccar.model.Event;
import org.traccar.model.Position;
-import org.traccar.reports.common.ReportUtils;
import org.traccar.reports.common.TripsConfig;
+import org.traccar.session.DeviceState;
import javax.inject.Inject;
+import java.util.Collections;
+import java.util.Map;
@ChannelHandler.Sharable
public class MotionEventHandler extends BaseEventHandler {
@@ -89,7 +88,7 @@ public class MotionEventHandler extends BaseEventHandler {
Position motionPosition = deviceState.getMotionPosition();
if (motionPosition != null) {
long motionTime = motionPosition.getFixTime().getTime();
- double distance = ReportUtils.calculateDistance(motionPosition, position, false);
+ double distance = PositionUtil.calculateDistance(motionPosition, position, false);
Boolean ignition = null;
if (tripsConfig.getUseIgnition()
&& position.getAttributes().containsKey(Position.KEY_IGNITION)) {
diff --git a/src/main/java/org/traccar/helper/model/PositionUtil.java b/src/main/java/org/traccar/helper/model/PositionUtil.java
index 64216a937..644517dac 100644
--- a/src/main/java/org/traccar/helper/model/PositionUtil.java
+++ b/src/main/java/org/traccar/helper/model/PositionUtil.java
@@ -28,4 +28,17 @@ public final class PositionUtil {
return lastPosition == null || position.getFixTime().compareTo(lastPosition.getFixTime()) >= 0;
}
+ public static double calculateDistance(Position first, Position last, boolean useOdometer) {
+ double distance;
+ double firstOdometer = first.getDouble(Position.KEY_ODOMETER);
+ double lastOdometer = last.getDouble(Position.KEY_ODOMETER);
+
+ if (useOdometer && firstOdometer != 0.0 && lastOdometer != 0.0) {
+ distance = lastOdometer - firstOdometer;
+ } else {
+ distance = last.getDouble(Position.KEY_TOTAL_DISTANCE) - first.getDouble(Position.KEY_TOTAL_DISTANCE);
+ }
+ return distance;
+ }
+
}
diff --git a/src/main/java/org/traccar/reports/EventsReportProvider.java b/src/main/java/org/traccar/reports/EventsReportProvider.java
index f0c8c31b6..6de313a13 100644
--- a/src/main/java/org/traccar/reports/EventsReportProvider.java
+++ b/src/main/java/org/traccar/reports/EventsReportProvider.java
@@ -16,19 +16,8 @@
*/
package org.traccar.reports;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Iterator;
-
import org.apache.poi.ss.util.WorkbookUtil;
import org.traccar.Context;
-import org.traccar.api.security.PermissionsService;
import org.traccar.model.Device;
import org.traccar.model.Event;
import org.traccar.model.Geofence;
@@ -36,28 +25,34 @@ import org.traccar.model.Group;
import org.traccar.model.Maintenance;
import org.traccar.reports.common.ReportUtils;
import org.traccar.reports.model.DeviceReportSection;
-import org.traccar.storage.Storage;
import org.traccar.storage.StorageException;
import javax.inject.Inject;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Iterator;
public class EventsReportProvider {
- private final PermissionsService permissionsService;
- private final Storage storage;
+ private final ReportUtils reportUtils;
@Inject
- public EventsReportProvider(PermissionsService permissionsService, Storage storage) {
- this.permissionsService = permissionsService;
- this.storage = storage;
+ public EventsReportProvider(ReportUtils reportUtils) {
+ this.reportUtils = reportUtils;
}
public Collection<Event> getObjects(
long userId, Collection<Long> deviceIds, Collection<Long> groupIds,
Collection<String> types, Date from, Date to) throws StorageException {
- ReportUtils.checkPeriodLimit(from, to);
+ reportUtils.checkPeriodLimit(from, to);
ArrayList<Event> result = new ArrayList<>();
- for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) {
+ for (long deviceId: reportUtils.getDeviceList(deviceIds, groupIds)) {
Context.getPermissionsManager().checkDevice(userId, deviceId);
Collection<Event> events = Context.getDataManager().getEvents(deviceId, from, to);
boolean all = types.isEmpty() || types.contains(Event.ALL_EVENTS);
@@ -65,9 +60,9 @@ public class EventsReportProvider {
if (all || types.contains(event.getType())) {
long geofenceId = event.getGeofenceId();
long maintenanceId = event.getMaintenanceId();
- if ((geofenceId == 0 || ReportUtils.getObject(storage, userId, Geofence.class, geofenceId) != null)
+ if ((geofenceId == 0 || reportUtils.getObject(userId, Geofence.class, geofenceId) != null)
&& (maintenanceId == 0
- || ReportUtils.getObject(storage, userId, Maintenance.class, maintenanceId) != null)) {
+ || reportUtils.getObject(userId, Maintenance.class, maintenanceId) != null)) {
result.add(event);
}
}
@@ -79,12 +74,12 @@ public class EventsReportProvider {
public void getExcel(
OutputStream outputStream, long userId, Collection<Long> deviceIds, Collection<Long> groupIds,
Collection<String> types, Date from, Date to) throws StorageException, IOException {
- ReportUtils.checkPeriodLimit(from, to);
+ reportUtils.checkPeriodLimit(from, to);
ArrayList<DeviceReportSection> devicesEvents = new ArrayList<>();
ArrayList<String> sheetNames = new ArrayList<>();
HashMap<Long, String> geofenceNames = new HashMap<>();
HashMap<Long, String> maintenanceNames = new HashMap<>();
- for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) {
+ for (long deviceId: reportUtils.getDeviceList(deviceIds, groupIds)) {
Context.getPermissionsManager().checkDevice(userId, deviceId);
Collection<Event> events = Context.getDataManager().getEvents(deviceId, from, to);
boolean all = types.isEmpty() || types.contains(Event.ALL_EVENTS);
@@ -94,16 +89,14 @@ public class EventsReportProvider {
long geofenceId = event.getGeofenceId();
long maintenanceId = event.getMaintenanceId();
if (geofenceId != 0) {
- Geofence geofence = ReportUtils.getObject(
- storage, userId, Geofence.class, geofenceId);
+ Geofence geofence = reportUtils.getObject(userId, Geofence.class, geofenceId);
if (geofence != null) {
geofenceNames.put(geofenceId, geofence.getName());
} else {
iterator.remove();
}
} else if (maintenanceId != 0) {
- Maintenance maintenance = ReportUtils.getObject(
- storage, userId, Maintenance.class, maintenanceId);
+ Maintenance maintenance = reportUtils.getObject(userId, Maintenance.class, maintenanceId);
if (maintenance != null) {
maintenanceNames.put(maintenanceId, maintenance.getName());
} else {
@@ -130,15 +123,14 @@ public class EventsReportProvider {
String templatePath = Context.getConfig().getString("report.templatesPath",
"templates/export/");
try (InputStream inputStream = new FileInputStream(templatePath + "/events.xlsx")) {
- var jxlsContext = ReportUtils.initializeContext(
- permissionsService.getServer(), permissionsService.getUser(userId));
- jxlsContext.putVar("devices", devicesEvents);
- jxlsContext.putVar("sheetNames", sheetNames);
- jxlsContext.putVar("geofenceNames", geofenceNames);
- jxlsContext.putVar("maintenanceNames", maintenanceNames);
- jxlsContext.putVar("from", from);
- jxlsContext.putVar("to", to);
- ReportUtils.processTemplateWithSheets(inputStream, outputStream, jxlsContext);
+ var context = reportUtils.initializeContext(userId);
+ context.putVar("devices", devicesEvents);
+ context.putVar("sheetNames", sheetNames);
+ context.putVar("geofenceNames", geofenceNames);
+ context.putVar("maintenanceNames", maintenanceNames);
+ context.putVar("from", from);
+ context.putVar("to", to);
+ reportUtils.processTemplateWithSheets(inputStream, outputStream, context);
}
}
}
diff --git a/src/main/java/org/traccar/reports/RouteReportProvider.java b/src/main/java/org/traccar/reports/RouteReportProvider.java
index e20ba6885..136a154aa 100644
--- a/src/main/java/org/traccar/reports/RouteReportProvider.java
+++ b/src/main/java/org/traccar/reports/RouteReportProvider.java
@@ -16,17 +16,8 @@
*/
package org.traccar.reports;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Date;
-
import org.apache.poi.ss.util.WorkbookUtil;
import org.traccar.Context;
-import org.traccar.api.security.PermissionsService;
import org.traccar.model.Device;
import org.traccar.model.Group;
import org.traccar.model.Position;
@@ -35,21 +26,28 @@ import org.traccar.reports.model.DeviceReportSection;
import org.traccar.storage.StorageException;
import javax.inject.Inject;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
public class RouteReportProvider {
- private final PermissionsService permissionsService;
+ private final ReportUtils reportUtils;
@Inject
- public RouteReportProvider(PermissionsService permissionsService) {
- this.permissionsService = permissionsService;
+ public RouteReportProvider(ReportUtils reportUtils) {
+ this.reportUtils = reportUtils;
}
public Collection<Position> getObjects(long userId, Collection<Long> deviceIds, Collection<Long> groupIds,
Date from, Date to) throws StorageException {
- ReportUtils.checkPeriodLimit(from, to);
+ reportUtils.checkPeriodLimit(from, to);
ArrayList<Position> result = new ArrayList<>();
- for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) {
+ for (long deviceId: reportUtils.getDeviceList(deviceIds, groupIds)) {
Context.getPermissionsManager().checkDevice(userId, deviceId);
result.addAll(Context.getDataManager().getPositions(deviceId, from, to));
}
@@ -59,10 +57,10 @@ public class RouteReportProvider {
public void getExcel(OutputStream outputStream,
long userId, Collection<Long> deviceIds, Collection<Long> groupIds,
Date from, Date to) throws StorageException, IOException {
- ReportUtils.checkPeriodLimit(from, to);
+ reportUtils.checkPeriodLimit(from, to);
ArrayList<DeviceReportSection> devicesRoutes = new ArrayList<>();
ArrayList<String> sheetNames = new ArrayList<>();
- for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) {
+ for (long deviceId: reportUtils.getDeviceList(deviceIds, groupIds)) {
Context.getPermissionsManager().checkDevice(userId, deviceId);
Collection<Position> positions = Context.getDataManager()
.getPositions(deviceId, from, to);
@@ -82,13 +80,12 @@ public class RouteReportProvider {
String templatePath = Context.getConfig().getString("report.templatesPath",
"templates/export/");
try (InputStream inputStream = new FileInputStream(templatePath + "/route.xlsx")) {
- var jxlsContext = ReportUtils.initializeContext(
- permissionsService.getServer(), permissionsService.getUser(userId));
- jxlsContext.putVar("devices", devicesRoutes);
- jxlsContext.putVar("sheetNames", sheetNames);
- jxlsContext.putVar("from", from);
- jxlsContext.putVar("to", to);
- ReportUtils.processTemplateWithSheets(inputStream, outputStream, jxlsContext);
+ var context = reportUtils.initializeContext(userId);
+ context.putVar("devices", devicesRoutes);
+ context.putVar("sheetNames", sheetNames);
+ context.putVar("from", from);
+ context.putVar("to", to);
+ reportUtils.processTemplateWithSheets(inputStream, outputStream, context);
}
}
}
diff --git a/src/main/java/org/traccar/reports/StopsReportProvider.java b/src/main/java/org/traccar/reports/StopsReportProvider.java
index b7896c423..807a6133b 100644
--- a/src/main/java/org/traccar/reports/StopsReportProvider.java
+++ b/src/main/java/org/traccar/reports/StopsReportProvider.java
@@ -14,65 +14,48 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package org.traccar.reports;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Date;
-
import org.apache.poi.ss.util.WorkbookUtil;
import org.traccar.Context;
-import org.traccar.Main;
-import org.traccar.api.security.PermissionsService;
-import org.traccar.database.DeviceManager;
-import org.traccar.database.IdentityManager;
import org.traccar.model.Device;
import org.traccar.model.Group;
import org.traccar.reports.common.ReportUtils;
-import org.traccar.reports.common.TripsConfig;
import org.traccar.reports.model.DeviceReportSection;
import org.traccar.reports.model.StopReportItem;
-import org.traccar.storage.Storage;
import org.traccar.storage.StorageException;
import javax.inject.Inject;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
public class StopsReportProvider {
- private final PermissionsService permissionsService;
- private final Storage storage;
- private final TripsConfig tripsConfig;
+ private final ReportUtils reportUtils;
@Inject
- public StopsReportProvider(PermissionsService permissionsService, Storage storage, TripsConfig tripsConfig) {
- this.permissionsService = permissionsService;
- this.storage = storage;
- this.tripsConfig = tripsConfig;
+ public StopsReportProvider(ReportUtils reportUtils) {
+ this.reportUtils = reportUtils;
}
private Collection<StopReportItem> detectStops(long deviceId, Date from, Date to) throws StorageException {
boolean ignoreOdometer = Context.getDeviceManager()
.lookupAttributeBoolean(deviceId, "report.ignoreOdometer", false, false, true);
-
- IdentityManager identityManager = Main.getInjector().getInstance(IdentityManager.class);
- DeviceManager deviceManager = Main.getInjector().getInstance(DeviceManager.class);
-
- return ReportUtils.detectTripsAndStops(
- storage, identityManager, deviceManager, Context.getDataManager().getPositions(deviceId, from, to),
- tripsConfig, ignoreOdometer, StopReportItem.class);
+ return reportUtils.detectTripsAndStops(
+ Context.getDataManager().getPositions(deviceId, from, to), ignoreOdometer, StopReportItem.class);
}
public Collection<StopReportItem> getObjects(
long userId, Collection<Long> deviceIds, Collection<Long> groupIds,
Date from, Date to) throws StorageException {
- ReportUtils.checkPeriodLimit(from, to);
+ reportUtils.checkPeriodLimit(from, to);
ArrayList<StopReportItem> result = new ArrayList<>();
- for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) {
+ for (long deviceId: reportUtils.getDeviceList(deviceIds, groupIds)) {
Context.getPermissionsManager().checkDevice(userId, deviceId);
result.addAll(detectStops(deviceId, from, to));
}
@@ -82,10 +65,10 @@ public class StopsReportProvider {
public void getExcel(
OutputStream outputStream, long userId, Collection<Long> deviceIds, Collection<Long> groupIds,
Date from, Date to) throws StorageException, IOException {
- ReportUtils.checkPeriodLimit(from, to);
+ reportUtils.checkPeriodLimit(from, to);
ArrayList<DeviceReportSection> devicesStops = new ArrayList<>();
ArrayList<String> sheetNames = new ArrayList<>();
- for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) {
+ for (long deviceId: reportUtils.getDeviceList(deviceIds, groupIds)) {
Context.getPermissionsManager().checkDevice(userId, deviceId);
Collection<StopReportItem> stops = detectStops(deviceId, from, to);
DeviceReportSection deviceStops = new DeviceReportSection();
@@ -104,13 +87,12 @@ public class StopsReportProvider {
String templatePath = Context.getConfig().getString("report.templatesPath",
"templates/export/");
try (InputStream inputStream = new FileInputStream(templatePath + "/stops.xlsx")) {
- var jxlsContext = ReportUtils.initializeContext(
- permissionsService.getServer(), permissionsService.getUser(userId));
- jxlsContext.putVar("devices", devicesStops);
- jxlsContext.putVar("sheetNames", sheetNames);
- jxlsContext.putVar("from", from);
- jxlsContext.putVar("to", to);
- ReportUtils.processTemplateWithSheets(inputStream, outputStream, jxlsContext);
+ var context = reportUtils.initializeContext(userId);
+ context.putVar("devices", devicesStops);
+ context.putVar("sheetNames", sheetNames);
+ context.putVar("from", from);
+ context.putVar("to", to);
+ reportUtils.processTemplateWithSheets(inputStream, outputStream, context);
}
}
diff --git a/src/main/java/org/traccar/reports/SummaryReportProvider.java b/src/main/java/org/traccar/reports/SummaryReportProvider.java
index 27dc5819d..28abe790b 100644
--- a/src/main/java/org/traccar/reports/SummaryReportProvider.java
+++ b/src/main/java/org/traccar/reports/SummaryReportProvider.java
@@ -16,19 +16,11 @@
*/
package org.traccar.reports;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.Collection;
-import java.util.Date;
-
import org.jxls.util.JxlsHelper;
import org.traccar.Context;
import org.traccar.api.security.PermissionsService;
import org.traccar.helper.UnitsConverter;
+import org.traccar.helper.model.PositionUtil;
import org.traccar.helper.model.UserUtil;
import org.traccar.model.Position;
import org.traccar.reports.common.ReportUtils;
@@ -36,13 +28,23 @@ import org.traccar.reports.model.SummaryReportItem;
import org.traccar.storage.StorageException;
import javax.inject.Inject;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Collection;
+import java.util.Date;
public class SummaryReportProvider {
+ private final ReportUtils reportUtils;
private final PermissionsService permissionsService;
@Inject
- public SummaryReportProvider(PermissionsService permissionsService) {
+ public SummaryReportProvider(ReportUtils reportUtils, PermissionsService permissionsService) {
+ this.reportUtils = reportUtils;
this.permissionsService = permissionsService;
}
@@ -64,8 +66,8 @@ public class SummaryReportProvider {
}
boolean ignoreOdometer = Context.getDeviceManager()
.lookupAttributeBoolean(deviceId, "report.ignoreOdometer", false, false, true);
- result.setDistance(ReportUtils.calculateDistance(firstPosition, previousPosition, !ignoreOdometer));
- result.setSpentFuel(ReportUtils.calculateFuel(firstPosition, previousPosition));
+ result.setDistance(PositionUtil.calculateDistance(firstPosition, previousPosition, !ignoreOdometer));
+ result.setSpentFuel(reportUtils.calculateFuel(firstPosition, previousPosition));
long durationMilliseconds;
if (firstPosition.getAttributes().containsKey(Position.KEY_HOURS)
@@ -134,9 +136,9 @@ public class SummaryReportProvider {
public Collection<SummaryReportItem> getObjects(
long userId, Collection<Long> deviceIds,
Collection<Long> groupIds, Date from, Date to, boolean daily) throws StorageException {
- ReportUtils.checkPeriodLimit(from, to);
+ reportUtils.checkPeriodLimit(from, to);
ArrayList<SummaryReportItem> result = new ArrayList<>();
- for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) {
+ for (long deviceId: reportUtils.getDeviceList(deviceIds, groupIds)) {
Context.getPermissionsManager().checkDevice(userId, deviceId);
Collection<SummaryReportItem> deviceResults = calculateSummaryResults(userId, deviceId, from, to, daily);
for (SummaryReportItem summaryReport : deviceResults) {
@@ -151,18 +153,17 @@ public class SummaryReportProvider {
public void getExcel(OutputStream outputStream,
long userId, Collection<Long> deviceIds, Collection<Long> groupIds,
Date from, Date to, boolean daily) throws StorageException, IOException {
- ReportUtils.checkPeriodLimit(from, to);
+ reportUtils.checkPeriodLimit(from, to);
Collection<SummaryReportItem> summaries = getObjects(userId, deviceIds, groupIds, from, to, daily);
String templatePath = Context.getConfig().getString("report.templatesPath",
"templates/export/");
try (InputStream inputStream = new FileInputStream(templatePath + "/summary.xlsx")) {
- var jxlsContext = ReportUtils.initializeContext(
- permissionsService.getServer(), permissionsService.getUser(userId));
- jxlsContext.putVar("summaries", summaries);
- jxlsContext.putVar("from", from);
- jxlsContext.putVar("to", to);
+ var context = reportUtils.initializeContext(userId);
+ context.putVar("summaries", summaries);
+ context.putVar("from", from);
+ context.putVar("to", to);
JxlsHelper.getInstance().setUseFastFormulaProcessor(false)
- .processTemplate(inputStream, outputStream, jxlsContext);
+ .processTemplate(inputStream, outputStream, context);
}
}
}
diff --git a/src/main/java/org/traccar/reports/TripsReportProvider.java b/src/main/java/org/traccar/reports/TripsReportProvider.java
index 0505baaa1..5e598cb50 100644
--- a/src/main/java/org/traccar/reports/TripsReportProvider.java
+++ b/src/main/java/org/traccar/reports/TripsReportProvider.java
@@ -16,42 +16,34 @@
*/
package org.traccar.reports;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Date;
-
import org.apache.poi.ss.util.WorkbookUtil;
import org.traccar.Context;
import org.traccar.Main;
-import org.traccar.api.security.PermissionsService;
import org.traccar.database.DeviceManager;
import org.traccar.database.IdentityManager;
import org.traccar.model.Device;
import org.traccar.model.Group;
import org.traccar.reports.common.ReportUtils;
-import org.traccar.reports.common.TripsConfig;
import org.traccar.reports.model.DeviceReportSection;
import org.traccar.reports.model.TripReportItem;
-import org.traccar.storage.Storage;
import org.traccar.storage.StorageException;
import javax.inject.Inject;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
public class TripsReportProvider {
- private final PermissionsService permissionsService;
- private final Storage storage;
- private final TripsConfig tripsConfig;
+ private final ReportUtils reportUtils;
@Inject
- public TripsReportProvider(PermissionsService permissionsService, Storage storage, TripsConfig tripsConfig) {
- this.permissionsService = permissionsService;
- this.storage = storage;
- this.tripsConfig = tripsConfig;
+ public TripsReportProvider(ReportUtils reportUtils) {
+ this.reportUtils = reportUtils;
}
private Collection<TripReportItem> detectTrips(long deviceId, Date from, Date to) throws StorageException {
@@ -61,16 +53,15 @@ public class TripsReportProvider {
IdentityManager identityManager = Main.getInjector().getInstance(IdentityManager.class);
DeviceManager deviceManager = Main.getInjector().getInstance(DeviceManager.class);
- return ReportUtils.detectTripsAndStops(
- storage, identityManager, deviceManager, Context.getDataManager().getPositions(deviceId, from, to),
- tripsConfig, ignoreOdometer, TripReportItem.class);
+ return reportUtils.detectTripsAndStops(
+ Context.getDataManager().getPositions(deviceId, from, to), ignoreOdometer, TripReportItem.class);
}
public Collection<TripReportItem> getObjects(long userId, Collection<Long> deviceIds, Collection<Long> groupIds,
Date from, Date to) throws StorageException {
- ReportUtils.checkPeriodLimit(from, to);
+ reportUtils.checkPeriodLimit(from, to);
ArrayList<TripReportItem> result = new ArrayList<>();
- for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) {
+ for (long deviceId: reportUtils.getDeviceList(deviceIds, groupIds)) {
Context.getPermissionsManager().checkDevice(userId, deviceId);
result.addAll(detectTrips(deviceId, from, to));
}
@@ -80,10 +71,10 @@ public class TripsReportProvider {
public void getExcel(OutputStream outputStream,
long userId, Collection<Long> deviceIds, Collection<Long> groupIds,
Date from, Date to) throws StorageException, IOException {
- ReportUtils.checkPeriodLimit(from, to);
+ reportUtils.checkPeriodLimit(from, to);
ArrayList<DeviceReportSection> devicesTrips = new ArrayList<>();
ArrayList<String> sheetNames = new ArrayList<>();
- for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) {
+ for (long deviceId: reportUtils.getDeviceList(deviceIds, groupIds)) {
Context.getPermissionsManager().checkDevice(userId, deviceId);
Collection<TripReportItem> trips = detectTrips(deviceId, from, to);
DeviceReportSection deviceTrips = new DeviceReportSection();
@@ -102,13 +93,12 @@ public class TripsReportProvider {
String templatePath = Context.getConfig().getString("report.templatesPath",
"templates/export/");
try (InputStream inputStream = new FileInputStream(templatePath + "/trips.xlsx")) {
- var jxlsContext = ReportUtils.initializeContext(
- permissionsService.getServer(), permissionsService.getUser(userId));
- jxlsContext.putVar("devices", devicesTrips);
- jxlsContext.putVar("sheetNames", sheetNames);
- jxlsContext.putVar("from", from);
- jxlsContext.putVar("to", to);
- ReportUtils.processTemplateWithSheets(inputStream, outputStream, jxlsContext);
+ var context = reportUtils.initializeContext(userId);
+ context.putVar("devices", devicesTrips);
+ context.putVar("sheetNames", sheetNames);
+ context.putVar("from", from);
+ context.putVar("to", to);
+ reportUtils.processTemplateWithSheets(inputStream, outputStream, context);
}
}
diff --git a/src/main/java/org/traccar/reports/common/ReportUtils.java b/src/main/java/org/traccar/reports/common/ReportUtils.java
index 36d5c0fb1..706475241 100644
--- a/src/main/java/org/traccar/reports/common/ReportUtils.java
+++ b/src/main/java/org/traccar/reports/common/ReportUtils.java
@@ -26,28 +26,33 @@ import org.jxls.transform.Transformer;
import org.jxls.transform.poi.PoiTransformer;
import org.jxls.util.TransformerFactory;
import org.traccar.Context;
+import org.traccar.api.security.PermissionsService;
+import org.traccar.config.Config;
import org.traccar.config.Keys;
import org.traccar.database.DeviceManager;
import org.traccar.database.IdentityManager;
+import org.traccar.geocoder.Geocoder;
import org.traccar.handler.events.MotionEventHandler;
import org.traccar.helper.UnitsConverter;
+import org.traccar.helper.model.PositionUtil;
import org.traccar.helper.model.UserUtil;
import org.traccar.model.BaseModel;
-import org.traccar.model.Server;
-import org.traccar.model.User;
-import org.traccar.session.DeviceState;
import org.traccar.model.Driver;
import org.traccar.model.Event;
import org.traccar.model.Position;
+import org.traccar.model.User;
import org.traccar.reports.model.BaseReportItem;
import org.traccar.reports.model.StopReportItem;
import org.traccar.reports.model.TripReportItem;
+import org.traccar.session.DeviceState;
import org.traccar.storage.Storage;
import org.traccar.storage.StorageException;
import org.traccar.storage.query.Columns;
import org.traccar.storage.query.Condition;
import org.traccar.storage.query.Request;
+import javax.annotation.Nullable;
+import javax.inject.Inject;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -61,13 +66,31 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
-public final class ReportUtils {
-
- private ReportUtils() {
+public class ReportUtils {
+
+ private final Config config;
+ private final Storage storage;
+ private final PermissionsService permissionsService;
+ private final IdentityManager identityManager;
+ private final DeviceManager deviceManager;
+ private final TripsConfig tripsConfig;
+ private final Geocoder geocoder;
+
+ @Inject
+ public ReportUtils(
+ Config config, Storage storage, PermissionsService permissionsService, IdentityManager identityManager,
+ DeviceManager deviceManager, TripsConfig tripsConfig, @Nullable Geocoder geocoder) {
+ this.config = config;
+ this.storage = storage;
+ this.permissionsService = permissionsService;
+ this.identityManager = identityManager;
+ this.deviceManager = deviceManager;
+ this.tripsConfig = tripsConfig;
+ this.geocoder = geocoder;
}
- public static <T extends BaseModel> T getObject(
- Storage storage, long userId, Class<T> clazz, long objectId) throws StorageException, SecurityException {
+ public <T extends BaseModel> T getObject(
+ long userId, Class<T> clazz, long objectId) throws StorageException, SecurityException {
return storage.getObject(clazz, new Request(
new Columns.Include("id"),
new Condition.And(
@@ -75,14 +98,14 @@ public final class ReportUtils {
new Condition.Permission(User.class, userId, clazz))));
}
- public static void checkPeriodLimit(Date from, Date to) {
- long limit = Context.getConfig().getLong(Keys.REPORT_PERIOD_LIMIT) * 1000;
+ public void checkPeriodLimit(Date from, Date to) {
+ long limit = config.getLong(Keys.REPORT_PERIOD_LIMIT) * 1000;
if (limit > 0 && to.getTime() - from.getTime() > limit) {
throw new IllegalArgumentException("Time period exceeds the limit");
}
}
- public static Collection<Long> getDeviceList(Collection<Long> deviceIds, Collection<Long> groupIds) {
+ public Collection<Long> getDeviceList(Collection<Long> deviceIds, Collection<Long> groupIds) {
Collection<Long> result = new LinkedHashSet<>(deviceIds);
for (long groupId : groupIds) {
result.addAll(Context.getPermissionsManager().getGroupDevices(groupId));
@@ -90,26 +113,7 @@ public final class ReportUtils {
return result;
}
- public static double calculateDistance(Position firstPosition, Position lastPosition) {
- return calculateDistance(firstPosition, lastPosition, true);
- }
-
- public static double calculateDistance(Position firstPosition, Position lastPosition, boolean useOdometer) {
- double distance = 0.0;
- double firstOdometer = firstPosition.getDouble(Position.KEY_ODOMETER);
- double lastOdometer = lastPosition.getDouble(Position.KEY_ODOMETER);
-
- if (useOdometer && firstOdometer != 0.0 && lastOdometer != 0.0) {
- distance = lastOdometer - firstOdometer;
- } else if (firstPosition.getAttributes().containsKey(Position.KEY_TOTAL_DISTANCE)
- && lastPosition.getAttributes().containsKey(Position.KEY_TOTAL_DISTANCE)) {
- distance = lastPosition.getDouble(Position.KEY_TOTAL_DISTANCE)
- - firstPosition.getDouble(Position.KEY_TOTAL_DISTANCE);
- }
- return distance;
- }
-
- public static double calculateFuel(Position firstPosition, Position lastPosition) {
+ public double calculateFuel(Position firstPosition, Position lastPosition) {
if (firstPosition.getAttributes().get(Position.KEY_FUEL_LEVEL) != null
&& lastPosition.getAttributes().get(Position.KEY_FUEL_LEVEL) != null) {
@@ -121,7 +125,7 @@ public final class ReportUtils {
return 0;
}
- public static String findDriver(Position firstPosition, Position lastPosition) {
+ public String findDriver(Position firstPosition, Position lastPosition) {
if (firstPosition.getAttributes().containsKey(Position.KEY_DRIVER_UNIQUE_ID)) {
return firstPosition.getString(Position.KEY_DRIVER_UNIQUE_ID);
} else if (lastPosition.getAttributes().containsKey(Position.KEY_DRIVER_UNIQUE_ID)) {
@@ -130,7 +134,7 @@ public final class ReportUtils {
return null;
}
- public static String findDriverName(Storage storage, String driverUniqueId) throws StorageException {
+ public String findDriverName(String driverUniqueId) throws StorageException {
if (driverUniqueId != null) {
Driver driver = storage.getObject(Driver.class, new Request(
new Columns.All(),
@@ -142,28 +146,29 @@ public final class ReportUtils {
return null;
}
- public static org.jxls.common.Context initializeContext(Server server, User user) {
- org.jxls.common.Context jxlsContext = PoiTransformer.createInitialContext();
- jxlsContext.putVar("distanceUnit", UserUtil.getDistanceUnit(server, user));
- jxlsContext.putVar("speedUnit", UserUtil.getSpeedUnit(server, user));
- jxlsContext.putVar("volumeUnit", UserUtil.getVolumeUnit(server, user));
- jxlsContext.putVar("webUrl", Context.getVelocityEngine().getProperty("web.url"));
- jxlsContext.putVar("dateTool", new DateTool());
- jxlsContext.putVar("numberTool", new NumberTool());
- jxlsContext.putVar("timezone", UserUtil.getTimezone(server, user));
- jxlsContext.putVar("locale", Locale.getDefault());
- jxlsContext.putVar("bracketsRegex", "[\\{\\}\"]");
- return jxlsContext;
+ public org.jxls.common.Context initializeContext(long userId) throws StorageException {
+ var server = permissionsService.getServer();
+ var user = permissionsService.getUser(userId);
+ var context = PoiTransformer.createInitialContext();
+ context.putVar("distanceUnit", UserUtil.getDistanceUnit(server, user));
+ context.putVar("speedUnit", UserUtil.getSpeedUnit(server, user));
+ context.putVar("volumeUnit", UserUtil.getVolumeUnit(server, user));
+ context.putVar("webUrl", Context.getVelocityEngine().getProperty("web.url"));
+ context.putVar("dateTool", new DateTool());
+ context.putVar("numberTool", new NumberTool());
+ context.putVar("timezone", UserUtil.getTimezone(server, user));
+ context.putVar("locale", Locale.getDefault());
+ context.putVar("bracketsRegex", "[\\{\\}\"]");
+ return context;
}
- public static void processTemplateWithSheets(
- InputStream templateStream, OutputStream targetStream,
- org.jxls.common.Context jxlsContext) throws IOException {
+ public void processTemplateWithSheets(
+ InputStream templateStream, OutputStream targetStream, org.jxls.common.Context context) throws IOException {
Transformer transformer = TransformerFactory.createTransformer(templateStream, targetStream);
List<Area> xlsAreas = new XlsCommentAreaBuilder(transformer).build();
for (Area xlsArea : xlsAreas) {
- xlsArea.applyAt(new CellRef(xlsArea.getStartCellRef().getCellName()), jxlsContext);
+ xlsArea.applyAt(new CellRef(xlsArea.getStartCellRef().getCellName()), context);
xlsArea.setFormulaProcessor(new StandardFormulaProcessor());
xlsArea.processFormulas();
}
@@ -171,9 +176,9 @@ public final class ReportUtils {
transformer.write();
}
- private static TripReportItem calculateTrip(
- Storage storage, IdentityManager identityManager, ArrayList<Position> positions,
- int startIndex, int endIndex, boolean ignoreOdometer) throws StorageException {
+ private TripReportItem calculateTrip(
+ ArrayList<Position> positions, int startIndex, int endIndex,
+ boolean ignoreOdometer) throws StorageException {
Position startTrip = positions.get(startIndex);
Position endTrip = positions.get(endIndex);
@@ -198,9 +203,8 @@ public final class ReportUtils {
trip.setStartLon(startTrip.getLongitude());
trip.setStartTime(startTrip.getFixTime());
String startAddress = startTrip.getAddress();
- if (startAddress == null && Context.getGeocoder() != null
- && Context.getConfig().getBoolean(Keys.GEOCODER_ON_REQUEST)) {
- startAddress = Context.getGeocoder().getAddress(startTrip.getLatitude(), startTrip.getLongitude(), null);
+ if (startAddress == null && geocoder != null && config.getBoolean(Keys.GEOCODER_ON_REQUEST)) {
+ startAddress = geocoder.getAddress(startTrip.getLatitude(), startTrip.getLongitude(), null);
}
trip.setStartAddress(startAddress);
@@ -209,13 +213,12 @@ public final class ReportUtils {
trip.setEndLon(endTrip.getLongitude());
trip.setEndTime(endTrip.getFixTime());
String endAddress = endTrip.getAddress();
- if (endAddress == null && Context.getGeocoder() != null
- && Context.getConfig().getBoolean(Keys.GEOCODER_ON_REQUEST)) {
- endAddress = Context.getGeocoder().getAddress(endTrip.getLatitude(), endTrip.getLongitude(), null);
+ if (endAddress == null && geocoder != null && config.getBoolean(Keys.GEOCODER_ON_REQUEST)) {
+ endAddress = geocoder.getAddress(endTrip.getLatitude(), endTrip.getLongitude(), null);
}
trip.setEndAddress(endAddress);
- trip.setDistance(calculateDistance(startTrip, endTrip, !ignoreOdometer));
+ trip.setDistance(PositionUtil.calculateDistance(startTrip, endTrip, !ignoreOdometer));
trip.setDuration(tripDuration);
if (tripDuration > 0) {
trip.setAverageSpeed(UnitsConverter.knotsFromMps(trip.getDistance() * 1000 / tripDuration));
@@ -224,7 +227,7 @@ public final class ReportUtils {
trip.setSpentFuel(calculateFuel(startTrip, endTrip));
trip.setDriverUniqueId(findDriver(startTrip, endTrip));
- trip.setDriverName(findDriverName(storage, trip.getDriverUniqueId()));
+ trip.setDriverName(findDriverName(trip.getDriverUniqueId()));
if (!ignoreOdometer
&& startTrip.getDouble(Position.KEY_ODOMETER) != 0
@@ -239,9 +242,8 @@ public final class ReportUtils {
return trip;
}
- private static StopReportItem calculateStop(
- IdentityManager identityManager, ArrayList<Position> positions,
- int startIndex, int endIndex, boolean ignoreOdometer) {
+ private StopReportItem calculateStop(
+ ArrayList<Position> positions, int startIndex, int endIndex, boolean ignoreOdometer) {
Position startStop = positions.get(startIndex);
Position endStop = positions.get(endIndex);
@@ -257,9 +259,8 @@ public final class ReportUtils {
stop.setLongitude(startStop.getLongitude());
stop.setStartTime(startStop.getFixTime());
String address = startStop.getAddress();
- if (address == null && Context.getGeocoder() != null
- && Context.getConfig().getBoolean(Keys.GEOCODER_ON_REQUEST)) {
- address = Context.getGeocoder().getAddress(stop.getLatitude(), stop.getLongitude(), null);
+ if (address == null && geocoder != null && config.getBoolean(Keys.GEOCODER_ON_REQUEST)) {
+ address = geocoder.getAddress(stop.getLatitude(), stop.getLongitude(), null);
}
stop.setAddress(address);
@@ -288,18 +289,19 @@ public final class ReportUtils {
}
- private static <T extends BaseReportItem> T calculateTripOrStop(
- Storage storage, IdentityManager identityManager, ArrayList<Position> positions,
- int startIndex, int endIndex, boolean ignoreOdometer, Class<T> reportClass) throws StorageException {
+ @SuppressWarnings("unchecked")
+ private <T extends BaseReportItem> T calculateTripOrStop(
+ ArrayList<Position> positions, int startIndex, int endIndex,
+ boolean ignoreOdometer, Class<T> reportClass) throws StorageException {
if (reportClass.equals(TripReportItem.class)) {
- return (T) calculateTrip(storage, identityManager, positions, startIndex, endIndex, ignoreOdometer);
+ return (T) calculateTrip(positions, startIndex, endIndex, ignoreOdometer);
} else {
- return (T) calculateStop(identityManager, positions, startIndex, endIndex, ignoreOdometer);
+ return (T) calculateStop(positions, startIndex, endIndex, ignoreOdometer);
}
}
- private static boolean isMoving(ArrayList<Position> positions, int index, TripsConfig tripsConfig) {
+ private boolean isMoving(ArrayList<Position> positions, int index, TripsConfig tripsConfig) {
if (tripsConfig.getMinimalNoDataDuration() > 0) {
boolean beforeGap = index < positions.size() - 1
&& positions.get(index + 1).getFixTime().getTime() - positions.get(index).getFixTime().getTime()
@@ -319,17 +321,16 @@ public final class ReportUtils {
}
}
- public static <T extends BaseReportItem> Collection<T> detectTripsAndStops(
- Storage storage, IdentityManager identityManager, DeviceManager deviceManager,
- Collection<Position> positionCollection,
- TripsConfig tripsConfig, boolean ignoreOdometer, Class<T> reportClass) throws StorageException {
+ public <T extends BaseReportItem> Collection<T> detectTripsAndStops(
+ Collection<Position> positionCollection, boolean ignoreOdometer,
+ Class<T> reportClass) throws StorageException {
Collection<T> result = new ArrayList<>();
ArrayList<Position> positions = new ArrayList<>(positionCollection);
if (!positions.isEmpty()) {
boolean trips = reportClass.equals(TripReportItem.class);
- MotionEventHandler motionHandler = new MotionEventHandler(identityManager, deviceManager, tripsConfig);
+ MotionEventHandler motionHandler = new MotionEventHandler(identityManager, deviceManager, tripsConfig);
DeviceState deviceState = new DeviceState();
deviceState.setMotionState(isMoving(positions, 0, tripsConfig));
int startEventIndex = trips == deviceState.getMotionState() ? 0 : -1;
@@ -355,15 +356,15 @@ public final class ReportUtils {
}
if (startEventIndex != -1 && startNoEventIndex != -1 && event != null
&& trips != deviceState.getMotionState()) {
- result.add(calculateTripOrStop(storage, identityManager, positions,
- startEventIndex, startNoEventIndex, ignoreOdometer, reportClass));
+ result.add(calculateTripOrStop(
+ positions, startEventIndex, startNoEventIndex, ignoreOdometer, reportClass));
startEventIndex = -1;
}
}
if (startEventIndex != -1 && (startNoEventIndex != -1 || !trips)) {
- result.add(calculateTripOrStop(storage, identityManager, positions,
- startEventIndex, startNoEventIndex != -1 ? startNoEventIndex : positions.size() - 1,
- ignoreOdometer, reportClass));
+ int endIndex = startNoEventIndex != -1 ? startNoEventIndex : positions.size() - 1;
+ result.add(calculateTripOrStop(
+ positions, startEventIndex, endIndex, ignoreOdometer, reportClass));
}
}
diff --git a/src/test/java/org/traccar/reports/ReportUtilsTest.java b/src/test/java/org/traccar/reports/ReportUtilsTest.java
index 9b287a0fd..92bfdae1c 100644
--- a/src/test/java/org/traccar/reports/ReportUtilsTest.java
+++ b/src/test/java/org/traccar/reports/ReportUtilsTest.java
@@ -2,7 +2,11 @@ package org.traccar.reports;
import org.junit.Test;
import org.traccar.BaseTest;
+import org.traccar.api.security.PermissionsService;
+import org.traccar.config.Config;
+import org.traccar.database.DeviceManager;
import org.traccar.database.IdentityManager;
+import org.traccar.helper.model.PositionUtil;
import org.traccar.model.Device;
import org.traccar.model.Position;
import org.traccar.reports.common.ReportUtils;
@@ -63,20 +67,23 @@ public class ReportUtilsTest extends BaseTest {
startPosition.set(Position.KEY_TOTAL_DISTANCE, 500.0);
Position endPosition = new Position();
endPosition.set(Position.KEY_TOTAL_DISTANCE, 700.0);
- assertEquals(ReportUtils.calculateDistance(startPosition, endPosition), 200.0, 10);
+ assertEquals(PositionUtil.calculateDistance(startPosition, endPosition, true), 200.0, 10);
startPosition.set(Position.KEY_ODOMETER, 50000);
endPosition.set(Position.KEY_ODOMETER, 51000);
- assertEquals(ReportUtils.calculateDistance(startPosition, endPosition), 1000.0, 10);
+ assertEquals(PositionUtil.calculateDistance(startPosition, endPosition, true), 1000.0, 10);
}
@Test
public void testCalculateSpentFuel() {
+ ReportUtils reportUtils = new ReportUtils(
+ mock(Config.class), mock(Storage.class), mock(PermissionsService.class),
+ mockIdentityManager(), mock(DeviceManager.class), mock(TripsConfig.class), null);
Position startPosition = new Position();
Position endPosition = new Position();
- assertEquals(ReportUtils.calculateFuel(startPosition, endPosition), 0.0, 0.01);
+ assertEquals(reportUtils.calculateFuel(startPosition, endPosition), 0.0, 0.01);
startPosition.set(Position.KEY_FUEL_LEVEL, 0.7);
endPosition.set(Position.KEY_FUEL_LEVEL, 0.5);
- assertEquals(ReportUtils.calculateFuel(startPosition, endPosition), 0.2, 0.01);
+ assertEquals(reportUtils.calculateFuel(startPosition, endPosition), 0.2, 0.01);
}
@Test
@@ -93,9 +100,11 @@ public class ReportUtilsTest extends BaseTest {
position("2016-01-01 00:07:00.000", 0, 3000));
TripsConfig tripsConfig = new TripsConfig(500, 300000, 180000, 900000, false, false, 0.01);
+ ReportUtils reportUtils = new ReportUtils(
+ mock(Config.class), mock(Storage.class), mock(PermissionsService.class),
+ mockIdentityManager(), mock(DeviceManager.class), tripsConfig, null);
- Collection<TripReportItem> trips = ReportUtils.detectTripsAndStops(
- mock(Storage.class), mockIdentityManager(), null, data, tripsConfig, false, TripReportItem.class);
+ Collection<TripReportItem> trips = reportUtils.detectTripsAndStops(data, false, TripReportItem.class);
assertNotNull(trips);
assertFalse(trips.isEmpty());
@@ -109,8 +118,7 @@ public class ReportUtilsTest extends BaseTest {
assertEquals(10, itemTrip.getMaxSpeed(), 0.01);
assertEquals(3000, itemTrip.getDistance(), 0.01);
- Collection<StopReportItem> stops = ReportUtils.detectTripsAndStops(
- mock(Storage.class), mockIdentityManager(), null, data, tripsConfig, false, StopReportItem.class);
+ Collection<StopReportItem> stops = reportUtils.detectTripsAndStops(data, false, StopReportItem.class);
assertNotNull(stops);
assertFalse(stops.isEmpty());
@@ -147,9 +155,11 @@ public class ReportUtilsTest extends BaseTest {
data.get(5).set(Position.KEY_IGNITION, false);
TripsConfig tripsConfig = new TripsConfig(500, 300000, 180000, 900000, true, false, 0.01);
+ ReportUtils reportUtils = new ReportUtils(
+ mock(Config.class), mock(Storage.class), mock(PermissionsService.class),
+ mockIdentityManager(), mock(DeviceManager.class), tripsConfig, null);
- Collection<TripReportItem> trips = ReportUtils.detectTripsAndStops(
- mock(Storage.class), mockIdentityManager(), null, data, tripsConfig, false, TripReportItem.class);
+ Collection<TripReportItem> trips = reportUtils.detectTripsAndStops(data, false, TripReportItem.class);
assertNotNull(trips);
assertFalse(trips.isEmpty());
@@ -163,8 +173,7 @@ public class ReportUtilsTest extends BaseTest {
assertEquals(10, itemTrip.getMaxSpeed(), 0.01);
assertEquals(3000, itemTrip.getDistance(), 0.01);
- trips = ReportUtils.detectTripsAndStops(
- mock(Storage.class), mockIdentityManager(), null, data, tripsConfig, false, TripReportItem.class);
+ trips = reportUtils.detectTripsAndStops(data, false, TripReportItem.class);
assertNotNull(trips);
assertFalse(trips.isEmpty());
@@ -178,8 +187,7 @@ public class ReportUtilsTest extends BaseTest {
assertEquals(10, itemTrip.getMaxSpeed(), 0.01);
assertEquals(3000, itemTrip.getDistance(), 0.01);
- Collection<StopReportItem> stops = ReportUtils.detectTripsAndStops(
- mock(Storage.class), mockIdentityManager(), null, data, tripsConfig, false, StopReportItem.class);
+ Collection<StopReportItem> stops = reportUtils.detectTripsAndStops(data, false, StopReportItem.class);
assertNotNull(stops);
assertFalse(stops.isEmpty());
@@ -218,9 +226,11 @@ public class ReportUtilsTest extends BaseTest {
position("2016-01-01 00:11:00.000", 0, 7000));
TripsConfig tripsConfig = new TripsConfig(500, 300000, 180000, 900000, false, false, 0.01);
+ ReportUtils reportUtils = new ReportUtils(
+ mock(Config.class), mock(Storage.class), mock(PermissionsService.class),
+ mockIdentityManager(), mock(DeviceManager.class), tripsConfig, null);
- Collection<TripReportItem> trips = ReportUtils.detectTripsAndStops(
- mock(Storage.class), mockIdentityManager(), null, data, tripsConfig, false, TripReportItem.class);
+ Collection<TripReportItem> trips = reportUtils.detectTripsAndStops(data, false, TripReportItem.class);
assertNotNull(trips);
assertFalse(trips.isEmpty());
@@ -234,8 +244,7 @@ public class ReportUtilsTest extends BaseTest {
assertEquals(10, itemTrip.getMaxSpeed(), 0.01);
assertEquals(7000, itemTrip.getDistance(), 0.01);
- Collection<StopReportItem> stops = ReportUtils.detectTripsAndStops(
- mock(Storage.class), mockIdentityManager(), null, data, tripsConfig, false, StopReportItem.class);
+ Collection<StopReportItem> stops = reportUtils.detectTripsAndStops(data, false, StopReportItem.class);
assertNotNull(stops);
assertFalse(stops.isEmpty());
@@ -268,9 +277,11 @@ public class ReportUtilsTest extends BaseTest {
position("2016-01-01 00:05:00.000", 0, 0));
TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, 900000, false, false, 0.01);
+ ReportUtils reportUtils = new ReportUtils(
+ mock(Config.class), mock(Storage.class), mock(PermissionsService.class),
+ mockIdentityManager(), mock(DeviceManager.class), tripsConfig, null);
- Collection<StopReportItem> result = ReportUtils.detectTripsAndStops(
- mock(Storage.class), mockIdentityManager(), null, data, tripsConfig, false, StopReportItem.class);
+ Collection<StopReportItem> result = reportUtils.detectTripsAndStops(data, false, StopReportItem.class);
assertNotNull(result);
assertFalse(result.isEmpty());
@@ -295,9 +306,11 @@ public class ReportUtilsTest extends BaseTest {
position("2016-01-01 00:05:00.000", 2, 0));
TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, 900000, false, false, 0.01);
+ ReportUtils reportUtils = new ReportUtils(
+ mock(Config.class), mock(Storage.class), mock(PermissionsService.class),
+ mockIdentityManager(), mock(DeviceManager.class), tripsConfig, null);
- Collection<StopReportItem> result = ReportUtils.detectTripsAndStops(
- mock(Storage.class), mockIdentityManager(), null, data, tripsConfig, false, StopReportItem.class);
+ Collection<StopReportItem> result = reportUtils.detectTripsAndStops(data, false, StopReportItem.class);
assertNotNull(result);
assertFalse(result.isEmpty());
@@ -322,9 +335,11 @@ public class ReportUtilsTest extends BaseTest {
position("2016-01-01 00:05:00.000", 0, 0));
TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, 900000, false, false, 0.01);
+ ReportUtils reportUtils = new ReportUtils(
+ mock(Config.class), mock(Storage.class), mock(PermissionsService.class),
+ mockIdentityManager(), mock(DeviceManager.class), tripsConfig, null);
- Collection<StopReportItem> result = ReportUtils.detectTripsAndStops(
- mock(Storage.class), mockIdentityManager(), null, data, tripsConfig, false, StopReportItem.class);
+ Collection<StopReportItem> result = reportUtils.detectTripsAndStops(data, false, StopReportItem.class);
assertNotNull(result);
assertFalse(result.isEmpty());
@@ -349,9 +364,11 @@ public class ReportUtilsTest extends BaseTest {
position("2016-01-01 00:05:00.000", 5, 0));
TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, 900000, false, false, 0.01);
+ ReportUtils reportUtils = new ReportUtils(
+ mock(Config.class), mock(Storage.class), mock(PermissionsService.class),
+ mockIdentityManager(), mock(DeviceManager.class), tripsConfig, null);
- Collection<StopReportItem> result = ReportUtils.detectTripsAndStops(
- mock(Storage.class), mockIdentityManager(), null, data, tripsConfig, false, StopReportItem.class);
+ Collection<StopReportItem> result = reportUtils.detectTripsAndStops(data, false, StopReportItem.class);
assertNotNull(result);
assertTrue(result.isEmpty());
@@ -372,9 +389,11 @@ public class ReportUtilsTest extends BaseTest {
position("2016-01-01 00:25:00.000", 5, 900));
TripsConfig tripsConfig = new TripsConfig(500, 200000, 200000, 900000, false, false, 0.01);
+ ReportUtils reportUtils = new ReportUtils(
+ mock(Config.class), mock(Storage.class), mock(PermissionsService.class),
+ mockIdentityManager(), mock(DeviceManager.class), tripsConfig, null);
- Collection<TripReportItem> trips = ReportUtils.detectTripsAndStops(
- mock(Storage.class), mockIdentityManager(), null, data, tripsConfig, false, TripReportItem.class);
+ Collection<TripReportItem> trips = reportUtils.detectTripsAndStops(data, false, TripReportItem.class);
assertNotNull(trips);
assertFalse(trips.isEmpty());
@@ -388,8 +407,7 @@ public class ReportUtilsTest extends BaseTest {
assertEquals(7, itemTrip.getMaxSpeed(), 0.01);
assertEquals(600, itemTrip.getDistance(), 0.01);
- Collection<StopReportItem> stops = ReportUtils.detectTripsAndStops(
- mock(Storage.class), mockIdentityManager(), null, data, tripsConfig, false, StopReportItem.class);
+ Collection<StopReportItem> stops = reportUtils.detectTripsAndStops(data, false, StopReportItem.class);
assertNotNull(stops);
assertFalse(stops.isEmpty());