Search in sources :

Example 6 with TripUpdate

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

the class GtfsRealtimeTripLibraryTest method testTprInterpolation_1.

/**
 * Same as above, but we should NOT create new timepoint prediction records
 * because the stop has already been served. Only thing different is current
 * time.
 *
 * Current time = 7:33. Trip update delay = 2 minutes
 *          Schedule time    Real-time from feed  Timepoint predicted departure time
 * Stop A   7:30             -----                ----
 * Stop B   7:40             7:43                 7:43
 */
@Test
public void testTprInterpolation_1() {
    _library.setCurrentTime(time(7, 33) * 1000);
    TripEntryImpl tripA = trip("tripA");
    stopTime(0, stop("stopA", 0, 0), tripA, time(7, 30), 0.0);
    stopTime(1, stop("stopB", 0, 0), tripA, time(7, 40), 10.0);
    BlockEntryImpl blockA = block("blockA");
    BlockConfigurationEntry blockConfigA = blockConfiguration(blockA, serviceIds("s1"), tripA);
    BlockInstance blockInstanceA = new BlockInstance(blockConfigA, 0L);
    StopTimeUpdate.Builder stopTimeUpdate = stopTimeUpdateWithDepartureDelay("stopB", 180);
    TripUpdate.Builder tripUpdate = tripUpdate("tripA", _library.getCurrentTime() / 1000, 120, stopTimeUpdate);
    Mockito.when(_entitySource.getTrip("tripA")).thenReturn(tripA);
    Mockito.when(_blockCalendarService.getActiveBlocks(Mockito.eq(blockA.getId()), Mockito.anyLong(), Mockito.anyLong())).thenReturn(Arrays.asList(blockInstanceA));
    VehicleLocationRecord record = vehicleLocationRecord(tripUpdate);
    long stopADept = getPredictedDepartureTimeByStopId(record, "stopA");
    // no tpr for this stop
    assertEquals(stopADept, -1);
    long stopBDept = getPredictedDepartureTimeByStopId(record, "stopB");
    assertEquals(stopBDept, time(7, 43) * 1000);
}
Also used : TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) StopTimeUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate) VehicleLocationRecord(org.onebusaway.realtime.api.VehicleLocationRecord) BlockEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.BlockEntryImpl) BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry) TripEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.TripEntryImpl) Test(org.junit.Test)

Example 7 with TripUpdate

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

the class GtfsRealtimeTripLibraryTest method testTprOnFutureTrip.

/**
 * This method tests that we propagate a time point prediction record
 * when it comes from a trip that hasn't started yet.
 *
 * Current time = 7:31. Trip update delay = 2 minutes
 *                  Schedule time    Real-time from feed
 * Stop A (trip A)  7:30             7:33
 * Stop A (trip B)  7:40             7:44
 */
@Test
public void testTprOnFutureTrip() {
    _library.setCurrentTime(time(7, 31) * 1000);
    TripEntryImpl tripA = trip("tripA");
    TripEntryImpl tripB = trip("tripB");
    StopEntryImpl stopA = stop("stopA", 0, 0);
    stopTime(0, stopA, tripA, time(7, 30), 0.0);
    stopTime(0, stopA, tripB, time(7, 40), 0.0);
    BlockEntryImpl blockA = block("blockA");
    BlockConfigurationEntry blockConfigA = blockConfiguration(blockA, serviceIds("s1"), tripA, tripB);
    BlockInstance blockInstanceA = new BlockInstance(blockConfigA, 0L);
    StopTimeUpdate.Builder stuA = stopTimeUpdateWithDepartureDelay("stopA", 180);
    TripUpdate.Builder tuA = tripUpdate("tripA", _library.getCurrentTime() / 1000, 120, stuA);
    StopTimeUpdate.Builder stuB = stopTimeUpdateWithDepartureDelay("stopA", 240);
    TripUpdate.Builder tuB = tripUpdate("tripB", _library.getCurrentTime() / 1000, 0, stuB);
    tuA.setVehicle(vehicle("bus1"));
    tuB.setVehicle(vehicle("bus1"));
    Mockito.when(_entitySource.getTrip("tripA")).thenReturn(tripA);
    Mockito.when(_entitySource.getTrip("tripB")).thenReturn(tripB);
    Mockito.when(_blockCalendarService.getActiveBlocks(Mockito.eq(blockA.getId()), Mockito.anyLong(), Mockito.anyLong())).thenReturn(Arrays.asList(blockInstanceA));
    VehicleLocationRecord record = vehicleLocationRecord(tuA, tuB);
    long tripADept = getPredictedDepartureTimeByStopIdAndTripId(record, "stopA", "tripA");
    assertEquals(tripADept, time(7, 33) * 1000);
    long tripBDept = getPredictedDepartureTimeByStopIdAndTripId(record, "stopA", "tripB");
    assertEquals(tripBDept, time(7, 44) * 1000);
}
Also used : TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) StopTimeUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate) VehicleLocationRecord(org.onebusaway.realtime.api.VehicleLocationRecord) BlockEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.BlockEntryImpl) BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry) TripEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.TripEntryImpl) StopEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.StopEntryImpl) Test(org.junit.Test)

