aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/traccar/handler/events/MotionEventHandlerTest.java
blob: b77676dc8a52c8c98e15697df7f3166678e31110 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package org.traccar.handler.events;

import org.junit.Test;
import org.traccar.BaseTest;
import org.traccar.model.Event;
import org.traccar.model.Position;
import org.traccar.reports.common.TripsConfig;
import org.traccar.session.state.MotionProcessor;
import org.traccar.session.state.MotionState;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class MotionEventHandlerTest extends BaseTest {

    private Position position(String time, boolean motion, double distance, Boolean ignition) throws ParseException {
        Position position = new Position();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        position.setTime(dateFormat.parse(time));
        position.set(Position.KEY_MOTION, motion);
        position.set(Position.KEY_TOTAL_DISTANCE, distance);
        position.set(Position.KEY_IGNITION, ignition);
        return position;
    }

    private void verifyState(MotionState motionState, boolean state, long distance) {
        assertEquals(state, motionState.getMotionState());
        assertEquals(distance, motionState.getMotionDistance(), 0.1);
    }

    @Test
    public void testMotionWithPosition() throws ParseException {
        TripsConfig tripsConfig = new TripsConfig(500, 300000, 300000, 0, false, false, 0.01);

        MotionState state = new MotionState();

        MotionProcessor.updateState(state, position("2017-01-01 00:00:00", false, 0, null), false, tripsConfig);
        assertNull(state.getEvent());
        verifyState(state, false, 0);

        MotionProcessor.updateState(state, position("2017-01-01 00:02:00", true, 100, null), true, tripsConfig);
        assertNull(state.getEvent());
        verifyState(state, true, 100);

        MotionProcessor.updateState(state, position("2017-01-01 00:02:00", true, 700, null), true, tripsConfig);
        assertEquals(Event.TYPE_DEVICE_MOVING, state.getEvent().getType());
        verifyState(state, true, 0);

        MotionProcessor.updateState(state, position("2017-01-01 00:03:00", false, 700, null), false, tripsConfig);
        assertNull(state.getEvent());
        verifyState(state, false, 700);

        MotionProcessor.updateState(state, position("2017-01-01 00:10:00", false, 700, null), false, tripsConfig);
        assertEquals(Event.TYPE_DEVICE_STOPPED, state.getEvent().getType());
        verifyState(state, false, 0);
    }

    @Test
    public void testMotionFluctuation() throws ParseException {
        TripsConfig tripsConfig = new TripsConfig(500, 300000, 300000, 0, false, false, 0.01);

        MotionState state = new MotionState();

        MotionProcessor.updateState(state, position("2017-01-01 00:00:00", false, 0, null), false, tripsConfig);
        assertNull(state.getEvent());
        verifyState(state, false, 0);

        MotionProcessor.updateState(state, position("2017-01-01 00:02:00", true, 100, null), true, tripsConfig);
        assertNull(state.getEvent());
        verifyState(state, true, 100);

        MotionProcessor.updateState(state, position("2017-01-01 00:02:00", true, 700, null), true, tripsConfig);
        assertEquals(Event.TYPE_DEVICE_MOVING, state.getEvent().getType());
        verifyState(state, true, 0);

        MotionProcessor.updateState(state, position("2017-01-01 00:03:00", false, 700, null), false, tripsConfig);
        assertNull(state.getEvent());
        verifyState(state, false, 700);

        MotionProcessor.updateState(state, position("2017-01-01 00:04:00", true, 1000, null), true, tripsConfig);
        assertNull(state.getEvent());
        verifyState(state, true, 0);

        MotionProcessor.updateState(state, position("2017-01-01 00:06:00", true, 2000, null), true, tripsConfig);
        assertNull(state.getEvent());
        verifyState(state, true, 0);
    }

    @Test
    public void testStopWithPositionIgnition() throws ParseException {
        TripsConfig tripsConfig = new TripsConfig(500, 300000, 300000, 0, true, false, 0.01);

        MotionState state = new MotionState();
        state.setMotionStreak(true);
        state.setMotionState(true);

        MotionProcessor.updateState(state, position("2017-01-01 00:00:00", false, 100, true), false, tripsConfig);
        assertNull(state.getEvent());
        verifyState(state, false, 100);

        MotionProcessor.updateState(state, position("2017-01-01 00:02:00", false, 100, false), false, tripsConfig);
        assertEquals(Event.TYPE_DEVICE_STOPPED, state.getEvent().getType());
        verifyState(state, false, 0);
    }

}