use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance 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;
}
use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.
the class ArrivalAndDepartureServiceImpl method getScheduledArrivalsAndDeparturesForStopInTimeRange.
@Override
public List<ArrivalAndDepartureInstance> getScheduledArrivalsAndDeparturesForStopInTimeRange(StopEntry stop, long currentTime, long fromTime, long toTime) {
List<StopTimeInstance> stis = _stopTimeService.getStopTimeInstancesInTimeRange(stop, new Date(fromTime), new Date(toTime), EFrequencyStopTimeBehavior.INCLUDE_UNSPECIFIED);
List<ArrivalAndDepartureInstance> instances = new ArrayList<ArrivalAndDepartureInstance>();
long prevFrequencyTime = Math.max(currentTime, fromTime);
for (StopTimeInstance sti : stis) {
BlockInstance blockInstance = sti.getBlockInstance();
ArrivalAndDepartureInstance instance = createArrivalAndDepartureForStopTimeInstance(sti, prevFrequencyTime);
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, currentTime);
if (scheduledLocation != null)
applyBlockLocationToInstance(instance, scheduledLocation, currentTime);
instances.add(instance);
}
} else {
if (isFrequencyBasedArrivalInRange(blockInstance, sti.getFrequency(), fromTime, toTime)) {
instances.add(instance);
}
}
}
return instances;
}
use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.
the class ArrivalAndDepartureServiceImpl method getStopTimeInstancesByBlockInstance.
private Map<BlockInstance, List<StopTimeInstance>> getStopTimeInstancesByBlockInstance(List<StopTimeInstance> stopTimes) {
Map<BlockInstance, List<StopTimeInstance>> r = new FactoryMap<BlockInstance, List<StopTimeInstance>>(new ArrayList<StopTimeInstance>());
for (StopTimeInstance stopTime : stopTimes) {
BlockStopTimeEntry blockStopTime = stopTime.getStopTime();
BlockTripEntry blockTrip = blockStopTime.getTrip();
BlockConfigurationEntry blockConfiguration = blockTrip.getBlockConfiguration();
long serviceDate = stopTime.getServiceDate();
BlockInstance blockInstance = new BlockInstance(blockConfiguration, serviceDate, stopTime.getFrequency());
r.get(blockInstance).add(stopTime);
}
return r;
}
use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.
the class SimulateScheduledVehicleLocationsController method index.
@RequestMapping()
public ModelAndView index(@RequestParam() String blockId, @RequestParam() long serviceDate, @RequestParam(defaultValue = "0") int scheduleDeviation, @RequestParam(defaultValue = "0.0") double noise) {
AgencyAndId bid = AgencyAndIdLibrary.convertFromString(blockId);
BlockInstance blockInstance = _blockCalendarService.getBlockInstance(bid, serviceDate);
CurrentVehicleEstimateQueryBean bean = new CurrentVehicleEstimateQueryBean();
long time = SystemTime.currentTimeMillis();
List<Record> records = new ArrayList<Record>();
for (int i = 0; i < 5 * 60; i += 30) {
int scheduleTime = (int) ((time - blockInstance.getServiceDate()) / 1000 - scheduleDeviation - i);
ScheduledBlockLocation location = _scheduledBlockLocationService.getScheduledBlockLocationFromScheduledTime(blockInstance.getBlock(), scheduleTime);
if (location != null) {
CoordinatePoint p = applyNoiseToLocation(location.getLocation(), noise);
Record r = new Record();
r.setLocation(location.getLocation());
r.setTimestamp(time - i * 1000);
r.setLocation(p);
r.setAccuracy(noise);
records.add(r);
}
}
bean.setRecords(records);
ModelAndView mv = new ModelAndView("simulate-vehicle-locations.jspx");
mv.addObject("time", time);
mv.addObject("query", bean);
return mv;
}
use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.
the class SiriService method handleVehicleMonitoring.
/**
**
* Private Methods
***
*/
private void handleVehicleMonitoring(ServiceDelivery serviceDelivery, VehicleMonitoringDeliveryStructure deliveryForModule, SiriEndpointDetails endpointDetails) {
List<VehicleLocationRecord> records = new ArrayList<VehicleLocationRecord>();
Date now = new Date();
long timeFrom = now.getTime() - _blockInstanceSearchWindow * 60 * 1000;
long timeTo = now.getTime() + _blockInstanceSearchWindow * 60 * 1000;
for (VehicleActivityStructure vehicleActivity : deliveryForModule.getVehicleActivity()) {
Date time = vehicleActivity.getRecordedAtTime();
if (time == null)
time = now;
MonitoredVehicleJourney mvj = vehicleActivity.getMonitoredVehicleJourney();
Duration delay = mvj.getDelay();
if (delay == null)
continue;
VehicleRefStructure vehicleRef = mvj.getVehicleRef();
if (vehicleRef == null || vehicleRef.getValue() == null)
continue;
BlockEntry block = getBlockForMonitoredVehicleJourney(mvj, endpointDetails);
if (block == null) {
TripEntry trip = getTripForMonitoredVehicleJourney(mvj, endpointDetails);
if (trip != null)
block = trip.getBlock();
}
if (block == null)
continue;
List<BlockInstance> instances = _blockCalendarService.getActiveBlocks(block.getId(), timeFrom, timeTo);
// TODO : We currently assume that a block won't overlap with itself
if (instances.size() != 1)
continue;
BlockInstance instance = instances.get(0);
VehicleLocationRecord r = new VehicleLocationRecord();
r.setTimeOfRecord(time.getTime());
r.setServiceDate(instance.getServiceDate());
r.setBlockId(block.getId());
String agencyId = block.getId().getAgencyId();
r.setVehicleId(new AgencyAndId(agencyId, vehicleRef.getValue()));
r.setScheduleDeviation(delay.getTimeInMillis(now) / 1000);
LocationStructure location = mvj.getVehicleLocation();
if (location != null) {
r.setCurrentLocationLat(location.getLatitude().doubleValue());
r.setCurrentLocationLon(location.getLongitude().doubleValue());
}
records.add(r);
}
if (!records.isEmpty())
_vehicleLocationListener.handleVehicleLocationRecords(records);
}
Aggregations