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(-) (limited to 'src') 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 From 3c7afdf75c4f3d045c0d51355d6ce62622eb2c4d Mon Sep 17 00:00:00 2001 From: jcardus Date: Wed, 26 Oct 2022 15:02:22 +0100 Subject: Update MaintenanceEventHandler.java trailing spaces --- src/main/java/org/traccar/handler/events/MaintenanceEventHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/main/java/org/traccar/handler/events/MaintenanceEventHandler.java b/src/main/java/org/traccar/handler/events/MaintenanceEventHandler.java index 25a622373..909950acf 100644 --- a/src/main/java/org/traccar/handler/events/MaintenanceEventHandler.java +++ b/src/main/java/org/traccar/handler/events/MaintenanceEventHandler.java @@ -52,7 +52,7 @@ public class MaintenanceEventHandler extends BaseEventHandler { double oldValue = lastPosition.getDouble(maintenance.getType()); double newValue = position.getDouble(maintenance.getType()); if (oldValue != 0.0 && newValue != 0.0 && newValue >= maintenance.getStart()) { - if (oldValue < 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); -- cgit v1.2.3 From 907e5d1669c154e266383a275c1212baea1bda2a Mon Sep 17 00:00:00 2001 From: jcardus Date: Wed, 26 Oct 2022 17:11:14 +0100 Subject: added MaintenanceEventHandlerTest --- .../events/MaintenanceEventHandlerTest.java | 60 ++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/test/java/org/traccar/handler/events/MaintenanceEventHandlerTest.java (limited to 'src') diff --git a/src/test/java/org/traccar/handler/events/MaintenanceEventHandlerTest.java b/src/test/java/org/traccar/handler/events/MaintenanceEventHandlerTest.java new file mode 100644 index 000000000..aa2d0bbe3 --- /dev/null +++ b/src/test/java/org/traccar/handler/events/MaintenanceEventHandlerTest.java @@ -0,0 +1,60 @@ +package org.traccar.handler.events; + +import org.junit.Test; +import org.traccar.BaseTest; +import org.traccar.model.Maintenance; +import org.traccar.model.Position; +import org.traccar.session.cache.CacheManager; + +import java.util.Arrays; +import java.util.Date; + +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.anyLong; + +public class MaintenanceEventHandlerTest extends BaseTest { + + @Test + public void testMaintenanceEventHandler() { + Position lastPosition = new Position(); + lastPosition.setDeviceId(1); + lastPosition.setFixTime(new Date(0)); + + Position position = new Position(); + position.setDeviceId(1); + position.setFixTime(new Date(0)); + + var maintenance = mock(Maintenance.class); + when(maintenance.getType()).thenReturn(Position.KEY_TOTAL_DISTANCE); + var maintenances = Arrays.asList(maintenance); + + var cacheManager = mock(CacheManager.class); + when(cacheManager.getDeviceObjects(anyLong(), eq(Maintenance.class))).thenReturn(maintenances); + when(cacheManager.getPosition(anyLong())).thenReturn(lastPosition); + MaintenanceEventHandler eventHandler = new MaintenanceEventHandler(cacheManager); + + when(maintenance.getStart()).thenReturn(10000.0); + when(maintenance.getPeriod()).thenReturn(2000.0); + + lastPosition.set(Position.KEY_TOTAL_DISTANCE, 1999); + position.set(Position.KEY_TOTAL_DISTANCE, 2001); + assertTrue(eventHandler.analyzePosition(position).isEmpty()); + + lastPosition.set(Position.KEY_TOTAL_DISTANCE, 3999); + position.set(Position.KEY_TOTAL_DISTANCE, 4001); + assertTrue(eventHandler.analyzePosition(position).isEmpty()); + + lastPosition.set(Position.KEY_TOTAL_DISTANCE, 9999); + position.set(Position.KEY_TOTAL_DISTANCE, 10001); + assertTrue(eventHandler.analyzePosition(position).size() == 1); + + lastPosition.set(Position.KEY_TOTAL_DISTANCE, 11999); + position.set(Position.KEY_TOTAL_DISTANCE, 12001); + assertTrue(eventHandler.analyzePosition(position).size() == 1); + + } + +} -- cgit v1.2.3