Skip to content

Commit

Permalink
refactor(Moved various methods into the LegTransitionNotification cla…
Browse files Browse the repository at this point in the history
…ss):
  • Loading branch information
br648 committed Dec 13, 2024
1 parent 17363ac commit bc20b84
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
package org.opentripplanner.middleware.models;

import org.opentripplanner.middleware.i18n.Message;
import org.opentripplanner.middleware.otp.response.Leg;
import org.opentripplanner.middleware.persistence.Persistence;
import org.opentripplanner.middleware.tripmonitor.jobs.CheckMonitoredTrip;
import org.opentripplanner.middleware.tripmonitor.jobs.NotificationType;
import org.opentripplanner.middleware.triptracker.TravelerPosition;
import org.opentripplanner.middleware.triptracker.TripStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

import static com.mongodb.client.model.Filters.eq;
import static org.opentripplanner.middleware.tripmonitor.jobs.NotificationType.ARRIVED_AND_MODE_CHANGE_NOTIFICATION;
import static org.opentripplanner.middleware.tripmonitor.jobs.NotificationType.ARRIVED_NOTIFICATION;
import static org.opentripplanner.middleware.tripmonitor.jobs.NotificationType.DEPARTED_NOTIFICATION;
import static org.opentripplanner.middleware.triptracker.TravelerLocator.hasRequiredTransitLeg;
import static org.opentripplanner.middleware.triptracker.TravelerLocator.hasRequiredTripStatus;
import static org.opentripplanner.middleware.triptracker.TravelerLocator.hasRequiredWalkLeg;
import static org.opentripplanner.middleware.triptracker.TravelerLocator.isApproachingEndOfLeg;
import static org.opentripplanner.middleware.triptracker.TravelerLocator.isAtStartOfLeg;

