Search in sources :

Example 1 with BlockLocation

use of org.onebusaway.transit_data_federation.services.realtime.BlockLocation in project onebusaway-application-modules by camsys.

the class TripStatusBeanServiceImpl method getTripsForId.

@Override
public ListBean<TripDetailsBean> getTripsForId(TripDetailsQueryBean query) {
    AgencyAndId tripId = AgencyAndIdLibrary.convertFromString(query.getTripId());
    long serviceDate = query.getServiceDate();
    AgencyAndId vehicleId = AgencyAndIdLibrary.convertFromString(query.getVehicleId());
    long time = query.getTime();
    TripEntry tripEntry = _transitGraphDao.getTripEntryForId(tripId);
    if (tripEntry == null)
        return new ListBean<TripDetailsBean>();
    Map<BlockInstance, List<BlockLocation>> locationsByInstance = _blockStatusService.getBlocks(tripEntry.getBlock().getId(), serviceDate, vehicleId, time);
    List<TripDetailsBean> tripDetails = new ArrayList<TripDetailsBean>();
    for (Map.Entry<BlockInstance, List<BlockLocation>> entry : locationsByInstance.entrySet()) {
        BlockInstance blockInstance = entry.getKey();
        List<BlockLocation> locations = entry.getValue();
        BlockTripInstance blockTripInstance = BlockTripInstanceLibrary.getBlockTripInstance(blockInstance, tripId);
        if (blockTripInstance == null)
            throw new IllegalStateException("expected blockTrip for trip=" + tripEntry + " and block=" + blockInstance);
        /**
         * If we have no locations for the specified block instance, it means the
         * block is not currently active. But we can still attempt to construct a
         * trip details
         */
        if (locations.isEmpty()) {
            TripDetailsBean details = getTripEntryAndBlockLocationAsTripDetails(blockTripInstance, null, query.getInclusion(), time);
            tripDetails.add(details);
        } else {
            for (BlockLocation location : locations) {
                TripDetailsBean details = getBlockLocationAsTripDetails(blockTripInstance, location, query.getInclusion(), time);
                tripDetails.add(details);
            }
        }
    }
    return new ListBean<TripDetailsBean>(tripDetails, false);
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) BlockTripInstance(org.onebusaway.transit_data_federation.services.blocks.BlockTripInstance) ArrayList(java.util.ArrayList) ListBean(org.onebusaway.transit_data.model.ListBean) TripEntry(org.onebusaway.transit_data_federation.services.transit_graph.TripEntry) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) BlockLocation(org.onebusaway.transit_data_federation.services.realtime.BlockLocation) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) TripDetailsBean(org.onebusaway.transit_data.model.trips.TripDetailsBean)

Example 2 with BlockLocation

use of org.onebusaway.transit_data_federation.services.realtime.BlockLocation in project onebusaway-application-modules by camsys.

the class TripStatusBeanServiceImpl method getTripsForRoute.

@Override
public ListBean<TripDetailsBean> getTripsForRoute(TripsForRouteQueryBean query) {
    AgencyAndId routeId = AgencyAndIdLibrary.convertFromString(query.getRouteId());
    List<BlockLocation> locations = _blockStatusService.getBlocksForRoute(routeId, query.getTime());
    return getBlockLocationsAsTripDetails(locations, query.getInclusion(), query.getTime());
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) BlockLocation(org.onebusaway.transit_data_federation.services.realtime.BlockLocation)

Example 3 with BlockLocation

use of org.onebusaway.transit_data_federation.services.realtime.BlockLocation in project onebusaway-application-modules by camsys.

the class TripStatusBeanServiceImpl method getBlockLocationsAsTripDetails.

/**
 **
 * Private Methods
 ***
 */
private ListBean<TripDetailsBean> getBlockLocationsAsTripDetails(List<BlockLocation> locations, TripDetailsInclusionBean inclusion, long time) {
    List<TripDetailsBean> tripDetails = new ArrayList<TripDetailsBean>();
    for (BlockLocation location : locations) {
        TripDetailsBean details = getBlockLocationAsTripDetails(location.getActiveTripInstance(), location, inclusion, time);
        tripDetails.add(details);
    }
    return new ListBean<TripDetailsBean>(tripDetails, false);
}
Also used : ArrayList(java.util.ArrayList) ListBean(org.onebusaway.transit_data.model.ListBean) BlockLocation(org.onebusaway.transit_data_federation.services.realtime.BlockLocation) TripDetailsBean(org.onebusaway.transit_data.model.trips.TripDetailsBean)

Example 4 with BlockLocation

