Search in sources :

Example 1 with EntitySelectorModel

use of org.onebusaway.gtfs_realtime.model.EntitySelectorModel in project onebusaway-application-modules by camsys.

the class AlertConvertor method readFeedEntity.

@Override
public AlertModel readFeedEntity(FeedEntity entity, long timestamp) {
    if (entity.hasAlert()) {
        AlertModel alrt = new AlertModel();
        alrt.setTimestamp(new Date(timestamp));
        Alert alert = entity.getAlert();
        for (TimeRange tr : alert.getActivePeriodList()) {
            TimeRangeModel timeRange = readTimeRange(tr);
            if (timeRange != null) {
                timeRange.setAlert(alrt);
                alrt.addTimeRangeModel(timeRange);
            }
        }
        for (EntitySelector es : alert.getInformedEntityList()) {
            EntitySelectorModel entitySelector = readEntitySelector(es);
            if (entitySelector != null) {
                entitySelector.setAlert(alrt);
                alrt.addEntitySelectorModel(entitySelector);
            }
        }
        if (alert.hasCause()) {
            String cause = alert.getCause().getValueDescriptor().getFullName();
            cause = cause.substring(cause.lastIndexOf('.') + 1);
            alrt.setCause(cause);
        }
        if (alert.hasEffect()) {
            String effect = alert.getEffect().getValueDescriptor().getFullName();
            effect = effect.substring(effect.lastIndexOf('.') + 1);
            alrt.setEffect(effect);
        }
        if (alert.hasUrl()) {
            alrt.setUrl(alert.getUrl().getTranslation(0).getText());
        }
        if (alert.hasHeaderText()) {
            alrt.setHeaderText(alert.getHeaderText().getTranslation(0).getText());
        }
        if (alert.hasDescriptionText()) {
            alrt.setDescriptionText(alert.getDescriptionText().getTranslation(0).getText());
        }
        return alrt;
    }
    return null;
}
Also used : TimeRange(com.google.transit.realtime.GtfsRealtime.TimeRange) EntitySelector(com.google.transit.realtime.GtfsRealtime.EntitySelector) AlertModel(org.onebusaway.gtfs_realtime.model.AlertModel) Alert(com.google.transit.realtime.GtfsRealtime.Alert) TimeRangeModel(org.onebusaway.gtfs_realtime.model.TimeRangeModel) EntitySelectorModel(org.onebusaway.gtfs_realtime.model.EntitySelectorModel) Date(java.util.Date)

Example 2 with EntitySelectorModel

use of org.onebusaway.gtfs_realtime.model.EntitySelectorModel in project onebusaway-application-modules by camsys.

the class FeedServiceImpl method readAlerts.

@Override
public List<AlertModel> readAlerts(FeedMessage alerts, GtfsRealtimeEntitySource entitySource) {
    List<AlertModel> updates = GtfsRealtimeConversionLibrary.readAlerts(alerts);
    for (AlertModel alert : updates) {
        // Update EntitySelectors to include the agency
        for (EntitySelectorModel esm : alert.getEntitySelectors()) {
            String agencyId = getAgencyId(entitySource);
            if (StringUtils.isEmpty(agencyId) && esm.getAgencyId() != null && esm.getAgencyId().length() > 0) {
                agencyId = esm.getAgencyId();
            }
            addAgencyIdToTripId(esm, agencyId, entitySource);
            addAgencyIdToRouteId(esm, agencyId, entitySource);
            addAgencyIdToStopId(esm, agencyId, entitySource);
        }
        _persistor.persist(alert);
    }
    return updates;
}
Also used : AlertModel(org.onebusaway.gtfs_realtime.model.AlertModel) EntitySelectorModel(org.onebusaway.gtfs_realtime.model.EntitySelectorModel)

Example 3 with EntitySelectorModel

use of org.onebusaway.gtfs_realtime.model.EntitySelectorModel 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

AlertModel (org.onebusaway.gtfs_realtime.model.AlertModel)3 EntitySelectorModel (org.onebusaway.gtfs_realtime.model.EntitySelectorModel)3 TimeRangeModel (org.onebusaway.gtfs_realtime.model.TimeRangeModel)2 Alert (com.google.transit.realtime.GtfsRealtime.Alert)1 EntitySelector (com.google.transit.realtime.GtfsRealtime.EntitySelector)1 FeedEntity (com.google.transit.realtime.GtfsRealtime.FeedEntity)1 FeedMessage (com.google.transit.realtime.GtfsRealtime.FeedMessage)1 TimeRange (com.google.transit.realtime.GtfsRealtime.TimeRange)1 TranslatedString (com.google.transit.realtime.GtfsRealtime.TranslatedString)1 IOException (java.io.IOException)1 Date (java.util.Date)1 Test (org.junit.Test)1