Example 8 with TripUpdate

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

the class TripUpdatesForAgencyAction method fillFeedMessage.

@Override
protected void fillFeedMessage(FeedMessage.Builder feed, String agencyId, long timestamp) {
    ListBean<VehicleStatusBean> vehicles = _service.getAllVehiclesForAgency(agencyId, timestamp);
    for (VehicleStatusBean vehicle : vehicles.getList()) {
        TripStatusBean tripStatus = vehicle.getTripStatus();
        if (tripStatus == null) {
            continue;
        }
        TripBean activeTrip = tripStatus.getActiveTrip();
        RouteBean route = activeTrip.getRoute();
        FeedEntity.Builder entity = feed.addEntityBuilder();
        entity.setId(Integer.toString(feed.getEntityCount()));
        TripUpdate.Builder tripUpdate = entity.getTripUpdateBuilder();
        TripDescriptor.Builder tripDesc = tripUpdate.getTripBuilder();
        tripDesc.setTripId(normalizeId(activeTrip.getId()));
        tripDesc.setRouteId(normalizeId(route.getId()));
        VehicleDescriptor.Builder vehicleDesc = tripUpdate.getVehicleBuilder();
        vehicleDesc.setId(normalizeId(vehicle.getVehicleId()));
        if (tripStatus.getTimepointPredictions() != null && tripStatus.getTimepointPredictions().size() > 0) {
            for (TimepointPredictionBean timepointPrediction : tripStatus.getTimepointPredictions()) {
                AgencyAndId stopId = modifiedStopId(agencyId, timepointPrediction.getTimepointId());
                if (!stopId.getAgencyId().equals(agencyId))
                    continue;
                TripUpdate.StopTimeUpdate.Builder stopTimeUpdate = tripUpdate.addStopTimeUpdateBuilder();
                stopTimeUpdate.setStopId(normalizeId(stopId.toString()));
                TripUpdate.StopTimeEvent.Builder arrival = stopTimeUpdate.getArrivalBuilder();
                if (timepointPrediction.getTimepointPredictedArrivalTime() != -1) {
                    arrival.setTime(timepointPrediction.getTimepointPredictedArrivalTime() / 1000L);
                }
                TripUpdate.StopTimeEvent.Builder departure = stopTimeUpdate.getDepartureBuilder();
                if (timepointPrediction.getTimepointPredictedDepartureTime() != -1) {
                    departure.setTime(timepointPrediction.getTimepointPredictedDepartureTime() / 1000L);
                }
            }
            tripUpdate.setTimestamp(vehicle.getLastUpdateTime() / 1000);
        } else {
            StopBean nextStop = tripStatus.getNextStop();
            if (nextStop != null) {
                AgencyAndId stopId = modifiedStopId(agencyId, nextStop.getId());
                if (stopId.getAgencyId().equals(agencyId)) {
                    TripUpdate.StopTimeUpdate.Builder stopTimeUpdate = tripUpdate.addStopTimeUpdateBuilder();
                    stopTimeUpdate.setStopId(normalizeId(stopId.toString()));
                    TripUpdate.StopTimeEvent.Builder departure = stopTimeUpdate.getDepartureBuilder();
                    departure.setTime(timestamp / 1000 + tripStatus.getNextStopTimeOffset());
                }
            }
        }
        tripUpdate.setDelay((int) tripStatus.getScheduleDeviation());
        tripUpdate.setTimestamp(vehicle.getLastUpdateTime() / 1000);
    }
}
Also used : TimepointPredictionBean(org.onebusaway.transit_data.model.trips.TimepointPredictionBean) TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) TripBean(org.onebusaway.transit_data.model.trips.TripBean) VehicleStatusBean(org.onebusaway.transit_data.model.VehicleStatusBean) RouteBean(org.onebusaway.transit_data.model.RouteBean) TripDescriptor(com.google.transit.realtime.GtfsRealtime.TripDescriptor) StopBean(org.onebusaway.transit_data.model.StopBean) TripStatusBean(org.onebusaway.transit_data.model.trips.TripStatusBean) FeedEntity(com.google.transit.realtime.GtfsRealtime.FeedEntity) VehicleDescriptor(com.google.transit.realtime.GtfsRealtime.VehicleDescriptor)

