use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.
the class BlockCalendarServiceImpl method getClosestActiveBlocks.
@Override
public List<BlockInstance> getClosestActiveBlocks(AgencyAndId blockId, long time) {
Date timeAsDate = new Date(time);
Min<BlockInstance> m = new Min<BlockInstance>();
BlockEntry blockEntry = _transitGraphDao.getBlockEntryForId(blockId);
for (BlockConfigurationEntry blockConfig : blockEntry.getConfigurations()) {
List<Date> serviceDates = _calendarService.getDatesForServiceIdsAsOrderedList(blockConfig.getServiceIds());
int index = index(Collections.binarySearch(serviceDates, timeAsDate));
if (index > 0) {
BlockInstance instance = new BlockInstance(blockConfig, serviceDates.get(index - 1).getTime());
long delta = getTimeToBlockInstance(instance, time);
m.add(delta, instance);
}
if (index < serviceDates.size()) {
BlockInstance instance = new BlockInstance(blockConfig, serviceDates.get(index).getTime());
long delta = getTimeToBlockInstance(instance, time);
m.add(delta, instance);
}
}
return m.getMinElements();
}
use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.
the class BlockCalendarServiceImpl method findBlockLayoversInRange.
private void findBlockLayoversInRange(LayoverIntervalBlock intervals, Date serviceDate, Date timeFrom, Date timeTo, List<BlockTripEntry> trips, Collection<BlockInstance> instances) {
int scheduledTimeFrom = (int) ((timeFrom.getTime() - serviceDate.getTime()) / 1000);
int scheduledTimeTo = (int) ((timeTo.getTime() - serviceDate.getTime()) / 1000);
int indexFrom = index(Arrays.binarySearch(intervals.getEndTimes(), scheduledTimeFrom));
int indexTo = index(Arrays.binarySearch(intervals.getStartTimes(), scheduledTimeTo));
InstanceState state = new InstanceState(serviceDate.getTime());
for (int in = indexFrom; in < indexTo; in++) {
BlockTripEntry trip = trips.get(in);
BlockConfigurationEntry block = trip.getBlockConfiguration();
BlockInstance instance = new BlockInstance(block, state);
instances.add(instance);
}
}
use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.
the class BlockLocationServiceImpl method groupRecord.
private Map<BlockLocationRecordKey, List<BlockLocationRecord>> groupRecord(List<BlockLocationRecord> predictions) {
Map<BlockLocationRecordKey, List<BlockLocationRecord>> recordsByKey = new FactoryMap<BlockLocationRecordKey, List<BlockLocationRecord>>(new ArrayList<BlockLocationRecord>());
for (BlockLocationRecord record : predictions) {
AgencyAndId blockId = record.getBlockId();
long serviceDate = record.getServiceDate();
BlockInstance blockInstance = _blockCalendarService.getBlockInstance(blockId, serviceDate);
if (blockInstance != null) {
BlockLocationRecordKey key = new BlockLocationRecordKey(blockInstance, record.getVehicleId());
recordsByKey.get(key).add(record);
}
}
return recordsByKey;
}
use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.
the class BlockLocationServiceImpl method handleVehicleLocationRecord.
/**
**
* {@link BlockVehicleLocationListener} Interface
***
*/
@Override
public void handleVehicleLocationRecord(VehicleLocationRecord record) {
BlockInstance instance = getVehicleLocationRecordAsBlockInstance(record);
if (instance != null) {
ScheduledBlockLocation scheduledBlockLocation = getScheduledBlockLocationForVehicleLocationRecord(record, instance);
if (!record.isScheduleDeviationSet()) {
int deviation = (int) ((record.getTimeOfRecord() - record.getServiceDate()) / 1000 - scheduledBlockLocation.getScheduledTime());
record.setScheduleDeviation(deviation);
}
ScheduleDeviationSamples samples = null;
if (_sampleScheduleDeviationHistory == true) {
samples = _realTimeHistoryService.sampleScheduleDeviationsForVehicle(instance, record, scheduledBlockLocation);
}
putBlockLocationRecord(instance, record, scheduledBlockLocation, samples);
}
}
use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.
the class BlockLocationServiceImpl method getBestBlockForRecord.
private BlockInstance getBestBlockForRecord(AgencyAndId blockId, long serviceDate, long timeOfRecord) {
long timeFrom = timeOfRecord - _blockInstanceMatchingWindow;
long timeTo = timeOfRecord + _blockInstanceMatchingWindow;
List<BlockInstance> blocks = _blockCalendarService.getActiveBlocks(blockId, timeFrom, timeTo);
if (blocks.isEmpty())
return null;
else if (blocks.size() == 1)
return blocks.get(0);
Min<BlockInstance> m = new Min<BlockInstance>();
for (BlockInstance block : blocks) {
long delta = Math.abs(block.getServiceDate() - serviceDate);
m.add(delta, block);
}
return m.getMinElement();
}
Aggregations