Search in sources :

Example 6 with FeedEntity

use of com.google.transit.realtime.GtfsRealtime.FeedEntity 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 7 with FeedEntity

use of com.google.transit.realtime.GtfsRealtime.FeedEntity 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 8 with FeedEntity

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

the class AlertsUpdateHandler method update.

public void update(FeedMessage message) {
    alertPatchService.expire(patchIds);
    patchIds.clear();
    for (FeedEntity entity : message.getEntityList()) {
        if (!entity.hasAlert()) {
            continue;
        }
        GtfsRealtime.Alert alert = entity.getAlert();
        String id = entity.getId();
        handleAlert(id, alert);
    }
}
Also used : I18NString(org.opentripplanner.util.I18NString) TranslatedString(org.opentripplanner.util.TranslatedString) FeedEntity(com.google.transit.realtime.GtfsRealtime.FeedEntity) GtfsRealtime(com.google.transit.realtime.GtfsRealtime)

Example 9 with FeedEntity

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

the class FeedServiceImplTest method createAlert.

private FeedEntity createAlert(String alertId, String header, String desc, Alert.Cause cause, Alert.Effect effect, String url, long startTime, long endTime, String agency, String route, String stop) {
    Alert.Builder alertBldr = Alert.newBuilder();
    // Header
    Translation translation = Translation.newBuilder().setLanguage("en").setText(header).build();
    TranslatedString trnStr = TranslatedString.newBuilder().addTranslation(translation).build();
    alertBldr.setHeaderText(trnStr);
    // Description
    translation = Translation.newBuilder().setLanguage("en").setText(desc).build();
    trnStr = TranslatedString.newBuilder().addTranslation(translation).build();
    alertBldr.setDescriptionText(trnStr);
    // Cause
    alertBldr.setCause(cause);
    // Effect
    alertBldr.setEffect(effect);
    // URL
    translation = Translation.newBuilder().setLanguage("en").setText(url).build();
    trnStr = TranslatedString.newBuilder().addTranslation(translation).build();
    alertBldr.setUrl(trnStr);
    // Build TimeRangeEntity
    TimeRange timeRange = createTimeRange(startTime, endTime);
    alertBldr.addActivePeriod(timeRange);
    // Build EntitySelectorEntity
    EntitySelector entitySelector = createEntitySelector(agency, route, stop);
    alertBldr.addInformedEntity(entitySelector);
    FeedEntity.Builder alertEntity = FeedEntity.newBuilder();
    alertEntity.setId(alertId);
    alertEntity.setAlert(alertBldr.build());
    return alertEntity.build();
}
Also used : TranslatedString(com.google.transit.realtime.GtfsRealtime.TranslatedString) TimeRange(com.google.transit.realtime.GtfsRealtime.TimeRange) Translation(com.google.transit.realtime.GtfsRealtime.TranslatedString.Translation) EntitySelector(com.google.transit.realtime.GtfsRealtime.EntitySelector) Alert(com.google.transit.realtime.GtfsRealtime.Alert) FeedEntity(com.google.transit.realtime.GtfsRealtime.FeedEntity)

Example 10 with FeedEntity

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

the class FeedServiceImplTest method testReadAlerts.

