use of org.onebusaway.transit_data.model.ArrivalsAndDeparturesQueryBean in project onebusaway-application-modules by camsys.
the class SiriSupport method fillOnwardCalls.
private static void fillOnwardCalls(MonitoredVehicleJourneyStructure monitoredVehicleJourney, BlockInstanceBean blockInstance, TripBean framedJourneyTripBean, TripStatusBean currentVehicleTripStatus, OnwardCallsMode onwardCallsMode, PresentationService presentationService, TransitDataService transitDataService, Map<String, TimepointPredictionRecord> stopLevelPredictions, int maximumOnwardCalls, boolean hasRealtimeData, long responseTimestamp) {
String tripIdOfMonitoredCall = framedJourneyTripBean.getId();
monitoredVehicleJourney.setOnwardCalls(new OnwardCallsStructure());
// no need to go further if this is the case!
if (maximumOnwardCalls == 0) {
return;
}
List<BlockTripBean> blockTrips = blockInstance.getBlockConfiguration().getTrips();
double distanceOfVehicleAlongBlock = 0;
int blockTripStopsAfterTheVehicle = 0;
int onwardCallsAdded = 0;
boolean foundActiveTrip = false;
for (int i = 0; i < blockTrips.size(); i++) {
BlockTripBean blockTrip = blockTrips.get(i);
if (!foundActiveTrip) {
if (currentVehicleTripStatus.getActiveTrip().getId().equals(blockTrip.getTrip().getId())) {
distanceOfVehicleAlongBlock += currentVehicleTripStatus.getDistanceAlongTrip();
foundActiveTrip = true;
} else {
// so to get the size of this one, we have to look at the next.
if (i + 1 < blockTrips.size()) {
distanceOfVehicleAlongBlock = blockTrips.get(i + 1).getDistanceAlongBlock();
}
// bus has already served this trip, so no need to go further
continue;
}
}
if (onwardCallsMode == OnwardCallsMode.STOP_MONITORING) {
// always include onward calls for the trip the monitored call is on ONLY.
if (!blockTrip.getTrip().getId().equals(tripIdOfMonitoredCall)) {
continue;
}
}
boolean foundMatch = false;
HashMap<String, Integer> visitNumberForStopMap = new HashMap<String, Integer>();
for (BlockStopTimeBean stopTime : blockTrip.getBlockStopTimes()) {
int visitNumber = getVisitNumber(visitNumberForStopMap, stopTime.getStopTime().getStop());
StopBean stop = stopTime.getStopTime().getStop();
double distanceOfCallAlongTrip = stopTime.getDistanceAlongBlock() - blockTrip.getDistanceAlongBlock();
double distanceOfVehicleFromCall = stopTime.getDistanceAlongBlock() - distanceOfVehicleAlongBlock;
// on future trips, count always.
if (currentVehicleTripStatus.getActiveTrip().getId().equals(blockTrip.getTrip().getId())) {
if (!hasRealtimeData) {
if (stop.getId().equals(currentVehicleTripStatus.getNextStop().getId()))
foundMatch = true;
if (foundMatch) {
blockTripStopsAfterTheVehicle++;
ArrivalsAndDeparturesQueryBean query = new ArrivalsAndDeparturesQueryBean();
StopWithArrivalsAndDeparturesBean result = transitDataService.getStopWithArrivalsAndDepartures(stop.getId(), query);
// We can't assume the first result is the correct result
Collections.sort(result.getArrivalsAndDepartures(), new SortByTime());
if (result.getArrivalsAndDepartures().isEmpty()) {
// bad data? abort!
continue;
}
ArrivalAndDepartureBean arrivalAndDeparture = result.getArrivalsAndDepartures().get(0);
distanceOfVehicleFromCall = arrivalAndDeparture.getDistanceFromStop();
// responseTimestamp = arrivalAndDeparture.getScheduledArrivalTime();
} else
continue;
} else if (stopTime.getDistanceAlongBlock() >= distanceOfVehicleAlongBlock) {
blockTripStopsAfterTheVehicle++;
} else {
// stop is behind the bus--no need to go further
continue;
}
// future trip--bus hasn't reached this trip yet, so count all stops
} else {
blockTripStopsAfterTheVehicle++;
}
monitoredVehicleJourney.getOnwardCalls().getOnwardCall().add(getOnwardCallStructure(stop, presentationService, distanceOfCallAlongTrip, distanceOfVehicleFromCall, visitNumber, blockTripStopsAfterTheVehicle - 1, stopLevelPredictions.get(stopTime.getStopTime().getStop().getId()), hasRealtimeData, responseTimestamp, (currentVehicleTripStatus.getServiceDate() + stopTime.getStopTime().getArrivalTime() * 1000)));
onwardCallsAdded++;
if (onwardCallsAdded >= maximumOnwardCalls) {
return;
}
}
// if we get here, we added our stops
return;
}
return;
}
use of org.onebusaway.transit_data.model.ArrivalsAndDeparturesQueryBean in project onebusaway-application-modules by camsys.
the class RealtimeServiceImpl method getArrivalsAndDeparturesForStop.
private List<ArrivalAndDepartureBean> getArrivalsAndDeparturesForStop(String stopId, long currentTime) {
ArrivalsAndDeparturesQueryBean query = new ArrivalsAndDeparturesQueryBean();
query.setTime(currentTime);
query.setMinutesBefore(5);
query.setMinutesAfter(65);
StopWithArrivalsAndDeparturesBean stopWithArrivalsAndDepartures = _transitDataService.getStopWithArrivalsAndDepartures(stopId, query);
return stopWithArrivalsAndDepartures.getArrivalsAndDepartures();
}
use of org.onebusaway.transit_data.model.ArrivalsAndDeparturesQueryBean in project onebusaway-application-modules by camsys.
the class ArrivalsAndDeparturesBeanServiceImplTest method test.
@Test
public void test() {
long t = dateAsLong("2010-10-05 16:30");
long serviceDate = dateAsLong("2010-10-05 00:00");
int minutesBefore = 5;
int minutesAfter = 30;
StopEntryImpl stopA = stop("stopA", 47.0, -122.0);
StopEntryImpl stopB = stop("stopB", 47.0, -122.0);
Mockito.when(_transitGraphDao.getStopEntryForId(stopA.getId(), true)).thenReturn(stopA);
Mockito.when(_transitGraphDao.getStopEntryForId(stopB.getId(), true)).thenReturn(stopB);
/**
**
* Block A
***
*/
BlockEntryImpl blockA = block("blockA");
TripEntryImpl tripA = trip("tripA", "sA", 3000);
stopTime(0, stopA, tripA, time(16, 30), time(16, 35), 1000);
StopTimeEntryImpl stopTimeAB = stopTime(1, stopB, tripA, time(16, 40), time(16, 45), 2000);
BlockConfigurationEntry blockConfigA = blockConfiguration(blockA, serviceIds(lsids("sA"), lsids()), tripA);
BlockStopTimeEntry bstAA = blockConfigA.getStopTimes().get(0);
BlockStopTimeEntry bstAB = blockConfigA.getStopTimes().get(1);
/**
**
* Block B
***
*/
BlockEntryImpl blockB = block("blockB");
TripEntryImpl tripB = trip("tripB", "sA", 3000);
stopTime(2, stopA, tripB, time(16, 40), time(16, 45), 1000);
StopTimeEntryImpl stopTimeBB = stopTime(3, stopB, tripB, time(16, 50), time(16, 55), 2000);
BlockConfigurationEntry blockConfigB = blockConfiguration(blockB, serviceIds(lsids("sA"), lsids()), tripB);
BlockStopTimeEntry bstBA = blockConfigB.getStopTimes().get(0);
BlockStopTimeEntry bstBB = blockConfigB.getStopTimes().get(1);
/**
**
*
***
*/
BlockInstance blockInstanceA = new BlockInstance(blockConfigA, serviceDate);
long lastUpdateTime = dateAsLong("2010-10-05 16:15");
BlockLocation blockLocationA = new BlockLocation();
blockLocationA.setActiveTrip(bstAA.getTrip());
blockLocationA.setBlockInstance(blockInstanceA);
blockLocationA.setClosestStop(bstAA);
blockLocationA.setDistanceAlongBlock(500);
blockLocationA.setInService(true);
blockLocationA.setLastUpdateTime(lastUpdateTime);
blockLocationA.setNextStop(bstAA);
blockLocationA.setPredicted(true);
blockLocationA.setScheduledDistanceAlongBlock(600);
blockLocationA.setScheduleDeviation(10 * 60);
blockLocationA.setVehicleId(aid("vehicle"));
/**
**
*
***
*/
BlockInstance blockInstanceB = new BlockInstance(blockConfigB, serviceDate);
BlockLocation blockLocationB = new BlockLocation();
blockLocationB.setActiveTrip(bstBA.getTrip());
blockLocationB.setBlockInstance(blockInstanceA);
blockLocationB.setClosestStop(bstBA);
blockLocationB.setDistanceAlongBlock(400);
blockLocationB.setInService(true);
blockLocationB.setNextStop(bstAA);
blockLocationB.setPredicted(false);
blockLocationB.setScheduledDistanceAlongBlock(400);
/**
**
*
***
*/
long stopTimeFrom = t - minutesBefore * 60 * 1000;
long stopTimeTo = t + minutesAfter * 60 * 1000;
StopTimeInstance sti1 = new StopTimeInstance(bstAB, blockInstanceA.getState());
ArrivalAndDepartureInstance in1 = new ArrivalAndDepartureInstance(sti1);
in1.setBlockLocation(blockLocationA);
in1.setPredictedArrivalTime((long) (in1.getScheduledArrivalTime() + 5 * 60 * 1000));
in1.setPredictedDepartureTime((long) (in1.getScheduledDepartureTime()));
StopTimeInstance sti2 = new StopTimeInstance(bstBB, blockInstanceB.getState());
ArrivalAndDepartureInstance in2 = new ArrivalAndDepartureInstance(sti2);
in2.setBlockLocation(blockLocationB);
TargetTime target = new TargetTime(t, t);
Mockito.when(_arrivalAndDepartureService.getArrivalsAndDeparturesForStopInTimeRange(stopB, target, stopTimeFrom, stopTimeTo)).thenReturn(Arrays.asList(in1, in2));
/**
**
*
***
*/
Builder stopTimeNarrative = StopTimeNarrative.builder();
stopTimeNarrative.setStopHeadsign("Downtown");
Mockito.when(_narrativeService.getStopTimeForEntry(stopTimeAB)).thenReturn(stopTimeNarrative.create());
stopTimeNarrative = StopTimeNarrative.builder();
stopTimeNarrative.setRouteShortName("XX");
Mockito.when(_narrativeService.getStopTimeForEntry(stopTimeBB)).thenReturn(stopTimeNarrative.create());
/**
**
*
***
*/
StopBean stopABean = new StopBean();
stopABean.setId("1_stopA");
Mockito.when(_stopBeanService.getStopForId(stopA.getId())).thenReturn(stopABean);
StopBean stopBBean = new StopBean();
stopBBean.setId("1_stopB");
Mockito.when(_stopBeanService.getStopForId(stopB.getId())).thenReturn(stopBBean);
/**
**
*
***
*/
TripBean tripABean = new TripBean();
Mockito.when(_tripBeanService.getTripForId(aid("tripA"))).thenReturn(tripABean);
TripBean tripBBean = new TripBean();
Mockito.when(_tripBeanService.getTripForId(aid("tripB"))).thenReturn(tripBBean);
/**
**
*
***
*/
TripStatusBean tripStatusBeanA = new TripStatusBean();
TripStatusBean tripStatusBeanB = new TripStatusBean();
Mockito.when(_tripDetailsBeanService.getBlockLocationAsStatusBean(blockLocationA, t)).thenReturn(tripStatusBeanA);
Mockito.when(_tripDetailsBeanService.getBlockLocationAsStatusBean(blockLocationB, t)).thenReturn(tripStatusBeanB);
/**
**
*
***
*/
ArrivalsAndDeparturesQueryBean query = new ArrivalsAndDeparturesQueryBean();
query.setTime(t);
query.setMinutesBefore(minutesBefore);
query.setMinutesAfter(minutesAfter);
query.setFrequencyMinutesBefore(minutesBefore);
query.setFrequencyMinutesAfter(minutesAfter);
List<ArrivalAndDepartureBean> arrivalsAndDepartures = _service.getArrivalsAndDeparturesByStopId(stopB.getId(), query);
assertEquals(2, arrivalsAndDepartures.size());
ArrivalAndDepartureBean bean = arrivalsAndDepartures.get(0);
assertEquals(1500, bean.getDistanceFromStop(), 0.0);
assertEquals(lastUpdateTime, bean.getLastUpdateTime().longValue());
assertEquals(1, bean.getNumberOfStopsAway());
assertEquals(dateAsLong("2010-10-05 16:45"), bean.getPredictedArrivalTime());
assertEquals(dateAsLong("2010-10-05 16:45"), bean.getPredictedDepartureTime());
assertNull(bean.getRouteShortName());
assertEquals(dateAsLong("2010-10-05 16:40"), bean.getScheduledArrivalTime());
assertEquals(dateAsLong("2010-10-05 16:45"), bean.getScheduledDepartureTime());
assertEquals(serviceDate, bean.getServiceDate());
assertEquals("default", bean.getStatus());
assertSame(stopBBean, bean.getStop());
assertSame(tripABean, bean.getTrip());
assertSame(tripStatusBeanA, bean.getTripStatus());
assertEquals("Downtown", bean.getTripHeadsign());
assertEquals("1_vehicle", bean.getVehicleId());
bean = arrivalsAndDepartures.get(1);
assertEquals(1600, bean.getDistanceFromStop(), 0.0);
assertNull(bean.getLastUpdateTime());
assertEquals(1, bean.getNumberOfStopsAway());
assertEquals(0L, bean.getPredictedArrivalTime());
assertEquals(0L, bean.getPredictedDepartureTime());
assertEquals("XX", bean.getRouteShortName());
assertEquals(dateAsLong("2010-10-05 16:50"), bean.getScheduledArrivalTime());
assertEquals(dateAsLong("2010-10-05 16:55"), bean.getScheduledDepartureTime());
assertEquals(serviceDate, bean.getServiceDate());
assertEquals("default", bean.getStatus());
assertSame(stopBBean, bean.getStop());
assertSame(tripBBean, bean.getTrip());
assertSame(tripStatusBeanB, bean.getTripStatus());
assertNull(bean.getTripHeadsign());
assertNull(bean.getVehicleId());
}
use of org.onebusaway.transit_data.model.ArrivalsAndDeparturesQueryBean in project onebusaway-application-modules by camsys.
the class RealtimeServiceV2Impl method getArrivalsAndDeparturesForStop.
private List<ArrivalAndDepartureBean> getArrivalsAndDeparturesForStop(String stopId, long currentTime) {
ArrivalsAndDeparturesQueryBean query = new ArrivalsAndDeparturesQueryBean();
query.setTime(currentTime);
query.setMinutesBefore(5 * 60);
query.setMinutesAfter(5 * 60);
StopWithArrivalsAndDeparturesBean stopWithArrivalsAndDepartures = _transitDataService.getStopWithArrivalsAndDepartures(stopId, query);
return stopWithArrivalsAndDepartures.getArrivalsAndDepartures();
}
use of org.onebusaway.transit_data.model.ArrivalsAndDeparturesQueryBean in project onebusaway-application-modules by camsys.
the class ScheduleAction method getModel.
@Override
public Body<ScheduleRoute> getModel() {
Body<ScheduleRoute> body = new Body<ScheduleRoute>();
List<AgencyAndId> routeIds = new ArrayList<AgencyAndId>();
if (this.isValid(body, routeIds)) {
AgencyBean agency = _transitDataService.getAgency(agencyId);
List<HashMap<String, HashSet<ScheduleStop>>> blockStopsMapList = new ArrayList<HashMap<String, HashSet<ScheduleStop>>>();
blockStopsMapList.add(new HashMap<String, HashSet<ScheduleStop>>());
blockStopsMapList.add(new HashMap<String, HashSet<ScheduleStop>>());
blockStopsMapList.add(new HashMap<String, HashSet<ScheduleStop>>());
blockStopsMapList.add(new HashMap<String, HashSet<ScheduleStop>>());
// Get All the Stops for a specific route
for (AgencyAndId routeId : routeIds) {
String route = AgencyAndId.convertToString(routeId);
StopsForRouteBean stopsForRoute = _service.getStopsForRoute(route);
for (StopGroupingBean stopGroupingBean : stopsForRoute.getStopGroupings()) {
for (StopGroupBean stopGroupBean : stopGroupingBean.getStopGroups()) {
// Weekday Trips
for (Long weekdayTime : DateUtil.getWeekdayDateTimes(agency.getTimezone())) {
ArrivalsAndDeparturesQueryBean query = new ArrivalsAndDeparturesQueryBean();
query.setTime(weekdayTime);
query.setMinutesBefore(0);
query.setMinutesAfter(MINUTES_IN_DAY);
StopsWithArrivalsAndDeparturesBean stopsWithArrivals = _service.getStopsWithArrivalsAndDepartures(stopGroupBean.getStopIds(), query);
for (ArrivalAndDepartureBean arrivalsAndDeparture : stopsWithArrivals.getArrivalsAndDepartures()) {
// Filter Arrivals and Departures By Route
if (arrivalsAndDeparture.getTrip().getRoute().getId().equals(route)) {
ScheduleStop scheduleStop = new ScheduleStop();
scheduleStop.setTag(getIdNoAgency(arrivalsAndDeparture.getStop().getId()));
scheduleStop.setEpochTime(arrivalsAndDeparture.getScheduledArrivalTime());
scheduleStop.setStopName(arrivalsAndDeparture.getStop().getName());
if (arrivalsAndDeparture.getTrip().getDirectionId().equals("0")) {
addStopByBlockId(blockStopsMapList.get(0), arrivalsAndDeparture.getTrip().getBlockId(), scheduleStop);
} else if (arrivalsAndDeparture.getTrip().getDirectionId().equals("1")) {
addStopByBlockId(blockStopsMapList.get(1), arrivalsAndDeparture.getTrip().getBlockId(), scheduleStop);
}
}
}
}
// Weekend Trips
for (Long weekendTime : DateUtil.getWeekendDateTimes(agency.getTimezone())) {
ArrivalsAndDeparturesQueryBean query = new ArrivalsAndDeparturesQueryBean();
query.setTime(weekendTime);
query.setMinutesBefore(0);
query.setMinutesAfter(MINUTES_IN_DAY);
StopsWithArrivalsAndDeparturesBean stopsWithArrivals = _service.getStopsWithArrivalsAndDepartures(stopGroupBean.getStopIds(), query);
for (ArrivalAndDepartureBean arrivalsAndDeparture : stopsWithArrivals.getArrivalsAndDepartures()) {
// Filter Arrivals and Departures By Route
if (arrivalsAndDeparture.getTrip().getRoute().getId().equals(route)) {
ScheduleStop scheduleStop = new ScheduleStop();
scheduleStop.setTag(getIdNoAgency(arrivalsAndDeparture.getStop().getId()));
scheduleStop.setEpochTime(arrivalsAndDeparture.getScheduledArrivalTime());
scheduleStop.setStopName(arrivalsAndDeparture.getStop().getName());
if (arrivalsAndDeparture.getTrip().getDirectionId().equals("0")) {
addStopByBlockId(blockStopsMapList.get(2), arrivalsAndDeparture.getTrip().getBlockId(), scheduleStop);
} else if (arrivalsAndDeparture.getTrip().getDirectionId().equals("1")) {
addStopByBlockId(blockStopsMapList.get(3), arrivalsAndDeparture.getTrip().getBlockId(), scheduleStop);
}
}
}
}
}
}
// Routes
for (int n = 0; n < blockStopsMapList.size(); n++) {
HashMap<String, HashSet<ScheduleStop>> blockStopsMap = blockStopsMapList.get(n);
ScheduleRoute scheduleRoute = new ScheduleRoute();
scheduleRoute.setTitle(stopsForRoute.getRoute().getLongName());
scheduleRoute.setDirection(Integer.toString(n % 2));
scheduleRoute.setTag(getIdNoAgency(stopsForRoute.getRoute().getId()));
scheduleRoute.setServiceClass(n < 3 ? "wkd" : "wkend");
// Blocks
for (Entry<String, HashSet<ScheduleStop>> entry : blockStopsMap.entrySet()) {
int tripStopTimeCounter = 0;
String blockId = entry.getKey();
HashSet<ScheduleStop> blockStops = entry.getValue();
ScheduleTableRow scheduleTr = new ScheduleTableRow(getIdNoAgency(blockId));
// Stop Times
for (ScheduleStop stop : blockStops) {
if (tripStopTimeCounter == 0) {
DisplayStop displayStop = new DisplayStop();
displayStop.setTag(stop.getTag());
displayStop.setValue(stop.getStopName());
scheduleRoute.getStops().add(displayStop);
}
// scheduleStop.setValue(value);
scheduleTr.getStops().add(stop);
}
scheduleRoute.getScheduleTableRow().add(scheduleTr);
}
body.getResponse().add(scheduleRoute);
}
}
}
return body;
}
Aggregations