Search in sources :

Example 6 with TripDescriptor

use of com.google.transit.realtime.GtfsRealtime.TripDescriptor in project OpenTripPlanner by opentripplanner.

the class AlertsUpdateHandler method handleAlert.

private void handleAlert(String id, GtfsRealtime.Alert alert) {
    Alert alertText = new Alert();
    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) {
        long bestStartTime = Long.MAX_VALUE;
        long lastEndTime = Long.MIN_VALUE;
        for (TimeRange activePeriod : alert.getActivePeriodList()) {
            final long realStart = activePeriod.hasStart() ? activePeriod.getStart() : 0;
            final long start = activePeriod.hasStart() ? realStart - earlyStart : 0;
            if (realStart > 0 && realStart < bestStartTime) {
                bestStartTime = realStart;
            }
            final long end = activePeriod.hasEnd() ? activePeriod.getEnd() : Long.MAX_VALUE;
            if (end > lastEndTime) {
                lastEndTime = end;
            }
            periods.add(new TimePeriod(start, end));
        }
        if (bestStartTime != Long.MAX_VALUE) {
            alertText.effectiveStartDate = new Date(bestStartTime * 1000);
        }
        if (lastEndTime != Long.MIN_VALUE) {
            alertText.effectiveEndDate = new Date(lastEndTime * 1000);
        }
    } 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));
    }
    for (EntitySelector informed : alert.getInformedEntityList()) {
        if (fuzzyTripMatcher != null && informed.hasTrip()) {
            TripDescriptor trip = fuzzyTripMatcher.match(feedId, informed.getTrip());
            informed = informed.toBuilder().setTrip(trip).build();
        }
        String patchId = createId(id, informed);
        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();
        }
        AlertPatch patch = new AlertPatch();
        patch.setFeedId(feedId);
        if (routeId != null) {
            patch.setRoute(new AgencyAndId(feedId, routeId));
            // Makes no sense to set direction if we don't have a route
            if (direction != -1) {
                patch.setDirectionId(direction);
            }
        }
        if (tripId != null) {
            patch.setTrip(new AgencyAndId(feedId, tripId));
        }
        if (stopId != null) {
            patch.setStop(new AgencyAndId(feedId, stopId));
        }
        if (agencyId != null && routeId == null && tripId == null && stopId == null) {
            patch.setAgencyId(agencyId);
        }
        patch.setTimePeriods(periods);
        patch.setAlert(alertText);
        patch.setId(patchId);
        patchIds.add(patchId);
        alertPatchService.apply(patch);
    }
}
Also used : EntitySelector(com.google.transit.realtime.GtfsRealtime.EntitySelector) AlertPatch(org.opentripplanner.routing.alertpatch.AlertPatch) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) TimePeriod(org.opentripplanner.routing.alertpatch.TimePeriod) I18NString(org.opentripplanner.util.I18NString) TranslatedString(org.opentripplanner.util.TranslatedString) TimeRange(com.google.transit.realtime.GtfsRealtime.TimeRange) TripDescriptor(com.google.transit.realtime.GtfsRealtime.TripDescriptor) Alert(org.opentripplanner.routing.alertpatch.Alert)

Example 7 with TripDescriptor

use of com.google.transit.realtime.GtfsRealtime.TripDescriptor in project OpenTripPlanner by opentripplanner.

the class GtfsRealtimeFuzzyTripMatcherTest method testMatch.

