use of org.opentripplanner.routing.alertpatch.TransitAlert in project OpenTripPlanner by opentripplanner.
the class AlertToLegMapper method addAlertPatchesToLeg.
public static void addAlertPatchesToLeg(Graph graph, Leg leg, boolean isFirstLeg, Locale requestedLocale) {
Set<StopCondition> departingStopConditions = isFirstLeg ? StopCondition.DEPARTURE : StopCondition.FIRST_DEPARTURE;
Date legStartTime = leg.startTime.getTime();
Date legEndTime = leg.endTime.getTime();
FeedScopedId fromStopId = leg.from == null ? null : leg.from.stopId;
FeedScopedId toStopId = leg.to == null ? null : leg.to.stopId;
if (leg.isTransitLeg()) {
FeedScopedId routeId = leg.getRoute().getId();
if (fromStopId != null) {
Collection<TransitAlert> alerts = getAlertsForStopAndRoute(graph, fromStopId, routeId);
addAlertPatchesToLeg(leg, departingStopConditions, alerts, requestedLocale, legStartTime, legEndTime);
}
if (toStopId != null) {
Collection<TransitAlert> alerts = getAlertsForStopAndRoute(graph, toStopId, routeId);
addAlertPatchesToLeg(leg, StopCondition.ARRIVING, alerts, requestedLocale, legStartTime, legEndTime);
}
FeedScopedId tripId = leg.getTrip().getId();
if (fromStopId != null) {
Collection<TransitAlert> alerts = getAlertsForStopAndTrip(graph, fromStopId, tripId);
addAlertPatchesToLeg(leg, departingStopConditions, alerts, requestedLocale, legStartTime, legEndTime);
}
if (toStopId != null) {
Collection<TransitAlert> alerts = getAlertsForStopAndTrip(graph, toStopId, tripId);
addAlertPatchesToLeg(leg, StopCondition.ARRIVING, alerts, requestedLocale, legStartTime, legEndTime);
}
if (leg.intermediateStops != null) {
for (StopArrival visit : leg.intermediateStops) {
Place place = visit.place;
if (place.stopId != null) {
Collection<TransitAlert> alerts = getAlertsForStopAndTrip(graph, place.stopId, tripId);
Date stopArrival = visit.arrival.getTime();
Date stopDepature = visit.departure.getTime();
addAlertPatchesToLeg(leg, StopCondition.PASSING, alerts, requestedLocale, stopArrival, stopDepature);
}
}
}
}
if (leg.intermediateStops != null) {
for (StopArrival visit : leg.intermediateStops) {
Place place = visit.place;
if (place.stopId != null) {
Collection<TransitAlert> alerts = getAlertsForStop(graph, place.stopId);
Date stopArrival = visit.arrival.getTime();
Date stopDepature = visit.departure.getTime();
addAlertPatchesToLeg(leg, StopCondition.PASSING, alerts, requestedLocale, stopArrival, stopDepature);
}
}
}
if (leg.from != null && fromStopId != null) {
Collection<TransitAlert> alerts = getAlertsForStop(graph, fromStopId);
addAlertPatchesToLeg(leg, departingStopConditions, alerts, requestedLocale, legStartTime, legEndTime);
}
if (leg.to != null && toStopId != null) {
Collection<TransitAlert> alerts = getAlertsForStop(graph, toStopId);
addAlertPatchesToLeg(leg, StopCondition.ARRIVING, alerts, requestedLocale, legStartTime, legEndTime);
}
if (leg.isTransitLeg()) {
Collection<TransitAlert> patches;
// trips
patches = alertPatchService(graph).getTripAlerts(leg.getTrip().getId());
addAlertPatchesToLeg(leg, patches, requestedLocale, legStartTime, legEndTime);
// route
patches = alertPatchService(graph).getRouteAlerts(leg.getRoute().getId());
addAlertPatchesToLeg(leg, patches, requestedLocale, legStartTime, legEndTime);
// agency
patches = alertPatchService(graph).getAgencyAlerts(leg.getAgency().getId());
addAlertPatchesToLeg(leg, patches, requestedLocale, legStartTime, legEndTime);
}
// Filter alerts when there are multiple timePeriods for each alert
leg.transitAlerts.removeIf(alertPatch -> !alertPatch.displayDuring(leg.startTime.getTimeInMillis() / 1000, leg.endTime.getTimeInMillis() / 1000));
}
use of org.opentripplanner.routing.alertpatch.TransitAlert in project OpenTripPlanner by opentripplanner.
the class AlertToLegMapper method getAlertsForStopAndRoute.
private static Collection<TransitAlert> getAlertsForStopAndRoute(Graph graph, FeedScopedId stopId, FeedScopedId routeId, boolean checkParentStop) {
Stop stop = graph.index.getStopForId(stopId);
if (stop == null) {
return new ArrayList<>();
}
Collection<TransitAlert> alertsForStopAndRoute = graph.getTransitAlertService().getStopAndRouteAlerts(stopId, routeId);
if (checkParentStop) {
if (alertsForStopAndRoute == null) {
alertsForStopAndRoute = new HashSet<>();
}
if (stop.isPartOfStation()) {
// Also check parent
Collection<TransitAlert> alerts = graph.getTransitAlertService().getStopAndRouteAlerts(stop.getParentStation().getId(), routeId);
if (alerts != null) {
alertsForStopAndRoute.addAll(alerts);
}
}
// TODO SIRI: Add support for fetching alerts attached to MultiModal-stops
// if (stop.getMultiModalStation() != null) {
// //Also check multimodal parent
//
// FeedScopedId multimodalStopId = new FeedScopedId(stopId.getAgencyId(), stop.getMultiModalStation());
// Collection<AlertPatch> multimodalStopAlerts = graph.index.getAlertsForStopAndRoute(multimodalStopId, routeId);
// if (multimodalStopAlerts != null) {
// alertsForStopAndRoute.addAll(multimodalStopAlerts);
// }
// }
}
return alertsForStopAndRoute;
}
use of org.opentripplanner.routing.alertpatch.TransitAlert in project OpenTripPlanner by opentripplanner.
the class AlertsUpdateHandler method mapAlert.
private TransitAlert mapAlert(String id, GtfsRealtime.Alert alert) {
TransitAlert alertText = new TransitAlert();
alertText.setId(id);
alertText.alertDescriptionText = deBuffer(alert.getDescriptionText());
alertText.alertHeaderText = deBuffer(alert.getHeaderText());
alertText.alertUrl = deBuffer(alert.getUrl());
ArrayList<TimePeriod> periods = new ArrayList<TimePeriod>();
if (alert.getActivePeriodCount() > 0) {
for (TimeRange activePeriod : alert.getActivePeriodList()) {
final long realStart = activePeriod.hasStart() ? activePeriod.getStart() : 0;
final long start = activePeriod.hasStart() ? realStart - earlyStart : 0;
final long end = activePeriod.hasEnd() ? activePeriod.getEnd() : Long.MAX_VALUE;
periods.add(new TimePeriod(start, end));
}
} else {
// Per the GTFS-rt spec, if an alert has no TimeRanges, than it should always be shown.
periods.add(new TimePeriod(0, Long.MAX_VALUE));
}
alertText.setTimePeriods(periods);
alertText.setFeedId(feedId);
for (GtfsRealtime.EntitySelector informed : alert.getInformedEntityList()) {
if (fuzzyTripMatcher != null && informed.hasTrip()) {
TripDescriptor trip = fuzzyTripMatcher.match(feedId, informed.getTrip());
informed = informed.toBuilder().setTrip(trip).build();
}
String routeId = null;
if (informed.hasRouteId()) {
routeId = informed.getRouteId();
}
int direction;
if (informed.hasTrip() && informed.getTrip().hasDirectionId()) {
direction = informed.getTrip().getDirectionId();
} else {
direction = -1;
}
// TODO: The other elements of a TripDescriptor are ignored...
String tripId = null;
if (informed.hasTrip() && informed.getTrip().hasTripId()) {
tripId = informed.getTrip().getTripId();
}
String stopId = null;
if (informed.hasStopId()) {
stopId = informed.getStopId();
}
String agencyId = informed.getAgencyId();
if (informed.hasAgencyId()) {
agencyId = informed.getAgencyId().intern();
}
if (tripId != null) {
if (stopId != null) {
alertText.addEntity(new EntitySelector.StopAndTrip(new FeedScopedId(feedId, stopId), new FeedScopedId(feedId, tripId)));
} else {
alertText.addEntity(new EntitySelector.Trip(new FeedScopedId(feedId, tripId)));
}
} else if (routeId != null) {
// TODO: Handle direction
if (stopId != null) {
alertText.addEntity(new EntitySelector.StopAndRoute(new FeedScopedId(feedId, stopId), new FeedScopedId(feedId, routeId)));
} else {
alertText.addEntity(new EntitySelector.Route(new FeedScopedId(feedId, routeId)));
}
} else if (stopId != null) {
alertText.addEntity(new EntitySelector.Stop(new FeedScopedId(feedId, stopId)));
} else if (agencyId != null) {
alertText.addEntity(new EntitySelector.Agency(new FeedScopedId(feedId, agencyId)));
}
}
return alertText;
}
use of org.opentripplanner.routing.alertpatch.TransitAlert in project OpenTripPlanner by opentripplanner.
the class AlertsUpdateHandler method update.
public void update(FeedMessage message) {
Collection<TransitAlert> alerts = new ArrayList<>();
for (FeedEntity entity : message.getEntityList()) {
if (!entity.hasAlert()) {
continue;
}
GtfsRealtime.Alert alert = entity.getAlert();
String id = entity.getId();
alerts.add(mapAlert(id, alert));
}
transitAlertService.setAlerts(alerts);
}
Aggregations