org.opentripplanner.updater.vehicle_position.PollingVehiclePositionUpdater Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of otp Show documentation
Show all versions of otp Show documentation
The OpenTripPlanner multimodal journey planning system
package org.opentripplanner.updater.vehicle_position;
import com.google.transit.realtime.GtfsRealtime.VehiclePosition;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
import org.opentripplanner.routing.services.RealtimeVehiclePositionService;
import org.opentripplanner.transit.model.network.TripPattern;
import org.opentripplanner.transit.model.timetable.Trip;
import org.opentripplanner.transit.service.TransitModel;
import org.opentripplanner.updater.PollingGraphUpdater;
import org.opentripplanner.updater.WriteToGraphCallback;
import org.opentripplanner.util.lang.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Add vehicle positions to OTP patterns via a realtime source *
*
* rt.type = vehicle-positions
* rt.frequencySec = 60
* rt.url = http://host.tld/path
* rt.feedId = TA
*
*/
public class PollingVehiclePositionUpdater extends PollingGraphUpdater {
private static final Logger LOG = LoggerFactory.getLogger(PollingVehiclePositionUpdater.class);
/**
* Update streamer
*/
private final VehiclePositionSource vehiclePositionSource;
private final VehiclePositionPatternMatcher vehiclePositionPatternMatcher;
/**
* Parent update manager. Is used to execute graph writer runnables.
*/
private WriteToGraphCallback saveResultOnGraph;
public PollingVehiclePositionUpdater(
VehiclePositionsUpdaterParameters params,
RealtimeVehiclePositionService vehiclePositionService,
TransitModel transitModel
) {
super(params);
this.vehiclePositionSource = new GtfsRealtimeHttpVehiclePositionSource(params.url());
var index = transitModel.getTransitModelIndex();
this.vehiclePositionPatternMatcher =
new VehiclePositionPatternMatcher(
params.feedId(),
tripId -> index.getTripForId().get(tripId),
trip -> index.getPatternForTrip().get(trip),
(trip, date) -> getPatternIncludingRealtime(transitModel, trip, date),
vehiclePositionService,
transitModel.getTimeZone()
);
LOG.info(
"Creating vehicle position updater running every {} seconds : {}",
pollingPeriodSeconds(),
vehiclePositionSource
);
}
@Override
public void setGraphUpdaterManager(WriteToGraphCallback saveResultOnGraph) {
this.saveResultOnGraph = saveResultOnGraph;
}
/**
* Repeatedly makes blocking calls to an UpdateStreamer to retrieve new stop time updates, and
* applies those updates to the graph.
*/
@Override
public void runPolling() {
// Get update lists from update source
List updates = vehiclePositionSource.getPositions();
if (updates != null) {
// Handle updating trip positions via graph writer runnable
var runnable = new VehiclePositionUpdaterRunnable(updates, vehiclePositionPatternMatcher);
saveResultOnGraph.execute(runnable);
}
}
@Override
public String toString() {
return ToStringBuilder.of(this.getClass()).addObj("source", vehiclePositionSource).toString();
}
private static TripPattern getPatternIncludingRealtime(
TransitModel transitModel,
Trip trip,
LocalDate sd
) {
return Optional
.ofNullable(transitModel.getTimetableSnapshot())
.map(snapshot -> snapshot.getRealtimeAddedTripPattern(trip.getId(), sd))
.orElseGet(() -> transitModel.getTransitModelIndex().getPatternForTrip().get(trip));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy