use of com.google.transit.realtime.GtfsRealtime.TripUpdate 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);
}
use of com.google.transit.realtime.GtfsRealtime.TripUpdate in project onebusaway-application-modules by camsys.
the class TripUpdatesForAgencyActionTest method test.
@Test
public void test() {
long now = System.currentTimeMillis();
List<VehicleStatusBean> vehicles = new ArrayList<VehicleStatusBean>();
RouteBean.Builder routeBuilder = RouteBean.builder();
routeBuilder.setId("1_r1");
RouteBean route = routeBuilder.create();
{
VehicleStatusBean vehicle = new VehicleStatusBean();
vehicles.add(vehicle);
vehicle.setLastUpdateTime(1234 * 1000);
vehicle.setVehicleId("1_v1");
TripStatusBean tripStatus = new TripStatusBean();
vehicle.setTripStatus(tripStatus);
tripStatus.setScheduleDeviation(2 * 60);
TripBean trip = new TripBean();
trip.setId("1_t0");
trip.setRoute(route);
tripStatus.setActiveTrip(trip);
StopBean stop = new StopBean();
stop.setId("1_s2");
tripStatus.setNextStop(stop);
tripStatus.setNextStopTimeOffset(5 * 60);
}
{
VehicleStatusBean vehicle = new VehicleStatusBean();
vehicles.add(vehicle);
vehicle.setLastUpdateTime(5678 * 1000);
vehicle.setVehicleId("1_v2");
TripStatusBean tripStatus = new TripStatusBean();
vehicle.setTripStatus(tripStatus);
tripStatus.setScheduleDeviation(3 * 60);
TripBean trip = new TripBean();
trip.setId("1_t1");
trip.setRoute(route);
tripStatus.setActiveTrip(trip);
StopBean stop = new StopBean();
stop.setId("1_s3");
tripStatus.setNextStop(stop);
tripStatus.setNextStopTimeOffset(10 * 60);
}
ListBean<VehicleStatusBean> bean = new ListBean<VehicleStatusBean>();
bean.setList(vehicles);
Mockito.when(_service.getAllVehiclesForAgency("1", now)).thenReturn(bean);
_action.setId("1");
_action.setTime(new Date(now));
_action.show();
ResponseBean model = _action.getModel();
FeedMessage feed = (FeedMessage) model.getData();
assertEquals(now / 1000, feed.getHeader().getTimestamp());
assertEquals(2, feed.getEntityCount());
{
FeedEntity entity = feed.getEntity(0);
assertEquals("1", entity.getId());
TripUpdate tripUpdate = entity.getTripUpdate();
assertEquals("t0", tripUpdate.getTrip().getTripId());
assertEquals("r1", tripUpdate.getTrip().getRouteId());
assertEquals("v1", tripUpdate.getVehicle().getId());
assertEquals(1234, tripUpdate.getTimestamp());
assertEquals(120, tripUpdate.getDelay());
assertEquals(1, tripUpdate.getStopTimeUpdateCount());
StopTimeUpdate stopTimeUpdate = tripUpdate.getStopTimeUpdate(0);
assertEquals("s2", stopTimeUpdate.getStopId());
assertEquals(now / 1000 + 5 * 60, stopTimeUpdate.getDeparture().getTime());
}
{
FeedEntity entity = feed.getEntity(1);
assertEquals("2", entity.getId());
TripUpdate tripUpdate = entity.getTripUpdate();
assertEquals("t1", tripUpdate.getTrip().getTripId());
assertEquals("r1", tripUpdate.getTrip().getRouteId());
assertEquals("v2", tripUpdate.getVehicle().getId());
assertEquals(5678, tripUpdate.getTimestamp());
assertEquals(180, tripUpdate.getDelay());
assertEquals(1, tripUpdate.getStopTimeUpdateCount());
StopTimeUpdate stopTimeUpdate = tripUpdate.getStopTimeUpdate(0);
assertEquals("s3", stopTimeUpdate.getStopId());
assertEquals(now / 1000 + 10 * 60, stopTimeUpdate.getDeparture().getTime());
}
}
use of com.google.transit.realtime.GtfsRealtime.TripUpdate in project onebusaway-application-modules by camsys.
the class GtfsRealtimeTripLibraryTest method testStopRewriting.
@Test
public void testStopRewriting() {
StopTimeUpdate.Builder stopTimeUpdate = StopTimeUpdate.newBuilder();
stopTimeUpdate.setStopId("replaceA");
StopTimeEvent.Builder stopTimeEvent = StopTimeEvent.newBuilder();
stopTimeEvent.setDelay(180);
stopTimeUpdate.setDeparture(stopTimeEvent);
stopTimeUpdate.setStopSequence(0);
TripUpdate tripUpdate = TripUpdate.newBuilder().setTrip(TripDescriptor.newBuilder().setTripId("tripA")).setDelay(120).setTimestamp(123456789).addStopTimeUpdate(stopTimeUpdate).build();
TripEntryImpl tripA = trip("tripA");
stopTime(0, stop("stopA", 0, 0), tripA, time(7, 30), 0.0);
BlockEntryImpl blockA = block("blockA");
BlockConfigurationEntry blockConfigA = blockConfiguration(blockA, serviceIds("s1"), tripA);
BlockInstance blockInstanceA = new BlockInstance(blockConfigA, 0L);
Mockito.when(_blockCalendarService.getActiveBlocks(Mockito.eq(blockA.getId()), Mockito.anyLong(), Mockito.anyLong())).thenReturn(Collections.singletonList(blockInstanceA));
CombinedTripUpdatesAndVehiclePosition update = new CombinedTripUpdatesAndVehiclePosition();
update.block = new BlockDescriptor();
update.block.setBlockInstance(blockInstanceA);
update.tripUpdates = Collections.singletonList(tripUpdate);
StopModificationStrategy strategy = Mockito.mock(StopModificationStrategy.class);
Mockito.when(strategy.convertStopId("replaceA")).thenReturn("stopA");
_library.setStopModificationStrategy(strategy);
VehicleLocationRecord record = _library.createVehicleLocationRecordForUpdate(update);
assertEquals(123456789000L, record.getTimeOfRecord());
assertEquals(120, record.getScheduleDeviation(), 0.0);
TimepointPredictionRecord tpr = record.getTimepointPredictions().get(0);
long departure = tpr.getTimepointPredictedDepartureTime();
assertEquals(departure, time(7, 33) * 1000);
}
use of com.google.transit.realtime.GtfsRealtime.TripUpdate in project onebusaway-application-modules by camsys.
the class GtfsRealtimeTripLibraryTest method testTprInterpolation_0.
/**
* This method tests that we create timepoint prediction records for stops
* that have not been served yet if there are TPRs downstream. If TPRs exist
* downstream of a stop, the bus is assumed to be ahead of that stop. This
* assumption is not necessarily true for stop time updates. We require that
* the trip update delay indicates realtime schedule adherence for this
* behavior to make sense.
*
* Current time = 7:31. Trip update delay = 2 minutes
* Schedule time Real-time from feed Timepoint predicted departure time
* Stop A 7:30 ----- 7:32
* Stop B 7:40 7:43 7:43
*/
@Test
public void testTprInterpolation_0() {
_library.setCurrentTime(time(7, 31) * 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");
assertEquals(stopADept, time(7, 32) * 1000);
long stopBDept = getPredictedDepartureTimeByStopId(record, "stopB");
assertEquals(stopBDept, time(7, 43) * 1000);
}
use of com.google.transit.realtime.GtfsRealtime.TripUpdate in project onebusaway-application-modules by camsys.
the class GtfsRealtimeTripLibraryTest method test.
@Test
public void test() {
FeedEntity tripUpdateEntityA = createTripUpdate("tripA", "stopA", 60, true);
FeedEntity tripUpdateEntityB = createTripUpdate("tripB", "stopB", 120, true);
FeedEntity tripUpdateEntityC = createTripUpdate("tripC", "stopA", 30, true);
FeedEntity tripUpdateEntityD = createTripUpdate("tripD", "stopB", 90, true);
FeedMessage.Builder tripUpdates = createFeed();
tripUpdates.addEntity(tripUpdateEntityA);
tripUpdates.addEntity(tripUpdateEntityB);
tripUpdates.addEntity(tripUpdateEntityC);
tripUpdates.addEntity(tripUpdateEntityD);
TripEntryImpl tripA = trip("tripA");
TripEntryImpl tripB = trip("tripB");
TripEntryImpl tripC = trip("tripC");
TripEntryImpl tripD = trip("tripD");
StopEntryImpl stopA = stop("stopA", 0, 0);
StopEntryImpl stopB = stop("stopB", 0, 0);
stopTime(0, stopA, tripA, time(7, 30), 0.0);
stopTime(1, stopB, tripB, time(8, 30), 0.0);
stopTime(2, stopA, tripC, time(8, 30), 0.0);
stopTime(3, stopB, tripD, time(9, 30), 0.0);
Mockito.when(_entitySource.getTrip("tripA")).thenReturn(tripA);
Mockito.when(_entitySource.getTrip("tripB")).thenReturn(tripB);
Mockito.when(_entitySource.getTrip("tripC")).thenReturn(tripC);
Mockito.when(_entitySource.getTrip("tripD")).thenReturn(tripD);
BlockEntryImpl blockA = block("blockA");
BlockEntryImpl blockB = block("blockB");
BlockConfigurationEntry blockConfigA = blockConfiguration(blockA, serviceIds("s1"), tripA, tripB);
BlockConfigurationEntry blockConfigB = blockConfiguration(blockB, serviceIds("s1"), tripC, tripD);
BlockInstance blockInstanceA = new BlockInstance(blockConfigA, 0L);
BlockInstance blockInstanceB = new BlockInstance(blockConfigB, 0L);
Mockito.when(_blockCalendarService.getActiveBlocks(Mockito.eq(blockA.getId()), Mockito.anyLong(), Mockito.anyLong())).thenReturn(Arrays.asList(blockInstanceA));
Mockito.when(_blockCalendarService.getActiveBlocks(Mockito.eq(blockB.getId()), Mockito.anyLong(), Mockito.anyLong())).thenReturn(Arrays.asList(blockInstanceB));
FeedMessage.Builder vehiclePositions = createFeed();
// FeedEntity.Builder vehiclePositionEntity = FeedEntity.newBuilder();
// vehiclePositions.addEntity(vehiclePositionEntity);
List<CombinedTripUpdatesAndVehiclePosition> groups = _library.groupTripUpdatesAndVehiclePositions(tripUpdates.build(), vehiclePositions.build());
assertEquals(2, groups.size());
Collections.sort(groups);
CombinedTripUpdatesAndVehiclePosition group = groups.get(0);
assertSame(blockA, group.block.getBlockInstance().getBlock().getBlock());
assertEquals(2, group.tripUpdates.size());
TripUpdate tripUpdate = group.tripUpdates.get(0);
assertEquals("tripA", tripUpdate.getTrip().getTripId());
tripUpdate = group.tripUpdates.get(1);
assertEquals("tripB", tripUpdate.getTrip().getTripId());
group = groups.get(1);
assertSame(blockB, group.block.getBlockInstance().getBlock().getBlock());
assertEquals(2, group.tripUpdates.size());
tripUpdate = group.tripUpdates.get(0);
assertEquals("tripC", tripUpdate.getTrip().getTripId());
tripUpdate = group.tripUpdates.get(1);
assertEquals("tripD", tripUpdate.getTrip().getTripId());
VehicleLocationRecord record = _library.createVehicleLocationRecordForUpdate(groups.get(0));
assertEquals(blockA.getId(), record.getBlockId());
assertEquals(120, record.getScheduleDeviation(), 0.0);
assertEquals(0L, record.getServiceDate());
assertEquals(blockA.getId(), record.getVehicleId());
record = _library.createVehicleLocationRecordForUpdate(groups.get(1));
assertEquals(blockB.getId(), record.getBlockId());
assertEquals(30, record.getScheduleDeviation(), 0.0);
assertEquals(0L, record.getServiceDate());
assertEquals(blockB.getId(), record.getVehicleId());
}
Aggregations