mirror of
https://github.com/home-assistant/android
synced 2024-09-19 16:11:37 +00:00
Discard duplicate locations coming within seconds of each other (#888)
* Discard duplicate locations coming within seconds of each other * Keep identical update timeout to 15 minutes. Co-authored-by: Justin Bassett <bassett.justint@gmail.com>
This commit is contained in:
parent
f9f1f6f0d6
commit
2de7ddab3e
|
@ -59,6 +59,9 @@ class LocationSensorManager : BroadcastReceiver(), SensorManager {
|
||||||
|
|
||||||
private var isBackgroundLocationSetup = false
|
private var isBackgroundLocationSetup = false
|
||||||
private var isZoneLocationSetup = false
|
private var isZoneLocationSetup = false
|
||||||
|
|
||||||
|
private var lastLocationSend = 0L
|
||||||
|
private var lastUpdateLocation = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
|
@ -223,33 +226,20 @@ class LocationSensorManager : BroadcastReceiver(), SensorManager {
|
||||||
if (Build.VERSION.SDK_INT >= 26) location.verticalAccuracyMeters.toInt() else 0
|
if (Build.VERSION.SDK_INT >= 26) location.verticalAccuracyMeters.toInt() else 0
|
||||||
)
|
)
|
||||||
|
|
||||||
val sensorDao = AppDatabase.getInstance(latestContext).sensorDao()
|
|
||||||
val fullSensor = sensorDao.getFull(backgroundLocation.id)
|
|
||||||
val locationEntity = fullSensor?.sensor
|
|
||||||
val lastLocationSend = fullSensor?.attributes?.firstOrNull { it.name == "lastLocationSent" }?.value?.toLongOrNull() ?: 0L
|
|
||||||
val now = System.currentTimeMillis()
|
val now = System.currentTimeMillis()
|
||||||
|
|
||||||
if (locationEntity?.state == updateLocation.gps.contentToString()) {
|
if (lastUpdateLocation == updateLocation.gps.contentToString()) {
|
||||||
if (now >= lastLocationSend + 900000) {
|
if (now < lastLocationSend + 900000) {
|
||||||
Log.d(TAG, "Sending location since it's been more than 15 minutes")
|
Log.d(TAG, "Duplicate location received, not sending to HA")
|
||||||
} else {
|
|
||||||
Log.d(TAG, "Same location as last update, not sending to HA")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
lastLocationSend = now
|
||||||
|
lastUpdateLocation = updateLocation.gps.contentToString()
|
||||||
|
|
||||||
ioScope.launch {
|
ioScope.launch {
|
||||||
try {
|
try {
|
||||||
integrationUseCase.updateLocation(updateLocation)
|
integrationUseCase.updateLocation(updateLocation)
|
||||||
onSensorUpdated(
|
|
||||||
latestContext,
|
|
||||||
backgroundLocation,
|
|
||||||
updateLocation.gps.contentToString(),
|
|
||||||
"",
|
|
||||||
mapOf(
|
|
||||||
"lastLocationSent" to now.toString()
|
|
||||||
)
|
|
||||||
)
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e(TAG, "Could not update location.", e)
|
Log.e(TAG, "Could not update location.", e)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue