use of org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance in project onebusaway-application-modules by camsys.
the class ArrivalAndDepartureAlarmServiceImpl method registerAlarmForArrivalAndDepartureAtStop.
/**
**
* {@link ArrivalAndDepartureAlarmService} Interface
***
*/
@Override
public AgencyAndId registerAlarmForArrivalAndDepartureAtStop(ArrivalAndDepartureQuery query, RegisterAlarmQueryBean alarmBean) {
ArrivalAndDepartureInstance instance = _arrivalAndDepartureService.getArrivalAndDepartureForStop(query);
if (instance == null)
throw new ServiceException("no arrival-departure found");
/**
* We group alarms by block instance
*/
BlockInstance blockInstance = instance.getBlockInstance();
/**
* Retrieve the alarms for the block instance
*/
AlarmsForBlockInstance alarms = getAlarmsForBlockInstance(blockInstance);
/**
* The effective schedule time is the point in the transit vehicle's
* schedule run time when the alarm should be fired
*/
int effectiveScheduleTime = computeEffectiveScheduleTimeForAlarm(alarmBean, instance);
/**
* Create and register the alarm
*/
AlarmAction action = new AlarmAction();
action.setUrl(alarmBean.getUrl());
AlarmForBlockInstance alarm = alarms.registerAlarm(action, effectiveScheduleTime, instance);
_alarmsById.put(alarm.getId(), alarm);
_log.debug("alarm created: {}", alarm.getId());
return alarm.getId();
}
use of org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance in project onebusaway-application-modules by camsys.
the class ArrivalAndDepartureServiceImpl method getArrivalAndDepartureForStop.
@Override
public ArrivalAndDepartureInstance getArrivalAndDepartureForStop(ArrivalAndDepartureQuery query) {
StopEntry stop = query.getStop();
int stopSequence = query.getStopSequence();
TripEntry trip = query.getTrip();
long serviceDate = query.getServiceDate();
AgencyAndId vehicleId = query.getVehicleId();
long time = query.getTime();
Map<BlockInstance, List<BlockLocation>> locationsByInstance = _blockStatusService.getBlocks(trip.getBlock().getId(), serviceDate, vehicleId, time);
if (locationsByInstance.isEmpty())
return null;
Map.Entry<BlockInstance, List<BlockLocation>> entry = locationsByInstance.entrySet().iterator().next();
BlockInstance blockInstance = entry.getKey();
List<BlockLocation> locations = entry.getValue();
int timeOfServiceDate = (int) ((time - serviceDate) / 1000);
ArrivalAndDepartureInstance instance = createArrivalAndDeparture(blockInstance, trip.getId(), stop.getId(), stopSequence, serviceDate, timeOfServiceDate, time);
if (!locations.isEmpty()) {
/**
* What if there are multiple locations? Pick the first?
*/
BlockLocation location = locations.get(0);
applyBlockLocationToInstance(instance, location, time);
}
return instance;
}
use of org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance in project onebusaway-application-modules by camsys.
the class ArrivalAndDepartureServiceImpl method applyPostInterpolateForFrequencyNoSchedule.
private void applyPostInterpolateForFrequencyNoSchedule(StopTimeInstance sti, long fromTime, long toTime, long frequencyOffsetTime, BlockInstance blockInstance, List<ArrivalAndDepartureInstance> results) {
if (results == null || results.size() == 0)
return;
// Find latest instance. Prefer realtime.
ArrivalAndDepartureInstance instance = findBestArrivalAndDepartureInstance(results);
// If no realtime data, don't make extrapolations.
if (instance.getBlockLocation() == null || !instance.getBlockLocation().isPredicted())
return;
BlockStopTimeEntry bst = sti.getStopTime();
// See similar calculation in
// FrequencyBlockStopTimeEntry.getStopTimeOffset()
int d0 = bst.getTrip().getDepartureTimeForIndex(0);
int d1 = bst.getStopTime().getDepartureTime();
int stopDelta = d1 - d0;
int stopEndTime = sti.getFrequency().getEndTime() + stopDelta;
long stopEndTimeExact = sti.getServiceDate() + stopEndTime * 1000;
int headwayMs = sti.getFrequency().getHeadwaySecs() * 1000;
/*
* TODO: if start_time is available from feed we should use it over this calculation
*/
long time = instance.getBestDepartureTime();
if (time == 0)
time = instance.getBestArrivalTime();
// Do not extrapolate trips starting at the headway change:
stopEndTimeExact -= headwayMs;
// Extrapolate future stop times.
while ((time += headwayMs) < Math.min(toTime, stopEndTimeExact)) {
ArrivalAndDepartureInstance newInstance = createArrivalAndDepartureForStopTimeInstanceWithTime(sti, time);
results.add(newInstance);
}
}
use of org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance in project onebusaway-application-modules by camsys.
the class ArrivalAndDepartureServiceImpl method applyRealTimeToStopTimeInstance.
/*
* here we map realtime on top of schedule and also filter
* out canceled trips.
*/
private void applyRealTimeToStopTimeInstance(StopTimeInstance sti, TargetTime targetTime, long fromTime, long toTime, long frequencyOffsetTime, BlockInstance blockInstance, List<BlockLocation> locations, List<ArrivalAndDepartureInstance> results) {
for (BlockLocation location : locations) {
if (sti.isFrequencyOffsetSpecified() && ((blockInstance.getBlock().getDepartureTimeForIndex(0) + sti.getFrequencyOffset()) != location.getBlockStartTime())) {
continue;
}
if (TransitDataConstants.STATUS_CANCELED.equals(location.getStatus())) {
continue;
}
ArrivalAndDepartureInstance instance = createArrivalAndDepartureForStopTimeInstance(sti, frequencyOffsetTime);
applyBlockLocationToInstance(instance, location, targetTime.getTargetTime());
if (isArrivalAndDepartureBeanInRange(instance, fromTime, toTime))
results.add(instance);
}
if (locations.isEmpty()) {
ArrivalAndDepartureInstance instance = createArrivalAndDepartureForStopTimeInstance(sti, frequencyOffsetTime);
if (sti.getFrequency() == null) {
/**
* We don't need to get the scheduled location of a vehicle unless its
* in our arrival window
*/
if (isArrivalAndDepartureBeanInRange(instance, fromTime, toTime)) {
BlockLocation scheduledLocation = _blockLocationService.getScheduledLocationForBlockInstance(blockInstance, targetTime.getTargetTime());
if (scheduledLocation != null)
applyBlockLocationToInstance(instance, scheduledLocation, targetTime.getTargetTime());
results.add(instance);
}
} else {
if (isFrequencyBasedArrivalInRange(blockInstance, sti.getFrequency(), fromTime, toTime)) {
results.add(instance);
}
}
}
}
use of org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance in project onebusaway-application-modules by camsys.
the class ArrivalAndDepartureServiceImpl method getArrivalsAndDeparturesForStopInTimeRange.
@Override
public List<ArrivalAndDepartureInstance> getArrivalsAndDeparturesForStopInTimeRange(StopEntry stop, TargetTime targetTime, long fromTime, long toTime) {
// We add a buffer before and after to catch late and early buses
Date fromTimeBuffered = new Date(fromTime - _blockStatusService.getRunningLateWindow() * 1000);
Date toTimeBuffered = new Date(toTime + _blockStatusService.getRunningEarlyWindow() * 1000);
List<StopTimeInstance> stis = _stopTimeService.getStopTimeInstancesInTimeRange(stop, fromTimeBuffered, toTimeBuffered, EFrequencyStopTimeBehavior.INCLUDE_UNSPECIFIED);
long frequencyOffsetTime = Math.max(targetTime.getTargetTime(), fromTime);
Map<BlockInstance, List<StopTimeInstance>> stisByBlockId = getStopTimeInstancesByBlockInstance(stis);
List<ArrivalAndDepartureInstance> instances = new ArrayList<ArrivalAndDepartureInstance>();
for (Map.Entry<BlockInstance, List<StopTimeInstance>> entry : stisByBlockId.entrySet()) {
BlockInstance blockInstance = entry.getKey();
List<BlockLocation> locations = _blockLocationService.getLocationsForBlockInstance(blockInstance, targetTime);
List<StopTimeInstance> stisForBlock = entry.getValue();
for (StopTimeInstance sti : stisForBlock) {
applyRealTimeToStopTimeInstance(sti, targetTime, fromTime, toTime, frequencyOffsetTime, blockInstance, locations, instances);
if (sti.getFrequency() != null && sti.getFrequency().getExactTimes() == 0) {
/*
* adjust following schedule times relative to current realtime data
*/
applyPostInterpolateForFrequencyNoSchedule(sti, fromTime, toTime, frequencyOffsetTime, blockInstance, instances);
}
}
}
if (removeFuturePredictionsWithoutRealtime) {
List<ArrivalAndDepartureInstance> filteredInstances = new ArrayList<ArrivalAndDepartureInstance>();
for (ArrivalAndDepartureInstance instance : instances) {
FrequencyEntry entry = instance.getFrequency();
boolean toAdd = // not a frequency-based instance
(entry == null) || // frequency interval has started
(instance.getServiceDate() + (entry.getStartTime() * 1000) < targetTime.getTargetTime()) || // instance has realtime data
(instance.getBlockLocation() != null && instance.getBlockLocation().isPredicted());
if (toAdd)
filteredInstances.add(instance);
}
return filteredInstances;
}
return instances;
}
Aggregations