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;
}
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;
}
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);
}
}
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();
}
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.
}
}
}
Aggregations