Example 9 with TripUpdate

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

the class GtfsRealtimeRetrieverImpl method writeTripUpdate.

private TripUpdate writeTripUpdate(TripUpdateModel model) {
    TripUpdate.Builder tu = TripUpdate.newBuilder();
    TripDescriptor.Builder t = TripDescriptor.newBuilder();
    if (model.getDelay() != null) {
        tu.setDelay(model.getDelay());
    }
    if (model.getTripId() != null) {
        t.setTripId(parseId(model.getTripId()));
    }
    if (model.getRouteId() != null) {
        t.setRouteId(parseId(model.getRouteId()));
    }
    if (model.getTripStart() != null) {
        Date date = model.getTripStart();
        String startDate = new SimpleDateFormat(GTFS_RT_DATE_FORMAT).format(date);
        String startTime = new SimpleDateFormat(GTFS_RT_TIME_FORMAT).format(date);
        t.setStartDate(startDate);
        t.setStartTime(startTime);
    }
    TripDescriptor.ScheduleRelationship sr = TripDescriptor.ScheduleRelationship.valueOf(model.getScheduleRelationship());
    if (sr != null) {
        t.setScheduleRelationship(sr);
    }
    if (model.getVehicleId() != null) {
        VehicleDescriptor.Builder v = VehicleDescriptor.newBuilder();
        v.setId(model.getVehicleId());
        if (!StringUtils.isEmpty(model.getVehicleLabel())) {
            v.setLabel(model.getVehicleLabel());
        }
        if (!StringUtils.isEmpty(model.getVehicleLicensePlate())) {
            v.setLicensePlate(model.getVehicleLicensePlate());
        }
        tu.setVehicle(v.build());
    }
    for (StopTimeUpdateModel stum : model.getStopTimeUpdates()) {
        StopTimeUpdate stu = writeStopTimeUpdate(stum);
        if (stum != null) {
            tu.addStopTimeUpdate(stu);
        }
    }
    tu.setTrip(t);
    return tu.build();
}
Also used : TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) StopTimeUpdateModel(org.onebusaway.gtfs_realtime.model.StopTimeUpdateModel) TripDescriptor(com.google.transit.realtime.GtfsRealtime.TripDescriptor) StopTimeUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate) SimpleDateFormat(java.text.SimpleDateFormat) VehicleDescriptor(com.google.transit.realtime.GtfsRealtime.VehicleDescriptor) Date(java.util.Date)

Example 10 with TripUpdate

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

the class TimetableSnapshotSource method applyTripUpdates.

/**
 * Method to apply a trip update list to the most recent version of the timetable snapshot. A
 * GTFS-RT feed is always applied against a single static feed (indicated by feedId).
 *<<<<<<< HEAD
 *
 *=======
 *
 * However, multi-feed support is not completed and we currently assume there is only one static
 * feed when matching IDs.
 *
 *>>>>>>> 7296be8ffd532a13afb0bec263a9f436ab787022
 * @param graph graph to update (needed for adding/changing stop patterns)
 * @param fullDataset true iff the list with updates represent all updates that are active right
 *        now, i.e. all previous updates should be disregarded
 * @param updates GTFS-RT TripUpdate's that should be applied atomically
 * @param feedId
 */
