Search in sources :

Example 11 with FeedMessage

use of com.google.transit.realtime.GtfsRealtime.FeedMessage in project OpenTripPlanner by opentripplanner.

the class GtfsRealtimeAlertsUpdater method runPolling.

@Override
protected void runPolling() {
    try {
        InputStream data = HttpUtils.getData(url);
        if (data == null) {
            throw new RuntimeException("Failed to get data from url " + url);
        }
        final FeedMessage feed = FeedMessage.PARSER.parseFrom(data);
        long feedTimestamp = feed.getHeader().getTimestamp();
        if (feedTimestamp <= lastTimestamp) {
            LOG.info("Ignoring feed with an old timestamp.");
            return;
        }
        // Handle update in graph writer runnable
        updaterManager.execute(new GraphWriterRunnable() {

            @Override
            public void run(Graph graph) {
                updateHandler.update(feed);
            }
        });
        lastTimestamp = feedTimestamp;
    } catch (Exception e) {
        LOG.error("Error reading gtfs-realtime feed from " + url, e);
    }
}
Also used : FeedMessage(com.google.transit.realtime.GtfsRealtime.FeedMessage) GraphWriterRunnable(org.opentripplanner.updater.GraphWriterRunnable) Graph(org.opentripplanner.routing.graph.Graph) InputStream(java.io.InputStream)

Example 12 with FeedMessage

use of com.google.transit.realtime.GtfsRealtime.FeedMessage in project OpenTripPlanner by opentripplanner.

the class GtfsRealtimeFileTripUpdateSource method getUpdates.

@Override
public List<TripUpdate> getUpdates() {
    FeedMessage feedMessage = null;
    List<FeedEntity> feedEntityList = null;
    List<TripUpdate> updates = null;
    fullDataset = true;
    try {
        InputStream is = new FileInputStream(file);
        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<TripUpdate>(feedEntityList.size());
            for (FeedEntity feedEntity : feedEntityList) {
                if (feedEntity.hasTripUpdate())
                    updates.add(feedEntity.getTripUpdate());
            }
        }
    } catch (Exception e) {
        LOG.warn("Failed to parse gtfs-rt feed at " + file + ":", e);
    }
    return updates;
}
Also used : FeedMessage(com.google.transit.realtime.GtfsRealtime.FeedMessage) TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FeedEntity(com.google.transit.realtime.GtfsRealtime.FeedEntity) FileInputStream(java.io.FileInputStream)

Example 13 with FeedMessage

use of com.google.transit.realtime.GtfsRealtime.FeedMessage in project OpenTripPlanner by opentripplanner.

the class GtfsRealtimeHttpTripUpdateSource method getUpdates.

@Override
public List<TripUpdate> getUpdates() {
    FeedMessage feedMessage = null;
    List<FeedEntity> feedEntityList = null;
    List<TripUpdate> updates = null;
    fullDataset = true;
    try {
        InputStream is = HttpUtils.getData(url);
        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;
}
Also used : FeedMessage(com.google.transit.realtime.GtfsRealtime.FeedMessage) TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) InputStream(java.io.InputStream) FeedEntity(com.google.transit.realtime.GtfsRealtime.FeedEntity)

Example 14 with FeedMessage

use of com.google.transit.realtime.GtfsRealtime.FeedMessage in project onebusaway-application-modules by camsys.

the class AlertsFromRssResource method getAllDebug.

@Path("/service-alerts-debug")
@GET
@Produces("text/plain")
public Response getAllDebug() {
    FeedMessage serviceAlertFeed = _alertsService.getServlceAlertFeed();
    if (serviceAlertFeed == null) {
        _log.info("empty feed");
        return Response.ok().build();
    }
    // return the string representation of the protocol buffers
    Response response = Response.ok(serviceAlertFeed.toString()).build();
    return response;
}
Also used : Response(javax.ws.rs.core.Response) FeedMessage(com.google.transit.realtime.GtfsRealtime.FeedMessage) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 15 with FeedMessage

use of com.google.transit.realtime.GtfsRealtime.FeedMessage in project onebusaway-application-modules by camsys.

the class GtfsRtProxyAction method execute.

public String execute() {
    FeedMessage tu = getFeedMessage(getTripUpdatesPath());
    if (tu != null) {
        tripUpdates = GtfsRealtimeConversionLibrary.readTripUpdates(tu);
    }
    FeedMessage vp = getFeedMessage(getVehiclePositionsPath());
    if (vp != null) {
        vehiclePositions = GtfsRealtimeConversionLibrary.readVehiclePositions(vp);
    }
    return SUCCESS;
}
Also used : FeedMessage(com.google.transit.realtime.GtfsRealtime.FeedMessage)

Aggregations

FeedMessage (com.google.transit.realtime.GtfsRealtime.FeedMessage)24 FeedEntity (com.google.transit.realtime.GtfsRealtime.FeedEntity)8 InputStream (java.io.InputStream)6 ArrayList (java.util.ArrayList)6 TripUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate)4 Date (java.util.Date)4 Test (org.junit.Test)4 ResponseBean (org.onebusaway.api.model.ResponseBean)3 ListBean (org.onebusaway.transit_data.model.ListBean)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 TranslatedString (com.google.transit.realtime.GtfsRealtime.TranslatedString)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 List (java.util.List)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 Response (javax.ws.rs.core.Response)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)2 RouteBean (org.onebusaway.transit_data.model.RouteBean)2