use of org.onebusaway.transit_data_federation.services.realtime.BlockLocation in project onebusaway-application-modules by camsys.

the class GtfsRealtimeServiceImpl method getTripUpdates.

@Override
public FeedMessage getTripUpdates() {
    FeedMessage.Builder feedMessage = createFeedWithDefaultHeader();
    List<BlockLocation> activeBlocks = _blockStatusService.getAllActiveBlocks(SystemTime.currentTimeMillis());
    for (BlockLocation activeBlock : activeBlocks) {
        // Only interested in blocks with real-time data
        if (!activeBlock.isPredicted())
            continue;
        // Only interested in blocks with a next stop
        BlockStopTimeEntry nextBlockStop = activeBlock.getNextStop();
        if (nextBlockStop == null)
            continue;
        // Only interested in blocks with a schedule deviation set
        if (!activeBlock.isScheduleDeviationSet())
            continue;
        TripUpdate.Builder tripUpdate = TripUpdate.newBuilder();
        BlockTripEntry activeBlockTrip = nextBlockStop.getTrip();
        TripEntry activeTrip = activeBlockTrip.getTrip();
        if (activeBlock.getTimepointPredictions() != null && activeBlock.getTimepointPredictions().size() > 0) {
            // If multiple stoptime predictions were originally obtained,
            // pass them through as received
            List<TimepointPredictionRecord> timepointPredictions = activeBlock.getTimepointPredictions();
            for (TimepointPredictionRecord tpr : timepointPredictions) {
                StopTimeUpdate.Builder stopTimeUpdate = StopTimeUpdate.newBuilder();
                stopTimeUpdate.setStopId(AgencyAndId.convertToString(tpr.getTimepointId()));
                stopTimeUpdate.setScheduleRelationship(com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate.ScheduleRelationship.SCHEDULED);
                if (tpr.getTimepointPredictedArrivalTime() != -1) {
                    StopTimeEvent.Builder arrivalStopTimeEvent = StopTimeEvent.newBuilder();
                    arrivalStopTimeEvent.setTime(tpr.getTimepointPredictedArrivalTime());
                    stopTimeUpdate.setArrival(arrivalStopTimeEvent);
                }
                if (tpr.getTimepointPredictedDepartureTime() != -1) {
                    StopTimeEvent.Builder departureStopTimeEvent = StopTimeEvent.newBuilder();
                    departureStopTimeEvent.setTime(tpr.getTimepointPredictedDepartureTime());
                    stopTimeUpdate.setDeparture(departureStopTimeEvent);
                }
                tripUpdate.addStopTimeUpdate(stopTimeUpdate);
            }
        } else {
            // No matter what our active trip is, we let our current trip be the the
            // trip of our next stop
            StopTimeEntry nextStopTime = nextBlockStop.getStopTime();
            StopEntry stop = nextStopTime.getStop();
            StopTimeUpdate.Builder stopTimeUpdate = StopTimeUpdate.newBuilder();
            stopTimeUpdate.setStopId(AgencyAndId.convertToString(stop.getId()));
            stopTimeUpdate.setStopSequence(nextStopTime.getSequence());
            stopTimeUpdate.setScheduleRelationship(com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate.ScheduleRelationship.SCHEDULED);
            StopTimeEvent.Builder stopTimeEvent = StopTimeEvent.newBuilder();
            stopTimeEvent.setDelay((int) activeBlock.getScheduleDeviation());
            stopTimeUpdate.setDeparture(stopTimeEvent);
            tripUpdate.addStopTimeUpdate(stopTimeUpdate);
        }
        AgencyAndId routeId = activeTrip.getRouteCollection().getId();
        AgencyAndId tripId = activeTrip.getId();
        BlockInstance blockInstance = activeBlock.getBlockInstance();
        String startDate = String.format("%1$ty%1$tm%1$td", new Date(blockInstance.getServiceDate()));
        TripDescriptor.Builder tripDescriptor = TripDescriptor.newBuilder();
        tripDescriptor.setRouteId(AgencyAndId.convertToString(routeId));
        tripDescriptor.setScheduleRelationship(ScheduleRelationship.SCHEDULED);
        tripDescriptor.setStartDate(startDate);
        tripDescriptor.setTripId(AgencyAndId.convertToString(tripId));
        tripUpdate.setTrip(tripDescriptor);
        AgencyAndId vehicleId = activeBlock.getVehicleId();
        VehicleDescriptor.Builder vehicleDescriptor = VehicleDescriptor.newBuilder();
        vehicleDescriptor.setId(AgencyAndId.convertToString(vehicleId));
        tripUpdate.setVehicle(vehicleDescriptor);
        FeedEntity.Builder feedEntity = FeedEntity.newBuilder();
        feedEntity.setTripUpdate(tripUpdate);
        feedEntity.setId(vehicleDescriptor.getId());
        feedMessage.addEntity(feedEntity);
    }
    return feedMessage.build();
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ServiceAlertLocalizedString(org.onebusaway.transit_data_federation.impl.service_alerts.ServiceAlertLocalizedString) BlockLocation(org.onebusaway.transit_data_federation.services.realtime.BlockLocation) StopTimeUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate) Date(java.util.Date) StopTimeEvent(com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeEvent) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) TimepointPredictionRecord(org.onebusaway.realtime.api.TimepointPredictionRecord)