public void testMatch() throws Exception {
    String feedId = graph.getFeedIds().iterator().next();
    GtfsRealtimeFuzzyTripMatcher matcher = new GtfsRealtimeFuzzyTripMatcher(graph.index);
    TripDescriptor trip1 = TripDescriptor.newBuilder().setRouteId("1").setDirectionId(0).setStartTime("06:47:00").setStartDate("20090915").build();
    assertEquals("10W1020", matcher.match(feedId, trip1).getTripId());
    trip1 = TripDescriptor.newBuilder().setRouteId("4").setDirectionId(0).setStartTime("00:02:00").setStartDate("20090915").build();
    assertEquals("40W1890", matcher.match(feedId, trip1).getTripId());
    // Test matching with "real time", when schedule uses time grater than 24:00
    trip1 = TripDescriptor.newBuilder().setRouteId("4").setDirectionId(0).setStartTime("12:00:00").setStartDate("20090915").build();
    // No departure at this time
    assertFalse(trip1.hasTripId());
    trip1 = TripDescriptor.newBuilder().setRouteId("1").setStartTime("06:47:00").setStartDate("20090915").build();
    // Missing direction id
    assertFalse(trip1.hasTripId());
}
Also used : TripDescriptor(com.google.transit.realtime.GtfsRealtime.TripDescriptor)

Example 8 with TripDescriptor

use of com.google.transit.realtime.GtfsRealtime.TripDescriptor in project OpenTripPlanner by opentripplanner.

the class TimetableSnapshotSource method validateAndHandleAddedTrip.

/**
 * Validate and handle GTFS-RT TripUpdate message containing an ADDED trip.
 *
 * @param graph graph to update
 * @param tripUpdate GTFS-RT TripUpdate message
 * @param feedId
 * @param serviceDate
 * @return true iff successful
 */
private boolean validateAndHandleAddedTrip(final Graph graph, final TripUpdate tripUpdate, final String feedId, final ServiceDate serviceDate) {
    // Preconditions
    Preconditions.checkNotNull(graph);
    Preconditions.checkNotNull(tripUpdate);
    Preconditions.checkNotNull(serviceDate);
    // 
    // Validate added trip
    // 
    // Check whether trip id of ADDED trip is available
    final TripDescriptor tripDescriptor = tripUpdate.getTrip();
    if (!tripDescriptor.hasTripId()) {
        LOG.warn("No trip id found for ADDED trip, skipping.");
        return false;
    }
    // Check whether trip id already exists in graph
    final String tripId = tripDescriptor.getTripId();
    final Trip trip = getTripForTripId(feedId, tripId);
    if (trip != null) {
        // TODO: should we support this and add a new instantiation of this trip (making it
        // frequency based)?
        LOG.warn("Graph already contains trip id of ADDED trip, skipping.");
        return false;
    }
    // Check whether a start date exists
    if (!tripDescriptor.hasStartDate()) {
        // TODO: should we support this and apply update to all days?
        LOG.warn("ADDED trip doesn't have a start date in TripDescriptor, skipping.");
        return false;
    }
    // Check whether at least two stop updates exist
    if (tripUpdate.getStopTimeUpdateCount() < 2) {
        LOG.warn("ADDED trip has less then two stops, skipping.");
        return false;
    }
    // Check whether all stop times are available and all stops exist
    final List<Stop> stops = checkNewStopTimeUpdatesAndFindStops(feedId, tripUpdate);
    if (stops == null) {
        return false;
    }
    // 
    // Handle added trip
    // 
    final boolean success = handleAddedTrip(graph, tripUpdate, stops, feedId, serviceDate);
    return success;
}
Also used : Trip(org.onebusaway.gtfs.model.Trip) TripDescriptor(com.google.transit.realtime.GtfsRealtime.TripDescriptor) Stop(org.onebusaway.gtfs.model.Stop)

Example 9 with TripDescriptor

use of com.google.transit.realtime.GtfsRealtime.TripDescriptor in project OpenTripPlanner by opentripplanner.

the class TimetableSnapshotSource method handleScheduledTrip.

