mirror of
https://github.com/home-assistant/android
synced 2024-09-18 23:52:51 +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 isZoneLocationSetup = false
|
||||
|
||||
private var lastLocationSend = 0L
|
||||
private var lastUpdateLocation = ""
|
||||
}
|
||||
|
||||
@Inject
|
||||
|
@ -223,33 +226,20 @@ class LocationSensorManager : BroadcastReceiver(), SensorManager {
|
|||
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()
|
||||
|
||||
if (locationEntity?.state == updateLocation.gps.contentToString()) {
|
||||
if (now >= lastLocationSend + 900000) {
|
||||
Log.d(TAG, "Sending location since it's been more than 15 minutes")
|
||||
} else {
|
||||
Log.d(TAG, "Same location as last update, not sending to HA")
|
||||
if (lastUpdateLocation == updateLocation.gps.contentToString()) {
|
||||
if (now < lastLocationSend + 900000) {
|
||||
Log.d(TAG, "Duplicate location received, not sending to HA")
|
||||
return
|
||||
}
|
||||
}
|
||||
lastLocationSend = now
|
||||
lastUpdateLocation = updateLocation.gps.contentToString()
|
||||
|
||||
ioScope.launch {
|
||||
try {
|
||||
integrationUseCase.updateLocation(updateLocation)
|
||||
onSensorUpdated(
|
||||
latestContext,
|
||||
backgroundLocation,
|
||||
updateLocation.gps.contentToString(),
|
||||
"",
|
||||
mapOf(
|
||||
"lastLocationSent" to now.toString()
|
||||
)
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Could not update location.", e)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue