From addbd4b58d959a7996af7631dd3449ac779b027f Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 9 Mar 2019 16:11:16 -0800 Subject: Refactor geofence event handler --- .../handler/events/GeofenceEventHandler.java | 25 ++++++++++++++-------- 1 file changed, 16 insertions(+), 9 deletions(-) (limited to 'src/org/traccar/handler') diff --git a/src/org/traccar/handler/events/GeofenceEventHandler.java b/src/org/traccar/handler/events/GeofenceEventHandler.java index 878331c84..067c97957 100644 --- a/src/org/traccar/handler/events/GeofenceEventHandler.java +++ b/src/org/traccar/handler/events/GeofenceEventHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2019 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. @@ -21,8 +21,9 @@ import java.util.List; import java.util.Map; import io.netty.channel.ChannelHandler; -import org.traccar.Context; +import org.traccar.database.CalendarManager; import org.traccar.database.GeofenceManager; +import org.traccar.database.IdentityManager; import org.traccar.model.Calendar; import org.traccar.model.Device; import org.traccar.model.Event; @@ -31,19 +32,24 @@ import org.traccar.model.Position; @ChannelHandler.Sharable public class GeofenceEventHandler extends BaseEventHandler { - private GeofenceManager geofenceManager; + private final IdentityManager identityManager; + private final GeofenceManager geofenceManager; + private final CalendarManager calendarManager; - public GeofenceEventHandler() { - geofenceManager = Context.getGeofenceManager(); + public GeofenceEventHandler( + IdentityManager identityManager, GeofenceManager geofenceManager, CalendarManager calendarManager) { + this.identityManager = identityManager; + this.geofenceManager = geofenceManager; + this.calendarManager = calendarManager; } @Override protected Map analyzePosition(Position position) { - Device device = Context.getIdentityManager().getById(position.getDeviceId()); + Device device = identityManager.getById(position.getDeviceId()); if (device == null) { return null; } - if (!Context.getIdentityManager().isLatestPosition(position) || !position.getValid()) { + if (!identityManager.isLatestPosition(position) || !position.getValid()) { return null; } @@ -61,7 +67,7 @@ public class GeofenceEventHandler extends BaseEventHandler { Map events = new HashMap<>(); for (long geofenceId : oldGeofences) { long calendarId = geofenceManager.getById(geofenceId).getCalendarId(); - Calendar calendar = calendarId != 0 ? Context.getCalendarManager().getById(calendarId) : null; + Calendar calendar = calendarId != 0 ? calendarManager.getById(calendarId) : null; if (calendar == null || calendar.checkMoment(position.getFixTime())) { Event event = new Event(Event.TYPE_GEOFENCE_EXIT, position.getDeviceId(), position.getId()); event.setGeofenceId(geofenceId); @@ -70,7 +76,7 @@ public class GeofenceEventHandler extends BaseEventHandler { } for (long geofenceId : newGeofences) { long calendarId = geofenceManager.getById(geofenceId).getCalendarId(); - Calendar calendar = calendarId != 0 ? Context.getCalendarManager().getById(calendarId) : null; + Calendar calendar = calendarId != 0 ? calendarManager.getById(calendarId) : null; if (calendar == null || calendar.checkMoment(position.getFixTime())) { Event event = new Event(Event.TYPE_GEOFENCE_ENTER, position.getDeviceId(), position.getId()); event.setGeofenceId(geofenceId); @@ -79,4 +85,5 @@ public class GeofenceEventHandler extends BaseEventHandler { } return events; } + } -- cgit v1.2.3