private boolean handleScheduledTrip(final TripUpdate tripUpdate, final String feedId, final ServiceDate serviceDate) {
    final TripDescriptor tripDescriptor = tripUpdate.getTrip();
    // This does not include Agency ID or feed ID, trips are feed-unique and we currently assume a single static feed.
    final String tripId = tripDescriptor.getTripId();
    final TripPattern pattern = getPatternForTripId(feedId, tripId);
    if (pattern == null) {
        LOG.warn("No pattern found for tripId {}, skipping TripUpdate.", tripId);
        return false;
    }
    if (tripUpdate.getStopTimeUpdateCount() < 1) {
        LOG.warn("TripUpdate contains no updates, skipping.");
        return false;
    }
    // Apply update on the *scheduled* time table and set the updated trip times in the buffer
    final TripTimes updatedTripTimes = pattern.scheduledTimetable.createUpdatedTripTimes(tripUpdate, timeZone, serviceDate);
    if (updatedTripTimes == null) {
        return false;
    }
    // Make sure that updated trip times have the correct real time state
    updatedTripTimes.setRealTimeState(RealTimeState.UPDATED);
    final boolean success = buffer.update(feedId, pattern, updatedTripTimes, serviceDate);
    return success;
}
Also used : TripDescriptor(com.google.transit.realtime.GtfsRealtime.TripDescriptor) TripTimes(org.opentripplanner.routing.trippattern.TripTimes) TripPattern(org.opentripplanner.routing.edgetype.TripPattern)

Example 10 with TripDescriptor

use of com.google.transit.realtime.GtfsRealtime.TripDescriptor in project onebusaway-application-modules by camsys.

the class GtfsRealtimeAlertLibraryTest method testGetAlertAsServiceAlert.

@Test
public void testGetAlertAsServiceAlert() {
    AgencyAndId alertId = new AgencyAndId("1", "A1");
    Alert.Builder alert = Alert.newBuilder();
    GtfsRealtime.TimeRange.Builder timeRange = GtfsRealtime.TimeRange.newBuilder();
    timeRange.setStart(1L);
    timeRange.setStart(2L);
    alert.addActivePeriod(timeRange);
    EntitySelector.Builder entitySelector = EntitySelector.newBuilder();
    entitySelector.setAgencyId("agencyId");
    entitySelector.setRouteId("routeId");
    entitySelector.setStopId("stopId");
    TripDescriptor.Builder tripDescriptor = TripDescriptor.newBuilder();
    tripDescriptor.setTripId("tripId");
    entitySelector.setTrip(tripDescriptor);
    alert.addInformedEntity(entitySelector);
    alert.setCause(Alert.Cause.ACCIDENT);
    alert.setEffect(Alert.Effect.DETOUR);
    TranslatedString.Builder headerTexts = TranslatedString.newBuilder();
    Translation.Builder headerText = Translation.newBuilder();
    headerText.setLanguage("en");
    headerText.setText("Summary");
    headerTexts.addTranslation(headerText);
    alert.setHeaderText(headerTexts);
    TranslatedString.Builder descriptionTexts = TranslatedString.newBuilder();
    Translation.Builder descriptionText = Translation.newBuilder();
    descriptionText.setLanguage("fr");
    descriptionText.setText("Description");
    descriptionTexts.addTranslation(descriptionText);
    alert.setDescriptionText(descriptionTexts);
    TranslatedString.Builder urls = TranslatedString.newBuilder();
    Translation.Builder url = Translation.newBuilder();
    url.setLanguage("es");
    url.setText("http://something/");
    urls.addTranslation(url);
    alert.setUrl(urls);
    Mockito.when(_entitySource.getRouteId("routeId")).thenReturn(ServiceAlertLibrary.id("1", "routeId"));
    Mockito.when(_entitySource.getStopId("stopId")).thenReturn(ServiceAlertLibrary.id("2", "stopId"));
    Mockito.when(_entitySource.getTripId("tripId")).thenReturn(ServiceAlertLibrary.id("3", "tripId"));
    ServiceAlert.Builder serviceAlert = _library.getAlertAsServiceAlert(alertId, alert.build());
    Id id = serviceAlert.getId();
    assertEquals("1", id.getAgencyId());
    assertEquals("A1", id.getId());
    assertEquals(1, serviceAlert.getAffectsCount());
    Affects affects = serviceAlert.getAffects(0);
    assertEquals("agencyId", affects.getAgencyId());
    assertEquals("1", affects.getRouteId().getAgencyId());
    assertEquals("routeId", affects.getRouteId().getId());
    assertEquals("2", affects.getStopId().getAgencyId());
    assertEquals("stopId", affects.getStopId().getId());
    assertEquals("3", affects.getTripId().getAgencyId());
    assertEquals("tripId", affects.getTripId().getId());
    assertEquals(ServiceAlert.Cause.ACCIDENT, serviceAlert.getCause());
    assertEquals(1, serviceAlert.getConsequenceCount());
    Consequence consequence = serviceAlert.getConsequence(0);
    assertEquals(Effect.DETOUR, consequence.getEffect());
    ServiceAlerts.TranslatedString summaries = serviceAlert.getSummary();
    assertEquals(1, summaries.getTranslationCount());
    ServiceAlerts.TranslatedString.Translation summary = summaries.getTranslation(0);
    assertEquals("en", summary.getLanguage());
    assertEquals("Summary", summary.getText());
    ServiceAlerts.TranslatedString descriptions = serviceAlert.getDescription();
    assertEquals(1, descriptions.getTranslationCount());
    ServiceAlerts.TranslatedString.Translation description = descriptions.getTranslation(0);
    assertEquals("fr", description.getLanguage());
    assertEquals("Description", description.getText());
    ServiceAlerts.TranslatedString alertUrls = serviceAlert.getUrl();
    assertEquals(1, alertUrls.getTranslationCount());
    ServiceAlerts.TranslatedString.Translation alertUrl = alertUrls.getTranslation(0);
    assertEquals("es", alertUrl.getLanguage());
    assertEquals("http://something/", alertUrl.getText());
}
Also used : TranslatedString(com.google.transit.realtime.GtfsRealtime.TranslatedString) EntitySelector(com.google.transit.realtime.GtfsRealtime.EntitySelector) Translation(com.google.transit.realtime.GtfsRealtime.TranslatedString.Translation) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ServiceAlerts(org.onebusaway.transit_data_federation.services.service_alerts.ServiceAlerts) Consequence(org.onebusaway.transit_data_federation.services.service_alerts.ServiceAlerts.Consequence) Affects(org.onebusaway.transit_data_federation.services.service_alerts.ServiceAlerts.Affects) ServiceAlert(org.onebusaway.transit_data_federation.services.service_alerts.ServiceAlerts.ServiceAlert) TripDescriptor(com.google.transit.realtime.GtfsRealtime.TripDescriptor) ServiceAlert(org.onebusaway.transit_data_federation.services.service_alerts.ServiceAlerts.ServiceAlert) Alert(com.google.transit.realtime.GtfsRealtime.Alert) Id(org.onebusaway.transit_data_federation.services.service_alerts.ServiceAlerts.Id) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Test(org.junit.Test)