Example 5 with BlockLocation

use of org.onebusaway.transit_data_federation.services.realtime.BlockLocation in project onebusaway-application-modules by camsys.

the class ArrivalsAndDeparturesBeanServiceImpl method applyBlockLocationToBean.

private void applyBlockLocationToBean(ArrivalAndDepartureInstance instance, ArrivalAndDepartureBean bean, long targetTime) {
    boolean hasFrequency = instance.getFrequency() != null;
    if (instance.isPredictedArrivalTimeSet()) {
        bean.setPredictedArrivalTime(instance.getPredictedArrivalTime());
        if (hasFrequency)
            bean.setScheduledArrivalTime(bean.getPredictedArrivalTime());
    }
    if (instance.isPredictedDepartureTimeSet()) {
        bean.setPredictedDepartureTime(instance.getPredictedDepartureTime());
        if (hasFrequency)
            bean.setScheduledDepartureTime(bean.getPredictedDepartureTime());
    }
    BlockStopTimeEntry stopTime = instance.getBlockStopTime();
    BlockLocation blockLocation = instance.getBlockLocation();
    if (blockLocation == null)
        return;
    bean.setPredicted(blockLocation.isPredicted());
    // Distance from stop
    if (blockLocation.isDistanceAlongBlockSet()) {
        double distanceFromStop = stopTime.getDistanceAlongBlock() - blockLocation.getDistanceAlongBlock();
        bean.setDistanceFromStop(distanceFromStop);
    } else {
        double distanceFromStop = stopTime.getDistanceAlongBlock() - blockLocation.getScheduledDistanceAlongBlock();
        bean.setDistanceFromStop(distanceFromStop);
    }
    // Number of stops away
    if (blockLocation.getNextStop() != null) {
        BlockStopTimeEntry nextStopTime = blockLocation.getNextStop();
        bean.setNumberOfStopsAway(stopTime.getBlockSequence() - nextStopTime.getBlockSequence());
    }
    if (blockLocation.getLastUpdateTime() > 0)
        bean.setLastUpdateTime(blockLocation.getLastUpdateTime());
    if (blockLocation.getVehicleId() != null)
        bean.setVehicleId(AgencyAndIdLibrary.convertToString(blockLocation.getVehicleId()));
    TripStatusBean tripStatusBean = _tripDetailsBeanService.getBlockLocationAsStatusBean(blockLocation, targetTime);
    bean.setTripStatus(tripStatusBean);
}
Also used : TripStatusBean(org.onebusaway.transit_data.model.trips.TripStatusBean) BlockLocation(org.onebusaway.transit_data_federation.services.realtime.BlockLocation) BlockStopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry)

Aggregations

BlockLocation (org.onebusaway.transit_data_federation.services.realtime.BlockLocation)33 BlockInstance (org.onebusaway.transit_data_federation.services.blocks.BlockInstance)19 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)15 ScheduledBlockLocation (org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation)15 ArrayList (java.util.ArrayList)14 TargetTime (org.onebusaway.transit_data_federation.model.TargetTime)12 Date (java.util.Date)11 ArrivalAndDepartureInstance (org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance)10 List (java.util.List)8 StopTimeInstance (org.onebusaway.transit_data_federation.model.StopTimeInstance)8 BlockConfigurationEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry)8 BlockStopTimeEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry)8 CoordinatePoint (org.onebusaway.geospatial.model.CoordinatePoint)7 BlockEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.BlockEntryImpl)7 VehicleLocationRecord (org.onebusaway.realtime.api.VehicleLocationRecord)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Record (org.onebusaway.transit_data.model.realtime.CurrentVehicleEstimateQueryBean.Record)5 ScheduledBlockLocationServiceImpl (org.onebusaway.transit_data_federation.impl.blocks.ScheduledBlockLocationServiceImpl)5 VehicleLocationRecordCacheImpl (org.onebusaway.transit_data_federation.impl.realtime.VehicleLocationRecordCacheImpl)5