From 2de7ddab3e57beb122b6679893ff229b783c90de Mon Sep 17 00:00:00 2001 From: Anna Tikhomirova Date: Tue, 8 Sep 2020 15:57:48 +0300 Subject: [PATCH] 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 --- .../android/sensors/LocationSensorManager.kt | 26 ++++++------------- 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/app/src/full/java/io/homeassistant/companion/android/sensors/LocationSensorManager.kt b/app/src/full/java/io/homeassistant/companion/android/sensors/LocationSensorManager.kt index e9ec5fd22..46577d588 100644 --- a/app/src/full/java/io/homeassistant/companion/android/sensors/LocationSensorManager.kt +++ b/app/src/full/java/io/homeassistant/companion/android/sensors/LocationSensorManager.kt @@ -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) }