Aggregations

TripDescriptor (com.google.transit.realtime.GtfsRealtime.TripDescriptor)12 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)4 EntitySelector (com.google.transit.realtime.GtfsRealtime.EntitySelector)2 TripUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate)2 StopTimeUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate)2 VehicleDescriptor (com.google.transit.realtime.GtfsRealtime.VehicleDescriptor)2 Date (java.util.Date)2 Stop (org.onebusaway.gtfs.model.Stop)2 Trip (org.onebusaway.gtfs.model.Trip)2 Affects (org.onebusaway.transit_data_federation.services.service_alerts.ServiceAlerts.Affects)2 Id (org.onebusaway.transit_data_federation.services.service_alerts.ServiceAlerts.Id)2 TripTimes (org.opentripplanner.routing.trippattern.TripTimes)2 Alert (com.google.transit.realtime.GtfsRealtime.Alert)1 FeedEntity (com.google.transit.realtime.GtfsRealtime.FeedEntity)1 Position (com.google.transit.realtime.GtfsRealtime.Position)1 TimeRange (com.google.transit.realtime.GtfsRealtime.TimeRange)1 TranslatedString (com.google.transit.realtime.GtfsRealtime.TranslatedString)1 Translation (com.google.transit.realtime.GtfsRealtime.TranslatedString.Translation)1 StopTimeEvent (com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeEvent)1 VehiclePosition (com.google.transit.realtime.GtfsRealtime.VehiclePosition)1