use of org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements in project onebusaway-application-modules by camsys.
the class BlockLocationServiceImpl method getBlockLocationRecordsAsVehicleLocationRecords.
private List<VehicleLocationCacheElements> getBlockLocationRecordsAsVehicleLocationRecords(BlockInstance blockInstance, List<BlockLocationRecord> records) {
List<VehicleLocationCacheElements> results = new ArrayList<VehicleLocationCacheElements>();
for (BlockLocationRecord record : records) {
VehicleLocationRecord vlr = new VehicleLocationRecord();
vlr.setBlockId(blockInstance.getBlock().getBlock().getId());
if (record.isLocationSet()) {
vlr.setCurrentLocationLat(record.getLocationLat());
vlr.setCurrentLocationLon(record.getLocationLon());
}
if (record.isOrientationSet())
vlr.setCurrentOrientation(record.getOrientation());
if (record.isDistanceAlongBlockSet())
vlr.setDistanceAlongBlock(record.getDistanceAlongBlock());
vlr.setPhase(record.getPhase());
if (record.isScheduleDeviationSet())
vlr.setScheduleDeviation(record.getScheduleDeviation());
vlr.setServiceDate(record.getServiceDate());
vlr.setStatus(record.getStatus());
vlr.setTimeOfRecord(record.getTime());
vlr.setVehicleId(record.getVehicleId());
VehicleLocationCacheElement element = new VehicleLocationCacheElement(vlr, null, null);
VehicleLocationCacheElements elements = new VehicleLocationCacheElements(blockInstance, element);
results.add(elements);
}
return results;
}
use of org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements in project onebusaway-application-modules by camsys.
the class BlockLocationServiceImpl method getBlockLocationRecordCollections.
private List<VehicleLocationCacheElements> getBlockLocationRecordCollections(RecordStrategy strategy, TargetTime time) {
List<VehicleLocationCacheElements> entries = strategy.getRecordsFromCache();
if (!entries.isEmpty()) {
List<VehicleLocationCacheElements> inRange = new ArrayList<VehicleLocationCacheElements>();
long offset = _predictionCacheMaxOffset * 1000;
for (VehicleLocationCacheElements elements : entries) {
if (elements.isEmpty())
continue;
Range range = elements.getTimeRange();
long tFrom = (long) (range.getMin() - offset);
long tTo = (long) (range.getMax() + offset);
if (tFrom <= time.getCurrentTime() && time.getCurrentTime() <= tTo)
inRange.add(elements);
}
if (!inRange.isEmpty())
return inRange;
}
long offset = _blockLocationRecordCacheWindowSize * 1000 / 2;
// We only consult persisted cache entries if the requested target time is
// not within our current cache window
boolean outOfRange = time.getTargetTime() + offset < time.getCurrentTime() || time.getCurrentTime() < time.getTargetTime() - offset;
if (outOfRange && _persistBlockLocationRecords) {
_blockLocationRecordPersistentStoreAccessCount.incrementAndGet();
long fromTime = time.getTargetTime() - offset;
long toTime = time.getTargetTime() + offset;
List<BlockLocationRecord> predictions = strategy.getRecordsFromDao(fromTime, toTime);
if (!predictions.isEmpty()) {
Map<BlockLocationRecordKey, List<BlockLocationRecord>> recordsByKey = groupRecord(predictions);
List<VehicleLocationCacheElements> allCollections = new ArrayList<VehicleLocationCacheElements>();
for (Map.Entry<BlockLocationRecordKey, List<BlockLocationRecord>> entry : recordsByKey.entrySet()) {
BlockLocationRecordKey key = entry.getKey();
List<BlockLocationRecord> blockLocationRecords = entry.getValue();
List<VehicleLocationCacheElements> someRecords = getBlockLocationRecordsAsVehicleLocationRecords(key.getBlockInstance(), blockLocationRecords);
allCollections.addAll(someRecords);
}
return allCollections;
}
}
return Collections.emptyList();
}
use of org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements in project onebusaway-application-modules by camsys.
the class BlockLocationServiceImpl method putBlockLocationRecord.
/**
* We add the {@link BlockInstance} to the local cache and persist it to
* a back-end data-store if necessary
*
* @param scheduledBlockLocation TODO
* @param samples
*/
private void putBlockLocationRecord(BlockInstance blockInstance, VehicleLocationRecord record, ScheduledBlockLocation scheduledBlockLocation, ScheduleDeviationSamples samples) {
// Cache the result
VehicleLocationCacheElements elements = _cache.addRecord(blockInstance, record, scheduledBlockLocation, samples);
if (!CollectionsLibrary.isEmpty(_blockLocationListeners)) {
/**
* We only fill in the block location if we have listeners
*/
BlockLocation location = getBlockLocation(blockInstance, elements, scheduledBlockLocation, record.getTimeOfRecord());
if (location != null) {
for (BlockLocationListener listener : _blockLocationListeners) {
listener.handleBlockLocation(location);
}
}
}
if (_persistBlockLocationRecords) {
List<BlockLocationRecord> blockLocationRecords = getVehicleLocationRecordAsBlockLocationRecord(blockInstance, record, scheduledBlockLocation);
addPredictionToPersistenceQueue(blockLocationRecords);
}
}
use of org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements in project onebusaway-application-modules by camsys.
the class BlockLocationServiceImpl method getLocationsForBlockInstance.
@Override
public Map<AgencyAndId, List<BlockLocation>> getLocationsForBlockInstance(BlockInstance blockInstance, List<Date> times, long currentTime) {
Map<AgencyAndId, List<BlockLocation>> locationsByVehicleId = new FactoryMap<AgencyAndId, List<BlockLocation>>(new ArrayList<BlockLocation>());
for (Date time : times) {
TargetTime tt = new TargetTime(time.getTime(), currentTime);
List<VehicleLocationCacheElements> records = getBlockLocationRecordCollectionForBlock(blockInstance, tt);
for (VehicleLocationCacheElements cacheRecord : records) {
BlockLocation location = getBlockLocation(blockInstance, cacheRecord, null, time.getTime());
if (location != null) {
locationsByVehicleId.get(location.getVehicleId()).add(location);
}
}
}
return locationsByVehicleId;
}
use of org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements in project onebusaway-application-modules by camsys.
the class BlockLocationServiceImpl method getLocationsForBlockInstance.
@Override
public List<BlockLocation> getLocationsForBlockInstance(BlockInstance blockInstance, TargetTime time) {
List<VehicleLocationCacheElements> records = getBlockLocationRecordCollectionForBlock(blockInstance, time);
List<BlockLocation> locations = new ArrayList<BlockLocation>();
for (VehicleLocationCacheElements cacheRecord : records) {
BlockLocation location = getBlockLocation(blockInstance, cacheRecord, null, time.getTargetTime());
if (location != null)
locations.add(location);
}
return locations;
}
Aggregations