use of org.onebusaway.transit_data.model.blocks.BlockStopTimeBean in project onebusaway-application-modules by camsys.
the class SiriSupport method fillMonitoredVehicleJourney.
/**
* NOTE: The tripDetails bean here may not be for the trip the vehicle is currently on
* in the case of A-D for stop!
*/
@SuppressWarnings("unused")
public static void fillMonitoredVehicleJourney(MonitoredVehicleJourneyStructure monitoredVehicleJourney, TripBean framedJourneyTripBean, TripStatusBean currentVehicleTripStatus, StopBean monitoredCallStopBean, OnwardCallsMode onwardCallsMode, PresentationService presentationService, TransitDataService transitDataService, int maximumOnwardCalls, List<TimepointPredictionRecord> stopLevelPredictions, boolean hasRealtimeData, long responseTimestamp, boolean showRawLocation) {
BlockInstanceBean blockInstance = transitDataService.getBlockInstance(currentVehicleTripStatus.getActiveTrip().getBlockId(), currentVehicleTripStatus.getServiceDate());
List<BlockTripBean> blockTrips = blockInstance.getBlockConfiguration().getTrips();
if (monitoredCallStopBean == null) {
monitoredCallStopBean = currentVehicleTripStatus.getNextStop();
}
// ///////////
LineRefStructure lineRef = new LineRefStructure();
lineRef.setValue(framedJourneyTripBean.getRoute().getId());
monitoredVehicleJourney.setLineRef(lineRef);
OperatorRefStructure operatorRef = new OperatorRefStructure();
operatorRef.setValue(framedJourneyTripBean.getRoute().getId().split("_")[0]);
monitoredVehicleJourney.setOperatorRef(operatorRef);
DirectionRefStructure directionRef = new DirectionRefStructure();
directionRef.setValue(framedJourneyTripBean.getDirectionId());
monitoredVehicleJourney.setDirectionRef(directionRef);
NaturalLanguageStringStructure routeShortName = new NaturalLanguageStringStructure();
String shortName = framedJourneyTripBean.getRoute().getShortName();
if (shortName == null) {
shortName = framedJourneyTripBean.getRoute().getId().split("_")[1];
}
if (!isBlank(currentVehicleTripStatus.getActiveTrip().getRouteShortName())) {
// look for an override like an express desginator
routeShortName.setValue(currentVehicleTripStatus.getActiveTrip().getRouteShortName());
} else {
routeShortName.setValue(shortName);
}
monitoredVehicleJourney.setPublishedLineName(routeShortName);
JourneyPatternRefStructure journeyPattern = new JourneyPatternRefStructure();
journeyPattern.setValue(framedJourneyTripBean.getShapeId());
monitoredVehicleJourney.setJourneyPatternRef(journeyPattern);
NaturalLanguageStringStructure headsign = new NaturalLanguageStringStructure();
headsign.setValue(framedJourneyTripBean.getTripHeadsign());
monitoredVehicleJourney.setDestinationName(headsign);
VehicleRefStructure vehicleRef = new VehicleRefStructure();
if (currentVehicleTripStatus.getVehicleId() == null) {
String tripId = framedJourneyTripBean.getId();
String blockId = framedJourneyTripBean.getBlockId();
String directionId = framedJourneyTripBean.getDirectionId();
String vehicleIdHash = Integer.toString((tripId + blockId + directionId).hashCode());
String agencyName = tripId.split("_")[0];
String vehicleId = agencyName + "_" + vehicleIdHash;
vehicleRef.setValue(vehicleId);
} else {
vehicleRef.setValue(currentVehicleTripStatus.getVehicleId());
}
monitoredVehicleJourney.setVehicleRef(vehicleRef);
monitoredVehicleJourney.getVehicleMode().add(toVehicleMode(currentVehicleTripStatus.getVehicleType()));
monitoredVehicleJourney.setMonitored(currentVehicleTripStatus.isPredicted());
monitoredVehicleJourney.setBearing((float) currentVehicleTripStatus.getOrientation());
monitoredVehicleJourney.setProgressRate(getProgressRateForPhaseAndStatus(currentVehicleTripStatus.getStatus(), currentVehicleTripStatus.getPhase()));
// origin-destination
for (int i = 0; i < blockTrips.size(); i++) {
BlockTripBean blockTrip = blockTrips.get(i);
if (blockTrip.getTrip().getId().equals(framedJourneyTripBean.getId())) {
List<BlockStopTimeBean> stops = blockTrip.getBlockStopTimes();
JourneyPlaceRefStructure origin = new JourneyPlaceRefStructure();
origin.setValue(stops.get(0).getStopTime().getStop().getId());
monitoredVehicleJourney.setOriginRef(origin);
StopBean lastStop = stops.get(stops.size() - 1).getStopTime().getStop();
DestinationRefStructure dest = new DestinationRefStructure();
dest.setValue(lastStop.getId());
monitoredVehicleJourney.setDestinationRef(dest);
break;
}
}
// framed journey
FramedVehicleJourneyRefStructure framedJourney = new FramedVehicleJourneyRefStructure();
DataFrameRefStructure dataFrame = new DataFrameRefStructure();
dataFrame.setValue(String.format("%1$tY-%1$tm-%1$td", currentVehicleTripStatus.getServiceDate()));
framedJourney.setDataFrameRef(dataFrame);
framedJourney.setDatedVehicleJourneyRef(framedJourneyTripBean.getId());
monitoredVehicleJourney.setFramedVehicleJourneyRef(framedJourney);
// location
// if vehicle is detected to be on detour, use actual lat/lon, not snapped location.
LocationStructure location = new LocationStructure();
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(6);
// if we want to show the raw location AND we have realtime or if its on detour, show actual location
if ((showRawLocation && currentVehicleTripStatus.getLastKnownLocation() != null) || (presentationService.isOnDetour(currentVehicleTripStatus))) {
location.setLatitude(new BigDecimal(df.format(currentVehicleTripStatus.getLastKnownLocation().getLat())));
location.setLongitude(new BigDecimal(df.format(currentVehicleTripStatus.getLastKnownLocation().getLon())));
} else {
// show snapped location
if (currentVehicleTripStatus.getLocation() != null) {
location.setLatitude(new BigDecimal(df.format(currentVehicleTripStatus.getLocation().getLat())));
location.setLongitude(new BigDecimal(df.format(currentVehicleTripStatus.getLocation().getLon())));
}
}
monitoredVehicleJourney.setVehicleLocation(location);
// progress status
List<String> progressStatuses = new ArrayList<String>();
if (presentationService.isInLayover(currentVehicleTripStatus)) {
progressStatuses.add("layover");
}
// "prevTrip" really means not on the framedvehiclejourney trip
if (!framedJourneyTripBean.getId().equals(currentVehicleTripStatus.getActiveTrip().getId())) {
progressStatuses.add("prevTrip");
}
if (!progressStatuses.isEmpty()) {
NaturalLanguageStringStructure progressStatus = new NaturalLanguageStringStructure();
progressStatus.setValue(StringUtils.join(progressStatuses, ","));
monitoredVehicleJourney.setProgressStatus(progressStatus);
}
// block ref
if (presentationService.isBlockLevelInference(currentVehicleTripStatus)) {
BlockRefStructure blockRef = new BlockRefStructure();
blockRef.setValue(framedJourneyTripBean.getBlockId());
monitoredVehicleJourney.setBlockRef(blockRef);
}
// scheduled depature time
if (presentationService.isBlockLevelInference(currentVehicleTripStatus) && (presentationService.isInLayover(currentVehicleTripStatus) || !framedJourneyTripBean.getId().equals(currentVehicleTripStatus.getActiveTrip().getId()))) {
BlockStopTimeBean originDepartureStopTime = null;
for (int t = 0; t < blockTrips.size(); t++) {
BlockTripBean thisTrip = blockTrips.get(t);
BlockTripBean nextTrip = null;
if (t + 1 < blockTrips.size()) {
nextTrip = blockTrips.get(t + 1);
}
if (thisTrip.getTrip().getId().equals(currentVehicleTripStatus.getActiveTrip().getId())) {
// just started new trip
if (currentVehicleTripStatus.getDistanceAlongTrip() < (0.5 * currentVehicleTripStatus.getTotalDistanceAlongTrip())) {
originDepartureStopTime = thisTrip.getBlockStopTimes().get(0);
// at end of previous trip
} else {
if (nextTrip != null) {
originDepartureStopTime = nextTrip.getBlockStopTimes().get(0);
}
}
break;
}
}
if (originDepartureStopTime != null) {
Date departureTime = new Date(currentVehicleTripStatus.getServiceDate() + (originDepartureStopTime.getStopTime().getDepartureTime() * 1000));
monitoredVehicleJourney.setOriginAimedDepartureTime(departureTime);
}
}
Map<String, TimepointPredictionRecord> stopIdToPredictionRecordMap = new HashMap<String, TimepointPredictionRecord>();
// (build map of vehicle IDs to TPRs)
if (stopLevelPredictions != null) {
for (TimepointPredictionRecord tpr : stopLevelPredictions) {
stopIdToPredictionRecordMap.put(AgencyAndId.convertToString(tpr.getTimepointId()), tpr);
}
}
// monitored call
if (!presentationService.isOnDetour(currentVehicleTripStatus))
fillMonitoredCall(monitoredVehicleJourney, blockInstance, currentVehicleTripStatus, monitoredCallStopBean, presentationService, transitDataService, stopIdToPredictionRecordMap, hasRealtimeData, responseTimestamp);
// onward calls
if (!presentationService.isOnDetour(currentVehicleTripStatus))
fillOnwardCalls(monitoredVehicleJourney, blockInstance, framedJourneyTripBean, currentVehicleTripStatus, onwardCallsMode, presentationService, transitDataService, stopIdToPredictionRecordMap, maximumOnwardCalls, hasRealtimeData, responseTimestamp);
// situations
fillSituations(monitoredVehicleJourney, currentVehicleTripStatus);
return;
}
use of org.onebusaway.transit_data.model.blocks.BlockStopTimeBean in project onebusaway-application-modules by camsys.
the class SiriSupport method fillMonitoredCall.
private static void fillMonitoredCall(MonitoredVehicleJourneyStructure monitoredVehicleJourney, BlockInstanceBean blockInstance, TripStatusBean tripStatus, StopBean monitoredCallStopBean, PresentationService presentationService, TransitDataService transitDataService, Map<String, TimepointPredictionRecord> stopLevelPredictions, boolean hasRealtimeData, long responseTimestamp) {
List<BlockTripBean> blockTrips = blockInstance.getBlockConfiguration().getTrips();
double distanceOfVehicleAlongBlock = 0;
int blockTripStopsAfterTheVehicle = 0;
boolean foundActiveTrip = false;
for (int i = 0; i < blockTrips.size(); i++) {
BlockTripBean blockTrip = blockTrips.get(i);
if (!foundActiveTrip) {
if (tripStatus.getActiveTrip().getId().equals(blockTrip.getTrip().getId())) {
double distanceAlongTrip = tripStatus.getDistanceAlongTrip();
if (!hasRealtimeData) {
distanceAlongTrip = tripStatus.getScheduledDistanceAlongTrip();
}
distanceOfVehicleAlongBlock += distanceAlongTrip;
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;
}
}
HashMap<String, Integer> visitNumberForStopMap = new HashMap<String, Integer>();
for (BlockStopTimeBean stopTime : blockTrip.getBlockStopTimes()) {
int visitNumber = getVisitNumber(visitNumberForStopMap, stopTime.getStopTime().getStop());
// on future trips, count always.
if (tripStatus.getActiveTrip().getId().equals(blockTrip.getTrip().getId())) {
if (stopTime.getDistanceAlongBlock() >= distanceOfVehicleAlongBlock) {
blockTripStopsAfterTheVehicle++;
} else {
// bus has passed this stop already--no need to go further
continue;
}
// future trip--bus hasn't reached this trip yet, so count all stops
} else {
blockTripStopsAfterTheVehicle++;
}
// monitored call
if (stopTime.getStopTime().getStop().getId().equals(monitoredCallStopBean.getId())) {
if (!presentationService.isOnDetour(tripStatus)) {
monitoredVehicleJourney.setMonitoredCall(getMonitoredCallStructure(stopTime.getStopTime().getStop(), presentationService, stopTime.getDistanceAlongBlock() - blockTrip.getDistanceAlongBlock(), stopTime.getDistanceAlongBlock() - distanceOfVehicleAlongBlock, visitNumber, blockTripStopsAfterTheVehicle - 1, stopLevelPredictions.get(monitoredCallStopBean.getId()), hasRealtimeData, responseTimestamp, tripStatus.getServiceDate() + (stopTime.getStopTime().getArrivalTime() * 1000)));
}
// we found our monitored call--stop
return;
}
}
}
}
use of org.onebusaway.transit_data.model.blocks.BlockStopTimeBean 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.blocks.BlockStopTimeBean in project onebusaway-application-modules by camsys.
the class SiriSupportV2 method fillMonitoredCall.
private static void fillMonitoredCall(MonitoredVehicleJourneyStructure monitoredVehicleJourney, BlockInstanceBean blockInstance, TripStatusBean tripStatus, StopBean monitoredCallStopBean, PresentationService presentationService, TransitDataService transitDataService, Map<String, TimepointPredictionRecord> stopLevelPredictions, boolean hasRealtimeData, DetailLevel detailLevel, long responseTimestamp) {
List<BlockTripBean> blockTrips = blockInstance.getBlockConfiguration().getTrips();
double distanceOfVehicleAlongBlock = 0;
int blockTripStopsAfterTheVehicle = 0;
boolean foundActiveTrip = false;
for (int i = 0; i < blockTrips.size(); i++) {
BlockTripBean blockTrip = blockTrips.get(i);
if (!foundActiveTrip) {
if (tripStatus.getActiveTrip().getId().equals(blockTrip.getTrip().getId())) {
double distanceAlongTrip = tripStatus.getDistanceAlongTrip();
if (!hasRealtimeData) {
distanceAlongTrip = tripStatus.getScheduledDistanceAlongTrip();
}
distanceOfVehicleAlongBlock += distanceAlongTrip;
foundActiveTrip = true;
} else {
// next.
if (i + 1 < blockTrips.size()) {
distanceOfVehicleAlongBlock = blockTrips.get(i + 1).getDistanceAlongBlock();
}
// further
continue;
}
}
HashMap<String, Integer> visitNumberForStopMap = new HashMap<String, Integer>();
for (BlockStopTimeBean stopTime : blockTrip.getBlockStopTimes()) {
int visitNumber = getVisitNumber(visitNumberForStopMap, stopTime.getStopTime().getStop());
// on future trips, count always.
if (tripStatus.getActiveTrip().getId().equals(blockTrip.getTrip().getId())) {
if (stopTime.getDistanceAlongBlock() >= distanceOfVehicleAlongBlock) {
blockTripStopsAfterTheVehicle++;
} else {
// further
continue;
}
// future trip--bus hasn't reached this trip yet, so count
// all stops
} else {
blockTripStopsAfterTheVehicle++;
}
// monitored call
if (stopTime.getStopTime().getStop().getId().equals(monitoredCallStopBean.getId())) {
if (!presentationService.isOnDetour(tripStatus)) {
monitoredVehicleJourney.setMonitoredCall(getMonitoredCallStructure(stopTime.getStopTime().getStop(), presentationService, stopTime.getDistanceAlongBlock() - blockTrip.getDistanceAlongBlock(), stopTime.getDistanceAlongBlock() - distanceOfVehicleAlongBlock, visitNumber, blockTripStopsAfterTheVehicle - 1, stopLevelPredictions.get(stopTime.getStopTime().getStop().getId()), detailLevel, responseTimestamp));
}
// we found our monitored call--stop
return;
}
}
}
}
Aggregations