Refactor driver event handler

This commit is contained in:
Anton Tananaev
2019-03-09 20:02:51 -08:00
parent c7e9f996ed
commit 69ebfe201e
3 changed files with 18 additions and 11 deletions

View File

@@ -59,8 +59,6 @@ public abstract class BasePipelineFactory extends ChannelInitializer<Channel> {
private boolean eventsEnabled;
private int timeout;
private DriverEventHandler driverEventHandler;
public BasePipelineFactory(TrackerServer server, String protocol) {
this.server = server;
eventsEnabled = Context.getConfig().getBoolean(Keys.EVENT_ENABLE);
@@ -68,10 +66,6 @@ public abstract class BasePipelineFactory extends ChannelInitializer<Channel> {
if (timeout == 0) {
timeout = Context.getConfig().getInteger(Keys.SERVER_TIMEOUT);
}
if (eventsEnabled) {
driverEventHandler = new DriverEventHandler();
}
}
protected abstract void addProtocolHandlers(PipelineBuilder pipeline);
@@ -152,7 +146,7 @@ public abstract class BasePipelineFactory extends ChannelInitializer<Channel> {
Main.getInjector().getInstance(AlertEventHandler.class),
Main.getInjector().getInstance(IgnitionEventHandler.class),
Main.getInjector().getInstance(MaintenanceEventHandler.class),
driverEventHandler);
Main.getInjector().getInstance(DriverEventHandler.class));
}
pipeline.addLast(new MainEventHandler());

View File

@@ -61,6 +61,7 @@ import org.traccar.handler.MotionHandler;
import org.traccar.handler.RemoteAddressHandler;
import org.traccar.handler.events.AlertEventHandler;
import org.traccar.handler.events.CommandResultEventHandler;
import org.traccar.handler.events.DriverEventHandler;
import org.traccar.handler.events.FuelDropEventHandler;
import org.traccar.handler.events.GeofenceEventHandler;
import org.traccar.handler.events.IgnitionEventHandler;
@@ -358,6 +359,12 @@ public class MainModule extends AbstractModule {
return new MaintenanceEventHandler(identityManager, maintenancesManager);
}
@Singleton
@Provides
public static DriverEventHandler provideDriverEventHandler(IdentityManager identityManager) {
return new DriverEventHandler(identityManager);
}
@Override
protected void configure() {
binder().requireExplicitBindings();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 Anton Tananaev (anton@traccar.org)
* Copyright 2017 - 2019 Anton Tananaev (anton@traccar.org)
* Copyright 2017 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -20,22 +20,28 @@ import java.util.Collections;
import java.util.Map;
import io.netty.channel.ChannelHandler;
import org.traccar.Context;
import org.traccar.database.IdentityManager;
import org.traccar.model.Event;
import org.traccar.model.Position;
@ChannelHandler.Sharable
public class DriverEventHandler extends BaseEventHandler {
private final IdentityManager identityManager;
public DriverEventHandler(IdentityManager identityManager) {
this.identityManager = identityManager;
}
@Override
protected Map<Event, Position> analyzePosition(Position position) {
if (!Context.getIdentityManager().isLatestPosition(position)) {
if (!identityManager.isLatestPosition(position)) {
return null;
}
String driverUniqueId = position.getString(Position.KEY_DRIVER_UNIQUE_ID);
if (driverUniqueId != null) {
String oldDriverUniqueId = null;
Position lastPosition = Context.getIdentityManager().getLastPosition(position.getDeviceId());
Position lastPosition = identityManager.getLastPosition(position.getDeviceId());
if (lastPosition != null) {
oldDriverUniqueId = lastPosition.getString(Position.KEY_DRIVER_UNIQUE_ID);
}