org.opentripplanner.updater.stoptime.GtfsRealtimeHttpTripUpdateSource 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.stoptime;
import com.google.transit.realtime.GtfsRealtime;
import com.google.transit.realtime.GtfsRealtime.FeedEntity;
import com.google.transit.realtime.GtfsRealtime.FeedMessage;
import com.google.transit.realtime.GtfsRealtime.TripUpdate;
import org.opentripplanner.util.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
public class GtfsRealtimeHttpTripUpdateSource implements TripUpdateSource {
private static final Logger LOG =
LoggerFactory.getLogger(GtfsRealtimeHttpTripUpdateSource.class);
/**
* True iff the last list with updates represent all updates that are active right now, i.e. all
* previous updates should be disregarded
*/
private boolean fullDataset = true;
/**
* Feed id that is used to match trip ids in the TripUpdates
*/
private final String feedId;
private final String url;
public GtfsRealtimeHttpTripUpdateSource(Parameters config) {
this.feedId = config.getFeedId();
this.url = config.getUrl();
}
@Override
public List getUpdates() {
FeedMessage feedMessage = null;
List feedEntityList = null;
List updates = null;
fullDataset = true;
try {
InputStream is = HttpUtils.getData(
URI.create(url),
"Accept",
"application/x-google-protobuf, application/x-protobuf, application/protobuf, application/octet-stream, */*");
if (is != null) {
// Decode message
feedMessage = FeedMessage.PARSER.parseFrom(is);
feedEntityList = feedMessage.getEntityList();
// Change fullDataset value if this is an incremental update
if (feedMessage.hasHeader()
&& feedMessage.getHeader().hasIncrementality()
&& feedMessage.getHeader().getIncrementality()
.equals(GtfsRealtime.FeedHeader.Incrementality.DIFFERENTIAL)) {
fullDataset = false;
}
// Create List of TripUpdates
updates = new ArrayList<>(feedEntityList.size());
for (FeedEntity feedEntity : feedEntityList) {
if (feedEntity.hasTripUpdate()) updates.add(feedEntity.getTripUpdate());
}
}
} catch (Exception e) {
LOG.warn("Failed to parse gtfs-rt feed from " + url + ":", e);
}
return updates;
}
@Override
public boolean getFullDatasetValueOfLastUpdates() {
return fullDataset;
}
public String toString() {
return "GtfsRealtimeHttpUpdateStreamer(" + url + ")";
}
@Override
public String getFeedId() {
return this.feedId;
}
interface Parameters {
String getFeedId();
String getUrl();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy