Skip to content

Commit

Permalink
Add driver behavior events
Browse files Browse the repository at this point in the history
  • Loading branch information
tananaev committed Aug 11, 2021
1 parent 51c6b0f commit a7288ed
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/main/java/org/traccar/BasePipelineFactory.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012 - 2020 Anton Tananaev (anton@traccar.org)
* Copyright 2012 - 2021 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.
Expand Down Expand Up @@ -40,6 +40,7 @@
import org.traccar.handler.StandardLoggingHandler;
import org.traccar.handler.TimeHandler;
import org.traccar.handler.events.AlertEventHandler;
import org.traccar.handler.events.BehaviorEventHandler;
import org.traccar.handler.events.CommandResultEventHandler;
import org.traccar.handler.events.DriverEventHandler;
import org.traccar.handler.events.FuelDropEventHandler;
Expand Down Expand Up @@ -132,6 +133,7 @@ protected void initChannel(Channel channel) {
DefaultDataHandler.class,
CommandResultEventHandler.class,
OverspeedEventHandler.class,
BehaviorEventHandler.class,
FuelDropEventHandler.class,
MotionEventHandler.class,
GeofenceEventHandler.class,
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/traccar/MainModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import org.traccar.handler.SpeedLimitHandler;
import org.traccar.handler.TimeHandler;
import org.traccar.handler.events.AlertEventHandler;
import org.traccar.handler.events.BehaviorEventHandler;
import org.traccar.handler.events.CommandResultEventHandler;
import org.traccar.handler.events.DriverEventHandler;
import org.traccar.handler.events.FuelDropEventHandler;
Expand Down Expand Up @@ -367,6 +368,12 @@ public static OverspeedEventHandler provideOverspeedEventHandler(
return new OverspeedEventHandler(config, deviceManager, geofenceManager);
}

@Singleton
@Provides
public static BehaviorEventHandler provideBehaviorEventHandler(Config config, IdentityManager identityManager) {
return new BehaviorEventHandler(config, identityManager);
}

@Singleton
@Provides
public static FuelDropEventHandler provideFuelDropEventHandler(IdentityManager identityManager) {
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/org/traccar/config/Keys.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,26 @@ private Keys() {
Collections.singletonList(KeyType.GLOBAL));

/**
* Relevant only for geofence speed limits. Use lowest speed limits from all geofences.
* Relevant only for geofence speed limits. Use the lowest speed limit from all geofences.
*/
public static final ConfigKey<Boolean> EVENT_OVERSPEED_PREFER_LOWEST = new ConfigKey<>(
"event.overspeed.preferLowest",
Collections.singletonList(KeyType.GLOBAL));

/**
* Driver behavior acceleration threshold. Value is in meter per second squared.
*/
public static final ConfigKey<Double> EVENT_BEHAVIOR_ACCELERATION_THRESHOLD = new ConfigKey<>(
"event.behavior.accelerationThreshold",
Collections.singletonList(KeyType.GLOBAL));

/**
* Driver behavior braking threshold. Value is in meter per second squared.
*/
public static final ConfigKey<Double> EVENT_BEHAVIOR_BRAKING_THRESHOLD = new ConfigKey<>(
"event.behavior.brakingThreshold",
Collections.singletonList(KeyType.GLOBAL));

/**
* Do not generate alert event if same alert was present in last known location.
*/
Expand Down
63 changes: 63 additions & 0 deletions src/main/java/org/traccar/handler/events/BehaviorEventHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.traccar.handler.events;

import io.netty.channel.ChannelHandler;
import org.traccar.config.Config;
import org.traccar.config.Keys;
import org.traccar.database.IdentityManager;
import org.traccar.helper.UnitsConverter;
import org.traccar.model.Event;
import org.traccar.model.Position;

import java.util.Collections;
import java.util.Map;

@ChannelHandler.Sharable
public class BehaviorEventHandler extends BaseEventHandler {

private final double accelerationThreshold;
private final double brakingThreshold;

private final IdentityManager identityManager;

public BehaviorEventHandler(Config config, IdentityManager identityManager) {
accelerationThreshold = config.getDouble(Keys.EVENT_BEHAVIOR_ACCELERATION_THRESHOLD);
brakingThreshold = config.getDouble(Keys.EVENT_BEHAVIOR_BRAKING_THRESHOLD);
this.identityManager = identityManager;
}

@Override
protected Map<Event, Position> analyzePosition(Position position) {

Position lastPosition = identityManager.getLastPosition(position.getDeviceId());
if (lastPosition != null) {
double acceleration = UnitsConverter.mpsFromKnots(position.getSpeed() - lastPosition.getSpeed()) * 1000
/ (position.getFixTime().getTime() - lastPosition.getFixTime().getTime());
if (accelerationThreshold != 0 && acceleration >= accelerationThreshold) {
Event event = new Event(Event.TYPE_ALARM, position);
event.set(Position.KEY_ALARM, Position.ALARM_ACCELERATION);
return Collections.singletonMap(event, position);
} else if (brakingThreshold != 0 && acceleration <= -brakingThreshold) {
Event event = new Event(Event.TYPE_ALARM, position);
event.set(Position.KEY_ALARM, Position.ALARM_BRAKING);
return Collections.singletonMap(event, position);
}
}
return null;
}

}

0 comments on commit a7288ed

Please sign in to comment.