Search in sources :

Example 1 with TimeRange

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

the class AlertsUpdateHandler method handleAlert.

private void handleAlert(String id, GtfsRealtime.Alert alert) {
    Alert alertText = new Alert();
    alertText.alertDescriptionText = deBuffer(alert.getDescriptionText());
    alertText.alertHeaderText = deBuffer(alert.getHeaderText());
    alertText.alertUrl = deBuffer(alert.getUrl());
    ArrayList<TimePeriod> periods = new ArrayList<TimePeriod>();
    if (alert.getActivePeriodCount() > 0) {
        long bestStartTime = Long.MAX_VALUE;
        long lastEndTime = Long.MIN_VALUE;
        for (TimeRange activePeriod : alert.getActivePeriodList()) {
            final long realStart = activePeriod.hasStart() ? activePeriod.getStart() : 0;
            final long start = activePeriod.hasStart() ? realStart - earlyStart : 0;
            if (realStart > 0 && realStart < bestStartTime) {
                bestStartTime = realStart;
            }
            final long end = activePeriod.hasEnd() ? activePeriod.getEnd() : Long.MAX_VALUE;
            if (end > lastEndTime) {
                lastEndTime = end;
            }
            periods.add(new TimePeriod(start, end));
        }
        if (bestStartTime != Long.MAX_VALUE) {
            alertText.effectiveStartDate = new Date(bestStartTime * 1000);
        }
        if (lastEndTime != Long.MIN_VALUE) {
            alertText.effectiveEndDate = new Date(lastEndTime * 1000);
        }
    } else {
        // Per the GTFS-rt spec, if an alert has no TimeRanges, than it should always be shown.
        periods.add(new TimePeriod(0, Long.MAX_VALUE));
    }
    for (EntitySelector informed : alert.getInformedEntityList()) {
        if (fuzzyTripMatcher != null && informed.hasTrip()) {
            TripDescriptor trip = fuzzyTripMatcher.match(feedId, informed.getTrip());
            informed = informed.toBuilder().setTrip(trip).build();
        }
        String patchId = createId(id, informed);
        String routeId = null;
        if (informed.hasRouteId()) {
            routeId = informed.getRouteId();
        }
        int direction;
        if (informed.hasTrip() && informed.getTrip().hasDirectionId()) {
            direction = informed.getTrip().getDirectionId();
        } else {
            direction = -1;
        }
        // TODO: The other elements of a TripDescriptor are ignored...
        String tripId = null;
        if (informed.hasTrip() && informed.getTrip().hasTripId()) {
            tripId = informed.getTrip().getTripId();
        }
        String stopId = null;
        if (informed.hasStopId()) {
            stopId = informed.getStopId();
        }
        String agencyId = informed.getAgencyId();
        if (informed.hasAgencyId()) {
            agencyId = informed.getAgencyId().intern();
        }
        AlertPatch patch = new AlertPatch();
        patch.setFeedId(feedId);
        if (routeId != null) {
            patch.setRoute(new AgencyAndId(feedId, routeId));
            // Makes no sense to set direction if we don't have a route
            if (direction != -1) {
                patch.setDirectionId(direction);
            }
        }
        if (tripId != null) {
            patch.setTrip(new AgencyAndId(feedId, tripId));
        }
        if (stopId != null) {
            patch.setStop(new AgencyAndId(feedId, stopId));
        }
        if (agencyId != null && routeId == null && tripId == null && stopId == null) {
            patch.setAgencyId(agencyId);
        }
        patch.setTimePeriods(periods);
        patch.setAlert(alertText);
        patch.setId(patchId);
        patchIds.add(patchId);
        alertPatchService.apply(patch);
    }
}
Also used : EntitySelector(com.google.transit.realtime.GtfsRealtime.EntitySelector) AlertPatch(org.opentripplanner.routing.alertpatch.AlertPatch) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) TimePeriod(org.opentripplanner.routing.alertpatch.TimePeriod) I18NString(org.opentripplanner.util.I18NString) TranslatedString(org.opentripplanner.util.TranslatedString) TimeRange(com.google.transit.realtime.GtfsRealtime.TimeRange) TripDescriptor(com.google.transit.realtime.GtfsRealtime.TripDescriptor) Alert(org.opentripplanner.routing.alertpatch.Alert)

Example 2 with TimeRange

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

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

the class AlertsForAgencyActionTest method test.

