diff --git a/app/src/main/java/io/homeassistant/companion/android/sensors/LastRebootSensorManager.kt b/app/src/main/java/io/homeassistant/companion/android/sensors/LastRebootSensorManager.kt index 85ba7ff0a..2af214363 100644 --- a/app/src/main/java/io/homeassistant/companion/android/sensors/LastRebootSensorManager.kt +++ b/app/src/main/java/io/homeassistant/companion/android/sensors/LastRebootSensorManager.kt @@ -4,15 +4,21 @@ import android.content.Context import android.os.SystemClock import android.util.Log import io.homeassistant.companion.android.R +import io.homeassistant.companion.android.database.AppDatabase +import io.homeassistant.companion.android.database.sensor.Setting import java.text.SimpleDateFormat import java.util.Calendar import java.util.Date import java.util.GregorianCalendar import java.util.TimeZone +import kotlin.math.absoluteValue class LastRebootSensorManager : SensorManager { companion object { private const val TAG = "LastReboot" + private const val LOCAL_TIME = "Local Time" + private const val DEADBAND = "deadband" + private const val TIME_MILLISECONDS = "Time in Milliseconds" private val lastRebootSensor = SensorManager.BasicSensor( "last_reboot", @@ -49,8 +55,17 @@ class LastRebootSensorManager : SensorManager { var local = "" var utc = "unavailable" + val sensorDao = AppDatabase.getInstance(context).sensorDao() + val fullSensor = sensorDao.getFull(lastRebootSensor.id) + val sensorSetting = sensorDao.getSettings(lastRebootSensor.id) + val lastTimeMillis = fullSensor?.attributes?.firstOrNull { it.name == TIME_MILLISECONDS }?.value?.toLongOrNull() ?: 0L + val settingDeadband = sensorSetting?.firstOrNull { it.name == DEADBAND }?.value?.toIntOrNull() ?: 60000 + sensorDao.add(Setting(lastRebootSensor.id, DEADBAND, settingDeadband.toString(), "number")) try { timeInMillis = System.currentTimeMillis() - SystemClock.elapsedRealtime() + val diffMillis = (timeInMillis - lastTimeMillis).absoluteValue + if (lastTimeMillis != 0L && settingDeadband > diffMillis) + return val cal: Calendar = GregorianCalendar() cal.timeInMillis = timeInMillis local = cal.time.toString() @@ -71,8 +86,8 @@ class LastRebootSensorManager : SensorManager { utc, icon, mapOf( - "Local Time" to local, - "Time in Milliseconds" to timeInMillis + LOCAL_TIME to local, + TIME_MILLISECONDS to timeInMillis ) ) } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 84f18cb48..a9e0cc54b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -186,7 +186,7 @@ like to connect to: Whether or not headphones are plugged into the device Whether or not the device is in an interactive state Information about the total and available storage space internally - The date and time of the devices last reboot + The date and time of the devices last reboot. The setting below will allow you to adjust the deadband in milliseconds, if you still find the value to jump incorrectly. The default value is 60000 (1 minute). The current level of illuminance Update your location behind the scenes, periodically. Import existing Home Assistant zones as geofences for zone based tracking.