public void applyTripUpdates(final Graph graph, final boolean fullDataset, final List<TripUpdate> updates, final String feedId) {
    if (updates == null) {
        LOG.warn("updates is null");
        return;
    }
    // Acquire lock on buffer
    bufferLock.lock();
    try {
        if (fullDataset) {
            // Remove all updates from the buffer
            buffer.clear(feedId);
        }
        LOG.debug("message contains {} trip updates", updates.size());
        int uIndex = 0;
        for (TripUpdate tripUpdate : updates) {
            if (fuzzyTripMatcher != null && tripUpdate.hasTrip()) {
                final TripDescriptor trip = fuzzyTripMatcher.match(feedId, tripUpdate.getTrip());
                tripUpdate = tripUpdate.toBuilder().setTrip(trip).build();
            }
            if (!tripUpdate.hasTrip()) {
                LOG.warn("Missing TripDescriptor in gtfs-rt trip update: \n{}", tripUpdate);
                continue;
            }
            ServiceDate serviceDate = new ServiceDate();
            final TripDescriptor tripDescriptor = tripUpdate.getTrip();
            if (tripDescriptor.hasStartDate()) {
                try {
                    serviceDate = ServiceDate.parseString(tripDescriptor.getStartDate());
                } catch (final ParseException e) {
                    LOG.warn("Failed to parse start date in gtfs-rt trip update: \n{}", tripUpdate);
                    continue;
                }
            } else {
            // TODO: figure out the correct service date. For the special case that a trip
            // starts for example at 40:00, yesterday would probably be a better guess.
            }
            uIndex += 1;
            LOG.debug("trip update #{} ({} updates) :", uIndex, tripUpdate.getStopTimeUpdateCount());
            LOG.trace("{}", tripUpdate);
            // Determine what kind of trip update this is
            boolean applied = false;
            final TripDescriptor.ScheduleRelationship tripScheduleRelationship = determineTripScheduleRelationship(tripUpdate);
            switch(tripScheduleRelationship) {
                case SCHEDULED:
                    applied = handleScheduledTrip(tripUpdate, feedId, serviceDate);
                    break;
                case ADDED:
                    applied = validateAndHandleAddedTrip(graph, tripUpdate, feedId, serviceDate);
                    break;
                case UNSCHEDULED:
                    applied = handleUnscheduledTrip(tripUpdate, feedId, serviceDate);
                    break;
                case CANCELED:
                    applied = handleCanceledTrip(tripUpdate, feedId, serviceDate);
                    break;
                case MODIFIED:
                    applied = validateAndHandleModifiedTrip(graph, tripUpdate, feedId, serviceDate);
                    break;
            }
            if (applied) {
                appliedBlockCount++;
            } else {
                LOG.warn("Failed to apply TripUpdate.");
                LOG.trace(" Contents: {}", tripUpdate);
            }
            if (appliedBlockCount % logFrequency == 0) {
                LOG.info("Applied {} trip updates.", appliedBlockCount);
            }
        }
        LOG.debug("end of update message");
        // Make sure that the public (locking) getTimetableSnapshot function is not called.
        if (purgeExpiredData) {
            final boolean modified = purgeExpiredData();
            getTimetableSnapshot(modified);
        } else {
            getTimetableSnapshot(false);
        }
    } finally {
        // Always release lock
        bufferLock.unlock();
    }
}
Also used : ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) TripDescriptor(com.google.transit.realtime.GtfsRealtime.TripDescriptor) ParseException(java.text.ParseException)

Aggregations

TripUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate)30 Test (org.junit.Test)20 StopTimeUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate)16 TripDescriptor (com.google.transit.realtime.GtfsRealtime.TripDescriptor)13 BlockInstance (org.onebusaway.transit_data_federation.services.blocks.BlockInstance)10 BlockConfigurationEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry)10 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)9 VehicleLocationRecord (org.onebusaway.realtime.api.VehicleLocationRecord)9 BlockEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.BlockEntryImpl)9 TripEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.TripEntryImpl)9 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)8 FeedEntity (com.google.transit.realtime.GtfsRealtime.FeedEntity)7 FeedMessage (com.google.transit.realtime.GtfsRealtime.FeedMessage)6 StopTimeEvent (com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeEvent)6 TimetableSnapshot (org.opentripplanner.routing.edgetype.TimetableSnapshot)6 TripPattern (org.opentripplanner.routing.edgetype.TripPattern)5 ArrayList (java.util.ArrayList)4 TimepointPredictionRecord (org.onebusaway.realtime.api.TimepointPredictionRecord)4 Timetable (org.opentripplanner.routing.edgetype.Timetable)4 TripTimes (org.opentripplanner.routing.trippattern.TripTimes)4