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);
}
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());
}
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);
}
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();
}
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);
}
Aggregations