use of org.onebusaway.realtime.api.TimepointPredictionRecord in project onebusaway-application-modules by camsys.
the class GtfsRealtimeTripLibraryTest method testCreateVehicleLocationRecordForUpdate_WithStopTimeUpdates.
@Test
public void testCreateVehicleLocationRecordForUpdate_WithStopTimeUpdates() {
StopTimeUpdate.Builder stopTimeUpdate = StopTimeUpdate.newBuilder();
stopTimeUpdate.setStopId("stopA");
StopTimeEvent.Builder stopTimeEvent = StopTimeEvent.newBuilder();
stopTimeEvent.setDelay(180);
stopTimeUpdate.setDeparture(stopTimeEvent);
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(Arrays.asList(blockInstanceA));
CombinedTripUpdatesAndVehiclePosition update = new CombinedTripUpdatesAndVehiclePosition();
update.block = new BlockDescriptor();
update.block.setBlockInstance(blockInstanceA);
update.tripUpdates = Arrays.asList(tripUpdate);
VehicleLocationRecord record = _library.createVehicleLocationRecordForUpdate(update);
TimepointPredictionRecord tpr = record.getTimepointPredictions().get(0);
long departure = tpr.getTimepointPredictedDepartureTime();
// 7:30 plus 3 min delay, + now we are in ms.
assertEquals(departure, time(7, 33) * 1000);
}
use of org.onebusaway.realtime.api.TimepointPredictionRecord 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.realtime.api.TimepointPredictionRecord in project onebusaway-application-modules by camsys.
the class RealtimeServiceV2Impl method getVehicleActivityForRoute.
/**
* SIRI METHODS
*/
@Override
public List<VehicleActivityStructure> getVehicleActivityForRoute(String routeId, String directionId, int maximumOnwardCalls, DetailLevel detailLevel, long currentTime) {
List<VehicleActivityStructure> output = new ArrayList<VehicleActivityStructure>();
ListBean<TripDetailsBean> trips = getAllTripsForRoute(routeId, currentTime);
for (TripDetailsBean tripDetails : trips.getList()) {
// filter out interlined routes
if (routeId != null && !tripDetails.getTrip().getRoute().getId().equals(routeId))
continue;
// filtered out by user
if (directionId != null && !tripDetails.getTrip().getDirectionId().equals(directionId))
continue;
if (!_presentationService.include(tripDetails.getStatus()))
continue;
VehicleActivityStructure activity = new VehicleActivityStructure();
activity.setRecordedAtTime(DateUtil.toXmlGregorianCalendar(tripDetails.getStatus().getLastUpdateTime()));
List<TimepointPredictionRecord> timePredictionRecords = null;
timePredictionRecords = _transitDataService.getPredictionRecordsForTrip(AgencyAndId.convertFromString(routeId).getAgencyId(), tripDetails.getStatus());
activity.setMonitoredVehicleJourney(new MonitoredVehicleJourney());
SiriSupportV2.fillMonitoredVehicleJourney(activity.getMonitoredVehicleJourney(), tripDetails.getTrip(), tripDetails.getStatus(), null, OnwardCallsMode.VEHICLE_MONITORING, _presentationService, _transitDataService, maximumOnwardCalls, timePredictionRecords, tripDetails.getStatus().isPredicted(), detailLevel, currentTime, null);
output.add(activity);
}
Collections.sort(output, new Comparator<VehicleActivityStructure>() {
public int compare(VehicleActivityStructure arg0, VehicleActivityStructure 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.realtime.api.TimepointPredictionRecord 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;
}
use of org.onebusaway.realtime.api.TimepointPredictionRecord in project onebusaway-application-modules by camsys.
the class BlockLocationServiceImpl method getBlockLocation.
/**
* @param blockInstance
* @param cacheElements
* @param scheduledLocation
* @param targetTime
* @return null if the effective scheduled block location cannot be determined
*/
private BlockLocation getBlockLocation(BlockInstance blockInstance, VehicleLocationCacheElements cacheElements, ScheduledBlockLocation scheduledLocation, long targetTime) {
BlockLocation location = new BlockLocation();
location.setTime(targetTime);
location.setBlockInstance(blockInstance);
VehicleLocationCacheElement cacheElement = null;
if (cacheElements != null)
cacheElement = cacheElements.getElementForTimestamp(targetTime);
if (cacheElement != null) {
VehicleLocationRecord record = cacheElement.getRecord();
if (scheduledLocation == null)
scheduledLocation = getScheduledBlockLocationForVehicleLocationCacheRecord(blockInstance, cacheElement, targetTime);
if (scheduledLocation != null) {
location.setEffectiveScheduleTime(scheduledLocation.getScheduledTime());
location.setDistanceAlongBlock(scheduledLocation.getDistanceAlongBlock());
}
location.setBlockStartTime(record.getBlockStartTime());
location.setPredicted(true);
location.setLastUpdateTime(record.getTimeOfRecord());
location.setLastLocationUpdateTime(record.getTimeOfLocationUpdate());
location.setScheduleDeviation(record.getScheduleDeviation());
location.setScheduleDeviations(cacheElement.getScheduleDeviations());
if (record.isCurrentLocationSet()) {
CoordinatePoint p = new CoordinatePoint(record.getCurrentLocationLat(), record.getCurrentLocationLon());
location.setLastKnownLocation(p);
}
location.setOrientation(record.getCurrentOrientation());
location.setPhase(record.getPhase());
location.setStatus(record.getStatus());
location.setVehicleId(record.getVehicleId());
List<TimepointPredictionRecord> timepointPredictions = record.getTimepointPredictions();
location.setTimepointPredictions(timepointPredictions);
if (timepointPredictions != null && !timepointPredictions.isEmpty()) {
SortedMap<Integer, Double> scheduleDeviations = new TreeMap<Integer, Double>();
BlockConfigurationEntry blockConfig = blockInstance.getBlock();
int tprIndexCounter = 0;
for (TimepointPredictionRecord tpr : timepointPredictions) {
AgencyAndId stopId = tpr.getTimepointId();
long predictedTime;
if (tpr.getTimepointPredictedDepartureTime() != -1) {
predictedTime = tpr.getTimepointPredictedDepartureTime();
} else {
predictedTime = tpr.getTimepointPredictedArrivalTime();
}
if (stopId == null || predictedTime == 0)
continue;
for (BlockStopTimeEntry blockStopTime : blockConfig.getStopTimes()) {
StopTimeEntry stopTime = blockStopTime.getStopTime();
StopEntry stop = stopTime.getStop();
// StopSequence equals to -1 when there is no stop sequence in the GTFS-rt
if (stopId.equals(stop.getId()) && stopTime.getTrip().getId().equals(tpr.getTripId()) && (tpr.getStopSequence() == -1 || stopTime.getSequence() == tpr.getStopSequence())) {
if (tpr.getStopSequence() == -1 && isFirstOrLastStopInTrip(stopTime) && isLoopRoute(stopTime)) {
if (isSinglePredictionForTrip(timepointPredictions, tpr, tprIndexCounter)) {
continue;
}
// If this isn't the last prediction, and we're on the first stop, then apply it
if (isLastPrediction(stopTime, timepointPredictions, tpr, tprIndexCounter) && isFirstStopInRoute(stopTime)) {
// Do not calculate schedule deviation
continue;
}
// If this is the last prediction, and we're on the last stop, then apply it
if (isFirstPrediction(stopTime, timepointPredictions, tpr, tprIndexCounter) && isLastStopInRoute(stopTime)) {
// Do not calculate schedule deviation
continue;
}
}
int arrivalOrDepartureTime;
// We currently use the scheduled arrival time of the stop as the search index
// This MUST be consistent with the index search in ArrivalAndSepartureServiceImpl.getBestScheduleDeviation()
int index = stopTime.getArrivalTime();
if (tpr.getTimepointPredictedDepartureTime() != -1) {
// Prefer departure time, because if both exist departure deviations should be the ones propagated downstream
arrivalOrDepartureTime = stopTime.getDepartureTime();
} else {
arrivalOrDepartureTime = stopTime.getArrivalTime();
}
int deviation = (int) ((predictedTime - blockInstance.getServiceDate()) / 1000 - arrivalOrDepartureTime);
scheduleDeviations.put(index, (double) deviation);
}
}
tprIndexCounter++;
}
double[] scheduleTimes = new double[scheduleDeviations.size()];
double[] scheduleDeviationMus = new double[scheduleDeviations.size()];
double[] scheduleDeviationSigmas = new double[scheduleDeviations.size()];
int index = 0;
for (Map.Entry<Integer, Double> entry : scheduleDeviations.entrySet()) {
scheduleTimes[index] = entry.getKey();
scheduleDeviationMus[index] = entry.getValue();
index++;
}
ScheduleDeviationSamples samples = new ScheduleDeviationSamples(scheduleTimes, scheduleDeviationMus, scheduleDeviationSigmas);
location.setScheduleDeviations(samples);
}
} else {
if (scheduledLocation == null)
scheduledLocation = getScheduledBlockLocationForBlockInstance(blockInstance, targetTime);
}
/**
* Will be null in the following cases:
*
* 1) When the effective schedule time is beyond the last scheduled stop
* time for the block.
*
* 2) When the effective distance along block is outside the range of the
* block's shape.
*/
if (scheduledLocation == null)
return null;
// if we have route info, set the vehicleType
if (scheduledLocation.getActiveTrip() != null && scheduledLocation.getActiveTrip().getTrip() != null && scheduledLocation.getActiveTrip().getTrip().getRoute() != null) {
location.setVehicleType(EVehicleType.toEnum(scheduledLocation.getActiveTrip().getTrip().getRoute().getType()));
}
location.setInService(scheduledLocation.isInService());
location.setActiveTrip(scheduledLocation.getActiveTrip());
location.setLocation(scheduledLocation.getLocation());
location.setOrientation(scheduledLocation.getOrientation());
location.setScheduledDistanceAlongBlock(scheduledLocation.getDistanceAlongBlock());
location.setClosestStop(scheduledLocation.getClosestStop());
location.setClosestStopTimeOffset(scheduledLocation.getClosestStopTimeOffset());
location.setNextStop(scheduledLocation.getNextStop());
location.setNextStopTimeOffset(scheduledLocation.getNextStopTimeOffset());
location.setPreviousStop(scheduledLocation.getPreviousStop());
return location;
}
Aggregations