From 8ceb20c2d9c70f5e0aa107095a23c187019401f1 Mon Sep 17 00:00:00 2001 From: jcardus Date: Wed, 26 Oct 2022 14:48:31 +0100 Subject: 2 Bugs on maintenance According to the documentation: "Traccar can help control maintenance intervals of devices. There are two attributes to configure: maintenance.start and maintenance.interval. They can be set in device, group or server attributes. Traccar generates event every time totalDistance attribute gets over maintenance.start + maintenance.interval * N value where N is a natural number. Example: maintenance.start=6000000, maintenance.interval=8000000 Events will be generated when totalDistance goes over 6000 km, 14000 km, 22000 km and so on." In this example, the event won't be fired when it goes over 6000 km. If maintenance.interval < maintenance.start and maintenance.start is multiple of maintenance.interval then the event will be fired before maintenance.start because the comparison will be on negative numbers. Another solution would be to use absolute numbers on the comparison. Example: maintenance.start = 10 000 000 maintenance.interval = 2 000 000 the event will be fired when it goes over 2 000 000 the event wont' be fired when it goes over 10 000 000 --- .../traccar/handler/events/MaintenanceEventHandler.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/traccar/handler/events/MaintenanceEventHandler.java b/src/main/java/org/traccar/handler/events/MaintenanceEventHandler.java index 4fcfcd079..25a622373 100644 --- a/src/main/java/org/traccar/handler/events/MaintenanceEventHandler.java +++ b/src/main/java/org/traccar/handler/events/MaintenanceEventHandler.java @@ -51,13 +51,15 @@ public class MaintenanceEventHandler extends BaseEventHandler { if (maintenance.getPeriod() != 0) { double oldValue = lastPosition.getDouble(maintenance.getType()); double newValue = position.getDouble(maintenance.getType()); - if (oldValue != 0.0 && newValue != 0.0 - && (long) ((oldValue - maintenance.getStart()) / maintenance.getPeriod()) + if (oldValue != 0.0 && newValue != 0.0 && newValue >= maintenance.getStart()) { + if (oldValue < maintenance.getStart() + || (long) ((oldValue - maintenance.getStart()) / maintenance.getPeriod()) < (long) ((newValue - maintenance.getStart()) / maintenance.getPeriod())) { - Event event = new Event(Event.TYPE_MAINTENANCE, position); - event.setMaintenanceId(maintenance.getId()); - event.set(maintenance.getType(), newValue); - events.put(event, position); + Event event = new Event(Event.TYPE_MAINTENANCE, position); + event.setMaintenanceId(maintenance.getId()); + event.set(maintenance.getType(), newValue); + events.put(event, position); + } } } } -- cgit v1.2.3