@Test
public void test() {
    long now = System.currentTimeMillis();
    List<ServiceAlertBean> alerts = new ArrayList<ServiceAlertBean>();
    {
        ServiceAlertBean alert = new ServiceAlertBean();
        alerts.add(alert);
        TimeRangeBean range = new TimeRangeBean(1234 * 1000, 5678 * 1000);
        alert.setActiveWindows(Arrays.asList(range));
        SituationAffectsBean affects = new SituationAffectsBean();
        affects.setAgencyId("1");
        affects.setRouteId("1_r0");
        affects.setStopId("1_s0");
        affects.setTripId("1_t0");
        SituationAffectsBean alsoAffects = new SituationAffectsBean();
        alsoAffects.setAgencyId("2");
        alert.setAllAffects(Arrays.asList(affects, alsoAffects));
        alert.setSummaries(Arrays.asList(new NaturalLanguageStringBean("Name", "en"), new NaturalLanguageStringBean("Nombre", "es")));
        alert.setDescriptions(Arrays.asList(new NaturalLanguageStringBean("Description", "en"), new NaturalLanguageStringBean("DescripciĆ³n", "es")));
    }
    {
        ServiceAlertBean alert = new ServiceAlertBean();
        alerts.add(alert);
        TimeRangeBean range = new TimeRangeBean(5678 * 1000, 1234 * 1000);
        alert.setActiveWindows(Arrays.asList(range));
        SituationAffectsBean affects = new SituationAffectsBean();
        affects.setAgencyId("2");
        affects.setRouteId("1_r1");
        affects.setStopId("1_s1");
        affects.setTripId("1_t1");
        alert.setAllAffects(Arrays.asList(affects));
        alert.setSummaries(Arrays.asList(new NaturalLanguageStringBean("Name", "en")));
        alert.setDescriptions(Arrays.asList(new NaturalLanguageStringBean("Description", "en")));
    }
    ListBean<ServiceAlertBean> bean = new ListBean<ServiceAlertBean>();
    bean.setList(alerts);
    Mockito.when(_service.getAllServiceAlertsForAgencyId("1")).thenReturn(bean);
    _action.setId("1");
    _action.setTime(new Date(now));
    _action.show();
    ResponseBean model = _action.getModel();
    FeedMessage feed = (FeedMessage) model.getData();
    assertEquals(now / 1000, feed.getHeader().getTimestamp());
    assertEquals(2, feed.getEntityCount());
    {
        FeedEntity entity = feed.getEntity(0);
        assertEquals("1", entity.getId());
        Alert alert = entity.getAlert();
        assertEquals(1, alert.getActivePeriodCount());
        TimeRange range = alert.getActivePeriod(0);
        assertEquals(1234, range.getStart());
        assertEquals(5678, range.getEnd());
        assertEquals(2, alert.getInformedEntityCount());
        {
            EntitySelector affects = alert.getInformedEntity(0);
            assertEquals("1", affects.getAgencyId());
            assertEquals("r0", affects.getRouteId());
            assertEquals("t0", affects.getTrip().getTripId());
            assertEquals("s0", affects.getStopId());
        }
        {
            EntitySelector affects = alert.getInformedEntity(1);
            assertEquals("2", affects.getAgencyId());
        }
        TranslatedString header = alert.getHeaderText();
        assertEquals(2, header.getTranslationCount());
        {
            Translation translation = header.getTranslation(0);
            assertEquals("Name", translation.getText());
            assertEquals("en", translation.getLanguage());
        }
        {
            Translation translation = header.getTranslation(1);
            assertEquals("Nombre", translation.getText());
            assertEquals("es", translation.getLanguage());
        }
        TranslatedString description = alert.getDescriptionText();
        assertEquals(2, description.getTranslationCount());
        {
            Translation translation = description.getTranslation(0);
            assertEquals("Description", translation.getText());
            assertEquals("en", translation.getLanguage());
        }
        {
            Translation translation = description.getTranslation(1);
            assertEquals("DescripciĆ³n", translation.getText());
            assertEquals("es", translation.getLanguage());
        }
    }
    {
        FeedEntity entity = feed.getEntity(1);
        assertEquals("2", entity.getId());
        Alert alert = entity.getAlert();
        assertEquals(1, alert.getActivePeriodCount());
        TimeRange range = alert.getActivePeriod(0);
        assertEquals(5678, range.getStart());
        assertEquals(1234, range.getEnd());
        assertEquals(1, alert.getInformedEntityCount());
        {
            EntitySelector affects = alert.getInformedEntity(0);
            assertEquals("2", affects.getAgencyId());
            assertEquals("r1", affects.getRouteId());
            assertEquals("t1", affects.getTrip().getTripId());
            assertEquals("s1", affects.getStopId());
        }
    }
}
Also used : TranslatedString(com.google.transit.realtime.GtfsRealtime.TranslatedString) EntitySelector(com.google.transit.realtime.GtfsRealtime.EntitySelector) Translation(com.google.transit.realtime.GtfsRealtime.TranslatedString.Translation) ArrayList(java.util.ArrayList) ListBean(org.onebusaway.transit_data.model.ListBean) Date(java.util.Date) TimeRangeBean(org.onebusaway.transit_data.model.service_alerts.TimeRangeBean) TimeRange(com.google.transit.realtime.GtfsRealtime.TimeRange) FeedMessage(com.google.transit.realtime.GtfsRealtime.FeedMessage) SituationAffectsBean(org.onebusaway.transit_data.model.service_alerts.SituationAffectsBean) NaturalLanguageStringBean(org.onebusaway.transit_data.model.service_alerts.NaturalLanguageStringBean) ResponseBean(org.onebusaway.api.model.ResponseBean) Alert(com.google.transit.realtime.GtfsRealtime.Alert) ServiceAlertBean(org.onebusaway.transit_data.model.service_alerts.ServiceAlertBean) FeedEntity(com.google.transit.realtime.GtfsRealtime.FeedEntity) Test(org.junit.Test)

