use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance 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.blocks.BlockInstance in project onebusaway-application-modules by camsys.
the class BlockCalendarServiceImpl method findBlockTripsInRange.
private void findBlockTripsInRange(ServiceIntervalBlock intervals, Date serviceDate, Date timeFrom, Date timeTo, List<BlockTripEntry> trips, Collection<BlockInstance> instances) {
int scheduledTimeFrom = (int) ((timeFrom.getTime() - serviceDate.getTime()) / 1000);
int scheduledTimeTo = (int) ((timeTo.getTime() - serviceDate.getTime()) / 1000);
int indexFrom = index(Arrays.binarySearch(intervals.getMaxDepartures(), scheduledTimeFrom));
int indexTo = index(Arrays.binarySearch(intervals.getMinArrivals(), scheduledTimeTo));
InstanceState state = new InstanceState(serviceDate.getTime());
for (int in = indexFrom; in < indexTo; in++) {
BlockTripEntry trip = trips.get(in);
BlockConfigurationEntry block = trip.getBlockConfiguration();
BlockInstance instance = new BlockInstance(block, state);
instances.add(instance);
}
}
use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance 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.blocks.BlockInstance in project onebusaway-application-modules by camsys.
the class GtfsRealtimeTripLibrary method getTripDescriptorAsBlockDescriptor.
private BlockDescriptor getTripDescriptorAsBlockDescriptor(MonitoredResult result, TripDescriptor trip, long currentTime) {
if (!trip.hasTripId()) {
return null;
}
TripEntry tripEntry = _entitySource.getTrip(trip.getTripId());
if (tripEntry == null) {
if (result != null) {
_log.debug("reporting unmatched trip with id=" + trip.getTripId());
result.addUnmatchedTripId(trip.getTripId());
} else {
_log.warn("no trip found with id=" + trip.getTripId());
}
return null;
}
ServiceDate serviceDate = null;
BlockInstance instance;
BlockEntry block = tripEntry.getBlock();
if (trip.hasStartDate() && !"0".equals(trip.getStartDate())) {
try {
serviceDate = ServiceDate.parseString(trip.getStartDate());
} catch (ParseException ex) {
_log.warn("Could not parse service date " + trip.getStartDate(), ex);
}
}
if (serviceDate != null) {
instance = _blockCalendarService.getBlockInstance(block.getId(), serviceDate.getAsDate().getTime());
if (instance == null) {
_log.warn("block " + block.getId() + " does not exist on service date " + serviceDate);
return null;
}
} else {
long timeFrom = currentTime - 30 * 60 * 1000;
long timeTo = currentTime + 30 * 60 * 1000;
List<BlockInstance> instances = _blockCalendarService.getActiveBlocks(block.getId(), timeFrom, timeTo);
if (instances.isEmpty()) {
instances = _blockCalendarService.getClosestActiveBlocks(block.getId(), currentTime);
}
if (instances.isEmpty()) {
_log.warn("could not find any active instances for the specified block=" + block.getId() + " trip=" + trip);
return null;
}
instance = instances.get(0);
}
if (serviceDate == null) {
serviceDate = new ServiceDate(new Date(instance.getServiceDate()));
}
BlockDescriptor blockDescriptor = new BlockDescriptor();
blockDescriptor.setBlockInstance(instance);
blockDescriptor.setStartDate(serviceDate);
if (trip.hasScheduleRelationship()) {
blockDescriptor.setScheduleRelationshipValue(trip.getScheduleRelationship().toString());
}
int tripStartTime = 0;
int blockStartTime = 0;
if (trip.hasStartTime() && !"0".equals(trip.getStartTime())) {
try {
tripStartTime = StopTimeFieldMappingFactory.getStringAsSeconds(trip.getStartTime());
} catch (InvalidStopTimeException iste) {
_log.error("invalid stopTime of " + trip.getStartTime() + " for trip " + trip);
}
blockStartTime = getBlockStartTimeForTripStartTime(instance, tripEntry.getId(), tripStartTime);
blockDescriptor.setStartTime(blockStartTime);
}
return blockDescriptor;
}
use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.
the class GtfsRealtimeTripLibrary method applyTripUpdatesToRecord.
private void applyTripUpdatesToRecord(MonitoredResult result, BlockDescriptor blockDescriptor, List<TripUpdate> tripUpdates, VehicleLocationRecord record, String vehicleId, String bestTripId) {
BlockInstance instance = blockDescriptor.getBlockInstance();
BlockConfigurationEntry blockConfiguration = instance.getBlock();
List<BlockTripEntry> blockTrips = blockConfiguration.getTrips();
Map<String, List<TripUpdate>> tripUpdatesByTripId = MappingLibrary.mapToValueList(tripUpdates, "trip.tripId");
long t = currentTime();
int currentTime = (int) ((t - instance.getServiceDate()) / 1000);
BestScheduleDeviation best = new BestScheduleDeviation();
long lastStopScheduleTime = Long.MIN_VALUE;
boolean singleTimepointRecord = false;
List<TimepointPredictionRecord> timepointPredictions = new ArrayList<TimepointPredictionRecord>();
for (BlockTripEntry blockTrip : blockTrips) {
TripEntry trip = blockTrip.getTrip();
AgencyAndId tripId = trip.getId();
List<TripUpdate> updatesForTrip = tripUpdatesByTripId.get(tripId.getId());
boolean tripUpdateHasDelay = false;
// onBestTrip is only relevant if bestTripId is set, which indicates that the TripUpdates
// came from the vehicleId map (as opposed to block index).
boolean onBestTrip = bestTripId == null || tripId.getId().equals(bestTripId);
if (updatesForTrip != null) {
for (TripUpdate tripUpdate : updatesForTrip) {
/**
* TODO: delete this code once all upstream systems have been
* migrated the new "delay" and "timestamp" fields.
*/
if (tripUpdate.hasExtension(GtfsRealtimeOneBusAway.obaTripUpdate) && onBestTrip) {
OneBusAwayTripUpdate obaTripUpdate = tripUpdate.getExtension(GtfsRealtimeOneBusAway.obaTripUpdate);
if (obaTripUpdate.hasDelay()) {
/**
* TODO: Improved logic around picking the "best" schedule deviation
*/
int delay = obaTripUpdate.getDelay();
best.delta = 0;
best.isInPast = false;
best.scheduleDeviation = delay;
best.tripId = tripId;
tripUpdateHasDelay = true;
}
if (obaTripUpdate.hasTimestamp() && onBestTrip) {
best.timestamp = obaTripUpdate.getTimestamp() * 1000;
}
}
if (tripUpdate.hasDelay() && onBestTrip) {
/**
* TODO: Improved logic around picking the "best" schedule deviation
*/
best.delta = 0;
best.isInPast = false;
best.scheduleDeviation = tripUpdate.getDelay();
best.tripId = tripId;
tripUpdateHasDelay = true;
}
if (tripUpdate.hasTimestamp() && onBestTrip) {
best.timestamp = tripUpdate.getTimestamp() * 1000;
}
for (StopTimeUpdate stopTimeUpdate : tripUpdate.getStopTimeUpdateList()) {
BlockStopTimeEntry blockStopTime = getBlockStopTimeForStopTimeUpdate(result, tripUpdate, stopTimeUpdate, blockTrip.getStopTimes(), instance.getServiceDate());
// loop through and store last stop time on trip
List<BlockStopTimeEntry> stopTimes = blockTrip.getStopTimes();
for (BlockStopTimeEntry bste : stopTimes) {
long scheduleTime = instance.getServiceDate() + bste.getStopTime().getArrivalTime() * 1000;
if (scheduleTime > lastStopScheduleTime) {
lastStopScheduleTime = scheduleTime;
}
}
if (blockStopTime == null)
continue;
StopTimeEntry stopTime = blockStopTime.getStopTime();
TimepointPredictionRecord tpr = new TimepointPredictionRecord();
tpr.setTimepointId(stopTime.getStop().getId());
tpr.setTripId(stopTime.getTrip().getId());
tpr.setTimepointScheduledTime(instance.getServiceDate() + stopTime.getArrivalTime() * 1000);
if (stopTimeUpdate.hasStopSequence()) {
tpr.setStopSequence(stopTimeUpdate.getStopSequence());
}
int currentArrivalTime = computeArrivalTime(stopTime, stopTimeUpdate, instance.getServiceDate());
int currentDepartureTime = computeDepartureTime(stopTime, stopTimeUpdate, instance.getServiceDate());
if (currentArrivalTime >= 0) {
if (onBestTrip) {
updateBestScheduleDeviation(currentTime, stopTime.getArrivalTime(), currentArrivalTime, best, tripId, vehicleId);
}
long timepointPredictedTime = instance.getServiceDate() + (currentArrivalTime * 1000L);
tpr.setTimepointPredictedArrivalTime(timepointPredictedTime);
}
if (currentDepartureTime >= 0) {
if (onBestTrip) {
updateBestScheduleDeviation(currentTime, stopTime.getDepartureTime(), currentDepartureTime, best, tripId, vehicleId);
}
long timepointPredictedTime = instance.getServiceDate() + (currentDepartureTime * 1000L);
tpr.setTimepointPredictedDepartureTime(timepointPredictedTime);
}
if (tpr.getTimepointPredictedArrivalTime() != -1 || tpr.getTimepointPredictedDepartureTime() != -1) {
timepointPredictions.add(tpr);
}
}
}
}
if (timepointPredictions.size() == 1 && tripUpdates.get(0).getStopTimeUpdateList().size() == 1) {
singleTimepointRecord = true;
}
// tripUpdateHasDelay = true => best.scheduleDeviation is TripUpdate delay
if ((timepointPredictions.size() > 0 && tripUpdateHasDelay) || singleTimepointRecord) {
Set<AgencyAndId> records = new HashSet<AgencyAndId>();
for (TimepointPredictionRecord tpr : timepointPredictions) {
records.add(tpr.getTimepointId());
}
long tprStartTime = getEarliestTimeInRecords(timepointPredictions);
for (StopTimeEntry stopTime : trip.getStopTimes()) {
if (records.contains(stopTime.getStop().getId())) {
continue;
}
long predictionOffset = instance.getServiceDate() + (best.scheduleDeviation * 1000L);
long predictedDepartureTime = (stopTime.getDepartureTime() * 1000L) + predictionOffset;
long predictedArrivalTime = (stopTime.getArrivalTime() * 1000L) + predictionOffset;
long scheduledArrivalTime = instance.getServiceDate() + stopTime.getArrivalTime() * 1000;
long time = best.timestamp != 0 ? best.timestamp : currentTime();
/*
* if the timpepointrecord needs interpolated (one before, one after),
* OR
* we have a single Timepoint record and the arrival is
* in the future and before the last stop
*/
if ((predictedDepartureTime > time && predictedDepartureTime < tprStartTime) || (singleTimepointRecord && (predictedDepartureTime > time && scheduledArrivalTime <= lastStopScheduleTime))) {
TimepointPredictionRecord tpr = new TimepointPredictionRecord();
tpr.setTimepointId(stopTime.getStop().getId());
tpr.setTripId(stopTime.getTrip().getId());
tpr.setStopSequence(stopTime.getGtfsSequence());
tpr.setTimepointPredictedArrivalTime(predictedArrivalTime);
tpr.setTimepointPredictedDepartureTime(predictedDepartureTime);
tpr.setTimepointScheduledTime(scheduledArrivalTime);
timepointPredictions.add(tpr);
}
}
}
}
record.setServiceDate(instance.getServiceDate());
if (blockDescriptor.getStartTime() != null) {
record.setBlockStartTime(blockDescriptor.getStartTime());
}
record.setScheduleDeviation(best.scheduleDeviation);
if (best.timestamp != 0) {
record.setTimeOfRecord(best.timestamp);
}
record.setTimepointPredictions(timepointPredictions);
}
Aggregations