diff options
4 files changed, 88 insertions, 2 deletions
diff --git a/src/main/java/org/traccar/BasePipelineFactory.java b/src/main/java/org/traccar/BasePipelineFactory.java index 642c75ea9..c9f3a2346 100644 --- a/src/main/java/org/traccar/BasePipelineFactory.java +++ b/src/main/java/org/traccar/BasePipelineFactory.java @@ -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. @@ -40,6 +40,7 @@ import org.traccar.handler.SpeedLimitHandler; 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; @@ -132,6 +133,7 @@ public abstract class BasePipelineFactory extends ChannelInitializer<Channel> { DefaultDataHandler.class, CommandResultEventHandler.class, OverspeedEventHandler.class, + BehaviorEventHandler.class, FuelDropEventHandler.class, MotionEventHandler.class, GeofenceEventHandler.class, diff --git a/src/main/java/org/traccar/MainModule.java b/src/main/java/org/traccar/MainModule.java index 350af6bd7..e1a4d6b1c 100644 --- a/src/main/java/org/traccar/MainModule.java +++ b/src/main/java/org/traccar/MainModule.java @@ -65,6 +65,7 @@ import org.traccar.handler.RemoteAddressHandler; 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; @@ -369,6 +370,12 @@ public class MainModule extends AbstractModule { @Singleton @Provides + public static BehaviorEventHandler provideBehaviorEventHandler(Config config, IdentityManager identityManager) { + return new BehaviorEventHandler(config, identityManager); + } + + @Singleton + @Provides public static FuelDropEventHandler provideFuelDropEventHandler(IdentityManager identityManager) { return new FuelDropEventHandler(identityManager); } diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java index ef0edbac3..1411e8a13 100644 --- a/src/main/java/org/traccar/config/Keys.java +++ b/src/main/java/org/traccar/config/Keys.java @@ -212,13 +212,27 @@ public final class 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. */ public static final ConfigKey<Boolean> EVENT_IGNORE_DUPLICATE_ALERTS = new ConfigKey<>( diff --git a/src/main/java/org/traccar/handler/events/BehaviorEventHandler.java b/src/main/java/org/traccar/handler/events/BehaviorEventHandler.java new file mode 100644 index 000000000..47f5def09 --- /dev/null +++ b/src/main/java/org/traccar/handler/events/BehaviorEventHandler.java @@ -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; + } + +} |