Example 4 with TimeRange

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

the class AlertsForAgencyAction method fillFeedMessage.

@Override
protected void fillFeedMessage(FeedMessage.Builder feed, String agencyId, long timestamp) {
    ListBean<ServiceAlertBean> alerts = _service.getAllServiceAlertsForAgencyId(agencyId);
    for (ServiceAlertBean serviceAlert : alerts.getList()) {
        FeedEntity.Builder entity = feed.addEntityBuilder();
        entity.setId(Integer.toString(feed.getEntityCount()));
        Alert.Builder alert = entity.getAlertBuilder();
        fillTranslations(serviceAlert.getSummaries(), alert.getHeaderTextBuilder());
        fillTranslations(serviceAlert.getDescriptions(), alert.getDescriptionTextBuilder());
        if (serviceAlert.getActiveWindows() != null) {
            for (TimeRangeBean range : serviceAlert.getActiveWindows()) {
                TimeRange.Builder timeRange = alert.addActivePeriodBuilder();
                if (range.getFrom() != 0) {
                    timeRange.setStart(range.getFrom() / 1000);
                }
                if (range.getTo() != 0) {
                    timeRange.setEnd(range.getTo() / 1000);
                }
            }
        }
        if (serviceAlert.getAllAffects() != null) {
            for (SituationAffectsBean affects : serviceAlert.getAllAffects()) {
                EntitySelector.Builder entitySelector = alert.addInformedEntityBuilder();
                if (affects.getAgencyId() != null) {
                    entitySelector.setAgencyId(affects.getAgencyId());
                }
                if (affects.getRouteId() != null) {
                    entitySelector.setRouteId(normalizeId(affects.getRouteId()));
                }
                if (affects.getTripId() != null) {
                    TripDescriptor.Builder trip = entitySelector.getTripBuilder();
                    trip.setTripId(normalizeId(affects.getTripId()));
                    entitySelector.setTrip(trip);
                }
                if (affects.getStopId() != null) {
                    AgencyAndId stopId = modifiedStopId(agencyId, affects.getStopId());
                    if (stopId.getAgencyId().equals(agencyId)) {
                        entitySelector.setStopId(normalizeId(stopId.toString()));
                    }
                }
            }
        }
    }
}
Also used : EntitySelector(com.google.transit.realtime.GtfsRealtime.EntitySelector) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) TimeRangeBean(org.onebusaway.transit_data.model.service_alerts.TimeRangeBean) TimeRange(com.google.transit.realtime.GtfsRealtime.TimeRange) SituationAffectsBean(org.onebusaway.transit_data.model.service_alerts.SituationAffectsBean) TripDescriptor(com.google.transit.realtime.GtfsRealtime.TripDescriptor) Alert(com.google.transit.realtime.GtfsRealtime.Alert) ServiceAlertBean(org.onebusaway.transit_data.model.service_alerts.ServiceAlertBean) FeedEntity(com.google.transit.realtime.GtfsRealtime.FeedEntity)

Example 5 with TimeRange

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

Aggregations

EntitySelector (com.google.transit.realtime.GtfsRealtime.EntitySelector)5 TimeRange (com.google.transit.realtime.GtfsRealtime.TimeRange)5 Alert (com.google.transit.realtime.GtfsRealtime.Alert)4 FeedEntity (com.google.transit.realtime.GtfsRealtime.FeedEntity)3 TranslatedString (com.google.transit.realtime.GtfsRealtime.TranslatedString)2 Translation (com.google.transit.realtime.GtfsRealtime.TranslatedString.Translation)2 TripDescriptor (com.google.transit.realtime.GtfsRealtime.TripDescriptor)2 Date (java.util.Date)2 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)2 ServiceAlertBean (org.onebusaway.transit_data.model.service_alerts.ServiceAlertBean)2 SituationAffectsBean (org.onebusaway.transit_data.model.service_alerts.SituationAffectsBean)2 TimeRangeBean (org.onebusaway.transit_data.model.service_alerts.TimeRangeBean)2 FeedMessage (com.google.transit.realtime.GtfsRealtime.FeedMessage)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 ResponseBean (org.onebusaway.api.model.ResponseBean)1 AlertModel (org.onebusaway.gtfs_realtime.model.AlertModel)1 EntitySelectorModel (org.onebusaway.gtfs_realtime.model.EntitySelectorModel)1 TimeRangeModel (org.onebusaway.gtfs_realtime.model.TimeRangeModel)1 ListBean (org.onebusaway.transit_data.model.ListBean)1