@Test
public void testReadAlerts() {
    // Create GTFS Feed with service alerts
    FeedEntity alertEntityA = createAlert("alertA", TEST_1, DESC_1, CAUSE_1, EFFECT_1, URL_1, TIME_START_1, TIME_END_1, AGENCY_1, ROUTE_1, STOP_1);
    FeedEntity alertEntityB = createAlert("alertB", TEST_2, DESC_2, CAUSE_2, EFFECT_2, URL_2, TIME_START_2, TIME_END_2, AGENCY_2, ROUTE_2, STOP_2);
    FeedEntity alertEntityC = createAlert("alertC", TEST_3, DESC_3, CAUSE_3, EFFECT_3, URL_3, TIME_START_3, TIME_END_3, AGENCY_3, ROUTE_3, STOP_3);
    // Create FeedMessage
    FeedMessage.Builder alerts = createFeed();
    alerts.addEntity(alertEntityA);
    alerts.addEntity(alertEntityB);
    alerts.addEntity(alertEntityC);
    FeedMessage alert = alerts.build();
    _feedService.readAlerts(alert, _entitySource);
    Collection<AlertModel> alertsFromDB = null;
    // the AlertThread, which actually writes to the DB.
    try {
        TimeUnit.SECONDS.sleep(15);
    } catch (Exception ignoredEx) {
    }
    // Get data that was persisted to the database
    try {
        alertsFromDB = (Collection<AlertModel>) _template.find("from AlertModel");
    } catch (Exception ex) {
        ex.getMessage();
        _log.info("find failed: " + ex.getMessage());
    }
    // Check persisted data against the original value.
    _log.info("results size: " + alertsFromDB.size());
    assertEquals(3, alertsFromDB.size());
    for (AlertModel alertFromDB : alertsFromDB) {
        String header = alertFromDB.getHeaderText();
        String desc = alertFromDB.getDescriptionText();
        String cause = alertFromDB.getCause();
        String effect = alertFromDB.getEffect();
        String url = alertFromDB.getUrl();
        long timeStart = 0L;
        long timeEnd = 0L;
        List<TimeRangeModel> timeRanges = alertFromDB.getTimeRanges();
        for (TimeRangeModel tr : timeRanges) {
            timeStart = tr.getStart();
            timeEnd = tr.getEnd();
        }
        String agency = "";
        String route = "";
        String stop = "";
        List<EntitySelectorModel> entitySelectors = alertFromDB.getEntitySelectors();
        for (EntitySelectorModel es : entitySelectors) {
            agency = es.getAgencyId();
            route = es.getRouteId();
            stop = es.getStopId();
        }
        if (header.equals(TEST_1)) {
            assertEquals(TEST_1, header);
            assertEquals(DESC_1, desc);
            assertEquals(CAUSE_1.toString(), cause);
            assertEquals(EFFECT_1.toString(), effect);
            assertEquals(URL_1.toString(), url);
            assertEquals(TIME_START_1, timeStart);
            assertEquals(TIME_END_1, timeEnd);
            assertEquals(AGENCY_1, agency);
            assertEquals(ROUTE_1, route);
            assertEquals(STOP_1, stop);
        } else if (header.equals(TEST_2)) {
            assertEquals(TEST_2, header);
            assertEquals(DESC_2, desc);
            assertEquals(CAUSE_2.toString(), cause);
            assertEquals(EFFECT_2.toString(), effect);
            assertEquals(URL_2.toString(), url);
            assertEquals(TIME_START_2, timeStart);
            assertEquals(TIME_END_2, timeEnd);
            assertEquals(AGENCY_2, agency);
            // Verify that agency has
            assertEquals(AGENCY_2 + "_" + ROUTE_2, route);
            // been prepended to
            // route.
            assertEquals(STOP_2, stop);
        } else if (header.equals(TEST_3)) {
            assertEquals(TEST_3, header);
            assertEquals(DESC_3, desc);
            assertEquals(CAUSE_3.toString(), cause);
            assertEquals(EFFECT_3.toString(), effect);
            assertEquals(URL_3.toString(), url);
            assertEquals(TIME_START_3, timeStart);
            assertEquals(TIME_END_3, timeEnd);
            assertEquals(AGENCY_3, agency);
            assertEquals(ROUTE_3, route);
            // Verify that agency has
            assertEquals(AGENCY_3 + "_" + STOP_3, stop);
        // been prepended to stop.
        }
    }
}
Also used : FeedMessage(com.google.transit.realtime.GtfsRealtime.FeedMessage) AlertModel(org.onebusaway.gtfs_realtime.model.AlertModel) TimeRangeModel(org.onebusaway.gtfs_realtime.model.TimeRangeModel) TranslatedString(com.google.transit.realtime.GtfsRealtime.TranslatedString) FeedEntity(com.google.transit.realtime.GtfsRealtime.FeedEntity) EntitySelectorModel(org.onebusaway.gtfs_realtime.model.EntitySelectorModel) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

FeedEntity (com.google.transit.realtime.GtfsRealtime.FeedEntity)13 FeedMessage (com.google.transit.realtime.GtfsRealtime.FeedMessage)8 TripUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate)6 ArrayList (java.util.ArrayList)6 Test (org.junit.Test)5 Alert (com.google.transit.realtime.GtfsRealtime.Alert)3 TranslatedString (com.google.transit.realtime.GtfsRealtime.TranslatedString)3 InputStream (java.io.InputStream)3 Date (java.util.Date)3 ResponseBean (org.onebusaway.api.model.ResponseBean)3 ListBean (org.onebusaway.transit_data.model.ListBean)3 EntitySelector (com.google.transit.realtime.GtfsRealtime.EntitySelector)2 TimeRange (com.google.transit.realtime.GtfsRealtime.TimeRange)2 Translation (com.google.transit.realtime.GtfsRealtime.TranslatedString.Translation)2 VehiclePosition (com.google.transit.realtime.GtfsRealtime.VehiclePosition)2 FileInputStream (java.io.FileInputStream)2 RouteBean (org.onebusaway.transit_data.model.RouteBean)2 VehicleStatusBean (org.onebusaway.transit_data.model.VehicleStatusBean)2 TripBean (org.onebusaway.transit_data.model.trips.TripBean)2 TripStatusBean (org.onebusaway.transit_data.model.trips.TripStatusBean)2