Search in sources :

Example 11 with TransitAlert

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));
}
Also used : TransitAlert(org.opentripplanner.routing.alertpatch.TransitAlert) StopArrival(org.opentripplanner.model.plan.StopArrival) StopCondition(org.opentripplanner.routing.alertpatch.StopCondition) FeedScopedId(org.opentripplanner.model.FeedScopedId) Date(java.util.Date) Place(org.opentripplanner.model.plan.Place)

Example 12 with TransitAlert

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;
}
Also used : TransitAlert(org.opentripplanner.routing.alertpatch.TransitAlert) Stop(org.opentripplanner.model.Stop) ArrayList(java.util.ArrayList)

Example 13 with TransitAlert

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;
}
Also used : TransitAlert(org.opentripplanner.routing.alertpatch.TransitAlert) EntitySelector(org.opentripplanner.routing.alertpatch.EntitySelector) TimePeriod(org.opentripplanner.routing.alertpatch.TimePeriod) ArrayList(java.util.ArrayList) I18NString(org.opentripplanner.util.I18NString) TranslatedString(org.opentripplanner.util.TranslatedString) GtfsRealtime(com.google.transit.realtime.GtfsRealtime) TimeRange(com.google.transit.realtime.GtfsRealtime.TimeRange) TripDescriptor(com.google.transit.realtime.GtfsRealtime.TripDescriptor) FeedScopedId(org.opentripplanner.model.FeedScopedId)

Example 14 with TransitAlert

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);
}
Also used : TransitAlert(org.opentripplanner.routing.alertpatch.TransitAlert) ArrayList(java.util.ArrayList) I18NString(org.opentripplanner.util.I18NString) TranslatedString(org.opentripplanner.util.TranslatedString) FeedEntity(com.google.transit.realtime.GtfsRealtime.FeedEntity) GtfsRealtime(com.google.transit.realtime.GtfsRealtime)

Aggregations

TransitAlert (org.opentripplanner.routing.alertpatch.TransitAlert)14 ArrayList (java.util.ArrayList)7 Stop (org.opentripplanner.model.Stop)5 HashSet (java.util.HashSet)4 FeedScopedId (org.opentripplanner.model.FeedScopedId)4 TranslatedString (org.opentripplanner.util.TranslatedString)4 Date (java.util.Date)3 Route (org.opentripplanner.model.Route)3 EntitySelector (org.opentripplanner.routing.alertpatch.EntitySelector)3 I18NString (org.opentripplanner.util.I18NString)3 GtfsRealtime (com.google.transit.realtime.GtfsRealtime)2 Scalars (graphql.Scalars)2 Relay (graphql.relay.Relay)2 GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)2 GraphQLList (graphql.schema.GraphQLList)2 GraphQLNonNull (graphql.schema.GraphQLNonNull)2 GraphQLObjectType (graphql.schema.GraphQLObjectType)2 GraphQLOutputType (graphql.schema.GraphQLOutputType)2 GraphQLSchema (graphql.schema.GraphQLSchema)2 GraphQLTypeReference (graphql.schema.GraphQLTypeReference)2