Synchonised access to the RssiLog

This commit is contained in:
Alexandros Schillings
2014-04-24 15:45:29 +01:00
parent e8db1685d8
commit e79b3f8c3d
2 changed files with 35 additions and 25 deletions
@@ -71,8 +71,7 @@ public class BluetoothLeDevice implements Parcelable{
mFirstTimestamp = timestamp; mFirstTimestamp = timestamp;
mRecordStore = new AdRecordStore(AdRecordUtils.parseScanRecordAsSparseArray(scanRecord)); mRecordStore = new AdRecordStore(AdRecordUtils.parseScanRecordAsSparseArray(scanRecord));
mScanRecord = scanRecord; mScanRecord = scanRecord;
mRssiLog = Collections.synchronizedMap( mRssiLog = new LimitedLinkHashMap<Long, Integer>(MAX_RSSI_LOG_SIZE);
new LimitedLinkHashMap<Long, Integer>(MAX_RSSI_LOG_SIZE));
updateRssiReading(timestamp, rssi); updateRssiReading(timestamp, rssi);
} }
@@ -108,8 +107,7 @@ public class BluetoothLeDevice implements Parcelable{
mFirstRssi = b.getInt(PARCEL_EXTRA_FIRST_RSSI, 0); mFirstRssi = b.getInt(PARCEL_EXTRA_FIRST_RSSI, 0);
mFirstTimestamp = b.getLong(PARCEL_EXTRA_FIRST_TIMESTAMP, 0); mFirstTimestamp = b.getLong(PARCEL_EXTRA_FIRST_TIMESTAMP, 0);
mRecordStore = b.getParcelable(PARCEL_EXTRA_DEVICE_SCANRECORD_STORE); mRecordStore = b.getParcelable(PARCEL_EXTRA_DEVICE_SCANRECORD_STORE);
mRssiLog = Collections.synchronizedMap( mRssiLog = (Map<Long, Integer>) b.getSerializable(PARCEL_EXTRA_DEVICE_RSSI_LOG);
(Map<Long, Integer>) b.getSerializable(PARCEL_EXTRA_DEVICE_RSSI_LOG));
mScanRecord = b.getByteArray(PARCEL_EXTRA_DEVICE_SCANRECORD); mScanRecord = b.getByteArray(PARCEL_EXTRA_DEVICE_SCANRECORD);
} }
@@ -120,6 +118,7 @@ public class BluetoothLeDevice implements Parcelable{
* @param rssiReading the rssi reading * @param rssiReading the rssi reading
*/ */
private void addToRssiLog(long timestamp, int rssiReading){ private void addToRssiLog(long timestamp, int rssiReading){
synchronized (mRssiLog) {
if(timestamp - mCurrentTimestamp > LOG_INVALIDATION_THRESHOLD){ if(timestamp - mCurrentTimestamp > LOG_INVALIDATION_THRESHOLD){
mRssiLog.clear(); mRssiLog.clear();
} }
@@ -128,6 +127,7 @@ public class BluetoothLeDevice implements Parcelable{
mCurrentTimestamp = timestamp; mCurrentTimestamp = timestamp;
mRssiLog.put(timestamp, rssiReading); mRssiLog.put(timestamp, rssiReading);
} }
}
/* (non-Javadoc) /* (non-Javadoc)
* @see android.os.Parcelable#describeContents() * @see android.os.Parcelable#describeContents()
@@ -264,8 +264,10 @@ public class BluetoothLeDevice implements Parcelable{
* @return the rssi log * @return the rssi log
*/ */
protected Map<Long, Integer> getRssiLog() { protected Map<Long, Integer> getRssiLog() {
synchronized (mRssiLog) {
return mRssiLog; return mRssiLog;
} }
}
/** /**
* Gets the running average rssi. * Gets the running average rssi.
@@ -275,18 +277,26 @@ public class BluetoothLeDevice implements Parcelable{
public double getRunningAverageRssi(){ public double getRunningAverageRssi(){
int sum = 0; int sum = 0;
int count = 0; int count = 0;
synchronized (mRssiLog) {
final Iterator<Long> it1 = mRssiLog.keySet().iterator(); final Iterator<Long> it1 = mRssiLog.keySet().iterator();
while(it1.hasNext()){ while(it1.hasNext()){
count ++; count ++;
sum += mRssiLog.get(it1.next()); sum += mRssiLog.get(it1.next());
} }
}
// for(final Map.Entry<Long,Integer> e : mRssiLog.entrySet()){
// count ++;
// sum += e.getValue();
// }
if(count > 0){ if(count > 0){
return sum/count; return sum/count;
} else { } else {
return 0; return 0;
} }
} }
/** /**
@@ -339,7 +349,7 @@ public class BluetoothLeDevice implements Parcelable{
* @param timestamp the timestamp * @param timestamp the timestamp
* @param rssiReading the rssi reading * @param rssiReading the rssi reading
*/ */
public synchronized void updateRssiReading(long timestamp, int rssiReading){ public void updateRssiReading(long timestamp, int rssiReading){
addToRssiLog(timestamp, rssiReading); addToRssiLog(timestamp, rssiReading);
} }
@@ -7,14 +7,14 @@ public class LimitedLinkHashMap<K, V> extends LinkedHashMap<K, V>{
private static final long serialVersionUID = -5375660288461724925L; private static final long serialVersionUID = -5375660288461724925L;
private final int mMaxSize; private final int mMaxSize;
public LimitedLinkHashMap(int maxSize){ public LimitedLinkHashMap(int maxSize){
super(); super(maxSize + 1, 1, false);
mMaxSize = maxSize; mMaxSize = maxSize;
} }
@Override @Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
{
return this.size() > mMaxSize; return this.size() > mMaxSize;
} }
} }