use of org.onebusaway.transit_data.model.trips.TripStatusBean 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.trips.TripStatusBean in project onebusaway-application-modules by camsys.
the class RealtimeServiceV2Impl method getMonitoredStopVisitsForStop.
@Override
public List<MonitoredStopVisitStructure> getMonitoredStopVisitsForStop(String stopId, int maximumOnwardCalls, DetailLevel detailLevel, long currentTime, List<AgencyAndId> routeIds, Map<Filters, String> filters) {
List<MonitoredStopVisitStructure> output = new ArrayList<MonitoredStopVisitStructure>();
String directionId = filters.get(Filters.DIRECTION_REF);
int maximumStopVisits = SiriSupportV2.convertToNumeric(filters.get(Filters.MAX_STOP_VISITS), Integer.MAX_VALUE);
Integer minimumStopVisitsPerLine = SiriSupportV2.convertToNumeric(filters.get(Filters.MIN_STOP_VISITS), null);
Map<AgencyAndId, Integer> visitCountByLine = new HashMap<AgencyAndId, Integer>();
int visitCount = 0;
for (ArrivalAndDepartureBean adBean : getArrivalsAndDeparturesForStop(stopId, currentTime)) {
TripStatusBean statusBeanForCurrentTrip = adBean.getTripStatus();
TripBean tripBeanForAd = adBean.getTrip();
if (statusBeanForCurrentTrip == null)
continue;
if (!_presentationService.include(statusBeanForCurrentTrip) || !_presentationService.include(adBean, statusBeanForCurrentTrip))
continue;
MonitoredStopVisitStructure stopVisit = new MonitoredStopVisitStructure();
stopVisit.setRecordedAtTime(DateUtil.toXmlGregorianCalendar(statusBeanForCurrentTrip.getLastUpdateTime()));
List<TimepointPredictionRecord> timePredictionRecords = null;
timePredictionRecords = _transitDataService.getPredictionRecordsForTrip(AgencyAndId.convertFromString(stopId).getAgencyId(), statusBeanForCurrentTrip);
MonitoredVehicleJourneyStructure mvjourney = new MonitoredVehicleJourneyStructure();
stopVisit.setMonitoredVehicleJourney(mvjourney);
SiriSupportV2.fillMonitoredVehicleJourney(mvjourney, tripBeanForAd, statusBeanForCurrentTrip, adBean.getStop(), OnwardCallsMode.STOP_MONITORING, _presentationService, _transitDataService, maximumOnwardCalls, timePredictionRecords, statusBeanForCurrentTrip.isPredicted(), detailLevel, currentTime, filters);
// FILTERS
AgencyAndId thisRouteId = AgencyAndIdLibrary.convertFromString(mvjourney.getLineRef().getValue());
String thisDirectionId = mvjourney.getDirectionRef().getValue();
if (routeIds.size() > 0 && !routeIds.contains(thisRouteId))
continue;
if (directionId != null && !thisDirectionId.equals(directionId))
continue;
// Monitored Stop Visits
Map<String, MonitoredStopVisitStructure> visitsMap = new HashMap<String, MonitoredStopVisitStructure>();
// visit count filters
Integer visitCountForThisLine = visitCountByLine.get(thisRouteId);
if (visitCountForThisLine == null) {
visitCountForThisLine = 0;
}
if (visitCount >= maximumStopVisits) {
if (minimumStopVisitsPerLine == null) {
break;
} else {
if (visitCountForThisLine >= minimumStopVisitsPerLine) {
continue;
}
}
}
// unique stops filters
if (stopVisit.getMonitoredVehicleJourney() == null || stopVisit.getMonitoredVehicleJourney().getVehicleRef() == null || StringUtils.isBlank(stopVisit.getMonitoredVehicleJourney().getVehicleRef().getValue())) {
continue;
} else {
String visitKey = stopVisit.getMonitoredVehicleJourney().getVehicleRef().getValue();
if (visitsMap.containsKey(stopVisit.getMonitoredVehicleJourney().getVehicleRef().getValue())) {
if (stopVisit.getMonitoredVehicleJourney().getProgressStatus() == null) {
visitsMap.remove(visitKey);
visitsMap.put(visitKey, stopVisit);
}
continue;
} else {
visitsMap.put(stopVisit.getMonitoredVehicleJourney().getVehicleRef().getValue(), stopVisit);
}
}
output.add(stopVisit);
visitCount++;
visitCountForThisLine++;
visitCountByLine.put(thisRouteId, visitCountForThisLine);
}
Collections.sort(output, new Comparator<MonitoredStopVisitStructure>() {
public int compare(MonitoredStopVisitStructure arg0, MonitoredStopVisitStructure arg1) {
try {
SiriExtensionWrapper wrapper0 = (SiriExtensionWrapper) arg0.getMonitoredVehicleJourney().getMonitoredCall().getExtensions().getAny();
SiriExtensionWrapper wrapper1 = (SiriExtensionWrapper) arg1.getMonitoredVehicleJourney().getMonitoredCall().getExtensions().getAny();
return wrapper0.getDistances().getDistanceFromCall().compareTo(wrapper1.getDistances().getDistanceFromCall());
} catch (Exception e) {
return -1;
}
}
});
return output;
}
use of org.onebusaway.transit_data.model.trips.TripStatusBean in project onebusaway-application-modules by camsys.
the class BeanFactoryV2 method getVehicleStatus.
public VehicleStatusV2Bean getVehicleStatus(VehicleStatusBean vehicleStatus) {
VehicleStatusV2Bean bean = new VehicleStatusV2Bean();
bean.setLastUpdateTime(vehicleStatus.getLastUpdateTime());
if (vehicleStatus.getLastLocationUpdateTime() > 0)
bean.setLastLocationUpdateTime(vehicleStatus.getLastLocationUpdateTime());
bean.setLocation(vehicleStatus.getLocation());
bean.setPhase(vehicleStatus.getPhase());
bean.setStatus(vehicleStatus.getStatus());
bean.setVehicleId(vehicleStatus.getVehicleId());
TripBean trip = vehicleStatus.getTrip();
if (trip != null) {
bean.setTripId(trip.getId());
addToReferences(trip);
}
TripStatusBean tripStatus = vehicleStatus.getTripStatus();
if (tripStatus != null)
bean.setTripStatus(getTripStatus(tripStatus));
return bean;
}
use of org.onebusaway.transit_data.model.trips.TripStatusBean in project onebusaway-application-modules by camsys.
the class VehiclePositionsForAgencyAction method fillFeedMessage.
@Override
protected void fillFeedMessage(FeedMessage.Builder feed, String agencyId, long timestamp) {
ListBean<VehicleStatusBean> vehicles = _service.getAllVehiclesForAgency(agencyId, timestamp);
for (VehicleStatusBean vehicle : vehicles.getList()) {
FeedEntity.Builder entity = feed.addEntityBuilder();
entity.setId(Integer.toString(feed.getEntityCount()));
VehiclePosition.Builder vehiclePosition = entity.getVehicleBuilder();
TripStatusBean tripStatus = vehicle.getTripStatus();
if (tripStatus != null) {
TripBean activeTrip = tripStatus.getActiveTrip();
RouteBean route = activeTrip.getRoute();
TripDescriptor.Builder tripDesc = vehiclePosition.getTripBuilder();
tripDesc.setTripId(normalizeId(activeTrip.getId()));
tripDesc.setRouteId(normalizeId(route.getId()));
}
VehicleDescriptor.Builder vehicleDesc = vehiclePosition.getVehicleBuilder();
vehicleDesc.setId(normalizeId(vehicle.getVehicleId()));
CoordinatePoint location = vehicle.getLocation();
if (location != null) {
Position.Builder position = vehiclePosition.getPositionBuilder();
position.setLatitude((float) location.getLat());
position.setLongitude((float) location.getLon());
}
vehiclePosition.setTimestamp(vehicle.getLastUpdateTime() / 1000);
}
}
use of org.onebusaway.transit_data.model.trips.TripStatusBean in project onebusaway-application-modules by camsys.
the class TripStatusBeanServiceImpl method getBlockLocationAsStatusBean.
@Override
public TripStatusBean getBlockLocationAsStatusBean(BlockLocation blockLocation, long time) {
TripStatusBean bean = new TripStatusBean();
bean.setStatus("default");
BlockInstance blockInstance = blockLocation.getBlockInstance();
long serviceDate = blockInstance.getServiceDate();
bean.setServiceDate(serviceDate);
bean.setLastUpdateTime(blockLocation.getLastUpdateTime());
bean.setLastLocationUpdateTime(blockLocation.getLastLocationUpdateTime());
bean.setLastKnownLocation(blockLocation.getLastKnownLocation());
bean.setLastKnownOrientation(blockLocation.getLastKnownOrientation());
bean.setLocation(blockLocation.getLocation());
bean.setOrientation(blockLocation.getOrientation());
bean.setLastKnownLocation(blockLocation.getLastKnownLocation());
if (blockLocation.isLastKnownOrientationSet())
bean.setLastKnownOrientation(blockLocation.getLastKnownOrientation());
bean.setScheduleDeviation(blockLocation.getScheduleDeviation());
BlockTripInstance activeTripInstance = blockLocation.getActiveTripInstance();
if (activeTripInstance != null) {
BlockTripEntry activeBlockTrip = activeTripInstance.getBlockTrip();
bean.setScheduledDistanceAlongTrip(blockLocation.getScheduledDistanceAlongBlock() - activeBlockTrip.getDistanceAlongBlock());
bean.setDistanceAlongTrip(blockLocation.getDistanceAlongBlock() - activeBlockTrip.getDistanceAlongBlock());
TripEntry activeTrip = activeBlockTrip.getTrip();
bean.setTotalDistanceAlongTrip(activeTrip.getTotalTripDistance());
TripBean activeTripBean = _tripBeanService.getTripForId(activeTrip.getId());
bean.setActiveTrip(activeTripBean);
bean.setBlockTripSequence(activeBlockTrip.getSequence());
if (blockLocation.isLastKnownDistanceAlongBlockSet()) {
bean.setLastKnownDistanceAlongTrip(blockLocation.getLastKnownDistanceAlongBlock() - activeBlockTrip.getDistanceAlongBlock());
}
FrequencyEntry frequencyLabel = activeTripInstance.getFrequencyLabel();
if (frequencyLabel != null) {
FrequencyBean fb = FrequencyBeanLibrary.getBeanForFrequency(serviceDate, frequencyLabel);
bean.setFrequency(fb);
}
} else {
_log.warn("no active block trip for block location: blockInstance=" + blockLocation.getBlockInstance() + " time=" + time);
}
BlockStopTimeEntry closestStop = blockLocation.getClosestStop();
if (closestStop != null) {
StopTimeEntry stopTime = closestStop.getStopTime();
StopBean stopBean = _stopBeanService.getStopForId(stopTime.getStop().getId());
bean.setClosestStop(stopBean);
bean.setClosestStopTimeOffset(blockLocation.getClosestStopTimeOffset());
}
BlockStopTimeEntry nextStop = blockLocation.getNextStop();
if (nextStop != null) {
StopTimeEntry stopTime = nextStop.getStopTime();
StopBean stopBean = _stopBeanService.getStopForId(stopTime.getStop().getId());
bean.setNextStop(stopBean);
bean.setNextStopTimeOffset(blockLocation.getNextStopTimeOffset());
bean.setNextStopDistanceFromVehicle(blockLocation.getNextStop().getDistanceAlongBlock() - blockLocation.getDistanceAlongBlock());
}
BlockStopTimeEntry previousStop = blockLocation.getPreviousStop();
if (previousStop != null) {
StopTimeEntry stopTime = previousStop.getStopTime();
StopBean stopBean = _stopBeanService.getStopForId(stopTime.getStop().getId());
bean.setPreviousStop(stopBean);
bean.setPreviousStopTimeOffset(blockLocation.getPreviousStopTimeOffset());
bean.setPreviousStopDistanceFromVehicle(blockLocation.getPreviousStop().getDistanceAlongBlock() - blockLocation.getDistanceAlongBlock());
}
EVehiclePhase phase = blockLocation.getPhase();
if (phase != null)
bean.setPhase(phase.toLabel());
String status = blockLocation.getStatus();
if (status != null)
bean.setStatus(status);
if (blockLocation.getVehicleType() != null)
bean.setVehicleType(blockLocation.getVehicleType().toLabel());
bean.setPredicted(blockLocation.isPredicted());
AgencyAndId vid = blockLocation.getVehicleId();
if (vid != null)
bean.setVehicleId(ApplicationBeanLibrary.getId(vid));
if (activeTripInstance != null) {
List<ServiceAlertBean> situations = _serviceAlertBeanService.getServiceAlertsForVehicleJourney(time, activeTripInstance, blockLocation.getVehicleId());
if (!situations.isEmpty())
bean.setSituations(situations);
}
if (blockLocation.getTimepointPredictions() != null && blockLocation.getTimepointPredictions().size() > 0) {
List<TimepointPredictionBean> timepointPredictions = new ArrayList<TimepointPredictionBean>();
for (TimepointPredictionRecord tpr : blockLocation.getTimepointPredictions()) {
TimepointPredictionBean tpb = new TimepointPredictionBean();
tpb.setTimepointId(tpr.getTimepointId().toString());
tpb.setTripId(tpr.getTripId().toString());
tpb.setStopSequence(tpr.getStopSequence());
tpb.setTimepointPredictedArrivalTime(tpr.getTimepointPredictedArrivalTime());
tpb.setTimepointPredictedDepartureTime(tpr.getTimepointPredictedDepartureTime());
timepointPredictions.add(tpb);
}
bean.setTimepointPredictions(timepointPredictions);
}
return bean;
}
Aggregations