public class LegTransitionNotification {
private static final Logger LOG = LoggerFactory.getLogger(LegTransitionNotification.class);

public String travelerName;
public NotificationType notificationType;
Expand Down Expand Up @@ -87,4 +101,50 @@ public static Set<OtpUser> getLegTransitionNotifyUsers(MonitoredTrip trip) {

return notifyUsers;
}

/**
* If a traveler is on schedule and on either a walk or transit leg check for possible leg transition notification.
*/
public static void checkForLegTransition(TripStatus tripStatus, TravelerPosition travelerPosition, MonitoredTrip trip) {
if (
hasRequiredTripStatus(tripStatus) &&
(hasRequiredWalkLeg(travelerPosition) || hasRequiredTransitLeg(travelerPosition))
) {
NotificationType notificationType = getLegTransitionNotificationType(travelerPosition);
if (notificationType != null) {
try {
new CheckMonitoredTrip(trip).processLegTransition(notificationType, travelerPosition);
} catch (CloneNotSupportedException e) {
LOG.error("Error encountered while checking leg transition.", e);
}
}
}
}

/**
* Depending on the traveler's proximity to the start/end of a leg return the appropriate notification type.
*/
private static NotificationType getLegTransitionNotificationType(TravelerPosition travelerPosition) {
if (isAtStartOfLeg(travelerPosition)) {
return DEPARTED_NOTIFICATION;
} else if (isApproachingEndOfLeg(travelerPosition)) {
if (hasModeChanged(travelerPosition)) {
return ARRIVED_AND_MODE_CHANGE_NOTIFICATION;
}
return ARRIVED_NOTIFICATION;
}
return null;
}

/**
* The traveler is at the end of the current leg and the mode has changed between this and the next leg.
*/
private static boolean hasModeChanged(TravelerPosition travelerPosition) {
Leg nextLeg = travelerPosition.nextLeg;
Leg expectedLeg = travelerPosition.expectedLeg;
return
isApproachingEndOfLeg(travelerPosition) &&
nextLeg != null &&
!nextLeg.mode.equalsIgnoreCase(expectedLeg.mode);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.opentripplanner.middleware.triptracker;

import org.eclipse.jetty.http.HttpStatus;
import org.opentripplanner.middleware.models.LegTransitionNotification;
import org.opentripplanner.middleware.models.TrackedJourney;
import org.opentripplanner.middleware.otp.response.Leg;
import org.opentripplanner.middleware.persistence.Persistence;
Expand Down Expand Up @@ -80,7 +81,7 @@ private static TrackingResponse doUpdateTracking(Request request, TripTrackingDa
);
}

TravelerLocator.checkForLegTransition(tripStatus, travelerPosition, tripData.trip);
LegTransitionNotification.checkForLegTransition(tripStatus, travelerPosition, tripData.trip);

// Provide response.
TripInstruction instruction = TravelerLocator.getInstruction(tripStatus, travelerPosition, create);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package org.opentripplanner.middleware.triptracker;

import io.leonard.PolylineUtils;
import org.opentripplanner.middleware.models.MonitoredTrip;
import org.opentripplanner.middleware.otp.response.Leg;
import org.opentripplanner.middleware.otp.response.Place;
import org.opentripplanner.middleware.otp.response.Step;
import org.opentripplanner.middleware.tripmonitor.jobs.CheckMonitoredTrip;
import org.opentripplanner.middleware.tripmonitor.jobs.NotificationType;
import org.opentripplanner.middleware.triptracker.instruction.ContinueInstruction;
import org.opentripplanner.middleware.triptracker.instruction.DeviatedInstruction;
import org.opentripplanner.middleware.triptracker.instruction.GetOffHereTransitInstruction;
Expand All @@ -20,8 +17,6 @@
import org.opentripplanner.middleware.utils.Coordinates;
import org.opentripplanner.middleware.utils.ConvertsToCoordinates;
import org.opentripplanner.middleware.utils.DateTimeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;
import java.time.Duration;
Expand All @@ -35,9 +30,6 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static org.opentripplanner.middleware.tripmonitor.jobs.NotificationType.ARRIVED_NOTIFICATION;
import static org.opentripplanner.middleware.tripmonitor.jobs.NotificationType.ARRIVED_AND_MODE_CHANGE_NOTIFICATION;
import static org.opentripplanner.middleware.tripmonitor.jobs.NotificationType.DEPARTED_NOTIFICATION;
import static org.opentripplanner.middleware.triptracker.instruction.TripInstruction.TRIP_INSTRUCTION_IMMEDIATE_RADIUS;
import static org.opentripplanner.middleware.triptracker.instruction.TripInstruction.TRIP_INSTRUCTION_UPCOMING_RADIUS;
import static org.opentripplanner.middleware.utils.GeometryUtils.getDistance;
Expand All @@ -50,8 +42,6 @@
*/
public class TravelerLocator {

private static final Logger LOG = LoggerFactory.getLogger(TravelerLocator.class);

public static final int ACCEPTABLE_AHEAD_OF_SCHEDULE_IN_MINUTES = 15;

private static final int MIN_TRANSIT_VEHICLE_SPEED = 5; // meters per second. 11.1 mph or 18 km/h.
Expand Down Expand Up @@ -92,56 +82,10 @@ public static TripInstruction getInstruction(
return null;
}

/**
* If a traveler is on schedule and on either a walk or transit leg check for possible leg transition notification.
*/
public static void checkForLegTransition(TripStatus tripStatus, TravelerPosition travelerPosition, MonitoredTrip trip) {
if (
hasRequiredTripStatus(tripStatus) &&
(hasRequiredWalkLeg(travelerPosition) || hasRequiredTransitLeg(travelerPosition))
) {
NotificationType notificationType = getLegTransitionNotificationType(travelerPosition);
if (notificationType != null) {
try {
new CheckMonitoredTrip(trip).processLegTransition(notificationType, travelerPosition);
} catch (CloneNotSupportedException e) {
LOG.error("Error encountered while checking leg transition.", e);
}
}
}
}

/**
* Depending on the traveler's proximity to the start/end of a leg return the appropriate notification type.
*/
private static NotificationType getLegTransitionNotificationType(TravelerPosition travelerPosition) {
if (isAtStartOfLeg(travelerPosition)) {
return DEPARTED_NOTIFICATION;
} else if (isApproachingEndOfLeg(travelerPosition)) {
if (hasModeChanged(travelerPosition)) {
return ARRIVED_AND_MODE_CHANGE_NOTIFICATION;
}
return ARRIVED_NOTIFICATION;
}
return null;
}

/**
* The traveler is at the end of the current leg and the mode has changed between this and the next leg.
*/
private static boolean hasModeChanged(TravelerPosition travelerPosition) {
Leg nextLeg = travelerPosition.nextLeg;
Leg expectedLeg = travelerPosition.expectedLeg;
return
isApproachingEndOfLeg(travelerPosition) &&
nextLeg != null &&
!nextLeg.mode.equalsIgnoreCase(expectedLeg.mode);
}

/**
* Has required walk leg.
*/
private static boolean hasRequiredWalkLeg(TravelerPosition travelerPosition) {
public static boolean hasRequiredWalkLeg(TravelerPosition travelerPosition) {
return
travelerPosition.expectedLeg != null &&
travelerPosition.expectedLeg.mode.equalsIgnoreCase("walk");
Expand All @@ -150,7 +94,7 @@ private static boolean hasRequiredWalkLeg(TravelerPosition travelerPosition) {
/**
* Has required transit leg.
*/
private static boolean hasRequiredTransitLeg(TravelerPosition travelerPosition) {
public static boolean hasRequiredTransitLeg(TravelerPosition travelerPosition) {
return
travelerPosition.expectedLeg != null &&
travelerPosition.expectedLeg.transitLeg;
Expand All @@ -159,7 +103,7 @@ private static boolean hasRequiredTransitLeg(TravelerPosition travelerPosition)
/**
* The trip instruction can only be provided if the traveler is close to the indicated route.
*/
private static boolean hasRequiredTripStatus(TripStatus tripStatus) {
public static boolean hasRequiredTripStatus(TripStatus tripStatus) {
return !tripStatus.equals(TripStatus.DEVIATED) && !tripStatus.equals(TripStatus.ENDED);
}

Expand Down Expand Up @@ -378,7 +322,7 @@ private static boolean isPositionPastStep(TravelerPosition travelerPosition, Con
/**
* Is the traveler approaching the leg destination.
*/
private static boolean isApproachingEndOfLeg(TravelerPosition travelerPosition) {
public static boolean isApproachingEndOfLeg(TravelerPosition travelerPosition) {
return getDistanceToEndOfLeg(travelerPosition) <= TRIP_INSTRUCTION_UPCOMING_RADIUS;
}

Expand Down

0 comments on commit bc20b84

Please sign in to comment.